-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathresultSet2.js
612 lines (536 loc) · 18 KB
/
resultSet2.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* Copyright (c) 2015, 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
* 55. resultSet2.js
*
* DESCRIPTION
* Testing driver resultSet feature.
*
*****************************************************************************/
"use strict";
const oracledb = require('oracledb');
const assert = require('assert');
const dbConfig = require('./dbconfig.js');
describe('55. resultSet2.js', function() {
let connection = null;
const tableName = "nodb_rs2_emp";
const rowsAmount = 300;
before('get one connection', async function() {
connection = await oracledb.getConnection(dbConfig);
});
after('release connection', async function() {
await connection.close();
});
describe('55.1 query a RDBMS function', function() {
it('55.1.1 LPAD function', async function() {
const result = await connection.execute(
"select lpad('a',100,'x') from dual",
[],
{ resultSet: true });
const row = await result.resultSet.getRow();
assert.strictEqual(row[0].length, 100);
await result.resultSet.close();
});
}); // 55.1
describe('55.2 binding variables', function() {
before(async function() {
await setUp(connection, tableName);
});
after(async function() {
await clearUp(connection, tableName);
});
it('55.2.1 query with one binding variable', async function() {
let rowCount = 0;
const result = await connection.execute(
"SELECT * FROM nodb_rs2_emp WHERE employees_id > :1",
[200],
{ resultSet: true });
while (true) { // eslint-disable-line
const row = await result.resultSet.getRow();
if (!row)
break;
rowCount++;
}
await result.resultSet.close();
assert.strictEqual(rowCount, 100);
});
}); // 55.2
describe('55.3 alternating getRow() & getRows() function', function() {
before(async function() {
await setUp(connection, tableName);
});
after(async function() {
await clearUp(connection, tableName);
});
it('55.3.1 result set', async function() {
const numRows = 4;
let accessCount = 0;
let getRow = true;
const result = await connection.execute(
"SELECT * FROM nodb_rs2_emp WHERE employees_id > :1",
[200],
{ resultSet: true });
while (true) { // eslint-disable-line
if (getRow) {
const row = await result.resultSet.getRow();
if (!row)
break;
getRow = false;
accessCount++;
} else {
const rows = await result.resultSet.getRows(numRows);
if (rows.length === 0)
break;
getRow = true;
accessCount++;
}
}
await result.resultSet.close();
assert.strictEqual(accessCount, (100 / (numRows + 1)) * 2);
});
it('55.3.2 REF Cursor', async function() {
const numRows = 4;
let accessCount = 0;
let getRow = true;
const result = await connection.execute(
"BEGIN nodb_rs2_get_emp(:in, :out); END;",
{
in: 200,
out: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
});
const rs = result.outBinds.out;
while (true) { // eslint-disable-line
if (getRow) {
const row = await rs.getRow();
if (!row)
break;
getRow = false;
accessCount++;
} else {
const rows = await rs.getRows(numRows);
if (rows.length === 0)
break;
getRow = true;
accessCount++;
}
}
await rs.close();
assert.strictEqual(accessCount, (100 / (numRows + 1)) * 2);
});
}); // 55.3
describe('55.4 automatically close resultSets and LOBs when the connection is closed', function() {
before(async function() {
await setUp(connection, tableName);
});
after(async function() {
await clearUp(connection, tableName);
});
let testConn = null;
beforeEach(async function() {
testConn = await oracledb.getConnection(dbConfig);
});
async function fetchRowFromRS(rs) {
while (true) { // eslint-disable-line
const row = await rs.getRow();
if (!row)
break;
}
await testConn.close();
await assert.rejects(
async () => await rs.close(),
/NJS-018:/
);
}
it('55.4.1 resultSet gets closed automatically', async function() {
const result = await testConn.execute(
"SELECT * FROM nodb_rs2_emp ORDER BY employees_id",
[],
{ resultSet: true });
await fetchRowFromRS(result.resultSet);
});
it('55.4.2 REF Cursor gets closed automatically', async function() {
const result = await testConn.execute(
"BEGIN nodb_rs2_get_emp(:in, :out); END;",
{
in: 200,
out: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
});
await fetchRowFromRS(result.outBinds.out);
});
}); // 55.4
describe('55.5 the content of resultSet should be consistent', function() {
before(async function() {
await setUp(connection, tableName);
});
after(async function() {
await clearUp(connection, tableName);
});
it('55.5.1 (1) get RS (2) modify data in that table and commit (3) check RS', async function() {
if (dbConfig.test.implicitPool) this.skip();
let rowsCount = 0;
const result = await connection.execute(
"SELECT * FROM nodb_rs2_emp ORDER BY employees_id",
[],
{ resultSet: true });
const rs = result.resultSet;
await connection.execute("TRUNCATE TABLE nodb_rs2_emp");
while (true) { // eslint-disable-line
const row = await rs.getRow();
if (!row)
break;
rowsCount++;
}
assert.strictEqual(rowsCount, rowsAmount);
await rs.close();
});
}); // 55.5
describe('55.6 access resultSet simultaneously', function() {
before(async function() {
await setUp(connection, tableName);
});
after(async function() {
await clearUp(connection, tableName);
});
it('55.6.1 concurrent operations on resultSet are not allowed (using getRows())', async function() {
const result = await connection.execute(
"SELECT * FROM nodb_rs2_emp ORDER BY employees_id",
[],
{ resultSet: true });
const rs = result.resultSet;
const promises = [
rs.getRows(),
rs.getRows()
];
const values = await Promise.allSettled(promises);
assert.strictEqual(values[1].status, 'rejected');
assert.match(values[1].reason.message, /NJS-017:/);
});
it('55.6.2 concurrent operation on REF Cursor are not allowed', async function() {
const result = await connection.execute(
"BEGIN nodb_rs2_get_emp(:in, :out); END;",
{
in: 0,
out: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
});
const rs = result.outBinds.out;
const promises = [
rs.getRows(),
rs.getRows()
];
const values = await Promise.allSettled(promises);
assert.strictEqual(values[1].status, 'rejected');
assert.match(values[1].reason.message, /NJS-017:/);
});
it('55.6.3 concurrent operations on resultSet are not allowed (using getRow())', async function() {
const result = await connection.execute(
"SELECT * FROM nodb_rs2_emp ORDER BY employees_id",
[],
{ resultSet: true });
const rs = result.resultSet;
const promises = [
rs.getRow(),
rs.getRow()
];
const values = await Promise.allSettled(promises);
assert.strictEqual(values[1].status, 'rejected');
assert.match(values[1].reason.message, /NJS-017:/);
});
it('55.6.4 concurrently closing resultSet not allowed', async function() {
const result = await connection.execute(
"SELECT * FROM nodb_rs2_emp ORDER BY employees_id",
[],
{ resultSet: true });
const rs = result.resultSet;
const promises = [
rs.close(),
rs.close()
];
const values = await Promise.allSettled(promises);
assert.strictEqual(values[1].status, 'rejected');
assert.match(values[1].reason.message, /NJS-017:/);
});
}); // 55.6
describe('55.7 getting multiple resultSets', function() {
before(async function() {
await setUp(connection, tableName);
});
after(async function() {
await clearUp(connection, tableName);
});
const numRows = 10; // number of rows to return from each call to getRows()
async function doQuery() {
const result = await connection.execute(
"SELECT * FROM nodb_rs2_emp ORDER BY employees_id",
[],
{ resultSet: true });
const rs = result.resultSet;
while (true) { // eslint-disable-line
const rows = await rs.getRows(numRows);
if (rows.length == 0)
break;
}
await rs.close();
}
async function doRefCursor() {
const result = await connection.execute(
"BEGIN nodb_rs2_get_emp(:in, :out); END;",
{
in: 200,
out: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
});
const rs = result.outBinds.out;
while (true) { // eslint-disable-line
const rows = await rs.getRows(numRows);
if (rows.length == 0)
break;
}
await rs.close();
}
it('55.7.1 can access multiple resultSet on one connection', async function() {
const promises = [
doQuery(),
doQuery(),
];
const values = await Promise.allSettled(promises);
for (let i = 0; i < promises.length; i++) {
assert.strictEqual(values[i].status, 'fulfilled');
}
});
it('55.7.2 can access multiple REF Cursor', async function() {
const promises = [
doRefCursor(),
doRefCursor(),
];
const values = await Promise.allSettled(promises);
for (let i = 0; i < promises.length; i++) {
assert.strictEqual(values[i].status, 'fulfilled');
}
});
}); // 55.7
describe('55.8 test querying a PL/SQL function', function() {
before(async function() {
await setUp(connection, tableName);
});
after(async function() {
await clearUp(connection, tableName);
});
it('55.8.1 ', async function() {
const proc =
"CREATE OR REPLACE FUNCTION nodb_rs2_testfunc RETURN VARCHAR2 \
IS \
emp_name VARCHAR2(20); \
BEGIN \
SELECT 'Clark Kent' INTO emp_name FROM dual; \
RETURN emp_name; \
END; ";
await connection.execute(proc);
const result = await connection.execute(
"SELECT nodb_rs2_testfunc FROM dual",
[],
{ resultSet: true });
assert.strictEqual(result.resultSet.metaData[0].name,
'NODB_RS2_TESTFUNC');
const row = await result.resultSet.getRow();
assert.strictEqual(row[0], 'Clark Kent');
await result.resultSet.close();
await connection.execute("DROP FUNCTION nodb_rs2_testfunc");
});
}); // 55.8
describe('55.9 calls getRows() once and then close RS before getting more rows', function() {
before(async function() {
await setUp(connection, tableName);
});
after(async function() {
await clearUp(connection, tableName);
});
it('55.9.1 ', async function() {
const numRows = 10;
const result = await connection.execute(
"SELECT * FROM nodb_rs2_emp ORDER BY employees_id",
[],
{ resultSet: true });
const rs = result.resultSet;
await rs.getRows(numRows);
await rs.close();
await assert.rejects(
async () => await rs.getRows(numRows),
/NJS-018:/
);
});
}); // 55.9
describe('55.10 use Resultset asyncIterator', function() {
before(async function() {
await setUp(connection, tableName);
});
after(async function() {
await clearUp(connection, tableName);
});
it('55.10.1 ', async function() {
const result = await connection.execute(
"SELECT * FROM nodb_rs2_emp ORDER BY employees_id",
[],
{ resultSet: true });
const rs = result.resultSet;
for await (const row of rs) {
assert.strictEqual(row.length, 2);
}
await rs.close();
});
}); // 55.10
describe('55.11 resultSet with Interval data types', function() {
it('55.11.1 INTERVAL YEAR TO MONTH data type', async function() {
const result = await connection.execute(
"SELECT dummy, to_yminterval('1-3') FROM dual", [], { resultSet: true });
const rs = result.resultSet;
for await (const row of rs) {
assert(row[1] instanceof oracledb.IntervalYM);
assert.strictEqual(row[1].years, 1);
assert.strictEqual(row[1].months, 3);
}
await rs.close();
});
it('55.11.2 INTERVAL DAY TO SECOND data type', async function() {
const result = await connection.execute(
"SELECT dummy, to_dsinterval('5 08:30:00') FROM dual", [], { resultSet: true });
const rs = result.resultSet;
for await (const row of rs) {
assert(row[1] instanceof oracledb.IntervalDS);
assert.strictEqual(row[1].days, 5);
assert.strictEqual(row[1].hours, 8);
assert.strictEqual(row[1].minutes, 30);
assert.strictEqual(row[1].seconds, 0);
assert.strictEqual(row[1].fseconds, 0);
}
await rs.close();
});
}); // 55.11
describe.skip('55.12 bind a cursor BIND_INOUT', function() {
before('prepare table nodb_rs2_emp', async function() {
await setUp(connection, tableName);
});
after('drop table', async function() {
await clearUp(connection, tableName);
});
it('55.12.1 has not supported binding a cursor with BIND_INOUT', async function() {
const proc =
"CREATE OR REPLACE PROCEDURE nodb_rs2_get_emp_inout (p_in IN NUMBER, p_out IN OUT SYS_REFCURSOR) \
AS \
BEGIN \
OPEN p_out FOR \
SELECT * FROM nodb_rs2_emp \
WHERE employees_id > p_in; \
END; ";
await connection.execute(proc);
await assert.rejects(async () => {
await connection.execute(
"BEGIN nodb_rs2_get_emp_inout(:in, :out); END;",
{
in: 200,
out: { type: oracledb.CURSOR, dir: oracledb.BIND_INOUT }
});
}, /NJS-007:/);
await connection.execute("DROP PROCEDURE nodb_rs2_get_emp_inout");
});
}); // 55.12
describe('55.13 Negative - Invalid Ref Cursor', function() {
const proc =
"CREATE OR REPLACE PROCEDURE get_invalid_refcur (p OUT SYS_REFCURSOR) " +
" AS " +
" BEGIN " +
" NULL; " +
" END;";
before(async function() {
await setUp(connection, tableName);
await connection.execute(proc);
});
after(async function() {
await connection.execute("DROP PROCEDURE get_invalid_refcur");
await clearUp(connection, tableName);
});
it('55.13.1 ', async function() {
await assert.rejects(async () => {
await connection.execute(
"BEGIN get_invalid_refcur ( :p ); END; ",
{
p: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
});
}, /NJS-107:/);
});
}); // 55.13
});
/********************* Helper functions *************************/
async function setUp(connection, tableName) {
await createTable(connection, tableName);
await insertData(connection, tableName);
await createProc1(connection, tableName);
}
async function clearUp(connection, tableName) {
await connection.execute('DROP PROCEDURE nodb_rs2_get_emp');
await connection.execute('DROP TABLE ' + tableName + ' PURGE');
}
async function createTable(connection, tableName) {
const sqlCreate =
"BEGIN " +
" DECLARE " +
" e_table_missing EXCEPTION; " +
" PRAGMA EXCEPTION_INIT(e_table_missing, -00942); " +
" BEGIN " +
" EXECUTE IMMEDIATE ('DROP TABLE " + tableName + " PURGE'); " +
" EXCEPTION " +
" WHEN e_table_missing " +
" THEN NULL; " +
" END; " +
" EXECUTE IMMEDIATE (' " +
" CREATE TABLE " + tableName + " ( " +
" employees_id NUMBER(10), " +
" employee_name VARCHAR2(20) " +
" )" +
" '); " +
"END; ";
await connection.execute(sqlCreate);
}
async function insertData(connection, tableName) {
const sqlInsert =
"DECLARE " +
" x NUMBER := 0; " +
" n VARCHAR2(20); " +
"BEGIN " +
" FOR i IN 1..300 LOOP " +
" x := x + 1; " +
" n := 'staff ' || x; " +
" INSERT INTO " + tableName + " VALUES (x, n); " +
" END LOOP; " +
"END; ";
await connection.execute(sqlInsert, [], { autoCommit: true });
}
async function createProc1(connection, tableName) {
const sqlProc =
"CREATE OR REPLACE PROCEDURE nodb_rs2_get_emp (p_in IN NUMBER, p_out OUT SYS_REFCURSOR) " +
" AS " +
" BEGIN " +
" OPEN p_out FOR " +
" SELECT * FROM " + tableName + " WHERE employees_id > p_in; " +
" END; ";
await connection.execute(sqlProc);
}