-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathfetchBlobAsBuffer1.js
1871 lines (1582 loc) · 67.7 KB
/
fetchBlobAsBuffer1.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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2017, 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
* 87. fetchBlobAsBuffer1.js
*
* DESCRIPTION
* Testing Oracle data type support - BLOB.
* To fetch BLOB columns as buffer by setting oracledb.fetchAsBuffer.
*
*****************************************************************************/
'use strict';
const oracledb = require('oracledb');
const assert = require('assert');
const fs = require('fs');
const dbConfig = require('./dbconfig.js');
const random = require('./random.js');
describe('87. fetchBlobAsBuffer1.js', function() {
let connection = null;
let insertID = 1; // assume id for insert into db starts from 1
const inFileName = './test/blobTmpFile.txt';
const defaultStmtCache = oracledb.stmtCacheSize;
const proc_create_table1 = "BEGIN \n" +
" DECLARE \n" +
" e_table_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_table_missing, -00942);\n" +
" BEGIN \n" +
" EXECUTE IMMEDIATE ('DROP TABLE nodb_blob1 PURGE' ); \n" +
" EXCEPTION \n" +
" WHEN e_table_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE ( ' \n" +
" CREATE TABLE nodb_blob1 ( \n" +
" ID NUMBER, \n" +
" B BLOB \n" +
" ) \n" +
" '); \n" +
"END; ";
const drop_table1 = "DROP TABLE nodb_blob1 PURGE";
before('get one connection', async function() {
oracledb.stmtCacheSize = 0;
connection = await oracledb.getConnection(dbConfig);
await fs.promises.writeFile(inFileName, '');
}); // before
after('release connection', async function() {
oracledb.stmtCacheSize = defaultStmtCache;
await connection.close();
await fs.promises.unlink(inFileName);
}); // after
// Generic function to insert a single row given ID, and data
const insertIntoBlobTable1 = async function(id, content) {
let result;
if (content == "EMPTY_BLOB") {
result = await connection.execute(
"INSERT INTO nodb_blob1 VALUES (:ID, EMPTY_BLOB())",
[ id ]);
assert.strictEqual(result.rowsAffected, 1);
} else {
result = await connection.execute(
"INSERT INTO nodb_blob1 VALUES (:ID, :B)",
{
ID: { val: id },
B: { val: content, dir: oracledb.BIND_IN, type: oracledb.BUFFER }
});
assert.strictEqual(result.rowsAffected, 1);
}
};
const updateBlobTable1 = async function(id, content) {
const result = await connection.execute(
"UPDATE nodb_blob1 set B = :B where ID = :ID",
{ ID: id, B: content });
assert.strictEqual(result.rowsAffected, 1);
};
describe('87.1 fetch BLOB columns by setting oracledb.fetchAsBuffer', function() {
before('Create table and populate', async function() {
await connection.execute(proc_create_table1);
}); // before
after('drop table', async function() {
await connection.execute(drop_table1);
}); // after
const insertAndFetch = async function(id, specialStr, insertContent) {
await insertIntoBlobTable1(id, insertContent);
const result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE ID = :id",
{ id: id });
const resultVal = result.rows[0][1];
if (specialStr === null) {
assert.strictEqual(resultVal, null);
} else {
assert.deepStrictEqual(resultVal, insertContent);
}
};
beforeEach('set oracledb.fetchAsBuffer', function() {
oracledb.fetchAsBuffer = [ oracledb.BLOB ];
}); // beforeEach
afterEach('clear the by-type specification', function() {
oracledb.fetchAsBuffer = [];
}); // afterEach
it('87.1.1 works with NULL value', async function() {
const id = insertID++;
const content = null;
await insertAndFetch(id, null, content);
}); // 87.1.1
it('87.1.2 works with empty Buffer', async function() {
const id = insertID++;
const content = Buffer.from("", "utf-8");
await insertAndFetch(id, null, content);
}); // 87.1.2
it('87.1.3 works with small value', async function() {
const id = insertID++;
const specialStr = '87.1.3';
const contentLength = 20;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.1.3
it('87.1.4 works with (64K - 1) value', async function() {
const id = insertID++;
const specialStr = '87.1.4';
const contentLength = 65535;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.1.4
it('87.1.5 works with (64K + 1) value', async function() {
const id = insertID++;
const specialStr = '87.1.5';
const contentLength = 65537;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.1.5
it('87.1.6 works with (1MB + 1) data', async function() {
const id = insertID++;
const specialStr = '87.1.6';
const contentLength = 1048577; // 1MB + 1
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.1.6
it('87.1.7 works with dbms_lob.substr()', async function() {
const id = insertID++;
const specialStr = '87.1.7';
const contentLength = 200;
const specialStrLength = specialStr.length;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertIntoBlobTable1(id, content);
const result = await connection.execute(
"SELECT dbms_lob.substr(B, " + specialStrLength + ", 1) from nodb_blob1 WHERE ID = :id",
{ id: id });
const resultVal = result.rows[0][0];
const buffer2Compare = Buffer.from(specialStr, "utf-8");
assert.deepStrictEqual(resultVal, buffer2Compare);
}); // 87.1.7
it('87.1.8 works with EMPTY_BLOB()', async function() {
const id = insertID++;
const content = "EMPTY_BLOB";
await insertAndFetch(id, null, content);
}); // 87.1.8
it('87.1.9 fetch multiple BLOB rows as Buffer', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.1.9_1';
const contentLength_1 = 200;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.1.9_2';
const contentLength_2 = 100;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
const result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE ID = " + id_1 + " or id = " + id_2);
assert.deepStrictEqual(result.rows[0][1], content_1);
assert.deepStrictEqual(result.rows[1][1], content_2);
}); // 87.1.9
it('87.1.10 fetch the same BLOB column multiple times', async function() {
const id = insertID++;
const specialStr = '87.1.10';
const contentLength = 200;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertIntoBlobTable1(id, content);
const result = await connection.execute(
"SELECT ID, B AS B1, B AS B2 from nodb_blob1 WHERE ID = " + id);
assert.deepStrictEqual(result.rows[0][1], content);
assert.deepStrictEqual(result.rows[0][2], content);
}); // 87.1.10
it('87.1.11 works with update statement', async function() {
const id = insertID++;
const specialStr_1 = '87.1.11_1';
const contentLength_1 = 208;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const specialStr_2 = '87.1.11_2';
const contentLength_2 = 200;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
await insertAndFetch(id, specialStr_1, content_1);
await updateBlobTable1(id, content_2);
const result = await connection.execute("SELECT ID, B from nodb_blob1 WHERE ID = " + id);
assert.deepStrictEqual(result.rows[0][1], content_2);
}); // 87.1.11
it('87.1.12 works with REF CURSOR', async function() {
const id = insertID++;
const specialStr = '87.1.12';
const contentLength = 100;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertIntoBlobTable1(id, content);
const ref_proc = "CREATE OR REPLACE PROCEDURE nodb_ref(blob_cursor OUT SYS_REFCURSOR)\n" +
"AS \n" +
"BEGIN \n" +
" OPEN blob_cursor FOR \n" +
" SELECT B from nodb_blob1 WHERE ID = " + id + "; \n" +
"END;";
await connection.execute(ref_proc);
const sql = "BEGIN nodb_ref(:b); END;";
const bindVar = {
b: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
};
const result = await connection.execute(sql, bindVar);
const rows = await result.outBinds.b.getRows(3);
assert.deepStrictEqual(rows[0][0], content);
await result.outBinds.b.close();
const ref_proc_drop = "DROP PROCEDURE nodb_ref";
await connection.execute(ref_proc_drop);
}); // 87.1.12
it('87.1.13 fetch BLOB with stream', async function() {
const id = insertID++;
const specialStr = '87.1.13';
const contentLength = 200;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertIntoBlobTable1(id, content);
oracledb.fetchAsBuffer = [];
const result = await connection.execute(
"SELECT B from nodb_blob1 WHERE ID = " + id);
const lob = result.rows[0][0];
let blobData = Buffer.alloc(0);
await new Promise((resolve, reject) => {
lob.on('data', function(chunk) {
blobData = Buffer.concat([blobData, chunk]);
});
lob.on('error', reject);
lob.on('end', lob.destroy);
lob.on('close', resolve);
});
assert.deepStrictEqual(blobData, content);
}); // 87.1.13
it('87.1.14 works with setting oracledb.maxRows < actual number of rows in the table', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.1.14_1';
const contentLength_1 = 200;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.1.14_2';
const contentLength_2 = 100;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
const maxRowsBak = oracledb.maxRows;
oracledb.maxRows = 1;
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
const result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE id = " + id_1 + " or id = " + id_2);
assert.strictEqual(result.rows.length, 1);
assert.deepStrictEqual(result.rows[0][1], content_1);
oracledb.maxRows = maxRowsBak;
}); // 87.1.14
it('87.1.15 works with setting oracledb.maxRows > actual number of rows in the table', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.1.15_1';
const contentLength_1 = 200;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.1.15_2';
const contentLength_2 = 100;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
const maxRowsBak = oracledb.maxRows;
oracledb.maxRows = 10;
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
const result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE id = " + id_1 + " or id = " + id_2);
assert.strictEqual(result.rows.length, 2);
assert.deepStrictEqual(result.rows[0][1], content_1);
assert.deepStrictEqual(result.rows[1][1], content_2);
oracledb.maxRows = maxRowsBak;
}); // 87.1.15
it('87.1.16 override oracledb.fetchAsBuffer with fetchInfo set to oracledb.DEFAULT', async function() {
const id = insertID++;
const specialStr = '87.1.16';
const contentLength = 20;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertIntoBlobTable1(id, content);
const result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE ID = :id",
{ id: id },
{
fetchInfo: { B: { type: oracledb.DEFAULT } }
});
const lob = result.rows[0][1];
const blobData = await lob.getData();
assert.deepStrictEqual(blobData, content);
lob.destroy();
}); // 87.1.16
it('87.1.17 works with await connection.queryStream()', async function() {
const id = insertID++;
const specialStr = '87.1.17';
const contentLength = 200;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertIntoBlobTable1(id, content);
const sql = "SELECT ID, B from nodb_blob1 WHERE ID = " + id;
const stream = await connection.queryStream(sql);
let counter = 0;
await new Promise((resolve, reject) => {
stream.on('error', reject);
stream.on('end', stream.destroy);
stream.on('close', resolve);
stream.on('data', function(data) {
assert.deepStrictEqual(data[1], content);
counter++;
});
});
assert.strictEqual(counter, 1);
}); // 87.1.17
it('87.1.18 works with await connection.queryStream() and oracledb.maxRows > actual number of rows in the table', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.1.18_1';
const contentLength_1 = 26;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.1.18_2';
const contentLength_2 = 30;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
const maxRowsBak = oracledb.maxRows;
oracledb.maxRows = 20;
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
const sql = "SELECT ID, B from nodb_blob1 WHERE ID = " + id_1 + " or id = " + id_2;
const stream = await connection.queryStream(sql);
let counter = 0;
await new Promise((resolve, reject) => {
stream.on('error', reject);
stream.on('end', stream.destroy);
stream.on('close', resolve);
stream.on('data', function(data) {
const result = data[1];
counter++;
if (counter == 1) {
assert.deepStrictEqual(result, content_1);
} else {
assert.deepStrictEqual(result, content_2);
}
});
});
oracledb.maxRows = maxRowsBak;
assert.strictEqual(counter, 2);
}); // 87.1.18
it('87.1.19 works with await connection.queryStream() and oracledb.maxRows = actual number of rows in the table', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.1.19_1';
const contentLength_1 = 26;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.1.19_2';
const contentLength_2 = 30;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
const maxRowsBak = oracledb.maxRows;
oracledb.maxRows = 2;
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
const sql = "SELECT ID, B from nodb_blob1 WHERE ID = " + id_1 + " or id = " + id_2;
const stream = await connection.queryStream(sql);
await new Promise((resolve, reject) => {
stream.on('error', function(error) {
assert.ifError(error, null, 'Error event should not be triggered');
reject();
});
let counter = 0;
stream.on('data', function(data) {
assert(data);
const result = data[1];
counter++;
if (counter == 1) {
assert.deepStrictEqual(result, content_1);
} else {
assert.deepStrictEqual(result, content_2);
}
});
stream.on('end', function() {
assert.strictEqual(counter, 2);
oracledb.maxRows = maxRowsBak;
stream.destroy();
});
stream.on('close', function() {
resolve();
});
});
}); // 87.1.19
it('87.1.20 works with await connection.queryStream() and oracledb.maxRows < actual number of rows in the table', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.1.20_1';
const contentLength_1 = 26;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.1.19_2';
const contentLength_2 = 30;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
const maxRowsBak = oracledb.maxRows;
oracledb.maxRows = 1;
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
const sql = "SELECT ID, B from nodb_blob1 WHERE ID = " + id_1 + " or id = " + id_2;
const stream = await connection.queryStream(sql);
await new Promise((resolve, reject) => {
stream.on('error', function(error) {
assert.ifError(error, null, 'Error event should not be triggered');
reject();
});
let counter = 0;
stream.on('data', function(data) {
assert(data);
const result = data[1];
counter++;
if (counter == 1) {
assert.deepStrictEqual(result, content_1);
} else {
assert.deepStrictEqual(result, content_2);
}
});
stream.on('end', function() {
assert.strictEqual(counter, 2);
oracledb.maxRows = maxRowsBak;
stream.destroy();
});
stream.on('close', function() {
resolve();
});
});
}); // 87.1.20
}); // 87.1
describe('87.2 fetch BLOB columns by setting oracledb.fetchAsBuffer and outFormat = oracledb.OUT_FORMAT_OBJECT', function() {
before('Create table and populate', async function() {
await connection.execute(proc_create_table1);
}); // before
after('drop table', async function() {
await connection.execute(drop_table1);
}); // after
beforeEach('set oracledb.fetchAsBuffer', function() {
oracledb.fetchAsBuffer = [ oracledb.BLOB ];
}); // beforeEach
afterEach('clear the by-type specification', function() {
oracledb.fetchAsBuffer = [];
}); // afterEach
const insertAndFetch = async function(id, specialStr, insertContent) {
await insertIntoBlobTable1(id, insertContent);
const result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE ID = :id",
{ id: id },
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
const resultVal = result.rows[0].B;
if (specialStr === null) {
assert.strictEqual(resultVal, null);
} else {
assert.deepStrictEqual(resultVal, insertContent);
}
};
it('87.2.1 works with NULL value', async function() {
const id = insertID++;
const content = null;
await insertAndFetch(id, null, content);
}); // 87.2.1
it('87.2.2 works with empty Buffer', async function() {
const id = insertID++;
const content = Buffer.from("", "utf-8");
await insertAndFetch(id, null, content);
}); // 87.2.2
it('87.2.3 works with small value', async function() {
const id = insertID++;
const specialStr = '87.2.3';
const contentLength = 20;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.2.3
it('87.2.4 works with (64K - 1) value', async function() {
const id = insertID++;
const specialStr = '87.2.4';
const contentLength = 65535;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.2.4
it('87.2.5 works with (64K + 1) value', async function() {
const id = insertID++;
const specialStr = '87.2.5';
const contentLength = 65537;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.2.5
it('87.2.6 works with (1MB + 1) data', async function() {
const id = insertID++;
const specialStr = '87.2.6';
const contentLength = 1048577; // 1MB + 1
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.2.6
it('87.2.7 works with dbms_lob.substr()', async function() {
const id = insertID++;
const specialStr = '87.2.7';
const contentLength = 200;
const specialStrLength = specialStr.length;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
let result = null;
await insertIntoBlobTable1(id, content);
result = await connection.execute(
"SELECT dbms_lob.substr(B, " + specialStrLength + ", 1) AS B1 from nodb_blob1 WHERE ID = :id",
{ id: id },
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
const resultVal = result.rows[0].B1;
const buffer2Compare = Buffer.from(specialStr, "utf-8");
assert.deepStrictEqual(resultVal, buffer2Compare);
}); // 87.2.7
it('87.2.8 works with EMPTY_BLOB()', async function() {
const id = insertID++;
const content = "EMPTY_BLOB";
await insertAndFetch(id, null, content);
}); // 87.2.8
it('87.2.9 fetch multiple BLOB rows as Buffer', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.2.9_1';
const contentLength_1 = 200;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.2.9_2';
const contentLength_2 = 100;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
let result = null;
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE ID = " + id_1 + " or id = " + id_2,
{ },
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.deepStrictEqual(result.rows[0].B, content_1);
assert.deepStrictEqual(result.rows[1].B, content_2);
}); // 87.2.9
it('87.2.10 fetch the same BLOB column multiple times', async function() {
const id = insertID++;
const specialStr = '87.2.10';
const contentLength = 200;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
let result = null;
await insertIntoBlobTable1(id, content);
result = await connection.execute(
"SELECT ID, B AS B1, B AS B2 from nodb_blob1 WHERE ID = " + id,
{ },
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.deepStrictEqual(result.rows[0].B1, content);
assert.deepStrictEqual(result.rows[0].B2, content);
}); // 87.2.10
it('87.2.11 works with update statement', async function() {
const id = insertID++;
const specialStr_1 = '87.2.11_1';
const contentLength_1 = 201;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const specialStr_2 = '87.2.11_2';
const contentLength_2 = 208;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
let result = null;
await insertAndFetch(id, specialStr_1, content_1);
await updateBlobTable1(id, content_2);
result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE ID = " + id,
{ },
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.deepStrictEqual(result.rows[0].B, content_2);
}); // 87.2.11
it('87.2.12 works with REF CURSOR', async function() {
const id = insertID++;
const specialStr = '87.2.12';
const contentLength = 100;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertIntoBlobTable1(id, content);
const ref_proc = "CREATE OR REPLACE PROCEDURE nodb_ref(blob_cursor OUT SYS_REFCURSOR)\n" +
"AS \n" +
"BEGIN \n" +
" OPEN blob_cursor FOR \n" +
" SELECT B from nodb_blob1 WHERE ID = " + id + "; \n" +
"END;";
await connection.execute(ref_proc);
const sql = "BEGIN nodb_ref(:b); END;";
const bindVar = {
b: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
};
const result = await connection.execute(sql, bindVar);
const rows = await result.outBinds.b.getRows(3);
assert.deepStrictEqual(rows[0][0], content);
await result.outBinds.b.close();
const ref_proc_drop = "DROP PROCEDURE nodb_ref";
await connection.execute(ref_proc_drop);
}); // 87.2.12
it('87.2.13 fetch BLOB with stream', async function() {
const id = insertID++;
const specialStr = '87.2.13';
const contentLength = 200;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
let result = null;
await insertIntoBlobTable1(id, content);
oracledb.fetchAsBuffer = [];
result = await connection.execute("SELECT B from nodb_blob1 WHERE ID = " + id);
const lob = result.rows[0][0];
let blobData = Buffer.alloc(0);
await new Promise((resolve, reject) => {
lob.on('error', reject);
lob.on('end', lob.destroy);
lob.on('close', resolve);
lob.on('data', function(chunk) {
blobData = Buffer.concat([blobData, chunk]);
});
});
assert.deepStrictEqual(blobData, content);
}); // 87.2.13
it('87.2.14 works with setting oracledb.maxRows < actual number of rows in the table', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.2.14_1';
const contentLength_1 = 200;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.2.14_2';
const contentLength_2 = 100;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
const maxRowsBak = oracledb.maxRows;
oracledb.maxRows = 1;
let result = null;
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE id = " + id_1 + " or id = " + id_2,
{ },
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.strictEqual(result.rows.length, 1);
assert.deepStrictEqual(result.rows[0].B, content_1);
oracledb.maxRows = maxRowsBak;
}); // 87.2.14
it('87.2.15 works with setting oracledb.maxRows > actual number of rows in the table', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.2.15_1';
const contentLength_1 = 200;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.2.15_2';
const contentLength_2 = 100;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
const maxRowsBak = oracledb.maxRows;
oracledb.maxRows = 10;
let result = null;
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE id = " + id_1 + " or id = " + id_2,
{ },
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.strictEqual(result.rows.length, 2);
assert.deepStrictEqual(result.rows[0].B, content_1);
assert.deepStrictEqual(result.rows[1].B, content_2);
oracledb.maxRows = maxRowsBak;
}); // 87.2.15
it('87.2.16 override oracledb.fetchAsBuffer with fetchInfo set to oracledb.DEFAULT', async function() {
const id = insertID++;
const specialStr = '87.2.16';
const contentLength = 20;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertIntoBlobTable1(id, content);
const result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE ID = :id",
{ id: id },
{
fetchInfo: { B: { type: oracledb.DEFAULT } }
});
const lob = result.rows[0][1];
const blobData = await lob.getData();
await lob.close();
assert.deepStrictEqual(blobData, content);
}); // 87.2.16
}); // 87.2
describe('87.3 fetch BLOB columns by setting oracledb.fetchAsBuffer, outFormat = oracledb.OUT_FORMAT_OBJECT and resultSet = true', function() {
before('Create table and populate', async function() {
await connection.execute(proc_create_table1);
}); // before
after('drop table', async function() {
await connection.execute(drop_table1);
}); // after
beforeEach('set oracledb.fetchAsBuffer', function() {
oracledb.fetchAsBuffer = [ oracledb.BLOB ];
}); // beforeEach
afterEach('clear the by-type specification', function() {
oracledb.fetchAsBuffer = [];
}); // afterEach
const insertAndFetch = async function(id, specialStr, insertContent) {
await insertIntoBlobTable1(id, insertContent);
const result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE ID = :id",
{ id: id },
{
outFormat: oracledb.OUT_FORMAT_OBJECT,
resultSet: true
});
const row = await result.resultSet.getRow();
const resultVal = row.B;
if (specialStr === null) {
assert.strictEqual(resultVal, null);
} else {
assert.deepStrictEqual(resultVal, insertContent);
}
await result.resultSet.close();
};
it('87.3.1 works with NULL value', async function() {
const id = insertID++;
const content = null;
await insertAndFetch(id, null, content);
}); // 87.3.1
it('87.3.2 works with empty Buffer', async function() {
const id = insertID++;
const content = Buffer.from("", "utf-8");
await insertAndFetch(id, null, content);
}); // 87.3.2
it('87.3.3 works with small value', async function() {
const id = insertID++;
const specialStr = '87.3.3';
const contentLength = 20;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.3.3
it('87.3.4 works with (64K - 1) value', async function() {
const id = insertID++;
const specialStr = '87.3.4';
const contentLength = 65535;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.3.4
it('87.3.5 works with (64K + 1) value', async function() {
const id = insertID++;
const specialStr = '87.3.5';
const contentLength = 65537;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.3.5
it('87.3.6 works with (1MB + 1) data', async function() {
const id = insertID++;
const specialStr = '87.3.6';
const contentLength = 1048577; // 1MB + 1
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertAndFetch(id, specialStr, content);
}); // 87.3.6
it('87.3.7 works with dbms_lob.substr()', async function() {
const id = insertID++;
const specialStr = '87.3.7';
const contentLength = 200;
const specialStrLength = specialStr.length;
const strBuf = random.getRandomString(contentLength, specialStr);
const content = Buffer.from(strBuf, "utf-8");
await insertIntoBlobTable1(id, content);
const result = await connection.execute(
"SELECT dbms_lob.substr(B, " + specialStrLength + ", 1) AS B1 from nodb_blob1 WHERE ID = :id",
{ id: id },
{
outFormat: oracledb.OUT_FORMAT_OBJECT,
resultSet: true
});
const row = await result.resultSet.getRow();
const buffer2Compare = Buffer.from(specialStr, "utf-8");
assert.deepStrictEqual(row.B1, buffer2Compare);
await result.resultSet.close();
}); // 87.3.7
it('87.3.8 works with EMPTY_BLOB()', async function() {
const id = insertID++;
const content = "EMPTY_BLOB";
await insertAndFetch(id, null, content);
}); // 87.3.8
it('87.3.9 fetch multiple BLOB rows as Buffer', async function() {
const id_1 = insertID++;
const specialStr_1 = '87.3.9_1';
const contentLength_1 = 200;
const strBuf_1 = random.getRandomString(contentLength_1, specialStr_1);
const content_1 = Buffer.from(strBuf_1, "utf-8");
const id_2 = insertID++;
const specialStr_2 = '87.3.9_2';
const contentLength_2 = 100;
const strBuf_2 = random.getRandomString(contentLength_2, specialStr_2);
const content_2 = Buffer.from(strBuf_2, "utf-8");
let result = null;
await insertIntoBlobTable1(id_1, content_1);
await insertIntoBlobTable1(id_2, content_2);
const rowNumFetched = 2;
result = await connection.execute(
"SELECT ID, B from nodb_blob1 WHERE ID = " + id_1 + " or id = " + id_2,
{ },
{
outFormat: oracledb.OUT_FORMAT_OBJECT,
resultSet: true
});
const row = await result.resultSet.getRows(
rowNumFetched);
assert.deepStrictEqual(row[0].B, content_1);
assert.deepStrictEqual(row[1].B, content_2);
await result.resultSet.close();
}); // 87.3.9