Option columns and condition folding

97 views
Skip to first unread message

Mike Limansky

unread,
Mar 28, 2016, 10:25:49 AM3/28/16
to Slick / ScalaQuery
Hi all,

   I'm trying to use Slick 3.1.1 in my current project and I've faced with following problem. I have schema with optional fields:

 
class Foos(tag: Tag) extends Table[Foo](tag, "foos") {
 
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
 
def a = column[Option[String]]("a")
 
def b = column[String]("b")

 
// and some more fields, both optional and not
}

In a service class I have optional parameters, since some of them can be not defined. I'm trying to reduce conditions to one query, like it shown in the Slick documentation, but it doesn't compile:

def findFoos(a: Option[String], b: Option[String]) = { // and some more parameters here

  val query
= (f: Foos) => List(
    a
.map(f.a === _),
    b
.map(f.b === _)
 
).flatten.reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])

The problem, this cannot be folded. As I understand the reason is that items have different types: Rep[Boolean] for not nullable fields and Rep[Option[Boolean]] for optionals. How should I use it properly?

--
Regards,
Mike.

Naftoli Gugenheim

unread,
Mar 29, 2016, 12:41:55 AM3/29/16
to Slick / ScalaQuery


On Mon, Mar 28, 2016, 10:25 AM Mike Limansky <mike.l...@gmail.com> wrote:
Hi all,

   I'm trying to use Slick 3.1.1 in my current project and I've faced with following problem. I have schema with optional fields:

 
class Foos(tag: Tag) extends Table[Foo](tag, "foos") {
 
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
 
def a = column[Option[String]]("a")
 
def b = column[String]("b")

 
// and some more fields, both optional and not
}

In a service class I have optional parameters, since some of them can be not defined. I'm trying to reduce conditions to one query, like it shown in the Slick documentation, but it doesn't compile:

Does it tell you why it doesn't compile?


def findFoos(a: Option[String], b: Option[String]) = { // and some more parameters here

  val query
= (f: Foos) => List(
    a
.map(f.a === _),
    b
.map(f.b === _)
 
).flatten.reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])

The problem, this cannot be folded. As I understand the reason is that items have different types: Rep[Boolean] for not nullable fields and Rep[Option[Boolean]] for optionals. How should I use it properly?

--
Regards,
Mike.

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/d11d6c9d-e8a7-48ff-9b67-47d73c163a20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Limansky

unread,
Mar 29, 2016, 3:23:54 AM3/29/16
to Slick / ScalaQuery
The error is following:

[error] projects/test/src/main/scala/Service.scala:42: value && is not a member of slick.lifted.Rep[_201]
[error]     ).flatten.reduceLeftOption(_ && _).getOrElse(true: Rep[_])
[error]                                  ^
[error] one error found

I think that since the list contains both Rep[Boolean] and Rep[Option[Boolean]] the type of the list will be List[Rep[Any]] (IntelliJ Idea shows it like List[Rep[_ >: Option[Boolean] with Boolean]]. As result neither booleanColumnExtensionMethods nor booleanOptionColumnExtensionMethods implicit methods doesn't applies to the list values.

I just wondering if there any common solution for such situation. The only idea I have now is to create two separate lists for optional and non-optional fields and merge it by hands, but it looks little bit ugly for me.

вторник, 29 марта 2016 г., 7:41:55 UTC+3 пользователь nafg написал:

Naftoli Gugenheim

unread,
Mar 31, 2016, 10:26:36 PM3/31/16
to Slick / ScalaQuery

First write it out concrete, then see how you can pull out and abstract over the repetitiveness. In this case I believe that might involve a polymorphic (having a type parameter) function. But in order to identify what your can do, let's see how it looks written out concrete and repetitive.


Reply all
Reply to author
Forward
0 new messages