/
data-types.js
340 lines (314 loc) · 9.42 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
'use strict';
const momentTz = require('moment-timezone');
const moment = require('moment');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined,
'https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/' +
'com.ibm.db2.luw.sql.ref.doc/doc/r0008478.html');
/**
* Removes unsupported Db2 options, i.e., LENGTH, UNSIGNED and ZEROFILL,
* for the integer data types.
*
* @param {object} dataType The base integer data type.
* @private
*/
function removeUnsupportedIntegerOptions(dataType) {
if (dataType._length || dataType.options.length || dataType._unsigned || dataType._zerofill) {
warn(`Db2 does not support '${dataType.key}' with options. Plain '${dataType.key}' will be used instead.`);
dataType._length = undefined;
dataType.options.length = undefined;
dataType._unsigned = undefined;
dataType._zerofill = undefined;
}
}
/**
* types: [hex, ...]
*
* @see Data types and table columns: https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.admin.dbobj.doc/doc/c0055357.html
*/
BaseTypes.DATE.types.db2 = ['TIMESTAMP'];
BaseTypes.STRING.types.db2 = ['VARCHAR'];
BaseTypes.CHAR.types.db2 = ['CHAR'];
BaseTypes.TEXT.types.db2 = ['VARCHAR', 'CLOB'];
BaseTypes.TINYINT.types.db2 = ['SMALLINT'];
BaseTypes.SMALLINT.types.db2 = ['SMALLINT'];
BaseTypes.MEDIUMINT.types.db2 = ['INTEGER'];
BaseTypes.INTEGER.types.db2 = ['INTEGER'];
BaseTypes.BIGINT.types.db2 = ['BIGINT'];
BaseTypes.FLOAT.types.db2 = ['DOUBLE', 'REAL', 'FLOAT'];
BaseTypes.TIME.types.db2 = ['TIME'];
BaseTypes.DATEONLY.types.db2 = ['DATE'];
BaseTypes.BOOLEAN.types.db2 = ['BOOLEAN', 'BOOL', 'SMALLINT', 'BIT'];
BaseTypes.BLOB.types.db2 = ['BLOB'];
BaseTypes.DECIMAL.types.db2 = ['DECIMAL'];
BaseTypes.UUID.types.db2 = ['CHAR () FOR BIT DATA'];
BaseTypes.ENUM.types.db2 = ['VARCHAR'];
BaseTypes.REAL.types.db2 = ['REAL'];
BaseTypes.DOUBLE.types.db2 = ['DOUBLE'];
BaseTypes.GEOMETRY.types.db2 = false;
class BLOB extends BaseTypes.BLOB {
toSql() {
if (this._length) {
if (this._length.toLowerCase() === 'tiny') { // tiny = 255 bytes
return 'BLOB(255)';
}
if (this._length.toLowerCase() === 'medium') { // medium = 16M
return 'BLOB(16M)';
}
if (this._length.toLowerCase() === 'long') { // long = 2GB
return 'BLOB(2G)';
}
return `BLOB(${ this._length })`;
}
return 'BLOB'; // 1MB
}
escape(blob) {
return `BLOB('${ blob.toString().replace(/'/g, "''") }')`;
}
_stringify(value) {
if (Buffer.isBuffer(value)) {
return `BLOB('${ value.toString().replace(/'/g, "''") }')`;
}
if (Array.isArray(value)) {
value = Buffer.from(value);
} else {
value = Buffer.from(value.toString());
}
const hex = value.toString('hex');
return this._hexify(hex);
}
_hexify(hex) {
return `x'${ hex }'`;
}
}
class STRING extends BaseTypes.STRING {
toSql() {
if (!this._binary) {
if (this._length <= 4000) {
return `VARCHAR(${ this._length })`;
}
return `CLOB(${ this._length })`;
}
if (this._length < 255) {
return `CHAR(${ this._length }) FOR BIT DATA`;
}
if (this._length <= 4000) {
return `VARCHAR(${ this._length }) FOR BIT DATA`;
}
return `BLOB(${ this._length })`;
}
_stringify(value, options) {
if (this._binary) {
return BLOB.prototype._hexify(value.toString('hex'));
}
return options.escape(value);
}
_bindParam(value, options) {
return options.bindParam(this._binary ? Buffer.from(value) : value);
}
}
STRING.prototype.escape = false;
class TEXT extends BaseTypes.TEXT {
toSql() {
let len = 0;
if (this._length) {
switch (this._length.toLowerCase()) {
case 'tiny':
len = 256; // tiny = 2^8
break;
case 'medium':
len = 8192; // medium = 2^13 = 8k
break;
case 'long':
len = 65536; // long = 64k
break;
}
if ( isNaN(this._length) ) {
this._length = 32672;
}
if (len > 0 ) { this._length = len; }
} else { this._length = 32672; }
if ( this._length > 32672 )
{
len = `CLOB(${ this._length })`;
}
else
{
len = `VARCHAR(${ this._length })`;
}
warn(`Db2 does not support TEXT datatype. ${len} will be used instead.`);
return len;
}
}
class BOOLEAN extends BaseTypes.BOOLEAN {
toSql() {
return 'BOOLEAN';
}
_sanitize(value) {
if (value !== null && value !== undefined) {
if (Buffer.isBuffer(value) && value.length === 1) {
// Bit fields are returned as buffers
value = value[0];
}
if (typeof value === 'string') {
// Only take action on valid boolean strings.
value = value === 'true' ? true : value === 'false' ? false : value;
value = value === '\u0001' ? true : value === '\u0000' ? false : value;
} else if (typeof value === 'number') {
// Only take action on valid boolean integers.
value = value === 1 ? true : value === 0 ? false : value;
}
}
return value;
}
}
BOOLEAN.parse = BOOLEAN.prototype._sanitize;
class UUID extends BaseTypes.UUID {
toSql() {
return 'CHAR(36) FOR BIT DATA';
}
}
class NOW extends BaseTypes.NOW {
toSql() {
return 'CURRENT TIME';
}
}
class DATE extends BaseTypes.DATE {
toSql() {
if (this._length < 0) { this._length = 0; }
if (this._length > 6) { this._length = 6; }
return `TIMESTAMP${ this._length ? `(${ this._length })` : ''}`;
}
_stringify(date, options) {
if (!moment.isMoment(date)) {
date = this._applyTimezone(date, options);
}
if (this._length > 0) {
let msec = '.';
for ( let i = 0; i < this._length && i < 6; i++ ) {
msec += 'S';
}
return date.format(`YYYY-MM-DD HH:mm:ss${msec}`);
}
return date.format('YYYY-MM-DD HH:mm:ss');
}
static parse(value) {
if (typeof value !== 'string') {
value = value.string();
}
if (value === null) {
return value;
}
value = new Date(momentTz.utc(value));
return value;
}
}
class DATEONLY extends BaseTypes.DATEONLY {
static parse(value) {
return momentTz(value).format('YYYY-MM-DD');
}
}
class INTEGER extends BaseTypes.INTEGER {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class TINYINT extends BaseTypes.TINYINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class SMALLINT extends BaseTypes.SMALLINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class BIGINT extends BaseTypes.BIGINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class REAL extends BaseTypes.REAL {
constructor(length, decimals) {
super(length, decimals);
// Db2 does not support any options for real
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('Db2 does not support REAL with options. Plain `REAL` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
}
}
class FLOAT extends BaseTypes.FLOAT {
constructor(length, decimals) {
super(length, decimals);
// Db2 does only support lengths as option.
// Values between 1-24 result in 7 digits precision (4 bytes storage size)
// Values between 25-53 result in 15 digits precision (8 bytes size)
// If decimals are provided remove these and print a warning
if (this._decimals) {
warn('Db2 does not support Float with decimals. Plain `FLOAT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
}
if (this._unsigned) {
warn('Db2 does not support Float unsigned. `UNSIGNED` was removed.');
this._unsigned = undefined;
}
if (this._zerofill) {
warn('Db2 does not support Float zerofill. `ZEROFILL` was removed.');
this._zerofill = undefined;
}
}
}
class ENUM extends BaseTypes.ENUM {
toSql() {
return 'VARCHAR(255)';
}
}
class DOUBLE extends BaseTypes.DOUBLE {
constructor(length, decimals) {
super(length, decimals);
// db2 does not support any parameters for double
if (this._length || this.options.length ||
this._unsigned || this._zerofill)
{
warn('db2 does not support DOUBLE with options. ' +
'Plain DOUBLE will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
}
toSql() {
return 'DOUBLE';
}
}
DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE';
return {
BLOB,
BOOLEAN,
ENUM,
STRING,
UUID,
DATE,
DATEONLY,
NOW,
TINYINT,
SMALLINT,
INTEGER,
DOUBLE,
'DOUBLE PRECISION': DOUBLE,
BIGINT,
REAL,
FLOAT,
TEXT
};
};