/
data-types.js
486 lines (409 loc) · 11.8 KB
/
data-types.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved
'use strict';
const moment = require('moment');
const momentTz = require('moment-timezone');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(
undefined,
'https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-D424D23B-0933-425F-BC69-9C0E6724693C'
);
BaseTypes.DATE.types.oracle = ['TIMESTAMP', 'TIMESTAMP WITH LOCAL TIME ZONE'];
BaseTypes.STRING.types.oracle = ['VARCHAR2', 'NVARCHAR2'];
BaseTypes.CHAR.types.oracle = ['CHAR', 'RAW'];
BaseTypes.TEXT.types.oracle = ['CLOB'];
BaseTypes.TINYINT.types.oracle = ['NUMBER'];
BaseTypes.SMALLINT.types.oracle = ['NUMBER'];
BaseTypes.MEDIUMINT.types.oracle = ['NUMBER'];
BaseTypes.INTEGER.types.oracle = ['INTEGER'];
BaseTypes.BIGINT.types.oracle = ['NUMBER'];
BaseTypes.FLOAT.types.oracle = ['BINARY_FLOAT'];
BaseTypes.DATEONLY.types.oracle = ['DATE'];
BaseTypes.BOOLEAN.types.oracle = ['CHAR(1)'];
BaseTypes.BLOB.types.oracle = ['BLOB'];
BaseTypes.DECIMAL.types.oracle = ['NUMBER'];
BaseTypes.UUID.types.oracle = ['VARCHAR2'];
BaseTypes.ENUM.types.oracle = ['VARCHAR2'];
BaseTypes.REAL.types.oracle = ['BINARY_DOUBLE'];
BaseTypes.DOUBLE.types.oracle = ['BINARY_DOUBLE'];
BaseTypes.JSON.types.oracle = ['BLOB'];
BaseTypes.GEOMETRY.types.oracle = false;
class STRING extends BaseTypes.STRING {
toSql() {
if (this.length > 4000 || this._binary && this._length > 2000) {
warn(
'Oracle supports length up to 32764 bytes or characters; Be sure that your administrator has extended the MAX_STRING_SIZE parameter. Check https://docs.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-7B72E154-677A-4342-A1EA-C74C1EA928E6'
);
}
if (!this._binary) {
return `NVARCHAR2(${this._length})`;
}
return `RAW(${this._length})`;
}
_stringify(value, options) {
if (this._binary) {
// For Binary numbers we're converting a buffer to hex then
// sending it over the wire as a string,
// We pass it through escape function to remove un-necessary quotes
// this.format in insert/bulkinsert query calls stringify hence we need to convert binary buffer
// to hex string. Since this block is used by both bind (insert/bulkinsert) and
// non-bind (select query where clause) hence we need to
// have an operation that supports both
return options.escape(value.toString('hex'));
}
return options.escape(value);
}
_getBindDef(oracledb) {
if (this._binary) {
return { type: oracledb.DB_TYPE_RAW, maxSize: this._length };
}
return { type: oracledb.DB_TYPE_VARCHAR, maxSize: this._length };
}
_bindParam(value, options) {
return options.bindParam(value);
}
}
STRING.prototype.escape = false;
class BOOLEAN extends BaseTypes.BOOLEAN {
toSql() {
return 'CHAR(1)';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_CHAR, maxSize: 1 };
}
_stringify(value) {
// If value is true we return '1'
// If value is false we return '0'
// Else we return it as is
// Converting number to char since in bindDef
// the type would be oracledb.DB_TYPE_CHAR
return value === true ? '1' : value === false ? '0' : value;
}
_sanitize(value) {
if (typeof value === 'string') {
// If value is a string we return true if among '1' and 'true'
// We return false if among '0' and 'false'
// Else return the value as is and let the DB raise error for invalid values
return value === '1' || value === 'true' ? true : value === '0' || value === 'false' ? false : value;
}
return super._sanitize(value);
}
}
class UUID extends BaseTypes.UUID {
toSql() {
return 'VARCHAR2(36)';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_VARCHAR, maxSize: 36 };
}
}
class NOW extends BaseTypes.NOW {
toSql() {
return 'SYSDATE';
}
_stringify() {
return 'SYSDATE';
}
}
class ENUM extends BaseTypes.ENUM {
toSql() {
return 'VARCHAR2(512)';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_VARCHAR, maxSize: 512 };
}
}
class TEXT extends BaseTypes.TEXT {
toSql() {
return 'CLOB';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_CLOB };
}
}
class CHAR extends BaseTypes.CHAR {
toSql() {
if (this._binary) {
warn('Oracle CHAR.BINARY datatype is not of Fixed Length.');
return `RAW(${this._length})`;
}
return super.toSql();
}
_getBindDef(oracledb) {
if (this._binary) {
return { type: oracledb.DB_TYPE_RAW, maxSize: this._length };
}
return { type: oracledb.DB_TYPE_CHAR, maxSize: this._length };
}
_bindParam(value, options) {
return options.bindParam(value);
}
}
class DATE extends BaseTypes.DATE {
toSql() {
return 'TIMESTAMP WITH LOCAL TIME ZONE';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_TIMESTAMP_LTZ };
}
_stringify(date, options) {
const format = 'YYYY-MM-DD HH24:MI:SS.FFTZH:TZM';
date = this._applyTimezone(date, options);
const formatedDate = date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
return `TO_TIMESTAMP_TZ('${formatedDate}','${format}')`;
}
_applyTimezone(date, options) {
if (options.timezone) {
if (momentTz.tz.zone(options.timezone)) {
date = momentTz(date).tz(options.timezone);
} else {
date = moment(date).utcOffset(options.timezone);
}
} else {
date = momentTz(date);
}
return date;
}
static parse(value, options) {
if (value === null) {
return value;
}
if (options && moment.tz.zone(options.timezone)) {
value = moment.tz(value.toString(), options.timezone).toDate();
}
return value;
}
/**
* avoids appending TO_TIMESTAMP_TZ in _stringify
*
* @override
*/
_bindParam(value, options) {
return options.bindParam(value);
}
}
DATE.prototype.escape = false;
class DECIMAL extends BaseTypes.DECIMAL {
toSql() {
let result = '';
if (this._length) {
result += `(${this._length}`;
if (typeof this._decimals === 'number') {
result += `,${this._decimals}`;
}
result += ')';
}
if (!this._length && this._precision) {
result += `(${this._precision}`;
if (typeof this._scale === 'number') {
result += `,${this._scale}`;
}
result += ')';
}
return `NUMBER${result}`;
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_NUMBER };
}
}
class TINYINT extends BaseTypes.TINYINT {
toSql() {
return 'NUMBER(3)';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_NUMBER };
}
}
class SMALLINT extends BaseTypes.SMALLINT {
toSql() {
if (this._length) {
return `NUMBER(${this._length},0)`;
}
return 'SMALLINT';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_NUMBER };
}
}
class MEDIUMINT extends BaseTypes.MEDIUMINT {
toSql() {
return 'NUMBER(8)';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_NUMBER };
}
}
class BIGINT extends BaseTypes.BIGINT {
constructor(length) {
super(length);
if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments);
// ORACLE does not support any options for bigint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('Oracle does not support BIGINT with options');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
}
toSql() {
return 'NUMBER(19)';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_NUMBER };
}
_sanitize(value) {
if (typeof value === 'bigint' || typeof value === 'number') {
return value.toString();
}
return value;
}
}
class NUMBER extends BaseTypes.NUMBER {
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_NUMBER };
}
}
class INTEGER extends BaseTypes.INTEGER {
toSql() {
if (this._length) {
return `NUMBER(${this._length},0)`;
}
return 'INTEGER';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_NUMBER };
}
}
class FLOAT extends BaseTypes.FLOAT {
toSql() {
return 'BINARY_FLOAT';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_BINARY_FLOAT };
}
}
class REAL extends BaseTypes.REAL {
toSql() {
return 'BINARY_DOUBLE';
}
// https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-0BA2E065-8006-426C-A3CB-1F6B0C8F283C
_stringify(value) {
if (value === Number.POSITIVE_INFINITY) {
return 'inf';
}
if (value === Number.NEGATIVE_INFINITY) {
return '-inf';
}
return value;
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_BINARY_DOUBLE };
}
}
class BLOB extends BaseTypes.BLOB {
// Generic hexify returns X'${hex}' but Oracle expects '${hex}' for BLOB datatype
_hexify(hex) {
return `'${hex}'`;
}
toSql() {
return 'BLOB';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_BLOB };
}
}
class JSONTYPE extends BaseTypes.JSON {
toSql() {
return 'BLOB';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_BLOB };
}
_stringify(value, options) {
return options.operation === 'where' && typeof value === 'string' ? value : JSON.stringify(value);
}
_bindParam(value, options) {
return options.bindParam(Buffer.from(JSON.stringify(value)));
}
}
class DOUBLE extends BaseTypes.DOUBLE {
constructor(length, decimals) {
super(length, decimals);
if (!(this instanceof DOUBLE)) return new BaseTypes.DOUBLE(length, decimals);
BaseTypes.DOUBLE.apply(this, arguments);
if (this._length || this._unsigned || this._zerofill) {
warn('Oracle does not support DOUBLE with options.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
this.key = 'DOUBLE PRECISION';
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_BINARY_DOUBLE };
}
toSql() {
return 'BINARY_DOUBLE';
}
}
class DATEONLY extends BaseTypes.DATEONLY {
parse(value) {
return moment(value).format('YYYY-MM-DD');
}
_sanitize(value) {
if (value) {
return moment(value).format('YYYY-MM-DD');
}
return value;
}
_stringify(date, options) {
// If date is not null only then we format the date
if (date) {
const format = 'YYYY/MM/DD';
return options.escape(`TO_DATE('${date}','${format}')`);
}
return options.escape(date);
}
_getBindDef(oracledb) {
return { type: oracledb.DB_TYPE_DATE };
}
/**
* avoids appending TO_DATE in _stringify
*
* @override
*/
_bindParam(value, options) {
if (typeof value === 'string') {
return options.bindParam(new Date(value));
}
return options.bindParam(value);
}
}
DATEONLY.prototype.escape = false;
return {
BOOLEAN,
'DOUBLE PRECISION': DOUBLE,
DOUBLE,
STRING,
TINYINT,
SMALLINT,
MEDIUMINT,
BIGINT,
NUMBER,
INTEGER,
FLOAT,
UUID,
DATEONLY,
DATE,
NOW,
BLOB,
ENUM,
TEXT,
CHAR,
JSON: JSONTYPE,
REAL,
DECIMAL
};
};