Skip to content

Commit

Permalink
Fix for some features in DBObject properties that do not work correct…
Browse files Browse the repository at this point in the history
…ly with Associated Arrays
  • Loading branch information
sharadraju committed Dec 18, 2024
1 parent e81df91 commit 60f3cd5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
10 changes: 10 additions & 0 deletions doc/src/api_manual/dbobject.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,20 @@ it is to convert that DbObject to and from a JavaScript object.

Returns the next index value for later use to obtain a value.

If the passed-in ``index`` parameter is not found in the :ref:`associative
array collection types indexed by integers <indexbyplsinteger>`, then this
method returns the next available higher index found in the associative
array.

.. method:: dbObject.getPrevIndex(Number index)

Returns the previous index for later use to obtain the value.

If the passed-in ``index`` parameter is not found in the :ref:`associative
array collection types indexed by integers <indexbyplsinteger>`, then this
method returns the next available lower index found in the associative
array.

.. method:: dbObject.hasElement(Number index)

Returns *true* if an element exists in the collection at the given
Expand Down
6 changes: 6 additions & 0 deletions doc/src/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ Thin Mode Changes
provided in the connect string.
See `Issue #1673 <https://github.com/oracle/node-oracledb/issues/1673>`__.

#) Fixed bug with associative arrays indexed by integers that did not sort
the index properly.

#) Fixed bug with associative arrays indexed by integers that caused the
:meth:`dbObject.getPrevIndex()` method to change the array order.

node-oracledb `v6.7.0 <https://github.com/oracle/node-oracledb/compare/v6.6.0...v6.7.0>`__ (18 Nov 2024)
---------------------------------------------------------------------------------------------------------

Expand Down
17 changes: 13 additions & 4 deletions lib/thin/dbObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ class ThinDbObjectImpl extends DbObjectImpl {
//---------------------------------------------------------------------------
_ensureAssocKeys() {
if (!this.unpackedAssocKeys) {
this.unpackedAssocKeys = [...this.unpackedAssocArray.keys()].sort();
// Associative arrays indexed by integer are only supported now
this.unpackedAssocKeys = [...this.unpackedAssocArray.keys()].sort((x, y) => x - y);
}
}

Expand Down Expand Up @@ -546,6 +547,9 @@ class ThinDbObjectImpl extends DbObjectImpl {
// getNextIndex()
//
// Returns the next index in a collection.
// For associative arrays indexed by integers, if the passed-in index
// parameter is not present, it will return the next higher index found
// in the associative array.
//---------------------------------------------------------------------------
getNextIndex(index) {
this._ensureUnpacked();
Expand All @@ -566,6 +570,9 @@ class ThinDbObjectImpl extends DbObjectImpl {
// getPrevIndex()
//
// Returns the previous index in a collection.
// For associative arrays indexed by integers, if the passed-in index
// parameter is not present, it will return the next lower index found
// in the associative array.
//---------------------------------------------------------------------------
getPrevIndex(index) {
this._ensureUnpacked();
Expand All @@ -575,9 +582,11 @@ class ThinDbObjectImpl extends DbObjectImpl {
}
} else if (this.unpackedAssocArray) {
this._ensureAssocKeys();
for (const key of this.unpackedAssocKeys.reverse()) {
if (key < index)
return key;
let prev;
for (const key of this.unpackedAssocKeys) {
if (key >= index)
return prev;
prev = key;
}
}
}
Expand Down
35 changes: 32 additions & 3 deletions test/dbObject20.js
Original file line number Diff line number Diff line change
Expand Up @@ -1181,8 +1181,9 @@ describe('290. dbObject20.js', () => {
FUNCTION F1 RETURN ${TYPE1} IS
R ${TYPE1};
BEGIN
R(2):=22;
R(5):=55;
R(2):=22;
R(10):=120;
RETURN R;
END ;
END ${PKG1} ;`;
Expand All @@ -1199,9 +1200,9 @@ describe('290. dbObject20.js', () => {
}
}); // after()

it('290.5.1 verify associative array outbinds ', async () => {
it('290.5.1 verify associative array outbinds', async () => {
// verify pls array of integers
const inDataobj = {2: 22, 5: 55};
const inDataobj = {5: 55, 2: 22, 10: 120};
const result = await conn.execute(
`BEGIN
:ret := ${PKG1}.f1;
Expand All @@ -1215,6 +1216,34 @@ describe('290. dbObject20.js', () => {
const res = result.outBinds.ret;
const outMap = res.toMap();
assert.deepStrictEqual(JSON.stringify(Object.fromEntries(outMap)), JSON.stringify(inDataobj));

// Check if you are able to access the indexes, keys and values across
// associative arrays in proper order
const firstIndex = res.getFirstIndex();
assert.strictEqual(firstIndex, 2);
assert.strictEqual(res.getPrevIndex(firstIndex), undefined);
const nextIndex = res.getNextIndex(firstIndex);
assert.strictEqual(nextIndex, 5);
assert.strictEqual(res.getPrevIndex(nextIndex), firstIndex);
const lastIndex = res.getLastIndex();
assert.strictEqual(lastIndex, 10);
assert.strictEqual(res.getNextIndex(lastIndex), undefined);

// Ensure the order of the keys are not changed!
assert.strictEqual(res.getNextIndex(firstIndex), 5);

// Use an index that does not exist. The previous and the next index
// should still be returned
assert.strictEqual(res.getPrevIndex(4), 2);
assert.strictEqual(res.getPrevIndex(1), undefined);
assert.strictEqual(res.getNextIndex(7), 10);
assert.strictEqual(res.getNextIndex(11), undefined);

// Check a few other properties
assert.strictEqual(res.hasElement(2), true);
assert.strictEqual(res.hasElement(12), false);
assert.deepStrictEqual(res.getKeys(), Object.keys(inDataobj).map(Number));
assert.deepStrictEqual(res.getValues(), Object.values(inDataobj).map(Number));
});
});

Expand Down

0 comments on commit 60f3cd5

Please sign in to comment.