Predicates
All normal filtering is based on fields, see here. However sometimes you may have a completely different filter criteria, that cannot be mapped on fields.
An example is the MySQL full text search. Let's do it:
# #![allow(unused_variables)] #fn main() { #[derive(Toql)] #[toql(predicate(name="search", sql="MATCH (..firstname, ..lastname) AGAINST (? IN BOOLEAN MODE)"))] #[toql(predicate(name="street", sql="EXISTS( SELECT 1 FROM User u JOIN Address a ON (u.address_id = a.id) \ WHERE a.street = ? AND u.id = ..id)"))] struct User { #[toql(key)] id: u64, firstname: String, lastname: String, } #}
With the two predicates above you can seach for users that have a certain name with @search 'peter'
and retrieve all users from a certain street with @street 'elmstreet'.
The question marks in the predicate are replaced by the arguments provided.
If there is only one argument, it can also be used to build an ON predicate in a join. See on aux param.
Reference
The full predicate syntax is
predicate(name="..", sql="..", handler="..", on_aux_param="..", count_filter=true|false)
where
- name is the name of the predicate. It can be called in a Toql query with
@name ... If a predicate is defined on a joined struct, that predicate can be called with a path@path_name ... See predicates in the query for more details. - sql is a raw QL expression. Use
?to insert a predicate param in the SQL,..for the table alias and<aux_param>for an aux param value. - handler allows a custom predicate handler (build SQL with a function).
Provide a function name without parenthesis that return a struct that implement
toql::prelude::PredicateHandler - on_aux_param sets the value of an aux_param. This aux param is only available when building custom joins and can only be used when the predicate takes exactly one argument. See example.
- count_filter determines if a predicate used in Toql query should also be used for count queries.
Default is
false.