-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.scala
63 lines (50 loc) · 1.64 KB
/
main.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import slick.driver.PostgresDriver.api._
import slick.driver.PostgresDriver.QueryBuilder
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
object ILike {
implicit class IlikeOps(s: Rep[String]) {
def ilike(p: Rep[String]): Rep[Boolean] = {
val expr = SimpleExpression.binary[String,String,Boolean] { (s, p, qb) =>
qb.expr(s)
qb.sqlBuilder += " ILIKE "
qb.expr(p)
}
expr.apply(s,p)
}
}
}
object Example extends App {
final case class Message(
sender: String,
content: Option[String],
id: Long = 0L)
def freshTestData = Seq(
Message("Dave", Some("Hello, HAL. Do you read me, HAL?")),
Message("HAL", Some("Affirmative, Dave. I read you.")),
Message("Dave", Some("Open the pod bay doors, HAL.")),
Message("HAL", Some("I'm sorry, Dave. I'm afraid I can't do that.")),
Message("Dave", None)
)
final class MessageTable(tag: Tag)
extends Table[Message](tag, "message") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def sender = column[String]("sender")
def content = column[Option[String]]("content")
def * = (sender, content, id) <> (Message.tupled, Message.unapply)
}
lazy val messages = TableQuery[MessageTable]
import ILike._
val program = for {
_ <- messages.schema.create
_ <- messages ++= freshTestData
senders <- messages.filter(m => m.sender ilike "%dave%").result
} yield senders
val db = Database.forConfig("example")
try {
println(
Await.result(db.run(program), 2 seconds)
)
} finally db.close
}