scalaquery

How to use SQL “LIKE” operator in SLICK

て烟熏妆下的殇ゞ 提交于 2019-12-03 23:33:24
Maybe a silly question. But I have not found an answer so far. So how do you represent the SQL's "LIKE" operator in SLICK ? Exactly as you normally would! val query = for { coffee <- Coffees if coffee.name like "%expresso%" } yield (coffee.name, coffee.price) Will generate SQL like SELECT name, price FROM coffees WHERE NAME like '%expresso%'; 来源: https://stackoverflow.com/questions/14700821/how-to-use-sql-like-operator-in-slick

How do I abstract the domain layer from the persistence layer in Scala

走远了吗. 提交于 2019-12-03 04:16:51
问题 UPDATE: I've edited the title and added this text to better explain what I'm trying to achieve: I'm trying to create a new application from the ground up, but don't want the business layer to know about the persistence layer, in the same way one would not want the business layer to know about a REST API layer. Below is an example of a persistence layer that I would like to use. I'm looking for good advice on integrating with this i.e. I need help with the design/architecture to cleanly split

How do I model a relational database link-table with Scala?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 04:01:37
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 ----- id subject STUDENT ------- id name ROOM_STUDENT ------------- room_id student_id If I want to use

How can I present a many-to-many relationship using a link table with ScalaQuery or SLICK?

强颜欢笑 提交于 2019-12-02 18:19:24
I've asked a similar question recently, and got a great reply on solving a many-to-many relationship problem with Lift Mapper. I looked at the ScalaQuery/SLICK documentation but it does not document an approach to persisting data where link tables are involved. If someone knows how to do many-to-many mapping using SLICK, it would be great if you can share it. Jack This test (created by Stefan Zeiger) is new in the SLICK test suite, and answers the question quite nicely: def testManyToMany(): Unit = db withSession { object A extends Table[(Int, String)]("a") { def id = column[Int]("id", O

How do I abstract the domain layer from the persistence layer in Scala

天大地大妈咪最大 提交于 2019-12-02 17:35:27
UPDATE: I've edited the title and added this text to better explain what I'm trying to achieve: I'm trying to create a new application from the ground up, but don't want the business layer to know about the persistence layer, in the same way one would not want the business layer to know about a REST API layer. Below is an example of a persistence layer that I would like to use. I'm looking for good advice on integrating with this i.e. I need help with the design/architecture to cleanly split the responsibilities between business logic and persistence logic. Maybe a concept along the line of

Dynamic OR filtering - Slick

∥☆過路亽.° 提交于 2019-12-01 11:33:36
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? Something like this should work. I had to use a similiar fragment in my own code and it is also close to what cvogt proposes in above comment (I think). val username = Option("")

How to write nested queries in select clause

核能气质少年 提交于 2019-11-30 18:04:58
I'm trying to produce this SQL with SLICK 1.0.0: select cat.categoryId, cat.title, ( select count(product.productId) from products product right join products_categories productCategory on productCategory.productId = product.productId right join categories c on c.categoryId = productCategory.categoryId where c.leftValue >= cat.leftValue and c.rightValue <= cat.rightValue ) as productCount from categories cat where cat.parentCategoryId = 2; My most successful attempt is (I dropped the "joins" part, so it's more readable): def subQuery(c: CategoriesTable.type) = (for { p <- ProductsTable } yield

Slick, how to map a query to an inheritance table model?

混江龙づ霸主 提交于 2019-11-30 07:08:38
Slick, how to map a query to an inheritance table model? i.e, I have table A, B, C A is the "parent" table and B & C are "child" tables What I would like to know is how should I model this using slick so A will be abstract and B & C concrete types, and querying for a row in A will result in a B or C object Something like JPA's InheritanceType.TABLE_PER_CLASS . We need to do couple of things. First find a way to map the hierarchy to an table. In this case I am using a column that stores the type. But you can use some other tricks as well. trait Base { val a: Int val b: String } case class

Slick and filtering by Option columns

旧巷老猫 提交于 2019-11-30 06:40:30
I'm trying to filter against an optional date column with Scala Slick 1.0.1. It may be I just don't see it, but I've got a table that looks something like this: case class UserRole(id:UUID, userID:UUID, role:String) object UserRole extends Table[UserRole]("User_Role") { //(id: Long = 0l, name: String, active: Boolean) extends KeyedEntity[Long] { def id = column[UUID]("ID", O.PrimaryKey) def userID = column[UUID]("user_id") def vendorID = column[UUID]("vendor_id") def role = column[String]("role") def user = foreignKey("user_FK", userID, User)(_.id) def start = column[java.sql.Date]("startDate"

How to make aggregations with slick

a 夏天 提交于 2019-11-30 04:33:14
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 (generates warning: " method max in class ColumnExtensionMethods is deprecated: Use Query.max instead"). How