Skip to main content

Expression Syntax

XINA expressions translate to MySQL expressions, which are evaluated as a query is executed.

All expressions have a standard form as a JSON object, with a type property specifying the expression type, and additional properties as needed by that expression type.

Additionally, certain expression types may be represented using a short form, which is formatted as a JSON object with a single property prefixed with the $ character.

Literals

Literal expressions represent a single, discrete value.

Null

The MySQL NULL value. May also be specified with the JSON null value.

PropertyValue
type"null"

Example (as object)

{ "type": "null" }

Example (as JSON literal)

null

Number

A numeric literal value. The value may be provided as a native JSON number, or encoded as a string. May also be provided directly as a JSON number value.

PropertyValue
type"number"
valuenumber or string

Example (as object)

{
 "type"  : "number",
 "value" : 123
}

Example (as JSON literal)

123

String

A string literal value. May also be provided directly as a JSON string.

PropertyValue
type"string"
valuestring

Example (as object)

{
 "type"  : "string",
 "value" : "foo"
}

Example (as JSON literal)

"foo"

Datetime

A datetime literal value. Interpreted by the database as Unix time in milliseconds.

PropertyValue
type"datetime" or "dt"
valueinteger or string

If the value provided is an integer it must be the number of milliseconds since the Unix epoch. If the value is a string it must be encoded according to the following syntax, taken from the ISO8601 standard:

date-opt-time     = date-element ['T' [time-element] [offset]]
date-element      = std-date-element | ord-date-element | week-date-element
std-date-element  = yyyy ['-' MM ['-' dd]]
ord-date-element  = yyyy ['-' DDD]
week-date-element = xxxx '-W' ww ['-' e]
time-element      = HH [minute-element] | [fraction]
minute-element    = ':' mm [second-element] | [fraction]
second-element    = ':' ss [fraction]
fraction          = ('.' | ',') digit [digit] [digit]
offset            = 'Z' | (('+' | '-') HH [':' mm [':' ss [('.' | ',') SSS]]])

If the offset is not provided the timezone will be assumed to be UTC.

Supports shorthand syntax with the $dt property.

PropertyValue
$dtinteger or string

Local Datetime

A local datetime literal value.

PropertyValue
type"localdatetime" or "ldt"
valuestring

The value must be encoded according to the same syntax as the datetime literal, except with the offset omitted.

Supports shorthand syntax with the $ldt property.

PropertyValue
$ldtstring

Local Date

A local date literal value.

PropertyValue
type"localdate" or "ld"
valuestring

The value must be encoded according to the same syntax as the date-element in the datetime literal.

Supports shorthand syntax with the $ld property.

PropertyValue
$ldstring

Local Time

A local time literal value.

PropertyValue
type"localtime" or "lt"
valuestring

The value must be encoded according to the same syntax as the time-element in the datetime literal.

Supports shorthand syntax with the $lt property.

PropertyValue
$ltstring

Columns

Column expressions specify a column of a table. Although each column type has a separate full syntax, there is a shorthand syntax with the $col property, which infers the column type based on the content.

PropertyValue
$colstring column
column          = system-column | database-column
system-column   = system-table-name '.' system-parameter-name
database-column = database-path ['@' database-table-name] '.' (parameter-name | attribute-name | field-name | blob-attribute)
blob-attribute  = blob-name ':' blob-attribute-name

Examples

  • user.email : email parameter of the user system table
  • a.b.record_id : record_id attribute of the record table of database b in group a
  • a.b@trash.record_id : record_id attribute of the trash table of database b in group a
  • a.b.c : c field of the record table of database b in group a

System Parameter Column

Specifies a column of a system table.

PropertyValue
type"column"
tablestring
columnstring

Database Parameter Column

Specifies a parameter column of a database table.

PropertyValue
type"column"
databasedatabase specifier
tablestring table name
columnstring parameter name

Database Attribute Column

Specifies an attribute column of a database table.

PropertyValue
type"column"
databasedatabase specifier
tablestring table name
columnstring attribute name

Database Field Column

Specifies a field column of a database table.

PropertyValue
type"column"
databasedatabase specifier
tablestring table name
columnfield specifier

Alias

Although the alias is not technically a column, it can refer directly by name to any column in the source, or to an alias of a result column.

PropertyValue
type"alias"
valuestring

Supports shorthand syntax with the $alias property.

PropertyValue
$aliasstring

Evaluations

Evaluations are expressions evaluated by MySQL.

Between

Returns true if the expression e is between min and max.

PropertyValue
type"between"
eexpression
minexpression
maxexpression

Supports shorthand syntax with the $between property. Takes a JSON array of exactly 3 expressions, in the order e, min, and max.

PropertyValue
$betweenarray of three expressions

Binary

Binary operation, evaluated as e1 op e2.

PropertyValue
type"binary"
opstring
e1expression
e2expression

Valid binary operators are as follows:

OperatorDescription
andlogical AND
orlogical OR
=equal
!=not equal
>greater
>=greater or equal
<less
<=less or equal
istest against NULL
likesimple pattern matching, see here
regexpadvanced pattern matching, see here
+addition
-subtraction
*multiplication
/division
%modulus
&bit-wise AND
bit-wise OR
<<left shift
>>right shift

Supports shorthand syntax with any operator by prefixing it with $. Takes a JSON array of two or more expressions. If more than two expressions are provided, behavior depends on the operator type. Logic and math operators perform each binary operation in order of expressions. For example:

  • {"$and": [true, true, false]} = (true and true) and false = false
  • {"$/": [12, 3, 2, 2]} = ((12 / 3) / 2) / 2 = 1

Comparison operators perform a logical AND of the comparisons of the first expression to each additional expression.

  • {"$=": [0, 1, 2]} = (0 = 1) and (0 = 2) = false

Case

Case logic expression. If the base is provided, returns the then expression of the first case in which when = base. Otherwise returns the first case in which when is true. If no case is satisfied returns else if it is provided, or NULL otherwise.

PropertyValue
type"case"
baseexpression (optional)
casesarray of case options
elseexpression (optional)

Case Option

PropertyValue
whenexpression
thenexpression

Collate

Performs the MySQL COLLATE function.

PropertyValue
type"collate"
eexpression
collationstring

Count Rows

Performs the MySQL COUNT(*) function.

PropertyValue
type"count_rows"

Example

{ "type": "count_rows" }

Exists

Returns true if the enclosed SELECT returns at least one row.

PropertyValue
type"exists"
selectselect

Supports shorthand syntax with the $exists property.

PropertyValue
$existsselect

Function

Performs a MySQL function. The number of arguments varies depending on the function.

PropertyValue
type"function"
functionstring
argsarray of expressions

Available functions are:

NameArgsAggregateDescription
AVG1yesarithmetic average
AVG_DISTINCT1yesarithmetic average of distinct values of argument
BIT_AND1yesbit-wise AND
BIT_OR1yesbit-wise OR
BIT_XOR1yesbit-wise XOR
CEIL1yesreturns the smallest integer value not less than the argument
COUNT1yesreturns the number of rows in the which the argument is not NULL
COUNT_DISTINCTnyesreturns the number of distinct value(s) of the arguments
FLOOR1yesreturns the largest integer value not greater than the argument
MAX1yesreturns the maximum value of the argument
MIN1yesreturns the minimum value of the argument
POW2no
STDDEV_POP1yesreturns the population standard deviation of the argument
STDDEV_SAMP1yesreturns the sample standard deviation of the argument
SUM1yesreturns the sum of the argument
SUM_DISTINCT1yesreturns the sum of the distinct values of the argument
TRUNCATE2no
VAR_POP1yesreturns the population variance of the argument
VAR_SAMP1yesreturns the sample variance of the argument

Supports shorthand syntax by prefixing any function name with $$. For example, { "$$pow": [2, 3] } evaluates to 8.


In

Returns true if an expression is contained in a set of values. If an empty array is provided for values, will always return false.

PropertyValue
type"in"
eexpression
valuesarray of expressions

Supports shorthand syntax with the $in property. Takes an array of a single expression (e), followed by either an array of expression(s) (values) or a SELECT object.

PropertyValue
$inarray of one expression, then either an array of expressions or a select

In Select

Returns true if e is in the result of the select query.

PropertyValue
type"in_select"
eexpression
selectselect

Supports shorthand syntax with the $in property (see above).

Select Expression

Returns the value of the first column in the first row of the result set of the query.

PropertyValue
type"select"
selectselect

Supports shorthand syntax with the $select property.

PropertyValue
$selectselect

Unary Expression

Unary operator expression, evaluated as op e.

PropertyValue
type"unary"
opstring
eexpression

Valid unary operators are:

OperatorDescription
notlogical NOT
-negate
~bit invert

Supports shorthand syntax with any operator by prefixing it with $. Takes a single expression as a value.

PropertyValue
$ opexpression