Case statements
Case statements can be used for branching execution, like a switch statement.
Syntax:
CASE
v
WHENt[1]
THENr[1]
WHENt[2]
THENr[2]
... WHENt[n]
THENr[n]
ELSEr[e]
END
The expression
is evaluated and matched for
equality against the v
value of each
t[i]
WHEN
clause. If a match is found, the corresponding
expression becomes the result of
the r[i]
CASE
statement. The WHEN
clauses are evaluated in order so
that if there's more than one matching clause, the result of the first matching clause
becomes the result of the CASE
statement. If there are no matches,
of the r[e]
ELSE
clause is
the result. If there's no match and no ELSE
clause, the result is
Undefined
.
CASE
statements require at least one WHEN
clause. An
ELSE
clause is optional.
For example:
Incoming payload published on topic topic/subtopic
:
{ "color":"yellow" }
SQL statement:
SELECT CASE color WHEN 'green' THEN 'go' WHEN 'yellow' THEN 'caution' WHEN 'red' THEN 'stop' ELSE 'you are not at a stop light' END as instructions FROM 'topic/subtopic'
The resulting output payload would be:
{ "instructions":"caution" }
Note
If
is v
Undefined
, the result
of the case statement is Undefined
.