Skip to content

Commit 60f3cd5

Browse files
committed
Fix for some features in DBObject properties that do not work correctly with Associated Arrays
1 parent e81df91 commit 60f3cd5

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

doc/src/api_manual/dbobject.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,20 @@ it is to convert that DbObject to and from a JavaScript object.
153153

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

156+
If the passed-in ``index`` parameter is not found in the :ref:`associative
157+
array collection types indexed by integers <indexbyplsinteger>`, then this
158+
method returns the next available higher index found in the associative
159+
array.
160+
156161
.. method:: dbObject.getPrevIndex(Number index)
157162

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

165+
If the passed-in ``index`` parameter is not found in the :ref:`associative
166+
array collection types indexed by integers <indexbyplsinteger>`, then this
167+
method returns the next available lower index found in the associative
168+
array.
169+
160170
.. method:: dbObject.hasElement(Number index)
161171

162172
Returns *true* if an element exists in the collection at the given

doc/src/release_notes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ Thin Mode Changes
4444
provided in the connect string.
4545
See `Issue #1673 <https://github.com/oracle/node-oracledb/issues/1673>`__.
4646

47+
#) Fixed bug with associative arrays indexed by integers that did not sort
48+
the index properly.
49+
50+
#) Fixed bug with associative arrays indexed by integers that caused the
51+
:meth:`dbObject.getPrevIndex()` method to change the array order.
52+
4753
node-oracledb `v6.7.0 <https://github.com/oracle/node-oracledb/compare/v6.6.0...v6.7.0>`__ (18 Nov 2024)
4854
---------------------------------------------------------------------------------------------------------
4955

lib/thin/dbObject.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ class ThinDbObjectImpl extends DbObjectImpl {
179179
//---------------------------------------------------------------------------
180180
_ensureAssocKeys() {
181181
if (!this.unpackedAssocKeys) {
182-
this.unpackedAssocKeys = [...this.unpackedAssocArray.keys()].sort();
182+
// Associative arrays indexed by integer are only supported now
183+
this.unpackedAssocKeys = [...this.unpackedAssocArray.keys()].sort((x, y) => x - y);
183184
}
184185
}
185186

@@ -546,6 +547,9 @@ class ThinDbObjectImpl extends DbObjectImpl {
546547
// getNextIndex()
547548
//
548549
// Returns the next index in a collection.
550+
// For associative arrays indexed by integers, if the passed-in index
551+
// parameter is not present, it will return the next higher index found
552+
// in the associative array.
549553
//---------------------------------------------------------------------------
550554
getNextIndex(index) {
551555
this._ensureUnpacked();
@@ -566,6 +570,9 @@ class ThinDbObjectImpl extends DbObjectImpl {
566570
// getPrevIndex()
567571
//
568572
// Returns the previous index in a collection.
573+
// For associative arrays indexed by integers, if the passed-in index
574+
// parameter is not present, it will return the next lower index found
575+
// in the associative array.
569576
//---------------------------------------------------------------------------
570577
getPrevIndex(index) {
571578
this._ensureUnpacked();
@@ -575,9 +582,11 @@ class ThinDbObjectImpl extends DbObjectImpl {
575582
}
576583
} else if (this.unpackedAssocArray) {
577584
this._ensureAssocKeys();
578-
for (const key of this.unpackedAssocKeys.reverse()) {
579-
if (key < index)
580-
return key;
585+
let prev;
586+
for (const key of this.unpackedAssocKeys) {
587+
if (key >= index)
588+
return prev;
589+
prev = key;
581590
}
582591
}
583592
}

test/dbObject20.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,8 +1181,9 @@ describe('290. dbObject20.js', () => {
11811181
FUNCTION F1 RETURN ${TYPE1} IS
11821182
R ${TYPE1};
11831183
BEGIN
1184-
R(2):=22;
11851184
R(5):=55;
1185+
R(2):=22;
1186+
R(10):=120;
11861187
RETURN R;
11871188
END ;
11881189
END ${PKG1} ;`;
@@ -1199,9 +1200,9 @@ describe('290. dbObject20.js', () => {
11991200
}
12001201
}); // after()
12011202

1202-
it('290.5.1 verify associative array outbinds ', async () => {
1203+
it('290.5.1 verify associative array outbinds', async () => {
12031204
// verify pls array of integers
1204-
const inDataobj = {2: 22, 5: 55};
1205+
const inDataobj = {5: 55, 2: 22, 10: 120};
12051206
const result = await conn.execute(
12061207
`BEGIN
12071208
:ret := ${PKG1}.f1;
@@ -1215,6 +1216,34 @@ describe('290. dbObject20.js', () => {
12151216
const res = result.outBinds.ret;
12161217
const outMap = res.toMap();
12171218
assert.deepStrictEqual(JSON.stringify(Object.fromEntries(outMap)), JSON.stringify(inDataobj));
1219+
1220+
// Check if you are able to access the indexes, keys and values across
1221+
// associative arrays in proper order
1222+
const firstIndex = res.getFirstIndex();
1223+
assert.strictEqual(firstIndex, 2);
1224+
assert.strictEqual(res.getPrevIndex(firstIndex), undefined);
1225+
const nextIndex = res.getNextIndex(firstIndex);
1226+
assert.strictEqual(nextIndex, 5);
1227+
assert.strictEqual(res.getPrevIndex(nextIndex), firstIndex);
1228+
const lastIndex = res.getLastIndex();
1229+
assert.strictEqual(lastIndex, 10);
1230+
assert.strictEqual(res.getNextIndex(lastIndex), undefined);
1231+
1232+
// Ensure the order of the keys are not changed!
1233+
assert.strictEqual(res.getNextIndex(firstIndex), 5);
1234+
1235+
// Use an index that does not exist. The previous and the next index
1236+
// should still be returned
1237+
assert.strictEqual(res.getPrevIndex(4), 2);
1238+
assert.strictEqual(res.getPrevIndex(1), undefined);
1239+
assert.strictEqual(res.getNextIndex(7), 10);
1240+
assert.strictEqual(res.getNextIndex(11), undefined);
1241+
1242+
// Check a few other properties
1243+
assert.strictEqual(res.hasElement(2), true);
1244+
assert.strictEqual(res.hasElement(12), false);
1245+
assert.deepStrictEqual(res.getKeys(), Object.keys(inDataobj).map(Number));
1246+
assert.deepStrictEqual(res.getValues(), Object.values(inDataobj).map(Number));
12181247
});
12191248
});
12201249

0 commit comments

Comments
 (0)