swcl.lib.query

Class Query

Library for easily accessing named queries of JSMA query and accessing the data more easily.

Performance

If you are working with large resultsets you might prefer to access them directly to prevent extra costs for convenience access. BUT usually the overhead for executing a query should be so large compared to the script execution time that the gained convenience easily justifies the little loss of performance for the additional api layer.

Example

The following snippet shows you how to load the lib, execute a query and create some JSON string from iterating the rows and accessing the data with columnName based random acess.

WebMacro

include("swcl.lib.query");

//construct a Query with a name of the named query - nothing is executed by now
var query = new Query(args[0]);

//query is executed with the given parameter list and the internal result is prepared
query.execute(args[1]);

var result = [];

//get the rowCount - getRowCount
var rowCount = query.getRowCount();

//get the columnCount - getColumnCount
var columnCount = query.getColumnCount();

//random access with rowIndex and columnName - getValue
for (var i = 0; i < rowCount; i++) {
  result.push({
    type: "getValue",
    productName: query.getValue(i, "name"),
    formatInfo: query.getValue(i, "format1") + " " + query.getValue(i, "format2"),
    index: i
  });
}

//row access with rowObject - getRow
for (var i = 0; i < rowCount; i++) {
  var row = query.getRow(i);
  result.push({
    type: "getRow",
    productName: row.name,
    formatInfo: row.format1 + " " + row.format2,
    index: row.rowIndex
  });
}

return JSON.stringify(result);

Named Query

SELECT id, name, format1, format2 FROM products where name like text(?)


The wrapper class for convenience access to JSMA query

Methods

The following functions are methods of the class Query:


Query, execute, getColumnCount, getColumnIndex, getRow, getRowCount, getValue, isExecuted


Constructor Query(string name)

Constructs a new query wrapper with a given named query

Parameter string name

The name of the named query

Returns Query

A new instance of Query

Query Query.execute(object... queryParams) throws Error

Executes the query with a variable parameter list given to the named query

Query Parameters

It will be called with variable parameter list for the parameters of the query.

Example

query.execute("Documentation", "JSMA%");

Parameter object... queryParams

Optional parameters for the query

Returns Query

returns this so you can chain the call

Throws Error

  • If the query has already been executed
  • If the query could not be found in the named queries

int Query.getColumnCount() throws Error

Returns the count of columns as integer

Returns int

The column count

Throws Error

  • If the query has not been executed

int Query.getColumnIndex(string columnName) throws Error

Returns the index of the column with the given columnName

Parameter string columnName

The name of the column

Returns int

The index of the column (zero-based)

Throws Error

  • If the query has not been executed
  • If the column with the columnName is not contained in the result

object Query.getRow(number rowIndex) throws Error

Returns a row object for the given rowIndex. The columns of the row can then be accessed a simple properties of the rowObject.

Access query wrapper and rowIndex

The property .rowIndex is set to the rowIndex of the rowObject.

The property .queryObject is set to the query wrapper creating the rowObject.

Example

for (var i = 0; i < rowCount; i++) {
  var row = query.getRow(i);
  result.push({
    type: "getRow",
    name: row.name,
    format: row.format1 + " " + row.format2,
    index: row.rowIndex
  });
}

Parameter number rowIndex

The index of the row to create a row object from

Returns object

The row object reflecting row rowIndex of the result

Throws Error

  • If the query has not been executed
  • If the rowIndex is not in range

int Query.getRowCount() throws Error

Returns the count of rows as integer

Returns int

The row count

Throws Error

  • If the query has not been executed

object Query.getValue(int rowIndex, string columnName) throws Error

Returns the value of the column with name columnName and the row rowIndex

Parameter int rowIndex

The index of the row to read from

Parameter string columnName

The name of the column

Returns object

The value at rowIndex:getColumnIndex(columnName) in the result

Throws Error

  • If the query has not been executed
  • If the column with this name is not contained in the result
  • If the rowIndex is not in range

boolean Query.isExecuted()

Check if the query has been executed

Returns boolean

  • true If the query has been executed
  • false otherwise