Skip to content

Commit b996fc5

Browse files
committed
Update Date and Timestamp examples and README file
1 parent 741a053 commit b996fc5

File tree

3 files changed

+179
-16
lines changed

3 files changed

+179
-16
lines changed

examples/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ To run the examples:
3232
Many examples can be run in either node-oracledb Thin (the default) or Thick
3333
modes. Thin mode is a pure JavaScript implementation of node-oracledb.
3434
Setting the environment variable `NODE_ORACLEDB_DRIVER_MODE` to `'thick'` will
35-
change node-oracledb to use Thick mode.
35+
make examples use Thick mode.
3636

3737
## Example Overview
3838

@@ -51,7 +51,8 @@ File Name | Description
5151
[`connectionpool.js`](connectionpool.js) | Basic example creating a pool of connections
5252
[`cqn1.js`](cqn1.js) | Basic Continuous Query Notification (CQN) example
5353
[`cqn2.js`](cqn2.js) | Continuous Query Notification with notification grouping
54-
[`date.js`](date.js) | Show some DATE and TIMESTAMP behaviors
54+
[`date_timestamp1.js`](date_timestamp1.js) | Show some basic DATE and TIMESTAMP behaviors
55+
[`date_timestamp2.js`](date_timestamp2.js) | Show some DATE and TIMESTAMP behaviors with timezones
5556
[`dbconfig.js`](dbconfig.js) | Common file used by examples for setting connection credentials
5657
[`dbmsoutputgetline.js`](dbmsoutputgetline.js) | Show fetching DBMS_OUTPUT by binding buffers
5758
[`dbmsoutputpipe.js`](dbmsoutputpipe.js) | Show fetching DBMS_OUTPUT by using a pipelined table

examples/date.js renamed to examples/date_timestamp1.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
* limitations under the License.
2424
*
2525
* NAME
26-
* date.js
26+
* date_timestamp1.js
2727
*
2828
* DESCRIPTION
2929
* Insert and query DATE and TIMESTAMP columns.
3030
*
3131
* When bound in an INSERT, JavaScript Dates are inserted using
32-
* TIMESTAMP WITH LOCAL TIMEZONE. Similarly for queries, TIMESTAMP
33-
* and DATE columns are fetched as TIMESTAMP WITH LOCAL TIMEZONE.
32+
* TIMESTAMP unless explicitly bound as another type.
33+
* Similarly for queries, TIMESTAMP and DATE columns are fetched
34+
* as TIMESTAMP WITH LOCAL TIMEZONE.
3435
*
3536
*****************************************************************************///
3637

@@ -83,11 +84,11 @@ async function run() {
8384
`DROP TABLE no_datetab`,
8485

8586
`CREATE TABLE no_datetab(
86-
id NUMBER,
87-
timestampcol TIMESTAMP,
88-
timestamptz TIMESTAMP WITH TIME ZONE,
89-
timestampltz TIMESTAMP WITH LOCAL TIME ZONE,
90-
datecol DATE)`
87+
id NUMBER,
88+
timestampcol TIMESTAMP,
89+
timestamptz TIMESTAMP WITH TIME ZONE,
90+
timestampltz TIMESTAMP WITH LOCAL TIME ZONE,
91+
datecol DATE)`
9192
];
9293

9394
for (const s of stmts) {
@@ -104,16 +105,16 @@ async function run() {
104105
console.log('Inserting JavaScript date: ' + date);
105106
result = await connection.execute(
106107
`INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
107-
VALUES (1, :ts, :tstz, :tsltz, :td)`,
108+
VALUES (1, :ts, :tstz, :tsltz, :td)`,
108109
{ ts: date, tstz: date, tsltz: date, td: date });
109110
console.log('Rows inserted: ' + result.rowsAffected);
110111

111112
console.log('Query Results:');
112113
result = await connection.execute(
113114
`SELECT id, timestampcol, timestamptz, timestampltz, datecol,
114115
TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
115-
FROM no_datetab
116-
ORDER BY id`);
116+
FROM no_datetab
117+
ORDER BY id`);
117118
console.log(result.rows);
118119

119120
console.log('Altering session time zone');
@@ -123,16 +124,16 @@ async function run() {
123124
console.log('Inserting JavaScript date: ' + date);
124125
result = await connection.execute(
125126
`INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
126-
VALUES (2, :ts, :tstz, :tsltz, :td)`,
127+
VALUES (2, :ts, :tstz, :tsltz, :td)`,
127128
{ ts: date, tstz: date, tsltz: date, td: date });
128129
console.log('Rows inserted: ' + result.rowsAffected);
129130

130131
console.log('Query Results:');
131132
result = await connection.execute(
132133
`SELECT id, timestampcol, timestamptz, timestampltz, datecol,
133134
TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
134-
FROM no_datetab
135-
ORDER BY id`);
135+
FROM no_datetab
136+
ORDER BY id`);
136137
console.log(result.rows);
137138

138139
// Show the queried dates are of type Date

examples/date_timestamp2.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/* Copyright (c) 2015, 2023, Oracle and/or its affiliates. */
2+
3+
/******************************************************************************
4+
*
5+
* This software is dual-licensed to you under the Universal Permissive License
6+
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
7+
* 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
8+
* either license.
9+
*
10+
* If you elect to accept the software under the Apache License, Version 2.0,
11+
* the following applies:
12+
*
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
*
17+
* https://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
*
25+
* NAME
26+
* date_timestamp2.js
27+
*
28+
* DESCRIPTION
29+
* Insert and query DATE and TIMESTAMP columns (one date in winter
30+
* and one date in summer) to test Daylight Savings Time (DST) settings.
31+
*
32+
* When bound in an INSERT, JavaScript Dates are inserted using
33+
* TIMESTAMP unless explicitly bound as another type.
34+
* Similarly for queries, TIMESTAMP and DATE columns are fetched
35+
* as TIMESTAMP WITH LOCAL TIMEZONE.
36+
*
37+
*****************************************************************************///
38+
39+
'use strict';
40+
41+
Error.stackTraceLimit = 50;
42+
43+
// Using a fixed Oracle time zone helps avoid machine and deployment differences
44+
// process.env.ORA_SDTZ = 'UTC';
45+
46+
const oracledb = require('oracledb');
47+
const dbConfig = require('./dbconfig.js');
48+
49+
// This example runs in both node-oracledb Thin and Thick modes.
50+
//
51+
// Optionally run in node-oracledb Thick mode
52+
if (process.env.NODE_ORACLEDB_DRIVER_MODE === 'thick') {
53+
// Thick mode requires Oracle Client or Oracle Instant Client libraries. On
54+
// Windows and macOS Intel you can specify the directory containing the
55+
// libraries at runtime or before Node.js starts. On other platforms (where
56+
// Oracle libraries are available) the system library search path must always
57+
// include the Oracle library path before Node.js starts. If the search path
58+
// is not correct, you will get a DPI-1047 error. See the node-oracledb
59+
// installation documentation.
60+
let clientOpts = {};
61+
if (process.platform === 'win32') { // Windows
62+
// clientOpts = { libDir: 'C:\\oracle\\instantclient_19_17' };
63+
} else if (process.platform === 'darwin' && process.arch === 'x64') { // macOS Intel
64+
clientOpts = { libDir: process.env.HOME + '/Downloads/instantclient_19_8' };
65+
}
66+
oracledb.initOracleClient(clientOpts); // enable node-oracledb Thick mode
67+
}
68+
69+
console.log(oracledb.thin ? 'Running in thin mode' : 'Running in thick mode');
70+
71+
oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT;
72+
73+
async function run() {
74+
75+
let connection;
76+
77+
try {
78+
let result, date;
79+
80+
connection = await oracledb.getConnection(dbConfig);
81+
console.log('Creating table');
82+
const stmts = [
83+
`DROP TABLE no_datetab`,
84+
85+
`CREATE TABLE no_datetab(
86+
id NUMBER,
87+
timestampcol TIMESTAMP,
88+
timestamptz TIMESTAMP WITH TIME ZONE,
89+
timestampltz TIMESTAMP WITH LOCAL TIME ZONE,
90+
datecol DATE)`
91+
];
92+
93+
for (const s of stmts) {
94+
try {
95+
await connection.execute(s);
96+
} catch (e) {
97+
if (e.errorNum != 942)
98+
console.error(e);
99+
}
100+
}
101+
102+
// Convert the fetched timestamp data to reflect
103+
// the locale time settings of the client
104+
oracledb.fetchTypeHandler = function(metadata) {
105+
if (metadata.dbType === oracledb.DB_TYPE_DATE ||
106+
metadata.dbType === oracledb.DB_TYPE_TIMESTAMP ||
107+
metadata.dbType === oracledb.DB_TYPE_TIMESTAMP_LTZ ||
108+
metadata.dbType === oracledb.DB_TYPE_TIMESTAMP_TZ)
109+
return {converter: (v) => v.toLocaleString() };
110+
};
111+
112+
date = new Date(2000, 11, 17); // 17th Dec 2000
113+
console.log('Inserting JavaScript date: ' + date);
114+
result = await connection.execute(
115+
`INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
116+
VALUES (1, :ts, :tstz, :tsltz, :td)`,
117+
{ ts: date, tstz: date, tsltz: date, td: date });
118+
console.log('Rows inserted: ' + result.rowsAffected);
119+
120+
date = new Date(2000, 3, 25); // 25th Apr 2000
121+
console.log('Inserting JavaScript date: ' + date);
122+
result = await connection.execute(
123+
`INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
124+
VALUES (2, :ts, :tstz, :tsltz, :td)`,
125+
{ ts: date, tstz: date, tsltz: date, td: date });
126+
console.log('Rows inserted: ' + result.rowsAffected);
127+
128+
console.log('Query Results:');
129+
result = await connection.execute(
130+
`SELECT id, timestampcol, timestamptz, timestampltz, datecol,
131+
TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
132+
FROM no_datetab
133+
ORDER BY id`);
134+
console.log(result.rows);
135+
136+
console.log('Altering session time zone');
137+
// await connection.execute(`ALTER SESSION SET TIME_ZONE='+5:00'`);
138+
await connection.execute(`ALTER SESSION SET TIME_ZONE='America/Chicago'`); // resets ORA_SDTZ value
139+
140+
console.log('Query Results:');
141+
result = await connection.execute(
142+
`SELECT id, timestampcol, timestamptz, timestampltz, datecol,
143+
TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
144+
FROM no_datetab
145+
ORDER BY id`);
146+
console.log(result.rows);
147+
148+
} catch (err) {
149+
console.error(err);
150+
} finally {
151+
if (connection) {
152+
try {
153+
await connection.close();
154+
} catch (err) {
155+
console.error(err);
156+
}
157+
}
158+
}
159+
}
160+
161+
run();

0 commit comments

Comments
 (0)