1 <if> Statements
If statements are conditional statements.
<if>
<expr> !
<cond> !
</expr>
<then> !
<instructions> !
</then>
<else-if> *
<expr> !
<cond> !
</expr>
<then> !
<instructions> !
</then>
</else-if>
<else> ?
<instructions> !
</else>
</if>
Arguments | |||||
---|---|---|---|---|---|
Name | Type | Required | Unique | Nullable | Description |
Eexpr | |||||
Vcond | boolean | ||||
Ethen | |||||
Vinstructions | code | ||||
Eelse-if | |||||
Eexpr | |||||
Vcond | boolean | ||||
Ethen | |||||
Vinstructions | code | ||||
Eelse | |||||
Vinstructions | code |
Notes
The statement must consist of a condition ( <expr> ), a set of actions to be carried out if said condition is fulfilled ( <then> ), and optionally, another set of statements to be executed in the event that the condition is not met ( <else> ).Conditional statement with <else>.
[...] <if> <expr> <eq> <string.substring> <p_ctafrm/> <i/> <add><i/>1</add> </string.substring> <string>.</string> </eq> </expr> <then> <exception> <p_ctafrm/> contains the character '.' (point) in the position <i/> </exception> </then> <else> <exception> <p_ctafrm/> does not contain the character '.' (point) </exception> </else> </if> [...]
Notes
The condition ( <expr> ) consists of the content of the variable p_ctafrm ( <p_ctafrm/> ). We get a substring of this variable ( <string.substring> ) starting with the position of the variable i ( <i/> ) and ending with the position that results from adding 1 unit to i ( <add><i/>1</add> ). The resulting string consists of a single character, proving that it is equal ( <eq> ) to the character '.' (period) ( <string>.</string> ).2 <ifthen> Statements
ifthen is a type of conditional statement. It accepts three arguments: Condition: boolean expression that returns true or false. Instructions if true: instructions that must be executed if the condition evaluates to true. Instructions if false: instructions that must be executed if the condition evaluates to false.
<ifthen>
<cond> !
<instructions_if_true> !
<instructions_if_false> !
</ifthen>
Arguments | |||||
---|---|---|---|---|---|
Name | Type | Required | Unique | Nullable | Description |
Vcond | boolean | Boolean expression which determines whether the first argument is executed ( true) or the second ( false). | |||
Vinstructions_if_true | code | Instructions that must be executed if the condition evaluates to true. | |||
Vinstructions_if_false | code | Instructions that must be executed if the condition evaluates to false. |
Returns | |
---|---|
Type | Description |
node | Returns the argument that was executed: the first if the condition evaluated to true and the second if it did not. |
Screen printing with use of <ifthen>.
<xsql-script name='ifthen_sample1'> <body> <println> <ifthen> <expr><eq><math.sqrt>4</math.sqrt>2</eq></expr> <string>the square root of 4 is 2</string> <string>the square root of 4 is NOT 2</string> </ifthen> </println> </body> </xsql-script>
3 <ifnull> Statements
Conditional statement which returns the first argument if it is not null, and the second if it is.
<ifnull>
<value_1> !
<value_2> !
</ifnull>
Arguments | |||||
---|---|---|---|---|---|
Name | Type | Required | Unique | Nullable | Description |
Vvalue_1 | object | ||||
Vvalue_2 | object |
Returns | |
---|---|
Type | Description |
node | Returns the first argument if it is not null and the second if it is. |
Printing with the use of <ifnull>.
<xsql-script name='ifnull_sample1'> <body> <null name='reason' /> <println> <ifnull> <reason/> <string>(no reason)</string> </ifnull> </println> </body> </xsql-script>
Notes
One possible application of this function is printing on console, where it is preferable to visualize a descriptive text to a null.4 <if-exists> Statements
Allows users to determine if an object (table, function, procedure) exists in the database. If it exists, the instructions indicated in the argument will be executed then. An else argument can also be indicated with alternative instructions, in case the object does not exist in the database.
<if-exists
type='type'
name='name'
>
<then> !
<cond> !
</then>
<else> ?
<instructions> !
</else>
</if-exists>
Attributes | |||||
---|---|---|---|---|---|
Name | Type | Required | Default | Description | |
Atype | string | The type of database object. Indicates if the system will search in tables (table), functions (function), or procedures (procedure). | |||
Aname | string | Name of the database object. |
Arguments | |||||
---|---|---|---|---|---|
Name | Type | Required | Unique | Nullable | Description |
Ethen | |||||
Vcond | boolean | ||||
Eelse | |||||
Vinstructions | code |
Returns | |
---|---|
Type | Description |
node | Returns the first argument if the object exists in the database, and the second if it does not. |
Exceptions
illegal type [...]
Signifies that an incorrect value was specified in the type attribute (any value other than table, function, or procedure).
<xsql-script name='if-exists_sample1'> <body> <if-exists type='table' name='gcompedh'> <then> <update table='gconcuen'> <column name='ctacon'><p_newcta/></column> <where>ctacon = <p_oldcta/></where> </update> </then> </if-exists> </body> </xsql-script>
Notes
This example is used to check if a table exists in the database ( type='table' ) with the name gcompedh (1 name='gcompedh' ). If this table does exist, a record update statement is executed on the gconcuen table.<xsql-script name='if-exists_sample2'> <body> <if-exists type='table' name='gcompedh'> <then></then> <else> <exception>Error: there is no table.</exception> </else> </if-exists> </body> </xsql-script>
Notes
To control for the existence of a table before executing a certain code, the 'if-exist' tag must be used without code in the 'then' clause, throwing the error to the 'else' clause.[...] <if-exists type='procedure' name='iges_gen_tmovites'> <then> <execute-procedure name='iges_gen_tmovites'> <in> <p_empcode/> </in> </execute-procedure> </then> </if-exists> [...]
Notes
The system checks for the existence of a procedure ( type='procedure') with the name iges_gen_tmovites ( name='iges_gen_tmovites'). If it exists, the procedure iges_gen_tmovites will be executed.5 <switch> Statements
The instruction 'switch' is a multiple-decision instruction, where the program tests or searches the value contained in a variable against a list of integers or character constants.
When the equality value between variable and constant is found, the group of instructions associated with said constant is executed.
If the equality value between variable and constant is not found, a group of instructions associated with a default can (optionally) be indicated.
The default comparison used between variables and constants is equality, but users can also employ regular expressions, indicating them with the attributes
'regexp' 'with value' 'true'.
<switch
name='name'
regexp='regexp'
>
<value /> ?
<case value='value'> +
<instructions /> !
</case>
<default /> ?
</switch>
Attributes | |||||
---|---|---|---|---|---|
Name | Type | Required | Default | Description | |
Aname | string | Name of the variable. | |||
Aregexp | boolean | True to be able to use regular expressions. |
Arguments | |||||
---|---|---|---|---|---|
Name | Type | Required | Unique | Nullable | Description |
Evalue | null | Replaces the name attribute to assign the evaluation value. | |||
Ecase | null | Enables evaluation of the expression (which can be regular). If the expression is simple (not regular), equality is used to determine if this option is selected. | |||
Avalue | string | Constant value with which the value of the variable is compared. More than one value can be indicated separated by the | (pipe) symbol. | |||
Einstructions | code | Instructions that will be executed if the associated case is met. | |||
Edefault | null |
Returns | |
---|---|
Type | Description |
node | Returns the node of the selected option. |
Exceptions
unknown switch (case|default) element
This message means that an incorrect tag has been used within <switch>.
Notes
The system compares the content of the variable indicated in the name attribute with the value specified in the value attribute of each <case> element. When the content of the variable matches a value, the system executes the block of statements associated with said value. If the interpreter does not find a match, the set of actions contained in the <default> tag is executed (though use of this tag is optional).
Assigning the value 'true' and the optional attribute 'regexp' allows programmers to use regular expressions in the value attribute of each case element.
Conditional statement with <case> and <default>.
<xsql-script name='switch_sample1'> <body> <set name='a'>hello</set> <switch name='a'> <case value='1'> <println>a = 1</println> </case> <default> <println>a = not valid</println> </default> </switch> </body> </xsql-script>
The result shown would be the following:
a = not valid
Let's use a conditional statement with <case> and <default> as an example. Compare the name of a file with constants and a regular expression.
<xsql-script name='switch_sample2'> <body> <set name='file_name'>test.sql</set> <switch name='file_name' regexp='true'> <case value='test'> <println>case 1</println> </case> <case value='.*\.sql'> <println>case 2 (regexp)</println> </case> <default> <println>not found</println> </default> </switch> </body> </xsql-script>
The result shown would be the following:
case 2 (regexp)
Below is a conditional statement with three <case> and <default>.
[...] <switch name='call_type'> <case value='execute-procedure'> <return>xudp</return> </case> <case value='execute-function'> <return>xudf</return> </case> <case value='call'> <return>xsql</return> </case> <case value='function'> <return>local_xsql</return> </case> <default> <return><null /></return> </default> </switch> [...]
Depending on the content of the call_type variable, a specific value is returned. If it is a call to an XUDP (<case value='execute procedure'>), then 'xudp' is returned. If it is a call to an XUDF (<case value='execute-fucntion'>), then the value 'xudf' is returned. If it is a call to a local function of the script (<case value='call'>), then 'xsql' is returned.
This statement provides an elegant alternative to having to use nested <if>... <else> statements. The example code would be equivalent to the following:
[...] <if> <expr><eq><call_type/>execute-procedure</eq></expr> <then> <return>xudp</return> </then> <else> <if> <expr><eq><call_type/>execute-function</eq></expr> <then> <return>xudf</return> </then> <else> <if> <expr><eq><call_type/>call</eq></expr> <then> <return>xsql</return> </then> <else> <if> <expr><eq><call_type/>function</eq></expr> <then> <return>local_xsql</return> </then> <else> <return><null /><return> </else> </if> </else> </if> </else> </if> </else> </if> [...]
Note that the code with the <switch> statement occupies 17 lines, while its alternative with <if> statements occupies 33, the first being more human-readable than the second.
Below is a conditional statement with two uses of <case> and <default>, as well as use of regular expressions.
<xsql-script name='switch_sample3'> <body> <set name='a'>c</set> <switch name='a' regexp='true'> <case value='a|b|c'> <println>a = in a, b, c</println> </case> <case value='string.*'> <println>a = string</println> </case> <default> <println>a = not valid</println> </default> </switch> </body> </xsql-script>
The result shown on screen would be:
a = in a, b, c
Below is a statement using the value argument.
<xsql-script name='switch_sample5'> <body> <switch regexp='true'> <value> <date.month> <date.today /> </date.month> </value> <case value='1|2|3'> </case> <case value='4|5|6'> </case> <case value='7|8|9'> </case> <case value='10|11|12'> </case> </switch> </body> </xsql-script>
6 <decode> Statements
Decode statements are another type of conditional.
<decode>
<expr> !
<value> !
</expr>
<value> +
<casevalue> !
<instructions> !
</value>
<else> ?
<instructions> !
</else>
</decode>
Arguments | |||||
---|---|---|---|---|---|
Name | Type | Required | Unique | Nullable | Description |
Eexpr | |||||
Vvalue | object | ||||
Evalue | |||||
Vcasevalue | object | ||||
Vinstructions | code | ||||
Eelse | |||||
Vinstructions | code |
Returns | |
---|---|
Type | Description |
node | Returns the node of the selected option. |
Exceptions
Expected [...] but found [...].
An incorrect tag was specified as an argument.
Notes
This statement operates similarly to <switch> statements. The system compares the content of the variable specified in the expr tag with the value indicated in the first argument of each <value> tag. If the value of said argument matches the content of the variable, then the content of the variable is returned by the value indicated in the second argument (of the element <value>). If the interpreter finds no match, then the content of the variable is returned by the value reported in the <else> tag.
Below is a conditional statement with two <value> and <else> tags.
<xsql-script> <body> <set name='a'>1</set> <println> <decode> <expr><a/></expr> <value><number>1</number><string>ONE</string></value> <value><number>2</number><string>TWO</string></value> <else><string>NONE</string></else> </decode> </println> </body> </xsql-script>