scalajdbcslickjdbc-postgres

Scala Slick: cannot get parameters in query


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?


Solution

  • 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.