filters

An article by Gaspard Bucher
  1. action
  2. ajax
  3. API
  4. classes
  5. common attributes
  6. conditions
  7. context
  8. dates
  9. display
  10. forms
  11. i18n
  12. meta
  13. SQLiss
    1. introduction
    2. filters
    3. visitor
    4. functions
    5. start
  14. urls

operators

The filters support the following operators:

+ - < <= = >= >
or and lt le eq ne ge gt
like 'not like' match in 'not in'

You can also use parenthesis to group clauses:

images where ((created_at < #{date}) and name like 'foo%') or type = 'Icon' in site

The “in” clause supports mixed clauses (the query below does not make any sense but should give you an idea of what you can do):

images where flag in (#{params[:a]}, #{params[:b]}, '45', 23, "foo#{params[:c]}")

or / and

These operators can be used in filter clauses (where…) or to merge queries like below:

(images where name = 'foo') or (documents where created_at < now - 4 days) limit 5

RubyLess

At some point, you will need to feed your filters with parameters from the url or other information in the current context. To do this, you simply use #{} and the content will be evaluated as RubyLess.

String interpolation:

pages where name like '#{params[:q]}%'

Direct value:

pages where event_at > #{current_date}

If such code makes you worry about SQL safety, you are right. String interpolation is very bad when you are in real SQL. Never do it. Here, however, we are in a sandboxed environment and such interpolations will produce proper bound variables without SQL injection risks.

We really hope that the use the #{} syntax in pseudo-sql will not produce the habit of using the same type of code in non-safe environments (Ruby, Rails).

class / role filtering

These filters use the special fields “class” and “role”.

Find objects that are posts (not sub-classes):

nodes where class = Post

Find objects that are posts (with sub-classes):

nodes where class like Post

Find objects that have the ‘Task’ role:

nodes where role = Task

relation filtering

These filters use the special fields “[relation name]_id”. For example, the following query finds all nodes that have node “33” as “hot” object.

nodes where hot_id = 33 in site