Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Created February 15, 2012 22:06
Show Gist options
  • Save gamlerhart/1839338 to your computer and use it in GitHub Desktop.
Save gamlerhart/1839338 to your computer and use it in GitHub Desktop.
ScalaQuery
database withSession {
ApiKeys.ddl.create
}
val database: Database = Database.forURL("jdbc:h2:~/apiKeyStore", driver = "org.h2.Driver")
// Imports for the query API
import org.scalaquery.ql.extended.H2Driver.Implicit._
import org.scalaquery.session.{Session, Database}
import org.scalaquery.ResultSetInvoker
import org.scalaquery.simple.StaticQuery
import org.scalaquery.ql.Query
// Query
val devicesByApiKey = for {
a <- ApiKeys if a.deviceId.like(deviceId)
} yield a.apiKey
// Do stuff with the result
devicesByApiKey.foreach(i=>doStuff(i))
// We use the thread local session management
// So we import this implicit parameter which returns the current session
// Of course we pass any other session / use our own implicits
import org.scalaquery.session.Database.threadLocalSession
// and we do stuff in our session
database withSession {
// do stuff
}
val ApiKeys = new ExtendedTable[(String, String)]("googleApiKeys") {
def deviceId = column[String]("deviceId", O.PrimaryKey)
def apiKey = column[String]("apiKey")
def * = deviceId ~ apiKey
}
val devicesByApiKey = for {
a <- ApiKeys if a.deviceId.like(deviceId)
} yield a.apiKey
devicesByApiKey.firstOption match {
case None => {
ApiKeys.insert(deviceId, googleKey)
}
case Some(_) => {
query.update(googleKey)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment