Skip to content

Commit

Permalink
fea: add support for getTypeInfo() APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
bimalkjha committed Nov 1, 2022
1 parent fc8e3c6 commit 9622bb4
Show file tree
Hide file tree
Showing 6 changed files with 490 additions and 39 deletions.
52 changes: 50 additions & 2 deletions APIDocumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
42. [.setAttrSync(attributeName, value)](#setAttrSyncApi)
43. [.getInfo(infoType, [infoLength], callback)](#getInfoApi)
44. [.getInfoSync(infoType, [infoLength])](#getInfoSyncApi)
45. [.getTypeInfo(dataType, callback)](#getTypeInfoApi)
46. [.getTypeInfoSync(dataType)](#getTypeInfoSyncApi)

* [**Connection Pooling APIs**](#PoolAPIs)
* [**bindingParameters**](#bindParameters)
Expand Down Expand Up @@ -1195,7 +1197,6 @@ Asynchronously retrieve the general information about the database management sy
* **infoType** - The type of information that is required. The possible values for this argument are described in [Information returned by SQLGetInfo()](https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.db2.luw.apdv.cli.doc/doc/r0000615.html#r0000615__tbginfo). The value for this argument should be an integer value if macro is not defined in `ibm_db/lib/climacros.js` file.
* **infoLength** - _OPTIONAL_ - Length of the string value to be retrieved. If not provided, getInfo() can return a string value of maximum size 255 bytes.
Delimiter splits mutliple query in the sqlFile.
* **callback** - `callback (error, value)`
Expand Down Expand Up @@ -1224,7 +1225,6 @@ Synchronously retrieve the general information about the database management sys
* **infoType** - The type of information that is required. The possible values for this argument are described in [Information returned by SQLGetInfo()](https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.db2.luw.apdv.cli.doc/doc/r0000615.html#r0000615__tbginfo). The value for this argument should be an integer value if macro is not defined in `ibm_db/lib/climacros.js` file.
* **infoLength** - _OPTIONAL_ - Length of the string value to be retrieved. If not provided, getInfo() can return a string value of maximum size 255 bytes.
Delimiter splits mutliple query in the sqlFile.
```javascript
var ibmdb = require("ibm_db")
Expand All @@ -1238,6 +1238,54 @@ ibmdb.open(cn, function(err, conn)
});
```
### <a name="getTypeInfoApi"></a> 45) .getTypeInfo(dataType, callback)
Asynchronously retrieve the information about the SQL data types that are supported by the connected database server.
If `ibmdb.SQL_ALL_TYPES` is specified, information about all supported data types would be returned in ascending order by `TYPE_NAME`. All unsupported data types would be absent from the result set.
* **dataType** - The SQL data type being queried. The supported values for this argument are described in [SQLGetTypeInfo function (CLI) - Get data type information](https://www.ibm.com/docs/en/db2/11.5?topic=functions-sqlgettypeinfo-function-get-data-type-information). The value for this argument should be an integer value if macro is not defined in `ibm_db/lib/climacros.js` file.
* **callback** - `callback (error, result)`
```javascript
var ibmdb = require("ibm_db")
, cn = "DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password";

ibmdb.open(cn, function(err, conn) {
conn.getTypeInfo(ibmdb.SQL_BLOB, function(error, result) {
if(error) console.log(error);
else console.log("SQL_BLOB Data Type Info = ", result);
conn.closeSync();
});
});

async function main()
{
let conn = await ibmdb.open(cn);
let data = await conn.getTypeInfo(ibmdb.SQL_ALL_TYPES);
console.log("All supported data types info = ", data);
await conn.close();
}
```
### <a name="getTypeInfoSyncApi"></a> 46) .getTypeInfoSync(dataType)
Synchronously retrieve the information about the SQL data types that are supported by the connected database server.
If `ibmdb.SQL_ALL_TYPES` is specified, information about all supported data types would be returned in ascending order by `TYPE_NAME`. All unsupported data types would be absent from the result set.
* **dataType** - The SQL data type being queried. The supported values for this argument are described in [SQLGetTypeInfo function (CLI) - Get data type information](https://www.ibm.com/docs/en/db2/11.5?topic=functions-sqlgettypeinfo-function-get-data-type-information). The value for this argument should be an integer value if macro is not defined in `ibm_db/lib/climacros.js` file.
```javascript
var ibmdb = require("ibm_db")
, cn = "DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password";

ibmdb.open(cn, function(err, conn) {
let result = conn.getTypeInfoSync(ibmdb.SQL_BIGINT);
console.log("SQL_BIGINT Data Type Info = ", result);
conn.closeSync();
});
```
## Create and Drop Database APIs
### <a name="createDbSyncApi"></a> .createDbSync(dbName, connectionString, [options])
Expand Down
33 changes: 33 additions & 0 deletions lib/climacros.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module.exports = {
SQL_MAX_STATEMENT_LEN : 105,
SQL_DBMS_FUNCTIONLVL : 203,
SQL_DATABASE_CODEPAGE : 2519,
SQL_APPLICATION_CODEPAGE : 2520,
SQL_CONNECT_CODEPAGE : 2521,
SQL_DB2_DRIVER_VER : 2550,
SQL_DRIVER_BLDLEVEL : 2604,
SQL_XOPEN_CLI_YEAR : 10000,
Expand All @@ -64,5 +66,36 @@ module.exports = {
FETCH_OBJECT : 4,
FETCH_NODATA : 0,

// InfoType for SQLGetTypeInfo i.e. ibmdb.getTypeInfo()
// Defined in sqlcli1.h and sqlext.h
ALLTYPES : 0, //SQL_ALL_TYPES
BIGINT : -5, //SQL_BIGINT
BINARY : -2,
BIT : -7,
BLOB : -98,
BOOLEAN : 16,
CHAR : 1,
CLOB : -99,
DATE : 91, //SQL_TYPE_DATE
DBCLOB : -350,
DECIMAL : 3,
DOUBLE : 8,
FLOAT : 6,
GRAPHIC : -95,
INTEGER : 4,
LONGVARBINARY : -4,
LONGVARCHAR : -1,
LONGVARGRAPHIC : -97,
NUMERIC : 2,
REAL : 7,
SMALLINT : 5,
TIME : 92, //SQL_TYPE_TIME
TIMESTAMP : 93, //SQL_TYPE_TIMESTAMP
TINYINT : -6,
VARBINARY : -3,
VARCHAR : 12,
VARGRAPHIC : -96,
XML : -370,

}

137 changes: 112 additions & 25 deletions lib/odbc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1493,42 +1493,129 @@ Database.prototype.getInfo = function (infoType, infoLen, cb)
cb = infoLen;
infoLen = MAXINFO_LEN;
}

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

if (Number.isInteger(infoType) === false) {
var err = { message : "Invalid infoType requested."};
deferred ? deferred.reject(err) : cb(err);
}
if (Number.isInteger(infoLen) === false) {
infoLen = MAXINFO_LEN;
}
exports.debug && console.log(getElapsedTime(), "odbc.js: infoType => ", infoType);

self.conn.getInfo(infoType, infoLen, function (err, value)
self.queue.push(function (next)
{
if (err)
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);
return next();
}

if (Number.isInteger(infoType) === false) {
var err = { message : "Invalid infoType used, " + infoType};
deferred ? deferred.reject(err) : cb(err);
return next();
}

self.conn.getInfo(infoType, infoLen, function (err, value)
{
self.checkConnectionError(err);
if(cb)
{
return cb(err);
} else
if (err)
{
deferred.reject(err)
self.checkConnectionError(err);
deferred ? deferred.reject(err) : cb(err);
return next();
}
}
deferred ? deferred.resolve(value) : cb(null, value);
deferred ? deferred.resolve(value) : cb(null, value);
return next();
});
});
if(deferred) return deferred.promise;
} //getInfo

Database.prototype.getTypeInfoSync = function(dataType)
{
// infoType - mandatory

var self = this;
var stmt = 0;
var data = [];
if (!self.connected)
{
throw ({ message : "Connection not open."});
}
if (Number.isInteger(dataType) === false) {
throw { message : "Invalid dataType used, " + dataType};
}

try {
stmt = self.conn.getTypeInfoSync(dataType);
data = stmt.fetchAllSync();
stmt.closeSync();
return data;
} catch (e) {
self.checkConnectionError(e);
throw new Error(e);
}
} //getTypeInfoSync

Database.prototype.getTypeInfo = function (dataType, cb)
{
// dataType - mandatory
// cb - optional. If not cb, then return promise

var self = this, deferred;

if (typeof dataType === 'function')
{
cb = dataType;
}
//support for promises
if (!cb && typeof dataType !== 'function')
{
deferred = Q.defer();
}

self.queue.push(function (next)
{
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);
return next();
}

if (Number.isInteger(dataType) === false) {
var err = { message : "Invalid dataType." };
if (typeof dataType === 'function') {
err = { message : "Missing dataType." };
}
deferred ? deferred.reject(err) : cb(err);
return next();
}

self.conn.getTypeInfo(dataType, function (err, result)
{
if (err) {
self.checkConnectionError(err);
deferred ? deferred.reject(err) : cb(err, false);
return next();
}

result.fetchAll(function (err, data)
{
result.closeSync();
if(err) self.checkConnectionError(err);
deferred ? deferred.resolve(data) : cb(err, data);
return next();
});
});
});
if(deferred) return deferred.promise;
} //getTypeInfo

//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 9622bb4

Please sign in to comment.