Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76095cc
commit 3bbb361
Showing
3 changed files
with
50 additions
and
29 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |