Skip to content

Instantly share code, notes, and snippets.

@0xYUANTI
Last active August 29, 2015 14:20
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 0xYUANTI/97714b76cf2fb718f443 to your computer and use it in GitHub Desktop.
Save 0xYUANTI/97714b76cf2fb718f443 to your computer and use it in GitHub Desktop.
// build.sbt
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
"org.scalaz" %% "scalaz-core" % "7.1.1",
"org.scalaz" %% "scalaz-effect" % "7.1.1",
"org.scalaz" %% "scalaz-concurrent" % "7.1.1",
"org.tpolecat" %% "doobie-core" % "0.2.1",
"org.tpolecat" %% "doobie-contrib-postgresql" % "0.2.1",
"org.tpolecat" %% "doobie-contrib-specs2" % "0.2.1",
"org.specs2" %% "specs2-core" % "2.4.15" % "test"
)
// test case
import org.specs2.mutable._
import doobie.imports._
import doobie.contrib.specs2.analysisspec._
import scalaz.concurrent.Task
class SqlSpec extends Specification with AnalysisSpec {
val host = sys.env("pghost")
val name = sys.env("pgdb")
val user = sys.env("pguser")
val pass = sys.env("pgpass")
val transactor = DriverManagerTransactor[Task](
"org.postgresql.Driver",
s"jdbc:postgresql://${host}/${name}",
user,
pass
)
def select(tab : String, key : String) =
sql"""
SELECT val
FROM $tab
WHERE key = $key
""".query[String]
val q = select("foo", "bar")
check(q)
}
// error
[info] SqlSpec
[info]
[info] Query0[String] defined at postgres.scala:69
[info]
[info] SELECT val
[info] FROM ?
[info] WHERE key = ?
[info]
[info] x SQL Compiles and Typechecks
[error] x ERROR: syntax error at or near "$1"
[error] Position: 40 (specs2.scala:60)
[info]
[info]
[info] Total for specification SqlSpec
[info] Finished in 13 ms
[info] 1 example, 1 failure, 0 error
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
@tpolecat
Copy link

Just for the record, you can implement select as follows, and it will have the same final type you want.

def select(tab: String, key: String) =
  Query[String, String](s"""
    SELECT val
    FROM   $tab
    WHERE  key = ?
  """, None).toQuery0(key)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment