Skip to content

Commit d3beb39

Browse files
committed
Add toMap API for dbObject of collection type (GH Issue #1627)
1 parent 4dd4721 commit d3beb39

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

doc/src/release_notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ node-oracledb `v6.4.0 <https://github.com/oracle/node-oracledb/compare/v6.3.0...
1313
Common Changes
1414
++++++++++++++
1515

16+
#) Added :meth:`~dbObject.toMap` method to :ref:`DbObject Class<dbobjectclass>`
17+
which returns a map object.
18+
See `Issue #1627 <https://github.com/oracle/node-oracledb/issues/1627>`__.
19+
1620
#) Accept an object as an input parameter for :meth:`connection.execute()`
1721
as per GitHub user request.
1822
See `Issue #1629 <https://github.com/oracle/node-oracledb/issues/1629>`__.

lib/dbObject.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,25 @@ class BaseDbObject {
439439
return (this._toPojo());
440440
}
441441

442+
//---------------------------------------------------------------------------
443+
// toMap()
444+
//
445+
// Returns the Map object where the collection’s indexes are the keys and
446+
// the elements are its values.
447+
//---------------------------------------------------------------------------
448+
toMap() {
449+
errors.assertArgCount(arguments, 0, 0);
450+
if (!this.isCollection) {
451+
errors.throwErr(errors.ERR_OBJECT_IS_NOT_A_COLLECTION,
452+
this.name);
453+
}
454+
const result = new Map();
455+
this.getKeys().forEach(element => {
456+
result.set(element, this.getElement(element));
457+
});
458+
return result;
459+
}
460+
442461
}
443462

444463
// method for transforming the error
@@ -472,6 +491,7 @@ wrapFns(BaseDbObject.prototype,
472491
"getNextIndex",
473492
"getPrevIndex",
474493
"getValues",
494+
"toMap",
475495
"hasElement",
476496
"setElement",
477497
"trim"

lib/errors.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ const ERR_WRONG_LENGTH_FOR_DBOBJECT_ATTR = 142;
142142
const ERR_WRONG_LENGTH_FOR_DBOBJECT_ELEM = 143;
143143
const ERR_VECTOR_FORMAT_NOT_SUPPORTED = 144;
144144
const ERR_VECTOR_VERSION_NOT_SUPPORTED = 145;
145+
const ERR_OBJECT_IS_NOT_A_COLLECTION = 146;
145146

146147
// Oracle Net layer errors start from 500
147148
const ERR_CONNECTION_CLOSED = 500;
@@ -414,6 +415,8 @@ messages.set(ERR_VECTOR_FORMAT_NOT_SUPPORTED, // NJS-144
414415
'VECTOR format %d is not supported');
415416
messages.set(ERR_VECTOR_VERSION_NOT_SUPPORTED, // NJS-145
416417
'VECTOR version %d is not supported');
418+
messages.set(ERR_OBJECT_IS_NOT_A_COLLECTION, // NJS-146
419+
'object %s is not a collection');
417420

418421
// Oracle Net layer errors
419422

@@ -804,6 +807,7 @@ module.exports = {
804807
ERR_WRONG_LENGTH_FOR_DBOBJECT_ELEM,
805808
ERR_VECTOR_FORMAT_NOT_SUPPORTED,
806809
ERR_VECTOR_VERSION_NOT_SUPPORTED,
810+
ERR_OBJECT_IS_NOT_A_COLLECTION,
807811
ERR_CONNECTION_CLOSED_CODE: `${ERR_PREFIX}-${ERR_CONNECTION_CLOSED}`,
808812
WRN_COMPILATION_CREATE,
809813
assert,

test/dbObject20.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,4 +1042,60 @@ describe('290. dbObject20.js', () => {
10421042
});
10431043

10441044
});
1045+
1046+
describe('290.5 Associative Arrays fetch ', function() {
1047+
let conn;
1048+
const PKG1 = 'NODB_PKG_OBJ_1_PLS_INTEGER';
1049+
const TYPE1 = 'NODB_TYP_OBJ_1_PLS_ARRAY_INTEGER';
1050+
1051+
before(async () => {
1052+
conn = await oracledb.getConnection(dbConfig);
1053+
let sql =
1054+
`CREATE OR REPLACE PACKAGE ${PKG1} AUTHID DEFINER AS
1055+
TYPE ${TYPE1} IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
1056+
FUNCTION F1 RETURN ${TYPE1};
1057+
END;`;
1058+
await conn.execute(sql);
1059+
1060+
sql = `CREATE OR REPLACE PACKAGE BODY ${PKG1} AS
1061+
FUNCTION F1 RETURN ${TYPE1} IS
1062+
R ${TYPE1};
1063+
BEGIN
1064+
R(2):=22;
1065+
R(5):=55;
1066+
RETURN R;
1067+
END ;
1068+
END ${PKG1} ;`;
1069+
await conn.execute(sql);
1070+
1071+
}); // before()
1072+
1073+
after(async () => {
1074+
if (conn) {
1075+
await testsUtil.dropType(conn, TYPE1);
1076+
const sql = `DROP package ${PKG1}`;
1077+
await conn.execute(sql);
1078+
await conn.close();
1079+
}
1080+
}); // after()
1081+
1082+
it('290.5.1 verify associative array outbinds ', async () => {
1083+
// verify pls array of integers
1084+
const inDataobj = {2: 22, 5: 55};
1085+
const result = await conn.execute(
1086+
`BEGIN
1087+
:ret := ${PKG1}.f1;
1088+
END;`,
1089+
{
1090+
ret: {
1091+
dir: oracledb.BIND_OUT,
1092+
type: `${PKG1}.${TYPE1}`
1093+
}
1094+
});
1095+
const res = result.outBinds.ret;
1096+
const outMap = res.toMap();
1097+
assert.deepStrictEqual(JSON.stringify(Object.fromEntries(outMap)), JSON.stringify(inDataobj));
1098+
});
1099+
});
1100+
10451101
});

0 commit comments

Comments
 (0)