Skip to content

Instantly share code, notes, and snippets.

@seratch
Created August 23, 2012 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seratch/3436229 to your computer and use it in GitHub Desktop.
Save seratch/3436229 to your computer and use it in GitHub Desktop.
SQLInterpolation prototype
scala> case class SQLInterpolationResult[P](strings: Seq[String], param: P)
defined class SQLInterpolationResult
scala> class SQLInterpolation(val s: StringContext) extends AnyVal {
| def sql[P](param: P) = new SQLInterpolationResult(s.parts, param)
| }
defined class SQLInterpolation
scala> @inline implicit def interpolation(s: StringContext) = new SQLInterpolation(s)
warning: there were 1 feature warnings; re-run with -feature for details
interpolation: (s: StringContext)SQLInterpolation
scala> val id = 123
id: Int = 123
scala> val name = "Martin"
name: String = Martin
scala> val res = sql"select * from users where id = $id and name = $name"
res: SQLInterpolationResult[(Int, String)] = SQLInterpolationResult(WrappedArray(select * from users where id = , and name = , ),(123,Martin))
scala> val q = res.strings.mkString("?")
q: String = select * from users where id = ? and name = ?
scala> val params = res.param
params: (Int, String) = (123,Martin)
scala> val res = sql"$name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name"
<console>:17: error: too many arguments for method sql: (param: P)SQLInterpolationResult[P]
val res = sql"$name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name"
^
// https://github.com/slick/slick/blob/master/src/main/scala/scala/slick/jdbc/StaticQuery.scala
// https://github.com/slick/slick-examples/blob/master/src/main/scala/scala/slick/examples/jdbc/PlainSQL.scala
case class SQLInterpolationResult[P](strings: Seq[String], param: P)
class SQLInterpolation(val s: StringContext) extends AnyVal {
def sql[P](param: P) = new SQLInterpolationResult(s.parts, param)
}
@inline implicit def interpolation(s: StringContext) = new SQLInterpolation(s)
val id = 123
val name = "Martin"
val res = sql"select * from users where id = $id and name = $name"
val q = res.strings.mkString("?")
val params = res.param
val res = sql"$name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name $name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment