Combining click requests into one request

Using Slick 3.1, how do I combine multiple requests into one request for the same type? This is not a union or union , but a union of query segments to create a single query query. These "segments" may be any individually valid requests.

val query = TableQuery[SomeThingValid] // build up pieces of the query in various parts of the application logic val q1 = query.filter(_.value > 10) val q2 = query.filter(_.value < 40) val q3 = query.sortBy(_.date.desc) val q4 = query.take(5) // how to combine these into a single query ? val finalQ = ??? q1 q2 q3 q4 ??? // in order to run in a single request val result = DB.connection.run(finalQ.result) 

EDIT: The expected sql should look something like this:

 SELECT * FROM "SomeThingValid" WHERE "SomeThingValid"."value" > 10 AND "SomeThingValid"."valid" < 40 ORDER BY "MemberFeedItem"."date" DESC LIMIT 5 
+5
source share
3 answers
 val q1 = query.filter(_.value > 10) val q2 = q1.filter(_.value < 40) val q3 = q2.sortBy(_.date.desc) val q4 = q3.take(5) 

I think you should do something like the above (and skip Query s), but if you insist on going around the "segments" of the query, something like this might work:

 type QuerySegment = Query[SomeThingValid, SomeThingValid, Seq] => Query[SomeThingValid, SomeThingValid, Seq] val q1: QuerySegment = _.filter(_.value > 10) val q2: QuerySegment = _.filter(_.value < 40) val q3: QuerySegment = _.sortBy(_.date.desc) val q4: QuerySegment = _.take(5) val finalQ = Function.chain(Seq(q1, q2, q3, q4))(query) 
+4
source

I used this "template" using slick2.0

 val query = TableQuery[SomeThingValid] val flag1, flag3 = false val flag2, flag4 = true val bottomFilteredQuery = if(flag1) query.filter(_.value > 10) else query val topFilteredQuery = if(flag2) bottomFilteredQuery.filter(_.value < 40) else bottomFilteredQuery val sortedQuery = if(flag3) topFilteredQuery.soryBy(_.date.desc) else topFilteredQuery val finalQ = if(flag4) sortedQuery.take(5) else sortedQuery 
0
source

I think this should work. But I have not tested it yet.

 val users = TableQuery[Users] val filter1: Query[Users, User, Seq] = users.filter(condition1) val filter2: Query[Users, User, Seq] = users.filter(condition2) (filter1 ++ filter2).result.headOption 
-1
source

Source: https://habr.com/ru/post/1238795/


All Articles