3535 *
3636 *****************************************************************************/ //
3737
38- 'use strict' ;
39-
40- Error . stackTraceLimit = 50 ;
41-
42- // Using a fixed Oracle time zone helps avoid machine and deployment differences
43- // process.env.ORA_SDTZ = 'UTC';
44-
45- const oracledb = require ( 'oracledb' ) ;
46- const dbConfig = require ( './dbconfig.js' ) ;
47-
48- // This example runs in both node-oracledb Thin and Thick modes.
49- //
50- // Optionally run in node-oracledb Thick mode
51- if ( process . env . NODE_ORACLEDB_DRIVER_MODE === 'thick' ) {
52- // Thick mode requires Oracle Client or Oracle Instant Client libraries. On
53- // Windows and macOS Intel you can specify the directory containing the
54- // libraries at runtime or before Node.js starts. On other platforms (where
55- // Oracle libraries are available) the system library search path must always
56- // include the Oracle library path before Node.js starts. If the search path
57- // is not correct, you will get a DPI-1047 error. See the node-oracledb
58- // installation documentation.
59- let clientOpts = { } ;
60- if ( process . platform === 'win32' ) { // Windows
61- // clientOpts = { libDir: 'C:\\oracle\\instantclient_19_17' };
62- } else if ( process . platform === 'darwin' && process . arch === 'x64' ) { // macOS Intel
63- clientOpts = { libDir : process . env . HOME + '/Downloads/instantclient_19_8' } ;
64- }
65- oracledb . initOracleClient ( clientOpts ) ; // enable node-oracledb Thick mode
66- }
67-
68- console . log ( oracledb . thin ? 'Running in thin mode' : 'Running in thick mode' ) ;
69-
70- oracledb . outFormat = oracledb . OUT_FORMAT_OBJECT ;
71-
72- async function run ( ) {
73-
74- let connection ;
75-
76- try {
77- let result , date ;
78-
79- connection = await oracledb . getConnection ( dbConfig ) ;
80-
81- console . log ( 'Creating table' ) ;
82-
83- const stmts = [
84- `DROP TABLE no_datetab` ,
85-
86- `CREATE TABLE no_datetab(
87- id NUMBER,
88- timestampcol TIMESTAMP,
89- timestamptz TIMESTAMP WITH TIME ZONE,
90- timestampltz TIMESTAMP WITH LOCAL TIME ZONE,
91- datecol DATE)`
92- ] ;
93-
94- for ( const s of stmts ) {
95- try {
96- await connection . execute ( s ) ;
97- } catch ( e ) {
98- if ( e . errorNum != 942 )
99- console . error ( e ) ;
100- }
101- }
102-
103- // When bound, JavaScript Dates are inserted using TIMESTAMP WITH LOCAL TIMEZONE
104- date = new Date ( 1995 , 11 , 17 ) ; // 17th Dec 1995
105- console . log ( 'Inserting JavaScript date: ' + date ) ;
106- result = await connection . execute (
107- `INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
108- VALUES (1, :ts, :tstz, :tsltz, :td)` ,
109- { ts : date , tstz : date , tsltz : date , td : date } ) ;
110- console . log ( 'Rows inserted: ' + result . rowsAffected ) ;
111-
112- console . log ( 'Query Results:' ) ;
113- result = await connection . execute (
114- `SELECT id, timestampcol, timestamptz, timestampltz, datecol,
115- TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
116- FROM no_datetab
117- ORDER BY id` ) ;
118- console . log ( result . rows ) ;
119-
120- console . log ( 'Altering session time zone' ) ;
121- await connection . execute ( `ALTER SESSION SET TIME_ZONE='+5:00'` ) ; // resets ORA_SDTZ value
122-
123- date = new Date ( ) ; // Current Date
124- console . log ( 'Inserting JavaScript date: ' + date ) ;
125- result = await connection . execute (
126- `INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
127- VALUES (2, :ts, :tstz, :tsltz, :td)` ,
128- { ts : date , tstz : date , tsltz : date , td : date } ) ;
129- console . log ( 'Rows inserted: ' + result . rowsAffected ) ;
130-
131- console . log ( 'Query Results:' ) ;
132- result = await connection . execute (
133- `SELECT id, timestampcol, timestamptz, timestampltz, datecol,
134- TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
135- FROM no_datetab
136- ORDER BY id` ) ;
137- console . log ( result . rows ) ;
138-
139- // Show the queried dates are of type Date
140- let ts = result . rows [ 0 ] [ 'TIMESTAMPCOL' ] ;
141- ts . setDate ( ts . getDate ( ) + 5 ) ;
142- console . log ( 'TIMESTAMP manipulation in JavaScript:' , ts ) ;
143-
144- } catch ( err ) {
145- console . error ( err ) ;
146- } finally {
147- if ( connection ) {
148- try {
149- await connection . close ( ) ;
150- } catch ( err ) {
151- console . error ( err ) ;
152- }
153- }
154- }
155- }
156-
157- run ( ) ;
158-
38+ 'use strict' ;
39+
40+ Error . stackTraceLimit = 50 ;
41+
42+ // Using a fixed Oracle time zone helps avoid machine and deployment differences
43+ // process.env.ORA_SDTZ = 'UTC';
44+
45+ const oracledb = require ( 'oracledb' ) ;
46+ const dbConfig = require ( './dbconfig.js' ) ;
47+
48+ // This example runs in both node-oracledb Thin and Thick modes.
49+ //
50+ // Optionally run in node-oracledb Thick mode
51+ if ( process . env . NODE_ORACLEDB_DRIVER_MODE === 'thick' ) {
52+ // Thick mode requires Oracle Client or Oracle Instant Client libraries. On
53+ // Windows and macOS Intel you can specify the directory containing the
54+ // libraries at runtime or before Node.js starts. On other platforms (where
55+ // Oracle libraries are available) the system library search path must always
56+ // include the Oracle library path before Node.js starts. If the search path
57+ // is not correct, you will get a DPI-1047 error. See the node-oracledb
58+ // installation documentation.
59+ let clientOpts = { } ;
60+ if ( process . platform === 'win32' ) { // Windows
61+ // clientOpts = { libDir: 'C:\\oracle\\instantclient_19_17' };
62+ } else if ( process . platform === 'darwin' && process . arch === 'x64' ) { // macOS Intel
63+ clientOpts = { libDir : process . env . HOME + '/Downloads/instantclient_19_8' } ;
64+ }
65+ oracledb . initOracleClient ( clientOpts ) ; // enable node-oracledb Thick mode
66+ }
67+
68+ console . log ( oracledb . thin ? 'Running in thin mode' : 'Running in thick mode' ) ;
69+
70+ oracledb . outFormat = oracledb . OUT_FORMAT_OBJECT ;
71+
72+ async function run ( ) {
73+
74+ let connection ;
75+
76+ try {
77+ let result , date ;
78+
79+ connection = await oracledb . getConnection ( dbConfig ) ;
80+
81+ console . log ( 'Creating table' ) ;
82+
83+ const stmts = [
84+ `DROP TABLE no_datetab` ,
85+
86+ `CREATE TABLE no_datetab(
87+ id NUMBER,
88+ timestampcol TIMESTAMP,
89+ timestamptz TIMESTAMP WITH TIME ZONE,
90+ timestampltz TIMESTAMP WITH LOCAL TIME ZONE,
91+ datecol DATE)`
92+ ] ;
93+
94+ for ( const s of stmts ) {
95+ try {
96+ await connection . execute ( s ) ;
97+ } catch ( e ) {
98+ if ( e . errorNum != 942 )
99+ console . error ( e ) ;
100+ }
101+ }
102+
103+ // When bound, JavaScript Dates are inserted using TIMESTAMP WITH LOCAL TIMEZONE
104+ date = new Date ( 1995 , 11 , 17 ) ; // 17th Dec 1995
105+ console . log ( 'Inserting JavaScript date: ' + date ) ;
106+ result = await connection . execute (
107+ `INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
108+ VALUES (1, :ts, :tstz, :tsltz, :td)` ,
109+ { ts : date , tstz : date , tsltz : date , td : date } ) ;
110+ console . log ( 'Rows inserted: ' + result . rowsAffected ) ;
111+
112+ console . log ( 'Query Results:' ) ;
113+ result = await connection . execute (
114+ `SELECT id, timestampcol, timestamptz, timestampltz, datecol,
115+ TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
116+ FROM no_datetab
117+ ORDER BY id` ) ;
118+ console . log ( result . rows ) ;
119+
120+ console . log ( 'Altering session time zone' ) ;
121+ await connection . execute ( `ALTER SESSION SET TIME_ZONE='+5:00'` ) ; // resets ORA_SDTZ value
122+
123+ date = new Date ( ) ; // Current Date
124+ console . log ( 'Inserting JavaScript date: ' + date ) ;
125+ result = await connection . execute (
126+ `INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
127+ VALUES (2, :ts, :tstz, :tsltz, :td)` ,
128+ { ts : date , tstz : date , tsltz : date , td : date } ) ;
129+ console . log ( 'Rows inserted: ' + result . rowsAffected ) ;
130+
131+ console . log ( 'Query Results:' ) ;
132+ result = await connection . execute (
133+ `SELECT id, timestampcol, timestamptz, timestampltz, datecol,
134+ TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
135+ FROM no_datetab
136+ ORDER BY id` ) ;
137+ console . log ( result . rows ) ;
138+
139+ // Show the queried dates are of type Date
140+ let ts = result . rows [ 0 ] [ 'TIMESTAMPCOL' ] ;
141+ ts . setDate ( ts . getDate ( ) + 5 ) ;
142+ console . log ( 'TIMESTAMP manipulation in JavaScript:' , ts ) ;
143+
144+ } catch ( err ) {
145+ console . error ( err ) ;
146+ } finally {
147+ if ( connection ) {
148+ try {
149+ await connection . close ( ) ;
150+ } catch ( err ) {
151+ console . error ( err ) ;
152+ }
153+ }
154+ }
155+ }
156+
157+ run ( ) ;
0 commit comments