Skip to content

Commit 7b21fcf

Browse files
committed
Move more code from C to JavaScript
1 parent 8008219 commit 7b21fcf

File tree

5 files changed

+116
-121
lines changed

5 files changed

+116
-121
lines changed

lib/connection.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,14 @@ class Connection extends EventEmitter {
104104
const cls = this._getDbObjectClass(objType.elementTypeClass);
105105
objType.elementTypeClass = cls;
106106
}
107+
nodbUtil.addTypeProperties(objType, "elementType");
107108
if (objType.attributes) {
108109
const props = {};
109110
for (const attr of objType.attributes) {
110111
if (attr.typeClass) {
111112
attr.typeClass = this._getDbObjectClass(attr.typeClass);
112113
}
114+
nodbUtil.addTypeProperties(attr, "type");
113115
const prop = {
114116
get() {
115117
return this._getAttrValue(attr);
@@ -1225,7 +1227,13 @@ class Connection extends EventEmitter {
12251227
errors.assertArgCount(arguments, 1, 1);
12261228
errors.assertParamValue(typeof sql === 'string', 1);
12271229
errors.assert(this._impl, errors.ERR_INVALID_CONNECTION);
1228-
return (await this._impl.getStatementInfo(sql));
1230+
const info = await this._impl.getStatementInfo(sql);
1231+
if (info.metaData) {
1232+
for (let i = 0; i < info.metaData.length; i++) {
1233+
nodbUtil.addTypeProperties(info.metaData[i], "dbType");
1234+
}
1235+
}
1236+
return info;
12291237
}
12301238

12311239
//---------------------------------------------------------------------------

lib/constants.js

+84-26
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ const VERSION_STRING =
3939
version.VERSION_PATCH +
4040
version.VERSION_SUFFIX;
4141

42+
// database types
43+
const DB_TYPE_BFILE = 2020;
44+
const DB_TYPE_BINARY_DOUBLE = 2008;
45+
const DB_TYPE_BINARY_FLOAT = 2007;
46+
const DB_TYPE_BINARY_INTEGER = 2009;
47+
const DB_TYPE_BLOB = 2019;
48+
const DB_TYPE_BOOLEAN = 2022;
49+
const DB_TYPE_CHAR = 2003;
50+
const DB_TYPE_CLOB = 2017;
51+
const DB_TYPE_CURSOR = 2021;
52+
const DB_TYPE_DATE = 2011;
53+
const DB_TYPE_INTERVAL_DS = 2015;
54+
const DB_TYPE_INTERVAL_YM = 2016;
55+
const DB_TYPE_JSON = 2027;
56+
const DB_TYPE_LONG = 2024;
57+
const DB_TYPE_LONG_RAW = 2025;
58+
const DB_TYPE_NCHAR = 2004;
59+
const DB_TYPE_NCLOB = 2018;
60+
const DB_TYPE_NUMBER = 2010;
61+
const DB_TYPE_NVARCHAR = 2002;
62+
const DB_TYPE_OBJECT = 2023;
63+
const DB_TYPE_RAW = 2006;
64+
const DB_TYPE_ROWID = 2005;
65+
const DB_TYPE_TIMESTAMP = 2012;
66+
const DB_TYPE_TIMESTAMP_LTZ = 2014;
67+
const DB_TYPE_TIMESTAMP_TZ = 2013;
68+
const DB_TYPE_VARCHAR = 2001;
69+
4270
module.exports = {
4371

4472
// version information
@@ -69,32 +97,62 @@ module.exports = {
6997
CQN_OPCODE_UPDATE: 4,
7098

7199
// database types
72-
DB_TYPE_BFILE: 2020,
73-
DB_TYPE_BINARY_DOUBLE: 2008,
74-
DB_TYPE_BINARY_FLOAT: 2007,
75-
DB_TYPE_BINARY_INTEGER: 2009,
76-
DB_TYPE_BLOB: 2019,
77-
DB_TYPE_BOOLEAN: 2022,
78-
DB_TYPE_CHAR: 2003,
79-
DB_TYPE_CLOB: 2017,
80-
DB_TYPE_CURSOR: 2021,
81-
DB_TYPE_DATE: 2011,
82-
DB_TYPE_INTERVAL_DS: 2015,
83-
DB_TYPE_INTERVAL_YM: 2016,
84-
DB_TYPE_JSON: 2027,
85-
DB_TYPE_LONG: 2024,
86-
DB_TYPE_LONG_RAW: 2025,
87-
DB_TYPE_NCHAR: 2004,
88-
DB_TYPE_NCLOB: 2018,
89-
DB_TYPE_NUMBER: 2010,
90-
DB_TYPE_NVARCHAR: 2002,
91-
DB_TYPE_OBJECT: 2023,
92-
DB_TYPE_RAW: 2006,
93-
DB_TYPE_ROWID: 2005,
94-
DB_TYPE_TIMESTAMP: 2012,
95-
DB_TYPE_TIMESTAMP_LTZ: 2014,
96-
DB_TYPE_TIMESTAMP_TZ: 2013,
97-
DB_TYPE_VARCHAR: 2001,
100+
DB_TYPE_BFILE,
101+
DB_TYPE_BINARY_DOUBLE,
102+
DB_TYPE_BINARY_FLOAT,
103+
DB_TYPE_BINARY_INTEGER,
104+
DB_TYPE_BLOB,
105+
DB_TYPE_BOOLEAN,
106+
DB_TYPE_CHAR,
107+
DB_TYPE_CLOB,
108+
DB_TYPE_CURSOR,
109+
DB_TYPE_DATE,
110+
DB_TYPE_INTERVAL_DS,
111+
DB_TYPE_INTERVAL_YM,
112+
DB_TYPE_JSON,
113+
DB_TYPE_LONG,
114+
DB_TYPE_LONG_RAW,
115+
DB_TYPE_NCHAR,
116+
DB_TYPE_NCLOB,
117+
DB_TYPE_NUMBER,
118+
DB_TYPE_NVARCHAR,
119+
DB_TYPE_OBJECT,
120+
DB_TYPE_RAW,
121+
DB_TYPE_ROWID,
122+
DB_TYPE_TIMESTAMP,
123+
DB_TYPE_TIMESTAMP_LTZ,
124+
DB_TYPE_TIMESTAMP_TZ,
125+
DB_TYPE_VARCHAR,
126+
127+
// database type names map
128+
DB_TYPE_NAMES_MAP: new Map([
129+
[DB_TYPE_BFILE, "BFILE"],
130+
[DB_TYPE_BINARY_DOUBLE, "BINARY_DOUBLE"],
131+
[DB_TYPE_BINARY_FLOAT, "BINARY_FLOAT"],
132+
[DB_TYPE_BINARY_INTEGER, "BINARY_INTEGER"],
133+
[DB_TYPE_BLOB, "BLOB"],
134+
[DB_TYPE_BOOLEAN, "BOOLEAN"],
135+
[DB_TYPE_CHAR, "CHAR"],
136+
[DB_TYPE_CLOB, "CLOB"],
137+
[DB_TYPE_CURSOR, "CURSOR"],
138+
[DB_TYPE_DATE, "DATE"],
139+
[DB_TYPE_INTERVAL_DS, "INTERVAL DAY TO SECOND"],
140+
[DB_TYPE_INTERVAL_YM, "INTERVAL YEAR TO MONTH"],
141+
[DB_TYPE_JSON, "JSON"],
142+
[DB_TYPE_LONG, "LONG"],
143+
[DB_TYPE_LONG_RAW, "LONG RAW"],
144+
[DB_TYPE_NCHAR, "NCHAR"],
145+
[DB_TYPE_NCLOB, "NCLOB"],
146+
[DB_TYPE_NUMBER, "NUMBER"],
147+
[DB_TYPE_NVARCHAR, "NVARCHAR2"],
148+
[DB_TYPE_OBJECT, "OBJECT"],
149+
[DB_TYPE_RAW, "RAW"],
150+
[DB_TYPE_ROWID, "ROWID"],
151+
[DB_TYPE_TIMESTAMP, "TIMESTAMP"],
152+
[DB_TYPE_TIMESTAMP_LTZ, "TIMESTAMP WITH LOCAL TIME ZONE"],
153+
[DB_TYPE_TIMESTAMP_TZ, "TIMESTAMP WITH TIME ZONE"],
154+
[DB_TYPE_VARCHAR, "VARCHAR2"]
155+
]),
98156

99157
// fetchInfo type defaulting
100158
DEFAULT: 0,

lib/resultset.js

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ class ResultSet {
200200
info.dbTypeClass = cls;
201201
setupData.dbObjectIndices.push(i);
202202
}
203+
nodbUtil.addTypeProperties(info, "dbType");
203204
let name = info.name;
204205
if (names.get(name) !== i) {
205206
let seqNum = 0;

lib/util.js

+22
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
'use strict';
2828

29+
const constants = require('./constants.js');
2930
const errors = require('./errors.js');
3031
const util = require('util');
3132

@@ -303,12 +304,33 @@ function isPrivateKeyValid(accessToken) {
303304
return true;
304305
}
305306

307+
//-----------------------------------------------------------------------------
308+
// addTypeProperties()
309+
//
310+
// Adds derived properties about the type as a convenience to the user.
311+
// Currently this is only the name of type, which is either the name of the
312+
// database object type (if the value refers to a database object) or the name
313+
// of the Oracle database type.
314+
// -----------------------------------------------------------------------------
315+
function addTypeProperties(obj, attrName) {
316+
const clsAttrName = attrName + "Class";
317+
const nameAttrName = attrName + "Name";
318+
const cls = obj[clsAttrName];
319+
const typeNum = obj[attrName];
320+
if (cls) {
321+
obj[nameAttrName] = cls.prototype.fqn;
322+
} else if (typeNum) {
323+
obj[nameAttrName] = constants.DB_TYPE_NAMES_MAP.get(typeNum);
324+
}
325+
}
326+
306327
// define exports
307328
module.exports = {
308329
BINARY_FILE,
309330
PACKAGE_JSON_VERSION,
310331
RELEASE_DIR,
311332
STAGING_DIR,
333+
addTypeProperties,
312334
callbackify,
313335
getInstallURL,
314336
getInstallHelp,

src/njsUtils.c

-94
Original file line numberDiff line numberDiff line change
@@ -41,108 +41,14 @@ bool njsUtils_addTypeProperties(napi_env env, napi_value obj,
4141
const char *propertyNamePrefix, uint32_t oracleTypeNum,
4242
njsDbObjectType *objType)
4343
{
44-
size_t typeNameLength = NAPI_AUTO_LENGTH;
4544
char propertyName[100];
46-
const char *typeName;
4745
napi_value temp;
4846

49-
switch (oracleTypeNum) {
50-
case DPI_ORACLE_TYPE_VARCHAR:
51-
typeName = "VARCHAR2";
52-
break;
53-
case DPI_ORACLE_TYPE_NVARCHAR:
54-
typeName = "NVARCHAR2";
55-
break;
56-
case DPI_ORACLE_TYPE_CHAR:
57-
typeName = "CHAR";
58-
break;
59-
case DPI_ORACLE_TYPE_NCHAR:
60-
typeName = "NCHAR";
61-
break;
62-
case DPI_ORACLE_TYPE_ROWID:
63-
typeName = "ROWID";
64-
break;
65-
case DPI_ORACLE_TYPE_RAW:
66-
typeName = "RAW";
67-
break;
68-
case DPI_ORACLE_TYPE_NATIVE_FLOAT:
69-
typeName = "BINARY_FLOAT";
70-
break;
71-
case DPI_ORACLE_TYPE_NATIVE_DOUBLE:
72-
typeName = "BINARY_DOUBLE";
73-
break;
74-
case DPI_ORACLE_TYPE_NATIVE_INT:
75-
typeName = "BINARY_INTEGER";
76-
break;
77-
case DPI_ORACLE_TYPE_NUMBER:
78-
typeName = "NUMBER";
79-
break;
80-
case DPI_ORACLE_TYPE_DATE:
81-
typeName = "DATE";
82-
break;
83-
case DPI_ORACLE_TYPE_TIMESTAMP:
84-
typeName = "TIMESTAMP";
85-
break;
86-
case DPI_ORACLE_TYPE_TIMESTAMP_TZ:
87-
typeName = "TIMESTAMP WITH TIME ZONE";
88-
break;
89-
case DPI_ORACLE_TYPE_TIMESTAMP_LTZ:
90-
typeName = "TIMESTAMP WITH LOCAL TIME ZONE";
91-
break;
92-
case DPI_ORACLE_TYPE_CLOB:
93-
typeName = "CLOB";
94-
break;
95-
case DPI_ORACLE_TYPE_NCLOB:
96-
typeName = "NCLOB";
97-
break;
98-
case DPI_ORACLE_TYPE_BLOB:
99-
typeName = "BLOB";
100-
break;
101-
case DPI_ORACLE_TYPE_LONG_VARCHAR:
102-
typeName = "LONG";
103-
break;
104-
case DPI_ORACLE_TYPE_LONG_RAW:
105-
typeName = "LONG RAW";
106-
break;
107-
case DPI_ORACLE_TYPE_OBJECT:
108-
typeName = objType->fqn;
109-
typeNameLength = objType->fqnLength;
110-
break;
111-
case DPI_ORACLE_TYPE_INTERVAL_DS:
112-
typeName = "INTERVAL DAY TO SECOND";
113-
break;
114-
case DPI_ORACLE_TYPE_INTERVAL_YM:
115-
typeName = "INTERVAL YEAR TO MONTH";
116-
break;
117-
case DPI_ORACLE_TYPE_BFILE:
118-
typeName = "BFILE";
119-
break;
120-
case DPI_ORACLE_TYPE_BOOLEAN:
121-
typeName = "BOOLEAN";
122-
break;
123-
case DPI_ORACLE_TYPE_STMT:
124-
typeName = "CURSOR";
125-
break;
126-
case DPI_ORACLE_TYPE_JSON:
127-
typeName = "JSON";
128-
break;
129-
default:
130-
typeName = "UNKNOWN";
131-
break;
132-
}
133-
13447
// set the type (integer constant)
13548
NJS_CHECK_NAPI(env, napi_create_uint32(env, oracleTypeNum, &temp))
13649
NJS_CHECK_NAPI(env, napi_set_named_property(env, obj, propertyNamePrefix,
13750
temp))
13851

139-
// set the type name
140-
(void) snprintf(propertyName, sizeof(propertyName), "%sName",
141-
propertyNamePrefix);
142-
NJS_CHECK_NAPI(env, napi_create_string_utf8(env, typeName, typeNameLength,
143-
&temp))
144-
NJS_CHECK_NAPI(env, napi_set_named_property(env, obj, propertyName, temp))
145-
14652
// set the type class, if applicable
14753
if (objType) {
14854
(void) snprintf(propertyName, sizeof(propertyName), "%sClass",

0 commit comments

Comments
 (0)