oak-db 3.3.8 → 3.3.9
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/lib/MySQL/translator.js +969 -969
- package/lib/sqlTranslator.js +997 -982
- package/package.json +2 -2
package/lib/MySQL/translator.js
CHANGED
|
@@ -1,969 +1,969 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MySqlTranslator = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const assert_1 = tslib_1.__importDefault(require("assert"));
|
|
6
|
-
const util_1 = require("util");
|
|
7
|
-
const lodash_1 = require("lodash");
|
|
8
|
-
const types_1 = require("oak-domain/lib/types");
|
|
9
|
-
const sqlTranslator_1 = require("../sqlTranslator");
|
|
10
|
-
const GeoTypes = [
|
|
11
|
-
{
|
|
12
|
-
type: 'point',
|
|
13
|
-
name: "Point"
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
type: 'path',
|
|
17
|
-
name: "LineString",
|
|
18
|
-
element: 'point',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
name: "MultiLineString",
|
|
22
|
-
element: "path",
|
|
23
|
-
multiple: true,
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
type: 'polygon',
|
|
27
|
-
name: "Polygon",
|
|
28
|
-
element: "path"
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
name: "MultiPoint",
|
|
32
|
-
element: "point",
|
|
33
|
-
multiple: true,
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
name: "MultiPolygon",
|
|
37
|
-
element: "polygon",
|
|
38
|
-
multiple: true,
|
|
39
|
-
}
|
|
40
|
-
];
|
|
41
|
-
function transformGeoData(data) {
|
|
42
|
-
if (data instanceof Array) {
|
|
43
|
-
const element = data[0];
|
|
44
|
-
if (element instanceof Array) {
|
|
45
|
-
return ` GeometryCollection(${data.map(ele => transformGeoData(ele)).join(',')})`;
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
const geoType = GeoTypes.find(ele => ele.type === element.type);
|
|
49
|
-
if (!geoType) {
|
|
50
|
-
throw new Error(`${element.type} is not supported in MySQL`);
|
|
51
|
-
}
|
|
52
|
-
const multiGeoType = GeoTypes.find(ele => ele.element === geoType.type && ele.multiple);
|
|
53
|
-
return ` ${multiGeoType.name}(${data.map(ele => transformGeoData(ele)).join(',')})`;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
const { type, coordinate } = data;
|
|
58
|
-
const geoType = GeoTypes.find(ele => ele.type === type);
|
|
59
|
-
if (!geoType) {
|
|
60
|
-
throw new Error(`${data.type} is not supported in MySQL`);
|
|
61
|
-
}
|
|
62
|
-
const { element, name } = geoType;
|
|
63
|
-
if (!element) {
|
|
64
|
-
// Point
|
|
65
|
-
return ` ${name}(${coordinate.join(',')})`;
|
|
66
|
-
}
|
|
67
|
-
// Polygon or Linestring
|
|
68
|
-
return ` ${name}(${coordinate.map((ele) => transformGeoData({
|
|
69
|
-
type: element,
|
|
70
|
-
coordinate: ele,
|
|
71
|
-
}))})`;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
class MySqlTranslator extends sqlTranslator_1.SqlTranslator {
|
|
75
|
-
getDefaultSelectFilter(alias, option) {
|
|
76
|
-
if (option?.includedDeleted) {
|
|
77
|
-
return '';
|
|
78
|
-
}
|
|
79
|
-
return ` (\`${alias}\`.\`$$deleteAt$$\` is null)`;
|
|
80
|
-
}
|
|
81
|
-
makeUpSchema() {
|
|
82
|
-
for (const entity in this.schema) {
|
|
83
|
-
const { attributes, indexes } = this.schema[entity];
|
|
84
|
-
const geoIndexes = [];
|
|
85
|
-
for (const attr in attributes) {
|
|
86
|
-
if (attributes[attr].type === 'geometry') {
|
|
87
|
-
const geoIndex = indexes?.find((idx) => idx.config?.type === 'spatial' && idx.attributes.find((attrDef) => attrDef.name === attr));
|
|
88
|
-
if (!geoIndex) {
|
|
89
|
-
geoIndexes.push({
|
|
90
|
-
name: `${entity}_geo_${attr}`,
|
|
91
|
-
attributes: [{
|
|
92
|
-
name: attr,
|
|
93
|
-
}],
|
|
94
|
-
config: {
|
|
95
|
-
type: 'spatial',
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (geoIndexes.length > 0) {
|
|
102
|
-
if (indexes) {
|
|
103
|
-
indexes.push(...geoIndexes);
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
(0, lodash_1.assign)(this.schema[entity], {
|
|
107
|
-
indexes: geoIndexes,
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
constructor(schema) {
|
|
114
|
-
super(schema);
|
|
115
|
-
// MySQL为geometry属性默认创建索引
|
|
116
|
-
this.makeUpSchema();
|
|
117
|
-
}
|
|
118
|
-
static supportedDataTypes = [
|
|
119
|
-
// numeric types
|
|
120
|
-
"bit",
|
|
121
|
-
"int",
|
|
122
|
-
"integer",
|
|
123
|
-
"tinyint",
|
|
124
|
-
"smallint",
|
|
125
|
-
"mediumint",
|
|
126
|
-
"bigint",
|
|
127
|
-
"float",
|
|
128
|
-
"double",
|
|
129
|
-
"double precision",
|
|
130
|
-
"real",
|
|
131
|
-
"decimal",
|
|
132
|
-
"dec",
|
|
133
|
-
"numeric",
|
|
134
|
-
"fixed",
|
|
135
|
-
"bool",
|
|
136
|
-
"boolean",
|
|
137
|
-
// date and time types
|
|
138
|
-
"date",
|
|
139
|
-
"datetime",
|
|
140
|
-
"timestamp",
|
|
141
|
-
"time",
|
|
142
|
-
"year",
|
|
143
|
-
// string types
|
|
144
|
-
"char",
|
|
145
|
-
"nchar",
|
|
146
|
-
"national char",
|
|
147
|
-
"varchar",
|
|
148
|
-
"nvarchar",
|
|
149
|
-
"national varchar",
|
|
150
|
-
"blob",
|
|
151
|
-
"text",
|
|
152
|
-
"tinyblob",
|
|
153
|
-
"tinytext",
|
|
154
|
-
"mediumblob",
|
|
155
|
-
"mediumtext",
|
|
156
|
-
"longblob",
|
|
157
|
-
"longtext",
|
|
158
|
-
"enum",
|
|
159
|
-
"set",
|
|
160
|
-
"binary",
|
|
161
|
-
"varbinary",
|
|
162
|
-
// json data type
|
|
163
|
-
"json",
|
|
164
|
-
// spatial data types
|
|
165
|
-
"geometry",
|
|
166
|
-
"point",
|
|
167
|
-
"linestring",
|
|
168
|
-
"polygon",
|
|
169
|
-
"multipoint",
|
|
170
|
-
"multilinestring",
|
|
171
|
-
"multipolygon",
|
|
172
|
-
"geometrycollection"
|
|
173
|
-
];
|
|
174
|
-
static spatialTypes = [
|
|
175
|
-
"geometry",
|
|
176
|
-
"point",
|
|
177
|
-
"linestring",
|
|
178
|
-
"polygon",
|
|
179
|
-
"multipoint",
|
|
180
|
-
"multilinestring",
|
|
181
|
-
"multipolygon",
|
|
182
|
-
"geometrycollection"
|
|
183
|
-
];
|
|
184
|
-
static withLengthDataTypes = [
|
|
185
|
-
"char",
|
|
186
|
-
"varchar",
|
|
187
|
-
"nvarchar",
|
|
188
|
-
"binary",
|
|
189
|
-
"varbinary"
|
|
190
|
-
];
|
|
191
|
-
static withPrecisionDataTypes = [
|
|
192
|
-
"decimal",
|
|
193
|
-
"dec",
|
|
194
|
-
"numeric",
|
|
195
|
-
"fixed",
|
|
196
|
-
"float",
|
|
197
|
-
"double",
|
|
198
|
-
"double precision",
|
|
199
|
-
"real",
|
|
200
|
-
"time",
|
|
201
|
-
"datetime",
|
|
202
|
-
"timestamp"
|
|
203
|
-
];
|
|
204
|
-
static withScaleDataTypes = [
|
|
205
|
-
"decimal",
|
|
206
|
-
"dec",
|
|
207
|
-
"numeric",
|
|
208
|
-
"fixed",
|
|
209
|
-
"float",
|
|
210
|
-
"double",
|
|
211
|
-
"double precision",
|
|
212
|
-
"real"
|
|
213
|
-
];
|
|
214
|
-
static unsignedAndZerofillTypes = [
|
|
215
|
-
"int",
|
|
216
|
-
"integer",
|
|
217
|
-
"smallint",
|
|
218
|
-
"tinyint",
|
|
219
|
-
"mediumint",
|
|
220
|
-
"bigint",
|
|
221
|
-
"decimal",
|
|
222
|
-
"dec",
|
|
223
|
-
"numeric",
|
|
224
|
-
"fixed",
|
|
225
|
-
"float",
|
|
226
|
-
"double",
|
|
227
|
-
"double precision",
|
|
228
|
-
"real"
|
|
229
|
-
];
|
|
230
|
-
static withWidthDataTypes = [
|
|
231
|
-
'int',
|
|
232
|
-
];
|
|
233
|
-
static dataTypeDefaults = {
|
|
234
|
-
"varchar": { length: 255 },
|
|
235
|
-
"nvarchar": { length: 255 },
|
|
236
|
-
"national varchar": { length: 255 },
|
|
237
|
-
"char": { length: 1 },
|
|
238
|
-
"binary": { length: 1 },
|
|
239
|
-
"varbinary": { length: 255 },
|
|
240
|
-
"decimal": { precision: 10, scale: 0 },
|
|
241
|
-
"dec": { precision: 10, scale: 0 },
|
|
242
|
-
"numeric": { precision: 10, scale: 0 },
|
|
243
|
-
"fixed": { precision: 10, scale: 0 },
|
|
244
|
-
"float": { precision: 12 },
|
|
245
|
-
"double": { precision: 22 },
|
|
246
|
-
"time": { precision: 0 },
|
|
247
|
-
"datetime": { precision: 0 },
|
|
248
|
-
"timestamp": { precision: 0 },
|
|
249
|
-
"bit": { width: 1 },
|
|
250
|
-
"int": { width: 11 },
|
|
251
|
-
"integer": { width: 11 },
|
|
252
|
-
"tinyint": { width: 4 },
|
|
253
|
-
"smallint": { width: 6 },
|
|
254
|
-
"mediumint": { width: 9 },
|
|
255
|
-
"bigint": { width: 20 }
|
|
256
|
-
};
|
|
257
|
-
maxAliasLength = 63;
|
|
258
|
-
populateDataTypeDef(type, params, enumeration) {
|
|
259
|
-
if (['date', 'datetime', 'time', 'sequence'].includes(type)) {
|
|
260
|
-
return 'bigint ';
|
|
261
|
-
}
|
|
262
|
-
if (['object', 'array'].includes(type)) {
|
|
263
|
-
return 'json ';
|
|
264
|
-
}
|
|
265
|
-
if (['image', 'function'].includes(type)) {
|
|
266
|
-
return 'text ';
|
|
267
|
-
}
|
|
268
|
-
if (type === 'ref') {
|
|
269
|
-
return 'char(36)';
|
|
270
|
-
}
|
|
271
|
-
if (type === 'money') {
|
|
272
|
-
return 'bigint';
|
|
273
|
-
}
|
|
274
|
-
if (type === 'enum') {
|
|
275
|
-
(0, assert_1.default)(enumeration);
|
|
276
|
-
return `enum(${enumeration.map(ele => `'${ele}'`).join(',')})`;
|
|
277
|
-
}
|
|
278
|
-
if (MySqlTranslator.withLengthDataTypes.includes(type)) {
|
|
279
|
-
if (params) {
|
|
280
|
-
const { length } = params;
|
|
281
|
-
return `${type}(${length}) `;
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
const { length } = MySqlTranslator.dataTypeDefaults[type];
|
|
285
|
-
return `${type}(${length}) `;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
if (MySqlTranslator.withPrecisionDataTypes.includes(type)) {
|
|
289
|
-
if (params) {
|
|
290
|
-
const { precision, scale } = params;
|
|
291
|
-
if (typeof scale === 'number') {
|
|
292
|
-
return `${type}(${precision}, ${scale}) `;
|
|
293
|
-
}
|
|
294
|
-
return `${type}(${precision})`;
|
|
295
|
-
}
|
|
296
|
-
else {
|
|
297
|
-
const { precision, scale } = MySqlTranslator.dataTypeDefaults[type];
|
|
298
|
-
if (typeof scale === 'number') {
|
|
299
|
-
return `${type}(${precision}, ${scale}) `;
|
|
300
|
-
}
|
|
301
|
-
return `${type}(${precision})`;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
if (MySqlTranslator.withWidthDataTypes.includes(type)) {
|
|
305
|
-
(0, assert_1.default)(type === 'int');
|
|
306
|
-
const { width } = params;
|
|
307
|
-
switch (width) {
|
|
308
|
-
case 1: {
|
|
309
|
-
return 'tinyint';
|
|
310
|
-
}
|
|
311
|
-
case 2: {
|
|
312
|
-
return 'smallint';
|
|
313
|
-
}
|
|
314
|
-
case 3: {
|
|
315
|
-
return 'mediumint';
|
|
316
|
-
}
|
|
317
|
-
case 4: {
|
|
318
|
-
return 'int';
|
|
319
|
-
}
|
|
320
|
-
default: {
|
|
321
|
-
return 'bigint';
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
return `${type} `;
|
|
326
|
-
}
|
|
327
|
-
translateAttrProjection(dataType, alias, attr) {
|
|
328
|
-
switch (dataType) {
|
|
329
|
-
case 'geometry': {
|
|
330
|
-
return ` st_astext(\`${alias}\`.\`${attr}\`)`;
|
|
331
|
-
}
|
|
332
|
-
default: {
|
|
333
|
-
return ` \`${alias}\`.\`${attr}\``;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
translateObjectPredicate(predicate, alias, attr) {
|
|
338
|
-
const translateInner = (o, p) => {
|
|
339
|
-
let stmt2 = '';
|
|
340
|
-
if (o instanceof Array) {
|
|
341
|
-
o.forEach((ele, idx) => {
|
|
342
|
-
if (ele !== undefined && ele !== null) {
|
|
343
|
-
const part = translateInner(ele, `${p}[${idx}]`);
|
|
344
|
-
if (stmt2) {
|
|
345
|
-
stmt2 += ' and ';
|
|
346
|
-
}
|
|
347
|
-
stmt2 += `${part}`;
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
else if (typeof o === 'object') {
|
|
352
|
-
for (const attr2 in o) {
|
|
353
|
-
if (attr2 === '$and') {
|
|
354
|
-
o[attr2].forEach((ele) => {
|
|
355
|
-
const part = translateInner(ele, p);
|
|
356
|
-
if (stmt2) {
|
|
357
|
-
stmt2 += ' and ';
|
|
358
|
-
}
|
|
359
|
-
stmt2 += `${part}`;
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
else if (attr2 === '$or') {
|
|
363
|
-
let stmtOr = '';
|
|
364
|
-
o[attr2].forEach((ele) => {
|
|
365
|
-
const part = translateInner(ele, p);
|
|
366
|
-
if (stmtOr) {
|
|
367
|
-
stmtOr += ' or ';
|
|
368
|
-
}
|
|
369
|
-
stmtOr += `${part}`;
|
|
370
|
-
});
|
|
371
|
-
if (stmt2) {
|
|
372
|
-
stmt2 += ' and ';
|
|
373
|
-
}
|
|
374
|
-
stmt2 += `(${stmtOr})`;
|
|
375
|
-
}
|
|
376
|
-
else if (attr2 === '$contains') {
|
|
377
|
-
// json_contains,多值的包含关系
|
|
378
|
-
const value = JSON.stringify(o[attr2]);
|
|
379
|
-
if (stmt2) {
|
|
380
|
-
stmt2 += ' and ';
|
|
381
|
-
}
|
|
382
|
-
if (p) {
|
|
383
|
-
stmt2 += `(JSON_CONTAINS(${alias}.${attr}->>"$${p}", CAST('${value}' AS JSON)))`;
|
|
384
|
-
}
|
|
385
|
-
else {
|
|
386
|
-
stmt2 += `(JSON_CONTAINS(${alias}.${attr}, CAST('${value}' AS JSON)))`;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
else if (attr2 === '$overlaps') {
|
|
390
|
-
// json_overlaps,多值的交叉关系
|
|
391
|
-
const value = JSON.stringify(o[attr2]);
|
|
392
|
-
if (stmt2) {
|
|
393
|
-
stmt2 += ' and ';
|
|
394
|
-
}
|
|
395
|
-
if (p) {
|
|
396
|
-
stmt2 += `(JSON_OVERLAPS(${alias}.${attr}->>"$${p}", CAST('${value}' AS JSON)))`;
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
stmt2 += `(JSON_OVERLAPS(${alias}.${attr}, CAST('${value}' AS JSON)))`;
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
else if (attr2 === '$length') {
|
|
403
|
-
// json length
|
|
404
|
-
const length = o[attr2];
|
|
405
|
-
if (stmt2) {
|
|
406
|
-
stmt2 += ' and ';
|
|
407
|
-
}
|
|
408
|
-
if (typeof length === 'number') {
|
|
409
|
-
if (p) {
|
|
410
|
-
stmt2 += `(JSON_LENGTH(${alias}.${attr}->>"$${p}") = ${length})`;
|
|
411
|
-
}
|
|
412
|
-
else {
|
|
413
|
-
stmt2 += `(JSON_LENGTH(${alias}.${attr}) = ${length})`;
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
else {
|
|
417
|
-
(0, assert_1.default)(typeof length === 'object');
|
|
418
|
-
const op = Object.keys(length)[0];
|
|
419
|
-
(0, assert_1.default)(op.startsWith('$'));
|
|
420
|
-
if (p) {
|
|
421
|
-
stmt2 += `(JSON_LENGTH(${alias}.${attr}->>"$${p}") ${this.translatePredicate(op, length[op])})`;
|
|
422
|
-
}
|
|
423
|
-
else {
|
|
424
|
-
stmt2 += `(JSON_LENGTH(${alias}.${attr}) ${this.translatePredicate(op, length[op])})`;
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
else if (attr2.startsWith('$')) {
|
|
429
|
-
if (stmt2) {
|
|
430
|
-
stmt2 += ' and ';
|
|
431
|
-
}
|
|
432
|
-
if (p) {
|
|
433
|
-
stmt2 += `(${alias}.${attr}->>"$${p}" ${this.translatePredicate(attr2, o[attr2])})`;
|
|
434
|
-
}
|
|
435
|
-
else {
|
|
436
|
-
stmt2 += `(${alias}.${attr} ${this.translatePredicate(attr2, o[attr2])})`;
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
else {
|
|
440
|
-
// 继续子对象解构
|
|
441
|
-
const attr3 = attr2.startsWith('.') ? attr2.slice(1) : attr2;
|
|
442
|
-
const part = translateInner(o[attr2], `${p}.${attr3}`);
|
|
443
|
-
if (stmt2) {
|
|
444
|
-
stmt2 += ' and ';
|
|
445
|
-
}
|
|
446
|
-
stmt2 += `${part}`;
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
else {
|
|
451
|
-
// 直接的属性处理
|
|
452
|
-
if (stmt2) {
|
|
453
|
-
stmt2 += ' and ';
|
|
454
|
-
}
|
|
455
|
-
if (typeof o === 'string') {
|
|
456
|
-
if (p) {
|
|
457
|
-
stmt2 += `(${alias}.${attr}->>"$${p}" = '${o}')`;
|
|
458
|
-
}
|
|
459
|
-
else {
|
|
460
|
-
// 对根对象的字符串比较
|
|
461
|
-
stmt2 += `(${alias}.${attr} = '${o}')`;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
else {
|
|
465
|
-
(0, assert_1.default)(p);
|
|
466
|
-
stmt2 += `(${alias}.${attr}->>"$${p}" = ${o})`;
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return stmt2;
|
|
470
|
-
};
|
|
471
|
-
return translateInner(predicate, '');
|
|
472
|
-
}
|
|
473
|
-
translateObjectProjection(projection, alias, attr, prefix) {
|
|
474
|
-
let stmt = '';
|
|
475
|
-
const translateInner = (o, p) => {
|
|
476
|
-
if (o instanceof Array) {
|
|
477
|
-
o.forEach((item, idx) => {
|
|
478
|
-
const p2 = `${p}[${idx}]`;
|
|
479
|
-
if (typeof item === 'number') {
|
|
480
|
-
if (stmt) {
|
|
481
|
-
stmt += ', ';
|
|
482
|
-
}
|
|
483
|
-
stmt += `${alias}.${attr}->>"$${p2}"`;
|
|
484
|
-
stmt += prefix ? ` as \`${prefix}.${attr}${p2}\`` : ` as \`${attr}${p2}\``;
|
|
485
|
-
}
|
|
486
|
-
else if (typeof item === 'object') {
|
|
487
|
-
translateInner(item, p2);
|
|
488
|
-
}
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
else {
|
|
492
|
-
for (const key in o) {
|
|
493
|
-
const p2 = `${p}.${key}`;
|
|
494
|
-
if (typeof o[key] === 'number') {
|
|
495
|
-
if (stmt) {
|
|
496
|
-
stmt += ', ';
|
|
497
|
-
}
|
|
498
|
-
stmt += `${alias}.${attr}->>"$${p2}"`;
|
|
499
|
-
stmt += prefix ? ` as \`${prefix}.${attr}${p2}\`` : ` as \`${attr}${p2}\``;
|
|
500
|
-
}
|
|
501
|
-
else {
|
|
502
|
-
translateInner(o[key], p2);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
};
|
|
507
|
-
translateInner(projection, '');
|
|
508
|
-
return stmt;
|
|
509
|
-
}
|
|
510
|
-
translateAttrValue(dataType, value) {
|
|
511
|
-
if (value === null || value === undefined) {
|
|
512
|
-
return 'null';
|
|
513
|
-
}
|
|
514
|
-
switch (dataType) {
|
|
515
|
-
case 'geometry': {
|
|
516
|
-
return transformGeoData(value);
|
|
517
|
-
}
|
|
518
|
-
case 'datetime':
|
|
519
|
-
case 'time':
|
|
520
|
-
case 'date': {
|
|
521
|
-
if (value instanceof Date) {
|
|
522
|
-
return `${value.valueOf()}`;
|
|
523
|
-
}
|
|
524
|
-
else if (typeof value === 'number') {
|
|
525
|
-
return `${value}`;
|
|
526
|
-
}
|
|
527
|
-
return `'${(new Date(value)).valueOf()}'`;
|
|
528
|
-
}
|
|
529
|
-
case 'object':
|
|
530
|
-
case 'array': {
|
|
531
|
-
return this.escapeStringValue(JSON.stringify(value));
|
|
532
|
-
}
|
|
533
|
-
/* case 'function': {
|
|
534
|
-
return `'${Buffer.from(value.toString()).toString('base64')}'`;
|
|
535
|
-
} */
|
|
536
|
-
default: {
|
|
537
|
-
if (typeof value === 'string') {
|
|
538
|
-
return this.escapeStringValue(value);
|
|
539
|
-
}
|
|
540
|
-
return value;
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
translateFullTextSearch(value, entity, alias) {
|
|
545
|
-
const { $search } = value;
|
|
546
|
-
const { indexes } = this.schema[entity];
|
|
547
|
-
const ftIndex = indexes && indexes.find((ele) => {
|
|
548
|
-
const { config } = ele;
|
|
549
|
-
return config && config.type === 'fulltext';
|
|
550
|
-
});
|
|
551
|
-
(0, assert_1.default)(ftIndex);
|
|
552
|
-
const { attributes } = ftIndex;
|
|
553
|
-
const columns2 = attributes.map(({ name }) => `${alias}.${name}`);
|
|
554
|
-
return ` match(${columns2.join(',')}) against ('${$search}' in natural language mode)`;
|
|
555
|
-
}
|
|
556
|
-
translateCreateEntity(entity, options) {
|
|
557
|
-
const ifExists = options?.ifExists;
|
|
558
|
-
const { schema } = this;
|
|
559
|
-
const entityDef = schema[entity];
|
|
560
|
-
const { storageName, attributes, indexes, view, static: _static } = entityDef;
|
|
561
|
-
let hasSequence = false;
|
|
562
|
-
// todo view暂还不支持
|
|
563
|
-
const entityType = view ? 'view' : 'table';
|
|
564
|
-
let sql = `create ${entityType} `;
|
|
565
|
-
if (ifExists === 'omit' || (_static && ifExists === 'dropIfNotStatic')) {
|
|
566
|
-
sql += ' if not exists';
|
|
567
|
-
}
|
|
568
|
-
if (storageName) {
|
|
569
|
-
sql += `\`${storageName}\` `;
|
|
570
|
-
}
|
|
571
|
-
else {
|
|
572
|
-
sql += `\`${entity}\` `;
|
|
573
|
-
}
|
|
574
|
-
if (view) {
|
|
575
|
-
throw new Error(' view unsupported yet');
|
|
576
|
-
}
|
|
577
|
-
else {
|
|
578
|
-
sql += '(';
|
|
579
|
-
// 翻译所有的属性
|
|
580
|
-
Object.keys(attributes).forEach((attr, idx) => {
|
|
581
|
-
const attrDef = attributes[attr];
|
|
582
|
-
const { type, params, default: defaultValue, unique, notNull, sequenceStart, enumeration, } = attrDef;
|
|
583
|
-
sql += `\`${attr}\` `;
|
|
584
|
-
sql += this.populateDataTypeDef(type, params, enumeration);
|
|
585
|
-
if (notNull || type === 'geometry') {
|
|
586
|
-
sql += ' not null ';
|
|
587
|
-
}
|
|
588
|
-
if (unique) {
|
|
589
|
-
sql += ' unique ';
|
|
590
|
-
}
|
|
591
|
-
if (sequenceStart) {
|
|
592
|
-
if (hasSequence) {
|
|
593
|
-
throw new Error(`「${entity}」只能有一个sequence列`);
|
|
594
|
-
}
|
|
595
|
-
hasSequence = sequenceStart;
|
|
596
|
-
sql += ' auto_increment unique ';
|
|
597
|
-
}
|
|
598
|
-
if (defaultValue !== undefined) {
|
|
599
|
-
(0, assert_1.default)(type !== 'ref');
|
|
600
|
-
sql += ` default ${this.translateAttrValue(type, defaultValue)}`;
|
|
601
|
-
}
|
|
602
|
-
if (attr === 'id') {
|
|
603
|
-
sql += ' primary key';
|
|
604
|
-
}
|
|
605
|
-
if (idx < Object.keys(attributes).length - 1) {
|
|
606
|
-
sql += ',\n';
|
|
607
|
-
}
|
|
608
|
-
});
|
|
609
|
-
// 翻译索引信息
|
|
610
|
-
if (indexes) {
|
|
611
|
-
sql += ',\n';
|
|
612
|
-
indexes.forEach(({ name, attributes, config }, idx) => {
|
|
613
|
-
const { unique, type, parser } = config || {};
|
|
614
|
-
// 因为有deleteAt的存在,这里的unique没意义,只能框架自己去建立checker来处理
|
|
615
|
-
/* if (unique) {
|
|
616
|
-
sql += ' unique ';
|
|
617
|
-
}
|
|
618
|
-
else */ if (type === 'fulltext') {
|
|
619
|
-
sql += ' fulltext ';
|
|
620
|
-
}
|
|
621
|
-
else if (type === 'spatial') {
|
|
622
|
-
sql += ' spatial ';
|
|
623
|
-
}
|
|
624
|
-
sql += `index ${name} `;
|
|
625
|
-
if (type === 'hash') {
|
|
626
|
-
sql += ` using hash `;
|
|
627
|
-
}
|
|
628
|
-
sql += '(';
|
|
629
|
-
let includeDeleteAt = false;
|
|
630
|
-
attributes.forEach(({ name, size, direction }, idx2) => {
|
|
631
|
-
sql += `\`${name}\``;
|
|
632
|
-
if (size) {
|
|
633
|
-
sql += ` (${size})`;
|
|
634
|
-
}
|
|
635
|
-
if (direction) {
|
|
636
|
-
sql += ` ${direction}`;
|
|
637
|
-
}
|
|
638
|
-
if (idx2 < attributes.length - 1) {
|
|
639
|
-
sql += ',';
|
|
640
|
-
}
|
|
641
|
-
if (name === '$$deleteAt$$') {
|
|
642
|
-
includeDeleteAt = true;
|
|
643
|
-
}
|
|
644
|
-
});
|
|
645
|
-
if (!includeDeleteAt && !type) {
|
|
646
|
-
sql += ', `$$deleteAt$$`'; // 在mysql80+之后,需要给属性加上``包裹,否则会报错
|
|
647
|
-
}
|
|
648
|
-
sql += ')';
|
|
649
|
-
if (parser) {
|
|
650
|
-
sql += ` with parser ${parser}`;
|
|
651
|
-
}
|
|
652
|
-
if (idx < indexes.length - 1) {
|
|
653
|
-
sql += ',\n';
|
|
654
|
-
}
|
|
655
|
-
});
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
sql += ')';
|
|
659
|
-
if (typeof hasSequence === 'number') {
|
|
660
|
-
sql += `auto_increment = ${hasSequence}`;
|
|
661
|
-
}
|
|
662
|
-
if (ifExists === 'drop' || (!_static && ifExists === 'dropIfNotStatic')) {
|
|
663
|
-
return [`drop ${entityType} if exists \`${storageName || entity}\`;`, sql];
|
|
664
|
-
}
|
|
665
|
-
return [sql];
|
|
666
|
-
}
|
|
667
|
-
translateFnName(fnName, argumentNumber) {
|
|
668
|
-
switch (fnName) {
|
|
669
|
-
case '$add': {
|
|
670
|
-
let result = '%s';
|
|
671
|
-
while (--argumentNumber > 0) {
|
|
672
|
-
result += ' + %s';
|
|
673
|
-
}
|
|
674
|
-
return result;
|
|
675
|
-
}
|
|
676
|
-
case '$subtract': {
|
|
677
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
678
|
-
return '%s - %s';
|
|
679
|
-
}
|
|
680
|
-
case '$multiply': {
|
|
681
|
-
let result = '%s';
|
|
682
|
-
while (--argumentNumber > 0) {
|
|
683
|
-
result += ' * %s';
|
|
684
|
-
}
|
|
685
|
-
return result;
|
|
686
|
-
}
|
|
687
|
-
case '$divide': {
|
|
688
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
689
|
-
return '%s / %s';
|
|
690
|
-
}
|
|
691
|
-
case '$abs': {
|
|
692
|
-
return 'ABS(%s)';
|
|
693
|
-
}
|
|
694
|
-
case '$round': {
|
|
695
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
696
|
-
return 'ROUND(%s, %s)';
|
|
697
|
-
}
|
|
698
|
-
case '$ceil': {
|
|
699
|
-
return 'CEIL(%s)';
|
|
700
|
-
}
|
|
701
|
-
case '$floor': {
|
|
702
|
-
return 'FLOOR(%s)';
|
|
703
|
-
}
|
|
704
|
-
case '$mod': {
|
|
705
|
-
return 'MOD(%s, %s)';
|
|
706
|
-
}
|
|
707
|
-
case '$pow': {
|
|
708
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
709
|
-
return 'POW(%s, %s)';
|
|
710
|
-
}
|
|
711
|
-
case '$gt': {
|
|
712
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
713
|
-
return '%s > %s';
|
|
714
|
-
}
|
|
715
|
-
case '$gte': {
|
|
716
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
717
|
-
return '%s >= %s';
|
|
718
|
-
}
|
|
719
|
-
case '$lt': {
|
|
720
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
721
|
-
return '%s < %s';
|
|
722
|
-
}
|
|
723
|
-
case '$lte': {
|
|
724
|
-
return '%s <= %s';
|
|
725
|
-
}
|
|
726
|
-
case '$eq': {
|
|
727
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
728
|
-
return '%s = %s';
|
|
729
|
-
}
|
|
730
|
-
case '$ne': {
|
|
731
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
732
|
-
return '%s <> %s';
|
|
733
|
-
}
|
|
734
|
-
case '$startsWith': {
|
|
735
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
736
|
-
return '%s like CONCAT(%s, \'%\')';
|
|
737
|
-
}
|
|
738
|
-
case '$endsWith': {
|
|
739
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
740
|
-
return '%s like CONCAT(\'%\', %s)';
|
|
741
|
-
}
|
|
742
|
-
case '$includes': {
|
|
743
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
744
|
-
return '%s like CONCAT(\'%\', %s, \'%\')';
|
|
745
|
-
}
|
|
746
|
-
case '$true': {
|
|
747
|
-
return '%s = true';
|
|
748
|
-
}
|
|
749
|
-
case '$false': {
|
|
750
|
-
return '%s = false';
|
|
751
|
-
}
|
|
752
|
-
case '$and': {
|
|
753
|
-
let result = '';
|
|
754
|
-
for (let iter = 0; iter < argumentNumber; iter++) {
|
|
755
|
-
result += '%s';
|
|
756
|
-
if (iter < argumentNumber - 1) {
|
|
757
|
-
result += ' and ';
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
return result;
|
|
761
|
-
}
|
|
762
|
-
case '$or': {
|
|
763
|
-
let result = '';
|
|
764
|
-
for (let iter = 0; iter < argumentNumber; iter++) {
|
|
765
|
-
result += '%s';
|
|
766
|
-
if (iter < argumentNumber - 1) {
|
|
767
|
-
result += ' or ';
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
return result;
|
|
771
|
-
}
|
|
772
|
-
case '$not': {
|
|
773
|
-
return 'not %s';
|
|
774
|
-
}
|
|
775
|
-
case '$year': {
|
|
776
|
-
return 'YEAR(%s)';
|
|
777
|
-
}
|
|
778
|
-
case '$month': {
|
|
779
|
-
return 'MONTH(%s)';
|
|
780
|
-
}
|
|
781
|
-
case '$weekday': {
|
|
782
|
-
return 'WEEKDAY(%s)';
|
|
783
|
-
}
|
|
784
|
-
case '$weekOfYear': {
|
|
785
|
-
return 'WEEKOFYEAR(%s)';
|
|
786
|
-
}
|
|
787
|
-
case '$day': {
|
|
788
|
-
return 'DAY(%s)';
|
|
789
|
-
}
|
|
790
|
-
case '$dayOfMonth': {
|
|
791
|
-
return 'DAYOFMONTH(%s)';
|
|
792
|
-
}
|
|
793
|
-
case '$dayOfWeek': {
|
|
794
|
-
return 'DAYOFWEEK(%s)';
|
|
795
|
-
}
|
|
796
|
-
case '$dayOfYear': {
|
|
797
|
-
return 'DAYOFYEAR(%s)';
|
|
798
|
-
}
|
|
799
|
-
case '$dateDiff': {
|
|
800
|
-
(0, assert_1.default)(argumentNumber === 3);
|
|
801
|
-
return 'DATEDIFF(%s, %s, %s)';
|
|
802
|
-
}
|
|
803
|
-
case '$contains': {
|
|
804
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
805
|
-
return 'ST_CONTAINS(%s, %s)';
|
|
806
|
-
}
|
|
807
|
-
case '$distance': {
|
|
808
|
-
(0, assert_1.default)(argumentNumber === 2);
|
|
809
|
-
return 'ST_DISTANCE(%s, %s)';
|
|
810
|
-
}
|
|
811
|
-
case '$concat': {
|
|
812
|
-
let result = ' concat(%s';
|
|
813
|
-
while (--argumentNumber > 0) {
|
|
814
|
-
result += ', %s';
|
|
815
|
-
}
|
|
816
|
-
result += ')';
|
|
817
|
-
return result;
|
|
818
|
-
}
|
|
819
|
-
default: {
|
|
820
|
-
throw new Error(`unrecoganized function ${fnName}`);
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
translateAttrInExpression(entity, attr, exprText) {
|
|
825
|
-
const { attributes } = this.schema[entity];
|
|
826
|
-
const { type } = attributes[attr];
|
|
827
|
-
if (['date', 'time', 'datetime'].includes(type)) {
|
|
828
|
-
// 从unix时间戵转成date类型参加expr的运算
|
|
829
|
-
return `from_unixtime(${exprText} / 1000)`;
|
|
830
|
-
}
|
|
831
|
-
return exprText;
|
|
832
|
-
}
|
|
833
|
-
translateExpression(entity, alias, expression, refDict) {
|
|
834
|
-
const translateConstant = (constant) => {
|
|
835
|
-
if (constant instanceof Date) {
|
|
836
|
-
return ` from_unixtime(${constant.valueOf()}/1000)`;
|
|
837
|
-
}
|
|
838
|
-
else if (typeof constant === 'string') {
|
|
839
|
-
return ` '${constant}'`;
|
|
840
|
-
}
|
|
841
|
-
else {
|
|
842
|
-
(0, assert_1.default)(typeof constant === 'number');
|
|
843
|
-
return ` ${constant}`;
|
|
844
|
-
}
|
|
845
|
-
};
|
|
846
|
-
const translateInner = (expr) => {
|
|
847
|
-
const k = Object.keys(expr);
|
|
848
|
-
let result;
|
|
849
|
-
if (k.includes('#attr')) {
|
|
850
|
-
const attrText = `\`${alias}\`.\`${(expr)['#attr']}\``;
|
|
851
|
-
result = this.translateAttrInExpression(entity, (expr)['#attr'], attrText);
|
|
852
|
-
}
|
|
853
|
-
else if (k.includes('#refId')) {
|
|
854
|
-
const refId = (expr)['#refId'];
|
|
855
|
-
const refAttr = (expr)['#refAttr'];
|
|
856
|
-
(0, assert_1.default)(refDict[refId]);
|
|
857
|
-
const attrText = `\`${refDict[refId][0]}\`.\`${refAttr}\``;
|
|
858
|
-
result = this.translateAttrInExpression(entity, (expr)['#refAttr'], attrText);
|
|
859
|
-
}
|
|
860
|
-
else {
|
|
861
|
-
(0, assert_1.default)(k.length === 1);
|
|
862
|
-
if ((expr)[k[0]] instanceof Array) {
|
|
863
|
-
const fnName = this.translateFnName(k[0], (expr)[k[0]].length);
|
|
864
|
-
const args = [fnName];
|
|
865
|
-
args.push(...(expr)[k[0]].map((ele) => {
|
|
866
|
-
if (['string', 'number'].includes(typeof ele) || ele instanceof Date) {
|
|
867
|
-
return translateConstant(ele);
|
|
868
|
-
}
|
|
869
|
-
else {
|
|
870
|
-
return translateInner(ele);
|
|
871
|
-
}
|
|
872
|
-
}));
|
|
873
|
-
result = util_1.format.apply(null, args);
|
|
874
|
-
}
|
|
875
|
-
else {
|
|
876
|
-
const fnName = this.translateFnName(k[0], 1);
|
|
877
|
-
const args = [fnName];
|
|
878
|
-
const arg = (expr)[k[0]];
|
|
879
|
-
if (['string', 'number'].includes(typeof arg) || arg instanceof Date) {
|
|
880
|
-
args.push(translateConstant(arg));
|
|
881
|
-
}
|
|
882
|
-
else {
|
|
883
|
-
args.push(translateInner(arg));
|
|
884
|
-
}
|
|
885
|
-
result = util_1.format.apply(null, args);
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
return result;
|
|
889
|
-
};
|
|
890
|
-
return translateInner(expression);
|
|
891
|
-
}
|
|
892
|
-
populateSelectStmt(projectionText, fromText, aliasDict, filterText, sorterText, groupByText, indexFrom, count, option) {
|
|
893
|
-
// todo hint of use index
|
|
894
|
-
let sql = `select ${projectionText} from ${fromText}`;
|
|
895
|
-
if (filterText) {
|
|
896
|
-
sql += ` where ${filterText}`;
|
|
897
|
-
}
|
|
898
|
-
if (sorterText) {
|
|
899
|
-
sql += ` order by ${sorterText}`;
|
|
900
|
-
}
|
|
901
|
-
if (groupByText) {
|
|
902
|
-
sql += ` group by ${groupByText}`;
|
|
903
|
-
}
|
|
904
|
-
if (typeof indexFrom === 'number') {
|
|
905
|
-
(0, assert_1.default)(typeof count === 'number');
|
|
906
|
-
sql += ` limit ${indexFrom}, ${count}`;
|
|
907
|
-
}
|
|
908
|
-
if (option?.forUpdate) {
|
|
909
|
-
sql += ' for update';
|
|
910
|
-
if (typeof option?.forUpdate === 'string') {
|
|
911
|
-
sql += ` ${option.forUpdate}`;
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
return sql;
|
|
915
|
-
}
|
|
916
|
-
populateUpdateStmt(updateText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
917
|
-
// todo using index
|
|
918
|
-
(0, assert_1.default)(updateText);
|
|
919
|
-
let sql = `update ${fromText} set ${updateText}`;
|
|
920
|
-
if (filterText) {
|
|
921
|
-
sql += ` where ${filterText}`;
|
|
922
|
-
}
|
|
923
|
-
if (sorterText) {
|
|
924
|
-
sql += ` order by ${sorterText}`;
|
|
925
|
-
}
|
|
926
|
-
if (typeof indexFrom === 'number') {
|
|
927
|
-
(0, assert_1.default)(typeof count === 'number');
|
|
928
|
-
sql += ` limit ${indexFrom}, ${count}`;
|
|
929
|
-
}
|
|
930
|
-
return sql;
|
|
931
|
-
}
|
|
932
|
-
populateRemoveStmt(updateText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
933
|
-
// todo using index
|
|
934
|
-
const alias = aliasDict['./'];
|
|
935
|
-
if (option?.deletePhysically) {
|
|
936
|
-
// assert(!updateText, 'physically delete does not support setting trigger data');
|
|
937
|
-
let sql = `delete ${alias} from ${fromText} `;
|
|
938
|
-
if (filterText) {
|
|
939
|
-
sql += ` where ${filterText}`;
|
|
940
|
-
}
|
|
941
|
-
if (sorterText) {
|
|
942
|
-
sql += ` order by ${sorterText}`;
|
|
943
|
-
}
|
|
944
|
-
if (typeof indexFrom === 'number') {
|
|
945
|
-
(0, assert_1.default)(typeof count === 'number');
|
|
946
|
-
sql += ` limit ${indexFrom}, ${count}`;
|
|
947
|
-
}
|
|
948
|
-
return sql;
|
|
949
|
-
}
|
|
950
|
-
// 新的remove应该包含$$deleteAt$$的值了
|
|
951
|
-
/* const now = Date.now();
|
|
952
|
-
|
|
953
|
-
const updateText2 = updateText ? `${updateText}, \`${alias}\`.\`$$deleteAt$$\` = '${now}'` : `\`${alias}\`.\`$$deleteAt$$\` = '${now}'`; */
|
|
954
|
-
(0, assert_1.default)(updateText.includes(types_1.DeleteAtAttribute));
|
|
955
|
-
let sql = `update ${fromText} set ${updateText}`;
|
|
956
|
-
if (filterText) {
|
|
957
|
-
sql += ` where ${filterText}`;
|
|
958
|
-
}
|
|
959
|
-
if (sorterText) {
|
|
960
|
-
sql += ` order by ${sorterText}`;
|
|
961
|
-
}
|
|
962
|
-
if (typeof indexFrom === 'number') {
|
|
963
|
-
(0, assert_1.default)(typeof count === 'number');
|
|
964
|
-
sql += ` limit ${indexFrom}, ${count}`;
|
|
965
|
-
}
|
|
966
|
-
return sql;
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
exports.MySqlTranslator = MySqlTranslator;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MySqlTranslator = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const assert_1 = tslib_1.__importDefault(require("assert"));
|
|
6
|
+
const util_1 = require("util");
|
|
7
|
+
const lodash_1 = require("lodash");
|
|
8
|
+
const types_1 = require("oak-domain/lib/types");
|
|
9
|
+
const sqlTranslator_1 = require("../sqlTranslator");
|
|
10
|
+
const GeoTypes = [
|
|
11
|
+
{
|
|
12
|
+
type: 'point',
|
|
13
|
+
name: "Point"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
type: 'path',
|
|
17
|
+
name: "LineString",
|
|
18
|
+
element: 'point',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: "MultiLineString",
|
|
22
|
+
element: "path",
|
|
23
|
+
multiple: true,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
type: 'polygon',
|
|
27
|
+
name: "Polygon",
|
|
28
|
+
element: "path"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: "MultiPoint",
|
|
32
|
+
element: "point",
|
|
33
|
+
multiple: true,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "MultiPolygon",
|
|
37
|
+
element: "polygon",
|
|
38
|
+
multiple: true,
|
|
39
|
+
}
|
|
40
|
+
];
|
|
41
|
+
function transformGeoData(data) {
|
|
42
|
+
if (data instanceof Array) {
|
|
43
|
+
const element = data[0];
|
|
44
|
+
if (element instanceof Array) {
|
|
45
|
+
return ` GeometryCollection(${data.map(ele => transformGeoData(ele)).join(',')})`;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const geoType = GeoTypes.find(ele => ele.type === element.type);
|
|
49
|
+
if (!geoType) {
|
|
50
|
+
throw new Error(`${element.type} is not supported in MySQL`);
|
|
51
|
+
}
|
|
52
|
+
const multiGeoType = GeoTypes.find(ele => ele.element === geoType.type && ele.multiple);
|
|
53
|
+
return ` ${multiGeoType.name}(${data.map(ele => transformGeoData(ele)).join(',')})`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const { type, coordinate } = data;
|
|
58
|
+
const geoType = GeoTypes.find(ele => ele.type === type);
|
|
59
|
+
if (!geoType) {
|
|
60
|
+
throw new Error(`${data.type} is not supported in MySQL`);
|
|
61
|
+
}
|
|
62
|
+
const { element, name } = geoType;
|
|
63
|
+
if (!element) {
|
|
64
|
+
// Point
|
|
65
|
+
return ` ${name}(${coordinate.join(',')})`;
|
|
66
|
+
}
|
|
67
|
+
// Polygon or Linestring
|
|
68
|
+
return ` ${name}(${coordinate.map((ele) => transformGeoData({
|
|
69
|
+
type: element,
|
|
70
|
+
coordinate: ele,
|
|
71
|
+
}))})`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
class MySqlTranslator extends sqlTranslator_1.SqlTranslator {
|
|
75
|
+
getDefaultSelectFilter(alias, option) {
|
|
76
|
+
if (option?.includedDeleted) {
|
|
77
|
+
return '';
|
|
78
|
+
}
|
|
79
|
+
return ` (\`${alias}\`.\`$$deleteAt$$\` is null)`;
|
|
80
|
+
}
|
|
81
|
+
makeUpSchema() {
|
|
82
|
+
for (const entity in this.schema) {
|
|
83
|
+
const { attributes, indexes } = this.schema[entity];
|
|
84
|
+
const geoIndexes = [];
|
|
85
|
+
for (const attr in attributes) {
|
|
86
|
+
if (attributes[attr].type === 'geometry') {
|
|
87
|
+
const geoIndex = indexes?.find((idx) => idx.config?.type === 'spatial' && idx.attributes.find((attrDef) => attrDef.name === attr));
|
|
88
|
+
if (!geoIndex) {
|
|
89
|
+
geoIndexes.push({
|
|
90
|
+
name: `${entity}_geo_${attr}`,
|
|
91
|
+
attributes: [{
|
|
92
|
+
name: attr,
|
|
93
|
+
}],
|
|
94
|
+
config: {
|
|
95
|
+
type: 'spatial',
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (geoIndexes.length > 0) {
|
|
102
|
+
if (indexes) {
|
|
103
|
+
indexes.push(...geoIndexes);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
(0, lodash_1.assign)(this.schema[entity], {
|
|
107
|
+
indexes: geoIndexes,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
constructor(schema) {
|
|
114
|
+
super(schema);
|
|
115
|
+
// MySQL为geometry属性默认创建索引
|
|
116
|
+
this.makeUpSchema();
|
|
117
|
+
}
|
|
118
|
+
static supportedDataTypes = [
|
|
119
|
+
// numeric types
|
|
120
|
+
"bit",
|
|
121
|
+
"int",
|
|
122
|
+
"integer",
|
|
123
|
+
"tinyint",
|
|
124
|
+
"smallint",
|
|
125
|
+
"mediumint",
|
|
126
|
+
"bigint",
|
|
127
|
+
"float",
|
|
128
|
+
"double",
|
|
129
|
+
"double precision",
|
|
130
|
+
"real",
|
|
131
|
+
"decimal",
|
|
132
|
+
"dec",
|
|
133
|
+
"numeric",
|
|
134
|
+
"fixed",
|
|
135
|
+
"bool",
|
|
136
|
+
"boolean",
|
|
137
|
+
// date and time types
|
|
138
|
+
"date",
|
|
139
|
+
"datetime",
|
|
140
|
+
"timestamp",
|
|
141
|
+
"time",
|
|
142
|
+
"year",
|
|
143
|
+
// string types
|
|
144
|
+
"char",
|
|
145
|
+
"nchar",
|
|
146
|
+
"national char",
|
|
147
|
+
"varchar",
|
|
148
|
+
"nvarchar",
|
|
149
|
+
"national varchar",
|
|
150
|
+
"blob",
|
|
151
|
+
"text",
|
|
152
|
+
"tinyblob",
|
|
153
|
+
"tinytext",
|
|
154
|
+
"mediumblob",
|
|
155
|
+
"mediumtext",
|
|
156
|
+
"longblob",
|
|
157
|
+
"longtext",
|
|
158
|
+
"enum",
|
|
159
|
+
"set",
|
|
160
|
+
"binary",
|
|
161
|
+
"varbinary",
|
|
162
|
+
// json data type
|
|
163
|
+
"json",
|
|
164
|
+
// spatial data types
|
|
165
|
+
"geometry",
|
|
166
|
+
"point",
|
|
167
|
+
"linestring",
|
|
168
|
+
"polygon",
|
|
169
|
+
"multipoint",
|
|
170
|
+
"multilinestring",
|
|
171
|
+
"multipolygon",
|
|
172
|
+
"geometrycollection"
|
|
173
|
+
];
|
|
174
|
+
static spatialTypes = [
|
|
175
|
+
"geometry",
|
|
176
|
+
"point",
|
|
177
|
+
"linestring",
|
|
178
|
+
"polygon",
|
|
179
|
+
"multipoint",
|
|
180
|
+
"multilinestring",
|
|
181
|
+
"multipolygon",
|
|
182
|
+
"geometrycollection"
|
|
183
|
+
];
|
|
184
|
+
static withLengthDataTypes = [
|
|
185
|
+
"char",
|
|
186
|
+
"varchar",
|
|
187
|
+
"nvarchar",
|
|
188
|
+
"binary",
|
|
189
|
+
"varbinary"
|
|
190
|
+
];
|
|
191
|
+
static withPrecisionDataTypes = [
|
|
192
|
+
"decimal",
|
|
193
|
+
"dec",
|
|
194
|
+
"numeric",
|
|
195
|
+
"fixed",
|
|
196
|
+
"float",
|
|
197
|
+
"double",
|
|
198
|
+
"double precision",
|
|
199
|
+
"real",
|
|
200
|
+
"time",
|
|
201
|
+
"datetime",
|
|
202
|
+
"timestamp"
|
|
203
|
+
];
|
|
204
|
+
static withScaleDataTypes = [
|
|
205
|
+
"decimal",
|
|
206
|
+
"dec",
|
|
207
|
+
"numeric",
|
|
208
|
+
"fixed",
|
|
209
|
+
"float",
|
|
210
|
+
"double",
|
|
211
|
+
"double precision",
|
|
212
|
+
"real"
|
|
213
|
+
];
|
|
214
|
+
static unsignedAndZerofillTypes = [
|
|
215
|
+
"int",
|
|
216
|
+
"integer",
|
|
217
|
+
"smallint",
|
|
218
|
+
"tinyint",
|
|
219
|
+
"mediumint",
|
|
220
|
+
"bigint",
|
|
221
|
+
"decimal",
|
|
222
|
+
"dec",
|
|
223
|
+
"numeric",
|
|
224
|
+
"fixed",
|
|
225
|
+
"float",
|
|
226
|
+
"double",
|
|
227
|
+
"double precision",
|
|
228
|
+
"real"
|
|
229
|
+
];
|
|
230
|
+
static withWidthDataTypes = [
|
|
231
|
+
'int',
|
|
232
|
+
];
|
|
233
|
+
static dataTypeDefaults = {
|
|
234
|
+
"varchar": { length: 255 },
|
|
235
|
+
"nvarchar": { length: 255 },
|
|
236
|
+
"national varchar": { length: 255 },
|
|
237
|
+
"char": { length: 1 },
|
|
238
|
+
"binary": { length: 1 },
|
|
239
|
+
"varbinary": { length: 255 },
|
|
240
|
+
"decimal": { precision: 10, scale: 0 },
|
|
241
|
+
"dec": { precision: 10, scale: 0 },
|
|
242
|
+
"numeric": { precision: 10, scale: 0 },
|
|
243
|
+
"fixed": { precision: 10, scale: 0 },
|
|
244
|
+
"float": { precision: 12 },
|
|
245
|
+
"double": { precision: 22 },
|
|
246
|
+
"time": { precision: 0 },
|
|
247
|
+
"datetime": { precision: 0 },
|
|
248
|
+
"timestamp": { precision: 0 },
|
|
249
|
+
"bit": { width: 1 },
|
|
250
|
+
"int": { width: 11 },
|
|
251
|
+
"integer": { width: 11 },
|
|
252
|
+
"tinyint": { width: 4 },
|
|
253
|
+
"smallint": { width: 6 },
|
|
254
|
+
"mediumint": { width: 9 },
|
|
255
|
+
"bigint": { width: 20 }
|
|
256
|
+
};
|
|
257
|
+
maxAliasLength = 63;
|
|
258
|
+
populateDataTypeDef(type, params, enumeration) {
|
|
259
|
+
if (['date', 'datetime', 'time', 'sequence'].includes(type)) {
|
|
260
|
+
return 'bigint ';
|
|
261
|
+
}
|
|
262
|
+
if (['object', 'array'].includes(type)) {
|
|
263
|
+
return 'json ';
|
|
264
|
+
}
|
|
265
|
+
if (['image', 'function'].includes(type)) {
|
|
266
|
+
return 'text ';
|
|
267
|
+
}
|
|
268
|
+
if (type === 'ref') {
|
|
269
|
+
return 'char(36)';
|
|
270
|
+
}
|
|
271
|
+
if (type === 'money') {
|
|
272
|
+
return 'bigint';
|
|
273
|
+
}
|
|
274
|
+
if (type === 'enum') {
|
|
275
|
+
(0, assert_1.default)(enumeration);
|
|
276
|
+
return `enum(${enumeration.map(ele => `'${ele}'`).join(',')})`;
|
|
277
|
+
}
|
|
278
|
+
if (MySqlTranslator.withLengthDataTypes.includes(type)) {
|
|
279
|
+
if (params) {
|
|
280
|
+
const { length } = params;
|
|
281
|
+
return `${type}(${length}) `;
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
const { length } = MySqlTranslator.dataTypeDefaults[type];
|
|
285
|
+
return `${type}(${length}) `;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (MySqlTranslator.withPrecisionDataTypes.includes(type)) {
|
|
289
|
+
if (params) {
|
|
290
|
+
const { precision, scale } = params;
|
|
291
|
+
if (typeof scale === 'number') {
|
|
292
|
+
return `${type}(${precision}, ${scale}) `;
|
|
293
|
+
}
|
|
294
|
+
return `${type}(${precision})`;
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
const { precision, scale } = MySqlTranslator.dataTypeDefaults[type];
|
|
298
|
+
if (typeof scale === 'number') {
|
|
299
|
+
return `${type}(${precision}, ${scale}) `;
|
|
300
|
+
}
|
|
301
|
+
return `${type}(${precision})`;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (MySqlTranslator.withWidthDataTypes.includes(type)) {
|
|
305
|
+
(0, assert_1.default)(type === 'int');
|
|
306
|
+
const { width } = params;
|
|
307
|
+
switch (width) {
|
|
308
|
+
case 1: {
|
|
309
|
+
return 'tinyint';
|
|
310
|
+
}
|
|
311
|
+
case 2: {
|
|
312
|
+
return 'smallint';
|
|
313
|
+
}
|
|
314
|
+
case 3: {
|
|
315
|
+
return 'mediumint';
|
|
316
|
+
}
|
|
317
|
+
case 4: {
|
|
318
|
+
return 'int';
|
|
319
|
+
}
|
|
320
|
+
default: {
|
|
321
|
+
return 'bigint';
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return `${type} `;
|
|
326
|
+
}
|
|
327
|
+
translateAttrProjection(dataType, alias, attr) {
|
|
328
|
+
switch (dataType) {
|
|
329
|
+
case 'geometry': {
|
|
330
|
+
return ` st_astext(\`${alias}\`.\`${attr}\`)`;
|
|
331
|
+
}
|
|
332
|
+
default: {
|
|
333
|
+
return ` \`${alias}\`.\`${attr}\``;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
translateObjectPredicate(predicate, alias, attr) {
|
|
338
|
+
const translateInner = (o, p) => {
|
|
339
|
+
let stmt2 = '';
|
|
340
|
+
if (o instanceof Array) {
|
|
341
|
+
o.forEach((ele, idx) => {
|
|
342
|
+
if (ele !== undefined && ele !== null) {
|
|
343
|
+
const part = translateInner(ele, `${p}[${idx}]`);
|
|
344
|
+
if (stmt2) {
|
|
345
|
+
stmt2 += ' and ';
|
|
346
|
+
}
|
|
347
|
+
stmt2 += `${part}`;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
else if (typeof o === 'object') {
|
|
352
|
+
for (const attr2 in o) {
|
|
353
|
+
if (attr2 === '$and') {
|
|
354
|
+
o[attr2].forEach((ele) => {
|
|
355
|
+
const part = translateInner(ele, p);
|
|
356
|
+
if (stmt2) {
|
|
357
|
+
stmt2 += ' and ';
|
|
358
|
+
}
|
|
359
|
+
stmt2 += `${part}`;
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
else if (attr2 === '$or') {
|
|
363
|
+
let stmtOr = '';
|
|
364
|
+
o[attr2].forEach((ele) => {
|
|
365
|
+
const part = translateInner(ele, p);
|
|
366
|
+
if (stmtOr) {
|
|
367
|
+
stmtOr += ' or ';
|
|
368
|
+
}
|
|
369
|
+
stmtOr += `${part}`;
|
|
370
|
+
});
|
|
371
|
+
if (stmt2) {
|
|
372
|
+
stmt2 += ' and ';
|
|
373
|
+
}
|
|
374
|
+
stmt2 += `(${stmtOr})`;
|
|
375
|
+
}
|
|
376
|
+
else if (attr2 === '$contains') {
|
|
377
|
+
// json_contains,多值的包含关系
|
|
378
|
+
const value = JSON.stringify(o[attr2]);
|
|
379
|
+
if (stmt2) {
|
|
380
|
+
stmt2 += ' and ';
|
|
381
|
+
}
|
|
382
|
+
if (p) {
|
|
383
|
+
stmt2 += `(JSON_CONTAINS(${alias}.${attr}->>"$${p}", CAST('${value}' AS JSON)))`;
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
stmt2 += `(JSON_CONTAINS(${alias}.${attr}, CAST('${value}' AS JSON)))`;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
else if (attr2 === '$overlaps') {
|
|
390
|
+
// json_overlaps,多值的交叉关系
|
|
391
|
+
const value = JSON.stringify(o[attr2]);
|
|
392
|
+
if (stmt2) {
|
|
393
|
+
stmt2 += ' and ';
|
|
394
|
+
}
|
|
395
|
+
if (p) {
|
|
396
|
+
stmt2 += `(JSON_OVERLAPS(${alias}.${attr}->>"$${p}", CAST('${value}' AS JSON)))`;
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
stmt2 += `(JSON_OVERLAPS(${alias}.${attr}, CAST('${value}' AS JSON)))`;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
else if (attr2 === '$length') {
|
|
403
|
+
// json length
|
|
404
|
+
const length = o[attr2];
|
|
405
|
+
if (stmt2) {
|
|
406
|
+
stmt2 += ' and ';
|
|
407
|
+
}
|
|
408
|
+
if (typeof length === 'number') {
|
|
409
|
+
if (p) {
|
|
410
|
+
stmt2 += `(JSON_LENGTH(${alias}.${attr}->>"$${p}") = ${length})`;
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
stmt2 += `(JSON_LENGTH(${alias}.${attr}) = ${length})`;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
(0, assert_1.default)(typeof length === 'object');
|
|
418
|
+
const op = Object.keys(length)[0];
|
|
419
|
+
(0, assert_1.default)(op.startsWith('$'));
|
|
420
|
+
if (p) {
|
|
421
|
+
stmt2 += `(JSON_LENGTH(${alias}.${attr}->>"$${p}") ${this.translatePredicate(op, length[op])})`;
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
stmt2 += `(JSON_LENGTH(${alias}.${attr}) ${this.translatePredicate(op, length[op])})`;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
else if (attr2.startsWith('$')) {
|
|
429
|
+
if (stmt2) {
|
|
430
|
+
stmt2 += ' and ';
|
|
431
|
+
}
|
|
432
|
+
if (p) {
|
|
433
|
+
stmt2 += `(${alias}.${attr}->>"$${p}" ${this.translatePredicate(attr2, o[attr2])})`;
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
stmt2 += `(${alias}.${attr} ${this.translatePredicate(attr2, o[attr2])})`;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
// 继续子对象解构
|
|
441
|
+
const attr3 = attr2.startsWith('.') ? attr2.slice(1) : attr2;
|
|
442
|
+
const part = translateInner(o[attr2], `${p}.${attr3}`);
|
|
443
|
+
if (stmt2) {
|
|
444
|
+
stmt2 += ' and ';
|
|
445
|
+
}
|
|
446
|
+
stmt2 += `${part}`;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
// 直接的属性处理
|
|
452
|
+
if (stmt2) {
|
|
453
|
+
stmt2 += ' and ';
|
|
454
|
+
}
|
|
455
|
+
if (typeof o === 'string') {
|
|
456
|
+
if (p) {
|
|
457
|
+
stmt2 += `(${alias}.${attr}->>"$${p}" = '${o}')`;
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
// 对根对象的字符串比较
|
|
461
|
+
stmt2 += `(${alias}.${attr} = '${o}')`;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
(0, assert_1.default)(p);
|
|
466
|
+
stmt2 += `(${alias}.${attr}->>"$${p}" = ${o})`;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return stmt2;
|
|
470
|
+
};
|
|
471
|
+
return translateInner(predicate, '');
|
|
472
|
+
}
|
|
473
|
+
translateObjectProjection(projection, alias, attr, prefix) {
|
|
474
|
+
let stmt = '';
|
|
475
|
+
const translateInner = (o, p) => {
|
|
476
|
+
if (o instanceof Array) {
|
|
477
|
+
o.forEach((item, idx) => {
|
|
478
|
+
const p2 = `${p}[${idx}]`;
|
|
479
|
+
if (typeof item === 'number') {
|
|
480
|
+
if (stmt) {
|
|
481
|
+
stmt += ', ';
|
|
482
|
+
}
|
|
483
|
+
stmt += `${alias}.${attr}->>"$${p2}"`;
|
|
484
|
+
stmt += prefix ? ` as \`${prefix}.${attr}${p2}\`` : ` as \`${attr}${p2}\``;
|
|
485
|
+
}
|
|
486
|
+
else if (typeof item === 'object') {
|
|
487
|
+
translateInner(item, p2);
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
for (const key in o) {
|
|
493
|
+
const p2 = `${p}.${key}`;
|
|
494
|
+
if (typeof o[key] === 'number') {
|
|
495
|
+
if (stmt) {
|
|
496
|
+
stmt += ', ';
|
|
497
|
+
}
|
|
498
|
+
stmt += `${alias}.${attr}->>"$${p2}"`;
|
|
499
|
+
stmt += prefix ? ` as \`${prefix}.${attr}${p2}\`` : ` as \`${attr}${p2}\``;
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
translateInner(o[key], p2);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
translateInner(projection, '');
|
|
508
|
+
return stmt;
|
|
509
|
+
}
|
|
510
|
+
translateAttrValue(dataType, value) {
|
|
511
|
+
if (value === null || value === undefined) {
|
|
512
|
+
return 'null';
|
|
513
|
+
}
|
|
514
|
+
switch (dataType) {
|
|
515
|
+
case 'geometry': {
|
|
516
|
+
return transformGeoData(value);
|
|
517
|
+
}
|
|
518
|
+
case 'datetime':
|
|
519
|
+
case 'time':
|
|
520
|
+
case 'date': {
|
|
521
|
+
if (value instanceof Date) {
|
|
522
|
+
return `${value.valueOf()}`;
|
|
523
|
+
}
|
|
524
|
+
else if (typeof value === 'number') {
|
|
525
|
+
return `${value}`;
|
|
526
|
+
}
|
|
527
|
+
return `'${(new Date(value)).valueOf()}'`;
|
|
528
|
+
}
|
|
529
|
+
case 'object':
|
|
530
|
+
case 'array': {
|
|
531
|
+
return this.escapeStringValue(JSON.stringify(value));
|
|
532
|
+
}
|
|
533
|
+
/* case 'function': {
|
|
534
|
+
return `'${Buffer.from(value.toString()).toString('base64')}'`;
|
|
535
|
+
} */
|
|
536
|
+
default: {
|
|
537
|
+
if (typeof value === 'string') {
|
|
538
|
+
return this.escapeStringValue(value);
|
|
539
|
+
}
|
|
540
|
+
return value;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
translateFullTextSearch(value, entity, alias) {
|
|
545
|
+
const { $search } = value;
|
|
546
|
+
const { indexes } = this.schema[entity];
|
|
547
|
+
const ftIndex = indexes && indexes.find((ele) => {
|
|
548
|
+
const { config } = ele;
|
|
549
|
+
return config && config.type === 'fulltext';
|
|
550
|
+
});
|
|
551
|
+
(0, assert_1.default)(ftIndex);
|
|
552
|
+
const { attributes } = ftIndex;
|
|
553
|
+
const columns2 = attributes.map(({ name }) => `${alias}.${name}`);
|
|
554
|
+
return ` match(${columns2.join(',')}) against ('${$search}' in natural language mode)`;
|
|
555
|
+
}
|
|
556
|
+
translateCreateEntity(entity, options) {
|
|
557
|
+
const ifExists = options?.ifExists;
|
|
558
|
+
const { schema } = this;
|
|
559
|
+
const entityDef = schema[entity];
|
|
560
|
+
const { storageName, attributes, indexes, view, static: _static } = entityDef;
|
|
561
|
+
let hasSequence = false;
|
|
562
|
+
// todo view暂还不支持
|
|
563
|
+
const entityType = view ? 'view' : 'table';
|
|
564
|
+
let sql = `create ${entityType} `;
|
|
565
|
+
if (ifExists === 'omit' || (_static && ifExists === 'dropIfNotStatic')) {
|
|
566
|
+
sql += ' if not exists';
|
|
567
|
+
}
|
|
568
|
+
if (storageName) {
|
|
569
|
+
sql += `\`${storageName}\` `;
|
|
570
|
+
}
|
|
571
|
+
else {
|
|
572
|
+
sql += `\`${entity}\` `;
|
|
573
|
+
}
|
|
574
|
+
if (view) {
|
|
575
|
+
throw new Error(' view unsupported yet');
|
|
576
|
+
}
|
|
577
|
+
else {
|
|
578
|
+
sql += '(';
|
|
579
|
+
// 翻译所有的属性
|
|
580
|
+
Object.keys(attributes).forEach((attr, idx) => {
|
|
581
|
+
const attrDef = attributes[attr];
|
|
582
|
+
const { type, params, default: defaultValue, unique, notNull, sequenceStart, enumeration, } = attrDef;
|
|
583
|
+
sql += `\`${attr}\` `;
|
|
584
|
+
sql += this.populateDataTypeDef(type, params, enumeration);
|
|
585
|
+
if (notNull || type === 'geometry') {
|
|
586
|
+
sql += ' not null ';
|
|
587
|
+
}
|
|
588
|
+
if (unique) {
|
|
589
|
+
sql += ' unique ';
|
|
590
|
+
}
|
|
591
|
+
if (sequenceStart) {
|
|
592
|
+
if (hasSequence) {
|
|
593
|
+
throw new Error(`「${entity}」只能有一个sequence列`);
|
|
594
|
+
}
|
|
595
|
+
hasSequence = sequenceStart;
|
|
596
|
+
sql += ' auto_increment unique ';
|
|
597
|
+
}
|
|
598
|
+
if (defaultValue !== undefined) {
|
|
599
|
+
(0, assert_1.default)(type !== 'ref');
|
|
600
|
+
sql += ` default ${this.translateAttrValue(type, defaultValue)}`;
|
|
601
|
+
}
|
|
602
|
+
if (attr === 'id') {
|
|
603
|
+
sql += ' primary key';
|
|
604
|
+
}
|
|
605
|
+
if (idx < Object.keys(attributes).length - 1) {
|
|
606
|
+
sql += ',\n';
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
// 翻译索引信息
|
|
610
|
+
if (indexes) {
|
|
611
|
+
sql += ',\n';
|
|
612
|
+
indexes.forEach(({ name, attributes, config }, idx) => {
|
|
613
|
+
const { unique, type, parser } = config || {};
|
|
614
|
+
// 因为有deleteAt的存在,这里的unique没意义,只能框架自己去建立checker来处理
|
|
615
|
+
/* if (unique) {
|
|
616
|
+
sql += ' unique ';
|
|
617
|
+
}
|
|
618
|
+
else */ if (type === 'fulltext') {
|
|
619
|
+
sql += ' fulltext ';
|
|
620
|
+
}
|
|
621
|
+
else if (type === 'spatial') {
|
|
622
|
+
sql += ' spatial ';
|
|
623
|
+
}
|
|
624
|
+
sql += `index ${name} `;
|
|
625
|
+
if (type === 'hash') {
|
|
626
|
+
sql += ` using hash `;
|
|
627
|
+
}
|
|
628
|
+
sql += '(';
|
|
629
|
+
let includeDeleteAt = false;
|
|
630
|
+
attributes.forEach(({ name, size, direction }, idx2) => {
|
|
631
|
+
sql += `\`${name}\``;
|
|
632
|
+
if (size) {
|
|
633
|
+
sql += ` (${size})`;
|
|
634
|
+
}
|
|
635
|
+
if (direction) {
|
|
636
|
+
sql += ` ${direction}`;
|
|
637
|
+
}
|
|
638
|
+
if (idx2 < attributes.length - 1) {
|
|
639
|
+
sql += ',';
|
|
640
|
+
}
|
|
641
|
+
if (name === '$$deleteAt$$') {
|
|
642
|
+
includeDeleteAt = true;
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
if (!includeDeleteAt && !type) {
|
|
646
|
+
sql += ', `$$deleteAt$$`'; // 在mysql80+之后,需要给属性加上``包裹,否则会报错
|
|
647
|
+
}
|
|
648
|
+
sql += ')';
|
|
649
|
+
if (parser) {
|
|
650
|
+
sql += ` with parser ${parser}`;
|
|
651
|
+
}
|
|
652
|
+
if (idx < indexes.length - 1) {
|
|
653
|
+
sql += ',\n';
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
sql += ')';
|
|
659
|
+
if (typeof hasSequence === 'number') {
|
|
660
|
+
sql += `auto_increment = ${hasSequence}`;
|
|
661
|
+
}
|
|
662
|
+
if (ifExists === 'drop' || (!_static && ifExists === 'dropIfNotStatic')) {
|
|
663
|
+
return [`drop ${entityType} if exists \`${storageName || entity}\`;`, sql];
|
|
664
|
+
}
|
|
665
|
+
return [sql];
|
|
666
|
+
}
|
|
667
|
+
translateFnName(fnName, argumentNumber) {
|
|
668
|
+
switch (fnName) {
|
|
669
|
+
case '$add': {
|
|
670
|
+
let result = '%s';
|
|
671
|
+
while (--argumentNumber > 0) {
|
|
672
|
+
result += ' + %s';
|
|
673
|
+
}
|
|
674
|
+
return result;
|
|
675
|
+
}
|
|
676
|
+
case '$subtract': {
|
|
677
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
678
|
+
return '%s - %s';
|
|
679
|
+
}
|
|
680
|
+
case '$multiply': {
|
|
681
|
+
let result = '%s';
|
|
682
|
+
while (--argumentNumber > 0) {
|
|
683
|
+
result += ' * %s';
|
|
684
|
+
}
|
|
685
|
+
return result;
|
|
686
|
+
}
|
|
687
|
+
case '$divide': {
|
|
688
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
689
|
+
return '%s / %s';
|
|
690
|
+
}
|
|
691
|
+
case '$abs': {
|
|
692
|
+
return 'ABS(%s)';
|
|
693
|
+
}
|
|
694
|
+
case '$round': {
|
|
695
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
696
|
+
return 'ROUND(%s, %s)';
|
|
697
|
+
}
|
|
698
|
+
case '$ceil': {
|
|
699
|
+
return 'CEIL(%s)';
|
|
700
|
+
}
|
|
701
|
+
case '$floor': {
|
|
702
|
+
return 'FLOOR(%s)';
|
|
703
|
+
}
|
|
704
|
+
case '$mod': {
|
|
705
|
+
return 'MOD(%s, %s)';
|
|
706
|
+
}
|
|
707
|
+
case '$pow': {
|
|
708
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
709
|
+
return 'POW(%s, %s)';
|
|
710
|
+
}
|
|
711
|
+
case '$gt': {
|
|
712
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
713
|
+
return '%s > %s';
|
|
714
|
+
}
|
|
715
|
+
case '$gte': {
|
|
716
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
717
|
+
return '%s >= %s';
|
|
718
|
+
}
|
|
719
|
+
case '$lt': {
|
|
720
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
721
|
+
return '%s < %s';
|
|
722
|
+
}
|
|
723
|
+
case '$lte': {
|
|
724
|
+
return '%s <= %s';
|
|
725
|
+
}
|
|
726
|
+
case '$eq': {
|
|
727
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
728
|
+
return '%s = %s';
|
|
729
|
+
}
|
|
730
|
+
case '$ne': {
|
|
731
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
732
|
+
return '%s <> %s';
|
|
733
|
+
}
|
|
734
|
+
case '$startsWith': {
|
|
735
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
736
|
+
return '%s like CONCAT(%s, \'%\')';
|
|
737
|
+
}
|
|
738
|
+
case '$endsWith': {
|
|
739
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
740
|
+
return '%s like CONCAT(\'%\', %s)';
|
|
741
|
+
}
|
|
742
|
+
case '$includes': {
|
|
743
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
744
|
+
return '%s like CONCAT(\'%\', %s, \'%\')';
|
|
745
|
+
}
|
|
746
|
+
case '$true': {
|
|
747
|
+
return '%s = true';
|
|
748
|
+
}
|
|
749
|
+
case '$false': {
|
|
750
|
+
return '%s = false';
|
|
751
|
+
}
|
|
752
|
+
case '$and': {
|
|
753
|
+
let result = '';
|
|
754
|
+
for (let iter = 0; iter < argumentNumber; iter++) {
|
|
755
|
+
result += '%s';
|
|
756
|
+
if (iter < argumentNumber - 1) {
|
|
757
|
+
result += ' and ';
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return result;
|
|
761
|
+
}
|
|
762
|
+
case '$or': {
|
|
763
|
+
let result = '';
|
|
764
|
+
for (let iter = 0; iter < argumentNumber; iter++) {
|
|
765
|
+
result += '%s';
|
|
766
|
+
if (iter < argumentNumber - 1) {
|
|
767
|
+
result += ' or ';
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return result;
|
|
771
|
+
}
|
|
772
|
+
case '$not': {
|
|
773
|
+
return 'not %s';
|
|
774
|
+
}
|
|
775
|
+
case '$year': {
|
|
776
|
+
return 'YEAR(%s)';
|
|
777
|
+
}
|
|
778
|
+
case '$month': {
|
|
779
|
+
return 'MONTH(%s)';
|
|
780
|
+
}
|
|
781
|
+
case '$weekday': {
|
|
782
|
+
return 'WEEKDAY(%s)';
|
|
783
|
+
}
|
|
784
|
+
case '$weekOfYear': {
|
|
785
|
+
return 'WEEKOFYEAR(%s)';
|
|
786
|
+
}
|
|
787
|
+
case '$day': {
|
|
788
|
+
return 'DAY(%s)';
|
|
789
|
+
}
|
|
790
|
+
case '$dayOfMonth': {
|
|
791
|
+
return 'DAYOFMONTH(%s)';
|
|
792
|
+
}
|
|
793
|
+
case '$dayOfWeek': {
|
|
794
|
+
return 'DAYOFWEEK(%s)';
|
|
795
|
+
}
|
|
796
|
+
case '$dayOfYear': {
|
|
797
|
+
return 'DAYOFYEAR(%s)';
|
|
798
|
+
}
|
|
799
|
+
case '$dateDiff': {
|
|
800
|
+
(0, assert_1.default)(argumentNumber === 3);
|
|
801
|
+
return 'DATEDIFF(%s, %s, %s)';
|
|
802
|
+
}
|
|
803
|
+
case '$contains': {
|
|
804
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
805
|
+
return 'ST_CONTAINS(%s, %s)';
|
|
806
|
+
}
|
|
807
|
+
case '$distance': {
|
|
808
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
809
|
+
return 'ST_DISTANCE(%s, %s)';
|
|
810
|
+
}
|
|
811
|
+
case '$concat': {
|
|
812
|
+
let result = ' concat(%s';
|
|
813
|
+
while (--argumentNumber > 0) {
|
|
814
|
+
result += ', %s';
|
|
815
|
+
}
|
|
816
|
+
result += ')';
|
|
817
|
+
return result;
|
|
818
|
+
}
|
|
819
|
+
default: {
|
|
820
|
+
throw new Error(`unrecoganized function ${fnName}`);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
translateAttrInExpression(entity, attr, exprText) {
|
|
825
|
+
const { attributes } = this.schema[entity];
|
|
826
|
+
const { type } = attributes[attr];
|
|
827
|
+
if (['date', 'time', 'datetime'].includes(type)) {
|
|
828
|
+
// 从unix时间戵转成date类型参加expr的运算
|
|
829
|
+
return `from_unixtime(${exprText} / 1000)`;
|
|
830
|
+
}
|
|
831
|
+
return exprText;
|
|
832
|
+
}
|
|
833
|
+
translateExpression(entity, alias, expression, refDict) {
|
|
834
|
+
const translateConstant = (constant) => {
|
|
835
|
+
if (constant instanceof Date) {
|
|
836
|
+
return ` from_unixtime(${constant.valueOf()}/1000)`;
|
|
837
|
+
}
|
|
838
|
+
else if (typeof constant === 'string') {
|
|
839
|
+
return ` '${constant}'`;
|
|
840
|
+
}
|
|
841
|
+
else {
|
|
842
|
+
(0, assert_1.default)(typeof constant === 'number');
|
|
843
|
+
return ` ${constant}`;
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
const translateInner = (expr) => {
|
|
847
|
+
const k = Object.keys(expr);
|
|
848
|
+
let result;
|
|
849
|
+
if (k.includes('#attr')) {
|
|
850
|
+
const attrText = `\`${alias}\`.\`${(expr)['#attr']}\``;
|
|
851
|
+
result = this.translateAttrInExpression(entity, (expr)['#attr'], attrText);
|
|
852
|
+
}
|
|
853
|
+
else if (k.includes('#refId')) {
|
|
854
|
+
const refId = (expr)['#refId'];
|
|
855
|
+
const refAttr = (expr)['#refAttr'];
|
|
856
|
+
(0, assert_1.default)(refDict[refId]);
|
|
857
|
+
const attrText = `\`${refDict[refId][0]}\`.\`${refAttr}\``;
|
|
858
|
+
result = this.translateAttrInExpression(entity, (expr)['#refAttr'], attrText);
|
|
859
|
+
}
|
|
860
|
+
else {
|
|
861
|
+
(0, assert_1.default)(k.length === 1);
|
|
862
|
+
if ((expr)[k[0]] instanceof Array) {
|
|
863
|
+
const fnName = this.translateFnName(k[0], (expr)[k[0]].length);
|
|
864
|
+
const args = [fnName];
|
|
865
|
+
args.push(...(expr)[k[0]].map((ele) => {
|
|
866
|
+
if (['string', 'number'].includes(typeof ele) || ele instanceof Date) {
|
|
867
|
+
return translateConstant(ele);
|
|
868
|
+
}
|
|
869
|
+
else {
|
|
870
|
+
return translateInner(ele);
|
|
871
|
+
}
|
|
872
|
+
}));
|
|
873
|
+
result = util_1.format.apply(null, args);
|
|
874
|
+
}
|
|
875
|
+
else {
|
|
876
|
+
const fnName = this.translateFnName(k[0], 1);
|
|
877
|
+
const args = [fnName];
|
|
878
|
+
const arg = (expr)[k[0]];
|
|
879
|
+
if (['string', 'number'].includes(typeof arg) || arg instanceof Date) {
|
|
880
|
+
args.push(translateConstant(arg));
|
|
881
|
+
}
|
|
882
|
+
else {
|
|
883
|
+
args.push(translateInner(arg));
|
|
884
|
+
}
|
|
885
|
+
result = util_1.format.apply(null, args);
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
return result;
|
|
889
|
+
};
|
|
890
|
+
return translateInner(expression);
|
|
891
|
+
}
|
|
892
|
+
populateSelectStmt(projectionText, fromText, aliasDict, filterText, sorterText, groupByText, indexFrom, count, option) {
|
|
893
|
+
// todo hint of use index
|
|
894
|
+
let sql = `select ${projectionText} from ${fromText}`;
|
|
895
|
+
if (filterText) {
|
|
896
|
+
sql += ` where ${filterText}`;
|
|
897
|
+
}
|
|
898
|
+
if (sorterText) {
|
|
899
|
+
sql += ` order by ${sorterText}`;
|
|
900
|
+
}
|
|
901
|
+
if (groupByText) {
|
|
902
|
+
sql += ` group by ${groupByText}`;
|
|
903
|
+
}
|
|
904
|
+
if (typeof indexFrom === 'number') {
|
|
905
|
+
(0, assert_1.default)(typeof count === 'number');
|
|
906
|
+
sql += ` limit ${indexFrom}, ${count}`;
|
|
907
|
+
}
|
|
908
|
+
if (option?.forUpdate) {
|
|
909
|
+
sql += ' for update';
|
|
910
|
+
if (typeof option?.forUpdate === 'string') {
|
|
911
|
+
sql += ` ${option.forUpdate}`;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
return sql;
|
|
915
|
+
}
|
|
916
|
+
populateUpdateStmt(updateText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
917
|
+
// todo using index
|
|
918
|
+
(0, assert_1.default)(updateText);
|
|
919
|
+
let sql = `update ${fromText} set ${updateText}`;
|
|
920
|
+
if (filterText) {
|
|
921
|
+
sql += ` where ${filterText}`;
|
|
922
|
+
}
|
|
923
|
+
if (sorterText) {
|
|
924
|
+
sql += ` order by ${sorterText}`;
|
|
925
|
+
}
|
|
926
|
+
if (typeof indexFrom === 'number') {
|
|
927
|
+
(0, assert_1.default)(typeof count === 'number');
|
|
928
|
+
sql += ` limit ${indexFrom}, ${count}`;
|
|
929
|
+
}
|
|
930
|
+
return sql;
|
|
931
|
+
}
|
|
932
|
+
populateRemoveStmt(updateText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
|
|
933
|
+
// todo using index
|
|
934
|
+
const alias = aliasDict['./'];
|
|
935
|
+
if (option?.deletePhysically) {
|
|
936
|
+
// assert(!updateText, 'physically delete does not support setting trigger data');
|
|
937
|
+
let sql = `delete ${alias} from ${fromText} `;
|
|
938
|
+
if (filterText) {
|
|
939
|
+
sql += ` where ${filterText}`;
|
|
940
|
+
}
|
|
941
|
+
if (sorterText) {
|
|
942
|
+
sql += ` order by ${sorterText}`;
|
|
943
|
+
}
|
|
944
|
+
if (typeof indexFrom === 'number') {
|
|
945
|
+
(0, assert_1.default)(typeof count === 'number');
|
|
946
|
+
sql += ` limit ${indexFrom}, ${count}`;
|
|
947
|
+
}
|
|
948
|
+
return sql;
|
|
949
|
+
}
|
|
950
|
+
// 新的remove应该包含$$deleteAt$$的值了
|
|
951
|
+
/* const now = Date.now();
|
|
952
|
+
|
|
953
|
+
const updateText2 = updateText ? `${updateText}, \`${alias}\`.\`$$deleteAt$$\` = '${now}'` : `\`${alias}\`.\`$$deleteAt$$\` = '${now}'`; */
|
|
954
|
+
(0, assert_1.default)(updateText.includes(types_1.DeleteAtAttribute));
|
|
955
|
+
let sql = `update ${fromText} set ${updateText}`;
|
|
956
|
+
if (filterText) {
|
|
957
|
+
sql += ` where ${filterText}`;
|
|
958
|
+
}
|
|
959
|
+
if (sorterText) {
|
|
960
|
+
sql += ` order by ${sorterText}`;
|
|
961
|
+
}
|
|
962
|
+
if (typeof indexFrom === 'number') {
|
|
963
|
+
(0, assert_1.default)(typeof count === 'number');
|
|
964
|
+
sql += ` limit ${indexFrom}, ${count}`;
|
|
965
|
+
}
|
|
966
|
+
return sql;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
exports.MySqlTranslator = MySqlTranslator;
|