@@ -317,7 +317,7 @@ describe('271. fetchTypeHandler.js', function() {
317
317
if ( connection . oracleServerVersion < 2100000000 || testsUtil . getClientVersion ( ) < 2100000000 ) {
318
318
this . skip ( ) ;
319
319
}
320
-
320
+ const TABLE = 'jsondata' ;
321
321
oracledb . fetchTypeHandler = function ( ) {
322
322
const myConverter = ( v ) => {
323
323
v . empId = 10 ;
@@ -326,21 +326,21 @@ describe('271. fetchTypeHandler.js', function() {
326
326
return { converter : myConverter } ;
327
327
} ;
328
328
329
- const createTable = ( `CREATE TABLE jsondata (
329
+ const createTable = ( `CREATE TABLE ${ TABLE } (
330
330
obj_data JSON
331
331
)
332
332
` ) ;
333
- const plsql = testsUtil . sqlCreateTable ( 'jsondata' , createTable ) ;
333
+ const plsql = testsUtil . sqlCreateTable ( TABLE , createTable ) ;
334
334
await connection . execute ( plsql ) ;
335
335
336
- const sql = `INSERT into jsondata VALUES ('{"empId": 1, "empName": "Employee1", "city": "New City"}')` ;
336
+ const sql = `INSERT into ${ TABLE } VALUES ('{"empId": 1, "empName": "Employee1", "city": "New City"}')` ;
337
337
await connection . execute ( sql ) ;
338
338
339
- const result = await connection . execute ( " select * from jsondata" ) ;
339
+ const result = await connection . execute ( ` select * from ${ TABLE } ` ) ;
340
340
assert . strictEqual ( result . rows [ 0 ] [ 0 ] [ "empId" ] , 10 ) ;
341
341
assert . strictEqual ( result . rows [ 0 ] [ 0 ] [ "empName" ] , 'Employee1' ) ;
342
342
assert . strictEqual ( result . rows [ 0 ] [ 0 ] [ "city" ] , 'New City' ) ;
343
- await connection . execute ( `DROP TABLE jsondata PURGE` ) ;
343
+ await connection . execute ( testsUtil . sqlDropTable ( TABLE ) ) ;
344
344
} ) ;
345
345
346
346
/*
@@ -384,4 +384,104 @@ describe('271. fetchTypeHandler.js', function() {
384
384
assert . strictEqual ( result . rows [ 0 ] . TS_NUM , numResults [ numStrs . indexOf ( element ) ] ) ;
385
385
}
386
386
} ) ;
387
- } ) ;
387
+
388
+ it ( '271.20 setting a private property in the metadata' , async function ( ) {
389
+ oracledb . fetchTypeHandler = function ( metadata ) {
390
+ metadata . _privateProp = 'I am a private property of ' + metadata . name ;
391
+ } ;
392
+
393
+ const sql = `select 5 as "MyId", 6 as "MyValue", 'A string' as "MyString" from dual` ;
394
+ const result = await connection . execute ( sql ) ;
395
+
396
+ assert . strictEqual ( result . metaData [ 0 ] . _privateProp , "I am a private property of MyId" ) ;
397
+ assert . strictEqual ( result . metaData [ 1 ] . _privateProp , "I am a private property of MyValue" ) ;
398
+ assert . strictEqual ( result . metaData [ 2 ] . _privateProp , "I am a private property of MyString" ) ;
399
+ } ) ;
400
+
401
+ it ( '271.21 fetchTypeHandler for nulls with converter function' , async function ( ) {
402
+ oracledb . fetchTypeHandler = function ( ) {
403
+ const myConverter = ( v ) => {
404
+ return String ( v ) ;
405
+ } ;
406
+ return { converter : myConverter } ;
407
+ } ;
408
+
409
+ const sql = `SELECT NULL FROM DUAL` ;
410
+ const result = await connection . execute ( sql ) ;
411
+ assert . strictEqual ( result . rows [ 0 ] [ 0 ] , "null" ) ;
412
+ } ) ;
413
+
414
+ it ( '271.22 converter function to convert column val to string' , async function ( ) {
415
+ const TABLE = 't' ;
416
+ const sql = `CREATE TABLE ${ TABLE } (n_col NUMBER)` ;
417
+ const plsql = testsUtil . sqlCreateTable ( TABLE , sql ) ;
418
+ await connection . execute ( plsql ) ;
419
+ const inssql = `INSERT INTO ${ TABLE } (n_col) VALUES (:bv)` ;
420
+ await connection . execute ( inssql , { bv : 123 } ) ;
421
+
422
+ function fetchTypeHandlerFunc ( metadata ) {
423
+ if ( metadata . dbType === oracledb . DB_TYPE_NUMBER ) {
424
+ return { converter : convertToString } ;
425
+ }
426
+ } ;
427
+
428
+ async function convertToString ( val ) {
429
+ if ( val !== null ) {
430
+ val = 'abc' ;
431
+ }
432
+ return val ;
433
+ }
434
+
435
+ const result = await connection . execute (
436
+ `select * from ${ TABLE } ` ,
437
+ [ ] ,
438
+ {
439
+ fetchTypeHandler : fetchTypeHandlerFunc ,
440
+ outFormat : oracledb . OUT_FORMAT_OBJECT
441
+ }
442
+ ) ;
443
+
444
+ assert . strictEqual ( result . rows [ 0 ] . N_COL , 'abc' ) ;
445
+ await connection . execute ( testsUtil . sqlDropTable ( TABLE ) ) ;
446
+ } ) ;
447
+
448
+ it ( '271.23 converter function with multiple columns' , async function ( ) {
449
+ await connection . execute ( "alter session set time_zone = '+0:00'" ) ;
450
+
451
+ oracledb . fetchTypeHandler = function ( metadata ) {
452
+ if ( metadata . dbTypeName === "TIMESTAMP" ) {
453
+ return { type : oracledb . DATE } ;
454
+ }
455
+ else if ( metadata . dbTypeName === "NUMBER" ) {
456
+ return { type : oracledb . STRING } ;
457
+ }
458
+ } ;
459
+ const TABLE = 'my_table' ;
460
+ const sql = `CREATE TABLE ${ TABLE } (
461
+ id NUMBER,
462
+ name VARCHAR2(50),
463
+ age NUMBER,
464
+ created_date TIMESTAMP
465
+ )` ;
466
+ const plsql = testsUtil . sqlCreateTable ( TABLE , sql ) ;
467
+ await connection . execute ( plsql ) ;
468
+
469
+ await connection . execute ( `INSERT INTO ${ TABLE } values (01, 'ABC', 23,
470
+ TO_TIMESTAMP('2023-04-27 10:30:00', 'YYYY-MM-DD HH24:MI:SS'))` ) ;
471
+ const result = await connection . execute ( `
472
+ SELECT id, name, age,
473
+ created_date AS TS_DATE FROM ${ TABLE } ` ,
474
+ [ ] ,
475
+ {
476
+ outFormat : oracledb . OUT_FORMAT_OBJECT
477
+ }
478
+ ) ;
479
+
480
+ assert . deepEqual ( Object . getOwnPropertyNames ( result . rows [ 0 ] ) , [ "ID" , "NAME" , "AGE" , "TS_DATE" ] ) ;
481
+ assert . deepEqual ( result . rows [ 0 ] . ID , "1" ) ;
482
+ assert . deepEqual ( result . rows [ 0 ] . NAME , "ABC" ) ;
483
+ assert . deepEqual ( result . rows [ 0 ] . AGE , "23" ) ;
484
+ assert . deepEqual ( result . rows [ 0 ] . TS_DATE , new Date ( '2023-04-27T10:30:00.000Z' ) ) ;
485
+ await connection . execute ( testsUtil . sqlDropTable ( TABLE ) ) ;
486
+ } ) ;
487
+ } ) ;
0 commit comments