17. .Net
LINQ makes a query
a first-class language construct
in C# and Visual Basic.
var results = from c in Suppliers
where c.size < 1
select new {c.name, c.address};
48. trait UnitInvoker[+R] extends Invoker[Unit, R] {
final def list()(implicit session: JdbcBackend#Session): List[R]
final def first()(implicit session: JdbcBackend#Session) : R
final def foreach(f: R => Unit, maxRows: Int)
(implicit session: JdbcBackend#Session): Unit
....
}
49. Lifted - predykaty
for {
speaker <- Query(Speakers)
if (speaker.name startsWith "Mac")
} yield (speaker.twitter)
===
=!=
notNull
50. Lifted - predykaty
for {
speaker <- Query(Speakers)
if (speaker.name startsWith "Mac")
} yield (speaker.twitter)
Rep[String]
Rep[TwitterId]
Rep[String]
51. Lifted - Join
for {
(s,p) <- Query(Speakers)
innerJoin Presentations
on (_.id === _.authorId)
} yield (s.twitter,p.abstr)
52. Lifted - własne typy
case class TwitterId(id:String) extends AnyVal
implicit val twitterId = MappedJdbcType.base[TwitterId,String]
(_.id,TwitterId.apply)
object Speakers extends Table[Speaker]("speakers") {
def twitter = column[TwitterId]("twitterId")
}
53. Lifted - kompozycja
def filter(pred
:Presentations=>Column[Boolean]) = {
for {
p <- Query(Presentations)
if (pred(p))
} yield (p.id)
}
54. Lifted - kompozycja
for {
presentation <- Query(Presentations)
author <- presentation.author
if author.name startsWith "Mac"
} yield presentation.title
66. Direct - metadane
@table("presentation")
case class Presentation(@column("id") id:Long,
@column("authorId") authorId:Long,
@column("title") title:String,
@column("abstract")abstr:String)
67. Direct - przykład
val query = for {
speaker <- Queryable[Speaker]
if speaker.name == "Kuba Nabrdalik"
} yield speaker.bio
68. Direct - przykład
val backend = new SlickBackend(profile,
AnnotationMapper)
def list[H](query:BaseQueryable[H])
: Iterable[H]
= backend.result(query, session)
dao.list(query) : List[Speaker]
69. Direct - typesafety :|
for {
presentation <- Queryable[Presentation]
if (presentation.title.contains("scala"))
} yield (presentation.id)
70. Direct - kompozycja??
def filter(pred:Presentation=>Boolean) = {
for {
p <- Queryable[Presentation]
if (pred(p))
} yield (presentation.id)
}
72. Slick OrientDB
@table("presentation")
case class Presentation(@column("id") id:Long,
@column("title") title:String,
@column("comments") comments:List[Comment])
case class Comment(@column("number") author:String,
@column("rate") rate:Integer,
@column("content") content:String)
73. Slick OrientDB
for {
presentation <- Queryable[Presentation]
if (presentation.comments
.filter(_.ratio > 5).size > 0)
} yield (presentation.title)
76. there is no valid translation of arbitrary
monadic Query expressions to SQL
(whose semantics are stuck half-way
between applicative functors and
monads)
- Stefan Zeiger
77. czy to się opłaca?
● Deklaratywne zapytania
● Znane API
● Brak magii
● ... oprócz odrobiny makr
● Skomplikowane :(