slick

How are reactive streams used in Slick for inserting data

吃可爱长大的小学妹 提交于 2019-11-29 04:01:17
In Slick's documentation examples for using Reactive Streams are presented just for reading data as a means of a DatabasePublisher. But what happens when you want to use your database as a Sink and backpreasure based on your insertion rate? I've looked for equivalent DatabaseSubscriber but it doesn't exist. So the question is, if I have a Source, say: val source = Source(0 to 100) how can I crete a Sink with Slick that writes those values into a table with schema: create table NumberTable (value INT) Serial Inserts The easiest way would be to do inserts within a Sink.foreach . Assuming you've

How to execute DDL only when tables don't exist?

扶醉桌前 提交于 2019-11-29 03:56:32
I'm using Slick 1.0 with Play Framework 2.1 and MySQL. I'd like to control the ddl table creation so that it only takes place if the tables don't exist. That is to say that the tables should only get created the first time I start play. How to do it in Slick? Since I like to control the creation of my tables individually and keep it DRY, I just tend to add a utility method to my apps: def createIfNotExists(tables: TableQuery[_ <: Table[_]]*)(implicit session: Session) { tables foreach {table => if(MTable.getTables(table.baseTableRow.tableName).list.isEmpty) table.ddl.create} } Then you can

How to make aggregations with slick

只谈情不闲聊 提交于 2019-11-29 01:46:57
问题 I want to force slick to create queries like select max(price) from coffees where ... But slick's documentation doesn't help val q = Coffees.map(_.price) //this is query Query[Coffees.type, ...] val q1 = q.min // this is Column[Option[Double]] val q2 = q.max val q3 = q.sum val q4 = q.avg Because those q1-q4 aren't queries, I can't get the results but can use them inside other queries. This statement for { coffee <- Coffees } yield coffee.price.max generates right query but is deprecated

Slick 3.0.0: How to query one-to-many / many-to-many relations

懵懂的女人 提交于 2019-11-29 00:25:55
问题 Basically the same question has been asked about a year ago for slick 2.x (scala slick one-to-many collections). I'm wondering if there has any progression been made with the release of reactive slick. Let's say for example we have three tables. library , book and library_to_book where a library has many books. What I want is a list of libraries with their books. In scala this would be something like Seq[(Library, Seq[Book])] . The query I have is as follows: val q = (for { l <- libraries ltb

Slick: create query conjunctions/disjunctions dynamically

折月煮酒 提交于 2019-11-29 00:08:53
问题 I'm trying to create a typesafe dynamic DSL for a Slick table but not sure how to achieve this. Users can post filters to the server by sending filters in form/json format, and I need to build a Slick query with all that. So basically this means transforming a Scala case class representing my filters to a Slick query. It seems the "predicates" can have 3 different shapes. I've seen the trait CanBeQueryCondition . Can I fold over these different possible shapes? I've seen the extension methods

Returning AutoInc ID after Insert in Slick 2.0

两盒软妹~` 提交于 2019-11-29 00:06:36
问题 I have looked to the ends of the earth for the answer to this question. There is not much info out there on slick 2.0. Below is my code for my Addresses model, how would I have the method create return the id after it made the insert? package models import play.api.Play.current import play.api.db.slick.Config.driver.simple._ import play.api.db.slick.DB object Addresses{ val DB_URL:String = "jdbc:h2:mem:fls-play" val DB_driver:String = "org.h2.Driver" class Addresses(tag: Tag) extends Table[

Customer Type Mapper for Slick SQL

送分小仙女□ 提交于 2019-11-28 23:39:49
I've found this example from slick testing: https://github.com/slick/slick/blob/master/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala sealed trait Bool case object True extends Bool case object False extends Bool implicit val boolTypeMapper = MappedColumnType.base[Bool, String]( { b => assertNotNull(b) if(b == True) "y" else "n" }, { i => assertNotNull(i) if(i == "y") True else False } ) But I'm trying to create a TypeMapper for org.joda.time.DateTime to/from java.sql.Timestamp - but without much success. The Bool example is very particular and I'm having

Slick left/right/outer joins with Option

情到浓时终转凉″ 提交于 2019-11-28 23:29:36
问题 In the Slick examples there are a few examples of joining where one of the resulting columns can be nulls, as it can be the case when doing left, right, or outer joins. For example: val explicitLeftOuterJoin = for { (c, s) <- Coffees leftJoin Suppliers on (_.supID === _.id) } yield (c.name, s.name.?) But what if I want to return the entire mapped object? What I mean is: val explicitLeftOuterJoin = for { (c, s) <- Coffees leftJoin Suppliers on (_.supID === _.id) } yield (c, s.?) This doesn't

Slick 3.0 bulk insert or update (upsert)

一个人想着一个人 提交于 2019-11-28 22:45:15
what is the correct way to do a bulk insertOrUpdate in Slick 3.0? I am using MySQL where the appropriate query would be INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b); MySQL bulk INSERT or UPDATE Here is my current code which is very slow :-( // FIXME -- this is slow but will stop repeats, an insertOrUpdate // functions for a list would be much better val rowsInserted = rows.map { row => await(run(TableQuery[FooTable].insertOrUpdate(row))) }.sum What I am looking for is the equivalent of def insertOrUpdate(values: Iterable[U]): DriverAction

Custom mapping to nested case class structure in Slick (more than 22 columns)

≯℡__Kan透↙ 提交于 2019-11-28 20:36:49
问题 I'm trying to map a DB row with more than 22 columns to a case class tree. I'd rather not using HList as I don't want to work with that API, and also for some exponential compilation time feedbacks that I've read somewhere... I have read this thread answered by Stefan Zeiger: How can I handle a > 22 column table with Slick using nested tuples or HLists? I've seen this test which shows how to define a custom mapping function and I'd like to do that: https://github.com/slick/slick/blob/2.1