Created
May 14, 2014 16:52
-
-
Save okapies/466bb0d57e6592513959 to your computer and use it in GitHub Desktop.
Slick 2.0 Sample
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
# create table "users" ("id" INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY,"name" VARCHAR NOT NULL,"display_name" VARCHAR NOT NULL) | |
# create table "text_messages" ("time" TIMESTAMP NOT NULL,"userId" INTEGER NOT NULL,"message" VARCHAR NOT NULL) | |
# alter table "text_messages" add constraint "user_fk" foreign key("userId") references "users"("id") on update NO ACTION on delete NO ACTION | |
# INSERT INTO "users" ("name","display_name") VALUES (?,?) | |
Users: | |
User(Some(1),alice,Alice Henderson) | |
User(Some(2),bob,Bob Sanders) | |
User(Some(3),carol,Carol Sanders) | |
<user>:<text>$ alice:hello | |
# select x2."id" from "users" x2 where x2."name" = 'alice' | |
# INSERT INTO "text_messages" ("time","userId","message") VALUES (?,?,?) | |
<user>:<text>$ bob:good morning | |
# select x2."id" from "users" x2 where x2."name" = 'bob' | |
# INSERT INTO "text_messages" ("time","userId","message") VALUES (?,?,?) | |
<user>:<text>$ carol:bye! | |
# select x2."id" from "users" x2 where x2."name" = 'carol' | |
# INSERT INTO "text_messages" ("time","userId","message") VALUES (?,?,?) | |
<user>:<text>$ | |
# select x2."time", x3."name", x3."display_name", x2."message" from "text_messages" x2, "users" x3 where x3."id" = x2."userId" order by x2."time" desc | |
TextMessages: | |
(2014-05-15T01:47:48.607+09:00,carol,Carol Sanders,bye!) | |
(2014-05-15T01:47:43.859+09:00,bob,Bob Sanders,good morning) | |
(2014-05-15T01:47:32.776+09:00,alice,Alice Henderson,hello) | |
# alter table "text_messages" drop constraint "user_fk" | |
# drop table "users" | |
# drop table "text_messages" |
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
import java.io.BufferedReader | |
import java.io.InputStreamReader | |
import scala.annotation.tailrec | |
import org.joda.time._ | |
import com.github.tototoshi.slick.H2JodaSupport._ | |
object MessageLogger extends App { | |
val profile = scala.slick.driver.H2Driver | |
import profile.simple._ | |
case class User(id: Option[Int], name: String, displayName: String) | |
case class TextMessage(time: DateTime, userId: Int, message: String) | |
class Users(tag: Tag) extends Table[User](tag, "users") { | |
def id = column[Int]("id", O.PrimaryKey, O.AutoInc) | |
def name = column[String]("name") | |
def displayName = column[String]("display_name") | |
def * = (id.?, name, displayName) <> (User.tupled, User.unapply) | |
} | |
val users = TableQuery[Users] | |
class TextMessages(tag: Tag) extends Table[TextMessage](tag, "text_messages") { | |
def time = column[DateTime]("time") | |
def userId = column[Int]("userId") | |
def message = column[String]("message") | |
def * = (time, userId, message) <> (TextMessage.tupled, TextMessage.unapply) | |
def user = foreignKey("user_fk", userId, users)(_.id) | |
} | |
val textMessages = TableQuery[TextMessages] | |
val ddl = users.ddl ++ textMessages.ddl | |
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession { implicit session => | |
ddl.createStatements.map("# " + _).foreach(println) | |
ddl.create | |
println(s"# ${users.insertStatement}") | |
users += User(None, "alice", "Alice Henderson") | |
users += User(None, "bob", "Bob Sanders") | |
users += User(None, "carol", "Carol Sanders") | |
println("Users:") | |
users.foreach(u => println(s" $u")) | |
@tailrec | |
def procLine(in: BufferedReader): Unit = { | |
print("<user>:<text>$ ") | |
val line = in.readLine() | |
if (line != null && !line.isEmpty) { | |
val splitted = line.split(":", 2) | |
if (splitted.length == 2) { | |
val userName = splitted(0) | |
val text = splitted(1) | |
val q1 = users.filter(_.name === userName).map(_.id) | |
println(s"# ${q1.selectStatement}") | |
val userId = q1.firstOption | |
userId match { | |
case Some(uid) => | |
println(s"# ${textMessages.insertStatement}") | |
textMessages += TextMessage(new DateTime, uid, text) | |
case None => | |
println(s"User not found: $userName") | |
} | |
} else { | |
println(s"invalid: $line") | |
} | |
procLine(in) | |
} | |
} | |
procLine(new BufferedReader(new InputStreamReader(System.in))) | |
val q2 = for { | |
m <- textMessages sortBy(_.time.desc) | |
u <- m.user | |
} yield (m.time, u.name, u.displayName, m.message) | |
println(s"# ${q2.selectStatement}") | |
println("TextMessages:") | |
q2.foreach(m => println(s" $m")) | |
ddl.dropStatements.map("# " + _).foreach(println) | |
ddl.drop | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment