Created
October 10, 2011 03:08
-
-
Save takscape/1274554 to your computer and use it in GitHub Desktop.
Scala: Squeryl
This file contains 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
import java.sql.DriverManager | |
import org.apache.commons.io.IOUtils | |
import org.h2.jdbcx.JdbcDataSource | |
import org.squeryl.adapters.H2Adapter | |
import org.squeryl.{KeyedEntity, Session, SessionFactory, Schema} | |
import scala.collection.JavaConversions._ | |
import org.squeryl.PrimitiveTypeMode._ | |
class User(val id: Long, val name: String) extends KeyedEntity[Long] | |
{ | |
def this() = this(0, "") | |
} | |
object MySchema extends Schema | |
{ | |
val users = table[User] | |
on(users) { u => declare( | |
u.id is(primaryKey, autoIncremented) | |
)} | |
} | |
object Sandbox | |
{ | |
def main(args: Array[String]) { | |
val ds = new org.h2.jdbcx.JdbcDataSource | |
ds.setURL("jdbc:h2:./db") | |
ds.setUser("sa") | |
ds.setPassword("") | |
SessionFactory.concreteFactory = Some(() => | |
Session.create(ds.getConnection, new H2Adapter)) | |
transaction { | |
import MySchema._ | |
drop | |
create | |
users.insert(new User(0, "foo")) | |
users.insert(new User(0, "bar")) | |
} | |
transaction { | |
import MySchema._ | |
val bar = from(users)(u => | |
where(u.name === "foo") | |
select(u) | |
) single | |
println("id = " + bar.id) | |
println("name = " + bar.name) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment