slick

Scala Enumerations (case objects) in Slick, good practice

女生的网名这么多〃 提交于 2020-01-03 17:29:49
问题 Suppose I have a trait representing a set of several valid states. Would it be a good practice to store the objects in the database? Would it be better to store Ints and map them to DoorState with an implicit function MappedColumnType.base[Int, DoorState]? trait DoorState case object Open extends DoorState case object Closed extends DoorState class Doors(tag: Tag) extends Table[Door](tag, "DOORS") { ... def state = column[DoorState]("DOOR_STATE") ... } 回答1: Recommendation from the makers use:

Scala Enumerations (case objects) in Slick, good practice

旧街凉风 提交于 2020-01-03 17:29:02
问题 Suppose I have a trait representing a set of several valid states. Would it be a good practice to store the objects in the database? Would it be better to store Ints and map them to DoorState with an implicit function MappedColumnType.base[Int, DoorState]? trait DoorState case object Open extends DoorState case object Closed extends DoorState class Doors(tag: Tag) extends Table[Door](tag, "DOORS") { ... def state = column[DoorState]("DOOR_STATE") ... } 回答1: Recommendation from the makers use:

Compilation Error with Slick for table > 22 columns (using HLists)

你。 提交于 2020-01-03 02:53:09
问题 I've created a custom generator to map my database into my app and I'm having an issue. I've customized it to generate the auto increment columns as optional, so I've made this: val codegen = new scala.slick.model.codegen.SourceCodeGenerator(model) { override def Table = new Table(_){ override def autoIncLastAsOption = true } } For the tables that has more than 22 columns, it uses HList implementation and not the Scala Tuple. It is generating this: implicit def GetResultVoicemailRow(implicit

Getting elements from Slick HLIST (or Convert Slick HLIst into Shapeless HList)

筅森魡賤 提交于 2020-01-02 13:17:32
问题 I have auto-generated scala code using slick codegen. I see that some tables Rows are implements as HLists. (but these are slick HList, not the normal shapeless HList) Now I want a specific element from the HList returned as a Row by the slick query. I googled and found this thread Getting elements from an HList But this does not work for slick HList. it works very well for Shapeless HList I also tried the apply method val x : Long = slickHList(2) but this doesn't compile because type Any

Generic CRUD operations using Slick 2.0

岁酱吖の 提交于 2020-01-02 06:28:48
问题 I am trying to write a generic CRUD trait for Slick 2.0. The trait should a) provide generic methods to read/update/delete entities as well as b) abstract from the database. Following this slick example (database abstraction) and this article (CRUD trait) I came up with the following (shortened) code snippet: trait Profile { val profile: JdbcProfile } trait Crud[T <: AbstractTable[A], A] { this: Profile => import profile.simple._ val qry: TableQuery[T] def countAll()(implicit session: Session

How to setup username and password with Slick's source code generator?

戏子无情 提交于 2020-01-02 01:56:10
问题 Following the directions in this page: http://slick.typesafe.com/doc/2.0.0/code-generation.html we see that something like the following segment of code is required to generate models for mysql tables val url = "jdbc:mysql://127.0.0.1/SOME_DB_SCHEMA?characterEncoding=UTF-8&useUnicode=true" val slickDriver = "scala.slick.driver.MySQLDriver" val jdbcDriver = "com.mysql.jdbc.Driver" val outputFolder = "/some/path" val pkg = "com.pligor.server" scala.slick.model.codegen.SourceCodeGenerator.main(

Dynamic OR filtering - Slick

假如想象 提交于 2019-12-30 11:07:37
问题 Ok, I've got a method with multiple optional arguments like this def(username: Option[String], petname: Option[String], favouritefood: Option[String]) and i want to write a dynamic query that will be capable of fetching the data of defined arguments in a way of this select * from table where un like username or pn like pn or ff like ff; so depending of which arguments are defined to add them to query with OR operator? 回答1: Something like this should work. I had to use a similiar fragment in

Is it possible to use IN clause in plain sql Slick for integers?

安稳与你 提交于 2019-12-30 06:32:20
问题 There is a similar question here but it doesn't actually answer the question. Is it possible to use IN clause in plain sql Slick? Note that this is actually part of a larger and more complex query, so I do need to use plain sql instead of slick's lifted embedding. Something like the following will be good: val ids = List(2,4,9) sql"SELECT * FROM coffee WHERE id IN ($ids)" 回答1: The sql prefix unlocks a StringContext where you can set SQL parameters. There is no SQL parameter for a list, so you

Is it possible to use IN clause in plain sql Slick for integers?

好久不见. 提交于 2019-12-30 06:32:09
问题 There is a similar question here but it doesn't actually answer the question. Is it possible to use IN clause in plain sql Slick? Note that this is actually part of a larger and more complex query, so I do need to use plain sql instead of slick's lifted embedding. Something like the following will be good: val ids = List(2,4,9) sql"SELECT * FROM coffee WHERE id IN ($ids)" 回答1: The sql prefix unlocks a StringContext where you can set SQL parameters. There is no SQL parameter for a list, so you

Upsert in Slick

久未见 提交于 2019-12-29 18:50:26
问题 Is there a way I can neatly do an upsert operation in Slick? The following works but is too obscure/verbose and I need to explicitly state the fields that should be updated: val id = 1 val now = new Timestamp(System.currentTimeMillis) val q = for { u <- Users if u.id === id } yield u.lastSeen q.update(now) match { case 0 => Users.insert((id, now, now)) case _ => Unit } 回答1: Updated for native upsert/merge support in Slick 2.1 Attention You have to use plain SQL embedding with your database