pjs.parallelQueries()

pjs.parallelQueries()

This API is available in Profound.js 4.15.0 and later. When using an IBM i database, it requires the Profound.js Connector module.

Description

pjs.parallelQueries() is a shortcut for running several pjs.query() calls at the same time. All of the SQL statements are started together rather than one after another, which can significantly reduce the total time when a program needs the results of several independent queries.

The results are returned as a JavaScript array with one element per statement, in the same order as the statements were passed.

When a Profound.js program is called from an IBM i job through PJSCALL or a proxy program, it is not possible to run multiple queries in parallel. In this scenario the queries are run in sequence, and the results are returned in the same form.

Syntax

var results = pjs.parallelQueries([sql1, sql2, ...]) // default database connection var results = pjs.parallelQueries([sql1, [sql2, param1, param2], ...]) // statements with bound parameters var results = pjs.parallelQueries(pjs.getDB("crm"), [sql1, sql2, ...]) // named database connection

Parameters

  1. Database connection (Object, optional) - A database connection definition object returned by pjs.getDB() or pjs.getRemoteDB(). If omitted, the default database connection is used. All of the statements run against the same connection.

  2. Queries (Array) - An array of SQL statements to run. Each element can be either:

    • A String - An SQL statement with no parameter markers.

    • An Array - In the form [sqlStatement, param1, param2, ...]: an SQL statement followed by one value for each parameter marker (?) in the statement.

     

Return Value

An array with one element for each statement that was passed, in the same order:

  • Successful statement - The value that pjs.query() would return for that statement; for a SELECT, an array of row objects.

  • Failed statement - The error object describing the failure. The other statements are still run, so one failure does not prevent the remaining results from being returned.

Exception Handling

Errors from an individual statement do not stop the API; they are returned in the result array in the position of the failed statement. Check each result before using it, for example with Array.isArray() or by testing for an Error object.

Examples

Example 1: Run Three Queries at Once in a Web Service

This example runs a count, a full table read, and a filtered read at the same time and returns all three results to the caller. The third statement uses parameter markers, so it is passed as an array whose first element is the statement and whose remaining elements are the bound values.

Prerequisite: a table PRODUCTSP in the library list with the columns PRID and PRNAME. Substitute your own table.

Run several statements in parallel
function getData(request, response) { var id = request.query.id; var name = request.query.name; var sql1 = "SELECT COUNT(*) AS total FROM productsp"; // plain SQL string var sql2 = "SELECT * FROM productsp ORDER BY prid"; // plain SQL string var sql3 = ["SELECT * FROM productsp WHERE prid = ? OR prname = ?", id, name]; // statement with bound values // All three statements are started at the same time. var qryResults = pjs.parallelQueries([sql1, sql2, sql3]); // qryResults has 3 elements, in the same order as the statements: // qryResults[0] - the result of sql1 (an array with one row containing "total") // qryResults[1] - the result of sql2 (an array of all rows) // qryResults[2] - the result of sql3 (an array of the matching rows) response.send(qryResults); } exports.default = getData;

Example 2: Check Each Result for an Error

Because a failed statement returns an error object in its position instead of stopping the API, each result should be checked before it is used.

Handle a failed statement in the result array
var results = pjs.parallelQueries([ "SELECT COUNT(*) AS total FROM productsp", "SELECT COUNT(*) AS total FROM nosuchtable" // this statement will fail ]); results.forEach(function (result, index) { if (Array.isArray(result)) { console.log("Statement " + index + " returned " + result[0].total); } else { // A failed statement returns its error object. console.log("Statement " + index + " failed: " + result.message); } });

Related Pages