XSQL
script is built according to statements and series.
A statement can be assigned, as well as a called function, a cycle, a conditional statement or even an empty statement.
1 Decisions
The main control structures are comprised of decisions which primarily include if, switch, and decode statements.
1.1 If
The <if> tag is one of the most important features of many languages, including XSQL
.
It allows the conditional execution of code fragments.
XSQL
has an 'if' structure similar to that of C:
<if> <expr> (EXPRESSION) </expr> <then> STATEMENT </then> <else> STATEMENT </else> </if>
As described in the section on expressions, the expression is evaluated to its boolean value.
If the expression evaluates to TRUE, XSQL
will execute the statement; if it is evaluated as FALSE, it will ignore it.
<xsql-script> <body> <set name='a'>20</set> <set name='b'>10</set> <if> <expr> <gt><a/><b/></gt> </expr> <then> <println><a/> is greater than <b/></println> </then> </if> </body> </xsql-script>
20 is greater than 10
1.2 Switch
The switch statement is similar to a series of IF statements in the same expression. In many cases, you may want to compare the same variable (or expression) with many different values, and execute a different part of the code depending on which value matches. This is exactly what switch statements are intended for.
Determine Variable Value
<xsql-script> <body> <set name='a'>20</set> <switch> <value> <a/> </value> <case value='10'><println>Es 10</println></case> <case value='20'><println>Es 20</println></case> <default> <println>It is not 10 or 20</println> </default> </switch> <!-- It could also be expressed as: --> <switch name='a'> <case value='10'><println>Es 10</println></case> <case value='20'><println>Es 20</println></case> <default> <println>It is not 10 or 20</println> </default> </switch> </body> </xsql-script>
Is 20
The switch statement can evaluate numbers, dates or strings, and can even evaluate regular expressions. The comparison of regular expressions requires the regexp attribute.
Determine the File Type from File Name
<xsql-script> <body> <set name='a'>test.sql</set> <switch regexp='true'> <value> <a/> </value> <case value='.*\.sql'><println>Es SQL</println></case> <case value='.*\.xml'><println>Es XML</println></case> <default> <println>It is not SQL or XML</println> </default> </switch> <!-- It could also be expressed as: --> <switch name='a' regexp='true'> <case value='.*\.sql'><println>Es SQL</println></case> <case value='.*\.xml'><println>Es XML</println></case> <default> <println>It is not SQL or XML</println> </default> </switch> </body> </xsql-script>
Es SQL
1.3 Decode
<Decode> statements evaluate an expression and return the equivalent value from the list of available values (value), or the default value found in the else clause.
Determine the Name of the Current Month
<xsql-script> <body> <set name='mes'><date.month><date.current /></date.month></set> <println> <decode> <expr><mes/></expr> <value><number>1</number><string>January</string></value> <value><number>2</number><string>February</string></value> <value><number>3</number><string>March</string></value> <value><number>4</number><string>April</string></value> <value><number>5</number><string>May</string></value> <value><number>6</number><string>June</string></value> <value><number>7</number><string>July</string></value> <value><number>8</number><string>August</string></value> <value><number>9</number><string>September</string></value> <value><number>10</number><string>October</string></value> <value><number>11</number><string>November</string></value> <value><number>12</number><string>December</string></value> <else><string>Not acceptable</string></else> </decode> </println> </body> </xsql-script>
Agosto
2 Loops
A loop or cycle, in programming, is a statement that is made repeatedly to an isolated piece of code, until the condition assigned to that loop is no longer met.
2.1 While
<while> loops are based on the execution of a set of <do></do> instructions or actions, as long as the <expr></expr> established condition is met.
Optionally and under certain conditions, during the execution of the while loop, you may wish to cause the iterative statement to exit (exit the loop) and resume the execution of the statement that follows the while loop. In this case, the <while.exit/> tag must be used.
Vowel Counter
<xsql-script> <body> <set name='word'>conch</set> <set name='i'>0</set> <set name='a'>0</set> <while> <expr> <lt> <i/> <string.length><word/></string.length> </lt> </expr> <do> <if> <expr> <eq> <string.substring> <word/> <i/> <add><i/>1</add> </string.substring> <string>a</string> </eq> </expr> <then> <set name='a'><add><a/>1</add></set> </then> </if> <set name='i'><add><i/>1</add></set> </do> </while> <println>The word <word/> contains <a/> the vowel 'o'</println> </body> </xsql-script>
the word conch contains the vowel 'o' 1 time
2.2 For
<for> loops use a numeric variable with unit increments or indicative increments, using the step attribute (if reported in the negative, equivalent decrements would apply). In this sense, they are different from classic loops based on expressions of languages such as C, Java or Javascript.
Optionally and under certain conditions, during the execution of the for loop, you may wish to cause the iterative statement to exit (exit the loop) and resume the execution of the statement that follows the while loop. In this case, the <for.exit/> tag must be used.
Multiplication Tables
<xsql-script> <body> <!-- header row --> <print> X </print> <for name='x' start='0' end='10'> <do> <print><string.format><string>%3d </string><x/></string.format></print> </do> </for> <println /> <!-- table --> <for name='x' start='0' end='10'> <do> <!-- left row --> <print><string.format><string>%3d </string><x/></string.format></print> <!-- table multiply row --> <for name='y' start='0' end='10'> <do> <print><string.format><string>%3d </string><mul><x/><y/></mul></string.format></print> </do> </for> <println /> </do> </for> </body> </xsql-script>
X 0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 2 3 4 5 6 7 8 9 10
2 0 2 4 6 8 10 12 14 16 18 20
3 0 3 6 9 12 15 18 21 24 27 30
4 0 4 8 12 16 20 24 28 32 36 40
5 0 5 10 15 20 25 30 35 40 45 50
6 0 6 12 18 24 30 36 42 48 54 60
7 0 7 14 21 28 35 42 49 56 63 70
8 0 8 16 24 32 40 48 56 64 72 80
9 0 9 18 27 36 45 54 63 72 81 90
10 0 10 20 30 40 50 60 70 80 90 100
2.3 Iterator
The iterator allows you to browse collections. Collections may include arrays of objects, lists or maps. The iterator deposits the value of each element in the collection in the variable indicated in the name attribute.
Two additional attributes permit definition:
- index - generation of a variable with the number of the element processed (0 ... n)
- type - only applicable to maps; indicates whether the variable indicated in name must take the value of the key or of the entry on the map. The default value is key.
Iterate Over Array
<xsql-script> <body> <set name='a'> <array> <number>10</number> <number>20</number> <number>30</number> </array> </set> <iterator name='data' index='idx'> <in> <a/> </in> <do> <println><idx/>=<data/></println> </do> </iterator> </body> </xsql-script>
0=10
1=20
2=30
Iterate Over Map (type=key)
<xsql-script> <body> <set name='a'> <map> <item><number>1</number>January</item> <item><number>2</number>February</item> <item><number>3</number>March</item> <item><number>4</number>April</item> <item><number>5</number>May</item> <item><number>6</number>June</item> </map> </set> <iterator name='data' index='idx'> <in> <a/> </in> <do> <println><idx/>=<data/></println> </do> </iterator> </body> </xsql-script>
0=1
1=2
2=3
3=4
4=5
5=6
Iterate Over Map (type=value)
<xsql-script> <body> <set name='a'> <map> <item><number>1</number>January</item> <item><number>2</number>February</item> <item><number>3</number>March</item> <item><number>4</number>April</item> <item><number>5</number>May</item> <item><number>6</number>June</item> </map> </set> <iterator name='data' index='idx' type='value'> <in> <a/> </in> <do> <println><idx/>=<data/></println> </do> </iterator> </body> </xsql-script>
0=January
1=February
2=March
3=April
4=May
5=June
2.4 Foreach
The <foreach> tag is especially important when processing data cursors, and in particular, processing SQL statements. We could say that this tag processes ResultSet type objects, whether they originate from a database or from memory structures.
Optionally and under certain conditions, during the execution of the foreach tag, you may wish to cause the iterative statement to exit (exit the loop) and resume the execution of the statement that follows the while loop. In this case, the <foreach.exit/> tag must be used.
In the same way (optionally), users may also wish to omit the execution of all (or part) of the foreach statements and move to the next cursor record. In these circumstances, the <foreach.continue/> tag must be utilized.
There are different use cases of foreach. For our purposes, a simple data table will be used.
CREATE TABLE IF NOT EXISTS continents ( code CHAR(2) NOT NULL, name VARCHAR(255), PRIMARY KEY (code) ) ; INSERT INTO continents VALUES('AF', 'Africa'); INSERT INTO continents VALUES('AS', 'Asia'); INSERT INTO continents VALUES('EU', 'Europe'); INSERT INTO continents VALUES('NA', 'North America'); INSERT INTO continents VALUES('SA', 'South America'); INSERT INTO continents VALUES('OC', 'Oceania'); INSERT INTO continents VALUES('AN', 'Antarctica');
2.4.1 foreach on a data cursor
You can iterate over the result of an SQL query by wrapping the statement (SQL expression) in the foreach tag. Note that an SQL <select> statement is used with a prefix which will encompass the resulting data in variables with this prefix. For each record obtained, the variables take on the corresponding values.
The content of 'continents' is listed using the variables m_code and n_name
<xsql-script> <body> <foreach> <select prefix='m_'> <columns>*</columns> <from table='continents'/> </select> <do> <println><m_code /> - <m_name /></println> </do> </foreach> </body> </xsql-script>
AF - Africa
AS - Asia
EU - Europe
NA - North America
SA - South America
OC - Oceania
AN - Antarctica
The SQL statement may include anything that returns an SQL cursor, whether a select tag, UNION operator, Native SQL, or a call to a stored procedure that returns a cursor.
2.4.2 foreach on Memory Tables (vtable)
As we have seen, database data cursors are processed by <foreach>. In addition, it can operate in the same way on certain in-memory data structures called vtable. For this operation, the implicit conversion mechanism in is all that is needed.
Here is an example:
foreach on In-Memory Data
<xsql-script> <body> <vtable name='planets'> <column name='planet' type='char' /> <column name='diameter_e' type='decimal' /> <column name='diameter_km' type='decimal' /> <column name='mass_E' type='decimal' /> </vtable> <vtable.insert name='planets'> <matrix> { "Mercury", "0.382", "4878", "0.055" } { "Venus", "0.949", "12104", "0.815" } { "Earth", "1", "12756", "1" } { "Mars" "0.532", "6787", "0.107" } { "Jupiter", "11.209", "142800", "318" } { "Saturn", "9.44", "120000", "95" } { "Uranus", "4.007", "51118", "15" } { "Neptune", "3.883", "49528", "17" } </matrix> </vtable.insert> <foreach> <in prefix='m_'> <planets /> </in> <do> <println>Mass of <m_planet/> is <m_mass_e />Earths</println> </do> </foreach> </body> </xsql-script>
Mass of Mercury is 0.055 Earths
Mass of Venus is 0.815 Earths
Mass of Earth is 1 Earths
Mass of Mars is 0.107 Earths
Mass of Jupiter is 318 Earths
Mass of Saturn is 95 Earths
Mass of Uranus is 15 Earths
Mass of Neptune is 17 Earths
2.4.3 foreach on SOAP Responses (ResultSet)
Users can iterate over the result of an SOAP query, either by wrapping the SOAP call in the foreach tag, or by prefetching the SOAP call. Just as in the case of foreach on vtable memory structures, to loop through cursors, the implicit conversion mechanism in is required.
SOAP Query Response with foreach
<xsql-script name='test_foreach_soapsqlresponse'> <body> <set name='m_soap_sql_response'> <soap.call url='http://localhost:8080/soap/servlet/rpcrouter' uri='urn:SOAPSQLServer' method='executeSQL' user='demo' password='abcdemo' > <parameters> <parameter name='database'>demo_sports</parameter> <parameter name='sqlstmt'>SELECT * FROM cdiarios</parameter> </parameters> </soap.call> </set> <foreach> <in> <m_soap_sql_response /> </in> <do> <println><codigo /> - <nomdia/></println> </do> </foreach> </body> </xsql-script>
0 - GENERAL
1 - CASH
2 - BANKS
3 - PURCHASES
4 - SALES
5 - ACCOUNTING
2.4.4 Groups
The <foreach> tag allows the automatic processing of breakout groups based on the variables of each record processed. The following control blocks can be used:
- before - Executed before foreach
- after - Executed after foreach
- before-group - Executed before the change of the indicated variable
- after-group - Executed after the change of the indicated variable
Breakout Groups before Variations of the Continent Code
<xsql-script> <body> <foreach> <select prefix='m_'> <columns>*</columns> <from table='continents'/> </select> <before> <println>Before</println> </before> <after> <println>After</println> </after> <before-group of='m_code'> <println> Before <m_code/></println> </before-group> <after-group of='m_code'> <println> After <m_code/></println> </after-group> <do> <println> <m_code /> - <m_name /></println> </do> </foreach> </body> </xsql-script>
Before
Before AF
AF - Africa
After AF
Before AS
AS - Asia
After AS
Before EU
EU - Europe
After EU
Before NA
NA - North America
After NA
Before SA
SA - South America
After SA
Before OC
OC - Oceania
After OC
Before AN
AN - Antarctica
After AN
After