slick

Slick: Filtering all records which have a joda DateTime date equal to today

半世苍凉 提交于 2019-12-06 06:16:01
问题 One way to achieve it would be like this: val now = DateTime.now val today = now.toLocalDate val tomorrow = today.plusDays(1) val startOfToday = today.toDateTimeAtStartOfDay(now.getZone) val startOfTomorrow = tomorrow.toDateTimeAtStartOfDay(now.getZone) val todayLogItems = logItems.filter(logItem => logItem.MyDateTime >= startOfToday && logItem.MyDateTime < startOfTomorrow ).list Is there any way to write the query in a more concise way? Something on the lines of: logItems.filter(_.MyDateTime

right usage of slick filter

非 Y 不嫁゛ 提交于 2019-12-06 05:33:01
问题 I'm using slick to access database. I want to query like this: case class Coupon(couponId: Long, shopId: String) class Coupons(tag: Tag) extends Table[Coupon](tag, "coupons"){ def couponId = column[Long]("coupon_id") def shopId = column[String]("shop_id") override def * = (couponId, shopId) <> (Coupon.tupled, Coupon.unapply) } object Coupons extends TableQuery(new Coupons(_)){ def findCouponBy(couponId: Long, shopId: Option[String]) = { val s = DB.createSession() try { val q = for { coupon <-

Scala Type Mismatch issue when using import from different scope

守給你的承諾、 提交于 2019-12-06 05:13:21
In my project(play 2.4 with slick 3.0), I have a setup: using MySQL for production and H2 for unit testing(start to feel that it is a horrible idea). To do that, I have to import slick.driver.JdbcProfile instead of any specific driver (e.g. slick.driver.MySQLDriver ) and import its api._ in my DAO class, like this: class SomeDAO(val context: MyContext){ import context.profile.api._ import context.SOME_TYPE def invoke(para: SOME_TYPE) = { ... } // other DBIO... } Everything is OK so far, however, I have a service class that requires this DAO, and a type in context as a parameter: class

Creating a generic update counter method

℡╲_俬逩灬. 提交于 2019-12-06 04:50:59
I'm trying to create a generic counter update method for a particular table. My table has many columns that are simply counters, and in my application I need to increment/decrement these counters. I was trying to create a method like this: private def updateCounter(column: String, id: Int, incr: Int)(implicit session: Session): Unit = { sqlu"update table1 set $column = $column + $incr where id=$id".first } I would then create a method that would call this (I don't want to expose this method outside of this Dao class). I am getting this error: [PSQLException: ERROR: syntax error at or near "$1"

How to use Slick code generator to include database views as well?

蓝咒 提交于 2019-12-06 04:43:15
问题 I'm trying to generate the Scala code for the database tables and views in my schema using Slick 3.0.3. Taking this blog as example I have the following file build.sbt . However, this will generate code for my database tables and will not include the database views. How can I get the views generated as well? According to slick issue 1022 I see it is possible to do but the API doesn't look alike and slick.codegen.SourceCodeGenerator doesn't have a getTables or defaultTables to include view

Dynamic query parameters in Slick (sorting)

泄露秘密 提交于 2019-12-05 22:40:22
I'm trying to convert anorm queries to slick in one of Play 2.3 samples , but I'm not sure how to implement dynamic sorting. This is the original method: def list(page: Int = 0, pageSize: Int = 10, orderBy: Int = 1, filter: String = "%"): Page[(Computer, Option[Company])] = { val offest = pageSize * page DB.withConnection { implicit connection => val computers = SQL( """ select * from computer left join company on computer.company_id = company.id where computer.name like {filter} order by {orderBy} nulls last limit {pageSize} offset {offset} """ ).on( 'pageSize -> pageSize, 'offset -> offest,

Scala: how to define an abstract copyable superclass for any case class?

纵饮孤独 提交于 2019-12-05 20:32:01
Please bear with me, there is some context until the OP makes sense. I'm using Slick 3.1.x and the slick code generator. btw The whole source code can be found in the play-authenticate-usage-scala github project . For this project I'd like to have a slick generic Dao to avoid repeating the same boilerplate code for every model. I have a postgres sql script that creates the database using evolutions here: 1.sql I then invoke a generator that generates the following data model: Tables.scala To be able to provide generic dao slick implementations for the model classes I need them to comply to

How can I create a custom column type with Typesafe Slick in Scala?

感情迁移 提交于 2019-12-05 19:02:36
问题 I have a PostgreSQL table with an enum , which is created by: CREATE TYPE file_status AS ENUM ('new', 'uploading', 'queued', 'processing', 'done', 'failed'); and an associated field CREATE TABLE files ( ... status file_status NOT NULL, ... ); With Scala 2.10 and Typesafe Slick 1.0.1, I've created mappings to my Files table that work great with the exception of the status field, which requires the custom file_status type, a string. def status = column[FileStatus]("status") I've been playing

Slick 3: how to drop and take on collections with some relations

对着背影说爱祢 提交于 2019-12-05 18:37:48
I'm working with Play! Scala 2.4 and Slick 3. I have a many to many relations as following: class Artists(tag: Tag) extends Table[Artist](tag, "artists") { def id = column[Long]("artistid", O.PrimaryKey, O.AutoInc) def name = column[String]("name") def * = (id.?, name) <> ((Artist.apply _).tupled, Artist.unapply) } The relation table: class ArtistsGenres(tag: Tag) extends Table[ArtistGenreRelation](tag, "artistsgenres") { def artistId = column[Long]("artistid") def genreId = column[Int]("genreid") def * = (artistId, genreId) <> ((ArtistGenreRelation.apply _).tupled, ArtistGenreRelation.unapply

Slick and bonecp: org.postgresql.util.PSQLException: FATAL: sorry, too many clients already error

若如初见. 提交于 2019-12-05 13:47:57
Locally when I am developing my application I launch my play2 application using sbt run I love how I can make code changes, and then reload my browser to see my changes. After about roughly 10 code changes or so, I get a postgresql too many connection error (see below). My db connection is using the below DatabaseAccess.scala class. I'm guessing on each reload it is creating a bunch of connections to postgresql. In my Global am currently doing: override def onStart(app: Application) { Logger.info("Global.onStart") DatabaseAccess.loadConfiguration() } In production I am also worried that if I