I'm trying to use Squeryl (0.9.4 for scala 2.8.1) dynamic queries (.?
and inhibitWhen(...)
). They are working fine while I'm using String/Int/whatever fields, but seem to interfere with squeryl's syntax sugar for boolean cond开发者_开发百科itions.
Assuming we have a is_trusted: Option[Boolean]
defined somewhere, the following code
where ( obj =>
obj.is_trusted === is_trusted.?
)
does not compile, throwing the following error:
... type mismatch;
[error] found : org.squeryl.dsl.ast.LogicalBoolean
[error] required: org.squeryl.dsl.NonNumericalExpression[org.squeryl.PrimitiveTypeMode.BooleanType]
[error] obj.is_trusted === is_trusted.?
[error] ^
even this one does not work, failing on the first condition:
where ( obj =>
obj.is_trusted.inhibitWhen(is_trusted == Some(true)) and
not(obj.is_trusted).inhibitWhen(is_trusted == Some(false))
)
The only working version uses double not
as a hint for the compiler:
not(not(obj.is_trusted)).inhibitWhen(is_trusted != Some(true)) and
not(obj.is_trusted).inhibitWhen(is_trusted != Some(false))
Is there a more sane way to do dynamic queries with booleans?
Hmm... I think this is probably another bug caused by an implicit conversion from Boolean -> LogicalBoolean. That feature has been deprecated in 0.9.5 because of issues similar to this. What the .? should do is trigger an implicit conversion from Boolean -> BooleanExpression but since LogicalBoolean has a .? method as well there is a conflict and the latter appears to be have precedence. I know it's not terribly pretty, but try this:
where ( obj =>
obj.is_trusted === is_trusted.~.?
)
The .~ should force the conversion to BooleanExpression[Option[Boolean]] before .? is invoked.
精彩评论