|
| 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