Created
February 15, 2012 22:06
-
-
Save gamlerhart/1839338 to your computer and use it in GitHub Desktop.
ScalaQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
database withSession { | |
ApiKeys.ddl.create | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val database: Database = Database.forURL("jdbc:h2:~/apiKeyStore", driver = "org.h2.Driver") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val ApiKeys = new ExtendedTable[(String, String)]("googleApiKeys") { | |
def deviceId = column[String]("deviceId", O.PrimaryKey) | |
def apiKey = column[String]("apiKey") | |
def * = deviceId ~ apiKey | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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