oak-db 2.0.2 → 2.1.0
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.
- package/README.md +36 -36
- package/lib/MySQL/connector.d.ts +15 -15
- package/lib/MySQL/connector.js +124 -124
- package/lib/MySQL/store.d.ts +29 -29
- package/lib/MySQL/store.js +377 -376
- package/lib/MySQL/translator.d.ts +104 -104
- package/lib/MySQL/translator.js +732 -732
- package/lib/MySQL/types/Configuration.d.ts +11 -11
- package/lib/MySQL/types/Configuration.js +2 -2
- package/lib/index.d.ts +2 -2
- package/lib/index.js +4 -4
- package/lib/sqlTranslator.d.ts +51 -51
- package/lib/sqlTranslator.js +755 -755
- package/lib/types/Translator.js +1 -1
- package/package.json +36 -36
package/lib/MySQL/translator.js
CHANGED
|
@@ -1,732 +1,732 @@
|
|
|
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
|
-
return '%s + %s';
|
|
361
|
-
}
|
|
362
|
-
case '$subtract': {
|
|
363
|
-
return '%s - %s';
|
|
364
|
-
}
|
|
365
|
-
case '$multiply': {
|
|
366
|
-
return '%s * %s';
|
|
367
|
-
}
|
|
368
|
-
case '$divide': {
|
|
369
|
-
return '%s / %s';
|
|
370
|
-
}
|
|
371
|
-
case '$abs': {
|
|
372
|
-
return 'ABS(%s)';
|
|
373
|
-
}
|
|
374
|
-
case '$round': {
|
|
375
|
-
return 'ROUND(%s, %s)';
|
|
376
|
-
}
|
|
377
|
-
case '$ceil': {
|
|
378
|
-
return 'CEIL(%s)';
|
|
379
|
-
}
|
|
380
|
-
case '$floor': {
|
|
381
|
-
return 'FLOOR(%s)';
|
|
382
|
-
}
|
|
383
|
-
case '$pow': {
|
|
384
|
-
return 'POW(%s, %s)';
|
|
385
|
-
}
|
|
386
|
-
case '$gt': {
|
|
387
|
-
return '%s > %s';
|
|
388
|
-
}
|
|
389
|
-
case '$gte': {
|
|
390
|
-
return '%s >= %s';
|
|
391
|
-
}
|
|
392
|
-
case '$lt': {
|
|
393
|
-
return '%s < %s';
|
|
394
|
-
}
|
|
395
|
-
case '$lte': {
|
|
396
|
-
return '%s <= %s';
|
|
397
|
-
}
|
|
398
|
-
case '$eq': {
|
|
399
|
-
return '%s = %s';
|
|
400
|
-
}
|
|
401
|
-
case '$ne': {
|
|
402
|
-
return '%s <> %s';
|
|
403
|
-
}
|
|
404
|
-
case '$startsWith': {
|
|
405
|
-
return '%s like CONCAT(%s, \'%\')';
|
|
406
|
-
}
|
|
407
|
-
case '$endsWith': {
|
|
408
|
-
return '%s like CONCAT(\'%\', %s)';
|
|
409
|
-
}
|
|
410
|
-
case '$includes': {
|
|
411
|
-
return '%s like CONCAT(\'%\', %s, \'%\')';
|
|
412
|
-
}
|
|
413
|
-
case '$true': {
|
|
414
|
-
return '%s = true';
|
|
415
|
-
}
|
|
416
|
-
case '$false': {
|
|
417
|
-
return '%s = false';
|
|
418
|
-
}
|
|
419
|
-
case '$and': {
|
|
420
|
-
var result = '';
|
|
421
|
-
for (var iter = 0; iter < argumentNumber; iter++) {
|
|
422
|
-
result += '%s';
|
|
423
|
-
if (iter < argumentNumber - 1) {
|
|
424
|
-
result += ' and ';
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
return result;
|
|
428
|
-
}
|
|
429
|
-
case '$or': {
|
|
430
|
-
var result = '';
|
|
431
|
-
for (var iter = 0; iter < argumentNumber; iter++) {
|
|
432
|
-
result += '%s';
|
|
433
|
-
if (iter < argumentNumber - 1) {
|
|
434
|
-
result += ' or ';
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return result;
|
|
438
|
-
}
|
|
439
|
-
case '$not': {
|
|
440
|
-
return 'not %s';
|
|
441
|
-
}
|
|
442
|
-
case '$year': {
|
|
443
|
-
return 'YEAR(%s)';
|
|
444
|
-
}
|
|
445
|
-
case '$month': {
|
|
446
|
-
return 'MONTH(%s)';
|
|
447
|
-
}
|
|
448
|
-
case '$weekday': {
|
|
449
|
-
return 'WEEKDAY(%s)';
|
|
450
|
-
}
|
|
451
|
-
case '$weekOfYear': {
|
|
452
|
-
return 'WEEKOFYEAR(%s)';
|
|
453
|
-
}
|
|
454
|
-
case '$day': {
|
|
455
|
-
return 'DAY(%s)';
|
|
456
|
-
}
|
|
457
|
-
case '$dayOfMonth': {
|
|
458
|
-
return 'DAYOFMONTH(%s)';
|
|
459
|
-
}
|
|
460
|
-
case '$dayOfWeek': {
|
|
461
|
-
return 'DAYOFWEEK(%s)';
|
|
462
|
-
}
|
|
463
|
-
case '$dayOfYear': {
|
|
464
|
-
return 'DAYOFYEAR(%s)';
|
|
465
|
-
}
|
|
466
|
-
case '$dateDiff': {
|
|
467
|
-
return 'DATEDIFF(%s, %s, %s)';
|
|
468
|
-
}
|
|
469
|
-
case '$contains': {
|
|
470
|
-
return 'ST_CONTAINS(%s, %s)';
|
|
471
|
-
}
|
|
472
|
-
case '$distance': {
|
|
473
|
-
return 'ST_DISTANCE(%s, %s)';
|
|
474
|
-
}
|
|
475
|
-
default: {
|
|
476
|
-
throw new Error("unrecoganized function ".concat(fnName));
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
};
|
|
480
|
-
MySqlTranslator.prototype.translateExpression = function (alias, expression, refDict) {
|
|
481
|
-
var _this = this;
|
|
482
|
-
var translateConstant = function (constant) {
|
|
483
|
-
if (typeof constant === 'string') {
|
|
484
|
-
return " ".concat(new Date(constant).valueOf());
|
|
485
|
-
}
|
|
486
|
-
else if (constant instanceof Date) {
|
|
487
|
-
return " ".concat(constant.valueOf());
|
|
488
|
-
}
|
|
489
|
-
else {
|
|
490
|
-
(0, assert_1.default)(typeof constant === 'number');
|
|
491
|
-
return " ".concat(constant);
|
|
492
|
-
}
|
|
493
|
-
};
|
|
494
|
-
var translateInner = function (expr) {
|
|
495
|
-
var k = Object.keys(expr);
|
|
496
|
-
var result;
|
|
497
|
-
if (k.includes('#attr')) {
|
|
498
|
-
var attrText = "`".concat(alias, "`.`").concat((expr)['#attr'], "`");
|
|
499
|
-
result = attrText;
|
|
500
|
-
}
|
|
501
|
-
else if (k.includes('#refId')) {
|
|
502
|
-
var refId = (expr)['#refId'];
|
|
503
|
-
var refAttr = (expr)['#refAttr'];
|
|
504
|
-
(0, assert_1.default)(refDict[refId]);
|
|
505
|
-
var attrText = "`".concat(refDict[refId], "`.`").concat(refAttr, "`");
|
|
506
|
-
result = attrText;
|
|
507
|
-
}
|
|
508
|
-
else {
|
|
509
|
-
(0, assert_1.default)(k.length === 1);
|
|
510
|
-
if ((expr)[k[0]] instanceof Array) {
|
|
511
|
-
var fnName = _this.translateFnName(k[0], (expr)[k[0]].length);
|
|
512
|
-
var args = [fnName];
|
|
513
|
-
args.push.apply(args, (expr)[k[0]].map(function (ele) {
|
|
514
|
-
if (['string', 'number'].includes(typeof ele) || ele instanceof Date) {
|
|
515
|
-
return translateConstant(ele);
|
|
516
|
-
}
|
|
517
|
-
else {
|
|
518
|
-
return translateInner(ele);
|
|
519
|
-
}
|
|
520
|
-
}));
|
|
521
|
-
result = util_1.format.apply(null, args);
|
|
522
|
-
}
|
|
523
|
-
else {
|
|
524
|
-
var fnName = _this.translateFnName(k[0], 1);
|
|
525
|
-
var args = [fnName];
|
|
526
|
-
var arg = (expr)[k[0]];
|
|
527
|
-
if (['string', 'number'].includes(typeof arg) || arg instanceof Date) {
|
|
528
|
-
args.push(translateConstant(arg));
|
|
529
|
-
}
|
|
530
|
-
else {
|
|
531
|
-
args.push(translateInner(arg));
|
|
532
|
-
}
|
|
533
|
-
result = util_1.format.apply(null, args);
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
return result;
|
|
537
|
-
};
|
|
538
|
-
return translateInner(expression);
|
|
539
|
-
};
|
|
540
|
-
MySqlTranslator.prototype.populateSelectStmt = function (projectionText, fromText, selection, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
541
|
-
// todo hint of use index
|
|
542
|
-
var sql = "select ".concat(projectionText, " from ").concat(fromText);
|
|
543
|
-
if (filterText) {
|
|
544
|
-
sql += " where ".concat(filterText);
|
|
545
|
-
}
|
|
546
|
-
if (sorterText) {
|
|
547
|
-
sql += " order by ".concat(sorterText);
|
|
548
|
-
}
|
|
549
|
-
if (typeof indexFrom === 'number') {
|
|
550
|
-
(0, assert_1.default)(typeof count === 'number');
|
|
551
|
-
sql += " limit ".concat(indexFrom, ", ").concat(count);
|
|
552
|
-
}
|
|
553
|
-
if (option === null || option === void 0 ? void 0 : option.forUpdate) {
|
|
554
|
-
sql += ' for update';
|
|
555
|
-
}
|
|
556
|
-
return sql;
|
|
557
|
-
};
|
|
558
|
-
MySqlTranslator.prototype.populateUpdateStmt = function (updateText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
559
|
-
// todo using index
|
|
560
|
-
(0, assert_1.default)(updateText);
|
|
561
|
-
var sql = "update ".concat(fromText, " set ").concat(updateText);
|
|
562
|
-
if (filterText) {
|
|
563
|
-
sql += " where ".concat(filterText);
|
|
564
|
-
}
|
|
565
|
-
if (sorterText) {
|
|
566
|
-
sql += " order by ".concat(sorterText);
|
|
567
|
-
}
|
|
568
|
-
if (typeof indexFrom === 'number') {
|
|
569
|
-
(0, assert_1.default)(typeof count === 'number');
|
|
570
|
-
sql += " limit ".concat(indexFrom, ", ").concat(count);
|
|
571
|
-
}
|
|
572
|
-
return sql;
|
|
573
|
-
};
|
|
574
|
-
MySqlTranslator.prototype.populateRemoveStmt = function (removeText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
575
|
-
// todo using index
|
|
576
|
-
var alias = aliasDict['./'];
|
|
577
|
-
var now = Date.now();
|
|
578
|
-
var sql = "update ".concat(fromText, " set `").concat(alias, "`.`$$deleteAt$$` = '").concat(now, "'");
|
|
579
|
-
if (filterText) {
|
|
580
|
-
sql += " where ".concat(filterText);
|
|
581
|
-
}
|
|
582
|
-
if (sorterText) {
|
|
583
|
-
sql += " order by ".concat(sorterText);
|
|
584
|
-
}
|
|
585
|
-
if (typeof indexFrom === 'number') {
|
|
586
|
-
(0, assert_1.default)(typeof count === 'number');
|
|
587
|
-
sql += " limit ".concat(indexFrom, ", ").concat(count);
|
|
588
|
-
}
|
|
589
|
-
return sql;
|
|
590
|
-
};
|
|
591
|
-
MySqlTranslator.supportedDataTypes = [
|
|
592
|
-
// numeric types
|
|
593
|
-
"bit",
|
|
594
|
-
"int",
|
|
595
|
-
"integer",
|
|
596
|
-
"tinyint",
|
|
597
|
-
"smallint",
|
|
598
|
-
"mediumint",
|
|
599
|
-
"bigint",
|
|
600
|
-
"float",
|
|
601
|
-
"double",
|
|
602
|
-
"double precision",
|
|
603
|
-
"real",
|
|
604
|
-
"decimal",
|
|
605
|
-
"dec",
|
|
606
|
-
"numeric",
|
|
607
|
-
"fixed",
|
|
608
|
-
"bool",
|
|
609
|
-
"boolean",
|
|
610
|
-
// date and time types
|
|
611
|
-
"date",
|
|
612
|
-
"datetime",
|
|
613
|
-
"timestamp",
|
|
614
|
-
"time",
|
|
615
|
-
"year",
|
|
616
|
-
// string types
|
|
617
|
-
"char",
|
|
618
|
-
"nchar",
|
|
619
|
-
"national char",
|
|
620
|
-
"varchar",
|
|
621
|
-
"nvarchar",
|
|
622
|
-
"national varchar",
|
|
623
|
-
"blob",
|
|
624
|
-
"text",
|
|
625
|
-
"tinyblob",
|
|
626
|
-
"tinytext",
|
|
627
|
-
"mediumblob",
|
|
628
|
-
"mediumtext",
|
|
629
|
-
"longblob",
|
|
630
|
-
"longtext",
|
|
631
|
-
"enum",
|
|
632
|
-
"set",
|
|
633
|
-
"binary",
|
|
634
|
-
"varbinary",
|
|
635
|
-
// json data type
|
|
636
|
-
"json",
|
|
637
|
-
// spatial data types
|
|
638
|
-
"geometry",
|
|
639
|
-
"point",
|
|
640
|
-
"linestring",
|
|
641
|
-
"polygon",
|
|
642
|
-
"multipoint",
|
|
643
|
-
"multilinestring",
|
|
644
|
-
"multipolygon",
|
|
645
|
-
"geometrycollection"
|
|
646
|
-
];
|
|
647
|
-
MySqlTranslator.spatialTypes = [
|
|
648
|
-
"geometry",
|
|
649
|
-
"point",
|
|
650
|
-
"linestring",
|
|
651
|
-
"polygon",
|
|
652
|
-
"multipoint",
|
|
653
|
-
"multilinestring",
|
|
654
|
-
"multipolygon",
|
|
655
|
-
"geometrycollection"
|
|
656
|
-
];
|
|
657
|
-
MySqlTranslator.withLengthDataTypes = [
|
|
658
|
-
"char",
|
|
659
|
-
"varchar",
|
|
660
|
-
"nvarchar",
|
|
661
|
-
"binary",
|
|
662
|
-
"varbinary"
|
|
663
|
-
];
|
|
664
|
-
MySqlTranslator.withPrecisionDataTypes = [
|
|
665
|
-
"decimal",
|
|
666
|
-
"dec",
|
|
667
|
-
"numeric",
|
|
668
|
-
"fixed",
|
|
669
|
-
"float",
|
|
670
|
-
"double",
|
|
671
|
-
"double precision",
|
|
672
|
-
"real",
|
|
673
|
-
"time",
|
|
674
|
-
"datetime",
|
|
675
|
-
"timestamp"
|
|
676
|
-
];
|
|
677
|
-
MySqlTranslator.withScaleDataTypes = [
|
|
678
|
-
"decimal",
|
|
679
|
-
"dec",
|
|
680
|
-
"numeric",
|
|
681
|
-
"fixed",
|
|
682
|
-
"float",
|
|
683
|
-
"double",
|
|
684
|
-
"double precision",
|
|
685
|
-
"real"
|
|
686
|
-
];
|
|
687
|
-
MySqlTranslator.unsignedAndZerofillTypes = [
|
|
688
|
-
"int",
|
|
689
|
-
"integer",
|
|
690
|
-
"smallint",
|
|
691
|
-
"tinyint",
|
|
692
|
-
"mediumint",
|
|
693
|
-
"bigint",
|
|
694
|
-
"decimal",
|
|
695
|
-
"dec",
|
|
696
|
-
"numeric",
|
|
697
|
-
"fixed",
|
|
698
|
-
"float",
|
|
699
|
-
"double",
|
|
700
|
-
"double precision",
|
|
701
|
-
"real"
|
|
702
|
-
];
|
|
703
|
-
MySqlTranslator.withWidthDataTypes = [
|
|
704
|
-
'int',
|
|
705
|
-
];
|
|
706
|
-
MySqlTranslator.dataTypeDefaults = {
|
|
707
|
-
"varchar": { length: 255 },
|
|
708
|
-
"nvarchar": { length: 255 },
|
|
709
|
-
"national varchar": { length: 255 },
|
|
710
|
-
"char": { length: 1 },
|
|
711
|
-
"binary": { length: 1 },
|
|
712
|
-
"varbinary": { length: 255 },
|
|
713
|
-
"decimal": { precision: 10, scale: 0 },
|
|
714
|
-
"dec": { precision: 10, scale: 0 },
|
|
715
|
-
"numeric": { precision: 10, scale: 0 },
|
|
716
|
-
"fixed": { precision: 10, scale: 0 },
|
|
717
|
-
"float": { precision: 12 },
|
|
718
|
-
"double": { precision: 22 },
|
|
719
|
-
"time": { precision: 0 },
|
|
720
|
-
"datetime": { precision: 0 },
|
|
721
|
-
"timestamp": { precision: 0 },
|
|
722
|
-
"bit": { width: 1 },
|
|
723
|
-
"int": { width: 11 },
|
|
724
|
-
"integer": { width: 11 },
|
|
725
|
-
"tinyint": { width: 4 },
|
|
726
|
-
"smallint": { width: 6 },
|
|
727
|
-
"mediumint": { width: 9 },
|
|
728
|
-
"bigint": { width: 20 }
|
|
729
|
-
};
|
|
730
|
-
return MySqlTranslator;
|
|
731
|
-
}(sqlTranslator_1.SqlTranslator));
|
|
732
|
-
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
|
+
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
|
+
return '%s + %s';
|
|
361
|
+
}
|
|
362
|
+
case '$subtract': {
|
|
363
|
+
return '%s - %s';
|
|
364
|
+
}
|
|
365
|
+
case '$multiply': {
|
|
366
|
+
return '%s * %s';
|
|
367
|
+
}
|
|
368
|
+
case '$divide': {
|
|
369
|
+
return '%s / %s';
|
|
370
|
+
}
|
|
371
|
+
case '$abs': {
|
|
372
|
+
return 'ABS(%s)';
|
|
373
|
+
}
|
|
374
|
+
case '$round': {
|
|
375
|
+
return 'ROUND(%s, %s)';
|
|
376
|
+
}
|
|
377
|
+
case '$ceil': {
|
|
378
|
+
return 'CEIL(%s)';
|
|
379
|
+
}
|
|
380
|
+
case '$floor': {
|
|
381
|
+
return 'FLOOR(%s)';
|
|
382
|
+
}
|
|
383
|
+
case '$pow': {
|
|
384
|
+
return 'POW(%s, %s)';
|
|
385
|
+
}
|
|
386
|
+
case '$gt': {
|
|
387
|
+
return '%s > %s';
|
|
388
|
+
}
|
|
389
|
+
case '$gte': {
|
|
390
|
+
return '%s >= %s';
|
|
391
|
+
}
|
|
392
|
+
case '$lt': {
|
|
393
|
+
return '%s < %s';
|
|
394
|
+
}
|
|
395
|
+
case '$lte': {
|
|
396
|
+
return '%s <= %s';
|
|
397
|
+
}
|
|
398
|
+
case '$eq': {
|
|
399
|
+
return '%s = %s';
|
|
400
|
+
}
|
|
401
|
+
case '$ne': {
|
|
402
|
+
return '%s <> %s';
|
|
403
|
+
}
|
|
404
|
+
case '$startsWith': {
|
|
405
|
+
return '%s like CONCAT(%s, \'%\')';
|
|
406
|
+
}
|
|
407
|
+
case '$endsWith': {
|
|
408
|
+
return '%s like CONCAT(\'%\', %s)';
|
|
409
|
+
}
|
|
410
|
+
case '$includes': {
|
|
411
|
+
return '%s like CONCAT(\'%\', %s, \'%\')';
|
|
412
|
+
}
|
|
413
|
+
case '$true': {
|
|
414
|
+
return '%s = true';
|
|
415
|
+
}
|
|
416
|
+
case '$false': {
|
|
417
|
+
return '%s = false';
|
|
418
|
+
}
|
|
419
|
+
case '$and': {
|
|
420
|
+
var result = '';
|
|
421
|
+
for (var iter = 0; iter < argumentNumber; iter++) {
|
|
422
|
+
result += '%s';
|
|
423
|
+
if (iter < argumentNumber - 1) {
|
|
424
|
+
result += ' and ';
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return result;
|
|
428
|
+
}
|
|
429
|
+
case '$or': {
|
|
430
|
+
var result = '';
|
|
431
|
+
for (var iter = 0; iter < argumentNumber; iter++) {
|
|
432
|
+
result += '%s';
|
|
433
|
+
if (iter < argumentNumber - 1) {
|
|
434
|
+
result += ' or ';
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return result;
|
|
438
|
+
}
|
|
439
|
+
case '$not': {
|
|
440
|
+
return 'not %s';
|
|
441
|
+
}
|
|
442
|
+
case '$year': {
|
|
443
|
+
return 'YEAR(%s)';
|
|
444
|
+
}
|
|
445
|
+
case '$month': {
|
|
446
|
+
return 'MONTH(%s)';
|
|
447
|
+
}
|
|
448
|
+
case '$weekday': {
|
|
449
|
+
return 'WEEKDAY(%s)';
|
|
450
|
+
}
|
|
451
|
+
case '$weekOfYear': {
|
|
452
|
+
return 'WEEKOFYEAR(%s)';
|
|
453
|
+
}
|
|
454
|
+
case '$day': {
|
|
455
|
+
return 'DAY(%s)';
|
|
456
|
+
}
|
|
457
|
+
case '$dayOfMonth': {
|
|
458
|
+
return 'DAYOFMONTH(%s)';
|
|
459
|
+
}
|
|
460
|
+
case '$dayOfWeek': {
|
|
461
|
+
return 'DAYOFWEEK(%s)';
|
|
462
|
+
}
|
|
463
|
+
case '$dayOfYear': {
|
|
464
|
+
return 'DAYOFYEAR(%s)';
|
|
465
|
+
}
|
|
466
|
+
case '$dateDiff': {
|
|
467
|
+
return 'DATEDIFF(%s, %s, %s)';
|
|
468
|
+
}
|
|
469
|
+
case '$contains': {
|
|
470
|
+
return 'ST_CONTAINS(%s, %s)';
|
|
471
|
+
}
|
|
472
|
+
case '$distance': {
|
|
473
|
+
return 'ST_DISTANCE(%s, %s)';
|
|
474
|
+
}
|
|
475
|
+
default: {
|
|
476
|
+
throw new Error("unrecoganized function ".concat(fnName));
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
MySqlTranslator.prototype.translateExpression = function (alias, expression, refDict) {
|
|
481
|
+
var _this = this;
|
|
482
|
+
var translateConstant = function (constant) {
|
|
483
|
+
if (typeof constant === 'string') {
|
|
484
|
+
return " ".concat(new Date(constant).valueOf());
|
|
485
|
+
}
|
|
486
|
+
else if (constant instanceof Date) {
|
|
487
|
+
return " ".concat(constant.valueOf());
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
(0, assert_1.default)(typeof constant === 'number');
|
|
491
|
+
return " ".concat(constant);
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
var translateInner = function (expr) {
|
|
495
|
+
var k = Object.keys(expr);
|
|
496
|
+
var result;
|
|
497
|
+
if (k.includes('#attr')) {
|
|
498
|
+
var attrText = "`".concat(alias, "`.`").concat((expr)['#attr'], "`");
|
|
499
|
+
result = attrText;
|
|
500
|
+
}
|
|
501
|
+
else if (k.includes('#refId')) {
|
|
502
|
+
var refId = (expr)['#refId'];
|
|
503
|
+
var refAttr = (expr)['#refAttr'];
|
|
504
|
+
(0, assert_1.default)(refDict[refId]);
|
|
505
|
+
var attrText = "`".concat(refDict[refId], "`.`").concat(refAttr, "`");
|
|
506
|
+
result = attrText;
|
|
507
|
+
}
|
|
508
|
+
else {
|
|
509
|
+
(0, assert_1.default)(k.length === 1);
|
|
510
|
+
if ((expr)[k[0]] instanceof Array) {
|
|
511
|
+
var fnName = _this.translateFnName(k[0], (expr)[k[0]].length);
|
|
512
|
+
var args = [fnName];
|
|
513
|
+
args.push.apply(args, (expr)[k[0]].map(function (ele) {
|
|
514
|
+
if (['string', 'number'].includes(typeof ele) || ele instanceof Date) {
|
|
515
|
+
return translateConstant(ele);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
return translateInner(ele);
|
|
519
|
+
}
|
|
520
|
+
}));
|
|
521
|
+
result = util_1.format.apply(null, args);
|
|
522
|
+
}
|
|
523
|
+
else {
|
|
524
|
+
var fnName = _this.translateFnName(k[0], 1);
|
|
525
|
+
var args = [fnName];
|
|
526
|
+
var arg = (expr)[k[0]];
|
|
527
|
+
if (['string', 'number'].includes(typeof arg) || arg instanceof Date) {
|
|
528
|
+
args.push(translateConstant(arg));
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
args.push(translateInner(arg));
|
|
532
|
+
}
|
|
533
|
+
result = util_1.format.apply(null, args);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return result;
|
|
537
|
+
};
|
|
538
|
+
return translateInner(expression);
|
|
539
|
+
};
|
|
540
|
+
MySqlTranslator.prototype.populateSelectStmt = function (projectionText, fromText, selection, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
541
|
+
// todo hint of use index
|
|
542
|
+
var sql = "select ".concat(projectionText, " from ").concat(fromText);
|
|
543
|
+
if (filterText) {
|
|
544
|
+
sql += " where ".concat(filterText);
|
|
545
|
+
}
|
|
546
|
+
if (sorterText) {
|
|
547
|
+
sql += " order by ".concat(sorterText);
|
|
548
|
+
}
|
|
549
|
+
if (typeof indexFrom === 'number') {
|
|
550
|
+
(0, assert_1.default)(typeof count === 'number');
|
|
551
|
+
sql += " limit ".concat(indexFrom, ", ").concat(count);
|
|
552
|
+
}
|
|
553
|
+
if (option === null || option === void 0 ? void 0 : option.forUpdate) {
|
|
554
|
+
sql += ' for update';
|
|
555
|
+
}
|
|
556
|
+
return sql;
|
|
557
|
+
};
|
|
558
|
+
MySqlTranslator.prototype.populateUpdateStmt = function (updateText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
559
|
+
// todo using index
|
|
560
|
+
(0, assert_1.default)(updateText);
|
|
561
|
+
var sql = "update ".concat(fromText, " set ").concat(updateText);
|
|
562
|
+
if (filterText) {
|
|
563
|
+
sql += " where ".concat(filterText);
|
|
564
|
+
}
|
|
565
|
+
if (sorterText) {
|
|
566
|
+
sql += " order by ".concat(sorterText);
|
|
567
|
+
}
|
|
568
|
+
if (typeof indexFrom === 'number') {
|
|
569
|
+
(0, assert_1.default)(typeof count === 'number');
|
|
570
|
+
sql += " limit ".concat(indexFrom, ", ").concat(count);
|
|
571
|
+
}
|
|
572
|
+
return sql;
|
|
573
|
+
};
|
|
574
|
+
MySqlTranslator.prototype.populateRemoveStmt = function (removeText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
575
|
+
// todo using index
|
|
576
|
+
var alias = aliasDict['./'];
|
|
577
|
+
var now = Date.now();
|
|
578
|
+
var sql = "update ".concat(fromText, " set `").concat(alias, "`.`$$deleteAt$$` = '").concat(now, "'");
|
|
579
|
+
if (filterText) {
|
|
580
|
+
sql += " where ".concat(filterText);
|
|
581
|
+
}
|
|
582
|
+
if (sorterText) {
|
|
583
|
+
sql += " order by ".concat(sorterText);
|
|
584
|
+
}
|
|
585
|
+
if (typeof indexFrom === 'number') {
|
|
586
|
+
(0, assert_1.default)(typeof count === 'number');
|
|
587
|
+
sql += " limit ".concat(indexFrom, ", ").concat(count);
|
|
588
|
+
}
|
|
589
|
+
return sql;
|
|
590
|
+
};
|
|
591
|
+
MySqlTranslator.supportedDataTypes = [
|
|
592
|
+
// numeric types
|
|
593
|
+
"bit",
|
|
594
|
+
"int",
|
|
595
|
+
"integer",
|
|
596
|
+
"tinyint",
|
|
597
|
+
"smallint",
|
|
598
|
+
"mediumint",
|
|
599
|
+
"bigint",
|
|
600
|
+
"float",
|
|
601
|
+
"double",
|
|
602
|
+
"double precision",
|
|
603
|
+
"real",
|
|
604
|
+
"decimal",
|
|
605
|
+
"dec",
|
|
606
|
+
"numeric",
|
|
607
|
+
"fixed",
|
|
608
|
+
"bool",
|
|
609
|
+
"boolean",
|
|
610
|
+
// date and time types
|
|
611
|
+
"date",
|
|
612
|
+
"datetime",
|
|
613
|
+
"timestamp",
|
|
614
|
+
"time",
|
|
615
|
+
"year",
|
|
616
|
+
// string types
|
|
617
|
+
"char",
|
|
618
|
+
"nchar",
|
|
619
|
+
"national char",
|
|
620
|
+
"varchar",
|
|
621
|
+
"nvarchar",
|
|
622
|
+
"national varchar",
|
|
623
|
+
"blob",
|
|
624
|
+
"text",
|
|
625
|
+
"tinyblob",
|
|
626
|
+
"tinytext",
|
|
627
|
+
"mediumblob",
|
|
628
|
+
"mediumtext",
|
|
629
|
+
"longblob",
|
|
630
|
+
"longtext",
|
|
631
|
+
"enum",
|
|
632
|
+
"set",
|
|
633
|
+
"binary",
|
|
634
|
+
"varbinary",
|
|
635
|
+
// json data type
|
|
636
|
+
"json",
|
|
637
|
+
// spatial data types
|
|
638
|
+
"geometry",
|
|
639
|
+
"point",
|
|
640
|
+
"linestring",
|
|
641
|
+
"polygon",
|
|
642
|
+
"multipoint",
|
|
643
|
+
"multilinestring",
|
|
644
|
+
"multipolygon",
|
|
645
|
+
"geometrycollection"
|
|
646
|
+
];
|
|
647
|
+
MySqlTranslator.spatialTypes = [
|
|
648
|
+
"geometry",
|
|
649
|
+
"point",
|
|
650
|
+
"linestring",
|
|
651
|
+
"polygon",
|
|
652
|
+
"multipoint",
|
|
653
|
+
"multilinestring",
|
|
654
|
+
"multipolygon",
|
|
655
|
+
"geometrycollection"
|
|
656
|
+
];
|
|
657
|
+
MySqlTranslator.withLengthDataTypes = [
|
|
658
|
+
"char",
|
|
659
|
+
"varchar",
|
|
660
|
+
"nvarchar",
|
|
661
|
+
"binary",
|
|
662
|
+
"varbinary"
|
|
663
|
+
];
|
|
664
|
+
MySqlTranslator.withPrecisionDataTypes = [
|
|
665
|
+
"decimal",
|
|
666
|
+
"dec",
|
|
667
|
+
"numeric",
|
|
668
|
+
"fixed",
|
|
669
|
+
"float",
|
|
670
|
+
"double",
|
|
671
|
+
"double precision",
|
|
672
|
+
"real",
|
|
673
|
+
"time",
|
|
674
|
+
"datetime",
|
|
675
|
+
"timestamp"
|
|
676
|
+
];
|
|
677
|
+
MySqlTranslator.withScaleDataTypes = [
|
|
678
|
+
"decimal",
|
|
679
|
+
"dec",
|
|
680
|
+
"numeric",
|
|
681
|
+
"fixed",
|
|
682
|
+
"float",
|
|
683
|
+
"double",
|
|
684
|
+
"double precision",
|
|
685
|
+
"real"
|
|
686
|
+
];
|
|
687
|
+
MySqlTranslator.unsignedAndZerofillTypes = [
|
|
688
|
+
"int",
|
|
689
|
+
"integer",
|
|
690
|
+
"smallint",
|
|
691
|
+
"tinyint",
|
|
692
|
+
"mediumint",
|
|
693
|
+
"bigint",
|
|
694
|
+
"decimal",
|
|
695
|
+
"dec",
|
|
696
|
+
"numeric",
|
|
697
|
+
"fixed",
|
|
698
|
+
"float",
|
|
699
|
+
"double",
|
|
700
|
+
"double precision",
|
|
701
|
+
"real"
|
|
702
|
+
];
|
|
703
|
+
MySqlTranslator.withWidthDataTypes = [
|
|
704
|
+
'int',
|
|
705
|
+
];
|
|
706
|
+
MySqlTranslator.dataTypeDefaults = {
|
|
707
|
+
"varchar": { length: 255 },
|
|
708
|
+
"nvarchar": { length: 255 },
|
|
709
|
+
"national varchar": { length: 255 },
|
|
710
|
+
"char": { length: 1 },
|
|
711
|
+
"binary": { length: 1 },
|
|
712
|
+
"varbinary": { length: 255 },
|
|
713
|
+
"decimal": { precision: 10, scale: 0 },
|
|
714
|
+
"dec": { precision: 10, scale: 0 },
|
|
715
|
+
"numeric": { precision: 10, scale: 0 },
|
|
716
|
+
"fixed": { precision: 10, scale: 0 },
|
|
717
|
+
"float": { precision: 12 },
|
|
718
|
+
"double": { precision: 22 },
|
|
719
|
+
"time": { precision: 0 },
|
|
720
|
+
"datetime": { precision: 0 },
|
|
721
|
+
"timestamp": { precision: 0 },
|
|
722
|
+
"bit": { width: 1 },
|
|
723
|
+
"int": { width: 11 },
|
|
724
|
+
"integer": { width: 11 },
|
|
725
|
+
"tinyint": { width: 4 },
|
|
726
|
+
"smallint": { width: 6 },
|
|
727
|
+
"mediumint": { width: 9 },
|
|
728
|
+
"bigint": { width: 20 }
|
|
729
|
+
};
|
|
730
|
+
return MySqlTranslator;
|
|
731
|
+
}(sqlTranslator_1.SqlTranslator));
|
|
732
|
+
exports.MySqlTranslator = MySqlTranslator;
|