Created
October 17, 2012 03:58
-
-
Save thata/3903629 to your computer and use it in GitHub Desktop.
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
package jp.co.esm.myscalatra.domain | |
import org.squeryl._ | |
import org.squeryl.PrimitiveTypeMode._ | |
import jp.co.esm.myscalatra.domain.model._ | |
import org.squeryl.adapters.PostgreSqlAdapter | |
object Schema extends Schema { | |
val users = table[User]("users") | |
def connect { | |
val db_driver = "org.postgresql.Driver" | |
val db_url = "jdbc:postgresql://localhost:5432/mydb" | |
val db_username = "thata" | |
val db_password = "" | |
Class.forName("org.postgresql.Driver") | |
SessionFactory.concreteFactory = Some( ()=> Session.create( | |
java.sql.DriverManager.getConnection( db_url, db_username, db_password ), new PostgreSqlAdapter )) | |
} | |
def main(args:Array[String]) { | |
connect | |
transaction { | |
Schema.create | |
} | |
} | |
} |
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
package jp.co.esm.myscalatra.domain.model | |
import org.squeryl._ | |
case class User(var name:String, var age:Int, var id:Long = 0L) extends KeyedEntity[Long] |
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 org.squeryl._ | |
import org.squeryl.PrimitiveTypeMode._ | |
import jp.co.esm.myscalatra.domain.model._ | |
import jp.co.esm.myscalatra.domain.Schema._ | |
// dbへ接続 | |
jp.co.esm.myscalatra.domain.Schema.connect | |
// insert | |
transaction { | |
users.insert(new User("thata", 17)) | |
users.insert(new User("haruka", 17)) | |
} | |
// 全件取得 | |
transaction { users.seq.toList } | |
// 指定ユーザ取得 | |
transaction { users.where(u => u.name === "thata").toList } | |
transaction { from(users) (u => where(u.name === "haruka") select(u)).toList } | |
transaction { from(users) (u => where(u.name === "haruka") select(u.name)).toList(0) } | |
transaction { users.lookup(1L) } | |
// update | |
transaction { var thata = users.where( u => u.name === "thata").first; thata.name = "htkymtks"; users.update(thata) } | |
// delete | |
transaction { val u:User = users.lookup(3L).get; users.delete(u.id) } | |
transaction { users.deleteWhere(u => u.name === "htkymtks") } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment