arussinov arussinov - 1 year ago 120
Scala Question

Pattern matching in squeryl queries

I have just started with squeryl and have no answer for how write a query like that

SELECT ref label
FROM x_table
WHERE ref like x% or lable like x%

is some value from the user, especially I haven't found analog of the sign
in squeryl or how can I use it.

My version:

val products = from(AppDB.productTable) (
s => where ((s.label like value) or (s.ref like value)) select(s)

doesn't work correct.

val value : Option[String]
I get from the user.

Answer Source

You can try to optionize your fields, like this:

val products = from(AppDB.productTable) (s => 
  where ((Some(s.label) like value) or (Some(s.ref) like value)) 

That will compile as the query will be comparing an Option[String] to an Option[String]. Squeryl will handle the option state internally.

If you are simply looking to add the wildcard to what you are comparing, assuming both sides of the like clause are of the type Option[String], then you can do this:

s.label like + "%")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download