oak-db 2.2.2 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,775 +1,776 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MySqlTranslator = void 0;
4
- var tslib_1 = require("tslib");
5
- var assert_1 = tslib_1.__importDefault(require("assert"));
6
- var util_1 = require("util");
7
- var lodash_1 = require("lodash");
8
- var sqlTranslator_1 = require("../sqlTranslator");
9
- var GeoTypes = [
10
- {
11
- type: 'point',
12
- name: "Point"
13
- },
14
- {
15
- type: 'path',
16
- name: "LineString",
17
- element: 'point',
18
- },
19
- {
20
- name: "MultiLineString",
21
- element: "path",
22
- multiple: true,
23
- },
24
- {
25
- type: 'polygon',
26
- name: "Polygon",
27
- element: "path"
28
- },
29
- {
30
- name: "MultiPoint",
31
- element: "point",
32
- multiple: true,
33
- },
34
- {
35
- name: "MultiPolygon",
36
- element: "polygon",
37
- multiple: true,
38
- }
39
- ];
40
- function transformGeoData(data) {
41
- if (data instanceof Array) {
42
- var element_1 = data[0];
43
- if (element_1 instanceof Array) {
44
- return " GeometryCollection(".concat(data.map(function (ele) { return transformGeoData(ele); }).join(','), ")");
45
- }
46
- else {
47
- var geoType_1 = GeoTypes.find(function (ele) { return ele.type === element_1.type; });
48
- if (!geoType_1) {
49
- throw new Error("".concat(element_1.type, " is not supported in MySQL"));
50
- }
51
- var multiGeoType = GeoTypes.find(function (ele) { return ele.element === geoType_1.type && ele.multiple; });
52
- return " ".concat(multiGeoType.name, "(").concat(data.map(function (ele) { return transformGeoData(ele); }).join(','), ")");
53
- }
54
- }
55
- else {
56
- var type_1 = data.type, coordinate = data.coordinate;
57
- var geoType = GeoTypes.find(function (ele) { return ele.type === type_1; });
58
- if (!geoType) {
59
- throw new Error("".concat(data.type, " is not supported in MySQL"));
60
- }
61
- var element_2 = geoType.element, name_1 = geoType.name;
62
- if (!element_2) {
63
- // Point
64
- return " ".concat(name_1, "(").concat(coordinate.join(','), ")");
65
- }
66
- // Polygon or Linestring
67
- return " ".concat(name_1, "(").concat(coordinate.map(function (ele) { return transformGeoData({
68
- type: element_2,
69
- coordinate: ele,
70
- }); }), ")");
71
- }
72
- }
73
- var MySqlTranslator = /** @class */ (function (_super) {
74
- tslib_1.__extends(MySqlTranslator, _super);
75
- function MySqlTranslator(schema) {
76
- var _this = _super.call(this, schema) || this;
77
- _this.maxAliasLength = 63;
78
- // MySQL为geometry属性默认创建索引
79
- _this.makeUpSchema();
80
- return _this;
81
- }
82
- MySqlTranslator.prototype.getDefaultSelectFilter = function (alias, option) {
83
- if (option === null || option === void 0 ? void 0 : option.includedDeleted) {
84
- return '';
85
- }
86
- return " (`".concat(alias, "`.`$$deleteAt$$` is null)");
87
- };
88
- MySqlTranslator.prototype.makeUpSchema = function () {
89
- for (var entity in this.schema) {
90
- var _a = this.schema[entity], attributes = _a.attributes, indexes = _a.indexes;
91
- var geoIndexes = [];
92
- var _loop_1 = function (attr) {
93
- if (attributes[attr].type === 'geometry') {
94
- var geoIndex = indexes === null || indexes === void 0 ? void 0 : indexes.find(function (idx) {
95
- var _a;
96
- return ((_a = idx.config) === null || _a === void 0 ? void 0 : _a.type) === 'spatial' && idx.attributes.find(function (attrDef) { return attrDef.name === attr; });
97
- });
98
- if (!geoIndex) {
99
- geoIndexes.push({
100
- name: "".concat(entity, "_geo_").concat(attr),
101
- attributes: [{
102
- name: attr,
103
- }],
104
- config: {
105
- type: 'spatial',
106
- }
107
- });
108
- }
109
- }
110
- };
111
- for (var attr in attributes) {
112
- _loop_1(attr);
113
- }
114
- if (geoIndexes.length > 0) {
115
- if (indexes) {
116
- indexes.push.apply(indexes, geoIndexes);
117
- }
118
- else {
119
- (0, lodash_1.assign)(this.schema[entity], {
120
- indexes: geoIndexes,
121
- });
122
- }
123
- }
124
- }
125
- };
126
- MySqlTranslator.prototype.populateDataTypeDef = function (type, params) {
127
- if (['date', 'datetime', 'time', 'sequence'].includes(type)) {
128
- return 'bigint ';
129
- }
130
- if (['object', 'array'].includes(type)) {
131
- return 'text ';
132
- }
133
- if (['image', 'function'].includes(type)) {
134
- return 'text ';
135
- }
136
- if (type === 'ref') {
137
- return 'char(36)';
138
- }
139
- if (MySqlTranslator.withLengthDataTypes.includes(type)) {
140
- if (params) {
141
- var length_1 = params.length;
142
- return "".concat(type, "(").concat(length_1, ") ");
143
- }
144
- else {
145
- var length_2 = MySqlTranslator.dataTypeDefaults[type].length;
146
- return "".concat(type, "(").concat(length_2, ") ");
147
- }
148
- }
149
- if (MySqlTranslator.withPrecisionDataTypes.includes(type)) {
150
- if (params) {
151
- var precision = params.precision, scale = params.scale;
152
- if (typeof scale === 'number') {
153
- return "".concat(type, "(").concat(precision, ", ").concat(scale, ") ");
154
- }
155
- return "".concat(type, "(").concat(precision, ")");
156
- }
157
- else {
158
- var _a = MySqlTranslator.dataTypeDefaults[type], precision = _a.precision, scale = _a.scale;
159
- if (typeof scale === 'number') {
160
- return "".concat(type, "(").concat(precision, ", ").concat(scale, ") ");
161
- }
162
- return "".concat(type, "(").concat(precision, ")");
163
- }
164
- }
165
- if (MySqlTranslator.withWidthDataTypes.includes(type)) {
166
- (0, assert_1.default)(type === 'int');
167
- var width = params.width;
168
- switch (width) {
169
- case 1: {
170
- return 'tinyint';
171
- }
172
- case 2: {
173
- return 'smallint';
174
- }
175
- case 3: {
176
- return 'mediumint';
177
- }
178
- case 4: {
179
- return 'int';
180
- }
181
- default: {
182
- return 'bigint';
183
- }
184
- }
185
- }
186
- return "".concat(type, " ");
187
- };
188
- MySqlTranslator.prototype.translateAttrProjection = function (dataType, alias, attr) {
189
- switch (dataType) {
190
- case 'geometry': {
191
- return " st_astext(`".concat(alias, "`.`").concat(attr, "`)");
192
- }
193
- default: {
194
- return " `".concat(alias, "`.`").concat(attr, "`");
195
- }
196
- }
197
- };
198
- MySqlTranslator.prototype.translateAttrValue = function (dataType, value) {
199
- if (value === null || value === undefined) {
200
- return 'null';
201
- }
202
- switch (dataType) {
203
- case 'geometry': {
204
- return transformGeoData(value);
205
- }
206
- case 'datetime':
207
- case 'time':
208
- case 'date': {
209
- if (value instanceof Date) {
210
- return "".concat(value.valueOf());
211
- }
212
- else if (typeof value === 'number') {
213
- return "".concat(value);
214
- }
215
- return "'".concat((new Date(value)).valueOf(), "'");
216
- }
217
- case 'object':
218
- case 'array': {
219
- return this.escapeStringValue(JSON.stringify(value));
220
- }
221
- /* case 'function': {
222
- return `'${Buffer.from(value.toString()).toString('base64')}'`;
223
- } */
224
- default: {
225
- if (typeof value === 'string') {
226
- return this.escapeStringValue(value);
227
- }
228
- return value;
229
- }
230
- }
231
- };
232
- MySqlTranslator.prototype.translateFullTextSearch = function (value, entity, alias) {
233
- var $search = value.$search;
234
- var indexes = this.schema[entity].indexes;
235
- var ftIndex = indexes && indexes.find(function (ele) {
236
- var config = ele.config;
237
- return config && config.type === 'fulltext';
238
- });
239
- (0, assert_1.default)(ftIndex);
240
- var attributes = ftIndex.attributes;
241
- var columns2 = attributes.map(function (_a) {
242
- var name = _a.name;
243
- return "".concat(alias, ".").concat(name);
244
- });
245
- return " match(".concat(columns2.join(','), ") against ('").concat($search, "' in natural language mode)");
246
- };
247
- MySqlTranslator.prototype.translateCreateEntity = function (entity, options) {
248
- var _this = this;
249
- var replace = options === null || options === void 0 ? void 0 : options.replace;
250
- var schema = this.schema;
251
- var entityDef = schema[entity];
252
- var storageName = entityDef.storageName, attributes = entityDef.attributes, indexes = entityDef.indexes, view = entityDef.view;
253
- var hasSequence = false;
254
- // todo view暂还不支持
255
- var entityType = view ? 'view' : 'table';
256
- var sql = "create ".concat(entityType, " ");
257
- if (storageName) {
258
- sql += "`".concat(storageName, "` ");
259
- }
260
- else {
261
- sql += "`".concat(entity, "` ");
262
- }
263
- if (view) {
264
- throw new Error(' view unsupported yet');
265
- }
266
- else {
267
- sql += '(';
268
- // 翻译所有的属性
269
- Object.keys(attributes).forEach(function (attr, idx) {
270
- var attrDef = attributes[attr];
271
- var type = attrDef.type, params = attrDef.params, defaultValue = attrDef.default, unique = attrDef.unique, notNull = attrDef.notNull, sequenceStart = attrDef.sequenceStart;
272
- sql += "`".concat(attr, "` ");
273
- sql += _this.populateDataTypeDef(type, params);
274
- if (notNull || type === 'geometry') {
275
- sql += ' not null ';
276
- }
277
- if (unique) {
278
- sql += ' unique ';
279
- }
280
- if (sequenceStart) {
281
- if (hasSequence) {
282
- throw new Error("\u300C".concat(entity, "\u300D\u53EA\u80FD\u6709\u4E00\u4E2Asequence\u5217"));
283
- }
284
- hasSequence = sequenceStart;
285
- sql += ' auto_increment unique ';
286
- }
287
- if (defaultValue !== undefined) {
288
- (0, assert_1.default)(type !== 'ref');
289
- sql += " default ".concat(_this.translateAttrValue(type, defaultValue));
290
- }
291
- if (attr === 'id') {
292
- sql += ' primary key';
293
- }
294
- if (idx < Object.keys(attributes).length - 1) {
295
- sql += ',\n';
296
- }
297
- });
298
- // 翻译索引信息
299
- if (indexes) {
300
- sql += ',\n';
301
- indexes.forEach(function (_a, idx) {
302
- var name = _a.name, attributes = _a.attributes, config = _a.config;
303
- var _b = config || {}, unique = _b.unique, type = _b.type, parser = _b.parser;
304
- if (unique) {
305
- sql += ' unique ';
306
- }
307
- else if (type === 'fulltext') {
308
- sql += ' fulltext ';
309
- }
310
- else if (type === 'spatial') {
311
- sql += ' spatial ';
312
- }
313
- sql += "index ".concat(name, " ");
314
- if (type === 'hash') {
315
- sql += " using hash ";
316
- }
317
- sql += '(';
318
- var includeDeleteAt = false;
319
- attributes.forEach(function (_a, idx2) {
320
- var name = _a.name, size = _a.size, direction = _a.direction;
321
- sql += "`".concat(name, "`");
322
- if (size) {
323
- sql += " (".concat(size, ")");
324
- }
325
- if (direction) {
326
- sql += " ".concat(direction);
327
- }
328
- if (idx2 < attributes.length - 1) {
329
- sql += ',';
330
- }
331
- if (name === '$$deleteAt$$') {
332
- includeDeleteAt = true;
333
- }
334
- });
335
- if (!includeDeleteAt && !type) {
336
- sql += ', $$deleteAt$$';
337
- }
338
- sql += ')';
339
- if (parser) {
340
- sql += " with parser ".concat(parser);
341
- }
342
- if (idx < indexes.length - 1) {
343
- sql += ',\n';
344
- }
345
- });
346
- }
347
- }
348
- sql += ')';
349
- if (typeof hasSequence === 'number') {
350
- sql += "auto_increment = ".concat(hasSequence);
351
- }
352
- if (!replace) {
353
- return [sql];
354
- }
355
- return ["drop ".concat(entityType, " if exists `").concat(storageName || entity, "`;"), sql];
356
- };
357
- MySqlTranslator.prototype.translateFnName = function (fnName, argumentNumber) {
358
- switch (fnName) {
359
- case '$add': {
360
- var result = '%s';
361
- while (--argumentNumber > 0) {
362
- result += ' + %s';
363
- }
364
- return result;
365
- }
366
- case '$subtract': {
367
- (0, assert_1.default)(argumentNumber === 2);
368
- return '%s - %s';
369
- }
370
- case '$multiply': {
371
- var result = '%s';
372
- while (--argumentNumber > 0) {
373
- result += ' * %s';
374
- }
375
- return result;
376
- }
377
- case '$divide': {
378
- (0, assert_1.default)(argumentNumber === 2);
379
- return '%s / %s';
380
- }
381
- case '$abs': {
382
- return 'ABS(%s)';
383
- }
384
- case '$round': {
385
- (0, assert_1.default)(argumentNumber === 2);
386
- return 'ROUND(%s, %s)';
387
- }
388
- case '$ceil': {
389
- return 'CEIL(%s)';
390
- }
391
- case '$floor': {
392
- return 'FLOOR(%s)';
393
- }
394
- case '$pow': {
395
- (0, assert_1.default)(argumentNumber === 2);
396
- return 'POW(%s, %s)';
397
- }
398
- case '$gt': {
399
- (0, assert_1.default)(argumentNumber === 2);
400
- return '%s > %s';
401
- }
402
- case '$gte': {
403
- (0, assert_1.default)(argumentNumber === 2);
404
- return '%s >= %s';
405
- }
406
- case '$lt': {
407
- (0, assert_1.default)(argumentNumber === 2);
408
- return '%s < %s';
409
- }
410
- case '$lte': {
411
- return '%s <= %s';
412
- }
413
- case '$eq': {
414
- (0, assert_1.default)(argumentNumber === 2);
415
- return '%s = %s';
416
- }
417
- case '$ne': {
418
- (0, assert_1.default)(argumentNumber === 2);
419
- return '%s <> %s';
420
- }
421
- case '$startsWith': {
422
- (0, assert_1.default)(argumentNumber === 2);
423
- return '%s like CONCAT(%s, \'%\')';
424
- }
425
- case '$endsWith': {
426
- (0, assert_1.default)(argumentNumber === 2);
427
- return '%s like CONCAT(\'%\', %s)';
428
- }
429
- case '$includes': {
430
- (0, assert_1.default)(argumentNumber === 2);
431
- return '%s like CONCAT(\'%\', %s, \'%\')';
432
- }
433
- case '$true': {
434
- return '%s = true';
435
- }
436
- case '$false': {
437
- return '%s = false';
438
- }
439
- case '$and': {
440
- var result = '';
441
- for (var iter = 0; iter < argumentNumber; iter++) {
442
- result += '%s';
443
- if (iter < argumentNumber - 1) {
444
- result += ' and ';
445
- }
446
- }
447
- return result;
448
- }
449
- case '$or': {
450
- var result = '';
451
- for (var iter = 0; iter < argumentNumber; iter++) {
452
- result += '%s';
453
- if (iter < argumentNumber - 1) {
454
- result += ' or ';
455
- }
456
- }
457
- return result;
458
- }
459
- case '$not': {
460
- return 'not %s';
461
- }
462
- case '$year': {
463
- return 'YEAR(%s)';
464
- }
465
- case '$month': {
466
- return 'MONTH(%s)';
467
- }
468
- case '$weekday': {
469
- return 'WEEKDAY(%s)';
470
- }
471
- case '$weekOfYear': {
472
- return 'WEEKOFYEAR(%s)';
473
- }
474
- case '$day': {
475
- return 'DAY(%s)';
476
- }
477
- case '$dayOfMonth': {
478
- return 'DAYOFMONTH(%s)';
479
- }
480
- case '$dayOfWeek': {
481
- return 'DAYOFWEEK(%s)';
482
- }
483
- case '$dayOfYear': {
484
- return 'DAYOFYEAR(%s)';
485
- }
486
- case '$dateDiff': {
487
- (0, assert_1.default)(argumentNumber === 3);
488
- return 'DATEDIFF(%s, %s, %s)';
489
- }
490
- case '$contains': {
491
- (0, assert_1.default)(argumentNumber === 2);
492
- return 'ST_CONTAINS(%s, %s)';
493
- }
494
- case '$distance': {
495
- (0, assert_1.default)(argumentNumber === 2);
496
- return 'ST_DISTANCE(%s, %s)';
497
- }
498
- case '$concat': {
499
- var result = ' concat(%s';
500
- while (--argumentNumber > 0) {
501
- result += ', %s';
502
- }
503
- result += ')';
504
- return result;
505
- }
506
- default: {
507
- throw new Error("unrecoganized function ".concat(fnName));
508
- }
509
- }
510
- };
511
- MySqlTranslator.prototype.translateAttrInExpression = function (entity, attr, exprText) {
512
- var attributes = this.schema[entity].attributes;
513
- var type = attributes[attr].type;
514
- if (['date', 'time', 'datetime'].includes(type)) {
515
- // 从unix时间戵转成date类型参加expr的运算
516
- return "from_unixtime(".concat(exprText, " / 1000)");
517
- }
518
- return exprText;
519
- };
520
- MySqlTranslator.prototype.translateExpression = function (entity, alias, expression, refDict) {
521
- var _this = this;
522
- var translateConstant = function (constant) {
523
- if (constant instanceof Date) {
524
- return " from_unixtime(".concat(constant.valueOf(), "/1000)");
525
- }
526
- else if (typeof constant === 'string') {
527
- return " '".concat(constant, "'");
528
- }
529
- else {
530
- (0, assert_1.default)(typeof constant === 'number');
531
- return " ".concat(constant);
532
- }
533
- };
534
- var translateInner = function (expr) {
535
- var k = Object.keys(expr);
536
- var result;
537
- if (k.includes('#attr')) {
538
- var attrText = "`".concat(alias, "`.`").concat((expr)['#attr'], "`");
539
- result = _this.translateAttrInExpression(entity, (expr)['#attr'], attrText);
540
- }
541
- else if (k.includes('#refId')) {
542
- var refId = (expr)['#refId'];
543
- var refAttr = (expr)['#refAttr'];
544
- (0, assert_1.default)(refDict[refId]);
545
- var attrText = "`".concat(refDict[refId][0], "`.`").concat(refAttr, "`");
546
- result = _this.translateAttrInExpression(entity, (expr)['#refAttr'], attrText);
547
- }
548
- else {
549
- (0, assert_1.default)(k.length === 1);
550
- if ((expr)[k[0]] instanceof Array) {
551
- var fnName = _this.translateFnName(k[0], (expr)[k[0]].length);
552
- var args = [fnName];
553
- args.push.apply(args, (expr)[k[0]].map(function (ele) {
554
- if (['string', 'number'].includes(typeof ele) || ele instanceof Date) {
555
- return translateConstant(ele);
556
- }
557
- else {
558
- return translateInner(ele);
559
- }
560
- }));
561
- result = util_1.format.apply(null, args);
562
- }
563
- else {
564
- var fnName = _this.translateFnName(k[0], 1);
565
- var args = [fnName];
566
- var arg = (expr)[k[0]];
567
- if (['string', 'number'].includes(typeof arg) || arg instanceof Date) {
568
- args.push(translateConstant(arg));
569
- }
570
- else {
571
- args.push(translateInner(arg));
572
- }
573
- result = util_1.format.apply(null, args);
574
- }
575
- }
576
- return result;
577
- };
578
- return translateInner(expression);
579
- };
580
- MySqlTranslator.prototype.populateSelectStmt = function (projectionText, fromText, aliasDict, filterText, sorterText, groupByText, indexFrom, count, option) {
581
- // todo hint of use index
582
- var sql = "select ".concat(projectionText, " from ").concat(fromText);
583
- if (filterText) {
584
- sql += " where ".concat(filterText);
585
- }
586
- if (sorterText) {
587
- sql += " order by ".concat(sorterText);
588
- }
589
- if (groupByText) {
590
- sql += " group by ".concat(groupByText);
591
- }
592
- if (typeof indexFrom === 'number') {
593
- (0, assert_1.default)(typeof count === 'number');
594
- sql += " limit ".concat(indexFrom, ", ").concat(count);
595
- }
596
- if (option === null || option === void 0 ? void 0 : option.forUpdate) {
597
- sql += ' for update';
598
- }
599
- return sql;
600
- };
601
- MySqlTranslator.prototype.populateUpdateStmt = function (updateText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
602
- // todo using index
603
- (0, assert_1.default)(updateText);
604
- var sql = "update ".concat(fromText, " set ").concat(updateText);
605
- if (filterText) {
606
- sql += " where ".concat(filterText);
607
- }
608
- if (sorterText) {
609
- sql += " order by ".concat(sorterText);
610
- }
611
- if (typeof indexFrom === 'number') {
612
- (0, assert_1.default)(typeof count === 'number');
613
- sql += " limit ".concat(indexFrom, ", ").concat(count);
614
- }
615
- return sql;
616
- };
617
- MySqlTranslator.prototype.populateRemoveStmt = function (removeText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
618
- // todo using index
619
- var alias = aliasDict['./'];
620
- var now = Date.now();
621
- var sql = "update ".concat(fromText, " set `").concat(alias, "`.`$$deleteAt$$` = '").concat(now, "'");
622
- if (filterText) {
623
- sql += " where ".concat(filterText);
624
- }
625
- if (sorterText) {
626
- sql += " order by ".concat(sorterText);
627
- }
628
- if (typeof indexFrom === 'number') {
629
- (0, assert_1.default)(typeof count === 'number');
630
- sql += " limit ".concat(indexFrom, ", ").concat(count);
631
- }
632
- return sql;
633
- };
634
- MySqlTranslator.supportedDataTypes = [
635
- // numeric types
636
- "bit",
637
- "int",
638
- "integer",
639
- "tinyint",
640
- "smallint",
641
- "mediumint",
642
- "bigint",
643
- "float",
644
- "double",
645
- "double precision",
646
- "real",
647
- "decimal",
648
- "dec",
649
- "numeric",
650
- "fixed",
651
- "bool",
652
- "boolean",
653
- // date and time types
654
- "date",
655
- "datetime",
656
- "timestamp",
657
- "time",
658
- "year",
659
- // string types
660
- "char",
661
- "nchar",
662
- "national char",
663
- "varchar",
664
- "nvarchar",
665
- "national varchar",
666
- "blob",
667
- "text",
668
- "tinyblob",
669
- "tinytext",
670
- "mediumblob",
671
- "mediumtext",
672
- "longblob",
673
- "longtext",
674
- "enum",
675
- "set",
676
- "binary",
677
- "varbinary",
678
- // json data type
679
- "json",
680
- // spatial data types
681
- "geometry",
682
- "point",
683
- "linestring",
684
- "polygon",
685
- "multipoint",
686
- "multilinestring",
687
- "multipolygon",
688
- "geometrycollection"
689
- ];
690
- MySqlTranslator.spatialTypes = [
691
- "geometry",
692
- "point",
693
- "linestring",
694
- "polygon",
695
- "multipoint",
696
- "multilinestring",
697
- "multipolygon",
698
- "geometrycollection"
699
- ];
700
- MySqlTranslator.withLengthDataTypes = [
701
- "char",
702
- "varchar",
703
- "nvarchar",
704
- "binary",
705
- "varbinary"
706
- ];
707
- MySqlTranslator.withPrecisionDataTypes = [
708
- "decimal",
709
- "dec",
710
- "numeric",
711
- "fixed",
712
- "float",
713
- "double",
714
- "double precision",
715
- "real",
716
- "time",
717
- "datetime",
718
- "timestamp"
719
- ];
720
- MySqlTranslator.withScaleDataTypes = [
721
- "decimal",
722
- "dec",
723
- "numeric",
724
- "fixed",
725
- "float",
726
- "double",
727
- "double precision",
728
- "real"
729
- ];
730
- MySqlTranslator.unsignedAndZerofillTypes = [
731
- "int",
732
- "integer",
733
- "smallint",
734
- "tinyint",
735
- "mediumint",
736
- "bigint",
737
- "decimal",
738
- "dec",
739
- "numeric",
740
- "fixed",
741
- "float",
742
- "double",
743
- "double precision",
744
- "real"
745
- ];
746
- MySqlTranslator.withWidthDataTypes = [
747
- 'int',
748
- ];
749
- MySqlTranslator.dataTypeDefaults = {
750
- "varchar": { length: 255 },
751
- "nvarchar": { length: 255 },
752
- "national varchar": { length: 255 },
753
- "char": { length: 1 },
754
- "binary": { length: 1 },
755
- "varbinary": { length: 255 },
756
- "decimal": { precision: 10, scale: 0 },
757
- "dec": { precision: 10, scale: 0 },
758
- "numeric": { precision: 10, scale: 0 },
759
- "fixed": { precision: 10, scale: 0 },
760
- "float": { precision: 12 },
761
- "double": { precision: 22 },
762
- "time": { precision: 0 },
763
- "datetime": { precision: 0 },
764
- "timestamp": { precision: 0 },
765
- "bit": { width: 1 },
766
- "int": { width: 11 },
767
- "integer": { width: 11 },
768
- "tinyint": { width: 4 },
769
- "smallint": { width: 6 },
770
- "mediumint": { width: 9 },
771
- "bigint": { width: 20 }
772
- };
773
- return MySqlTranslator;
774
- }(sqlTranslator_1.SqlTranslator));
775
- exports.MySqlTranslator = MySqlTranslator;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MySqlTranslator = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var assert_1 = tslib_1.__importDefault(require("assert"));
6
+ var util_1 = require("util");
7
+ var lodash_1 = require("lodash");
8
+ var sqlTranslator_1 = require("../sqlTranslator");
9
+ var GeoTypes = [
10
+ {
11
+ type: 'point',
12
+ name: "Point"
13
+ },
14
+ {
15
+ type: 'path',
16
+ name: "LineString",
17
+ element: 'point',
18
+ },
19
+ {
20
+ name: "MultiLineString",
21
+ element: "path",
22
+ multiple: true,
23
+ },
24
+ {
25
+ type: 'polygon',
26
+ name: "Polygon",
27
+ element: "path"
28
+ },
29
+ {
30
+ name: "MultiPoint",
31
+ element: "point",
32
+ multiple: true,
33
+ },
34
+ {
35
+ name: "MultiPolygon",
36
+ element: "polygon",
37
+ multiple: true,
38
+ }
39
+ ];
40
+ function transformGeoData(data) {
41
+ if (data instanceof Array) {
42
+ var element_1 = data[0];
43
+ if (element_1 instanceof Array) {
44
+ return " GeometryCollection(".concat(data.map(function (ele) { return transformGeoData(ele); }).join(','), ")");
45
+ }
46
+ else {
47
+ var geoType_1 = GeoTypes.find(function (ele) { return ele.type === element_1.type; });
48
+ if (!geoType_1) {
49
+ throw new Error("".concat(element_1.type, " is not supported in MySQL"));
50
+ }
51
+ var multiGeoType = GeoTypes.find(function (ele) { return ele.element === geoType_1.type && ele.multiple; });
52
+ return " ".concat(multiGeoType.name, "(").concat(data.map(function (ele) { return transformGeoData(ele); }).join(','), ")");
53
+ }
54
+ }
55
+ else {
56
+ var type_1 = data.type, coordinate = data.coordinate;
57
+ var geoType = GeoTypes.find(function (ele) { return ele.type === type_1; });
58
+ if (!geoType) {
59
+ throw new Error("".concat(data.type, " is not supported in MySQL"));
60
+ }
61
+ var element_2 = geoType.element, name_1 = geoType.name;
62
+ if (!element_2) {
63
+ // Point
64
+ return " ".concat(name_1, "(").concat(coordinate.join(','), ")");
65
+ }
66
+ // Polygon or Linestring
67
+ return " ".concat(name_1, "(").concat(coordinate.map(function (ele) { return transformGeoData({
68
+ type: element_2,
69
+ coordinate: ele,
70
+ }); }), ")");
71
+ }
72
+ }
73
+ var MySqlTranslator = /** @class */ (function (_super) {
74
+ tslib_1.__extends(MySqlTranslator, _super);
75
+ function MySqlTranslator(schema) {
76
+ var _this = _super.call(this, schema) || this;
77
+ _this.maxAliasLength = 63;
78
+ // MySQL为geometry属性默认创建索引
79
+ _this.makeUpSchema();
80
+ return _this;
81
+ }
82
+ MySqlTranslator.prototype.getDefaultSelectFilter = function (alias, option) {
83
+ if (option === null || option === void 0 ? void 0 : option.includedDeleted) {
84
+ return '';
85
+ }
86
+ return " (`".concat(alias, "`.`$$deleteAt$$` is null)");
87
+ };
88
+ MySqlTranslator.prototype.makeUpSchema = function () {
89
+ for (var entity in this.schema) {
90
+ var _a = this.schema[entity], attributes = _a.attributes, indexes = _a.indexes;
91
+ var geoIndexes = [];
92
+ var _loop_1 = function (attr) {
93
+ if (attributes[attr].type === 'geometry') {
94
+ var geoIndex = indexes === null || indexes === void 0 ? void 0 : indexes.find(function (idx) {
95
+ var _a;
96
+ return ((_a = idx.config) === null || _a === void 0 ? void 0 : _a.type) === 'spatial' && idx.attributes.find(function (attrDef) { return attrDef.name === attr; });
97
+ });
98
+ if (!geoIndex) {
99
+ geoIndexes.push({
100
+ name: "".concat(entity, "_geo_").concat(attr),
101
+ attributes: [{
102
+ name: attr,
103
+ }],
104
+ config: {
105
+ type: 'spatial',
106
+ }
107
+ });
108
+ }
109
+ }
110
+ };
111
+ for (var attr in attributes) {
112
+ _loop_1(attr);
113
+ }
114
+ if (geoIndexes.length > 0) {
115
+ if (indexes) {
116
+ indexes.push.apply(indexes, geoIndexes);
117
+ }
118
+ else {
119
+ (0, lodash_1.assign)(this.schema[entity], {
120
+ indexes: geoIndexes,
121
+ });
122
+ }
123
+ }
124
+ }
125
+ };
126
+ MySqlTranslator.prototype.populateDataTypeDef = function (type, params) {
127
+ if (['date', 'datetime', 'time', 'sequence'].includes(type)) {
128
+ return 'bigint ';
129
+ }
130
+ if (['object', 'array'].includes(type)) {
131
+ return 'text ';
132
+ }
133
+ if (['image', 'function'].includes(type)) {
134
+ return 'text ';
135
+ }
136
+ if (type === 'ref') {
137
+ return 'char(36)';
138
+ }
139
+ if (MySqlTranslator.withLengthDataTypes.includes(type)) {
140
+ if (params) {
141
+ var length_1 = params.length;
142
+ return "".concat(type, "(").concat(length_1, ") ");
143
+ }
144
+ else {
145
+ var length_2 = MySqlTranslator.dataTypeDefaults[type].length;
146
+ return "".concat(type, "(").concat(length_2, ") ");
147
+ }
148
+ }
149
+ if (MySqlTranslator.withPrecisionDataTypes.includes(type)) {
150
+ if (params) {
151
+ var precision = params.precision, scale = params.scale;
152
+ if (typeof scale === 'number') {
153
+ return "".concat(type, "(").concat(precision, ", ").concat(scale, ") ");
154
+ }
155
+ return "".concat(type, "(").concat(precision, ")");
156
+ }
157
+ else {
158
+ var _a = MySqlTranslator.dataTypeDefaults[type], precision = _a.precision, scale = _a.scale;
159
+ if (typeof scale === 'number') {
160
+ return "".concat(type, "(").concat(precision, ", ").concat(scale, ") ");
161
+ }
162
+ return "".concat(type, "(").concat(precision, ")");
163
+ }
164
+ }
165
+ if (MySqlTranslator.withWidthDataTypes.includes(type)) {
166
+ (0, assert_1.default)(type === 'int');
167
+ var width = params.width;
168
+ switch (width) {
169
+ case 1: {
170
+ return 'tinyint';
171
+ }
172
+ case 2: {
173
+ return 'smallint';
174
+ }
175
+ case 3: {
176
+ return 'mediumint';
177
+ }
178
+ case 4: {
179
+ return 'int';
180
+ }
181
+ default: {
182
+ return 'bigint';
183
+ }
184
+ }
185
+ }
186
+ return "".concat(type, " ");
187
+ };
188
+ MySqlTranslator.prototype.translateAttrProjection = function (dataType, alias, attr) {
189
+ switch (dataType) {
190
+ case 'geometry': {
191
+ return " st_astext(`".concat(alias, "`.`").concat(attr, "`)");
192
+ }
193
+ default: {
194
+ return " `".concat(alias, "`.`").concat(attr, "`");
195
+ }
196
+ }
197
+ };
198
+ MySqlTranslator.prototype.translateAttrValue = function (dataType, value) {
199
+ if (value === null || value === undefined) {
200
+ return 'null';
201
+ }
202
+ switch (dataType) {
203
+ case 'geometry': {
204
+ return transformGeoData(value);
205
+ }
206
+ case 'datetime':
207
+ case 'time':
208
+ case 'date': {
209
+ if (value instanceof Date) {
210
+ return "".concat(value.valueOf());
211
+ }
212
+ else if (typeof value === 'number') {
213
+ return "".concat(value);
214
+ }
215
+ return "'".concat((new Date(value)).valueOf(), "'");
216
+ }
217
+ case 'object':
218
+ case 'array': {
219
+ return this.escapeStringValue(JSON.stringify(value));
220
+ }
221
+ /* case 'function': {
222
+ return `'${Buffer.from(value.toString()).toString('base64')}'`;
223
+ } */
224
+ default: {
225
+ if (typeof value === 'string') {
226
+ return this.escapeStringValue(value);
227
+ }
228
+ return value;
229
+ }
230
+ }
231
+ };
232
+ MySqlTranslator.prototype.translateFullTextSearch = function (value, entity, alias) {
233
+ var $search = value.$search;
234
+ var indexes = this.schema[entity].indexes;
235
+ var ftIndex = indexes && indexes.find(function (ele) {
236
+ var config = ele.config;
237
+ return config && config.type === 'fulltext';
238
+ });
239
+ (0, assert_1.default)(ftIndex);
240
+ var attributes = ftIndex.attributes;
241
+ var columns2 = attributes.map(function (_a) {
242
+ var name = _a.name;
243
+ return "".concat(alias, ".").concat(name);
244
+ });
245
+ return " match(".concat(columns2.join(','), ") against ('").concat($search, "' in natural language mode)");
246
+ };
247
+ MySqlTranslator.prototype.translateCreateEntity = function (entity, options) {
248
+ var _this = this;
249
+ var replace = options === null || options === void 0 ? void 0 : options.replace;
250
+ var schema = this.schema;
251
+ var entityDef = schema[entity];
252
+ var storageName = entityDef.storageName, attributes = entityDef.attributes, indexes = entityDef.indexes, view = entityDef.view;
253
+ var hasSequence = false;
254
+ // todo view暂还不支持
255
+ var entityType = view ? 'view' : 'table';
256
+ var sql = "create ".concat(entityType, " ");
257
+ if (storageName) {
258
+ sql += "`".concat(storageName, "` ");
259
+ }
260
+ else {
261
+ sql += "`".concat(entity, "` ");
262
+ }
263
+ if (view) {
264
+ throw new Error(' view unsupported yet');
265
+ }
266
+ else {
267
+ sql += '(';
268
+ // 翻译所有的属性
269
+ Object.keys(attributes).forEach(function (attr, idx) {
270
+ var attrDef = attributes[attr];
271
+ var type = attrDef.type, params = attrDef.params, defaultValue = attrDef.default, unique = attrDef.unique, notNull = attrDef.notNull, sequenceStart = attrDef.sequenceStart;
272
+ sql += "`".concat(attr, "` ");
273
+ sql += _this.populateDataTypeDef(type, params);
274
+ if (notNull || type === 'geometry') {
275
+ sql += ' not null ';
276
+ }
277
+ if (unique) {
278
+ sql += ' unique ';
279
+ }
280
+ if (sequenceStart) {
281
+ if (hasSequence) {
282
+ throw new Error("\u300C".concat(entity, "\u300D\u53EA\u80FD\u6709\u4E00\u4E2Asequence\u5217"));
283
+ }
284
+ hasSequence = sequenceStart;
285
+ sql += ' auto_increment unique ';
286
+ }
287
+ if (defaultValue !== undefined) {
288
+ (0, assert_1.default)(type !== 'ref');
289
+ sql += " default ".concat(_this.translateAttrValue(type, defaultValue));
290
+ }
291
+ if (attr === 'id') {
292
+ sql += ' primary key';
293
+ }
294
+ if (idx < Object.keys(attributes).length - 1) {
295
+ sql += ',\n';
296
+ }
297
+ });
298
+ // 翻译索引信息
299
+ if (indexes) {
300
+ sql += ',\n';
301
+ indexes.forEach(function (_a, idx) {
302
+ var name = _a.name, attributes = _a.attributes, config = _a.config;
303
+ var _b = config || {}, unique = _b.unique, type = _b.type, parser = _b.parser;
304
+ // 因为有deleteAt的存在,这里的unique没意义,只能框架自己去建立checker来处理
305
+ /* if (unique) {
306
+ sql += ' unique ';
307
+ }
308
+ else */ if (type === 'fulltext') {
309
+ sql += ' fulltext ';
310
+ }
311
+ else if (type === 'spatial') {
312
+ sql += ' spatial ';
313
+ }
314
+ sql += "index ".concat(name, " ");
315
+ if (type === 'hash') {
316
+ sql += " using hash ";
317
+ }
318
+ sql += '(';
319
+ var includeDeleteAt = false;
320
+ attributes.forEach(function (_a, idx2) {
321
+ var name = _a.name, size = _a.size, direction = _a.direction;
322
+ sql += "`".concat(name, "`");
323
+ if (size) {
324
+ sql += " (".concat(size, ")");
325
+ }
326
+ if (direction) {
327
+ sql += " ".concat(direction);
328
+ }
329
+ if (idx2 < attributes.length - 1) {
330
+ sql += ',';
331
+ }
332
+ if (name === '$$deleteAt$$') {
333
+ includeDeleteAt = true;
334
+ }
335
+ });
336
+ if (!includeDeleteAt && !type) {
337
+ sql += ', $$deleteAt$$';
338
+ }
339
+ sql += ')';
340
+ if (parser) {
341
+ sql += " with parser ".concat(parser);
342
+ }
343
+ if (idx < indexes.length - 1) {
344
+ sql += ',\n';
345
+ }
346
+ });
347
+ }
348
+ }
349
+ sql += ')';
350
+ if (typeof hasSequence === 'number') {
351
+ sql += "auto_increment = ".concat(hasSequence);
352
+ }
353
+ if (!replace) {
354
+ return [sql];
355
+ }
356
+ return ["drop ".concat(entityType, " if exists `").concat(storageName || entity, "`;"), sql];
357
+ };
358
+ MySqlTranslator.prototype.translateFnName = function (fnName, argumentNumber) {
359
+ switch (fnName) {
360
+ case '$add': {
361
+ var result = '%s';
362
+ while (--argumentNumber > 0) {
363
+ result += ' + %s';
364
+ }
365
+ return result;
366
+ }
367
+ case '$subtract': {
368
+ (0, assert_1.default)(argumentNumber === 2);
369
+ return '%s - %s';
370
+ }
371
+ case '$multiply': {
372
+ var result = '%s';
373
+ while (--argumentNumber > 0) {
374
+ result += ' * %s';
375
+ }
376
+ return result;
377
+ }
378
+ case '$divide': {
379
+ (0, assert_1.default)(argumentNumber === 2);
380
+ return '%s / %s';
381
+ }
382
+ case '$abs': {
383
+ return 'ABS(%s)';
384
+ }
385
+ case '$round': {
386
+ (0, assert_1.default)(argumentNumber === 2);
387
+ return 'ROUND(%s, %s)';
388
+ }
389
+ case '$ceil': {
390
+ return 'CEIL(%s)';
391
+ }
392
+ case '$floor': {
393
+ return 'FLOOR(%s)';
394
+ }
395
+ case '$pow': {
396
+ (0, assert_1.default)(argumentNumber === 2);
397
+ return 'POW(%s, %s)';
398
+ }
399
+ case '$gt': {
400
+ (0, assert_1.default)(argumentNumber === 2);
401
+ return '%s > %s';
402
+ }
403
+ case '$gte': {
404
+ (0, assert_1.default)(argumentNumber === 2);
405
+ return '%s >= %s';
406
+ }
407
+ case '$lt': {
408
+ (0, assert_1.default)(argumentNumber === 2);
409
+ return '%s < %s';
410
+ }
411
+ case '$lte': {
412
+ return '%s <= %s';
413
+ }
414
+ case '$eq': {
415
+ (0, assert_1.default)(argumentNumber === 2);
416
+ return '%s = %s';
417
+ }
418
+ case '$ne': {
419
+ (0, assert_1.default)(argumentNumber === 2);
420
+ return '%s <> %s';
421
+ }
422
+ case '$startsWith': {
423
+ (0, assert_1.default)(argumentNumber === 2);
424
+ return '%s like CONCAT(%s, \'%\')';
425
+ }
426
+ case '$endsWith': {
427
+ (0, assert_1.default)(argumentNumber === 2);
428
+ return '%s like CONCAT(\'%\', %s)';
429
+ }
430
+ case '$includes': {
431
+ (0, assert_1.default)(argumentNumber === 2);
432
+ return '%s like CONCAT(\'%\', %s, \'%\')';
433
+ }
434
+ case '$true': {
435
+ return '%s = true';
436
+ }
437
+ case '$false': {
438
+ return '%s = false';
439
+ }
440
+ case '$and': {
441
+ var result = '';
442
+ for (var iter = 0; iter < argumentNumber; iter++) {
443
+ result += '%s';
444
+ if (iter < argumentNumber - 1) {
445
+ result += ' and ';
446
+ }
447
+ }
448
+ return result;
449
+ }
450
+ case '$or': {
451
+ var result = '';
452
+ for (var iter = 0; iter < argumentNumber; iter++) {
453
+ result += '%s';
454
+ if (iter < argumentNumber - 1) {
455
+ result += ' or ';
456
+ }
457
+ }
458
+ return result;
459
+ }
460
+ case '$not': {
461
+ return 'not %s';
462
+ }
463
+ case '$year': {
464
+ return 'YEAR(%s)';
465
+ }
466
+ case '$month': {
467
+ return 'MONTH(%s)';
468
+ }
469
+ case '$weekday': {
470
+ return 'WEEKDAY(%s)';
471
+ }
472
+ case '$weekOfYear': {
473
+ return 'WEEKOFYEAR(%s)';
474
+ }
475
+ case '$day': {
476
+ return 'DAY(%s)';
477
+ }
478
+ case '$dayOfMonth': {
479
+ return 'DAYOFMONTH(%s)';
480
+ }
481
+ case '$dayOfWeek': {
482
+ return 'DAYOFWEEK(%s)';
483
+ }
484
+ case '$dayOfYear': {
485
+ return 'DAYOFYEAR(%s)';
486
+ }
487
+ case '$dateDiff': {
488
+ (0, assert_1.default)(argumentNumber === 3);
489
+ return 'DATEDIFF(%s, %s, %s)';
490
+ }
491
+ case '$contains': {
492
+ (0, assert_1.default)(argumentNumber === 2);
493
+ return 'ST_CONTAINS(%s, %s)';
494
+ }
495
+ case '$distance': {
496
+ (0, assert_1.default)(argumentNumber === 2);
497
+ return 'ST_DISTANCE(%s, %s)';
498
+ }
499
+ case '$concat': {
500
+ var result = ' concat(%s';
501
+ while (--argumentNumber > 0) {
502
+ result += ', %s';
503
+ }
504
+ result += ')';
505
+ return result;
506
+ }
507
+ default: {
508
+ throw new Error("unrecoganized function ".concat(fnName));
509
+ }
510
+ }
511
+ };
512
+ MySqlTranslator.prototype.translateAttrInExpression = function (entity, attr, exprText) {
513
+ var attributes = this.schema[entity].attributes;
514
+ var type = attributes[attr].type;
515
+ if (['date', 'time', 'datetime'].includes(type)) {
516
+ // 从unix时间戵转成date类型参加expr的运算
517
+ return "from_unixtime(".concat(exprText, " / 1000)");
518
+ }
519
+ return exprText;
520
+ };
521
+ MySqlTranslator.prototype.translateExpression = function (entity, alias, expression, refDict) {
522
+ var _this = this;
523
+ var translateConstant = function (constant) {
524
+ if (constant instanceof Date) {
525
+ return " from_unixtime(".concat(constant.valueOf(), "/1000)");
526
+ }
527
+ else if (typeof constant === 'string') {
528
+ return " '".concat(constant, "'");
529
+ }
530
+ else {
531
+ (0, assert_1.default)(typeof constant === 'number');
532
+ return " ".concat(constant);
533
+ }
534
+ };
535
+ var translateInner = function (expr) {
536
+ var k = Object.keys(expr);
537
+ var result;
538
+ if (k.includes('#attr')) {
539
+ var attrText = "`".concat(alias, "`.`").concat((expr)['#attr'], "`");
540
+ result = _this.translateAttrInExpression(entity, (expr)['#attr'], attrText);
541
+ }
542
+ else if (k.includes('#refId')) {
543
+ var refId = (expr)['#refId'];
544
+ var refAttr = (expr)['#refAttr'];
545
+ (0, assert_1.default)(refDict[refId]);
546
+ var attrText = "`".concat(refDict[refId][0], "`.`").concat(refAttr, "`");
547
+ result = _this.translateAttrInExpression(entity, (expr)['#refAttr'], attrText);
548
+ }
549
+ else {
550
+ (0, assert_1.default)(k.length === 1);
551
+ if ((expr)[k[0]] instanceof Array) {
552
+ var fnName = _this.translateFnName(k[0], (expr)[k[0]].length);
553
+ var args = [fnName];
554
+ args.push.apply(args, (expr)[k[0]].map(function (ele) {
555
+ if (['string', 'number'].includes(typeof ele) || ele instanceof Date) {
556
+ return translateConstant(ele);
557
+ }
558
+ else {
559
+ return translateInner(ele);
560
+ }
561
+ }));
562
+ result = util_1.format.apply(null, args);
563
+ }
564
+ else {
565
+ var fnName = _this.translateFnName(k[0], 1);
566
+ var args = [fnName];
567
+ var arg = (expr)[k[0]];
568
+ if (['string', 'number'].includes(typeof arg) || arg instanceof Date) {
569
+ args.push(translateConstant(arg));
570
+ }
571
+ else {
572
+ args.push(translateInner(arg));
573
+ }
574
+ result = util_1.format.apply(null, args);
575
+ }
576
+ }
577
+ return result;
578
+ };
579
+ return translateInner(expression);
580
+ };
581
+ MySqlTranslator.prototype.populateSelectStmt = function (projectionText, fromText, aliasDict, filterText, sorterText, groupByText, indexFrom, count, option) {
582
+ // todo hint of use index
583
+ var sql = "select ".concat(projectionText, " from ").concat(fromText);
584
+ if (filterText) {
585
+ sql += " where ".concat(filterText);
586
+ }
587
+ if (sorterText) {
588
+ sql += " order by ".concat(sorterText);
589
+ }
590
+ if (groupByText) {
591
+ sql += " group by ".concat(groupByText);
592
+ }
593
+ if (typeof indexFrom === 'number') {
594
+ (0, assert_1.default)(typeof count === 'number');
595
+ sql += " limit ".concat(indexFrom, ", ").concat(count);
596
+ }
597
+ if (option === null || option === void 0 ? void 0 : option.forUpdate) {
598
+ sql += ' for update';
599
+ }
600
+ return sql;
601
+ };
602
+ MySqlTranslator.prototype.populateUpdateStmt = function (updateText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
603
+ // todo using index
604
+ (0, assert_1.default)(updateText);
605
+ var sql = "update ".concat(fromText, " set ").concat(updateText);
606
+ if (filterText) {
607
+ sql += " where ".concat(filterText);
608
+ }
609
+ if (sorterText) {
610
+ sql += " order by ".concat(sorterText);
611
+ }
612
+ if (typeof indexFrom === 'number') {
613
+ (0, assert_1.default)(typeof count === 'number');
614
+ sql += " limit ".concat(indexFrom, ", ").concat(count);
615
+ }
616
+ return sql;
617
+ };
618
+ MySqlTranslator.prototype.populateRemoveStmt = function (removeText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
619
+ // todo using index
620
+ var alias = aliasDict['./'];
621
+ var now = Date.now();
622
+ var sql = "update ".concat(fromText, " set `").concat(alias, "`.`$$deleteAt$$` = '").concat(now, "'");
623
+ if (filterText) {
624
+ sql += " where ".concat(filterText);
625
+ }
626
+ if (sorterText) {
627
+ sql += " order by ".concat(sorterText);
628
+ }
629
+ if (typeof indexFrom === 'number') {
630
+ (0, assert_1.default)(typeof count === 'number');
631
+ sql += " limit ".concat(indexFrom, ", ").concat(count);
632
+ }
633
+ return sql;
634
+ };
635
+ MySqlTranslator.supportedDataTypes = [
636
+ // numeric types
637
+ "bit",
638
+ "int",
639
+ "integer",
640
+ "tinyint",
641
+ "smallint",
642
+ "mediumint",
643
+ "bigint",
644
+ "float",
645
+ "double",
646
+ "double precision",
647
+ "real",
648
+ "decimal",
649
+ "dec",
650
+ "numeric",
651
+ "fixed",
652
+ "bool",
653
+ "boolean",
654
+ // date and time types
655
+ "date",
656
+ "datetime",
657
+ "timestamp",
658
+ "time",
659
+ "year",
660
+ // string types
661
+ "char",
662
+ "nchar",
663
+ "national char",
664
+ "varchar",
665
+ "nvarchar",
666
+ "national varchar",
667
+ "blob",
668
+ "text",
669
+ "tinyblob",
670
+ "tinytext",
671
+ "mediumblob",
672
+ "mediumtext",
673
+ "longblob",
674
+ "longtext",
675
+ "enum",
676
+ "set",
677
+ "binary",
678
+ "varbinary",
679
+ // json data type
680
+ "json",
681
+ // spatial data types
682
+ "geometry",
683
+ "point",
684
+ "linestring",
685
+ "polygon",
686
+ "multipoint",
687
+ "multilinestring",
688
+ "multipolygon",
689
+ "geometrycollection"
690
+ ];
691
+ MySqlTranslator.spatialTypes = [
692
+ "geometry",
693
+ "point",
694
+ "linestring",
695
+ "polygon",
696
+ "multipoint",
697
+ "multilinestring",
698
+ "multipolygon",
699
+ "geometrycollection"
700
+ ];
701
+ MySqlTranslator.withLengthDataTypes = [
702
+ "char",
703
+ "varchar",
704
+ "nvarchar",
705
+ "binary",
706
+ "varbinary"
707
+ ];
708
+ MySqlTranslator.withPrecisionDataTypes = [
709
+ "decimal",
710
+ "dec",
711
+ "numeric",
712
+ "fixed",
713
+ "float",
714
+ "double",
715
+ "double precision",
716
+ "real",
717
+ "time",
718
+ "datetime",
719
+ "timestamp"
720
+ ];
721
+ MySqlTranslator.withScaleDataTypes = [
722
+ "decimal",
723
+ "dec",
724
+ "numeric",
725
+ "fixed",
726
+ "float",
727
+ "double",
728
+ "double precision",
729
+ "real"
730
+ ];
731
+ MySqlTranslator.unsignedAndZerofillTypes = [
732
+ "int",
733
+ "integer",
734
+ "smallint",
735
+ "tinyint",
736
+ "mediumint",
737
+ "bigint",
738
+ "decimal",
739
+ "dec",
740
+ "numeric",
741
+ "fixed",
742
+ "float",
743
+ "double",
744
+ "double precision",
745
+ "real"
746
+ ];
747
+ MySqlTranslator.withWidthDataTypes = [
748
+ 'int',
749
+ ];
750
+ MySqlTranslator.dataTypeDefaults = {
751
+ "varchar": { length: 255 },
752
+ "nvarchar": { length: 255 },
753
+ "national varchar": { length: 255 },
754
+ "char": { length: 1 },
755
+ "binary": { length: 1 },
756
+ "varbinary": { length: 255 },
757
+ "decimal": { precision: 10, scale: 0 },
758
+ "dec": { precision: 10, scale: 0 },
759
+ "numeric": { precision: 10, scale: 0 },
760
+ "fixed": { precision: 10, scale: 0 },
761
+ "float": { precision: 12 },
762
+ "double": { precision: 22 },
763
+ "time": { precision: 0 },
764
+ "datetime": { precision: 0 },
765
+ "timestamp": { precision: 0 },
766
+ "bit": { width: 1 },
767
+ "int": { width: 11 },
768
+ "integer": { width: 11 },
769
+ "tinyint": { width: 4 },
770
+ "smallint": { width: 6 },
771
+ "mediumint": { width: 9 },
772
+ "bigint": { width: 20 }
773
+ };
774
+ return MySqlTranslator;
775
+ }(sqlTranslator_1.SqlTranslator));
776
+ exports.MySqlTranslator = MySqlTranslator;