pjs.setConnectAttr()

pjs.setConnectAttr()

This API applies to IBM i Db2 database connections and requires the Profound.js Connector module. With other database drivers, the call is accepted but has no effect.

Description

pjs.setConnectAttr() sets an attribute of an SQL connection, such as the naming convention, the commitment control (transaction isolation) level, or the date format used for the connection. The attributes correspond to the connection attributes of the IBM i SQL CLI.

Many of the commonly used connection attributes can also be set with pjs.setSQLOption(), which uses the same option names and values as the embedded SQL SET OPTION statement and is scoped to the current program or module.

Syntax

pjs.setConnectAttr(attribute, value) // default database connection pjs.setConnectAttr(pjs.getDB("name"), attribute, value) // 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.

  2. Attribute - The attribute to change (see Attributes and Valid Values below).

  3. Value - The new value for the attribute. Integer values and string values are both accepted, depending on the attribute.

Attributes and Valid Values

Any attribute supported by the IBM i SQL CLI function SQLSetConnectAttr can be used. The CLI constants are available as global constants in Profound.js modules. Commonly used attributes include:

  • SQL_ATTR_DBC_SYS_NAMING - SQL_TRUE for the system naming convention (library/table) or SQL_FALSE for the SQL naming convention (schema.table).

  • SQL_ATTR_COMMIT (SQL_TXN_ISOLATION) - The commitment control level: SQL_TXN_NO_COMMIT, SQL_TXN_READ_UNCOMMITTED, SQL_TXN_READ_COMMITTED, SQL_TXN_REPEATABLE_READ, or SQL_TXN_SERIALIZABLE.

  • SQL_ATTR_DATE_FMT, SQL_ATTR_DATE_SEP, SQL_ATTR_TIME_FMT, SQL_ATTR_TIME_SEP - The date and time formats and separators used for the connection.

Return Value

Nothing is returned. The result is reported in the SQLCA (see Exception Handling below).

Exception Handling

SQL diagnostics are reported in the SQLCA (see the IBM documentation for SQLCA field descriptions). SQLCA fields are defined in Profound.js programs with their names in lowercase.

  • sqlcode - Set to 0 when the attribute is set successfully.

  • sqlcode - Set to a negative value when the database reports an error; check sqlstate and sqlerrmc for details.

Additional SQL diagnostic output can be enabled with the PJS_SQL_DEBUG setting.

Examples

Example 1: Use the SQL Naming Convention

This example switches the default connection to the SQL naming convention so that the table can be qualified as demolib.productsp, then runs a statement and reads its last row.

Prerequisite: a table DEMOLIB/PRODUCTSP with a column PRID (packed decimal 5,0). Substitute your own library and table.

Set the naming convention for the default connection
pjs.define("prid", { type: "packed decimal", length: 5, decimals: 0 }); // Use the SQL naming convention (schema.table) instead of system naming (library/table). pjs.setConnectAttr(SQL_ATTR_DBC_SYS_NAMING, SQL_FALSE); var stmt = pjs.allocStmt(); pjs.setStmtAttr(stmt, SQL_ATTR_CURSOR_SCROLLABLE, SQL_TRUE); pjs.executeDirect(stmt, "SELECT prid FROM demolib.productsp ORDER BY prid"); pjs.fetchLast(stmt, prid); console.log("Highest product id: " + prid); pjs.close(stmt);

Example 2: Run Statements Without Commitment Control on a Named Connection

This example turns off commitment control on the connection named orders before updating a table that is not journaled. Without this, the update would fail on IBM i if the table is not journaled, because the default commitment control level requires journaling.

Set a connection attribute on a named connection
var db = pjs.getDB("orders"); // Run the following statements on the "orders" connection without commitment control, // the equivalent of COMMIT = *NONE. pjs.setConnectAttr(db, SQL_ATTR_COMMIT, SQL_TXN_NO_COMMIT); var stmt = pjs.prepare(db, "UPDATE orderhdr SET status = ? WHERE orderid = ?"); stmt.bindParameters(["SHIPPED", 5001]); stmt.execute(); if (sqlcode === 0) { console.log(sqlerrd[3] + " row(s) updated"); } stmt.close();

Related Pages