scalaquery

Select * or Equivalent in Scala Query

强颜欢笑 提交于 2019-12-11 07:49:03
问题 I've got this ScalaQuery model in Playframework 2 object User { implicit object UserFormat extends Format[User] { def reads(json: JsValue) : User = User( None, (json \ "firstName").as[String], (json \ "lastName").as[String], (json \ "email").as[String] ) def writes(user: User) : JsValue = JsObject(Seq( "firstName" -> JsString(user.firstName), "lastName" -> JsString(user.lastName), "email" -> JsString(user.email)) ) } } object UsersTable extends Table[User]("users") { def id = column[Long](

Select single row based on Id in Slick

坚强是说给别人听的谎言 提交于 2019-12-08 15:07:32
问题 I want to query a single row from user based on Id. I have following dummy code case class User( id: Option[Int], name: String } object Users extends Table[User]("user") { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) def name = column[String]("name") def * = id ~ name <>(User, User.unapply _) def findById(userId: Int)(implicit session: Session): Option[User] = { val user = this.map { e => e }.where(u => u.id === userId).take(1) val usrList = user.list if (usrList.isEmpty) None else

Supertype for scalaquery query

▼魔方 西西 提交于 2019-12-08 05:46:15
问题 What is the supertype for all Scalaquery queries? As far as i have understood, Query[Projection[Product]] should be it, e.g.: Projection2[Int, Int] <: Projection[Tuple2[Int,Int]] <: Projection[Product] so val query: Query[Projection[Product]] = for (all <- Tab) yield all.* should work for Tab = new Table[(Int, Int)] {…} …but appearantly i don’t understand how typing in scala works. I’m totally confused, so if i missed something, please ask. 回答1: This doesn't work because the type parameter

Describing optional fields in Slick

落爺英雄遲暮 提交于 2019-12-07 10:43:32
问题 The Slick DSL allows two ways to create optional fields in tables. For this case class: case class User(id: Option[Long] = None, fname: String, lname: String) You can create a table mapping in one of the following ways: object Users extends Table[User]("USERS") { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) def fname = column[String]("FNAME") def lname = column[String]("LNAME") def * = id.? ~ fname ~ lname <> (User, User.unapply _) } and object Users extends Table[User]("USERS") { def

How to count results with a filter using Slick?

妖精的绣舞 提交于 2019-12-06 19:19:27
问题 I face a problem I would like to simplify : (quite sure, I'm doing it wrong in fact). Wanted I would like to count the number of users having an id = 1. In SQL language let's say it is something like this : SELECT COUNT(*) FROM users WHERE id = 1 Code I'm using Slick in it's "lifted" form, so here is my piece of code counting the users : Query(Users.where( _.id === 1).length).first Actually what happens here is that Slick alias ScalaQuery, is actually creating a subquery with the filter cause

How to parametrize Scala Slick queries by WHERE clause conditions?

廉价感情. 提交于 2019-12-06 17:07:37
问题 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: ??? =>

How to count results with a filter using Slick?

♀尐吖头ヾ 提交于 2019-12-05 02:14:52
I face a problem I would like to simplify : (quite sure, I'm doing it wrong in fact). Wanted I would like to count the number of users having an id = 1. In SQL language let's say it is something like this : SELECT COUNT(*) FROM users WHERE id = 1 Code I'm using Slick in it's "lifted" form, so here is my piece of code counting the users : Query(Users.where( _.id === 1).length).first Actually what happens here is that Slick alias ScalaQuery, is actually creating a subquery with the filter cause and then counting the results of the sub-request. SELECT COUNT(*) FROM (SELECT * FROM users WHERE id =

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 do I model a relational database link-table with Scala?

跟風遠走 提交于 2019-12-04 09:46:31
问题 I need to do quite a few many to many mapping of objects in Scala and save it to a relational database. Here is a fake example to simplify the problem: Let's say we want to model lecture rooms and students. A lecture room may have many students, but those students can also go to different rooms. From a relational database model point of view, you would create 3 tables, one for ROOM, one for STUDENT and one to link students and rooms, ROOM_STUDENT. Let's say the tables look like this: ROOM ---

Play framework + SLICK (Scalaquery) tutorial

▼魔方 西西 提交于 2019-12-04 08:06:44
问题 Does anybody know of a good tutorial or a sample project (github) of using Play framework with SLICK (ScalaQuery)? I am struggling to make them work together. I am getting this error: [info] play - Application started (Dev) [error] application - ! @6b13oi41c - Internal server error, for request [GET /listBooks] -> play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[NoClassDefFoundError: Could not initialize class scala.slick.ast.opt.Relational$]] at play.core