scalastring-interpolationscalikejdbc

How to forbid unknown parameter type in a scalikejdbc query?


When writing a string interpolation with sql"", how can I make sure that a Binder is present for a given parameter type instead of using the default that might not do what I want?

For example

case class MyClass(id: String)
val param = MyClass("param")
// I would like a compilation error on the following line if there is no Binder for MyClass
sql"SELECT column FROM table WHERE id = ${param}"

Solution

  • I don't believe that's possible with SQLInterpolation because it seems to use Any for all parameters and pass parameters of non-natively handled types to java.sql.PreparedStatement.setObject directly. However, there is QueryDSL that provides type-safety that you're looking for.

    An example from ScalikeJDBC 2.4.0 release notes:

    scala> case class Name(value: String)
    defined class Name
    
    scala> val name = Name("Alice")
    name: Name = Name(Alice)
    
    scala> select.from(User as u).where.eq(u.name, name)
    <console>:30: error: Could not find an implicit value of the ParameterBinderFactory[Name].
           select.from(User as u).where.eq(u.name, name)