0

I have just spent hours on this. I'm trying to make a pretty complicated query in PostgreSQL through Slick in Scala, but Slick will NEVER take any of my parameters into account. If I try something as simple as:

def get(location: String) = {
  val query = sql"select * from cities_v where name = $location limit 10"
  println(query.toString)
}

The output will be:

SQLActionBuilder(Vector(select * from cities_v where name = ? limit 10),<function2>)

If instead, I try the literal insert:

def get(location: String) = {
  val query = sql"select * from cities_v where name = #$location limit 10"
  println(query.toString)
}

The output will be:

SQLActionBuilder(Vector(select * from cities_v where name = , ville,  limit 10),<function2>

Slick will ALWAYS add commas around any literal argument, no matter where it is placed, even if it's the only argument in the query, as in:

sql"#$q"

Now, what I'd like is make more complex queries, with calculations and function calls within Postgres. But I can't get anywhere given that Slick won't let me use any of my variables.

I tried setting all sorts of implicits, some that extend GetResult some that extend StatementParameters, Slick seems to ignore all them and either replaces my arguments with ? or surrounds them with commas, thereby rendering all of my queries invalid. I would like to add that the #$ is not great because it does not provide sanitization. I'd like to stick with Slick because it's asynchronous, but I'm not sure where to go from here.

These are my version numbers, according to build.sbt:

"postgresql"         %  "postgresql"           % "9.1-901-1.jdbc4",
"com.typesafe.slick" %  "slick_2.12"           % "3.2.3",

What am I doing wrong?

eje211
  • 2,385
  • 3
  • 28
  • 44

1 Answers1

1

There is nothing wrong with that ? appears in you result statements. It is just parametrized query, and each ? represents placeholder for future value.

Just run you queries against any database and check results.

Yevhenii Popadiuk
  • 1,024
  • 7
  • 18
  • Let me try that. It seems that it had not, unlike what I thought, been run against a database. I went with another solution, but I need the raw SQL statements again, so I'll do this and let you know how it went. – eje211 Nov 13 '18 at 04:59
  • You were absolutely right. I was just not used to how Slick works. So far, I find Slick fantastic and their documentation abysmal. The docs are really (in my opinion at least) awful. But it's all sorted out now. Thanks for the help! – eje211 Nov 13 '18 at 21:43
  • You are welcome. I suggest you read [essential slick](https://books.underscore.io/essential-slick/essential-slick-3.html) from underscore.io (i still use `ctrl+f` on this book to refresh on something). – Yevhenii Popadiuk Nov 14 '18 at 10:21