slick

Slick 3.0 many-to-many query with the join as an iterable

一笑奈何 提交于 2019-12-04 20:52:44
问题 I've created a many-to-many collection using Slick 3.0, but I'm struggling to retrieve data in the way I want. There is a many-to-many relationship between Events and Interests. Here are my tables: case class EventDao(title: String, id: Option[Int] = None) class EventsTable(tag: Tag) extends Table[EventDao](tag, "events") { def id = column[Int]("event_id", O.PrimaryKey, O.AutoInc) def title = column[String]("title") def * = ( title, id.?) <> (EventDao.tupled, EventDao.unapply) def interests =

How to parametrize Scala Slick queries by WHERE clause conditions?

旧巷老猫 提交于 2019-12-04 20:48:13
Assume these two simple queries: def findById(id: Long): Option[Account] = database.withSession { implicit s: Session => val query = for (a <- Accounts if a.id === id) yield a.* query.list.headOption } def findByUID(uid: String): Option[Account] = database.withSession { implicit s: Session => val query = for (a <- Accounts if a.uid === uid) yield a.* query.list.headOption } I would like to rewrite it to remove the boilerplate duplication to something like this: def findBy(criteria: ??? => Boolean): Option[Account] = database.withSession { implicit s: Session => val query = for (a <- Accounts

How to combine multiple columns in one case class field when using lifted embedding?

允我心安 提交于 2019-12-04 17:56:34
We have a MySQL table containing several boolean columns which specify the roles a user may have. Is it possible with slick's lifted embedding to write a type mapper which combines & transforms these multiple columns to one field in the case class User like shown below? case class User(id: Option[Int], nickname: String, role: Seq[Role.Role]) object Users extends Table[(User)]("ask_user") { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) def nickname = column[String]("nickname") def is_editor = column[Boolean]("is_editor") def is_moderator = column[Boolean]("is_moderator") def is

Slick logging with slf4j-simple

别等时光非礼了梦想. 提交于 2019-12-04 17:26:13
I am using slf4j-simple in my project. I would like to change logging level for slick to INFO. After reading Logging options for Slick and Class SimpleLogger docs I have tried to add following options to my VM line: -Dorg.slf4j.simpleLogger.defaultLogLevel=INFO -Dlogger.scala.slick=INFO -Dlogger.scala.slick.jdbc.JdbcBackend.statement=INFO -Dorg.slf4j.simpleLogger.log.scala.slick=INFO I see a few INFO level logs comming from jetty, therefore the basic logging seems to be working. I am also able to change level of logs shown by using -Dorg.slf4j.simpleLogger.defaultLogLevel=TRACE , but even that

compose slick dbaction with authenticated action

人走茶凉 提交于 2019-12-04 16:32:03
I have my custom authenticated action that is like def Authenticated(rights: String*) = new ActionBuilder[MyAuthenticatedRequest] { *** } In my controller I use this action to check the user has the right to view the page def password = Authenticated(UserRights.USER) { implicit request: MyAuthenticatedRequest[_] => Users.findById(request.account.id) match { case Some(user) => Ok(views.html.settings.password(frontUser, user)) case _ => NotFound } } My oject Users uses slick to retrieve the user in the database def findById(id: Long)(implicit s: Session): Option[User] = users.where(_.id === id)

Mapped projection with <> to a case class with companion object in Slick

≡放荡痞女 提交于 2019-12-04 13:13:04
问题 With Slick, I am trying to project database table entries directly to the case class they represent. Following the example in the documentation, I set up a mapped projection using the <> operator: case class SomeEntity3(id: Int, entity1: Int, entity2: Int) val SomeEntityTable = new Table[SomeEntity3]("some_entity_table") { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) def entity1 = column[Int]("entity1") def entity2 = column[Int]("entity2") def * = id ~ entity1 ~ entity2 <> (SomeEntity3

Slick 2.10-RC1, Scala 2.11.x, bypassing 22 arity limit with case class (heterogenous)

故事扮演 提交于 2019-12-04 12:18:26
问题 I am having issues in mapping a Table that has > 22 columns specifically into a case class , assuming you have the following code import slick.driver.PostgresDriver import scala.slick.collection.heterogenous._ import syntax._ import shapeless.Generic case class TwentyThreeCaseClass( val id:Option[Long], val one:String, val two:String, val three:String, val four:String, val five:String, val six:String, val seven:String, val eight:String, val nine:String, val ten:String, val eleven:String, val

Create a MySQL connection in Playframework with slick

主宰稳场 提交于 2019-12-04 12:00:09
问题 I'm trying to connect to a mysql database with slick 1.0.0. What I've done so far: in Build.scala I've added val appDependencies = Seq( anorm, "mysql" % "mysql-connector-java" % "5.1.24", "com.typesafe.slick" % "slick_2.10" % "1.0.0", "org.slf4j" % "slf4j-nop" % "1.6.4" ) in application.conf db.default.driver=com.mysql.jdbc.Driver db.default.url="url to mysql db" db.default.user=user db.default.pass=password and now I'm trying to read an Entry from the DB. For this I have a model package

Slick 3.0.0 database agnostism

懵懂的女人 提交于 2019-12-04 10:08:00
I started to use Slick 3.0.0 and I like it's succinct syntax. Nevertheless, I wasn't able to find a way to use it in a database agnostic way. In the following example provided in the documentation: http://slick.typesafe.com/doc/3.0.0/gettingstarted.html I'd like to be able to decouple somehow this code of the database used and avoid importing database specific in my code (i.e slick.driver.H2Driver.api._ ). I tried to get rid of it by providing the connection using the cake pattern, but the ".result" member isn't available then. A workaround would be to import slick.driver.JdbcDriver.api._ ,

Slick 3 - upsert works too slow

橙三吉。 提交于 2019-12-04 10:05:08
I use flowing code for upsert list items case class Item(id: String, text: String) class Items(tag: Tag) extends Table[Item](tag, "items"){ ... } val tbl = TableQuery[Items] def insertItems(items: List[Item]):Future[Int] = { val q = DBIO.sequence(items.map(tbl.insertOrUpdate).toSeq).map(_.sum) db.run(q) } For items list with length 2000, upsert takes ~10 seconds. It's too long... I think, most part time takes compiling queries. How should I rewrite insertItems for acceleration it? Use compiled queries ( docs ). AFAIK, Compiled queries for insert are available after slick 2.0. Also, to insert a