Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scala 2.10 String interpolation experiment (Sql parameters)
package tests01
object lab {
object PreparedStatement {
case class Param[A](val index : Int, val value : A)
case class BoundSql(val sql: String, val params : Seq[Param[_]])
implicit class SqlStringContext(val sc : StringContext) extends AnyVal {
def sql(args : Any*) = BoundSql(sc.parts.mkString("?"),
((1 to args.length) zip args).map(x => Param(x._1,x._2)))
}
}
import PreparedStatement._
val id = 500 //> id : Int = 500
val name = "%test%" //> name : String = %test%
println(
sql"""
SELECT *
FROM mytable
WHERE id = $id
OR name LIKE $name
"""
) //> BoundSql(
//| SELECT *
//| FROM mytable
//| WHERE id = ?
//| OR name LIKE ?
//| ,Vector(Param(1,500), Param(2,%test%)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.