1 Obtain a serial

Obtains the last serial number to be used in a table. For example, after inserting a row in a table with a SERIAL column, sqlca.serial (XSQL) or getSerial (server- side script) contains the new generated serial number.

Copy
<xsql-script>
    <body> 
        <nativesql>
            DROP TABLE IF EXISTS table_cities;
            CREATE TABLE table_cities (
                code                SERIAL       not null,
                city_name           VARCHAR(20)  not null,
                country_name        VARCHAR(20)  not null,
                official_language   VARCHAR(20)  not null
            );
        </nativesql> 

        <insert table='table_cities'>
            <column name='code'>0</column>       
            <column name='city_name'>Rome</column>
            <column name='country_name'>Italy</column>
            <column name='official_language'>Italian</column>
        </insert>

        <set name='m_serial'><sqlca.serial /></set>

        <println>
            The new serial is: <m_serial />
        </println>
        
        <nativesql>
            DROP TABLE IF EXISTS table_cities;
        </nativesql> 
    </body>
</xsql-script>
The new serial is : 1
Copy
Ax.db.execute(`
        DROP TABLE IF EXISTS table_cities;
        CREATE TABLE table_cities (
                code                SERIAL     not null,
                city_name           VARCHAR(20)  not null,
                country_name        VARCHAR(20)  not null,
                official_language   VARCHAR(20)  not null
        );   
    `); 
    
 var mObjCity = {
        code: 0,
        city_name: "Rome",
        country_name: "Italy",
        official_language: "Italian",
    };

var mIntSerial = Ax.db.insert("table_cities", mObjCity).getSerial();

console.log("The new serial is: " + mIntSerial);

Ax.db.execute(`DROP TABLE IF EXISTS table_cities;`);
The new serial is : 1

2 Obtain number of registers

Return the number of processed registers in a sentence of data manipulation like: insert, update or delete.

Remenber that in server- side script the function only applies for delete operation, however in XSQL has a range to insert, update or delete.

Copy
<xsql-script name='sqlcaCount_sample1'>
    <body> 
        <nativesql>
            DROP TABLE IF EXISTS table_cities;
            CREATE TABLE table_cities (
                code                SERIAL       not null,
                city_name           VARCHAR(20)  not null,
                country_name        VARCHAR(20)  not null,
                official_language   VARCHAR(20)  not null
            );

            INSERT INTO table_cities VALUES (0,  'Lima',      'Perú',     'Spanish' );
            INSERT INTO table_cities VALUES (0,  'Montreal',  'Canada',   'French'  );
        </nativesql> 

        <insert table='table_cities'>
            <column name='code'>0</column>       
            <column name='city_name'>Rome</column>
            <column name='country_name'>Italy</column>
            <column name='official_language'>Italian</column>
        </insert>

         <update table='table_cities'>
            <column name='city_name'>Miami</column>
            <column name='country_name'>USA</column>
            <column name='official_language'>English</column>
            <where>
                code = 1
            </where>
        </update>

        <set name='m_count'><sqlca.count /></set>

        <println>
            How many process are : <m_count />
        </println>

       <nativesql>
            DROP TABLE IF EXISTS table_cities;
        </nativesql> 
    </body>
</xsql-script>
How many process are : 1
Copy
Ax.db.execute(`
        DROP TABLE IF EXISTS table_cities;
        CREATE TABLE table_cities(
            code                SERIAL       not null,
            city_name           VARCHAR(20)  not null,
            country_name        VARCHAR(20)  not null,
            official_language   VARCHAR(20)  not null
    )`
);

Ax.db.execute(` INSERT INTO table_cities VALUES (0,  'Lima',      'Perú',     'Spanish' )`);      
Ax.db.execute(` INSERT INTO table_cities VALUES (0,  'Montreal',  'Canada',   'French'  )`);   
Ax.db.execute(` INSERT INTO table_cities VALUES (0,  'Rome',      'Italy',    'Italian' )`);

var mIntCount = Ax.db.delete("table_cities","code = 2").getCount();

console.log("Number of row deleted : " + mIntCount);

Ax.db.execute(`DROP TABLE IF EXISTS table_cities;`);
Number of row deleted : 1

3 Obtain a row

Return the number of row which is being processed to a foreach.

Operator XSQL Equivalent server-side script
<sqlca.row /> getRow()
Copy
<xsql-script name='sql_sample'>
    <body>
        <nativesql>
            DROP TABLE IF EXISTS table_cities;
            CREATE TABLE table_cities (
                code                SERIAL       not null,
                city_name           VARCHAR(20)  not null,
                country_name        VARCHAR(20)  not null,
                official_language   VARCHAR(20)  not null
            );

            INSERT INTO table_cities VALUES (0,  'Lima',      'Perú',     'Spanish' );
            INSERT INTO table_cities VALUES (0,  'Montreal',  'Canada',   'French'  );
            INSERT INTO table_cities VALUES (0,  'Rome',      'Italy',    'Italian'  );
        </nativesql>
        <foreach>
            <select prefix='m_'>
                <columns>city_name</columns>
                <from table='table_cities' />
            </select>
            <do>
                <println>Number of row which is being processed : <sqlca.row />  (table_cities.city_name = <m_city_name />)</println>
            </do>
        </foreach>
        <nativesql>
            DROP TABLE IF EXISTS table_cities;
        </nativesql> 
    </body>
</xsql-script>
Number of row which is being processed : 0  (table_cities.city_name = Lima)
Number of row which is being processed : 1  (table_cities.city_name = Montreal)
Number of row which is being processed : 2  (table_cities.city_name = Rome)
Copy
Ax.db.execute(`
        DROP TABLE IF EXISTS table_cities;
        CREATE TABLE table_cities (
                code                SERIAL     not null,
                city_name           VARCHAR(20)  not null,
                country_name        VARCHAR(20)  not null,
                official_language   VARCHAR(20)  not null
        );   
    `); 

Ax.db.execute(`INSERT INTO table_cities  VALUES ('0', 'Lima',           'Perú',     'Spanish'   )`);   
Ax.db.execute(`INSERT INTO table_cities  VALUES ('0', 'Montreal',       'Canadá',   'French'    )`);   
Ax.db.execute(`INSERT INTO table_cities  VALUES ('0', 'Rome',           'Italy',    'Italian'   )`);   
    
var mRsSystable = Ax.db.executeQuery(`
    <select>
        <columns>
            city_name
        </columns>
        <from table='table_cities' />
    </select>
`);

mRsSystable.forEach(function(mRow){
    console.log("Number of row which is being processed : " + mRsSystable.getRow() + " (table_cities.city_name: " + mRow.city_name + ")");
});

Ax.db.execute(`DROP TABLE IF EXISTS table_cities;`);
Number of row which is being processed : 1 (table_cities.city_name: Lima)
Number of row which is being processed : 2 (table_cities.city_name: Montreal)
Number of row which is being processed : 3 (table_cities.city_name: Rome)

In XSQL it starts with 0,1,2. While in server-side script it starts with 1,2,3.

4 Obtain names of columns

The names of all the columns of the table will be returned as a parameter.

Operator XSQL Equivalent server-side script Description
<sqlca.allcolumns/> getColumnNames() Returns the names of all the columns of the table passed as parameter in a DB2 database engine.

In the case of XSQL is possible that it returns the character *.

Copy
<xsql-script name='sql_sample'>
    <body>
        <nativesql>
            DROP TABLE IF EXISTS table_cities;
            CREATE TABLE table_cities (
                code                SERIAL       not null,
                city_name           VARCHAR(20)  not null,
                country_name        VARCHAR(20)  not null,
                official_language   VARCHAR(20)  not null
            );

        </nativesql>
        <println><sqlca.allcolumns table='table_cities'/></println>
        <nativesql>
            DROP TABLE IF EXISTS table_cities;
        </nativesql> 
    </body>
</xsql-script>
*
Copy
Ax.db.execute(`
        DROP TABLE IF EXISTS table_cities;
        CREATE TABLE table_cities (
                code                SERIAL     not null,
                city_name           VARCHAR(20)  not null,
                country_name        VARCHAR(20)  not null,
                official_language   VARCHAR(20)  not null
        );   
    `);  

var mRsSystable = Ax.db.executeQuery("SELECT * FROM table_cities");
console.log(mRsSystable.getMetaData().getColumnNames());
mRsSystable.close();

Ax.db.execute(`DROP TABLE IF EXISTS table_cities;`);
[code, city_name, country_name, official_language]