Permalink
Browse files

renames and a bit of code cleaning

  • Loading branch information...
1 parent 76095cc commit 3bbb361e64242730ace1e0ba3744b0a1fc0d77e2 @fernandezpablo85 committed Apr 7, 2013
Showing with 50 additions and 29 deletions.
  1. +0 −29 src/main/scala/DefaultExample.scala
  2. +14 −0 src/main/scala/Main.scala
  3. +36 −0 src/main/scala/UserDao.scala
@@ -1,29 +0,0 @@
-package org.example
-
-import scala.slick.driver.PostgresDriver.simple._
-import Database.threadLocalSession
-
-object DefaultExample {
-
- val database = Database.forURL("jdbc:postgresql://localhost:5432/demo", driver = "org.postgresql.Driver")
-
- case class User(id: Option[Int], name: String, pass: String)
- object User {
- def dbCreate(name: String, pass: String) = User(None, name, pass)
- def dbRetrieve(user: User) = Some((user.name, user.pass))
- }
-
- object Users extends Table[User]("users") {
- def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
- def name = column[String]("name")
- def pass = column[String]("pass")
-
- def * = name ~ pass <> (User.dbCreate _, User.dbRetrieve _)
- }
-
- def insert(name: String, pass: String) = {
- database withSession {
- Users.insert(User(None, name, pass))
- }
- }
-}
View
@@ -0,0 +1,14 @@
+package org.example
+
+import util._
+
+object Main extends App {
+
+ benchmark() {
+ UserDao.insert("pablo", "fernandez")
+ UserDao.insert("noob", "saibot")
+ UserDao.insert("uma", "thurman")
+
+ val users = UserDao.findByLastName("fernandez")
+ }
+}
@@ -0,0 +1,36 @@
+package org.example
+
+import scala.slick.driver.PostgresDriver.simple._
+import Database.threadLocalSession
+
+object UserDao {
+
+ val database = Database.forURL("jdbc:postgresql://localhost:5432/demo", driver = "org.postgresql.Driver")
+
+ case class User(id: Option[Int], name: String, last: String)
+
+ object Users extends Table[User]("users") {
+ def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
+ def name = column[String]("name")
+ def lastName = column[String]("last_name")
+
+ def * = (id.? ~ name ~ lastName) <> (User, User.unapply _)
+
+ // Shim needed to handle Postgress - Slick quirks with autoincrement columns.
+ def insertProjection = (name ~ lastName) <> (
+ {(first, last) => User(None, first, last)},
+ {(user: User) => Some(user.name, user.last)}
+ )
+ }
+
+ def insert(name: String, lastName: String) = {
+ database withSession {
+ Users.insertProjection.insert(User(None, name, lastName))
+ }
+ }
+
+ def findByLastName(last: String) = database withSession {
+ val query = for (u <- Users if u.lastName is last) yield (u)
+ query.list
+ }
+}

0 comments on commit 3bbb361

Please sign in to comment.