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