-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
data-types.js
321 lines (267 loc) · 7.22 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
'use strict';
const dayjs = require('dayjs');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/db2/rbafzch2data.htm');
/**
* Removes unsupported Db2 for i options, i.e., 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._zerofill || dataType._unsigned) {
warn(`IBM i Db2 does not support '${dataType.key}' with LENGTH, UNSIGNED or ZEROFILL. Plain '${dataType.key}' will be used instead.`);
dataType._length = undefined;
dataType.options.length = undefined;
dataType._unsigned = undefined;
dataType._zerofill = undefined;
}
}
/**
* @see https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/db2/rbafzch2data.htm
*/
BaseTypes.DATE.types.ibmi = [93];
BaseTypes.STRING.types.ibmi = [12];
BaseTypes.CHAR.types.ibmi = [1];
BaseTypes.TEXT.types.ibmi = [12];
BaseTypes.TINYINT.types.ibmi = false;
BaseTypes.SMALLINT.types.ibmi = [5];
BaseTypes.MEDIUMINT.types.ibmi = false;
BaseTypes.INTEGER.types.ibmi = [4];
BaseTypes.BIGINT.types.ibmi = [-5];
BaseTypes.FLOAT.types.ibmi = [8];
BaseTypes.TIME.types.ibmi = [92];
BaseTypes.DATEONLY.types.ibmi = [91];
BaseTypes.BOOLEAN.types.ibmi = [5];
BaseTypes.BLOB.types.ibmi = [-4, -2];
BaseTypes.DECIMAL.types.ibmi = [3];
BaseTypes.UUID.types.ibmi = [1];
BaseTypes.ENUM.types.ibmi = false;
BaseTypes.REAL.types.ibmi = [7];
BaseTypes.DOUBLE.types.ibmi = [8];
BaseTypes.GEOMETRY.types.ibmi = false;
BaseTypes.JSON.types.ibmi = false;
class ENUM extends BaseTypes.ENUM {
toSql() {
return 'VARCHAR(255)';
}
}
class DATE extends BaseTypes.DATE {
toSql() {
return 'TIMESTAMP';
}
static parse(date) {
if (!date.includes('+')) {
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
return dayjs.utc(date).toDate();
}
return dayjs.utc(date).toDate();
}
_stringify(date, options) {
date = this._applyTimezone(date, options);
return date.format('YYYY-MM-DD HH:mm:ss.SSS');
}
}
class UUID extends BaseTypes.UUID {
toSql() {
return 'CHAR(36)';
}
}
class DATEONLY extends BaseTypes.DATEONLY {
static parse(date) {
return date;
}
}
class BOOLEAN extends BaseTypes.BOOLEAN {
toSql() {
return 'SMALLINT';
}
_stringify(value) {
if (value) {
return 1;
}
return 0;
}
static parse(value) {
if (value) {
return true;
}
return false;
}
}
class STRING extends BaseTypes.STRING {
toSql() {
if (this._binary) {
return `BINARY(${this._length})`;
}
return super.toSql(this);
}
_stringify(value, options) {
if (Buffer.isBuffer(value)) {
return `BLOB(X'${value.toString('hex')}')`;
}
return value.toString();
}
}
class TEXT extends BaseTypes.TEXT {
toSql() {
let len = 0;
if (this._length) {
if (typeof this._length === 'string') {
switch (this._length.toLowerCase()) {
case 'tiny':
this._length = 256; // tiny = 2^8
break;
case 'medium':
this._length = 8192; // medium = 2^13 = 8k
break;
case 'long':
this._length = 65_536; // long = 64k
break;
default:
throw new Error('Unknown length value passed for TEXT type. Valid values are: positive integers, "tiny", "medium", and "long"');
}
} else if (!Number.isSafeInteger(this._length) || this._length <= 0) {
throw new Error('Unknown length for TEXT type. Valid values are: positive integers, "tiny", "medium", and "long"');
}
} else {
this._length = 8192;
}
if (this._length > 32_672) {
len = `CLOB(${this._length})`;
} else {
len = `VARCHAR(${this._length})`;
}
return len;
}
}
class CHAR extends BaseTypes.CHAR {
toSql() {
if (this._binary) {
return `CLOB(${this._length})`;
}
return super.toSql();
}
}
class NUMBER extends BaseTypes.NUMBER {
toSql() {
let result = this.key;
if (this._length) {
result += `(${this._length}`;
if (typeof this._decimals === 'number') {
result += `,${this._decimals}`;
}
result += ')';
}
return result;
}
}
class SMALLINT extends BaseTypes.SMALLINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class INTEGER extends BaseTypes.INTEGER {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class BIGINT extends BaseTypes.BIGINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
_stringify(value) {
return value.toString();
}
static parse(value) {
return value.toString();
}
}
class FLOAT extends BaseTypes.FLOAT {
constructor(length, decimals) {
super(length, decimals);
if (this._decimals) {
warn('Db2 for i does not support FLOAT with decimal scale. FLOAT will be used instead. If you want to specify scale, use DECIMAL or NUMERIC data types.');
this._length = undefined;
this._decimals = undefined;
}
}
}
class REAL extends BaseTypes.REAL {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
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(1M)';
}
_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());
}
return `BLOB('${value}')`;
}
static parse(value) {
return Buffer.from(value);
}
}
function parseFloating(value) {
if (typeof value !== 'string') {
return value;
}
if (value === 'NaN') {
return NaN;
}
if (value === 'Infinity') {
return Infinity;
}
if (value === '-Infinity') {
return -Infinity;
}
}
for (const floating of [FLOAT, REAL]) {
floating.parse = parseFloating;
}
for (const num of [FLOAT, REAL, SMALLINT, INTEGER]) {
num.prototype.toSql = NUMBER.prototype.toSql;
}
return {
BOOLEAN,
DATE,
DATEONLY,
STRING,
CHAR,
NUMBER,
FLOAT,
ENUM,
REAL,
SMALLINT,
INTEGER,
BIGINT,
TEXT,
UUID,
BLOB,
};
};