Squeryl Dynamic Join Clauses
What I'm looking for seems fairly common but I can't seem to figure it out through the Squeryl api. I need to have a conditional piece to my on statement for a join. def getAllJoin
Solution 1:
If you are simply looking to add a conditional to your where clause, you can do something like this:
on(
t1.secondary === t2.id and
(if(hasFallback.isDefined) t1.fallback.isNotNull else 1 === 1.inhibitWhen(true))
)
The inhibitWhen clause should always prevent the output of 1 === 1 from showing up in the actual SQL, but it allows your if/else to return a LogicalBoolean.
Solution 2:
There is a talk related to a similar question on github and a google group:
https://github.com/max-l/Squeryl/pull/168
https://github.com/max-l/Squeryl/pull/144
https://groups.google.com/forum/#!topic/squeryl/BBAXbtRq9v4
If you're using squeryl-0.9.5-6, then you should find a way out by your own. I chose to do (a similar task) the following way:
/** thin wrappwer with no runtime cost */implicitclassRichLogicalBoolean(val e: LogicalBoolean) extends AnyVal {
def and(condition: Boolean, other: LogicalBoolean) = if (condition) other and e else e
}
after this, I can write a code like:
t1.secondary === t2.id and
(yourCondition, t1.fallback.isNotNull)
Post a Comment for "Squeryl Dynamic Join Clauses"