Query a database from server- side script is easy using Axional library. When performing a query, database returns an SQL ResultSet.

A SQL result set is a set of rows from a database, as well as metadata about the query such as the column names, and the types and sizes of each column. Depending on the database system, the number of rows in the result set may or may not be known. Usually, this number is not known up front because the result set is built on-the-fly.

The database object provides basic methods to perform query execution.

Return Method Description
JSResultSet executeQuery(String sql, Object... args) Executes a query.
Object executeGet(String sql, Object... args) Executes a single row and single column query. Better use executeGetXXX for the appropiate number type to avoid script engine side effects.
JSResultSet executeScriptOrQuery(String sql, Object... args) Executes a script or query.
JSResultSet executeCachedQuery(String,int,Object[]) Executes a query.

1 Query a database

1.1 One field

To interact with <select> element in server- side script to get one field, you use the method Ax.db.executeGet. The method executeGet is simplified query for single row / column values.

Copy
<xsql-script>
    <body>
        <select prefix='m_'>
            <columns>
                tabid 
            </columns>
            <from table='systables'/>
            <where>
                tabid = 1
            </where>
        </select>
        
        <println>Tabid: [<m_tabid />]</println>    
    </body>
</xsql-script>
Tabid: [1]
Copy
var tabid = Ax.db.executeGet(`
        <select>
            <columns>
                tabid 
            </columns>
            <from table='systables'/>
            <where>
                tabid = 1
            </where>
        </select>`);
         
    console.log(`Tabid: [${tabid}]`);
Tabid: [1]

The following example shows another way to obtain the same result by preparing the 'select' instruction, using the ternary operator '?'.

Copy
var mIntTabid = 1;

var tabid = Ax.db.executeGet(`
    <select>
        <columns>
            tabid 
        </columns>
        <from table='systables'/>
        <where>
            tabid = ?
        </where>
    </select>`, mIntTabid);
     
console.log(`Tabid: [${tabid}]`);
Tabid: [1]

1.2 One row

Using method Ax.db.executeQuery and toOne function to convert directly to a JSON object

Copy
<xsql-script>
    <body>
        <select prefix='m_'>
            <columns>
                tabid, tabname 
            </columns>
            <from table='systables'/>
            <where>
                tabid = 1
            </where>
        </select>
        
        <println>Tabid: [<m_tabid />], Tabname: [<m_tabname />]</println>    
    </body>
</xsql-script>
Tabid: [1], Tabname: [systables]
Copy
var rsSystables = Ax.db.executeQuery(`
        <select>
            <columns>
                tabid, tabname 
            </columns>
            <from table='systables'/>
            <where>
                tabid = 1
            </where>
        </select>`).toOne();
         
    console.log(`Tabid: [${rsSystables.tabid}], Tabname: [${rsSystables.tabname}]`);
Tabid: [1], Tabname: [systables]

The following example shows another way to obtain the same result by preparing the 'select' instruction, using the ternary operator '?'.

Copy
var mIntTabid = 1;

var rsSystables = Ax.db.executeQuery(`
    <select>
        <columns>
            tabid, tabname
        </columns>
        <from table='systables'/>
        <where>
            tabid = ?
        </where>
    </select>`, mIntTabid).toOne();
     
console.log(`Tabid: [${rsSystables.tabid}], Tabname: [${rsSystables.tabname}]`);
Tabid: [1], Tabname: [systables]

1.3 More than one row

The executeQuery statements executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query. The ResulSet object is iterable. So we can use a for loop to traverse it. On each iteration, the returned object is a JSON object with row data.

Copy
<xsql-script>
    <body>
        <foreach>
            <select prefix='m_'>
                <columns>
                    tabid, tabname 
                </columns>
                <from table='systables'/>
                <where>
                    3 > tabid
                </where>
            </select>
            <do>
                <println>Tabid: [<m_tabid />], Tabname: [<m_tabname />]</println>    
            </do>
        </foreach>
    </body>
</xsql-script>
Tabid: [1], Tabname: [systables]
Tabid: [2], Tabname: [syscolumns]
Copy
var rsSystables = Ax.db.executeQuery(`
        <select>
            <columns>
                tabid, tabname 
            </columns>
            <from table='systables'/>
            <where>
                3 > tabid
            </where>
        </select>`);
    
    for (var rowSystables of rsSystables) {
        console.log(`Tabid: [${rowSystables.tabid}], Tabname: [${rowSystables.tabname}]`);
    }
Tabid: [1], Tabname: [systables]
Tabid: [2], Tabname: [syscolumns]

The following example shows another way to obtain the same result by preparing the 'select' instruction, using Template literals allowing embedded expressions. Template literals are enclosed by the back-tick (` `) (grave accent) character instead of double or single quotes. Template literals can contain placeholders. These are indicated by the dollar sign and curly braces (${expression}). The expressions in the placeholders and the text between them get passed to a function.

Copy
var mSqlCond = "tabid BETWEEN 1 AND 2";

var rsSystables = Ax.db.executeQuery(`
    <select>
        <columns>
            tabid, tabname 
        </columns>
        <from table='systables'/>
        <where>
            ${mSqlCond}
        </where>
    </select>`);

for (var rowSystables of rsSystables) {
    console.log(`Tabid: [${rowSystables.tabid}], Tabname: [${rowSystables.tabname}]`);
}
Tabid: [1], Tabname: [systables]
Tabid: [2], Tabname: [syscolumns]

1.4 Other ways to make a query

The executeScriptOrQuery statements executes the XSQL code in this PreparedStatement object and returns one ResultSet object.

Copy
var rsSystables = Ax.db.executeScriptOrQuery(`
    <select>
        <columns>
            tabid, tabname 
        </columns>
        <from table='systables'/>
        <where>
            3 > tabid
        </where>
    </select>`);

for (var rowSystables of rsSystables) {
    console.log(`Tabid: [${rowSystables.tabid}], Tabname: [${rowSystables.tabname}]`);
}
Tabid: [1], Tabname: [systables]
Tabid: [2], Tabname: [syscolumns]

For database querys it should be more reasonable use the JDBC cache via executeCachedQuery.

Copy
var rsSystables = Ax.db.executeCachedQuery(`
    <select>
        <columns>
            tabid, tabname 
        </columns>
        <from table='systables'/>
        <where>
            3 > tabid
        </where>
    </select>`);

for (var rowSystables of rsSystables) {
    console.log(`Tabid: [${rowSystables.tabid}], Tabname: [${rowSystables.tabname}]`);
}
Tabid: [1], Tabname: [systables]
Tabid: [2], Tabname: [syscolumns]

For database querys it should be more reasonable use the JDBC cache via executeCachedQuery.

Copy
var rsSystables = Ax.db.executeCachedQuery(`
    <select>
        <columns>
            tabid, tabname 
        </columns>
        <from table='systables'/>
        <where>
            3 > tabid
        </where>
    </select>`);

for (var rowSystables of rsSystables) {
    console.log(`Tabid: [${rowSystables.tabid}], Tabname: [${rowSystables.tabname}]`);
}
Tabid: [1], Tabname: [systables]
Tabid: [2], Tabname: [syscolumns]

2 Cursor

2.1 Group, before, after

A cursor is a special type of ResultSet processor. It wraps the ResultSet iterable and allows to setup cursor function handlers. Use of forEach method is mandatory. If forEach() is not called, the cursor is not iterated and none of the grouping configuration methods defined will work. Variables defined in one group can be seen in other group definition. A simple cursor may contain a before all and after all function. Finally you iterate the cursor by using forEach. A common use of cursors is to perform grouping. The next example groups by column tabid.

Copy
<xsql-script>
    <body>
        <foreach>
            <select prefix='m_'>
                <columns>
                    tabid, tabname 
                </columns>
                <from table='systables'/>
                <where>
                    3 > tabid
                </where>
            </select>
            <before-group of='m_tabid'>
                <println>Before group: [<m_tabid />]</println>
            </before-group>
            <after-group of='m_tabid'>
                <println>After group: [<m_tabid />]</println>
            </after-group>
            <do>
                <println>Tabid: [<m_tabid />], Tabname: [<m_tabname />]</println>    
            </do>
        </foreach>    
    </body>
</xsql-script>
Before group: [1]
    Tabid: [1], Tabname: [systables]
    After group: [1]
    Before group: [2]
    Tabid: [2], Tabname: [syscolumns]
    After group: [2]
Copy
var rsSystables = Ax.db.executeQuery(`
        <select>
            <columns>
                tabid, tabname 
            </columns>
            <from table='systables'/>
            <where>
                3 > tabid
            </where>
        </select>`);                                               
        
     rsSystables.cursor()
        .group("tabid")
            .before(rowSystables => {
                console.log(`Before group: ${rowSystables.tabid}`);
            })
            .after(rowSystables => {
                console.log(`After group: ${rowSystables.tabid}`);
        })
        .forEach(rowSystables => {
            console.log(`Tabid: [${rowSystables.tabid}], Tabname: [${rowSystables.tabname}]`);
        });
Before group: 1
    Tabid: [1], Tabname: [systables]
    After group: 1
    Before group: 2
    Tabid: [2], Tabname: [syscolumns]
    After group: 2