Dynamic Queries
It is often necessary to create queries that varies depending on values
known only at runtime.
Dynamically excluding tables (or sub queries) from a Query
The method inhibitWhen(b: Boolean) available on
org.squeryl.Queryable[A]
is meant to be called in a from clause, it will turn a Table[A] or
(sub) Query[A]
into a Queryable[Option[A]] and remove the queryable from the
query
and all expression referencing this queryable.
Example :
Notice the Option[] in the result type of the query, and how the Author
table disappears from the generated SQL base on the input of inhibitWhen:
— first query: searchForBooks(None, “Un Loup est un loup”).toList
h3. Dynamic Where clause construction
The following method builds a query with a dynamically varying where clause, the .? function on the optional parameters has the effect of removing the enclosing condition when the argument is None.
- Note the .? operator is just a shorthand for the inhibitWhen(b: Boolean) methods that applies on any AST node
Observe how following calls affect the generated SQL :
- Note the .? operator is just a shorthand for the inhibitWhen(b: Boolean) methods that applies on any AST node, it translates into a.inhibitWhen(a === None)
The same method using inhibitWhen would be :