-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathplsqlBindIndexedTable2.js
executable file
·452 lines (387 loc) · 16 KB
/
plsqlBindIndexedTable2.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
/* Copyright (c) 2015, 2023, 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
* 44. plsqlBindIndexedTable2.js
*
* DESCRIPTION
* Testing PL/SQL indexed tables (associative arrays).
*
*****************************************************************************/
'use strict';
const oracledb = require('oracledb');
const assert = require('assert');
const dbConfig = require('./dbconfig.js');
describe('44. plsqlBindIndexedTable2.js', function() {
let connection;
beforeEach(async function() {
connection = await oracledb.getConnection(dbConfig);
let proc = "BEGIN \n" +
" DECLARE \n" +
" e_table_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_table_missing, -00942);\n " +
" BEGIN \n" +
" EXECUTE IMMEDIATE ('DROP TABLE nodb_waveheight PURGE'); \n" +
" EXCEPTION \n" +
" WHEN e_table_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE (' \n" +
" CREATE TABLE nodb_waveheight (beach VARCHAR2(50), depth NUMBER) \n" +
" '); \n" +
"END; ";
await connection.execute(proc);
proc = "CREATE OR REPLACE PACKAGE nodb_beachpkg IS\n" +
" TYPE beachType IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER;\n" +
" TYPE depthType IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;\n" +
" PROCEDURE array_in(beaches IN beachType, depths IN depthType);\n" +
" PROCEDURE array_out(beaches OUT beachType, depths OUT depthType); \n" +
" PROCEDURE array_inout(beaches IN OUT beachType, depths IN OUT depthType); \n" +
"END;";
await connection.execute(proc);
proc = "CREATE OR REPLACE PACKAGE BODY nodb_beachpkg IS \n" +
" PROCEDURE array_in(beaches IN beachType, depths IN depthType) IS \n" +
" BEGIN \n" +
" IF beaches.COUNT <> depths.COUNT THEN \n" +
" RAISE_APPLICATION_ERROR(-20000, 'Array lengths must match for this example.'); \n" +
" END IF; \n" +
" FORALL i IN INDICES OF beaches \n" +
" INSERT INTO nodb_waveheight (beach, depth) VALUES (beaches(i), depths(i)); \n" +
" END; \n" +
" PROCEDURE array_out(beaches OUT beachType, depths OUT depthType) IS \n" +
" BEGIN \n" +
" SELECT beach, depth BULK COLLECT INTO beaches, depths FROM nodb_waveheight; \n" +
" END; \n" +
" PROCEDURE array_inout(beaches IN OUT beachType, depths IN OUT depthType) IS \n" +
" BEGIN \n" +
" IF beaches.COUNT <> depths.COUNT THEN \n" +
" RAISE_APPLICATION_ERROR(-20001, 'Array lenghts must match for this example.'); \n" +
" END IF; \n" +
" FORALL i IN INDICES OF beaches \n" +
" INSERT INTO nodb_waveheight (beach, depth) VALUES (beaches(i), depths(i)); \n" +
" SELECT beach, depth BULK COLLECT INTO beaches, depths FROM nodb_waveheight ORDER BY 1; \n" +
" END; \n " +
"END;";
await connection.execute(proc);
await connection.commit();
}); // before
afterEach(async function() {
await connection.execute("DROP TABLE nodb_waveheight PURGE");
await connection.execute("DROP PACKAGE nodb_beachpkg");
await connection.close();
});
it('44.1 example case', async function() {
await connection.execute("BEGIN nodb_beachpkg.array_in(:beach_in, :depth_in); END;",
{
beach_in: { type: oracledb.STRING,
dir: oracledb.BIND_IN,
val: ["Malibu Beach", "Bondi Beach", "Waikiki Beach"] },
depth_in: { type: oracledb.NUMBER,
dir: oracledb.BIND_IN,
val: [45, 30, 67]
}
}
);
let result = await connection.execute(
"BEGIN nodb_beachpkg.array_out(:beach_out, :depth_out); END;",
{
beach_out: { type: oracledb.STRING,
dir: oracledb.BIND_OUT,
maxArraySize: 3 },
depth_out: { type: oracledb.NUMBER,
dir: oracledb.BIND_OUT,
maxArraySize: 3 }
});
assert.deepStrictEqual(result.outBinds.beach_out, ([ 'Malibu Beach', 'Bondi Beach', 'Waikiki Beach' ]));
assert.deepStrictEqual(result.outBinds.depth_out, ([45, 30, 67]));
await connection.rollback();
result = await connection.execute(
"BEGIN nodb_beachpkg.array_inout(:beach_inout, :depth_inout); END;",
{
beach_inout: { type: oracledb.STRING,
dir: oracledb.BIND_INOUT,
val: ["Port Melbourne Beach", "Eighty Mile Beach", "Chesil Beach"],
maxArraySize: 3},
depth_inout: { type: oracledb.NUMBER,
dir: oracledb.BIND_INOUT,
val: [8, 3, 70],
maxArraySize: 3}
});
assert.deepStrictEqual(result.outBinds.beach_inout, ([ 'Chesil Beach', 'Eighty Mile Beach', 'Port Melbourne Beach' ]));
assert.deepStrictEqual(result.outBinds.depth_inout, ([ 70, 3, 8 ]));
}); // 44.1
it('44.2 example case binding by position', async function() {
await connection.execute(
"BEGIN nodb_beachpkg.array_in(:1, :2); END;",
[
{ type: oracledb.STRING,
dir: oracledb.BIND_IN,
val: ["Malibu Beach", "Bondi Beach", "Waikiki Beach"] },
{ type: oracledb.NUMBER,
dir: oracledb.BIND_IN,
val: [45, 30, 67]
}
]);
let result = await connection.execute(
"BEGIN nodb_beachpkg.array_out(:1, :2); END;",
[
{ type: oracledb.STRING,
dir: oracledb.BIND_OUT,
maxArraySize: 3 },
{ type: oracledb.NUMBER,
dir: oracledb.BIND_OUT,
maxArraySize: 3 }
]);
assert.deepStrictEqual(result.outBinds[0], ([ 'Malibu Beach', 'Bondi Beach', 'Waikiki Beach' ]));
assert.deepStrictEqual(result.outBinds[1], ([45, 30, 67]));
await connection.rollback();
result = await connection.execute(
"BEGIN nodb_beachpkg.array_inout(:1, :2); END;",
[
{ type: oracledb.STRING,
dir: oracledb.BIND_INOUT,
val: ["Port Melbourne Beach", "Eighty Mile Beach", "Chesil Beach"],
maxArraySize: 3},
{ type: oracledb.NUMBER,
dir: oracledb.BIND_INOUT,
val: [8, 3, 70],
maxArraySize: 3}
]);
assert.deepStrictEqual(result.outBinds[0], ([ 'Chesil Beach', 'Eighty Mile Beach', 'Port Melbourne Beach' ]));
assert.deepStrictEqual(result.outBinds[1], ([ 70, 3, 8 ]));
});
it('44.3 default binding type and direction with binding by name', async function() {
await connection.execute(
"BEGIN nodb_beachpkg.array_in(:beach_in, :depth_in); END;",
{
beach_in: { //type: oracledb.STRING,
//dir: oracledb.BIND_IN,
val: ["Malibu Beach", "Bondi Beach", "Waikiki Beach"] },
depth_in: { type: oracledb.NUMBER,
dir: oracledb.BIND_IN,
val: [45, 30, 67]
}
}
);
let result = await connection.execute("BEGIN nodb_beachpkg.array_out(:beach_out, :depth_out); END;",
{
beach_out: { type: oracledb.STRING,
dir: oracledb.BIND_OUT,
maxArraySize: 3 },
depth_out: { type: oracledb.NUMBER,
dir: oracledb.BIND_OUT,
maxArraySize: 3 }
});
assert.deepStrictEqual(result.outBinds.beach_out, ([ 'Malibu Beach', 'Bondi Beach', 'Waikiki Beach' ]));
assert.deepStrictEqual(result.outBinds.depth_out, ([45, 30, 67]));
await connection.rollback();
result = await connection.execute(
"BEGIN nodb_beachpkg.array_inout(:beach_inout, :depth_inout); END;",
{
beach_inout: { type: oracledb.STRING,
dir: oracledb.BIND_INOUT,
val: ["Port Melbourne Beach", "Eighty Mile Beach", "Chesil Beach"],
maxArraySize: 3},
depth_inout: { type: oracledb.NUMBER,
dir: oracledb.BIND_INOUT,
val: [8, 3, 70],
maxArraySize: 3}
});
//console.log(result.outBinds);
assert.deepStrictEqual(result.outBinds.beach_inout, ([ 'Chesil Beach', 'Eighty Mile Beach', 'Port Melbourne Beach' ]));
assert.deepStrictEqual(result.outBinds.depth_inout, ([ 70, 3, 8 ]));
}); // 44.3
it('44.4 default binding type and direction with binding by position', async function() {
await connection.execute("BEGIN nodb_beachpkg.array_in(:1, :2); END;",
[
{ type: oracledb.STRING,
// dir: oracledb.BIND_IN,
val: ["Malibu Beach", "Bondi Beach", "Waikiki Beach"] },
{ type: oracledb.NUMBER,
dir: oracledb.BIND_IN,
val: [45, 30, 67]
}
]);
let result = await connection.execute(
"BEGIN nodb_beachpkg.array_out(:1, :2); END;",
[
{ type: oracledb.STRING,
dir: oracledb.BIND_OUT,
maxArraySize: 3 },
{ type: oracledb.NUMBER,
dir: oracledb.BIND_OUT,
maxArraySize: 3 }
]);
assert.deepStrictEqual(result.outBinds[0], ([ 'Malibu Beach', 'Bondi Beach', 'Waikiki Beach' ]));
assert.deepStrictEqual(result.outBinds[1], ([45, 30, 67]));
await connection.rollback();
result = await connection.execute(
"BEGIN nodb_beachpkg.array_inout(:1, :2); END;",
[
{ type: oracledb.STRING,
dir: oracledb.BIND_INOUT,
val: ["Port Melbourne Beach", "Eighty Mile Beach", "Chesil Beach"],
maxArraySize: 3},
{ type: oracledb.NUMBER,
dir: oracledb.BIND_INOUT,
val: [8, 3, 70],
maxArraySize: 3}
]);
assert.deepStrictEqual(result.outBinds[0], ([ 'Chesil Beach', 'Eighty Mile Beach', 'Port Melbourne Beach' ]));
assert.deepStrictEqual(result.outBinds[1], ([ 70, 3, 8 ]));
});
it('44.5 null elements in String and Number arrays', async function() {
await connection.execute(
"BEGIN nodb_beachpkg.array_in(:beach_in, :depth_in); END;",
{
beach_in: { type: oracledb.STRING,
dir: oracledb.BIND_IN,
val: ["Malibu Beach", "Bondi Beach", null, "Waikiki Beach", '', null] },
depth_in: { type: oracledb.NUMBER,
dir: oracledb.BIND_IN,
val: [null, null, 45, 30, 67, null, ]
}
});
let result = await connection.execute(
"BEGIN nodb_beachpkg.array_out(:beach_out, :depth_out); END;",
{
beach_out: { type: oracledb.STRING,
dir: oracledb.BIND_OUT,
maxArraySize: 10 },
depth_out: { type: oracledb.NUMBER,
dir: oracledb.BIND_OUT,
maxArraySize: 10 }
});
assert.deepStrictEqual(result.outBinds.beach_out, ([ 'Malibu Beach', 'Bondi Beach', null, 'Waikiki Beach', null, null ]));
assert.deepStrictEqual(result.outBinds.depth_out, ([ null, null, 45, 30, 67, null ]));
await connection.rollback();
result = await connection.execute(
"BEGIN nodb_beachpkg.array_inout(:beach_inout, :depth_inout); END;",
{
beach_inout: { type: oracledb.STRING,
dir: oracledb.BIND_INOUT,
val: ["Port Melbourne Beach", "Eighty Mile Beach", '', "Chesil Beach", null, ''],
maxArraySize: 10},
depth_inout: { type: oracledb.NUMBER,
dir: oracledb.BIND_INOUT,
val: [null, 8, null, 3, null, 70],
maxArraySize: 10}
});
// console.log(result.outBinds);
assert.deepStrictEqual(result.outBinds.beach_inout, ([ 'Chesil Beach', 'Eighty Mile Beach', 'Port Melbourne Beach', null, null, null ]));
assert.deepStrictEqual(result.outBinds.depth_inout, ([ 3, 8, null, null, 70, null ]));
}); // 44.5
it('44.6 empty array for BIND_IN and BIND_INOUT', async function() {
await connection.execute("BEGIN nodb_beachpkg.array_in(:beach_in, :depth_in); END;",
{
beach_in: { type: oracledb.STRING,
dir: oracledb.BIND_IN,
val: [] },
depth_in: { type: oracledb.NUMBER,
dir: oracledb.BIND_IN,
val: []
}
});
await assert.rejects(
async () => await connection.execute(
"BEGIN nodb_beachpkg.array_inout(:beach_inout, :depth_inout); END;",
{
beach_inout: { type: oracledb.STRING,
dir: oracledb.BIND_INOUT,
val: [],
maxArraySize: 0
},
depth_inout: { type: oracledb.NUMBER,
dir: oracledb.BIND_INOUT,
val: [],
maxArraySize: 3}
}),
/NJS-007:/);
}); // 44.6
it('44.7 empty array for BIND_OUT', async function() {
let proc = "CREATE OR REPLACE PACKAGE\n" +
"oracledb_testpack\n" +
"IS\n" +
" TYPE stringsType IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER;\n" +
" PROCEDURE test(p OUT stringsType);\n" +
"END;";
await connection.execute(proc);
proc = "CREATE OR REPLACE PACKAGE BODY\n" +
"oracledb_testpack\n" +
"IS\n" +
" PROCEDURE test(p OUT stringsType) IS BEGIN NULL; END;\n" +
"END;";
await connection.execute(proc);
const result = await connection.execute("BEGIN oracledb_testpack.test(:0); END;",
[
{type: oracledb.STRING, dir: oracledb.BIND_OUT, maxArraySize: 1}
]
);
assert.deepStrictEqual(result.outBinds[0], []);
await connection.execute("DROP PACKAGE oracledb_testpack");
}); // 44.7
it('44.8 maxSize option applies to each elements of an array', async function() {
await connection.execute("BEGIN nodb_beachpkg.array_in(:beach_in, :depth_in); END;",
{
beach_in: { type: oracledb.STRING,
dir: oracledb.BIND_IN,
val: ["Malibu", "Bondi", "Waikiki"] },
depth_in: { type: oracledb.NUMBER,
dir: oracledb.BIND_IN,
val: [45, 30, 67]
}
});
await assert.rejects(
async () => await connection.execute(
"BEGIN nodb_beachpkg.array_out(:beach_out, :depth_out); END;",
{
beach_out: { type: oracledb.STRING,
dir: oracledb.BIND_OUT,
maxArraySize: 3,
maxSize: 6 },
depth_out: { type: oracledb.NUMBER,
dir: oracledb.BIND_OUT,
maxArraySize: 3 }
}
),
/ORA-06502:/
);
await connection.rollback();
await assert.rejects(
async () => await connection.execute(
"BEGIN nodb_beachpkg.array_inout(:beach_inout, :depth_inout); END;",
{
beach_inout: { type: oracledb.STRING,
dir: oracledb.BIND_INOUT,
val: ["Port Melbourne Beach", "Eighty Mile Beach", "Chesil Beach"],
maxArraySize: 3,
maxSize: 5},
depth_inout: { type: oracledb.NUMBER,
dir: oracledb.BIND_INOUT,
val: [8, 3, 70],
maxArraySize: 3}
}),
/NJS-058:/
);
}); // 44.8
});