-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathdbObjectsNestedTable.js
344 lines (286 loc) · 11.5 KB
/
dbObjectsNestedTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* Copyright (c) 2021, 2025, Oracle and/or its affiliates. */
/******************************************************************************
*
* This software is dual-licensed to you under the Universal Permissive License
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
* 2.0 as shown at https://www.apache.org/licenses/LICENSE-2.0. You may choose
* either license.
*
* If you elect to accept the software under the Apache License, Version 2.0,
* the following applies:
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NAME
* 197. dbObjectsNestedTable.js
*
* DESCRIPTION
* Testing Oracle Database Nested Table Object Collection type(s)
*
*****************************************************************************/
'use strict';
const oracledb = require ('oracledb');
const assert = require ('assert');
const dbConfig = require ('./dbconfig.js');
describe('197. dbObjectsNestedTable.js', () => {
let connection = null;
before (async () => {
connection = await oracledb.getConnection (dbConfig);
let sql = `CREATE TYPE nodb_test197_typ IS TABLE OF VARCHAR2(30)`;
await connection.execute (sql);
await connection.commit ();
sql = `CREATE TABLE nodb_test197_TAB (
ID NUMBER, DEPT_NAMES nodb_test197_typ)
NESTED TABLE dept_names store as dnames_nt`;
await connection.execute (sql);
await connection.commit ();
});
after (async () => {
await connection.execute (`drop table nodb_test197_tab purge`);
await connection.execute (`drop type nodb_test197_typ`);
await connection.commit ();
await connection.close ();
});
it('197.1 Insert into table with Nested-table + getValues',
async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19701;
let obj = new objClass ();
obj.append ("Shipping");
obj.append ("Finance");
obj.append ("Sales") ;
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB`;
const result = await connection.execute (sql);
obj = result.rows[0][1];
const arr = obj.getValues ();
assert.equal (arr.length, obj.length);
assert.strictEqual (arr[0], "Shipping");
assert.strictEqual (arr[1], "Finance");
assert.strictEqual (arr[2], "Sales");
}
); // 197.1
it ('197.2 Insert into table with Nested-table + getKeys',
async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES (:id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19702;
let obj = new objClass ();
obj.append ("Shipping");
obj.append ("Finance");
obj.append ("Sales") ;
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB`;
const result = await connection.execute (sql);
obj = result.rows[0][1];
const arr = obj.getKeys ();
assert.equal (arr.length, 3);
assert.strictEqual (arr[0], 0);
assert.strictEqual (arr[1], 1);
assert.strictEqual (arr[2], 2);
}
); // 197.2
it ('197.3 Insert into table with Nested-table + first * next',
async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19703;
let obj = new objClass ();
obj.append ("Shipping");
obj.append ("Finance");
obj.append ("Sales") ;
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB`;
const result = await connection.execute (sql);
obj = result.rows[0][1];
let index = obj.getFirstIndex ();
while (index != null) {
const v = obj.getElement (index);
switch (index) {
case 0:
assert.strictEqual (v, "Shipping");
break;
case 1:
assert.strictEqual (v, "Finance");
break;
case 2:
assert.strictEqual (v, "Sales");
break;
}
index = obj.getNextIndex (index);
}
}
); //197.3
it('197.4 Insert into table with Nested-table + last + prev',
async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19704;
let obj = new objClass ();
obj.append ("Shipping");
obj.append ("Finance");
obj.append ("Sales") ;
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB`;
const result = await connection.execute (sql);
obj = result.rows[0][1];
let index = obj.getLastIndex ();
while (index != null) {
const v = obj.getElement (index);
switch (index) {
case 0:
assert.strictEqual (v, "Shipping");
break;
case 1:
assert.strictEqual (v, "Finance");
break;
case 2:
assert.strictEqual (v, "Sales");
break;
}
index = obj.getPrevIndex (index);
}
}
); //197.4
it('197.5 Insert into table with Nested-table + getElement',
async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19705;
let obj = new objClass ();
obj.append ("Shipping");
obj.append ("Finance");
obj.append ("Sales") ;
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB`;
const result = await connection.execute (sql);
obj = result.rows[0][1];
// randomly use getElement ()
let v = obj.getElement (1);
assert.strictEqual (v, "Finance");
v = obj.getElement (0);
assert.strictEqual (v, "Shipping");
}
); //197.5
it('197.6 Insert into table with Nested-table + hasElement',
async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19706;
let obj = new objClass ();
obj.append ("Shipping");
obj.append ("Finance");
obj.append ("Sales") ;
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB`;
const result = await connection.execute (sql);
obj = result.rows[0][1];
assert.strictEqual(obj.hasElement(0), true);
assert.strictEqual(obj.hasElement(1), true);
assert.strictEqual(obj.hasElement(2), true);
assert.strictEqual(obj.hasElement(3), false);
}
); //197.6
it('197.7 Insert into table with Nested-table NULL Value for object',
async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19707;
await connection.execute (sql, { id: id,
v: { type: objClass, val: null } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :ID`;
const result = await connection.execute (sql, { ID: 19707});
const obj = result.rows[0][1];
assert.strictEqual (obj, null);
}
); // 197.7
it('197.8 Insert into table with Nested-table NULL Value for object',
async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19708;
let obj = new objClass ();
obj.append (null);
obj.append (null);
obj.append (null) ;
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :ID`;
const result = await connection.execute (sql, { ID: 19708 });
obj = result.rows[0][1];
const arr = obj.getValues();
assert.deepStrictEqual(arr, [ null, null, null ]);
assert.strictEqual (obj.getElement(0), null);
}
); // 197.8
it('197.9 Insert into table and use deleteElement()', async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19709;
let obj = new objClass ([ "One", "Two", "Three", "four" ]);
obj.deleteElement (2); // delete the 3rd element "Three"
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :id`;
const result = await connection.execute (sql, { id: id });
obj = result.rows[0][1];
const arr = obj.getValues ();
assert.deepStrictEqual(arr, [ 'One', 'Two', 'four' ]);
}); // 197.9
it('197.10 Insert into table and use setElement()', async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19710;
let obj = new objClass ([ "One", "Two", "Three", "Four" ]);
obj.setElement (2, "3");
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :id`;
const result = await connection.execute (sql, { id: id });
obj = result.rows[0][1];
const arr = obj.getValues ();
assert.deepStrictEqual(arr, [ 'One', 'Two', '3', 'Four' ]);
}); // 197.10
it('197.11 Insert into table and use trim()', async () => {
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const id = 19711;
let obj = new objClass ([ "One", "Two", "Three", "Four" ]);
obj.trim (2);
await connection.execute (sql, { id: id, v: { val: obj } });
await connection.commit ();
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :id`;
const result = await connection.execute (sql, { id: id });
obj = result.rows[0][1];
const arr = obj.getValues ();
assert.deepStrictEqual(arr, [ 'One', 'Two' ]);
}); // 197.11
it('197.12 Negative - use setElement() on an out-of-range index', async () => {
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const obj = new objClass ([ "One", "Two", "Three", "Four" ]);
assert.throws(() => obj.setElement (4, "Five"), /NJS-131:/);
}); // 197.12
it('197.13 Negative - use getElement() on an out-of-range index', async () => {
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
const obj = new objClass ([ "One", "Two", "Three", "Four" ]);
assert.throws(() => obj.getElement (4), /NJS-132:/);
}); // 197.13
});