Filter the query – The Expression Engine

Because Collection represents SQL SELECT query, they offer way to filter the query. Clear offer the Expression Engine, which is inspired by Sequel. It basically helps you to write complex filter conditions without sacrificing on code expressiveness.

The where clause

Filtering by value

In this example, let's assume we are looking for first_name or last_name equals to Richard. There's many ways to write the condition:

# Using the expression engine
User.query.where{ (first_name == "Richard") | (last_name == "Richard") }
# Using the "?" syntax
User.query.where("first_name = ? OR last_name = ?", "Richard", "Richard")
# The tuple syntax
User.query.where("first_name = :value OR last_name = :value", {value: "Richard"})

For very simple queries, using tuple is the way to go:

User.query.where(first_name: "Richard") # WHERE first_name = 'Richard'

For more complex querying with elegance, see below.

Expression Engine: Operators

Example

y = 1
User.query.where{ x != y } # WHERE x != 1
User.query.where{ x == nil } # WHERE x IS NULL
User.query.where{ x != nil } # WHERE x IS NOT NULL
User.query.where{ first_name =~ /richard/i } # WHERE x ~* 'richard'
User.query.where{ first_name !~ /richard/ } # WHERE x !~ 'richard'
User.query.where{ ~(users.id == 1) } # WHERE NOT( users.id = 1 )
circle-exclamation

List of permitted operators: <, >, <=, >=, !=, ==, =~, /, *, +, -

circle-check

Expression Engine: Var, Raw

As explained above, one of the caveats of the expression engine is the variable scope reduction. Basically, whenever a part of the expression can be reduced to his value in Crystal, the Expression Engine will do it, which can lead to some surprises like in this example:

In this example, id will be reduced to the value of the variable id and the comparaison will fail, leading to this:

Thankfully, the expression engine will reject any "static code" and throw an exception at compile time in this case. The good way to do it would be to use var or raw as below:

triangle-exclamation

Range and array and other methods

Expression engine manage natively range, array and other methods as see below.

Range:

Array / Tuples:

AND, OR methods

AND and OR operators are respectively mapped as & and | . As of now, we cannot override the operators && and || in Crystal. Since & and | behave differently in terms of priority order, parenthesis around the condition must be provided.

Last updated