Query function syntax

The syntax of the query function looks like the SQL syntax, but it works on a more low level
See also Sofawiki Database Primer and Differences between SQL and Sofawiki database

query = (selectstatement | importstatement | wherestatement | groupstatement | orderstatement | limitstatement | UNION | exceptstatement | joinstatement | CROSS | COPY | POP | SWAP | projectstatement | ROTATE | renamestatement | formatstatement | numberformatstatement | calcstatement | templatestatement | outputstatement | stylestatement | FIELDS | SHOWKEY | VERBOSE)+

selectstatement = SELECT fieldlist (FROM (VIRTUAL)? name)? (wherestatement)?

fieldlist = field (, field)*

name = string

wherestatement = WHERE field (operator string | dollaroperator expression)

operator = ($)? (!)? (numericoperator | stringoperator | relaxedstringoperator | otheroperator)

numericoperator = (= | <> | < | > | <= | >=)

stringoperator = (== | =* | *= | *=* | << | >> | >=)

relaxedstringoperator = { ~~ | !~ | ~* | *~ | *~* )

otheroperator = (* | 0 | r=)

dollaroperator = $operator

expression = (field | number | quotedstring | + | - | * | / | . | :: | ABS | SIGN | COPY | POP | SWAP | SUBSTR | REPLACE | TRIM )*

quotedstring = "(.)+(""(.)*)*"

importstatement = IMPORT (TAB|FIELDS) name

groupstatement = GROUP (fieldaggregation) (, fieldaggregation)* BY fieldlist

fieldaggregation = field (COUNT | SUM | MIN | MAX | AVG | NULL)?

orderstatement = ORDER fieldorder (, fieldorder)*

fieldorder = field (NUMERIC)? (DESC)?

limistatement = LIMIT number number

orstatement = UNION

exceptstatement = EXCEPT (field)?

joinstatement = (JOIN | LEFTJOIN | OUTERJOIN) field

projectstatement = PROJECT fieldlist

renamestatement = RENAME (field | * | # ) string (, field string)*

keystatement = KEY expression

formatstatement = FORMAT field string

numberformatstatement = NUMERFORMAT field (number)? (, field (number)? )*

calcstatement = CALC field expression

templatestatement = TEMPLATE field templatename

outputstatement = OUTPUT (HTML | FIXED (fieldlist)* | FIELDS | TAB | TABHEADER | TEXT | TEXTSPACE | ROWTEMPLATE string)

stylestatement = STYLE string