UNPKG

7.77 kBJavaScriptView Raw
1// Copyright (c) 2022, 2024, 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//-----------------------------------------------------------------------------
26
27'use strict';
28
29const constants = require('./constants.js');
30const errors = require('./errors.js');
31const types = require('./types.js');
32const nodbUtil = require("./util.js");
33const vector = require('./impl/datahandlers/vector.js');
34
35class Settings {
36
37 constructor() {
38 this.autoCommit = false;
39 this.connectionClass = '';
40 this.dbObjectAsPojo = false;
41 this.edition = '';
42 this.errorOnConcurrentExecute = false;
43 this.events = false;
44 this.externalAuth = false;
45 this.fetchArraySize = 100;
46 this.fetchAsBuffer = [];
47 this.fetchAsString = [];
48 this.lobPrefetchSize = 16384;
49 this.maxRows = 0;
50 this.outFormat = constants.OUT_FORMAT_ARRAY;
51 this.poolIncrement = 1;
52 this.poolMax = 4;
53 this.poolMaxPerShard = 0;
54 this.poolMin = 0;
55 this.poolPingInterval = 60;
56 this.poolPingTimeout = 5000;
57 this.poolTimeout = 60;
58 this.prefetchRows = 2;
59 this.queueTimeout = 60000;
60 this.queueMax = 500;
61 this.stmtCacheSize = 30;
62 this.thin = true;
63 this.thinDriverInitialized = false;
64 this.createFetchTypeMap(this.fetchAsString, this.fetchAsBuffer);
65 this.fetchTypeHandler = undefined;
66 }
67
68 //---------------------------------------------------------------------------
69 // _getDateComponents()
70 //
71 // Returns the components of a date. DATE and TIMESTAMP data from the
72 // database are returned as though they used the JavaScript time zone
73 // setting. TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE data
74 // are returned in native JavaScript format (since they contain time zone
75 // information). This is used only in Thick mode (from node-oracledb 6.0.0).
76 //---------------------------------------------------------------------------
77 _getDateComponents(useLocal, date) {
78 if (useLocal) {
79 return [
80 date.getFullYear(),
81 date.getMonth() + 1,
82 date.getDate(),
83 date.getHours(),
84 date.getMinutes(),
85 date.getSeconds(),
86 date.getMilliseconds() * 1000 * 1000
87 ];
88 } else {
89 return [
90 date.getUTCFullYear(),
91 date.getUTCMonth() + 1,
92 date.getUTCDate(),
93 date.getUTCHours(),
94 date.getUTCMinutes(),
95 date.getUTCSeconds(),
96 date.getUTCMilliseconds() * 1000 * 1000
97 ];
98 }
99 }
100
101 //---------------------------------------------------------------------------
102 // _makeDate()
103 //
104 // Returns a date from the given components. DATE and TIMESTAMP data from the
105 // database are returned as though they used the JavaScript time zone
106 // setting. TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE data
107 // are returned in native JavaScript format (since they contain time zone
108 // information).
109 //---------------------------------------------------------------------------
110 _makeDate(useLocal, year, month, day, hour, minute, second, fseconds, offset) {
111 return nodbUtil.makeDate(useLocal, year, month, day, hour, minute, second, fseconds, offset);
112 }
113
114 //---------------------------------------------------------------------------
115 // _decodeVector()
116 //
117 // Returns a typed array by decoding buffer.
118 //
119 //---------------------------------------------------------------------------
120 _decodeVector(buffer) {
121 const decoder = new vector.VectorDecoder(buffer);
122 return decoder.decode();
123 }
124
125 //---------------------------------------------------------------------------
126 // _encodeVector()
127 //
128 // Create a Vector image from typedarray
129 //
130 //---------------------------------------------------------------------------
131 _encodeVector(value) {
132 const encoder = new vector.VectorEncoder();
133 return encoder.encode(value);
134 }
135
136 //---------------------------------------------------------------------------
137 // addToOptions()
138 //
139 // Adds the named settingsto the options, if no option has already been
140 // specified.
141 //---------------------------------------------------------------------------
142 addToOptions(options) {
143 for (let i = 1; i < arguments.length; i++) {
144 const key = arguments[i];
145 if (options[key] === undefined)
146 options[key] = this[key];
147 }
148 }
149
150 //---------------------------------------------------------------------------
151 // createFetchTypeMap()
152 //
153 // Creates the fetch type map. This overrides the default fetch type mapping
154 // used by the driver with the contents of the fetchAsString and
155 // fetchAsBuffer arrays. The error checking is performed here as well in
156 // order to eliminate repeated code.
157 // ---------------------------------------------------------------------------
158 createFetchTypeMap(fetchAsString, fetchAsBuffer) {
159
160 // create a copy of the default fetch type map
161 const map = new Map(types.DB_TYPE_FETCH_TYPE_MAP);
162
163 // adjust map for fetchAsString settings
164 for (const element of fetchAsString) {
165 switch (element) {
166 case types.DB_TYPE_NUMBER:
167 map.set(types.DB_TYPE_BINARY_DOUBLE, types.DB_TYPE_VARCHAR);
168 map.set(types.DB_TYPE_BINARY_FLOAT, types.DB_TYPE_VARCHAR);
169 map.set(types.DB_TYPE_BINARY_INTEGER, types.DB_TYPE_VARCHAR);
170 map.set(types.DB_TYPE_NUMBER, types.DB_TYPE_VARCHAR);
171 break;
172 case types.DB_TYPE_TIMESTAMP:
173 map.set(types.DB_TYPE_DATE, types.DB_TYPE_VARCHAR);
174 map.set(types.DB_TYPE_TIMESTAMP, types.DB_TYPE_VARCHAR);
175 map.set(types.DB_TYPE_TIMESTAMP_TZ, types.DB_TYPE_VARCHAR);
176 map.set(types.DB_TYPE_TIMESTAMP_LTZ, types.DB_TYPE_VARCHAR);
177 break;
178 case types.DB_TYPE_CLOB:
179 case types.DB_TYPE_NCLOB:
180 map.set(types.DB_TYPE_CLOB, types.DB_TYPE_LONG);
181 map.set(types.DB_TYPE_NCLOB, types.DB_TYPE_LONG_NVARCHAR);
182 break;
183 case types.DB_TYPE_VECTOR:
184 map.set(types.DB_TYPE_VECTOR, types.DB_TYPE_LONG);
185 break;
186 case types.DB_TYPE_RAW:
187 map.set(types.DB_TYPE_RAW, types.DB_TYPE_VARCHAR);
188 break;
189 case types.DB_TYPE_JSON:
190 map.set(types.DB_TYPE_JSON, types.DB_TYPE_VARCHAR);
191 break;
192 default:
193 errors.throwErr(errors.ERR_INVALID_TYPE_FOR_CONVERSION);
194 }
195 }
196
197 // adjust map for fetchAsBuffer settings
198 for (const element of fetchAsBuffer) {
199 switch (element) {
200 case types.DB_TYPE_BLOB:
201 map.set(types.DB_TYPE_BLOB, types.DB_TYPE_LONG_RAW);
202 break;
203 default:
204 errors.throwErr(errors.ERR_INVALID_TYPE_FOR_CONVERSION);
205 }
206 }
207
208 // assign calculated fetchTypeMap for later use
209 this.fetchTypeMap = map;
210
211 }
212
213}
214
215module.exports = new Settings();