idb-connector
Advanced tools
Comparing version 1.1.5 to 1.1.6
# idb-connector changelog | ||
## 1.1.6 | ||
- Added alias shorthands for use during bindParams() | ||
- Added Add checks for value, io type and bindIndicator during bindParams() | ||
- Return null instead of empty [] when output params are not present, during execute() | ||
- Updated examples, API reference and test cases | ||
## 1.1.5 | ||
@@ -8,2 +15,3 @@ | ||
- Added CHANGLOG.md | ||
- Made buffer bigger for some unicode characters | ||
@@ -10,0 +18,0 @@ ## 1.1.4 |
1115
docs/README.md
@@ -1,175 +0,332 @@ | ||
# DB2 for i Access APIs | ||
[//]: # (TOC only works on BB) | ||
# DB2 for i Access APIs - idb-connector | ||
[//]: # (TOC built in macro on BB makes table of contents) | ||
[TOC] | ||
___ | ||
# Introduction | ||
The new DB2 add-on for Node.js is a JavaScript API set for DB2 database manipulation on IBM i. The add-on is shipped with Node.js and located in `/QOpenSys/QIBM/ProdData/OPS/Node4/os400/db2i/`. | ||
- `idb-connector` provides connectivity to DB2 from Node.js. | ||
It contains a new non-blocking driver db2ia.node and a deprecated blocking driver db2i.node in the bin directory, and their respective library file db2a.js and db2.js in the lib directory. For the usage of the old synchronized db2i add-on, please refer to the | ||
[document](https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/IBM%20i%20Technology%20Updates/page/DB2%20for%20i%20access%20APIs) | ||
- `idb-connector` uses DB2 CLI API to access DB2 subsequently this module is only supported for `IBM i` systems. | ||
To use the DB2 add-on, you only need to require the db2a.js file in your source code. | ||
## Install | ||
**Note:** The new DB2 Add-on requires Node.js v4, please check the effective Node.js by command `node -v` first. | ||
Available on npm | ||
- `npm install idb-connector` | ||
Build from source | ||
- `git clone https://bitbucket.org/litmis/nodejs-idb-connector.git` | ||
- `cd nodejs-idb-connector` | ||
- `npm install --build-from-source` | ||
___ | ||
# Examples | ||
[//]: # (On BB headers are prefixed by #markdown-header) | ||
[//]: # (Also the title of the header are made lower case) | ||
[//]: # (So to link to Async Exec header: #markdown-header-async-exec) | ||
### Async Exec | ||
### Basic Query | ||
```javascript | ||
var db = require('/QOpenSys/QIBM/ProdData/OPS/Node4/os400/db2i/lib/db2a'); | ||
var sql = "SELECT STATE,LSTNAM FROM QIWS.QCUSTCDT"; | ||
const {dbconn, dbstmt} = require('idb-connector'); | ||
var dbconn = new db.dbconn(); // Create a connection object. | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
connection = new dbconn(); // Create a connection object. | ||
dbconn.conn("*LOCAL"); // Connect to a database. | ||
connection.conn('*LOCAL'); // Connect to a database. | ||
var stmt = new db.dbstmt(dbconn); // Create a statement object of the connection. | ||
let statement = new dbstmt(dbconn); // Create a statement object of the connection. | ||
stmt.exec(sql, function(result) { | ||
statement.exec(sql, (result, error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
console.log('Result Set:\n', JSON.stringify(result)); | ||
console.log("Result: %s", JSON.stringify(result)); | ||
statement.close(); // Clean up the statement object. | ||
connection.disconn(); // Disconnect from the database. | ||
connection.close(); // Clean up the connection object. | ||
}); | ||
var fieldNum = stmt.numFields(); | ||
``` | ||
___ | ||
console.log("There are %d fields in each row.", fieldNum); | ||
### Sync Exec | ||
console.log("Name | Length | Type | Precise | Scale | Null"); | ||
for(var i = 0; i < fieldNum; i++) | ||
```javascript | ||
console.log("%s | %d | %d | %d | %d | %d", stmt.fieldName(i), stmt.fieldWidth(i), stmt.fieldType(i), stmt.fieldPrecise(i), stmt.fieldScale(i), stmt.fieldNullable(i)); | ||
const {dbconn, dbstmt} = require('idb-connector'); | ||
let sql = 'SELECT STATE FROM QIWS.QCUSTCDT', | ||
connection = new dbconn(); // Create a connection object. | ||
stmt.close(); // Clean up the statement object. | ||
connection.conn('*LOCAL'); // Connect to the database. | ||
dbconn.disconn(); // Disconnect from the database. | ||
let statement = new dbstmt(connection); // Create a statement object. | ||
dbconn.close(); // Clean up the connection object. | ||
result = statement.execSync(sql); | ||
console.log('Result Set:\n', JSON.stringify(result)); | ||
}); | ||
statement.close(); // Clean up the statement object. | ||
connection.disconn(); // Disconnect from the database. | ||
connection.close(); // Clean up the connection object. | ||
``` | ||
___ | ||
### Stored Procedure | ||
### Async Prepare Bind Execute | ||
Calling a Stored Procedure | ||
```javascript | ||
var db = require('/QOpenSys/QIBM/ProdData/OPS/Node4/os400/db2i/lib/db2a'); | ||
var sql = "call QXMLSERV.iPLUG512K(?,?,?,?)"; | ||
const idb = require('idb-connector'), | ||
{dbconn, dbstmt, IN, OUT, CHAR, CLOB} = idb; | ||
var dbconn = new db.dbconn(); | ||
let sql = 'call QXMLSERV.iPLUG512K(?,?,?,?)', | ||
connection = new dbconn(), | ||
ipc = '*NA', | ||
ctl = '*here', | ||
xmlIn ='<xmlservice><sh>system wrksbs</sh></xmlservice>', | ||
xmlOut = '', | ||
params = [[ipc, IN, CHAR], | ||
[ctl, IN, CHAR], | ||
[xmlIn, IN, CLOB], | ||
[xmlOut, OUT, CLOB] | ||
]; | ||
dbconn.conn("*LOCAL"); | ||
connection.conn('*LOCAL'); | ||
let statement = new dbstmt(connection); | ||
var stmt = new db.dbstmt(dbconn); | ||
statement.prepare(sql, (error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
statement.bindParam(params, (error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
statement.execute((out, error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
console.log('Output Params:\n', out); | ||
statement.close(); | ||
connection.disconn(); | ||
connection.close(); | ||
}); | ||
}); | ||
}); | ||
var ipc = "*NA"; | ||
``` | ||
___ | ||
var ctl = "*here"; | ||
### Sync Prepare Bind Execute | ||
var xmlIn ="<xmlservice><sh>system 'wrkactjob'</sh></xmlservice>"; | ||
var xmlOut = ""; | ||
```javascript | ||
stmt.prepare(sql, function(){ | ||
const {dbconn, dbstmt, IN, OUT, CLOB, CHAR} = require('idb-connector'); | ||
stmt.bindParam([ | ||
let sql = 'call QXMLSERV.iPLUG512K(?,?,?,?)', | ||
connection = new dbconn(), | ||
ipc = '*NA', | ||
ctl = '*here', | ||
xmlIn ='<xmlservice><sh>system wrksbs</sh></xmlservice>', | ||
xmlOut = '', | ||
params = [[ipc, IN, CHAR], | ||
[ctl, IN, CHAR], | ||
[xmlIn, IN, CLOB], | ||
[xmlOut, OUT, CLOB] | ||
]; | ||
[ipc, db.SQL_PARAM_INPUT, 1], | ||
connection.conn('*LOCAL'); | ||
[ctl, db.SQL_PARAM_INPUT, 1], | ||
let statement = new dbstmt(connection), | ||
[xmlIn, db.SQL_PARAM_INPUT, 0], | ||
statement.prepareSync(sql); | ||
statement.bindParamSync(params); | ||
[xmlOut, db.SQL_PARAM_OUTPUT, 0], | ||
let out = statement.executeSync(); | ||
], function(){ | ||
console.log('Output Params:\n', JSON.stringify(out)); | ||
stmt.execute(function(out) { //out is an array of the output parameters. | ||
statement.close(); | ||
connection.disconn(); | ||
connection.close(); | ||
``` | ||
___ | ||
for(var i=0; i<out.length; i++) | ||
### Async Fetch | ||
console.log("Output Param[%d] = %s \n", i, out[i]); | ||
Asynchronously retrieve one row from the result set | ||
stmt.close(); | ||
```javascript | ||
const {dbconn, dbstmt} = require('idb-connector'); | ||
dbconn.disconn(); | ||
let connection = new dbconn(); | ||
dbconn.close(); | ||
connection.conn('*LOCAL'); | ||
}); | ||
let statement = new dbstmt(connection); | ||
statement.prepare('SELECT * FROM QIWS.QCUSTCDT', (error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
statement.execute((error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
statement.fetch((row, rc) => { | ||
if (rc instanceof Error){ | ||
throw rc; | ||
} | ||
console.log('Row: \n', JSON.stringify(row)); | ||
statement.close(); | ||
connection.disconn(); | ||
connection.close(); | ||
}); | ||
}); | ||
}); | ||
``` | ||
___ | ||
### Concurrent Query | ||
### Sync Fetch | ||
Synchronously retrieve one row from the result set | ||
```javascript | ||
var dba = require('/QOpenSys/QIBM/ProdData/OPS/Node4/os400/db2i/lib/db2a'); | ||
var dbconn = new dba.dbconn(); | ||
const {dbconn, dbstmt} = require('idb-connector'); | ||
dbconn.conn("*LOCAL"); | ||
let connection = new dbconn(); | ||
var sqlA = new dba.dbstmt(dbconn); | ||
connection.conn('*LOCAL'); | ||
var sqlB = new dba.dbstmt(dbconn); | ||
let statement = new dbstmt(connection); | ||
console.log("Execute A."); | ||
statement.prepareSync('SELECT * FROM QIWS.QCUSTCDT'); | ||
statement.executeSync(); | ||
sqlA.exec(sql, function() { | ||
let row = statement.fetchSync(); | ||
console.log("Execute A Done."); | ||
console.log('Row:\n', JSON.stringify(row)); | ||
sqlA.close(); | ||
statement.close(); | ||
connection.disconn(); | ||
connection.close(); | ||
}); | ||
``` | ||
___ | ||
console.log("Execute B."); | ||
### Async FetchAll | ||
sqlB.exec(sql, function() { | ||
Asynchronously retrieve all rows from the result set | ||
console.log("Execute B Done."); | ||
```javascript | ||
sqlB.close(); | ||
const {dbstmt, dbconn} = require('idb-connector'); | ||
let connection = new dbconn(); | ||
connection.connect('*LOCAL'); | ||
let staement = new dbstmt(connection); | ||
statement.prepare('SELECT * FROM QWIS.QCUSTCDT', (error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
statement.execute((error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
statement.fetchAll(function(result, error){ | ||
if (error){ | ||
throw error; | ||
} | ||
console.log('Result Set:\n', JSON.stringify(result)); | ||
statement.close(); | ||
connection.disconn(); | ||
connection.close(); | ||
}); | ||
}); | ||
}); | ||
``` | ||
___ | ||
setTimeout(function(){ | ||
### Sync FetchAll | ||
dbconn.disconn(); | ||
Synchronously retrieve all rows from the result set | ||
dbconn.close(); | ||
```javascript | ||
},1000); //Sleep for 1 second to wait for both queries done. | ||
const {dbconn, dbstmt} = require('idb-connector'); | ||
let connection = new dbconn(); | ||
connection.conn('*LOCAL'); | ||
let statement = new dbstmt(connection); | ||
statement.prepareSync("SELECT * FROM SCHEMA.MYTABLE"); | ||
statement.executeSync(); | ||
let result = statement.fetchAllSync(); | ||
console.log('Result Set:\n', JSON.stringify(result)); | ||
statement.close(); | ||
connection.disconn(); | ||
connection.close(); | ||
``` | ||
# API Documentation | ||
# Class: dbconn | ||
The dbconn class is used to create a connection object. | ||
# Class dbconn | ||
The dbconn class represents a connection object. It can connect to a database, and disconnect from it. Please use the new operator to instantiate it and the delete function to clean up. | ||
Once the idb-connector is installed you can gain access with: | ||
`const {dbconn} = require('idb-connector');` | ||
## setConnAttr | ||
use the new operator to instantiate it | ||
`let conn = new dbconn();` | ||
Once instantiated the methods documented below can be performed. | ||
Make sure to call `disconn` and `close` when finished. | ||
## Contructor: dbconn() | ||
**Description:** | ||
Set connection attributes. | ||
Allocates a new connection handle, ensure `conn` function is called to connect to the target database. | ||
___ | ||
## dbconn.setConnAttr(attribute, value) | ||
**Description:** | ||
Sets a connection attribute to a provided valid value. | ||
**Syntax:** | ||
setConnAttr(int Attribute, int/string Value) | ||
setConnAttr(attribute, value) | ||
**Parameters** | ||
- **Attribute:** is the connection attribute to set. Refer to [this table](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/cli/rzadpfnsconx.htm%23rzadpfnsconx__tbcono) for more details. | ||
- **attribute:** `number(integer)` is the connection attribute to set. | ||
- **value:** `string | number(integer)` the value to set the specified attribute to. | ||
- **Value:** Depending on the Attribute, this can be an integer value, or a character string. | ||
**Returns:** | ||
`boolean(true)` upon success otherwise an error is thrown. | ||
**DB2 CLI API:** SQLSetConnectAttr | ||
@@ -179,19 +336,28 @@ | ||
**Comments:** The Auto Commit feature is automatically enabled. | ||
**Comments:** | ||
## getConnAttr | ||
Auto Commit attribute is automatically enabled. | ||
Refer to this [table](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/cli/rzadpfnsconx.htm%23rzadpfnsconx__tbcono) for more details. | ||
___ | ||
## dbconn.getConnAttr(attribute) | ||
**Description:** | ||
Returns the current settings for the specified connection option | ||
Returns the current settings for the specified connection attribute. | ||
**Syntax:** | ||
getConnAttr(int Attribute) | ||
getConnAttr(attribute) | ||
**Parameters:** | ||
- **Attribute:** is the connection attribute to set. Refer to [this table](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/cli/rzadpfngcona.htm) for more details. | ||
**Returns:** It returns the attribute option in the format of an integer or a string depending on the attribute type. | ||
- **attribute:** `number(integer)` the connection attribute to set. | ||
**Returns:** | ||
`string | number(integer)` depending on the attribute. | ||
**DB2 CLI API:** SQLGetConnectAttr | ||
@@ -201,117 +367,193 @@ | ||
**Comments:** | ||
## conn | ||
Refer to this [table](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/cli/rzadpfngcona.htm) for more details. | ||
___ | ||
## dbconn.conn(database) | ||
**Description:** | ||
Establishes a connection to the target database. The application can optionally supply a target SQL database, an authorization name, and an authentication string. | ||
Establishes a connection to the target database. | ||
**NOTE** '*LOCAL' can be provided as the `database` when connecting to a local database, allowing `user` & `password` to be optionally passed. | ||
**Syntax 1:** | ||
conn(string Database) | ||
conn(database) | ||
**Syntax 2:** | ||
conn(string Database, function Callback) | ||
conn(database, callback) | ||
**Syntax 3:** | ||
conn(string Database, string User, string Password) | ||
conn(database, user, password) | ||
**Syntax 4:** | ||
conn(string Database, string User, string Password, function Callback) | ||
conn(database, user, password, callback) | ||
**Parameters:** | ||
- **Database:** is the name or alias name of the database. | ||
- **User:** is the authorization name (user identifier). | ||
- **Password:** is the authentication string (password). | ||
- **Callback:** is a callback function running after the connection established. | ||
- **database:** `string` is the name or alias name of the database. | ||
- **user:** `string` is the authorization name (user identifier). | ||
- **password:** `string` is the authentication string (password). | ||
- **callback:** `function` is a callback function running after the `conn` is complete | ||
**DB2 CLI API:** SQLConnect | ||
**Valid Scope:** Before calling the exec() or prepare() function. | ||
**Valid Scope:** Before calling the dbstmt.exec() or dbstmt.prepare() function. | ||
___ | ||
## dbconn.disconn() | ||
## disconn | ||
**Description:** | ||
Ends the connection associated with the database connection handle. After calling this function, either call conn() to connect to another database, or delete the connection object. | ||
Ends the connection associated with the database connection handle. | ||
After calling this function, either call `conn` to connect to another database or `close`. | ||
**Syntax:** | ||
disconn() | ||
**DB2 CLI API:** SQLDisconnect | ||
**Valid Scope:** After calling the conn() function. | ||
**Returns:** | ||
## close | ||
`boolean(true)` upon success otherwise an error is thrown. | ||
___ | ||
## dbconn.close() | ||
**Description:** | ||
Frees the connection object. All DB2 for i resources associated with the connection object are freed. disconn() must be called before calling this function. | ||
Frees all DB2 for i resources associated with the connection object. | ||
`disconn()` must be called before calling this function. | ||
**Syntax:** | ||
close() | ||
**DB2 CLI API:** SQLFreeConnect | ||
**Valid Scope:** After calling the disconn() function. | ||
**Returns:** | ||
## debug | ||
`boolean(true)` upon success otherwise an error is thrown. | ||
___ | ||
## dbconn.debug(flag) | ||
**Description:** | ||
Print more detailed debugging information during execution. | ||
Enables or disables verbose output to the console. | ||
**Syntax:** | ||
debug(boolean OnOff) | ||
debug(flag) | ||
**Parameters:** | ||
- **OnOff:** Default value is false. If it is true, the program will print more detailed information. | ||
- **flag:** `boolean` to turn debug mode on or off. Default value is `false`. | ||
**Valid Scope:** All the life cycle. | ||
**Returns:** | ||
## validStmt | ||
`boolean` the current state of the debug flag otherwise an error is thrown. | ||
**Valid Scope:** Entire life cycle. | ||
___ | ||
## dbconn.validStmt(sql) | ||
**Description:** | ||
Checks if the SQL string is valid and interprets vendor escape clauses. If the original SQL string that is passed by the application contains vendor escape clause sequences, DB2 for i CLI returns the transformed SQL string that is seen by the data source (with vendor escape clauses either converted or discarded as appropriate). | ||
Checks if the SQL string is valid and interprets vendor escape clauses. | ||
If the original SQL string that is passed by the application contains vendor escape clause sequences, | ||
DB2 for i CLI returns the transformed SQL string that is seen by the data source (with vendor escape clauses either converted or discarded as appropriate). | ||
**Syntax:** | ||
validStmt(string Statement) | ||
validStmt(sql) | ||
**Parameters:** | ||
- **Statement:** is a SQL string that needs to be checked and escaped. | ||
- **sql:** `string` that needs to be checked and escaped. | ||
**Returns:** It returns an integer value indicating the scale of the specified column in the result set. | ||
**Returns:** | ||
`string` the transformed sql string upon success, otherwise an error is thrown. | ||
**DB2 CLI API:** SQLNativeSql | ||
**Valid Scope:** After calling conn() function | ||
**Valid Scope:** After calling `conn` function | ||
___ | ||
# Class dbstmt | ||
The dbstmt class represents a SQL statement object along with its query result set. The construct function accepts an input parameter of a connection object. One connection object can derive many statement objects. Please use the new operator to instantiate it and the delete function to clean up. | ||
Once the idb-connector is installed you can gain access with: | ||
## setStmtAttr | ||
`const {dbstmt} = require('idb-connector');` | ||
A connected `dbconn` object is required to create a new `dbstmt` object from. | ||
```javascript | ||
const {dbconn, dbstmt} = require('idb-connector'); | ||
let connection = new dbconn(); | ||
connection.conn('*LOCAL'); | ||
let statement = new dbstmt(dbconn); | ||
``` | ||
Once instantiated the methods documented below can be performed. | ||
Make sure to call `close` when finished with the statement object. | ||
## Constructor: dbstmt(connection) | ||
**Parameters:** | ||
- **dbconn:** `dbconn object` the connection object to create the statement from. | ||
- Ensure `connection` has connected to the database first with `conn` function. | ||
___ | ||
## dbstmt.setStmtAttr(attribute, value) | ||
**Description:** | ||
Set an attribute of a specific statement handle. To set an option for all statement handles associated with a connection handle, the application can call setConnAttr(). | ||
Set an attribute of a specific statement handle. | ||
To set an option for all statement handles associated with a connection handle `setConnAttr` can be used. | ||
**Syntax:** | ||
setStmtAttr(int Attribute, int/string Value) | ||
setStmtAttr(attribute, value) | ||
**Parameters:** | ||
- **Attribute:** is the statement attribute to set. Refer to [this table](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/cli/rzadpfnsstma.htm%23rzadpfnsstma__tbstmto) for more details. | ||
- **attribute:** `number(integer)` is the statement attribute to set. | ||
- **Value:** Depending on the Attribute, this can be an integer value, or a character string. | ||
- **value:** `string | number (integer)` the value to set the specified attribute to. | ||
**DB2 CLI API:** SQLSetStmtAttr | ||
**Valid Scope:** After allocating the statement handler. | ||
___ | ||
## getStmtAttr | ||
## dbstmt.getStmtAttr | ||
@@ -328,6 +570,8 @@ **Description:** | ||
- **Attribute:** is the connection attribute to set. Refer to [this table](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/cli/rzadpfngstma.htm) for more details. | ||
- **attribute:** is the connection attribute to set. Refer to this [table](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/cli/rzadpfngstma.htm) for more details. | ||
**Returns:** It returns the attribute option in the format of an integer or a string depending on the attribute type. | ||
**Returns:** | ||
`string | number (integer)` depending on the attribute. | ||
**DB2 CLI API:** SQLGetStmtAttr | ||
@@ -337,51 +581,70 @@ | ||
___ | ||
## exec | ||
## dbstmt.exec(sql, callback) | ||
**Description:** | ||
Directly runs the specified SQL statement. The statement can only be processed once. Also, the connected database server must be able to prepare the statement. This is a non-blocking API. | ||
Asynchronously runs the specified SQL statement. | ||
**NOTE** use the `execute` function to call stored procedures | ||
**Syntax:** | ||
exec(string SQL, function Callback(JsonObj)) | ||
exec(sql,callback) | ||
exec(string SQL, function Callback(JsonObj, Error)) | ||
**Parameters** | ||
- **SQL:** is the SQL statement string. | ||
- **sql:** `string` is the sql statement to execute. | ||
- **Callback(JsonObj [, Error]):** is a callback function to process the result set of the SQL statement for callers. JsonObj is the result set of the SELECT SQL statement. It is in the JSON format. Error is the error message when error happens. It's a optional parameter. | ||
- **callback(resultSet, error):** `function` to process after `exec` is complete. | ||
- **resultSet:** `array` of `objects` each object represents a row of data. | ||
- If an error occurred or there is no `resultSet` it is set to `null`. | ||
- **error:** `Error object` when `execSync` is unsuccessful. Otherwise `error` is set to `null`. | ||
**DB2 CLI API:** SQLExecDirect | ||
**Valid Scope:** After calling the conn() function. | ||
**Valid Scope:** After calling the `conn` function. | ||
**Example:** [Here](#markdown-header-async-exec) | ||
## execSync | ||
___ | ||
## dbstmt.execSync(sql [, callback]) | ||
**Description:** | ||
The synchronized version of exec(). This is a blocking API. | ||
The synchronous blocking version of `exec`. | ||
**Syntax 1:** | ||
exec(string SQL) | ||
execSync(sql) | ||
**Parameters** | ||
- **sql:** `string` is the sql statement to execute. | ||
**Returns:** | ||
- **resultSet:** `array` of `objects` each object represents a row of data. | ||
- If an error occurred or there is no `resultSet` , `null` will be returned. | ||
**Syntax 2:** | ||
exec(string SQL, function Callback(JsonObj)) | ||
execSync(sql, callback(resultSet, error)) | ||
**Parameters** | ||
- **SQL:** is the SQL statement string. | ||
- **sql:** `string` is the sql statement to execute. | ||
- **Callback(JsonObj):** is a callback function to process the result set of the SQL statement for callers. JsonObj is the result set of the SELECT SQL statement. It is in the JSON format. | ||
- **callback(resultSet, error):** `function` to process after `execSync` is complete. | ||
- **resultSet:** `array` of `objects` each object represents a row of data. If an error occurred or there is no `resultSet` it is set to `null`. | ||
- **error:** `Error object` when `execSync` is unsuccessful. Otherwise `error` is set to `null`. | ||
**Comments:** | ||
- If the SQL statement is UPDATE, INSERT, MERGE, SELECT from INSERT, or DELETE statement, nothing will be returned and it is recommended to use Syntax 1. And user can issue the numRows() function to get the affected row number. | ||
- It is recommended to invoke the numFields(), numRows(), fieldName() and other result set related functions in this callback function. Because they rely on the temporal result set in memory. After running execSync() the result set will be destroyed and cleaned up. | ||
- It is recommended to invoke the `numFields`, `numRows`, `fieldName` and other result set related functions in this callback function, because they rely on the temporal result set in memory. | ||
- After running `execSync` the result set will be destroyed and cleaned up. | ||
@@ -392,37 +655,30 @@ **DB2 CLI API:** SQLExecDirect | ||
Example: | ||
```javascript | ||
var db = require('/QOpenSys/QIBM/ProdData/OPS/Node4/os400/db2i/lib/db2a'); | ||
var sql = "SELECT STATE FROM QIWS.QCUSTCDT"; | ||
var dbconn = new db.dbconn(); // Create a connection object. | ||
dbconn.conn("*LOCAL"); // Connect to a database. | ||
var stmt = new db.dbstmt(dbconn); // Create a statement object of the connection. | ||
stmt.execSync(sql, function(result) { | ||
console.log("Result: %s", JSON.stringify(result)); | ||
}); | ||
stmt.close(); | ||
conn.disconn(); | ||
conn.close(); | ||
``` | ||
**Example:** [Here](#markdown-header-sync-exec) | ||
## prepare | ||
___ | ||
## dbstmt.prepare(sql, callback) | ||
**Description:** | ||
Associates an SQL statement with the input statement handle and sends the statement to the DBMS to be prepared. The application can reference this prepared statement by passing the statement handle to other functions. | ||
Asynchronously associates an SQL statement with the input statement handle and sends the statement to the DBMS to be prepared. | ||
**Syntax:** | ||
prepare(string SQL, function Callback()) | ||
prepare(sql, callback) | ||
prepare(string SQL, function Callback(Error)) | ||
**Parameters** | ||
- **SQL:** is the SQL statement string. | ||
- **sql:** is the SQL statement string. | ||
- **Callback([Error]):** is a callback function. Error is an optional parameter of the callback function. It is the error message when error happens. | ||
- **callback(error)**: `function` to process after `prepare` is complete. | ||
- **error**: `Error object` when `prepare` is unsuccessful. Otherwise `error` is set to `null`. | ||
**Comments:** If the statement handler has been used with a SELECT statement, closeCursor() must be called to close the cursor, before calling prepare() again. | ||
**Comments:** | ||
If the statement handler has been used with a SELECT statement, | ||
`closeCursor` must be called to close the cursor, before calling `prepare` again. | ||
**DB2 CLI API:** SQLPrepare | ||
@@ -436,25 +692,40 @@ | ||
**Example:** [Here](#markdown-header-async-prepare-bind-execute) | ||
___ | ||
## prepareSync | ||
## dbstmt.prepareSync(sql [, callback]) | ||
**Description:** | ||
The synchronized version of prepare(). This is a blocking API. | ||
Synchronous version of `prepare`. | ||
**Syntax 1:** | ||
prepareSync(string SQL) | ||
prepareSync(sql) | ||
**Parameters:** | ||
- **sql:** `string` the sql statement to prepare. | ||
**Returns:** | ||
`void` no return type, if an error occurred it will be thrown. | ||
**Syntax 2:** | ||
prepareSync(string SQL, function Callback()) | ||
prepareSync(sql, callback) | ||
**Parameters:** | ||
- **SQL:** is the SQL statement string. | ||
- **sql:** `string` the sql statement to prepare. | ||
- **Callback():** is a callback function. | ||
- **callback(error)**: `function` to process after `prepareSync` is complete. | ||
- **error**: `Error object` when `prepareSync` is unsuccessful. Otherwise `error` is set to `null`. | ||
**Comments:** If the statement handler has been used with a SELECT statement, closeCursor() must be called to close the cursor, before calling prepareSync() again. | ||
**Comments:** | ||
If the statement handler has been used with a SELECT statement | ||
`closeCursor` must be called first before calling `prepareSync` again. | ||
**DB2 CLI API:** SQLPrepare | ||
@@ -464,77 +735,100 @@ | ||
- After calling the conn() function. | ||
- After calling the `conn` function. | ||
- Before calling the executeSync() or bindParamSync() function. | ||
- Before calling the `executeSync` or `bindParamSync` function. | ||
**Example:** | ||
```javascript | ||
var db = require('/QOpenSys/QIBM/ProdData/OPS/Node4/os400/db2i/lib/db2a'); | ||
var sql = "SELECT STATE FROM QIWS.QCUSTCDT"; | ||
var dbconn = new db.dbconn(); // Create a connection object. | ||
dbconn.conn("*LOCAL"); // Connect to a database. | ||
var stmt = new db.dbstmt(dbconn); // Create a statement object of the connection. | ||
stmt.prepareSync("call QXMLSERV.iPLUG512K(?,?,?,?)"); | ||
stmt.bindParamSync([ | ||
["*NA", db.SQL_PARAM_INPUT, 1], | ||
["*here", db.SQL_PARAM_INPUT, 1], | ||
["<xmlservice></xmlservice>", db.SQL_PARAM_INPUT, 0], | ||
["", db.SQL_PARAM_OUTPUT, 0], | ||
]); | ||
stmt.executeSync(function callback(out) { //out is an array of the output parameters. | ||
console.log("Result: %s", JSON.stringify(out)); | ||
}); | ||
stmt.close(); | ||
conn.disconn(); | ||
conn.close(); | ||
``` | ||
**Example:** [Here](#markdown-header-sync-prepare-bind-execute) | ||
___ | ||
## bindParam | ||
## dbstmt.bindParam(params, callback) | ||
**Description:** | ||
Associate (bind) parameter markers in an SQL statement to application variables. Data is transferred from the application to the Database Management System (DBMS) when execute() is called. Data conversion might occur when the data is transferred. | ||
Asynchronously associate (bind) parameter markers in an SQL statement to application variables. | ||
This function must also be used to bind application storage to a parameter of a stored procedure where the parameter can be input, output. | ||
Data is transferred from the application to the Database Management System (DBMS) when `execute` function is called. | ||
This is a non-blocking API. | ||
Data conversion might occur when the data is transferred. | ||
This function must also be used to bind to a parameter of a stored procedure where the parameter can be: input, output, or both | ||
**Syntax:** | ||
bindParam(array ParamList, function Callback()) | ||
bindParam(params, callback) | ||
bindParam(array ParamList, function Callback(Error)) | ||
**Parameters:** | ||
**Parameters** | ||
- **params**: `array` representing the binding parameter list. Each parameter element will also be an Array with 3 values `[value, io, indicator]`. | ||
- `value` is the parameter to bind. | ||
- `io` specifies whether the parameter is for input, output, or both. | ||
- `io` can be: | ||
- SQL_PARAM_INPUT | ||
- SQL_PARAM_OUTPUT | ||
- SQL_PARAM_INPUT_OUTPUT | ||
- Shorthand equivleants are: | ||
- IN | ||
- OUT | ||
- INOUT | ||
- `indicator` specifies how to process the parameter. For example to process a string as a CLOB set the indicator to CLOB. | ||
- `indcator` can be: | ||
- CHAR | ||
- INT | ||
- NUMERIC | ||
- BINARY | ||
- BLOB | ||
- CLOB | ||
- BOOLEAN | ||
- NULL | ||
- **ParamList:** is a javascript array representing the binding parameter list. Each parameter has three attributes which are also represented as an array. The three attributes are Value, In/Out Type and Indicator. Value can be an integer value or a string. In/Out Type is an integer. If Value is an input value, please set it to 0. Otherwise, set it to 1. Indicator is an integer flag to tell the function how to process some special types of parameters. Different type of Value requires corresponding Indicator. If the parameter is CLOB string, set it to 0, if it is a NTS string, set it to 1, if it is an integer, set it to 2. | ||
These values are constants which are attached to object returned when you `const idb = require('idb-connector')`. | ||
- **Callback([Error]):** is a callback function. Error is an optional parameter. It is the error message when error happens. | ||
You can access the constants like: `idb.IN`. | ||
- **callback(error):** `function` to process after `bindParam` is complete. | ||
- **error:** `Error object` when `bindParam` is unsuccessful. Otherwise `error` is set to `null`. | ||
**DB2 CLI API:** SQLBindParameter | ||
**Valid Scope:** In the callback function of the prepare() function. | ||
**Valid Scope:** In the callback function of the `prepare` function. | ||
**Example:** [Here](#markdown-header-async-prepare-bind-execute) | ||
## bindParamSync | ||
___ | ||
## dbstmt.bindParamSync(params [, callback]) | ||
**Description:** | ||
The synchronized version of bindParam(). | ||
This is a blocking API. | ||
Synchronous version of `bindParam`. | ||
**Syntax 1:** | ||
bindParamSync(array ParamList) | ||
bindParamSync(params) | ||
**Parmeters:** | ||
- **params**: as described in [bindParam](#dbstmt.bindParam()) | ||
**Returns:** | ||
`void` no return type, if an error occurred it will be thrown. | ||
**Syntax 2:** | ||
bindParamSync(array ParamList, function Callback()) | ||
bindParamSync(params, callback) | ||
**Parameters** | ||
- **ParamList**: is a javascript array representing the binding parameter list. Each parameter has three attributes which are also represented as an array. The three attributes are Value, In/Out Type and Indicator. Value can be an integer value or a string. In/Out Type is an integer. If Value is an input value, please set it to 0. Otherwise, set it to 1. Indicator is an integer flag to tell the function how to process some special types of parameters. Different type of Value requires corresponding Indicator. If the parameter is CLOB string, set it to 0, if it is a NTS string, set it to 1, if it is an integer, set it to 2. | ||
- **params**: as described in [bindParam()](#dbstmt.bindParam()) | ||
- **Callback():** is a callback function. | ||
- **callback(error)**: `function` to process after `bindParamSync` is complete. | ||
- **error**: `Error object` when `bindParamSync` is unsuccessful. Otherwise `error` is set to `null`. | ||
**Example:** [Here](#markdown-header-sync-prepare-bind-execute) | ||
**DB2 CLI API:** SQLBindParameter | ||
@@ -544,43 +838,39 @@ | ||
- After calling the prepareSync() function. | ||
- Before calling the executeSync() function. | ||
- After calling the `prepareSync` function. | ||
- Before calling the `executeSync` function. | ||
**Example:** See the example of prepareSync(). | ||
___ | ||
## dbstmt.execute(callback) | ||
## execute | ||
**Description:** | ||
Runs a statement that was successfully prepared using prepare() once or multiple times. The statement is processed with the current values of any application variables that were bound to parameters markers by bindParam(). | ||
Asynchronously Runs a statement that was successfully prepared using `prepare`. | ||
This is a non-blocking API. | ||
The statement is processed with the current values of any application variables that were bound to parameters markers by `bindParam`. | ||
**Syntax 1:** | ||
**Syntax:** | ||
execute(function Callback()) | ||
execute(callback(outputParams, error)) | ||
**Syntax 2:** | ||
execute(function Callback(Out)) | ||
**Syntax 3:** | ||
execute(function Callback(Error)) | ||
**Parameters** | ||
- **Callback(Out):** is a callback function. If the output contains parameters of the parameter markers, Out is a list of the output parameters in the java script array format. Otherwise, the callback function takes no output Parameters and is only for fetching the result set. Error is the error message when error happens. | ||
- **callback(outputParams, error):** `function` to process after `execute` is complete. | ||
- **outputParams**: an `array` of an output parameters. If an error occurred or no output parameters are available `outputParams` is set to `null`. | ||
- **error**: `Error object` when `execute` is unsuccessful. Otherwise `error` is set to `null`. | ||
**DB2 CLI API:** SQLExecute | ||
**Valid Scope:** In the callback function of the prepare() or bindParam() function. | ||
**Valid Scope:** In the callback function of the `prepare` or `bindParam` function. | ||
**Example:** [Here](#markdown-header-async-prepare-bind-execute) | ||
## executeSync | ||
___ | ||
## dbstmt.executeSync([callback]) | ||
**Description:** | ||
The synchronized version of execute(). | ||
This is a blocking API. | ||
The synchronized version of `execute`. | ||
@@ -591,12 +881,22 @@ **Syntax 1:** | ||
**Returns:** | ||
- **outputParams**: an `array` of output parameters. If an error occured it is thrown. | ||
- If no output parameters are available `null` is returned. | ||
**Syntax 2:** | ||
executeSync(function Callback(OutParamList)) | ||
executeSync(callback) | ||
**Parameters:** | ||
- **Callback(OutParamList):** is a callback function to process the output parameters of the parameter markers. OutParamList is a list of the output parameters. It is in the java script array format. | ||
- **callback(outputParams, error):** `function` to process after `executeSync` is complete. | ||
- **outputParams**: `array` of an output parameters. If an error occured or no output parameters are available `outputParams` is set to `null`. | ||
- **error**: `Error object` when `executeSync` is unsuccessful. Otherwise `error` is set to `null`. | ||
**Comments:** If the statement also return a result set, user can issue the fetch() function to retrieve the data row by row. | ||
**Comments:** | ||
If the statement also returns a result set, user can issue the `fetch` function to retrieve the data row by row. | ||
**DB2 CLI API:** SQLExecute | ||
@@ -606,10 +906,10 @@ | ||
**Example:** See the example of prepareSync(). | ||
**Example:** [Here](#markdown-header-sync-prepare-bind-execute) | ||
___ | ||
## dbstmt.nextResult() | ||
## nextResult | ||
**Description:** | ||
Determines whether there is more information available on the statement handle that has been associated with a stored procedure that is returning result sets. | ||
Determines whether there is more information available on the statement handle that has been associated with a stored procedure that is returning multiple result sets. | ||
@@ -620,4 +920,8 @@ **Syntax:** | ||
**Comments:** After completely processing the first result set, the application can call nextResult() to determine if another result set is available. If the current result set has unfetched rows, nextResult() discards them by closing the cursor. | ||
**Comments:** | ||
After completely processing the first result set, the application can call `nextResult` to determine if another result set is available. | ||
If the current result set has unfetched rows, `nextResult` discards them by closing the cursor. | ||
**DB2 CLI API:** SQLMoreResults | ||
@@ -627,8 +931,9 @@ | ||
___ | ||
## commit | ||
## dbstmt.commit() | ||
**Description:** | ||
Commit all changes to the database that have been made on the connection since connect time or the previous call to commit(). | ||
Commit all changes to the database that have been made on the connection since connect time or the previous call to `commit`. | ||
@@ -643,8 +948,13 @@ **Syntax:** | ||
**Comments:** | ||
## rollback | ||
Auto commit is enabled by default. | ||
___ | ||
## dbstmt.rollback() | ||
**Description:** | ||
Rollback all changes to the database that have been made on the connection since connect time or the previous call to commit(). | ||
Rollback all changes to the database that have been made on the connection since connect time or the previous call to `commit`. | ||
@@ -657,11 +967,18 @@ **Syntax:** | ||
**Valid Scope:** After calling the execute() or exec() function. | ||
**Valid Scope:** After calling the `execute` or `exec` function. | ||
___ | ||
## closeCursor | ||
## dbstmt.closeCursor() | ||
**Description:** | ||
Calling closeCursor() closes any cursor associated with the dbstmt object and discards any pending results. If no open cursor is associated with the dbstmt object, the function has no effect. If the dbstmt object references a stored procedure that has multiple result sets, the closeCursor() closes only the current result set. Any additional result sets remain open and usable.If you want to reuse the dbstmt object, please call closeCursor() before calling exec() or prepare()for another SQL statement. Otherwise, you just need to call delete to completely remove it. | ||
Calling `closeCursor` closes any cursor associated with the dbstmt object and discards any pending results. | ||
If no open cursor is associated with the dbstmt object, the function has no effect. | ||
If the dbstmt object references a stored procedure that has multiple result sets, `closeCursor` closes only the current result set. Any additional result sets remain open and usable. | ||
If you want to reuse the dbstmt object, please call `closeCursor` before calling `exec` or `prepare` for another SQL statement. | ||
**Syntax:** | ||
@@ -675,9 +992,12 @@ | ||
## close | ||
___ | ||
## dbstmt.close() | ||
**Description:** | ||
DB2 for i resources associated with the statement object are freed. The open cursor, if any, is closed and all pending results are discarded. | ||
DB2 for i resources associated with the statement object are freed. | ||
The open cursor, if any, is closed and all pending results are discarded. | ||
**Syntax:** | ||
@@ -691,37 +1011,32 @@ | ||
## fetch | ||
___ | ||
## dbstmt.fetch([orient,] [offset,] callback) | ||
**Description:** | ||
Advances the cursor to the next row of the result set, and retrieves any bound columns. Or positions the cursor based on the requested orientation and then retrieves any bound columns. | ||
Asynchronously advances the cursor to the next row of the result set, and retrieves any bound columns. | ||
This is a non-blocking API. | ||
Or positions the cursor based on the requested orientation and then retrieves any bound columns. | ||
**Syntax 1:** | ||
fetch() | ||
fetch(callback) | ||
**Syntax 2:** | ||
**Parameters** | ||
fetch(function Callback(Row)) | ||
- **callback(row, error):** `function` to process after `fetchS` is complete. | ||
- **row**: `object` representing a row of data. If an error occured or there is nothing to fetch `row` is set to `null`. | ||
- **error**: `Error object` when `fetch` is unsuccessful. Otherwise `error` is set to the return code from `SQLFETCH`. When error = `SQL_NO_DATA_FOUND` the end of the result set has been reached. | ||
fetch(function Callback(Row, Error)) | ||
**Syntax 2:** | ||
**Syntax 3:** | ||
fetch(orient, offset, callback) | ||
fetch(int Orient, int Offset) | ||
**Parameters** | ||
**Syntax 4:** | ||
- **orient:** `number(integer)` sets the fetch orientation. The valid values are below: | ||
fetch(int Orient, int Offset, function Callback(Row)) | ||
fetch(int Orient, int Offset, function Callback(Row, Error)) | ||
**Parameters:** | ||
- **Callback(Row, Error):** is a callback function to process the fetched row. Row is the fetched row represented in a key-value java script array. Error is the error message when error happens. | ||
- **Orient:** sets the fetch orientation. The valid values are below: | ||
- `SQL_FETCH_ABSOLUTE`: Move to the row specified by the Offset argument. | ||
@@ -745,9 +1060,16 @@ | ||
- **Offset:** is the row offset for relative positioning. | ||
- **Offset:** `number(integer)` is the row offset for relative positioning. | ||
**Note:** To use orientation `SQL_FETCH_RELATIVE` with `Offset`, the cursor must be **dynamic**. | ||
```javascript | ||
stmt.setStmtAttr(db.SQL_ATTR_CURSOR_TYPE, db.SQL_CURSOR_DYNAMIC); | ||
``` | ||
`stmt.setStmtAttr(idb.SQL_ATTR_CURSOR_TYPE, idb.SQL_CURSOR_DYNAMIC);` | ||
- **callback(row, rc):** `function` to process after `fetch` is complete. | ||
- **row**: `object` representing a row of data. If an error occured or there is nothing to fetch `row` is set to `null`. | ||
- **rc**: `Error object` when `fetch` is unsuccessful. Otherwise `rc` is set to the return code from `SQLFETCH`. When error = `SQL_NO_DATA` the end of the result set has been reached. | ||
**DB2 CLI API:** SQLFetch or SQLFetchScroll | ||
@@ -757,32 +1079,12 @@ | ||
**Example:** | ||
```javascript | ||
stmt.prepare("SELECT * FROM ...", function(){ | ||
stmt.execute(function(){ | ||
function asyncFetch(){ | ||
stmt.fetch(function callback(row, rc){ | ||
assert(rc != db.SQL_ERROR); | ||
console.log(row); | ||
if(rc != db.SQL_NO_DATA_FOUND) | ||
asyncFetch(); | ||
else { // the last row. | ||
stmt.close(); | ||
conn.disconn(); | ||
conn.close(); | ||
} | ||
}); | ||
} | ||
asyncFetch(); | ||
}); | ||
}); | ||
``` | ||
**Example:** [Here](#markdown-header-async-fetch) | ||
## fetchSync | ||
___ | ||
## dbstmt.fetchSync() | ||
**Description:** | ||
The synchronized version of fetch(). | ||
Synchronous version of `fetch`. | ||
This is a blocking API. | ||
**Syntax 1:** | ||
@@ -806,31 +1108,11 @@ | ||
- **Callback(Row):** is a callback function to process the fetched row. Row is the fetched row represented in a key-value java script array. | ||
- **orient:** as described in `fetch` above | ||
- **Orient:** sets the fetch orientation. The valid values are below: | ||
- **offset:** as described in `fetch` above. | ||
- `SQL_FETCH_ABSOLUTE`: Move to the row specified by the Offset argument. | ||
- **callback(row, error):** `function` to process after `fetchSync` is complete. | ||
- **row**: `object` representing a row of data. If an error occured or there is nothing to fetch `row` is set to `null`. | ||
- **error**: `Error object` when `fetch` is unsuccessful. Otherwise `error` is set to the return code from `SQLFETCH`. When error = `SQL_NO_DATA` the end of the result set has been reached. | ||
- `SQL_FETCH_FIRST`: Move to the first row of the result set. | ||
- `SQL_FETCH_LAST`: Move to the last row of the result set. | ||
- `SQL_FETCH_NEXT`: Move to the row following the current cursor position. | ||
- `SQL_FETCH_PRIOR`: Move to the row preceding the current cursor position. | ||
- `SQL_FETCH_RELATIVE` If Offset is: | ||
- Positive, advance the cursor that number of rows. | ||
- Negative, back up the cursor that number of rows. | ||
- Zero, do not move the cursor. | ||
- **Offset:** is the row offset for relative positioning. | ||
**Note:** To use orientation `SQL_FETCH_RELATIVE` with `Offset`, the cursor must be **dynamic**. | ||
```javascript | ||
stmt.setStmtAttr(db.SQL_ATTR_CURSOR_TYPE, db.SQL_CURSOR_DYNAMIC); | ||
``` | ||
**DB2 CLI API:** SQLFetch or SQLFetchScroll | ||
@@ -840,20 +1122,21 @@ | ||
**Example:** [Here](#markdown-header-sync-fetch) | ||
## fetchAll | ||
## dbstmt.fetchAll(callback) | ||
**Description:** | ||
Fetch all the rows of data from the result set in one time. | ||
Asynchronously retrieves all the rows from the result set if available. | ||
This is a non-blocking API. | ||
**Syntax:** | ||
fetchAll(function Callback(Row)) | ||
fetchAll(callback) | ||
fetchAll(function Callback(Row, Error)) | ||
**Parameters** | ||
- **Callback(Row[ , Error]):** is a callback function to process the fetched row. Row is the fetched row represented in a key-value java script array. Error is an optional parameter. It is the error message when error happens. | ||
- **callback(resultSet, error):** `function` to process after `fetchAll` is complete. | ||
- **resultSet**: an `array` of `objects` each object represents a row of data. | ||
- If an error occured or there is no `resultSet` it is set to `null` | ||
- **error**: `Error object` when `fetchAll` is unsuccessful. Otherwise `error` is set to `null`. | ||
@@ -864,31 +1147,35 @@ **DB2 CLI API:** SQLFetch | ||
**Example:** | ||
```javascript | ||
stmt.prepare("SELECT * FROM ...", function(){ | ||
stmt.execute(function(){ | ||
stmt.fetchAll(function(result){ | ||
console.log(result); | ||
stmt.close(); | ||
}); | ||
}); | ||
}); | ||
``` | ||
**Example:** [Here](#markdown-header-async-fetchAll) | ||
___ | ||
## dbstmt.fetchAllSync([callback]) | ||
## fetchAllSync | ||
**Description:** | ||
The synchronized version of fetchAll(). | ||
Synchronous version of `fetchAll`. | ||
This is a blocking API. | ||
**Syntax 1:** | ||
fetchAllSync() | ||
**Syntax:** | ||
**Returns** | ||
fetchAllSync(function Callback(Row)) | ||
- **resultSet**: an `array` of `objects` each object represents a row of data. | ||
- If there is no result set `null` is returned. | ||
- If an error occurs it will be thrown. | ||
**Syntax 2:** | ||
fetchAllSync(callback) | ||
**Parameters** | ||
- **Callback(Row):** is a callback function to process the fetched row. Row is the fetched row represented in a key-value java script array. | ||
- **callback(resultSet, error):** `function` to process after `fetchAll` is complete. | ||
- **resultSet**: an `array` of `objects` each object represents a row of data. | ||
- If an error occured `resultSet` is set to `null` | ||
- **error**: `Error object` when `fetchAllSync` is unsuccessful. Otherwise `error` is set to `null`. | ||
**Example:** [Here](#markdown-header-sync-fetchAll) | ||
**DB2 CLI API:** SQLFetch | ||
@@ -898,18 +1185,9 @@ | ||
**Example:** | ||
```javascript | ||
stmt.prepareSync("SELECT * FROM ..."); | ||
stmt.executeSync(); | ||
stmt.fetchAllSync(function callback(rs){ | ||
console.log(rs); | ||
}); | ||
stmt.close(); | ||
``` | ||
___ | ||
## numFields | ||
## dbstmt.numFields() | ||
**Description:** | ||
Returns the number of fields contained in a result set. | ||
Retrieves number of fields contained in the result set if available. | ||
@@ -920,14 +1198,16 @@ **Syntax:** | ||
**Returns:** It returns an integer value indicating number of fields in the result set. | ||
**Returns:** | ||
`number(integer)` indicating number of fields in the result set. | ||
**DB2 CLI API:** SQLNumResultCols | ||
**Valid Scope:** When the result set is available. | ||
___ | ||
## dbstmt.numRows() | ||
## numRows | ||
**Description:** | ||
Returns the number of rows in a table affected by an UPDATE, INSERT, MERGE, SELECT from INSERT, or DELETE statement processed against the table. | ||
Returns the number of rows in a table affected by the last executed sql statement if available. | ||
@@ -938,66 +1218,74 @@ **Syntax:** | ||
**Returns:** It returns an integer value indicating number of rows affected by the operation. | ||
**Returns:** | ||
`number(integer)` indicating number of rows affected by the operation. | ||
**DB2 CLI API:** SQLRowCount | ||
**Valid Scope:** When the result set is available. | ||
___ | ||
## dbstmt.fieldType(index) | ||
## fieldType | ||
**Description:** | ||
Returns the data type of the indicated column in a result set. | ||
If a valid index is provided, `fieldType` returns the data type of the indicated field. | ||
**Syntax:** | ||
fieldType(int Index) | ||
fieldType(index) | ||
**Parameters:** | ||
- **Index:** is the column number in a result set, ordered sequentially left to right, starting at 0. | ||
- **index:** `number(integer)` the column number in a result set, ordered sequentially left to right, starting at 0. | ||
**Returns:** It returns an integer value indicating the data type of the specified column in the result set. | ||
**Returns:** | ||
`number(integer)` indicating the data type of the specified column in the result set. | ||
**DB2 CLI API:** SQLColAttribute | ||
**Valid Scope:** When the result set is available. | ||
___ | ||
## fieldWidth | ||
## dbstmt.fieldWidth(index) | ||
**Description:** | ||
Returns the width of the indicated column in a result set. | ||
If a valid index is provided, `fieldWidth` returns the field width of the indicated field. | ||
**Syntax:** | ||
fieldWidth(int Index) | ||
fieldWidth(index) | ||
**Parameters** | ||
- **Index:** is the column number in a result set, ordered sequentially left to right, starting at 0. | ||
- **index:** `number(integer)` the column number in a result set, ordered sequentially left to right, starting at 0. | ||
**Returns:** It returns an integer value indicating the width of the specified column in the result set. | ||
**Returns:** | ||
`number(integer)` indicating the width of the specified column in the result set. | ||
**DB2 CLI API:** SQLColAttribute | ||
**Valid Scope:** When the result set is available. | ||
___ | ||
## dbstmt.fieldNullable(index) | ||
## fieldNullable | ||
**Description:** | ||
Returns if the indicated column in a result set can be NULL. | ||
If a valid index is provided, fieldNullable returns true | false indicating if field can be set to null. | ||
**Syntax:** | ||
fieldNullable(int Index) | ||
fieldNullable(index) | ||
**Parameters** | ||
- **Index:** is the column number in a result set, ordered sequentially left to right, starting at 0. | ||
- **index:** `number(integer)` the column number in a result set, ordered sequentially left to right, starting at 0. | ||
**Returns:** It returns an Boolean value indicating if the indicated column in a result set can be NULL. | ||
**Returns:** | ||
`boolean` indicating if the column can be set to NULL. | ||
**DB2 CLI API:** SQLColAttribute | ||
@@ -1007,65 +1295,71 @@ | ||
___ | ||
## dbstmt.fieldName(index) | ||
## fieldName | ||
**Description:** | ||
Returns the name of the indicated column in a result set. | ||
If a valid index is provided, `fieldName` returns the name of the indicated field. | ||
**Syntax:** | ||
fieldName(int Index) | ||
fieldName(index) | ||
**Parameters** | ||
- **Index:** is the column number in a result set, ordered sequentially left to right, starting at 0. | ||
- **index:** `number(integer)` the column number in a result set, ordered sequentially left to right, starting at 0. | ||
**Returns:** It returns an string value indicating the name of the specified column in the result set. | ||
**Returns:** | ||
`string` indicating the name of the specified column in the result set. | ||
**DB2 CLI API:** SQLColAttribute | ||
**Valid Scope:** When the result set is available. | ||
___ | ||
## fieldPrecise | ||
## dbstmt.fieldPrecise(index) | ||
**Description:** | ||
Returns the precision of the indicated column in a result set. | ||
If a valid index is provided, `fieldPrecise` returns the precision of the indicated field. | ||
**Syntax:** | ||
fieldPrecise(int Index) | ||
fieldPrecise(index) | ||
**Parameters** | ||
- **Index:** is the column number in a result set, ordered sequentially left to right, starting at 0. | ||
- **index:** `number(integer)` the column number in a result set, ordered sequentially left to right, starting at 0. | ||
**Returns:** It returns an integer value indicating the precision of the specified column in the result set. | ||
**Returns:** | ||
`number(integer)` indicating the precision of the specified column in the result set. | ||
**DB2 CLI API:** SQLColAttribute | ||
**Valid Scope:** When the result set is available. | ||
___ | ||
## fieldScale | ||
## dbstmt.fieldScale(index) | ||
**Description:** | ||
Returns the scale of the indicated column in a result set. | ||
If a valid index is provided, `fieldScale` returns the scale of the indicated column. | ||
**Syntax:** | ||
fieldScale(int Index) | ||
fieldScale(index) | ||
**Parameters:** | ||
- **Index:** is the column number in a result set, ordered sequentially left to right, starting at 0. | ||
- **index:** `number(integer)` the column number in a result set, ordered sequentially left to right, starting at 0. | ||
**Returns:** It returns an integer value indicating the scale of the specified column in the result set. | ||
**Returns:** | ||
`number(integer)` indicating the scale of the specified column in the result set. | ||
**DB2 CLI API:** SQLColAttribute | ||
**Valid Scope:** When the result set is available. | ||
___ | ||
## stmtError | ||
## dbstmt.stmtError(callback) | ||
**Description:** | ||
@@ -1077,7 +1371,7 @@ | ||
stmtError(int hType, int Recno, function Callback(ErrMsg)) | ||
stmtError(hType, recno, callback) | ||
**Parameters** | ||
- **hType:** indicates the handler type of diagnostic information. It can be following values: | ||
- **hType:** `number(integer)` indicates the handler type of diagnostic information. It can be following values: | ||
@@ -1090,5 +1384,6 @@ - `SQL_HANDLE_ENV`: Retrieve the environment diagnostic information | ||
- **Recno:** indicates which error should be retrieved. The first error record is number 1. | ||
- **recno:** `number(integer)` indicates which error should be retrieved. The first error record is number 1. | ||
- **Callback(ErrMsg):** is a callback function to process the retrieved error message. ErrMsg is the retrieved error message. The information consists of a standardized SQLSTATE, the error code, and a text message. | ||
- **callback(errmsg):** is a callback function to process the retrieved error message. | ||
- **errmsg:** `string` consists of a standardized SQLSTATE, the error code, and a text message. | ||
@@ -1099,3 +1394,3 @@ **DB2 CLI API:** SQLGetDiagRec | ||
### ***Diagnostics*** | ||
***Diagnostics*** | ||
@@ -1102,0 +1397,0 @@ |
{ | ||
"name": "idb-connector", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "A Node.js DB2 driver for IBM i", | ||
@@ -5,0 +5,0 @@ "main": "lib/db2a.js", |
# Node.js iDB Connector | ||
The Node.js iDB Connector is a Node.js DB2 driver open source project from IBM. | ||
# Installation | ||
npm i idb-connector | ||
#### The Node.js iDB Connector is a Node.js DB2 driver open source project from IBM. | ||
[![NPM](https://nodei.co/npm/idb-connector.png?downloads=true&downloadRank=true)](https://nodei.co/npm/idb-connector/) | ||
# Installation | ||
```sh | ||
npm i idb-connector | ||
``` | ||
Then you can _require_ in your code, as shown below. | ||
@@ -14,4 +18,4 @@ | ||
``` | ||
# Quick Example | ||
@@ -21,13 +25,13 @@ | ||
```js | ||
const db = require('idb-connector'); | ||
const {dbconn, dbstmt} = require('idb-connector'); | ||
const sSql = 'SELECT STATE FROM QIWS.QCUSTCDT'; | ||
const dbconn = new db.dbconn(); | ||
dbconn.conn("*LOCAL"); | ||
const stmt = new db.dbstmt(dbconn); | ||
const connection = new dbconn(); | ||
connection.conn("*LOCAL"); | ||
const statement = new dbstmt(connection); | ||
stmt.exec(sSql, (x) => { | ||
statement.exec(sSql, (x) => { | ||
console.log("%s", JSON.stringify(x)); | ||
stmt.close(); | ||
dbconn.disconn(); | ||
dbconn.close(); | ||
statement.close(); | ||
connection.disconn(); | ||
connection.close(); | ||
}); | ||
@@ -38,13 +42,13 @@ ``` | ||
```js | ||
const db = require('idb-connector'); | ||
const {dbconn, dbstmt} = require('idb-connector'); | ||
const sSql = 'SELECT STATE FROM QIWS.QCUSTCDT'; | ||
const dbconn = new db.dbconn(); | ||
dbconn.conn("*LOCAL"); | ||
const stmt = new db.dbstmt(dbconn); | ||
const connection = new dbconn(); | ||
connection.conn("*LOCAL"); | ||
const statement = new dbstmt(connection); | ||
stmt.prepare(sSql, () => { | ||
stmt.execute(() => { | ||
stmt.fetchAll((x) => { | ||
statement.prepare(sSql, () => { | ||
statement.execute(() => { | ||
statement.fetchAll((x) => { | ||
console.log("%s", JSON.stringify(x)); | ||
stmt.close(); | ||
statement.close(); | ||
}); | ||
@@ -57,7 +61,8 @@ }); | ||
```js | ||
const db = require('idb-connector'); | ||
const idb = require('idb-connector'), | ||
{dbconn, dbstmt, IN, OUT, CHAR, CLOB} = idb; | ||
const sql = "CALL QXMLSERV.iPLUG512K(?,?,?,?)"; | ||
const dbconn = new db.dbconn(); | ||
dbconn.conn("*LOCAL"); | ||
const stmt = new db.dbstmt(dbconn); | ||
const connection = new dbconn(); | ||
connection.conn("*LOCAL"); | ||
const statement = new dbstmt(connection); | ||
@@ -69,15 +74,15 @@ const ipc = "*NA"; | ||
stmt.prepare(sql, () => { | ||
stmt.bindParam([ | ||
[ipc, db.SQL_PARAM_INPUT, db.BIND_STRING], | ||
[ctl, db.SQL_PARAM_INPUT, db.BIND_STRING], | ||
[xmlIn, db.SQL_PARAM_INPUT, db.BIND_CLOB], | ||
[xmlOut, db.SQL_PARAM_OUTPUT, db.BIND_CLOB] | ||
statement.prepare(sql, () => { | ||
statement.bindParam([ | ||
[ipc, IN, CHAR], | ||
[ctl, IN, CHAR], | ||
[xmlIn, IN, CLOB], | ||
[xmlOut, OUT, CLOB] | ||
], () => { | ||
stmt.execute((out) => { // 'out' is an array of output params | ||
statement.execute((out) => { // 'out' is an array of output params | ||
for(let i = 0; i < out.length; i++) | ||
console.log(out[i]); | ||
stmt.close(); | ||
dbconn.disconn(); | ||
dbconn.close(); | ||
statement.close(); | ||
connection.disconn(); | ||
connection.close(); | ||
}); | ||
@@ -89,4 +94,7 @@ }); | ||
# API Reference | ||
[DeveloperWorks](https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/IBM%20i%20Technology%20Updates/page/DB2%20for%20i%20Access%20APIs%20-%20New) | ||
[DB2 for i Access APIs](https://bitbucket.org/litmis/nodejs-idb-connector/src/master/docs/README.md) | ||
# Change Log | ||
View [`CHANGELOG.md`](https://bitbucket.org/litmis/nodejs-idb-connector/src/master/CHANGELOG.md) file. | ||
# Build | ||
@@ -93,0 +101,0 @@ Note that building isn't necessary for end users and is more for developers looking to compile the native Node.js extensions (C code). |
@@ -1,23 +0,18 @@ | ||
const assert = require('chai').assert; | ||
const expect = require('chai').expect; | ||
const addon = require('bindings')('db2ia'); | ||
const db2a = require('../lib/db2a'); | ||
const util = require('util'); | ||
const fs = require('fs'); | ||
const {OUT, IN, CHAR, CLOB, NUMERIC, dbstmt, dbconn} = db2a; | ||
//Test Statement Class Async Methods | ||
describe('Statement Async Test', () => { | ||
describe('prepare', () => { | ||
describe('async prepare', () => { | ||
it('Prepares valid SQL and sends it to the DBMS, if fail, error is returned. ', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepare(sql, (error) =>{ | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
dbStmt.prepare(sql, (error) => { | ||
if (error) { | ||
throw error; | ||
@@ -31,75 +26,63 @@ } | ||
describe('bindParams', () => { | ||
describe('async bindParams', () => { | ||
it('associate parameter markers in an SQL statement to app variables', (done) => { | ||
let sql = 'INSERT INTO QIWS.QCUSTCDT(CUSNUM,LSTNAM,INIT,STREET,CITY,STATE,ZIPCOD,CDTLMT,CHGCOD,BALDUE,CDTDUE) VALUES (?,?,?,?,?,?,?,?,?,?,?) with NONE ', | ||
dbConn = new addon.dbconn(), | ||
dbConn2 = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbConn2 = new dbconn(), | ||
dbStmt, | ||
dbStmt2; | ||
// dbConn.debug(true); | ||
// dbConn2.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbConn2.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn), | ||
dbStmt2 = new addon.dbstmt(dbConn2); | ||
dbStmt = new dbstmt(dbConn), | ||
dbStmt2 = new dbstmt(dbConn2); | ||
let params = [ | ||
[9997, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //CUSNUM | ||
['Doe', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //LASTNAME | ||
['J D', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //INITIAL | ||
['123 Broadway', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //ADDRESS | ||
['Hope', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //CITY | ||
['WA', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //STATE | ||
[98101, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //ZIP | ||
[2000, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //CREDIT LIMIT | ||
[1, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], // change | ||
[250, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //BAL DUE | ||
[0.00, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC] //CREDIT DUE | ||
[9997, IN, NUMERIC], //CUSNUM | ||
['Doe', IN, CHAR], //LASTNAME | ||
['J D', IN, CHAR], //INITIAL | ||
['123 Broadway', IN, CHAR], //ADDRESS | ||
['Hope', IN, CHAR], //CITY | ||
['WA', IN, CHAR], //STATE | ||
[98101, IN, NUMERIC], //ZIP | ||
[2000, IN, NUMERIC], //CREDIT LIMIT | ||
[1, IN, NUMERIC], // change | ||
[250, IN, NUMERIC], //BAL DUE | ||
[0.00, IN, NUMERIC] //CREDIT DUE | ||
]; | ||
dbStmt.prepare('SELECT * FROM QIWS.QCUSTCDT', ()=> { | ||
dbStmt.execute( ()=> { | ||
dbStmt.fetchAll((result)=>{ | ||
let rowsBefore = result.length; | ||
dbStmt.close(); | ||
dbStmt.exec('SELECT COUNT(CUSNUM) FROM QIWS.QCUSTCDT', (result, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
let rowsBefore = result[0]['00001']; | ||
rowsBefore = Number(rowsBefore); | ||
dbStmt.close(); | ||
dbStmt2.prepare(sql, (error) => { | ||
if (error){ | ||
dbStmt2.prepare(sql, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
dbStmt2.bindParam(params, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
dbStmt2.execute((out, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
dbStmt2.bindParam(params, (error) =>{ | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
expect(error).to.be.null; | ||
dbStmt2.close(); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.exec('SELECT COUNT(CUSNUM) FROM QIWS.QCUSTCDT', (result, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
dbStmt2.execute( (out, error)=>{ | ||
if (error){ | ||
throw error; | ||
} | ||
let change = dbStmt2.numRows(); | ||
dbStmt = new addon.dbstmt(dbConn), | ||
dbStmt.prepare('SELECT * FROM QIWS.QCUSTCDT', (error)=> { | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.execute( (out, error)=> { | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.fetchAll((result, error)=>{ | ||
if (error){ | ||
throw error; | ||
} | ||
let rowsFinal = result.length; | ||
expect(rowsFinal).to.equal(rowsBefore + 1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
let rowsAfter = result[0]['00001']; | ||
rowsAfter = Number(rowsAfter); | ||
dbStmt.close(); | ||
expect(rowsAfter).to.equal(rowsBefore + 1); | ||
done(); | ||
}); | ||
@@ -111,121 +94,72 @@ }); | ||
}); | ||
}); | ||
describe('execute', () => { | ||
before( () => { | ||
let user = (process.env.USER).toUpperCase(), | ||
sql = `CREATE SCHEMA ${user}`, | ||
sql2 = `CREATE OR REPLACE PROCEDURE ${user}.BALMAX(OUT OUTPUT NUMERIC( 6,2 )) | ||
LANGUAGE SQL | ||
BEGIN | ||
DECLARE MAXBAL NUMERIC ( 6 , 2 ); | ||
SELECT MAX ( BALDUE ) INTO MAXBAL FROM QIWS . QCUSTCDT; | ||
SET OUTPUT = MAXBAL; | ||
END`, | ||
dbConn = new addon.dbconn(), | ||
dbStmt; | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
describe('async execute', () => { | ||
it('retrieves output params from stored proc', (done) => { | ||
let sql = 'call QXMLSERV.iPLUG512K(?,?,?,?)', | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
dbStmt.exec(sql, (result, error) => { | ||
//if Schema already exsists will error but ignore | ||
dbStmt.closeCursor(); | ||
dbStmt.exec(sql2, (result, error) => { | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
throw error; | ||
} | ||
}); | ||
}); | ||
}); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new dbstmt(dbConn); | ||
it('runs SQLExecute and retrieves output params from SP if available', (done) => { | ||
//SP which returns as an outparam the highest bal from QIWS.QCUSTCDT | ||
let user = (process.env.USER).toUpperCase(), | ||
sql = `CALL ${user}.BALMAX(?)`, | ||
dbConn = new addon.dbconn(), | ||
dbStmt, | ||
bal = 0; | ||
let ipc = '*NA', | ||
ctl = '*here', | ||
xmlIn = '<xmlservice><sh>system "wrksbs"<\/sh><\/xmlservice>', | ||
xmlOut = '', | ||
params = [ | ||
[ipc, IN, CHAR], | ||
[ctl, IN, CHAR], | ||
[xmlIn, IN, CLOB], | ||
[xmlOut, OUT, CLOB], | ||
]; | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt.prepare(sql, (error)=>{ | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.bindParam([[bal, db2a.SQL_PARAM_OUT, db2a.SQL_NUMERIC]], (error)=>{ | ||
if (error){ | ||
dbStmt.prepare(sql, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
dbStmt.execute( (result, error) =>{ | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
dbStmt.bindParam(params, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
expect(result).to.be.a('array'); | ||
expect(result.length).to.be.greaterThan(0); | ||
done(); | ||
dbStmt.execute((out, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
expect(out).to.be.a('array'); | ||
expect(out.length).to.be.eq(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('execute xmlservice call', () => { | ||
it('tests xml service SP', (done) => { | ||
let sql = 'call QXMLSERV.iPLUG512K(?,?,?,?)', | ||
dbConn = new addon.dbconn(), | ||
dbStmt, | ||
bal = 0; | ||
it('executes prepared statement returns null because no output params are available', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT WHERE BALDUE > ?', | ||
dbConn = new dbconn(), | ||
dbStmt, | ||
params = [ | ||
[10.00, IN, NUMERIC], | ||
]; | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new dbstmt(dbConn); | ||
let ipc = '*NA', | ||
ctl = '*here', | ||
xmlIn = '<xmlservice><sh>system "wrksbs"<\/sh><\/xmlservice>', | ||
xmlOut = ''; | ||
// console.log(xmlIn); | ||
let params = [ | ||
[ipc, db2a.SQL_PARAM_INPUT, 1], | ||
[ctl, db2a.SQL_PARAM_INPUT, 1], | ||
[xmlIn, db2a.SQL_PARAM_INPUT, 0], | ||
[xmlOut, db2a.SQL_PARAM_OUTPUT, 0], | ||
]; | ||
dbStmt.prepare(sql, (error)=>{ | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.bindParam(params, (error)=>{ | ||
if (error){ | ||
dbStmt.prepare(sql, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
dbStmt.execute( (out, error) =>{ | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
dbStmt.bindParam(params, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
// for (let i = 0; i < out.length; i++) { | ||
// console.log(out[out.length - 1]); | ||
// console.log('==========================================='); | ||
// console.log('\n\n\n'); | ||
// } | ||
// console.log(util.inspect(out)); | ||
dbStmt.close(); | ||
dbConn.disconn(); | ||
dbConn.close(); | ||
expect(error).to.be.null; | ||
// console.log(`ExecuteAsync results:\n ${JSON.stringify(out)}`); | ||
expect(out).to.be.a('array'); | ||
expect(out.length).to.be.greaterThan(0); | ||
done(); | ||
dbStmt.execute((out, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
expect(out).to.be.null; | ||
done(); | ||
}); | ||
}); | ||
@@ -235,52 +169,48 @@ }); | ||
}); | ||
}); | ||
describe('exec', () => { | ||
it('performs action of given SQL String', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
describe('async exec', () => { | ||
it('performs action of given SQL String', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new dbconn(); | ||
dbStmt.exec(sql, (result, error) => { | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
// console.log(`Exec Async results:\n ${JSON.stringify(result)}\n`); | ||
expect(result).to.be.an('array'); | ||
expect(result.length).to.be.greaterThan(0); | ||
done(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new dbstmt(dbConn); | ||
dbStmt.exec(sql, (result, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
expect(result).to.be.an('array'); | ||
expect(result.length).to.be.greaterThan(0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('fetchAll', () => { | ||
it('retrieves all rows from execute function:', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
describe('async fetchAll', () => { | ||
it('retrieves all rows from execute function:', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepare(sql, (error) =>{ | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.execute( (out, error) =>{ | ||
if (error){ | ||
dbStmt.prepare(sql, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
dbStmt.fetchAll( (result, error) =>{ | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
dbStmt.execute((out, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
// console.log(`FetchAll Async results:\n ${JSON.stringify(result)}\n`); | ||
expect(result).to.be.a('array'); | ||
expect(result.length).to.be.greaterThan(0); | ||
done(); | ||
dbStmt.fetchAll((result, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
expect(result).to.be.a('array'); | ||
expect(result.length).to.be.greaterThan(0); | ||
done(); | ||
}); | ||
}); | ||
@@ -290,28 +220,28 @@ }); | ||
}); | ||
}); | ||
describe('fetch', () => { | ||
it('retrieves one row from execute function:', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
describe('async fetch', () => { | ||
it('retrieves one row from result set:', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepare(sql, (error)=>{ | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.execute( (out, error)=>{ | ||
if (error){ | ||
dbStmt.prepare(sql, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
dbStmt.fetch( (row, returnCode) =>{ | ||
if (returnCode !== 0){ //SQL_SUCCESS | ||
throw new Error('Rreturn Code was Not SQL SUCESS'); | ||
dbStmt.execute((out, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(returnCode).to.equal(0); | ||
expect(row).to.be.a('object'); | ||
done(); | ||
dbStmt.fetch((row, returnCode) => { | ||
if (returnCode !== 0) { //SQL_SUCCESS | ||
throw new Error('Rreturn Code was Not SQL SUCESS'); | ||
} | ||
expect(returnCode).to.equal(0); | ||
expect(row).to.be.a('object'); | ||
done(); | ||
}); | ||
}); | ||
@@ -322,2 +252,2 @@ }); | ||
}); | ||
}); | ||
}); |
@@ -1,23 +0,22 @@ | ||
const assert = require('chai').assert; | ||
const expect = require('chai').expect; | ||
const addon = require('bindings')('db2ia'); | ||
const db2a = require('../lib/db2a'); | ||
const {dbconn} = db2a; | ||
// Test connection Class | ||
describe('Connection Test', () => { | ||
//if successful returns undefined | ||
describe('conn & disconn & isConnected', () => { | ||
it('disconnects an exsisting connection to the datbase.', () => { | ||
let connection = new addon.dbconn(), | ||
let connection = new dbconn(), | ||
result = connection.conn("*LOCAL"); | ||
result = connection.isConnected(); | ||
expect(result).to.be.true; | ||
result = connection.disconn(); | ||
expect(result).to.be.true; | ||
result = connection.isConnected(); | ||
expect(result).to.be.false; | ||
// Test the callback style | ||
@@ -27,6 +26,6 @@ connection.conn("*LOCAL", () => { | ||
expect(result).to.be.true; | ||
result = connection.disconn(); | ||
expect(result).to.be.true; | ||
result = connection.isConnected(); | ||
@@ -42,3 +41,3 @@ expect(result).to.be.false; | ||
let attr = db2a.SQL_ATTR_AUTOCOMMIT, | ||
connection = new addon.dbconn(), | ||
connection = new dbconn(), | ||
result = connection.getConnAttr(attr); | ||
@@ -48,10 +47,10 @@ | ||
}); | ||
it('getConnAttr(SQL_ATTR_DBC_DEFAULT_LIB) should return type String', () => { | ||
let attr = db2a.SQL_ATTR_DBC_DEFAULT_LIB, | ||
connection = new addon.dbconn(), | ||
connection = new dbconn(), | ||
result = connection.getConnAttr(attr); | ||
expect(result).to.be.a('string'); | ||
}); | ||
}); | ||
}); | ||
@@ -65,22 +64,22 @@ | ||
value = db2a.SQL_TRUE, | ||
connection = new addon.dbconn(), | ||
connection = new dbconn(), | ||
result = connection.setConnAttr(attr, value); | ||
expect(result).to.be.true; | ||
result = connection.getConnAttr(attr); | ||
expect(result).to.equal(db2a.SQL_TRUE); | ||
value = db2a.SQL_FALSE; | ||
result = connection.setConnAttr(attr, value); | ||
expect(result).to.be.true; | ||
result = connection.getConnAttr(attr); | ||
expect(result).to.equal(db2a.SQL_FALSE); | ||
}); | ||
it('setConnAttr(SQL_ATTR_INFO_APPLNAME, "NODEJSTEST") should return true', () => { | ||
let attr = db2a.SQL_ATTR_INFO_APPLNAME, | ||
value = "NODEJSTEST", | ||
connection = new addon.dbconn(), | ||
connection = new dbconn(), | ||
result = connection.setConnAttr(attr, value); | ||
@@ -96,14 +95,13 @@ | ||
let choice = true, | ||
connection = new addon.dbconn(), | ||
connection = new dbconn(), | ||
result = connection.debug(choice); | ||
expect(result).to.equal(choice); | ||
expect(result).to.equal(choice); | ||
choice = !choice; | ||
result = connection.debug(choice); | ||
expect(result).to.equal(choice); | ||
expect(result).to.equal(choice); | ||
}); | ||
}); | ||
//if successful returns String | ||
@@ -113,3 +111,3 @@ describe('validStmt', () => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
connection = new addon.dbconn(), | ||
connection = new dbconn(), | ||
result = connection.validStmt(sql); | ||
@@ -119,11 +117,11 @@ | ||
}); | ||
it('if the SQL is invalid, validStmt() should return null', () => { | ||
try{ | ||
try { | ||
let sql = 'SELECT * FORM QIWS.QCUSTCDT', | ||
connection = new addon.dbconn(), | ||
connection = new dbconn(), | ||
result = connection.validStmt(sql); | ||
expect(result).to.equal(null); | ||
} catch(e) { console.error(e); } | ||
} catch (e) { console.error(e); } | ||
}); | ||
@@ -135,3 +133,3 @@ }); | ||
it('frees the connection object. ', () => { | ||
let connection = new addon.dbconn(), | ||
let connection = new dbconn(), | ||
result = connection.close(); | ||
@@ -138,0 +136,0 @@ |
@@ -1,9 +0,7 @@ | ||
const assert = require('chai').assert; | ||
const expect = require('chai').expect; | ||
const addon = require('bindings')('db2ia'); | ||
const db2a = require('../lib/db2a'); | ||
const util = require('util'); | ||
const fs = require('fs'); | ||
const {BLOB, BINARY, IN, dbstmt, dbconn} = db2a; | ||
//Test Statement Class Async Methods | ||
describe('Data Type Test', () => { | ||
@@ -13,7 +11,7 @@ describe('select number types', () => { | ||
let sql = 'select * from (values smallint( -32768 )) as x (smallint_val)', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -28,10 +26,11 @@ dbStmt.exec(sql, (result, error) => { | ||
}); | ||
it('int', (done) => { | ||
let sql = 'select * from (values int( -2147483648 )) as x (int_val)', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -46,10 +45,11 @@ dbStmt.exec(sql, (result, error) => { | ||
}); | ||
it('bigint', (done) => { | ||
let sql = 'select * from (values bigint( -9223372036854775808 )) as x (bigint_val)', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -64,10 +64,11 @@ dbStmt.exec(sql, (result, error) => { | ||
}); | ||
/* Currently Does not pass real type not supported yet | ||
it('real', (done) => { | ||
let sql = 'select * from (values real( -12345.54321 )) as x (real_val)', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -82,5 +83,6 @@ dbStmt.exec(sql, (result, error) => { | ||
}); | ||
*/ | ||
}); | ||
describe('bind parameters blob/binary/varbinary', () => { | ||
@@ -93,9 +95,8 @@ before( () => { | ||
sql4 = `CREATE OR REPLACE TABLE ${user}.VARBINARYTEST(VARBINARY_COLUMN VARBINARY(3000))`, | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.exec(sql, (result, error) => { | ||
@@ -114,2 +115,3 @@ //if Schema already exsists will error but ignore | ||
it('runs SQLExecute and to bind blob', (done) => { | ||
@@ -120,3 +122,3 @@ | ||
sql = `INSERT INTO ${user}.BLOBTEST(BLOB_COLUMN) VALUES(?)`, | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
@@ -128,7 +130,5 @@ | ||
} | ||
// console.log(util.inspect(buffer)); | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
@@ -139,3 +139,3 @@ dbStmt.prepare(sql, (error)=>{ | ||
} | ||
dbStmt.bindParam([[buffer, db2a.SQL_PARAM_INPUT, db2a.SQL_BLOB]], (error)=>{ | ||
dbStmt.bindParam([[buffer, IN, BLOB]], (error)=>{ | ||
if (error){ | ||
@@ -156,9 +156,10 @@ throw error; | ||
}); | ||
it('runs SQLExecute and to bind binary', (done) => { | ||
let user = (process.env.USER).toUpperCase(), | ||
// Table which only contains one BLOB(10) Field | ||
sql = `INSERT INTO ${user}.BINARYTEST(BINARY_COLUMN) VALUES(?)`, | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
@@ -170,6 +171,5 @@ | ||
} | ||
// console.log(util.inspect(buffer)); | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
@@ -180,3 +180,3 @@ dbStmt.prepare(sql, (error)=>{ | ||
} | ||
dbStmt.bindParam([[buffer, db2a.SQL_PARAM_INPUT, db2a.SQL_BINARY]], (error)=>{ | ||
dbStmt.bindParam([[buffer, IN, BINARY]], (error)=>{ | ||
if (error){ | ||
@@ -187,3 +187,2 @@ throw error; | ||
if (error){ | ||
// console.log(util.inspect(error)); | ||
throw error; | ||
@@ -198,3 +197,4 @@ } | ||
}); | ||
it('runs SQLExecute and to bind varbinary', (done) => { | ||
@@ -204,3 +204,3 @@ let user = (process.env.USER).toUpperCase(), | ||
sql = `INSERT INTO ${user}.VARBINARYTEST(VARBINARY_COLUMN) VALUES(?)`, | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
@@ -212,5 +212,4 @@ | ||
} | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
@@ -221,3 +220,3 @@ dbStmt.prepare(sql, (error)=>{ | ||
} | ||
dbStmt.bindParam([[buffer, db2a.SQL_PARAM_INPUT, db2a.SQL_BINARY]], (error)=>{ | ||
dbStmt.bindParam([[buffer, IN, BLOB]], (error)=>{ | ||
if (error){ | ||
@@ -239,11 +238,12 @@ throw error; | ||
}); | ||
describe('exec read blob test', () => { | ||
it('performs action of given SQL String', (done) => { | ||
let sql = 'SELECT CAST(\'test\' AS BLOB(10k)) FROM SYSIBM.SYSDUMMY1', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -264,11 +264,11 @@ dbStmt.exec(sql, (result, error) => { | ||
describe('exec read binary test', () => { | ||
it('performs action of given SQL String', (done) => { | ||
let sql = 'SELECT CAST(\'test\' AS BINARY(10)) FROM SYSIBM.SYSDUMMY1', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -289,11 +289,11 @@ dbStmt.exec(sql, (result, error) => { | ||
describe('exec read varbinary test', () => { | ||
it('performs action of given SQL String', (done) => { | ||
let sql = 'SELECT CAST(\'test\' AS VARBINARY(10)) FROM SYSIBM.SYSDUMMY1', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -300,0 +300,0 @@ dbStmt.exec(sql, (result, error) => { |
@@ -1,11 +0,7 @@ | ||
const assert = require('chai').assert; | ||
const expect = require('chai').expect; | ||
const addon = require('bindings')('db2ia'); | ||
const db2a = require('../lib/db2a'); | ||
const util = require('util'); | ||
const {IN, CHAR, NUMERIC, dbstmt, dbconn} = db2a; | ||
// Test Statement Misc | ||
describe('Statement Misc Test', () => { | ||
//if successful returns undefined | ||
describe('setStmtAttr & getStmtAttr', () => { | ||
@@ -15,17 +11,17 @@ it('setStmtAttr(attribute, value) then getStmtAttr(attribute) should equal value', () => { | ||
value = db2a.SQL_TRUE, | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn), | ||
let dbStmt = new dbstmt(dbConn), | ||
result = dbStmt.setStmtAttr(attr, value); | ||
expect(result).to.be.true; | ||
result = dbStmt.getStmtAttr(attr); | ||
expect(result).to.be.equal(value); | ||
value = db2a.SQL_FALSE; | ||
result = dbStmt.setStmtAttr(attr, value); | ||
expect(result).to.be.true; | ||
result = dbStmt.getStmtAttr(attr); | ||
@@ -36,24 +32,26 @@ expect(result).to.be.equal(value); | ||
// whats the passing use case for next Result? | ||
// describe('nextResult', () => { | ||
// it('Determines whether there is more information available on the statement', () => { | ||
// let sql = "SELECT * FROM QIWS.QCUSTCDT"; | ||
// let dbConn = new addon.dbconn(); | ||
// dbConn.conn("*LOCAL"); | ||
// let dbStmt = new addon.dbstmt(dbConn); | ||
// dbStmt.prepare(sql); | ||
// dbStmt.execute(); | ||
// let result = dbStmt.nextResult(); | ||
// expect(result).to.be.a('object'); | ||
// }); | ||
// }) | ||
/* | ||
TODO create pssing unit test for nextResult() | ||
//if successful returns undefined | ||
describe('nextResult', () => { | ||
it('Determines whether there is more information available on the statement', () => { | ||
let sql = "SELECT * FROM QIWS.QCUSTCDT"; | ||
let dbConn = new dbconn(); | ||
dbConn.conn("*LOCAL"); | ||
let dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepare(sql); | ||
dbStmt.execute(); | ||
let result = dbStmt.nextResult(); | ||
expect(result).to.be.a('object'); | ||
}); | ||
}) | ||
*/ | ||
describe('rollback', () => { | ||
it('Rollback all changes to the database that have been made on the connection', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -76,23 +74,22 @@ dbStmt.prepare(sql, (error) =>{ | ||
//if successful returns undefined | ||
describe('commit', () => { | ||
it('adds all changes to the database that have been made on the connection since connect time ', (done) => { | ||
let sql = 'INSERT INTO QIWS.QCUSTCDT(CUSNUM,LSTNAM,INIT,STREET,CITY,STATE,ZIPCOD,CDTLMT,CHGCOD,BALDUE,CDTDUE) VALUES (?,?,?,?,?,?,?,?,?,?,?) with NONE ', | ||
dbConn = new addon.dbconn(); | ||
// dbConn.debug(true); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
let params = [ | ||
[9997, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //CUSNUM | ||
['Johnson', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //LASTNAME | ||
['A J', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //INITIAL | ||
['453 Example', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //ADDRESS | ||
['Fort', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //CITY | ||
['TN', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //STATE | ||
[37211, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //ZIP | ||
[1000, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //CREDIT LIMIT | ||
[1, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], // change | ||
[150, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //BAL DUE | ||
[0.00, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC] //CREDIT DUE | ||
[9997, IN, NUMERIC], //CUSNUM | ||
['Johnson', IN, CHAR], //LASTNAME | ||
['A J', IN, CHAR], //INITIAL | ||
['453 Example', IN, CHAR], //ADDRESS | ||
['Fort', IN, CHAR], //CITY | ||
['TN', IN, CHAR], //STATE | ||
[37211, IN, NUMERIC], //ZIP | ||
[1000, IN, NUMERIC], //CREDIT LIMIT | ||
[1, IN, NUMERIC], // change | ||
[150, IN, NUMERIC], //BAL DUE | ||
[0.00, IN, NUMERIC] //CREDIT DUE | ||
]; | ||
@@ -106,3 +103,2 @@ | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
throw error; | ||
@@ -123,10 +119,10 @@ } | ||
//if successful returns an Int | ||
describe('numFields', () => { | ||
it('retrieves number of fields contained in result', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -142,3 +138,2 @@ dbStmt.prepare(sql, (error)=>{ | ||
let fields = dbStmt.numFields(); | ||
// console.log(`\nNumber of Fields: ${fields}`); | ||
expect(fields).to.be.a('number'); | ||
@@ -151,10 +146,10 @@ done(); | ||
//if successful returns an Int | ||
describe('numRows', () => { | ||
it('retrieves number of rows that were effected by a Querry', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -170,3 +165,2 @@ dbStmt.prepare(sql, (error)=>{ | ||
let rows = dbStmt.numRows(); | ||
// console.log(`Number of Rows: ${rows}`); | ||
expect(rows).to.be.a('number'); | ||
@@ -179,10 +173,10 @@ done(); | ||
//if successful returns an Int | ||
describe('fieldType', () => { | ||
it('requires an int index parameter, returns the data type of the indicated column', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepare(sql, (error) =>{ | ||
@@ -199,4 +193,2 @@ if (error){ | ||
// console.log(`column 1 fieldType = ${col1}`); | ||
// console.log(`column 2 fieldType = ${col2}`); | ||
expect(col1).to.be.a('number'); | ||
@@ -210,10 +202,10 @@ expect(col2).to.be.a('number'); | ||
//if successful returns an Int | ||
describe('fieldWidth', () => { | ||
it('requires an int index parameter, returns the field width of the indicated column', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepare(sql, (error) =>{ | ||
@@ -230,4 +222,2 @@ if (error){ | ||
// console.log(`column 1 fieldWidth = ${col1}`); | ||
// console.log(`column 2 fieldWidth = ${col2}`); | ||
expect(col1).to.be.a('number'); | ||
@@ -241,10 +231,10 @@ expect(col2).to.be.a('number'); | ||
//if successful returns an Int but should return boolean based on doc , UPDATE 3-6-18 added logic to return the boolean. (makeBool method in idb-p) | ||
describe('fieldNullable', () => { | ||
it('requires an int index parameter, returns t/f if the indicated column can be Null', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -262,5 +252,2 @@ dbStmt.prepare(sql, (error) =>{ | ||
// console.log(`column 1 Nullable? = ${col1}`); | ||
// console.log(`column 2 Nullable? = ${col2}`); | ||
//****Documnetation says it should return a boolean | ||
expect(col1).to.equal(false); | ||
@@ -274,10 +261,10 @@ expect(col2).to.equal(false); | ||
//if successful returns an String | ||
describe('fieldName', () => { | ||
it('requires an int index parameter,returns name of the indicated column ', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -295,4 +282,2 @@ dbStmt.prepare(sql, (error)=>{ | ||
// console.log(`column 1 Name = ${col1}`); | ||
// console.log(`column 2 Name = ${col2}`); | ||
expect(col1).to.be.a('string'); | ||
@@ -306,10 +291,10 @@ expect(col2).to.be.a('string'); | ||
//if successful returns an Int | ||
describe('fieldPrecise', () => { | ||
it('requires an int index parameter, returns the precision of the indicated column', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -327,4 +312,2 @@ dbStmt.prepare(sql, (error) => { | ||
// console.log('column 1 fieldPrecision = : ' + col1); | ||
// console.log('column 2 fieldPrecision = : ' + col2); | ||
expect(col1).to.be.a('number'); | ||
@@ -338,10 +321,10 @@ expect(col2).to.be.a('number'); | ||
//if successful returns an Int | ||
describe('fieldScale', () => { | ||
it('requires an int index parameter, returns the scale of the indicated column', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -359,4 +342,2 @@ dbStmt.prepare(sql, (error) => { | ||
// console.log(`column 1 fieldScale = ${col1}`); | ||
// console.log(`column 2 fieldScale = ${col2}`); | ||
expect(col1).to.be.a('number'); | ||
@@ -370,3 +351,3 @@ expect(col2).to.be.a('number'); | ||
//if successful returns the error message | ||
describe('stmtError', () => { | ||
@@ -376,6 +357,6 @@ it('Returns the diagnostic information ', (done) =>{ | ||
expectedError = "SQLSTATE=42704 SQLCODE=-204", | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
dbStmt.exec(sql, (out, error) => { | ||
@@ -390,10 +371,10 @@ dbStmt.stmtError(db2a.SQL_HANDLE_STMT, 1, (rs) => { | ||
//if successful returns true | ||
describe('closeCursor', () => { | ||
it('closes any cursor associated with the dbstmt object and discards any pending results. ', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -408,10 +389,10 @@ dbStmt.exec(sql, ()=>{ | ||
//if successful returns undefined | ||
describe('reset', () => { | ||
it('Reset the dbstmt object. ', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -425,12 +406,11 @@ dbStmt.exec(sql, ()=>{ | ||
}); | ||
//if successful returns true | ||
describe('close', () => { | ||
it('frees the statement object. ', (done) => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(); | ||
dbConn = new dbconn(); | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
let dbStmt = new addon.dbstmt(dbConn); | ||
let dbStmt = new dbstmt(dbConn); | ||
@@ -444,3 +424,2 @@ dbStmt.exec(sql, (result, error)=>{ | ||
}); | ||
}); | ||
}); |
@@ -1,6 +0,5 @@ | ||
const assert = require('chai').assert; | ||
const expect = require('chai').expect; | ||
const addon = require('bindings')('db2ia'); | ||
const db2a = require('../lib/db2a'); | ||
const util = require('util'); | ||
const {OUT, IN, CHAR, CLOB, NUMERIC, dbconn, dbstmt} = db2a; | ||
@@ -11,11 +10,10 @@ describe('Statement Sync Test', () => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepareSync(sql, (error) =>{ | ||
if (error){ | ||
// console.log(`Error is: ${result}`); | ||
throw error; | ||
@@ -31,7 +29,7 @@ } | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
@@ -45,80 +43,56 @@ dbStmt.prepareSync(sql); | ||
let sql = 'INSERT INTO QIWS.QCUSTCDT(CUSNUM,LSTNAM,INIT,STREET,CITY,STATE,ZIPCOD,CDTLMT,CHGCOD,BALDUE,CDTDUE) VALUES (?,?,?,?,?,?,?,?,?,?,?) with NONE ', | ||
dbConn = new addon.dbconn(), | ||
dbConn2 = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbConn2 = new dbconn(), | ||
dbStmt, | ||
dbStmt2; | ||
// dbConn.debug(true); | ||
// dbConn2.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbConn2.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn), | ||
dbStmt2 = new addon.dbstmt(dbConn2); | ||
dbStmt = new dbstmt(dbConn), | ||
dbStmt2 = new dbstmt(dbConn2); | ||
let params = [ | ||
[9997, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //CUSNUM | ||
['Doe', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //LASTNAME | ||
['J D', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //INITIAL | ||
['123 Broadway', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //ADDRESS | ||
['Hope', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //CITY | ||
['WA', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //STATE | ||
[98101, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //ZIP | ||
[2000, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //CREDIT LIMIT | ||
[1, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], // change | ||
[250, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //BAL DUE | ||
[0.00, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC] //CREDIT DUE | ||
[9997, IN, NUMERIC], //CUSNUM | ||
['Doe', IN, CHAR], //LASTNAME | ||
['J D', IN, CHAR], //INITIAL | ||
['123 Broadway', IN, CHAR], //ADDRESS | ||
['Hope', IN, CHAR], //CITY | ||
['WA', IN, CHAR], //STATE | ||
[98101, IN, NUMERIC], //ZIP | ||
[2000, IN, NUMERIC], //CREDIT LIMIT | ||
[1, IN, NUMERIC], // change | ||
[250, IN, NUMERIC], //BAL DUE | ||
[0.00, IN, NUMERIC] //CREDIT DUE | ||
]; | ||
dbStmt.prepareSync('SELECT * FROM QIWS.QCUSTCDT', (error)=> { | ||
let result = dbStmt.execSync('SELECT COUNT(CUSNUM) FROM QIWS.QCUSTCDT'), | ||
rowsBefore = result[0]['00001']; | ||
rowsBefore = Number(rowsBefore); | ||
dbStmt.close(); | ||
dbStmt2.prepareSync(sql, (error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.executeSync( (out, error) => { | ||
dbStmt2.bindParamSync(params, (error) =>{ | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.fetchAllSync((result, error) =>{ | ||
expect(error).to.be.null; | ||
dbStmt2.executeSync( (out, error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
let rowsBefore = result.length; | ||
dbStmt = new dbstmt(dbConn); | ||
let result2 = dbStmt.execSync('SELECT COUNT(CUSNUM) FROM QIWS.QCUSTCDT'), | ||
rowsAfter = result2[0]['00001']; | ||
rowsAfter = Number(rowsAfter); | ||
dbStmt.close(); | ||
dbStmt2.prepareSync(sql, (error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt2.bindParamSync(params, (error) =>{ | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
dbStmt2.executeSync( (out, error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
let change = dbStmt2.numRows(); | ||
dbStmt = new addon.dbstmt(dbConn), | ||
dbStmt.prepareSync('SELECT * FROM QIWS.QCUSTCDT', (error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.executeSync( (out, error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
dbStmt.fetchAllSync((result, error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
let rowsFinal = result.length; | ||
expect(rowsFinal).to.equal(rowsBefore + 1); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(rowsAfter).to.equal(rowsBefore + 1); | ||
}); | ||
@@ -133,47 +107,46 @@ }); | ||
let sql = 'INSERT INTO QIWS.QCUSTCDT(CUSNUM,LSTNAM,INIT,STREET,CITY,STATE,ZIPCOD,CDTLMT,CHGCOD,BALDUE,CDTDUE) VALUES (?,?,?,?,?,?,?,?,?,?,?) with NONE ', | ||
dbConn = new addon.dbconn(), | ||
dbConn2 = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbConn2 = new dbconn(), | ||
dbStmt, | ||
dbStmt2; | ||
dbStmt2, | ||
params = [ | ||
[9997,IN, NUMERIC], //CUSNUM | ||
['Doe', IN, CHAR], //LASTNAME | ||
['J D', IN, CHAR], //INITIAL | ||
['123 Broadway', IN, CHAR], //ADDRESS | ||
['Hope', IN, CHAR], //CITY | ||
['WA', IN, CHAR], //STATE | ||
[98101, IN, NUMERIC], //ZIP | ||
[2000, IN, NUMERIC], //CREDIT LIMIT | ||
[1, IN, NUMERIC], // change | ||
[250, IN, NUMERIC], //BAL DUE | ||
[0.00, IN, NUMERIC] //CREDIT DUE | ||
]; | ||
// dbConn.debug(true); | ||
// dbConn2.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbConn2.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn), | ||
dbStmt2 = new addon.dbstmt(dbConn2); | ||
dbStmt = new dbstmt(dbConn), | ||
dbStmt2 = new dbstmt(dbConn2); | ||
// first get count of current rows | ||
let result = dbStmt.execSync('SELECT COUNT(CUSNUM) FROM QIWS.QCUSTCDT'), | ||
rowsBefore = result[0]['00001']; | ||
let params = [ | ||
[9997, addon.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //CUSNUM | ||
['Doe', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //LASTNAME | ||
['J D', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //INITIAL | ||
['123 Broadway', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //ADDRESS | ||
['Hope', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //CITY | ||
['WA', db2a.SQL_PARAM_INPUT, db2a.SQL_CHAR], //STATE | ||
[98101, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //ZIP | ||
[2000, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //CREDIT LIMIT | ||
[1, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], // change | ||
[250, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC], //BAL DUE | ||
[0.00, db2a.SQL_PARAM_INPUT, db2a.SQL_NUMERIC] //CREDIT DUE | ||
]; | ||
rowsBefore = Number(rowsBefore); //count retrurns as a String cast it to Number | ||
dbStmt.prepareSync('SELECT * FROM QIWS.QCUSTCDT'); | ||
dbStmt.executeSync(); | ||
let result = dbStmt.fetchAllSync(); | ||
let rowsBefore = result.length; | ||
dbStmt.close(); | ||
// now perform insert | ||
dbStmt2.prepareSync(sql); | ||
dbStmt2.bindParamSync(params); | ||
dbStmt2.executeSync(); | ||
let change = dbStmt2.numRows(); | ||
dbStmt = new addon.dbstmt(dbConn), | ||
dbStmt.prepareSync('SELECT * FROM QIWS.QCUSTCDT'); | ||
dbStmt.executeSync(); | ||
let result2 = dbStmt.fetchAllSync(); | ||
let rowsFinal = result2.length; | ||
expect(rowsFinal).to.equal(rowsBefore + 1); | ||
dbStmt = new dbstmt(dbConn); | ||
let result2 = dbStmt.execSync('SELECT COUNT(CUSNUM) FROM QIWS.QCUSTCDT'), | ||
rowsAfter = result2[0]['00001']; | ||
rowsAfter = Number(rowsAfter); | ||
expect(rowsAfter).to.equal((rowsBefore + 1)); | ||
}); | ||
@@ -185,12 +158,10 @@ }); | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.execSync(sql, (result, error) => { | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
throw error; | ||
@@ -201,2 +172,3 @@ } | ||
expect(result.length).to.be.greaterThan(0); | ||
expect(result[0]).to.be.an('object'); | ||
}); | ||
@@ -209,8 +181,7 @@ }); | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
@@ -220,34 +191,45 @@ let result = dbStmt.execSync(sql); | ||
expect(result.length).to.be.greaterThan(0); | ||
expect(result[0]).to.be.an('object'); | ||
}); | ||
}); | ||
//if successful returns an array of length 0?. Why,even return it if size === 0? | ||
describe('execute callback', () => { | ||
before( () => { | ||
let user = (process.env.USER).toUpperCase(), | ||
sql = `CREATE SCHEMA ${user}`, | ||
sql2 = `CREATE OR REPLACE PROCEDURE ${user}.BALMAX(OUT OUTPUT NUMERIC( 6,2 )) | ||
LANGUAGE SQL | ||
BEGIN | ||
DECLARE MAXBAL NUMERIC ( 6 , 2 ); | ||
SELECT MAX ( BALDUE ) INTO MAXBAL FROM QIWS . QCUSTCDT; | ||
SET OUTPUT = MAXBAL; | ||
END`, | ||
dbConn = new addon.dbconn(), | ||
it('retrieves output parameters from stored proc using executeSync with a callback', () => { | ||
let sql = 'call QXMLSERV.iPLUG512K(?,?,?,?)', | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.execSync(sql, (result, error) => { | ||
let ipc = '*NA', | ||
ctl = '*here', | ||
xmlIn = '<xmlservice><sh>system "wrksbs"<\/sh><\/xmlservice>', | ||
xmlOut = '', | ||
params = [ | ||
[ipc, IN, CHAR], | ||
[ctl, IN, CHAR], | ||
[xmlIn, IN, CLOB], | ||
[xmlOut, OUT, CLOB], | ||
]; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepareSync(sql, (error)=>{ | ||
if (error){ | ||
//if Schema already exsists will error but ignore | ||
// console.log(util.inspect(error)); | ||
throw error; | ||
} | ||
dbStmt.execSync(sql2, (result, error) => { | ||
dbStmt.bindParamSync(params, (error) => { | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
throw error; | ||
} | ||
dbStmt.executeSync( (result, error) => { | ||
if (error){ | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
expect(result).to.be.a('array'); | ||
expect(result.length).to.be.eq(1); | ||
}); | ||
}); | ||
@@ -257,28 +239,28 @@ }); | ||
it('retrieves results from execute function:', () => { | ||
let user = (process.env.USER).toUpperCase(), | ||
sql = `CALL ${user}.BALMAX(?)`, | ||
dbConn = new addon.dbconn(), | ||
it('executes prepared statement using executeSync with callback. Returns null because no output params are available', () => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT WHERE BALDUE > ?', | ||
dbConn = new dbconn(), | ||
dbStmt, | ||
bal = 0; | ||
params = [ | ||
[10.00, IN, NUMERIC], | ||
]; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepareSync(sql, (error)=>{ | ||
if (error){ | ||
dbStmt.prepareSync(sql, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
dbStmt.bindParamSync([[bal, db2a.SQL_PARAM_OUT, db2a.SQL_NUMERIC]], () => { | ||
if (error){ | ||
dbStmt.bindParamSync(params, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
dbStmt.executeSync( (result, error) => { | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
dbStmt.executeSync((out, error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
expect(error).to.be.null; | ||
expect(result).to.be.a('array'); | ||
expect(result.length).to.be.greaterThan(0); | ||
expect(out).to.be.null; | ||
}); | ||
@@ -291,50 +273,47 @@ }); | ||
describe('execute no-callback', () => { | ||
before( () => { | ||
let user = (process.env.USER).toUpperCase(), | ||
sql = `CREATE SCHEMA ${user}`, | ||
sql2 = `CREATE OR REPLACE PROCEDURE ${user}.BALMAX(OUT OUTPUT NUMERIC( 6,2 )) | ||
LANGUAGE SQL | ||
BEGIN | ||
DECLARE MAXBAL NUMERIC ( 6 , 2 ); | ||
SELECT MAX ( BALDUE ) INTO MAXBAL FROM QIWS . QCUSTCDT; | ||
SET OUTPUT = MAXBAL; | ||
END`, | ||
dbConn = new addon.dbconn(), | ||
it('retrieves output parameters from stored procedure using executeSync without a callback:', () => { | ||
let sql = 'CALL QXMLSERV.iPLUG512K(?,?,?,?)', | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
// dbConn.debug(true); | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.execSync(sql, function(result, error){ | ||
if (error){ | ||
//if Schema already exsists will error but ignore | ||
// console.log(util.inspect(error)); | ||
} | ||
let ipc = '*NA', | ||
ctl = '*here', | ||
xmlIn = '<xmlservice><sh>system "wrksbs"<\/sh><\/xmlservice>', | ||
xmlOut = '', | ||
params = [ | ||
[ipc, IN, CHAR], | ||
[ctl, IN, CHAR], | ||
[xmlIn, IN, CLOB], | ||
[xmlOut, OUT, CLOB], | ||
]; | ||
dbStmt.execSync(sql2, function(result, error){ | ||
if (error){ | ||
console.log(util.inspect(error)); | ||
throw error; | ||
} | ||
}); | ||
}); | ||
dbStmt.prepareSync(sql); | ||
dbStmt.bindParamSync(params); | ||
let out = dbStmt.executeSync(); | ||
expect(out).to.be.a('array'); | ||
expect(out.length).to.be.eq(1); | ||
}); | ||
it('retrieves results from execute function:', () => { | ||
let user = (process.env.USER).toUpperCase(), | ||
sql = `CALL ${user}.BALMAX(?)`, | ||
dbConn = new addon.dbconn(), | ||
it('executes prepared statement using executeSync without callback. Returns null because no output params are available', () => { | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT WHERE BALDUE > ?', | ||
dbConn = new dbconn(), | ||
dbStmt, | ||
bal = 0; | ||
params = [ | ||
[10.00, IN, NUMERIC], | ||
]; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
dbStmt.prepareSync(sql); | ||
dbStmt.bindParamSync([[bal, db2a.SQL_PARAM_OUT, db2a.SQL_NUMERIC]]); | ||
let result = dbStmt.executeSync(); | ||
expect(result).to.be.a('array'); | ||
expect(result.length).to.be.greaterThan(0); | ||
dbStmt.bindParamSync(params); | ||
let out = dbStmt.executeSync(); | ||
expect(out).to.be.null; | ||
}); | ||
@@ -347,7 +326,7 @@ }); | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
@@ -379,7 +358,7 @@ dbStmt.prepareSync(sql, (error) =>{ | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
@@ -397,7 +376,7 @@ dbStmt.prepareSync(sql); | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
@@ -427,7 +406,7 @@ dbStmt.prepareSync(sql, (error)=>{ | ||
let sql = 'SELECT * FROM QIWS.QCUSTCDT', | ||
dbConn = new addon.dbconn(), | ||
dbConn = new dbconn(), | ||
dbStmt; | ||
dbConn.conn('*LOCAL'); | ||
dbStmt = new addon.dbstmt(dbConn); | ||
dbStmt = new dbstmt(dbConn); | ||
@@ -441,2 +420,2 @@ dbStmt.prepareSync(sql); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105
150
2145085
35423