Skip to main content

Module row_rule

Module row_rule 

Source
Expand description

The values a compiled row rule computes with, and its operators.

The schema generator compiles each row rule a module declares (a condition over one row’s own fields) into a Rust expression over Val, built from the functions here, and evaluates it against the row the write would store, read through the row’s JSON form so enums, decimals and dates arrive as the text and numbers their columns hold.

The semantics are SQL’s, because the same rule also compiles to a CHECK constraint and both must give one answer:

  • A missing or null field is Val::Null, and an operator over a null is null: name.length >= 1 with no name is neither true nor false.
  • &&, || and ! are three-valued: false && null is false, true || null is true, anything else with a null is null.
  • A rule holds unless it is definitely false, as a CHECK passes on null. x == null and x != null test for null itself and are never null.
  • A comparison between a number and a text parses the text, the way a decimal column serialized as "12.50" compares to 0; a comparison between a time and a text parses the text as a timestamp or a date.

A rule may also read the row before the write ($old.x, through field on that row), the writer (actor) and the time of the write (at).

Enums§

Val
One value inside a rule.

Functions§

abs
actor
The writer, as $actor.id reads it; null for a job, a seed or an unscoped caller.
add
and
at
The time of the write, as now() reads it.
ceil
changed
Whether any of fields differs between the row before and the row after a write. A rule about the time of the write checks an update only when it changes what the rule reads, so a later edit of a row whose deadline has passed is not a break.
coalesce
The first value that is not null.
cond
c ? t : e, as SQL’s CASE WHEN c THEN t ELSE e END: a null condition takes e.
contains
a.contains(b): a text containing a text, or a list containing an item.
div
Division by zero is null rather than an error: a rule never panics a write.
ends_with
eq
field
The row’s field name; Val::Null when it is absent or null.
floor
ge
gt
has_role
$actor.has_role(role): null without a writer, without an installed roles source (crate::actor_roles), or when the source cannot tell; the rule then holds.
holds
Whether a rule’s value lets the write through: everything but a definite false.
in_list
a in [..], as SQL’s IN: true on a match, null when a is null or no item matched but one was null, false otherwise.
is_empty
is_not_null
is_null
le
length
A text’s length in characters (code points, as on every target), or a list’s.
lower
lt
matches
a matches 'pattern': whether the text contains a match of the pattern. The generator only emits patterns that compile.
mul
ne
neg
not
not_in
or
referencing
How many rows still reference the row being deleted: sql counts them with the row’s id as $1, run on the request’s own connection. Null outside a request or without an id.
rem
round
starts_with
sub
text
trim
upper