Skip to content

Commit

Permalink
support for set connect attributes, #970
Browse files Browse the repository at this point in the history
  • Loading branch information
bimalkjha committed Feb 26, 2024
1 parent 3045f4f commit b7289cd
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 36 deletions.
12 changes: 10 additions & 2 deletions APIDocumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1175,8 +1175,12 @@ ibmdb.open(cn, function(err, conn){
### <a name="setAttrApi"></a> 41) .setAttr(attributeName, value, callback)
Set statement level attributes asynchronously. It requires attributeName and corresponding value.
Set connection and statement level attributes asynchronously. It requires attributeName and corresponding value.
conn.setAttr() - sets connection level attributes post connection.
stmt.setAttr() - sets statement level attributes post creation of statement handle.
```
await conn.setAttr("SQL_ATTR_INFO_USERID", 'appuser');

stmt.setAttr(ibmdb.SQL_ATTR_PARAMSET_SIZE, 4, function(err, result) {
if(err) { console.log(err); stmt.closeSync(); }
else { ... }
Expand All @@ -1185,8 +1189,12 @@ stmt.setAttr(ibmdb.SQL_ATTR_PARAMSET_SIZE, 4, function(err, result) {
### <a name="setAttrSyncApi"></a> 42) .setAttrSync(attributeName, value)
Set statement level attributes synchronously. It requires attributeName and corresponding value.
Set connection and statement level attributes synchronously. It requires attributeName and corresponding value.
conn.setAttrSync() - sets connection level attributes post connection.
stmt.setAttrSync() - sets statement level attributes post creation of statement handle.
```
conn.setAttrSync(ibmdb.SQL_ATTR_INFO_APPLNAME, 'mynodeApp');

var err = stmt.setAttrSync(ibmdb.SQL_ATTR_PARAMSET_SIZE, 5);
err = stmt.setAttrSync(ibmdb.SQL_ATTR_QUERY_TIMEOUT, 50);
err = stmt.setAttrSync(3, 2); //SQL_ATTR_MAX_LENGTH = 3
Expand Down
50 changes: 50 additions & 0 deletions lib/climacros.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,33 @@
// Application can use it as ibmdb.SQL_ATTR_PARAMSET_SIZE instead of 22

module.exports.attributes = {
// Connection Attributes
SQL_ATTR_ACCESS_MODE : 101,
SQL_ATTR_AUTOCOMMIT : 102,
SQL_ATTR_LOGIN_TIMEOUT : 103,
SQL_ATTR_TRACE : 104,
SQL_ATTR_TRACEFILE : 105,
SQL_ATTR_TRANSLATE_LIB : 106,
SQL_ATTR_TRANSLATE_OPTION : 107,
SQL_ATTR_TXN_ISOLATION : 108,
SQL_ATTR_CURRENT_CATALOG : 109,
SQL_ATTR_ODBC_CURSORS : 110,
SQL_ATTR_QUIET_MODE : 111,
SQL_ATTR_PACKET_SIZE :112,
SQL_ATTR_CONNECTION_TIMEOUT : 113,
SQL_ATTR_DISCONNECT_BEHAVIOR : 114,
SQL_ATTR_ANSI_APP : 115,
SQL_ATTR_RESET_CONNECTION : 116,
SQL_ATTR_ASYNC_DBC_FUNCTIONS_ENABLE : 117,
SQL_ATTR_INFO_USERID : 1281,
SQL_ATTR_INFO_WRKSTNNAME : 1282,
SQL_ATTR_INFO_APPLNAME : 1283,
SQL_ATTR_INFO_ACCTSTR : 1284,
SQL_ATTR_ALLOW_INTERLEAVED_GETDATA : 2599,

// Statement Attributes
SQL_ATTR_QUERY_TIMEOUT : 0,
SQL_ATTR_ASYNC_ENABLE : 4,
SQL_ATTR_PARAM_BIND_TYPE : 18,
SQL_ATTR_PARAMSET_SIZE : 22,
SQL_ATTR_ROW_ARRAY_SIZE : 27,
Expand Down Expand Up @@ -369,3 +394,28 @@ module.exports.funcId = {
SQLRELOADCONFIG : 1299
}

module.exports.connAttributes = {
SQL_ATTR_ACCESS_MODE : 101,
SQL_ATTR_AUTOCOMMIT : 102,
SQL_ATTR_LOGIN_TIMEOUT : 103,
SQL_ATTR_TRACE : 104,
SQL_ATTR_TRACEFILE : 105,
SQL_ATTR_TRANSLATE_LIB : 106,
SQL_ATTR_TRANSLATE_OPTION : 107,
SQL_ATTR_TXN_ISOLATION : 108,
SQL_ATTR_CURRENT_CATALOG : 109,
SQL_ATTR_ODBC_CURSORS : 110,
SQL_ATTR_QUIET_MODE : 111,
SQL_ATTR_PACKET_SIZE :112,
SQL_ATTR_CONNECTION_TIMEOUT : 113,
SQL_ATTR_DISCONNECT_BEHAVIOR : 114,
SQL_ATTR_ANSI_APP : 115,
SQL_ATTR_RESET_CONNECTION : 116,
SQL_ATTR_ASYNC_DBC_FUNCTIONS_ENABLE : 117,
SQL_ATTR_INFO_USERID : 1281,
SQL_ATTR_INFO_WRKSTNNAME : 1282,
SQL_ATTR_INFO_APPLNAME : 1283,
SQL_ATTR_INFO_ACCTSTR : 1284,
SQL_ATTR_ALLOW_INTERLEAVED_GETDATA : 2599
}

117 changes: 85 additions & 32 deletions lib/odbc.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ module.exports.debug = function(x) {
}
};

var connError = {
message: "Connection not open.",
sqlstate: "08001",
sqlcode: -30081
};

module.exports.open = function (connStr, options, cb)
{
var db, deferred;
Expand Down Expand Up @@ -640,11 +646,8 @@ Database.prototype.query = function (query, params, cb)

if (!self.connected)
{
var err = {message : "Connection not open.",
sqlstate: "08001",
sqlcode : -30081};
sqlca = {sqlstate: "08001", sqlcode : -30081};
deferred ? deferred.reject(err) : cb(err, [], sqlca);
deferred ? deferred.reject(connError) : cb(connError, [], sqlca);
return next();
}

Expand Down Expand Up @@ -713,10 +716,7 @@ Database.prototype.queryResult = function (query, params, cb)
//ODBCConnection.query() is the fastest-path querying mechanism.
if (!self.connected)
{
var err = {message : "Connection not open.",
sqlstate: "08001",
sqlcode : -30081};
deferred ? deferred.reject(err) : cb(err);
deferred ? deferred.reject(connError) : cb(connError);
return next();
}

Expand Down Expand Up @@ -969,12 +969,7 @@ Database.prototype.executeFile = function (inputfile, delimiter, outputfile, cb)
}

if (!self.connected) {
var err = {
message: "Connection not open.",
sqlstate: "08001",
sqlcode: -30081
};
deferred ? deferred.reject(err) : cb(err);
deferred ? deferred.reject(connError) : cb(connError);
}
fs.readFile(sql, function (err, sql) {
if (err) {
Expand Down Expand Up @@ -1524,12 +1519,7 @@ Database.prototype.getInfo = function (infoType, infoLen, cb)
{
exports.debug && console.log(getElapsedTime(), "odbc.js: infoType => ", infoType);
if (!self.connected) {
var err = {
message: "Connection not open.",
sqlstate: "08001",
sqlcode: -30081
};
deferred ? deferred.reject(err) : cb(err);
deferred ? deferred.reject(connError) : cb(connError);
return next();
}

Expand Down Expand Up @@ -1601,12 +1591,7 @@ Database.prototype.getTypeInfo = function (dataType, cb)
{
exports.debug && console.log(getElapsedTime(), "odbc.js: dataType => ", dataType);
if (!self.connected) {
var err = {
message: "Connection not open.",
sqlstate: "08001",
sqlcode: -30081
};
deferred ? deferred.reject(err) : cb(err);
deferred ? deferred.reject(connError) : cb(connError);
return next();
}

Expand Down Expand Up @@ -1709,12 +1694,7 @@ Database.prototype.getFunctions = function (functionId, cb)
{
exports.debug && console.log(getElapsedTime(), "odbc.js: functionId => ", functionId);
if (!self.connected) {
var err = {
message: "Connection not open.",
sqlstate: "08001",
sqlcode: -30081
};
deferred ? deferred.reject(err) : cb(err);
deferred ? deferred.reject(connError) : cb(connError);
return next();
}

Expand Down Expand Up @@ -1761,6 +1741,79 @@ Database.prototype.getFunctions = function (functionId, cb)
if(deferred) return deferred.promise;
} //getFunctions

// Function to set connection level attributes
Database.prototype.setAttr = function (attr, value, cb)
{
// attr, value - mandatory
// cb - optional. If not cb, then return promise

var self = this, deferred;

//support for promises
if (!cb)
{
deferred = Q.defer();
}

self.queue.push(function (next)
{
exports.debug && console.log(getElapsedTime(),
"odbc.js:setAttr() Attribute = ", attr, ", Value = ", value);
if (!self.connected) {
deferred ? deferred.reject(connError) : cb(connError);
return next();
}

if (Number.isInteger(attr) === false) {
var connattr = macro.connAttributes[attr];
if (Number.isInteger(connattr) === false) {
var err = { message : "Invalid attr = " + attr};
deferred ? deferred.reject(err) : cb(err);
return next();
} else {
attr = connattr;
}
}

self.conn.setAttr(attr, value, function (err)
{
if (err)
{
self.checkConnectionError(err);
deferred ? deferred.reject(err) : cb(err);
return next();
}
deferred ? deferred.resolve(true) : cb(null, true);
return next();
});
});
if(deferred) return deferred.promise;
} //setAttr

// Async Function to set connection level attributes
Database.prototype.setAttrSync = function (attr, value)
{
// attr, value - mandatory
var self = this;

exports.debug && console.log(getElapsedTime(),
"odbc.js:setAttrSync() Attribute = ", attr, ", Value = ", value);

if (!self.connected) {
return (connError);
}
if (Number.isInteger(attr) === false) {
return { message : "Invalid Attribute."};
}

try {
return self.conn.setAttrSync(attr, value);
} catch (e) {
self.checkConnectionError(e);
return new Error(e);
}
}; // setAttrSync

//Proxy all of the ODBCStatement functions so that they are queued
if( !odbc.ODBCStatement.prototype._execute ) { //issue #514
odbc.ODBCStatement.prototype._execute = odbc.ODBCStatement.prototype.execute;
Expand Down
Loading

0 comments on commit b7289cd

Please sign in to comment.