Skip to content

Instantly share code, notes, and snippets.

@vovanmix
Created February 14, 2017 10:12
Show Gist options
  • Save vovanmix/d52b4c41375d68f293d68419b31de7e2 to your computer and use it in GitHub Desktop.
Save vovanmix/d52b4c41375d68f293d68419b31de7e2 to your computer and use it in GitHub Desktop.
Scala ORM comparison
/// Slick
// schema
case class Album(
artist : String,
title : String,
id : Long = 0L)
class AlbumTable(tag: Tag) extends Table[Album](tag, "albums") {
def artist = column[String]("artist")
def title = column[String]("title")
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def * = (artist, title, id) <> (Album.tupled, Album.unapply)
}
// create
val insertAction =
AlbumTable += Album(
"Pink Floyd",
"Dark Side of the Moon",
1973,
Rating.Awesome)
// delete
val deleteAction =
AlbumTable
.filter(_.artist === "Keyboard Cat")
.delete
// update
val updateAction =
AlbumTable
.filter(_.artist === "Keyboard Cat")
.map(_.title)
.update("Even Greater Hits")
// find
val selectWhereQuery =
AlbumTable.filter(_.artist === "Spice Girls").sortBy(_.year.asc).drop(2).take(1)
exec(selectWhereQuery)
//// QUILL
// Compile-time Language Integrated Queries for Scala
// the runtime overhead is very low and similar to using the database driver directly
case class Product(id: Long, description: String, sku: Long)
// create
val a = quote{
query[Contact].insert(lift(Contact(999, "+1510488988")))
}
ctx.run(a)
// or
def create(user: User) = user.copy(id = run(users.insert(lift(user)).returning(_.id)))
// update
val a = quote {
query[Person].filter(_.id == 999).update(lift(Person(999, "John", 22)))
}
ctx.run(a)
// delete
val a = quote {
query[Person].filter(p => p.name == "").delete
}
ctx.run(a)
// find
val q = quote {
for {
p <- query[Person] if(p.id == 999)
c <- query[Contact] if(c.personId == p.id)
} yield {
(p.name, c.phone)
}
}
ctx.run(q)
//// squeryl
// A Scala ORM and DSL for talking with Databases with minimum verbosity and maximum type safety
case class Author(val id: Long,
val firstName: String,
val lastName: String,
val email: Option[String]) extends KeyedEntity[Long] {
val id: Long = 0
def this() = this(0,"","",Some(""))
}
object AppDB extends Schema {
val barTable = table[Bar]("bar")
}
// create
val bar = AppDB.barTable insert Bar(Some("foo"))
bar.id
val herbyHancock = artists.insert(new Artist("Herby Hancock"))
// or
new Artist("Herby Hancock").save
// update
watermelonMan.title = "The Watermelon Man"
watermelonMan.year = watermelonMan.year + 1
songs.update(watermelonMan)
watermelonMan.update
// find
import MusicDb._
def songs =
from(songs)(s => where(s.artistId === id) select(s))
val bars = from(barTable)(barTable => select(authors))
val songsFromThe60sInFunkAndLatinJazzPlaylist =
from(songs)(s=>
where(s.id in
from(funkAndLatinJazz.songsInPlaylistOrder)
(s2 => select(s2.id))
)
select(s)
)
import MusicDb._
def lookupArtist = artists.lookup(artistId)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment