1 IF statement
Use the 'If' control structure to specify the block of code to be executed if the condition is true.
<xsql-script> <body> <set name='m_code'>A</set> <if> <expr> <eq> <m_code/> <string>A</string> </eq> </expr> <then> <println>Throw exception type Abort ['<m_code/>']</println> </then> <else> <println>Continue</println> </else> </if> </body> </xsql-script>
Throw exception type Abort ['A']
var mStrCode = 'A'; if(mStrCode == 'A') { console.log("Throw exception type Abort ['"+mStrCode+"']."); } else { console.log("Continue"); }
Throw exception type Abort ['A']
2 IFTHEN statement
In the same way as 'If', the 'Ifthen' control structure is a type of condition that
is responsible for executing a code snippet if the defined instruction is met.
The difference is that XSQL expects 3 parameters: condition, true code
fragment and false code fragment, compared to server side script
, the same result is obtained
with a ternary operator.
<xsql-script name='ifthen_sample'> <body> <set name='m_code'>A</set> <ifthen> <expr><eq><m_code/>A</eq></expr> <set name='m_message'><string>m_code is A</string></set> <set name='m_message'><string>m_code is not A</string></set> </ifthen> <println><m_message/></println> </body> </xsql-script>
m_code is A
var mStrCode = 'A'; var mStrMessage = null; mStrMessage = mStrCode == 'A' ? 'mStrCode is A' : 'mStrCode is not A'; console.log(mStrMessage);
mStrCode is A
3 IFNULL statement
Use 'Ifnull' to evaluate whether the expected result of the condition is null.
The difference is that XSQL expects 2 parameters: the parameter to be evaluated and
the true code snippet, and to get the same result with server side script
just use the ternary operator comparing the variable with null, as shown
in the example.
<xsql-script name='ifnull_sample'> <body> <null name='m_code'/> <ifnull> <m_code/> <set name='m_message'><string>m_code is null</string></set> </ifnull> <println><m_message/></println> </body> </xsql-script>
m_code is null
var mStrCode = null; var mStrMessage = null; mStrMessage = mStrCode == null ? 'mStrCode is null' : 'mStrCode is not null'; console.log(mStrMessage);
mStrCode is null
4 SWITCH statement
The 'Switch' statement is like a series of IF statements, where the program is in charge of comparing variables with constants until a match is found and execute the defined code snippet.
<xsql-script> <body> <set name='m_code'>A</set> <switch name='m_code'> <case value='A'> <set name='m_message'> <string>m_code is A</string> </set> </case> <default> <set name='m_message'> <string>m_code is not A</string> </set> </default> </switch> <println><m_message/></println> </body> </xsql-script>
m_code is A
var mStrCode = 'A'; var mStrMessage = null; switch(mStrCode) { case 'A': mStrMessage = 'mStrCode is A'; break; default: mStrMessage = 'mStrCode is not A'; } console.log(mStrMessage);
mStrCode is A