Control statements allows to control the code flows in scripts based on certain conditions. For example, to perform an action only if a certain variable is defined, or to show an array of data through a loop.

1 IF statement

Use the 'If' control structure to specify the block of code to be executed if the condition is true.

Copy
<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']
Copy
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.

Copy
<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
Copy
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.

Copy
<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
Copy
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.

Copy
<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
Copy
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