Expressions
An expression is a condition or a computed value. The same language is used by where:,
check, require … when, deny when, set … =, var, a note’s when:, the row filter of
an access rule, and the value: of a computed column.
table events "Events" { title text required "Title" starts_at datetime required "Starts" ends_at datetime "Ends" signup_deadline date "Sign-up deadline" kind enum(open "Open", limited "Limited") required "Kind" capacity int "Capacity" { min: 1 max: 500 } published bool "Published" signups count(registrations.event) "Sign-ups"
places_left int "Places left" { value: capacity - signups } full bool "Full" { value: signups >= capacity } fill_percent int "Filled (%)" { value: round(ratio(signups * 100, capacity), scale: 0, ties: up) }
check ends_at >= starts_at "The end must not be before the start." require capacity when kind = limited "A limited event needs a capacity."}
page event "Event" { route: /events/{id} access: member
data event of events where published var days_left = days_until(:event.signup_deadline)
note "Sign-up closes soon" { style: warning when: :days_left >= 0 and :days_left <= 7 text: "{:days_left} days are left to sign up." }
form of registrations { fields: name set event = :event.id deny when event.full "This event is full." deny when :days_left < 0 "Sign-up has closed." }}The example needs a role member and a table registrations with a ref(events) column
event and a text column name. Each part of it has a section below.
Where an expression can be used
Section titled “Where an expression can be used”Each site gives an expression a different set of values. The compiler refuses a value that the site cannot read, and the message names the site.
| Site | The row | :user.… |
:route.… |
data rows and vars |
|---|---|---|---|---|
check, require … when |
the row of the table | no | no | no |
value: of a computed column |
the row of the table | yes | no | no |
set and deny when in a transition |
the row that moves | yes | no | no |
the row filter of a page’s, a region’s or a note’s access |
the rows of the page’s tables | yes | yes | no |
the row filter of a right |
the rows of the right’s table | yes | no | no |
data … where |
the row that the page loads | yes | yes | no |
var |
none | yes | yes | yes, a var only reads the vars above it |
a note’s when: |
none | yes | yes | yes |
a region’s where: |
the rows of the region’s table | yes | yes | yes |
set and deny when in a form |
the row that the form saves | yes | yes | yes |
A check or a require … when can also read the other rows of its table that share a key, with
exists and count. Inside those two, :this.<column> names a column of
the row that the rule judges. No other site reads :this.
Every site can read today and now. Most sites can also call dev(); the
refusals list the ones that cannot. “The row” includes the rows that its ref columns point
to, its computed columns and its aggregates.
A form’s set values and their when conditions read the candidate row before any sets apply:
stored values, defaults, then submitted fields. Sets do not read each other’s results.
A form’s deny when reads the completed row after all sets apply. See
set for an example.
Two more sites use a smaller part of the language, because the browser evaluates them:
show_when:reads only the fields of its form.suggest:is onedatetimefield,+, and one duration, such asstarts_at + 1h.
Values
Section titled “Values”Column names and paths
Section titled “Column names and paths”A bare name is a column of the row. A dotted name is a path: each step goes through a ref
column to the row that it points to, or through a one column to the
row that points here.
where: capacity > 10where: event.capacity > 10where: registration.event.organizer.name = "Anna"A path can go through at most 4 ref or one columns, and each one counts as one step. The
compiler refuses a longer path and names the number of steps. Store the value closer to the row, or aggregate over a relation instead.
Write the name of the column as it is declared. The ref column event is event, and
never event_id.
A path can end at a stored column, a computed column or an aggregate. The compiler refuses these ends:
| Refused | Write instead |
|---|---|
a step through a column that is not a ref |
a path through the ref columns that the message names |
a multi or a list column |
exists, or an aggregate such as count |
a one column on its own |
a column of its row, such as report_grade.points, or report_grade.id is empty |
an img column |
a condition on another column |
A bool column alone is a condition. where: published shows the published rows. An empty
bool counts as false.
The lifecycle column of a table has two flags that you can read with a dot. status.public and
status.gone are true when the row is in a state with that flag. To test one state, compare the
column like any enum: status = published, or status in (draft, review) for more than one
state. The compiler refuses status.published, and the message shows status = published. See
lifecycle.
Literals
Section titled “Literals”| Literal | Example | Type |
|---|---|---|
| a whole number | 10, -5, 0 |
whole number |
| a number with up to 4 decimal places | 14.5, -0.25, 2.50 |
a number with that many places |
| text in double quotes | "Jazz", "Say \"hello\"" |
text |
true, false |
published = false |
condition |
a value of an enum |
kind = limited |
the enum of the column on the other side |
A number must fit in int64: at most 9223372036854775807. An amount of money is a whole number
of minor units, so 12.50 euros is 1250. The same holds for an aggregate over money: it shows
as an amount, and an expression reads it in minor units.
An enum value is a bare word. It is a value only on the other side of a comparison with an
enum column, including a page data column such as :mark.area = oral. The same member names
work in in and not in lists, such as :mark.area in (oral, project). If the word is not a
value of that column, the compiler names the nearest value.
A number with a decimal point is exact: the engine stores 14.5 as the whole number 145 with
one place. It has the places you write, so 2.50 has two. The compiler refuses a number with
more than four places. Beside an amount of money it refuses a decimal point, because money is
in minor units there: write price <= 999 for 9.99 euros. See Numbers with places.
The language has no date or time literals.
A duration such as 30min, 1h or 7d is accepted only in suggest:. Everywhere else the
compiler refuses it. So an expression cannot add a time span to a date or a datetime.
today and now
Section titled “today and now”| Value | Type | Is |
|---|---|---|
today |
date |
the current date in the application’s time zone |
now |
datetime |
the current instant, in UTC |
The engine reads the clock once per request. So every expression of one request has the same
today and the same now.
The application’s timezone: decides which day today is. A page’s
timezone: and the reader’s time zone do not change today or now. They also do not change
which rows a region shows. Stored datetime values are always UTC.
Page values
Section titled “Page values”A value that starts with : comes from the request or from the page.
| Value | Is |
|---|---|
:user.id |
the id of the login. Empty for a visitor who is not signed in. |
:user.<profile>_id |
the id of the login’s row in a profile, such as :user.member_id. Empty when the login has no row in that profile. |
:route.<parameter> |
a parameter of the page’s route, as the URL wrote it |
:<data row>.<column> |
a column of the page’s data row, such as :event.id or :event.title |
:<var> |
a page var, such as :days_left |
The compiler refuses these page values:
:user.role. A login can hold several roles. Test a role with an access rule.- a
:user.…value that no profile supplies. The message names the values that exist. - a
:route.…parameter that the route does not have. The message names the route’s parameters. :<var>.<something>. A var is one value and has no columns.- a var that is declared below the expression that reads it.
:<data row>.<column> reads one stored column. The compiler accepts more than the engine can
read. When the engine opens the application, it refuses these data-row values in an expression:
- a computed column or an aggregate, such as
:event.full; - a path through a
ref, such as:event.organizer.name; - a state test, such as
:event.status.public; - the whole row, such as
:event, with no column.
Read such a value through the row that refers to it. On a form of registrations, write
deny when event.full, and not deny when :event.full.
Operators
Section titled “Operators”Comparisons
Section titled “Comparisons”<value> = <value><value> != <value><value> < <value><value> <= <value><value> > <value><value> >= <value>A comparison has two sides and gives a condition. The compiler refuses a chain such as
1 < capacity < 10. Write capacity > 1 and capacity < 10.
| Type | = and != |
<, <=, >, >= |
Can be compared with |
|---|---|---|---|
number: int, money, decimal, an aggregate |
yes | yes | a number. Different places are compared exactly |
text: text, long text, markdown, email, slug |
yes | yes | text |
date, datetime |
yes | yes | a date or a datetime |
enum |
yes | no | a value of the enum |
bool, a condition |
yes | no | a condition |
ref |
yes | no | another ref, or an id: :user.member_id, :event.id, a whole number |
Two references compare their row ids. For example, department.head = employee tests whether
the employee is the department head. Use != to exclude that employee.
:user.id and :user.<profile>_id are ids, and compare like a ref. A :route.… value has no
declared type, so it can be compared with any type.
Text compares exactly: "Anna" = "anna" is false. The order of < is the order of the
UTF-8 bytes of the text, not the alphabetical order of a language:
| Comparison | Result | Reason |
|---|---|---|
"Zoe" < "adam" |
true | A capital letter comes before a lower-case letter. |
"Zimmer" < "Ärger" |
true | Ä comes after every letter of the English alphabet. |
"Öztürk" < "Zimmer" |
false | Ö comes after Z. |
A listing’s sort: is different. It orders text with German rules, so a report lists “Öztürk”
before “Zimmer” even though where: name < "Zimmer" leaves “Öztürk” out.
The compiler warns for <, <=, > and >= when a side is text. The warning does not stop an
install. The message shows the position of the operator:
pages/members.mtd:8:19: warning: "<" compares text by its UTF-8 bytes, so "Zoe" < "adam" is true and "Ärger" comes after "Zimmer" compare a date, a datetime or a whole number instead, or use = or in for an exact match; keep the comparison if byte order is what you mean (a listing's sort: still orders text in German, spec §11.5)To remove the warning, compare a date, a datetime or a number, or use = or in. If byte
order is what you mean, keep the comparison. The warning is not repeated for =, != or in.
A date compared with a datetime counts as the start of that day in UTC. So
signup_deadline >= now is false during the whole deadline day.
The compiler refuses these comparisons:
- two values of different types, such as text with a whole number.
<,<=,>or>=on anenum, aboolor aref. These have no order.- a
ratio(…)that noround(…)encloses. betweenandcontains. The message shows the spelling to use instead.
is empty and is not empty
Section titled “is empty and is not empty”where: ends_at is emptywhere: organizer is not emptyis empty is true when the value is empty. A form stores an empty field as empty, and never as
an empty text. is empty and is not empty are never empty themselves.
The compiler refuses is empty on a multi or a list column. Write not exists(tags) to find
rows with no tags.
and, or and not
Section titled “and, or and not”where: published and starts_at >= nowwhere: kind = open or capacity > 100where: not fulland and or join two conditions. not reverses one condition. The compiler refuses a whole
number, a text or a date on either side, and tells you to compare the value or to test it with
is empty.
in and not in
Section titled “in and not in”where: status in (archived, draft)where: points not in (1, 2, 3, 4)check grade in (1, 2, 3, 4, 5, 6) "A grade is 1 to 6."require reason when area not in (written) "Give a reason."in tests a value against a list of values. x in (a, b, c) means x = a or x = b or x = c,
and x not in (a, b) means x != a and x != b. The compiler changes the list into these
comparisons, so the list works in every place that accepts a comparison, show_when: included.
The result is the same as when you write the comparisons yourself.
The left side can be any value that a comparison accepts. Each item in the list must be a value:
a value of the enum on the left, a whole number, a quoted text, today, now, true, false
or a page value such as :route.status. The compiler checks each item as it checks x = item,
and an error points to the item.
An empty value on the left follows the rule for comparisons. In a where:, x in (…) and
x not in (…) are both false, so a row with an empty value is not listed. In a check, the
list has no answer and the save is accepted. See Empty values.
A where: that names a gone state in a list, such as status in (archived, draft), names that
state as its chain of comparisons does. The region must then say gone: show, or the compiler
refuses the where:. See state.
The compiler refuses these lists:
- an empty list,
(). - a list with the same item two times. Numbers compare by value, so
0and-0are the same. - a list of more than 64 items. Keep a longer set in a table, and ask
exists(…). - a column name, a path such as
course.max, arithmetic or a function call as an item. - a list without parentheses, such as
in scale(…)orin :subscription.categories. For a set of rows, writeexists(…).
Arithmetic
Section titled “Arithmetic”places_left int "Places left" { value: capacity - signups }total money "Total" { value: quantity * unit_price }The language has three arithmetic operators: +, - and *. All three work on numbers and
are exact. There is no division operator. To divide, write ratio inside one
round.
The compiler refuses these:
/. The message showsround(ratio(a, b), scale: 2, ties: up).- text, a date, a datetime or a condition as an operand.
- a result that can be larger than int64. See Whole numbers and overflow.
moneyand a number with places in one sum, difference or comparison. See Numbers with places.
If one operand is empty, the result is empty.
Numbers with places
Section titled “Numbers with places”A decimal column and a literal such as 14.5 have decimal
places. The engine calculates with whole numbers, so every number carries its places, and the
compiler applies these rules. Each rule is exact.
| Expression | Places of the result |
|---|---|
a + b, a - b |
the larger number of places. The other value is multiplied by 10, 100, … |
a * b |
the places of a plus the places of b |
a comparison, in |
compared at the larger number of places |
coalesce(a, b) |
the largest number of places among the values |
ratio(a, b) |
the two values are brought to the same places, so the division is exact |
round(…, scale: n, …) |
n |
sum, min, max of a decimal |
the places of the column, plus the places of a weight: |
So a whole number beside a decimal is in the decimal’s units. raw_points <= 20 on a
decimal(1) means 20.0, and raw_points <= 14.5 means 14.5.
check raw_points is empty or raw_points <= max_points "More points than the test allows."half_max decimal(2) "Half" { value: max_points * 0.5 }money is in minor units in an expression, so the compiler refuses a number with places beside
it: price <= 9.99 would mean nine cents. Write price <= 999.
A value that goes into a column gets the column’s places. This is true for a computed column’s
value: and for a set. Fewer places are multiplied exactly. More places are a compile error,
because the compiler does not round without a rule: write
round(ratio(<value>, 1), scale: 1, ties: up). A var keeps its places and shows them, at most
four.
Precedence
Section titled “Precedence”The operators bind in this order, from the loosest to the tightest:
| Order | Operators |
|---|---|
| 1 | or |
| 2 | and |
| 3 | not |
| 4 | =, !=, <, <=, >, >=, is empty, is not empty, in, not in |
| 5 | +, - |
| 6 | * |
| 7 | values, function calls, ( … ) |
So a or b and c means a or (b and c), and not a = b means not (a = b). Use parentheses
to group differently. The operators of one level apply from left to right.
Empty values
Section titled “Empty values”A comparison with an empty value is false. capacity > 10 is false for a row with no
capacity, and not (capacity > 10) is true for that row. This applies to where:, access rules,
deny when, set … when, a note’s when:, show_when: and computed values.
A table rule is different. In check and in the condition of require … when, a comparison with
an empty value has no answer, and a rule with no answer does not refuse the save. So
check ends_at >= starts_at accepts an event with no end. Read
Empty values in a rule.
To treat an empty value in a special way, say so. Use is empty, is not empty or
coalesce.
Whole numbers and overflow
Section titled “Whole numbers and overflow”Every number in the language is a whole number in int64. SQLite does not report an overflow: it changes the result to a floating-point number. So the compiler calculates the range of every arithmetic result from the ranges of its operands. It refuses an expression whose range does not fit in int64.
The range of an operand comes from its declaration:
| Operand | Range that the compiler uses |
|---|---|
int or money with min: and max: |
the declared range. min: and max: accept -2147483648 to 2147483647. |
int or money with no min: or max: |
-2147483647 to 2147483647 |
decimal(n) |
its min: and max:, or -2147483647 to 2147483647, in the stored whole number: 14.5 in a decimal(1) is 145 |
bool |
0 to 1 |
count |
0 to 10000 |
sum |
the value’s range × 10000 rows, narrowed by min: and max: |
min, max |
the range of the column that it reads, narrowed by min: and max: |
days_until, days_since |
-3652500 to 3652500 |
So two undeclared int columns can be multiplied, and three cannot. The refusal names the
operand that has no declared range:
the product cannot be bounded: its operands are -4611686014132420609…4611686014132420609 and -2147483647…2147483647 declare the domain of "score" — …To fix it, declare min: and max: on that column, one property on each line. For
round, a smaller scale: also helps.
The engine checks every arithmetic result again when it reads it. If a result is not a whole number, the engine refuses to show it, and the request fails.
coalesce
Section titled “coalesce”coalesce(<value>, <value>, …)coalesce gives the first of its values that is not empty.
where: coalesce(capacity, 0) < 10where: coalesce(ends_at, starts_at) >= now- It takes at least 2 values. All values must have one type, and the result has that type.
- The result is empty only when every value is empty.
- It takes no named arguments.
The compiler refuses fewer than 2 values, values of different types, and a ratio(…).
days_since
Section titled “days_since”days_since(<date or datetime>)days_since gives the number of whole days from a date to today. It is positive for a date in
the past and negative for a date in the future.
where: days_since(joined_on) >= 365- The argument is one
dateordatetime. For adatetime, the function uses the calendar day of the stored UTC value. - The result is a whole number from -3652500 to 3652500.
- The result is empty when the argument is empty.
The compiler refuses more or fewer than 1 argument, and an argument that is not a date.
days_until
Section titled “days_until”days_until(<date or datetime>)days_until is the reverse of days_since. It gives the number of whole days from today to a
date. It is positive for a date in the future and negative for a date in the past.
data event of eventsvar days_left = days_until(:event.signup_deadline)The argument, the result and the refusals are the same as for days_since. An
event with no deadline has an empty days_left, so a note with when: :days_left <= 7 does not
show.
dev()dev() is a condition. It is true when the engine runs with -dev, and false on every other
node. It takes no arguments, and you always write the parentheses.
page debug "Debug" { route: /debug access: admin where dev()
note { when: dev() text: "This page exists only in development." }}The engine reads the mode once per request and binds it as a parameter, like today and now.
The compiled model is the same for both modes. A page, a region or a note whose condition is
false is absent, and a denied page answers like any denied page: see
Show a page only in development.
dev() can be used in:
- a page’s, a region’s or a note’s
accessrule withwhere, and aright’s row filter; where:of a region and of adatarow;- a
var, and thewhen:of a note; deny whenof a form and of a transition, and thewhenof aset.
The compiler refuses dev() where the answer would have to be the same on every node:
| Refused in | Why |
|---|---|
check and require … when |
the rule holds for every writer, including an import or a hook that has no request |
the value: of a computed column, and so in a named condition |
the value is derived on every node that reads it |
the where: of an aggregate column |
a count would depend on the node that reads it |
default: |
a default is stored with the row |
the value of a set |
the value is stored with the row. Refuse the write with deny when dev() instead. |
show_when: |
the browser evaluates it, and the browser does not know the mode |
a page that a nav lists |
the menu asks who may open a page, not whether its rows are there, so production would list a link that answers 404. Leave the page out of the nav. |
A page whose access condition is false in production because of dev() alone answers 404, also
when it only lists rows. admin where dev() and admin where status.open and dev() are such
conditions. admin where dev() or status.open is not: it still narrows the page’s lists and the
page opens.
dev, :dev, dev(x) and dev() = true are compile errors. The repair is dev(), on its own,
joined with other conditions by and, or and not.
deny when dev() refuses a save or a move in development, for example in front of a transition
with a real-world effect. deny when <rule> and dev() protects nothing in production, because
dev() is false there. Write the real rule without dev().
exists
Section titled “exists”exists(<relation>)exists(<relation> where <condition>)exists is true when the relation holds at least one row. With where, the row must also meet
the condition.
where: exists(tags)where: not exists(tags where name = "Archive")where: exists(course.enrolments where student = :user.student_id)- The relation is a
multior alistcolumn of the row. A path throughrefcolumns can come before it, such ascourse.enrolments. - Inside the
where, a bare name is a column of the relation’s table. Page values mean what they mean outside. - The result is a condition, and never empty.
- In an application with a scope,
existssees only the rows in the chosen scope of a scoped table. It does not apply the reader’s access rules. In a table rule,existsdoes not apply the scope either. Seeexistsin a table rule.
The compiler refuses these:
- a relation that is a table name. Declare a
listcolumn and name it. - a
refcolumn or another column that is not a relation. The message names the relation columns of the table. - a
wherepart that is not a condition. existsin avaror a note’swhen:. These have no row.
exists in a table rule
Section titled “exists in a table rule”In a check or in the condition of a
require … when, exists asks about the other rows of the table
that share a key with the row being saved. The where can name that row with :this.<column>:
check status != confirmed or not exists(room.bookings where status = confirmed and id != :this.id and starts_at < :this.ends_at and ends_at > :this.starts_at) "This room is already booked in that time."The example needs a ref column room and a list(bookings.room) column bookings on the
rooms table. A rule is one line, so write it on one line.
:this.<column>is one stored column of the row that the rule judges. It has the type of the column. Arefcolumn is named by its field::this.room, and never:this.room_id.:this.idis empty for a row that does not exist yet. The engine reads it as0, and no row has the id0. Soid != :this.idis true for every stored row when the rule judges an insert, and it leaves the row out of its own comparison when the rule judges an update.- The compiler refuses
:thisoutside thewhereof anexists(…)or acount(…)in a table rule. In the rule itself, write the column without the colon:ends_at. Inside thewhere, a bare name is a column of the related rows. :thisdoes not step.:this.shift.neededis refused. Write the walk in the rule, outside the aggregate:shift.needed.:thisnames a stored column. The compiler refuses alistormulticolumn, an aggregate, a computed column, animgorfilecolumn, and a column that the engine writes (such ascreated_at). The message lists the stored columns of the table.- The compiler refuses a
sensitivecolumn in thewhereofexists, in a table rule. Whether other rows pass a test on that column shows in the refusal that every writer reads. - A rule belongs to the data, so its
existsignores the person who saves. It applies no access rule, no app scope, and no:user. The compiler refuses:user.…and:route.…in a table rule.existsin a rule sees rows in agonestate. - When the row has no key (the
refbefore the relation is empty), theexistsmatches no row. Sonot exists(…)is true and the rule passes. To require a key, add a rule such ascheck room is not empty or resource is not empty "A booking needs a room or equipment." - Inside the
where, a comparison with an empty value is false. A row with an emptystarts_atorends_atoverlaps nothing. Declare such a columnrequiredwhen the rule needs it. - A comparison of
starts_at < :this.ends_at and ends_at > :this.starts_atis half-open. A booking that starts when another ends does not overlap it.
count(<relation>)count(<relation> where <condition>)count is the number of rows in a relation. It is a whole number, and it has a value in a
check or in the condition of a
require … when only. In a rule, compare it with a number or with a
column:
check status != confirmed or count(shift.roster where status = confirmed and id != :this.id) < shift.needed "This shift already has all the volunteers it needs."The example needs a ref column shift and a list(assignments.shift) column roster on the
shifts table, and an int column needed on shifts.
- The relation, the
whereand:thisfollow the rules ofexistsin a table rule. A path ofrefcolumns can come before the relation. countleaves out the rows in agonestate.existsincludes them. To ask about agonerow, useexists.counthas nogone: show. The compiler refuses awherethat names agonestate of the relation’s table.- A
countignores the person who saves, asexistsdoes in a rule.
The compiler refuses these:
count(…)anywhere other than a table rule. To store or show a count, declare an aggregate column such asbookings_n count(bookings) { where: … }on the table that owns the relation, and read it by name.- a relation that is a table name, or a column that is not a
listor amulti. - a
gonestate in thewhere, as described above. - a
sensitivecolumn in thewhere.
ratio(<numerator>, <denominator>)ratio is the only division in the language. It gives an exact fraction of two whole numbers.
A fraction cannot be stored, shown or compared, so the only place for a ratio is directly
inside a round.
reading_minutes int "Reading time" { value: round(ratio(words, 200), scale: 0, ties: up) }- The numerator and the denominator are whole numbers.
- The division happens once, in the
round, with exact values. Nothing is rounded before that.
The compiler refuses a ratio with more or fewer than 2 arguments, an argument that is not a
whole number, and a ratio inside a ratio. It also refuses a ratio in any place other than
directly inside a round: at the end of an expression, beside an operator, in a comparison, in
coalesce and before is empty.
round(ratio(<numerator>, <denominator>), scale: <places>, ties: <rule>)round divides a ratio and rounds the result to a number with scale: places.
average decimal(2) "Average" { value: round(ratio(points, weight), scale: 2, ties: up) }percent int "Percent" { value: round(ratio(done * 100, total), scale: 0, ties: even) }scale: and ties: are named arguments, and both are mandatory.
| Argument | Values | Effect |
|---|---|---|
scale: |
a whole number from 0 to 9 | the number of decimal places that the result keeps |
ties: |
up, even or threshold(<n>) |
what happens to the part after the last kept place |
The result is a number with scale: decimal places. So
round(ratio(171, 18), scale: 2, ties: up) is 9.50, and a decimal(2) column stores it as it
is. Into an int column the same result is the whole number 950, and the compiler warns and
suggests decimal(2).
ties: |
Rounds up when the rest is | Example at scale: 0 |
|---|---|---|
up |
one half or more | 2.5 is 3, -2.5 is -2 |
even |
more than one half. At exactly one half, to the even number. | 2.5 is 2, 3.5 is 4 |
threshold(<n>) |
n hundredths or more |
with threshold(60), 2.59 is 2 and 2.60 is 3 |
- The result is empty when the numerator or the denominator is empty.
- The result is empty when the denominator is 0.
The compiler refuses these:
- a
roundwith noscale:or with noties:. - a first argument that is not a
ratio. A whole number needs no rounding. - a
roundinside around. An expression is rounded once, at the end. - a
scale:that is not a whole number from 0 to 9. - a
ties:that is notup,evenorthreshold(<n>). - a rounding whose multiplied numerator does not fit in int64. See Whole numbers and overflow.
threshold
Section titled “threshold”threshold(<hundredths>)threshold is a value of ties: in round, and nothing else. threshold(60) rounds
up from 60 hundredths of the last kept place. threshold(50) is the same as ties: up.
grade int "Grade" { value: round(ratio(points, weight), scale: 0, ties: threshold(60)) }The compiler refuses threshold outside ties:, a threshold with more or fewer than 1
argument, and a value that is not a whole number from 1 to 99.
Named conditions
Section titled “Named conditions”A computed bool column is a named condition. Write the condition once, on the table, and use
the column name wherever the condition applies.
counts bool "Counts towards the average" { value: missed != excused and missed != makeup_pending }A where:, a check, a deny when and the row filter of an access rule can then write
counts. A report can show the column, sort by it and export it, like a stored column.
A computed column has no stored value. It is read-only and never appears in a form. The compiler
refuses a computed column that reads itself, directly or through other computed columns. A
computed column that reads a sensitive column must be sensitive too. See
Sensitive columns are granted per role.
What the compiler refuses
Section titled “What the compiler refuses”Every refusal has a file:line:column and a second line that tells you what to write. These are
the refusals that apply to all sites:
| Refused | The message |
|---|---|
| a column that the table does not have | names the table, the step of the path, and the nearest column name |
event_id instead of event |
names the declared name |
a path through more than 4 ref columns |
names the number of steps |
| a function that does not exist | names the nearest function, or lists the 8 functions |
a named argument on a function other than round |
tells you to write the arguments in order |
| a value that the site cannot read | names the site and what it can read. See Where an expression can be used. |
values of two types in one comparison or one coalesce |
names both types |
a number with more than 4 decimal places, /, or a duration outside suggest: |
shows what to write instead |
| a value with more places than the column it goes into | shows the round(...) to write |
money beside a number with decimal places |
tells you to write the amount in minor units |
| a number larger than int64 | names the largest number |
| an arithmetic result that can overflow int64 | names the operand to declare with min: and max: |
The compiler also refuses words that other languages have and this one does not. Instead of
between, write two comparisons. Instead of contains, write exists(…). in takes only a
list of values in parentheses; for a set of rows, write exists(…). The language has no text concatenation, no conditional expression and no
functions of your own. A condition on a note is its when:, and a condition on a save is a
deny when. Anything more is a hook.