oak-db 3.3.5 → 3.3.7
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/connector.d.ts +15 -15
- package/lib/MySQL/store.d.ts +36 -36
- package/lib/MySQL/store.js +2 -1
- package/lib/MySQL/translator.js +1 -1
- package/lib/sqlTranslator.js +982 -982
- package/package.json +2 -2
package/lib/sqlTranslator.js
CHANGED
|
@@ -1,982 +1,982 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SqlTranslator = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const assert_1 = tslib_1.__importDefault(require("assert"));
|
|
6
|
-
const sqlstring_1 = tslib_1.__importDefault(require("sqlstring"));
|
|
7
|
-
const lodash_1 = require("lodash");
|
|
8
|
-
const types_1 = require("oak-domain/lib/types");
|
|
9
|
-
const relation_1 = require("oak-domain/lib/store/relation");
|
|
10
|
-
const filter_1 = require("oak-domain/lib/store/filter");
|
|
11
|
-
;
|
|
12
|
-
;
|
|
13
|
-
class SqlTranslator {
|
|
14
|
-
schema;
|
|
15
|
-
constructor(schema) {
|
|
16
|
-
this.schema = this.makeFullSchema(schema);
|
|
17
|
-
}
|
|
18
|
-
makeFullSchema(schema2) {
|
|
19
|
-
const schema = (0, lodash_1.cloneDeep)(schema2);
|
|
20
|
-
for (const entity in schema) {
|
|
21
|
-
const { attributes, indexes } = schema[entity];
|
|
22
|
-
// 增加默认的属性
|
|
23
|
-
(0, lodash_1.assign)(attributes, {
|
|
24
|
-
id: {
|
|
25
|
-
type: 'char',
|
|
26
|
-
params: {
|
|
27
|
-
length: 36,
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
[types_1.SeqAttribute]: {
|
|
31
|
-
type: 'sequence',
|
|
32
|
-
sequenceStart: 10000,
|
|
33
|
-
},
|
|
34
|
-
[types_1.CreateAtAttribute]: {
|
|
35
|
-
type: 'datetime',
|
|
36
|
-
notNull: true,
|
|
37
|
-
},
|
|
38
|
-
[types_1.UpdateAtAttribute]: {
|
|
39
|
-
type: 'datetime',
|
|
40
|
-
notNull: true,
|
|
41
|
-
},
|
|
42
|
-
[types_1.DeleteAtAttribute]: {
|
|
43
|
-
type: 'datetime',
|
|
44
|
-
},
|
|
45
|
-
[types_1.TriggerDataAttribute]: {
|
|
46
|
-
type: 'object',
|
|
47
|
-
},
|
|
48
|
-
[types_1.TriggerUuidAttribute]: {
|
|
49
|
-
type: 'char',
|
|
50
|
-
params: {
|
|
51
|
-
length: 36,
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
// 增加默认的索引
|
|
56
|
-
const intrinsticIndexes = [
|
|
57
|
-
{
|
|
58
|
-
name: `${entity}_create_at_auto_create`,
|
|
59
|
-
attributes: [{
|
|
60
|
-
name: types_1.CreateAtAttribute,
|
|
61
|
-
}, {
|
|
62
|
-
name: types_1.DeleteAtAttribute,
|
|
63
|
-
}]
|
|
64
|
-
}, {
|
|
65
|
-
name: `${entity}_update_at_auto_create`,
|
|
66
|
-
attributes: [{
|
|
67
|
-
name: types_1.UpdateAtAttribute,
|
|
68
|
-
}, {
|
|
69
|
-
name: types_1.DeleteAtAttribute,
|
|
70
|
-
}],
|
|
71
|
-
}, {
|
|
72
|
-
name: `${entity}_trigger_uuid`,
|
|
73
|
-
attributes: [{
|
|
74
|
-
name: types_1.TriggerUuidAttribute,
|
|
75
|
-
}]
|
|
76
|
-
},
|
|
77
|
-
];
|
|
78
|
-
// 增加外键等相关属性上的索引
|
|
79
|
-
for (const attr in attributes) {
|
|
80
|
-
if (attributes[attr].type === 'ref') {
|
|
81
|
-
if (!(indexes?.find(ele => ele.attributes[0].name === attr))) {
|
|
82
|
-
intrinsticIndexes.push({
|
|
83
|
-
name: `${entity}_fk_${attr}_auto_create`,
|
|
84
|
-
attributes: [{
|
|
85
|
-
name: attr,
|
|
86
|
-
}, {
|
|
87
|
-
name: '$$deleteAt$$',
|
|
88
|
-
}]
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (attr === 'entity' && attributes[attr].type === 'varchar') {
|
|
93
|
-
const entityIdDef = attributes.entityId;
|
|
94
|
-
if (entityIdDef?.type === 'varchar') {
|
|
95
|
-
if (!(indexes?.find(ele => ele.attributes[0].name === 'entity' && ele.attributes[1]?.name === 'entityId'))) {
|
|
96
|
-
intrinsticIndexes.push({
|
|
97
|
-
name: `${entity}_fk_entity_entityId_auto_create`,
|
|
98
|
-
attributes: [{
|
|
99
|
-
name: 'entity',
|
|
100
|
-
}, {
|
|
101
|
-
name: 'entityId',
|
|
102
|
-
}, {
|
|
103
|
-
name: '$$deleteAt$$',
|
|
104
|
-
}]
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (attr.endsWith('State') && attributes[attr].type === 'varchar') {
|
|
110
|
-
if (!(indexes?.find(ele => ele.attributes[0].name === attr))) {
|
|
111
|
-
intrinsticIndexes.push({
|
|
112
|
-
name: `${entity}_${attr}_auto_create`,
|
|
113
|
-
attributes: [{
|
|
114
|
-
name: attr,
|
|
115
|
-
}, {
|
|
116
|
-
name: '$$deleteAt$$',
|
|
117
|
-
}]
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (attr === 'expired' && attributes[attr].type === 'boolean') {
|
|
122
|
-
const expiresAtDef = attributes.expiresAt;
|
|
123
|
-
if (expiresAtDef?.type === 'datetime') {
|
|
124
|
-
if (!(indexes?.find(ele => ele.attributes[0].name === 'expired' && ele.attributes[1]?.name === 'expiresAt'))) {
|
|
125
|
-
intrinsticIndexes.push({
|
|
126
|
-
name: `${entity}_expires_expiredAt_auto_create`,
|
|
127
|
-
attributes: [{
|
|
128
|
-
name: 'expired',
|
|
129
|
-
}, {
|
|
130
|
-
name: 'expiresAt',
|
|
131
|
-
}, {
|
|
132
|
-
name: '$$deleteAt$$',
|
|
133
|
-
}]
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (indexes) {
|
|
140
|
-
indexes.push(...intrinsticIndexes);
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
(0, lodash_1.assign)(schema[entity], {
|
|
144
|
-
indexes: intrinsticIndexes,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return schema;
|
|
149
|
-
}
|
|
150
|
-
getStorageName(entity) {
|
|
151
|
-
const { storageName } = this.schema[entity];
|
|
152
|
-
return (storageName || entity);
|
|
153
|
-
}
|
|
154
|
-
translateInsert(entity, data) {
|
|
155
|
-
const { schema } = this;
|
|
156
|
-
const { attributes, storageName = entity } = schema[entity];
|
|
157
|
-
let sql = `insert into \`${storageName}\`(`;
|
|
158
|
-
/**
|
|
159
|
-
* 这里的attrs要用所有行的union集合
|
|
160
|
-
*/
|
|
161
|
-
const dataFull = data.reduce((prev, cur) => Object.assign({}, cur, prev), {});
|
|
162
|
-
const attrs = Object.keys(dataFull).filter(ele => attributes.hasOwnProperty(ele));
|
|
163
|
-
attrs.forEach((attr, idx) => {
|
|
164
|
-
sql += ` \`${attr}\``;
|
|
165
|
-
if (idx < attrs.length - 1) {
|
|
166
|
-
sql += ',';
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
sql += ') values ';
|
|
170
|
-
data.forEach((d, dataIndex) => {
|
|
171
|
-
sql += '(';
|
|
172
|
-
attrs.forEach((attr, attrIdx) => {
|
|
173
|
-
const attrDef = attributes[attr];
|
|
174
|
-
const { type: dataType } = attrDef;
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
sql +=
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
sql += ')';
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
alias2 =
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
alias2 =
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
alias2 =
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
alias2 =
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
alias2 =
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
alias2 =
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
$
|
|
432
|
-
$
|
|
433
|
-
$
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
return ` ${SQL_OP[attr]} ${value}`;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
case '$
|
|
448
|
-
return ` like '
|
|
449
|
-
}
|
|
450
|
-
case '$
|
|
451
|
-
return ` like '%${value}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
(
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
return
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
if (
|
|
509
|
-
return
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
return
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
if (
|
|
551
|
-
whereText += '
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
whereText +=
|
|
582
|
-
}
|
|
583
|
-
else if (attr
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
whereText += ` (${translateInner(
|
|
594
|
-
}
|
|
595
|
-
else if (rel
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
*
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
{
|
|
624
|
-
'#
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
if (
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
else
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
if (k.startsWith('#
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
projSubText2 = `
|
|
881
|
-
}
|
|
882
|
-
else if (k.startsWith('#
|
|
883
|
-
if (data.distinct) {
|
|
884
|
-
projSubText = `distinct ${projSubText}`;
|
|
885
|
-
}
|
|
886
|
-
projSubText2 = `
|
|
887
|
-
}
|
|
888
|
-
else
|
|
889
|
-
if (data.distinct) {
|
|
890
|
-
projSubText = `distinct ${projSubText}`;
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
}
|
|
909
|
-
const {
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
}
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
const
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
}
|
|
944
|
-
return this.populateRemoveStmt(updateText, fromText, aliasDict, filterText, /* sorterText */ undefined, indexFrom, count, option);
|
|
945
|
-
}
|
|
946
|
-
translateUpdate(entity, operation, option) {
|
|
947
|
-
const { attributes } = this.schema[entity];
|
|
948
|
-
const { filter, sorter, indexFrom, count, data } = operation;
|
|
949
|
-
(0, assert_1.default)(!sorter, '当前update不支持sorter行为');
|
|
950
|
-
const { aliasDict, filterRefAlias, from: fromText, currentNumber } = this.analyzeJoin(entity, { filter, sorter });
|
|
951
|
-
const alias = aliasDict['./'];
|
|
952
|
-
let updateText = '';
|
|
953
|
-
for (const attr in data) {
|
|
954
|
-
if (updateText) {
|
|
955
|
-
updateText += ',';
|
|
956
|
-
}
|
|
957
|
-
(0, assert_1.default)(attributes.hasOwnProperty(attr));
|
|
958
|
-
const value = this.translateAttrValue(attributes[attr].type, data[attr]);
|
|
959
|
-
updateText += `\`${alias}\`.\`${attr}\` = ${value}`;
|
|
960
|
-
}
|
|
961
|
-
const { stmt: filterText } = this.translateFilter(entity, filter, aliasDict, filterRefAlias, currentNumber, option);
|
|
962
|
-
// const sorterText = sorter && this.translateSorter(entity, sorter, aliasDict);
|
|
963
|
-
return this.populateUpdateStmt(updateText, fromText, aliasDict, filterText, /* sorterText */ undefined, indexFrom, count, option);
|
|
964
|
-
}
|
|
965
|
-
translateDestroyEntity(entity, truncate) {
|
|
966
|
-
const { schema } = this;
|
|
967
|
-
const { storageName = entity, view } = schema[entity];
|
|
968
|
-
let sql;
|
|
969
|
-
if (view) {
|
|
970
|
-
sql = `drop view if exists \`${storageName}\``;
|
|
971
|
-
}
|
|
972
|
-
else {
|
|
973
|
-
sql = truncate ? `truncate table \`${storageName}\`` : `drop table if exists \`${storageName}\``;
|
|
974
|
-
}
|
|
975
|
-
return sql;
|
|
976
|
-
}
|
|
977
|
-
escapeStringValue(value) {
|
|
978
|
-
const result = sqlstring_1.default.escape(value);
|
|
979
|
-
return result;
|
|
980
|
-
}
|
|
981
|
-
}
|
|
982
|
-
exports.SqlTranslator = SqlTranslator;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqlTranslator = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const assert_1 = tslib_1.__importDefault(require("assert"));
|
|
6
|
+
const sqlstring_1 = tslib_1.__importDefault(require("sqlstring"));
|
|
7
|
+
const lodash_1 = require("lodash");
|
|
8
|
+
const types_1 = require("oak-domain/lib/types");
|
|
9
|
+
const relation_1 = require("oak-domain/lib/store/relation");
|
|
10
|
+
const filter_1 = require("oak-domain/lib/store/filter");
|
|
11
|
+
;
|
|
12
|
+
;
|
|
13
|
+
class SqlTranslator {
|
|
14
|
+
schema;
|
|
15
|
+
constructor(schema) {
|
|
16
|
+
this.schema = this.makeFullSchema(schema);
|
|
17
|
+
}
|
|
18
|
+
makeFullSchema(schema2) {
|
|
19
|
+
const schema = (0, lodash_1.cloneDeep)(schema2);
|
|
20
|
+
for (const entity in schema) {
|
|
21
|
+
const { attributes, indexes } = schema[entity];
|
|
22
|
+
// 增加默认的属性
|
|
23
|
+
(0, lodash_1.assign)(attributes, {
|
|
24
|
+
id: {
|
|
25
|
+
type: 'char',
|
|
26
|
+
params: {
|
|
27
|
+
length: 36,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
[types_1.SeqAttribute]: {
|
|
31
|
+
type: 'sequence',
|
|
32
|
+
sequenceStart: 10000,
|
|
33
|
+
},
|
|
34
|
+
[types_1.CreateAtAttribute]: {
|
|
35
|
+
type: 'datetime',
|
|
36
|
+
notNull: true,
|
|
37
|
+
},
|
|
38
|
+
[types_1.UpdateAtAttribute]: {
|
|
39
|
+
type: 'datetime',
|
|
40
|
+
notNull: true,
|
|
41
|
+
},
|
|
42
|
+
[types_1.DeleteAtAttribute]: {
|
|
43
|
+
type: 'datetime',
|
|
44
|
+
},
|
|
45
|
+
[types_1.TriggerDataAttribute]: {
|
|
46
|
+
type: 'object',
|
|
47
|
+
},
|
|
48
|
+
[types_1.TriggerUuidAttribute]: {
|
|
49
|
+
type: 'char',
|
|
50
|
+
params: {
|
|
51
|
+
length: 36,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
// 增加默认的索引
|
|
56
|
+
const intrinsticIndexes = [
|
|
57
|
+
{
|
|
58
|
+
name: `${entity}_create_at_auto_create`,
|
|
59
|
+
attributes: [{
|
|
60
|
+
name: types_1.CreateAtAttribute,
|
|
61
|
+
}, {
|
|
62
|
+
name: types_1.DeleteAtAttribute,
|
|
63
|
+
}]
|
|
64
|
+
}, {
|
|
65
|
+
name: `${entity}_update_at_auto_create`,
|
|
66
|
+
attributes: [{
|
|
67
|
+
name: types_1.UpdateAtAttribute,
|
|
68
|
+
}, {
|
|
69
|
+
name: types_1.DeleteAtAttribute,
|
|
70
|
+
}],
|
|
71
|
+
}, {
|
|
72
|
+
name: `${entity}_trigger_uuid`,
|
|
73
|
+
attributes: [{
|
|
74
|
+
name: types_1.TriggerUuidAttribute,
|
|
75
|
+
}]
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
// 增加外键等相关属性上的索引
|
|
79
|
+
for (const attr in attributes) {
|
|
80
|
+
if (attributes[attr].type === 'ref') {
|
|
81
|
+
if (!(indexes?.find(ele => ele.attributes[0].name === attr))) {
|
|
82
|
+
intrinsticIndexes.push({
|
|
83
|
+
name: `${entity}_fk_${attr}_auto_create`,
|
|
84
|
+
attributes: [{
|
|
85
|
+
name: attr,
|
|
86
|
+
}, {
|
|
87
|
+
name: '$$deleteAt$$',
|
|
88
|
+
}]
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (attr === 'entity' && attributes[attr].type === 'varchar') {
|
|
93
|
+
const entityIdDef = attributes.entityId;
|
|
94
|
+
if (entityIdDef?.type === 'varchar') {
|
|
95
|
+
if (!(indexes?.find(ele => ele.attributes[0].name === 'entity' && ele.attributes[1]?.name === 'entityId'))) {
|
|
96
|
+
intrinsticIndexes.push({
|
|
97
|
+
name: `${entity}_fk_entity_entityId_auto_create`,
|
|
98
|
+
attributes: [{
|
|
99
|
+
name: 'entity',
|
|
100
|
+
}, {
|
|
101
|
+
name: 'entityId',
|
|
102
|
+
}, {
|
|
103
|
+
name: '$$deleteAt$$',
|
|
104
|
+
}]
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (attr.endsWith('State') && attributes[attr].type === 'varchar') {
|
|
110
|
+
if (!(indexes?.find(ele => ele.attributes[0].name === attr))) {
|
|
111
|
+
intrinsticIndexes.push({
|
|
112
|
+
name: `${entity}_${attr}_auto_create`,
|
|
113
|
+
attributes: [{
|
|
114
|
+
name: attr,
|
|
115
|
+
}, {
|
|
116
|
+
name: '$$deleteAt$$',
|
|
117
|
+
}]
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (attr === 'expired' && attributes[attr].type === 'boolean') {
|
|
122
|
+
const expiresAtDef = attributes.expiresAt;
|
|
123
|
+
if (expiresAtDef?.type === 'datetime') {
|
|
124
|
+
if (!(indexes?.find(ele => ele.attributes[0].name === 'expired' && ele.attributes[1]?.name === 'expiresAt'))) {
|
|
125
|
+
intrinsticIndexes.push({
|
|
126
|
+
name: `${entity}_expires_expiredAt_auto_create`,
|
|
127
|
+
attributes: [{
|
|
128
|
+
name: 'expired',
|
|
129
|
+
}, {
|
|
130
|
+
name: 'expiresAt',
|
|
131
|
+
}, {
|
|
132
|
+
name: '$$deleteAt$$',
|
|
133
|
+
}]
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (indexes) {
|
|
140
|
+
indexes.push(...intrinsticIndexes);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
(0, lodash_1.assign)(schema[entity], {
|
|
144
|
+
indexes: intrinsticIndexes,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return schema;
|
|
149
|
+
}
|
|
150
|
+
getStorageName(entity) {
|
|
151
|
+
const { storageName } = this.schema[entity];
|
|
152
|
+
return (storageName || entity);
|
|
153
|
+
}
|
|
154
|
+
translateInsert(entity, data) {
|
|
155
|
+
const { schema } = this;
|
|
156
|
+
const { attributes, storageName = entity } = schema[entity];
|
|
157
|
+
let sql = `insert into \`${storageName}\`(`;
|
|
158
|
+
/**
|
|
159
|
+
* 这里的attrs要用所有行的union集合
|
|
160
|
+
*/
|
|
161
|
+
const dataFull = data.reduce((prev, cur) => Object.assign({}, cur, prev), {});
|
|
162
|
+
const attrs = Object.keys(dataFull).filter(ele => attributes.hasOwnProperty(ele));
|
|
163
|
+
attrs.forEach((attr, idx) => {
|
|
164
|
+
sql += ` \`${attr}\``;
|
|
165
|
+
if (idx < attrs.length - 1) {
|
|
166
|
+
sql += ',';
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
sql += ') values ';
|
|
170
|
+
data.forEach((d, dataIndex) => {
|
|
171
|
+
sql += '(';
|
|
172
|
+
attrs.forEach((attr, attrIdx) => {
|
|
173
|
+
const attrDef = attributes[attr];
|
|
174
|
+
const { type: dataType } = attrDef;
|
|
175
|
+
// undefined应该不要处理
|
|
176
|
+
if (d[attr] !== undefined) {
|
|
177
|
+
const value = this.translateAttrValue(dataType, d[attr]);
|
|
178
|
+
sql += value;
|
|
179
|
+
if (attrIdx < attrs.length - 1) {
|
|
180
|
+
sql += ',';
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
if (dataIndex < data.length - 1) {
|
|
185
|
+
sql += '),';
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
sql += ')';
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
return sql;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* analyze the join relations in projection/query/sort
|
|
195
|
+
* 所有的层次关系都当成left join处理,如果有内表为空的情况,请手动处理
|
|
196
|
+
* {
|
|
197
|
+
* b: {
|
|
198
|
+
* name: {
|
|
199
|
+
* $exists: false,
|
|
200
|
+
* }
|
|
201
|
+
* }
|
|
202
|
+
* }
|
|
203
|
+
* 这样的query会把内表为空的行也返回
|
|
204
|
+
* @param param0
|
|
205
|
+
*/
|
|
206
|
+
analyzeJoin(entity, { projection, filter, sorter, aggregation }, initialNumber) {
|
|
207
|
+
const { schema } = this;
|
|
208
|
+
let number = initialNumber || 1;
|
|
209
|
+
const projectionRefAlias = {};
|
|
210
|
+
const filterRefAlias = {};
|
|
211
|
+
const alias = `${entity}_${number++}`;
|
|
212
|
+
let from = ` \`${this.getStorageName(entity)}\` \`${alias}\` `;
|
|
213
|
+
const aliasDict = {
|
|
214
|
+
'./': alias,
|
|
215
|
+
};
|
|
216
|
+
const analyzeFilterNode = ({ node, path, entityName, alias }) => {
|
|
217
|
+
Object.keys(node).forEach((op) => {
|
|
218
|
+
if (['$and', '$or'].includes(op)) {
|
|
219
|
+
node[op].forEach((subNode) => analyzeFilterNode({
|
|
220
|
+
node: subNode,
|
|
221
|
+
path,
|
|
222
|
+
entityName,
|
|
223
|
+
alias,
|
|
224
|
+
}));
|
|
225
|
+
}
|
|
226
|
+
else if (['$not'].includes(op)) {
|
|
227
|
+
analyzeFilterNode({
|
|
228
|
+
node: node[op],
|
|
229
|
+
path,
|
|
230
|
+
entityName,
|
|
231
|
+
alias,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
else if (['$text'].includes(op)) {
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
const rel = (0, relation_1.judgeRelation)(this.schema, entityName, op);
|
|
238
|
+
if (typeof rel === 'string') {
|
|
239
|
+
let alias2;
|
|
240
|
+
const pathAttr = `${path}${op}/`;
|
|
241
|
+
if (!aliasDict.hasOwnProperty(pathAttr)) {
|
|
242
|
+
alias2 = `${rel}_${number++}`;
|
|
243
|
+
(0, lodash_1.assign)(aliasDict, {
|
|
244
|
+
[pathAttr]: alias2,
|
|
245
|
+
});
|
|
246
|
+
from += ` left join \`${this.getStorageName(rel)}\` \`${alias2}\` on \`${alias}\`.\`${op}Id\` = \`${alias2}\`.\`id\``;
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
alias2 = aliasDict[pathAttr];
|
|
250
|
+
}
|
|
251
|
+
analyzeFilterNode({
|
|
252
|
+
node: node[op],
|
|
253
|
+
path: pathAttr,
|
|
254
|
+
entityName: rel,
|
|
255
|
+
alias: alias2,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
else if (rel === 2) {
|
|
259
|
+
let alias2;
|
|
260
|
+
const pathAttr = `${path}${op}/`;
|
|
261
|
+
if (!aliasDict.hasOwnProperty(pathAttr)) {
|
|
262
|
+
alias2 = `${op}_${number++}`;
|
|
263
|
+
(0, lodash_1.assign)(aliasDict, {
|
|
264
|
+
[pathAttr]: alias2,
|
|
265
|
+
});
|
|
266
|
+
from += ` left join \`${this.getStorageName(op)}\` \`${alias2}\` on \`${alias}\`.\`entityId\` = \`${alias2}\`.\`id\` and \`${alias}\`.\`entity\` = '${op}'`;
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
alias2 = aliasDict[pathAttr];
|
|
270
|
+
}
|
|
271
|
+
analyzeFilterNode({
|
|
272
|
+
node: node[op],
|
|
273
|
+
path: pathAttr,
|
|
274
|
+
entityName: op,
|
|
275
|
+
alias: alias2,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
// 不支持一对多
|
|
280
|
+
// assert(rel === 0 || rel === 1);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
if (node['#id']) {
|
|
285
|
+
(0, assert_1.default)(!filterRefAlias[node['#id']]);
|
|
286
|
+
(0, lodash_1.assign)(filterRefAlias, {
|
|
287
|
+
[node['#id']]: [alias, entityName],
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
if (filter) {
|
|
292
|
+
analyzeFilterNode({
|
|
293
|
+
node: filter,
|
|
294
|
+
path: './',
|
|
295
|
+
entityName: entity,
|
|
296
|
+
alias,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
const analyzeSortNode = ({ node, path, entityName, alias }) => {
|
|
300
|
+
const attr = (0, lodash_1.keys)(node)[0];
|
|
301
|
+
const rel = (0, relation_1.judgeRelation)(this.schema, entityName, attr);
|
|
302
|
+
if (typeof rel === 'string') {
|
|
303
|
+
const pathAttr = `${path}${attr}/`;
|
|
304
|
+
let alias2;
|
|
305
|
+
if (!aliasDict.hasOwnProperty(pathAttr)) {
|
|
306
|
+
alias2 = `${rel}_${number++}`;
|
|
307
|
+
(0, lodash_1.assign)(aliasDict, {
|
|
308
|
+
[pathAttr]: alias2,
|
|
309
|
+
});
|
|
310
|
+
from += ` left join \`${this.getStorageName(rel)}\` \`${alias2}\` on \`${alias}\`.\`${attr}Id\` = \`${alias2}\`.\`id\``;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
alias2 = aliasDict[pathAttr];
|
|
314
|
+
}
|
|
315
|
+
analyzeSortNode({
|
|
316
|
+
node: node[attr],
|
|
317
|
+
path: pathAttr,
|
|
318
|
+
entityName: rel,
|
|
319
|
+
alias: alias2,
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
else if (rel === 2) {
|
|
323
|
+
const pathAttr = `${path}${attr}/`;
|
|
324
|
+
let alias2;
|
|
325
|
+
if (!aliasDict.hasOwnProperty(pathAttr)) {
|
|
326
|
+
alias2 = `${attr}_${number++}`;
|
|
327
|
+
(0, lodash_1.assign)(aliasDict, {
|
|
328
|
+
[pathAttr]: alias2,
|
|
329
|
+
});
|
|
330
|
+
from += ` left join \`${this.getStorageName(attr)}\` \`${alias2}\` on \`${alias}\`.\`entityId\` = \`${alias2}\`.\`id\` and \`${alias}\`.\`entity\` = '${attr}'`;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
alias2 = aliasDict[pathAttr];
|
|
334
|
+
}
|
|
335
|
+
analyzeSortNode({
|
|
336
|
+
node: node[attr],
|
|
337
|
+
path: pathAttr,
|
|
338
|
+
entityName: attr,
|
|
339
|
+
alias: alias2,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
(0, assert_1.default)(rel === 0 || rel === 1);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
if (sorter) {
|
|
347
|
+
sorter.forEach((sortNode) => {
|
|
348
|
+
analyzeSortNode({
|
|
349
|
+
node: sortNode.$attr,
|
|
350
|
+
path: './',
|
|
351
|
+
entityName: entity,
|
|
352
|
+
alias,
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
const analyzeProjectionNode = ({ node, path, entityName, alias }) => {
|
|
357
|
+
const { attributes } = schema[entityName];
|
|
358
|
+
Object.keys(node).forEach((attr) => {
|
|
359
|
+
const rel = (0, relation_1.judgeRelation)(this.schema, entityName, attr);
|
|
360
|
+
if (typeof rel === 'string') {
|
|
361
|
+
const pathAttr = `${path}${attr}/`;
|
|
362
|
+
let alias2;
|
|
363
|
+
if (!aliasDict.hasOwnProperty(pathAttr)) {
|
|
364
|
+
alias2 = `${rel}_${number++}`;
|
|
365
|
+
(0, lodash_1.assign)(aliasDict, {
|
|
366
|
+
[pathAttr]: alias2,
|
|
367
|
+
});
|
|
368
|
+
from += ` left join \`${this.getStorageName(rel)}\` \`${alias2}\` on \`${alias}\`.\`${attr}Id\` = \`${alias2}\`.\`id\``;
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
alias2 = aliasDict[pathAttr];
|
|
372
|
+
}
|
|
373
|
+
analyzeProjectionNode({
|
|
374
|
+
node: node[attr],
|
|
375
|
+
path: pathAttr,
|
|
376
|
+
entityName: rel,
|
|
377
|
+
alias: alias2,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
else if (rel === 2) {
|
|
381
|
+
const pathAttr = `${path}${attr}/`;
|
|
382
|
+
let alias2;
|
|
383
|
+
if (!aliasDict.hasOwnProperty(pathAttr)) {
|
|
384
|
+
alias2 = `${attr}_${number++}`;
|
|
385
|
+
(0, lodash_1.assign)(aliasDict, {
|
|
386
|
+
[pathAttr]: alias2,
|
|
387
|
+
});
|
|
388
|
+
from += ` left join \`${this.getStorageName(attr)}\` \`${alias2}\` on \`${alias}\`.\`entityId\` = \`${alias2}\`.\`id\` and \`${alias}\`.\`entity\` = '${attr}'`;
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
alias2 = aliasDict[pathAttr];
|
|
392
|
+
}
|
|
393
|
+
analyzeProjectionNode({
|
|
394
|
+
node: node[attr],
|
|
395
|
+
path: pathAttr,
|
|
396
|
+
entityName: attr,
|
|
397
|
+
alias: alias2,
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
if (node['#id']) {
|
|
402
|
+
(0, assert_1.default)(!projectionRefAlias[node['#id']], `projection上有重复的#id定义「${node['#id']}」`);
|
|
403
|
+
(0, lodash_1.assign)(projectionRefAlias, {
|
|
404
|
+
[node['#id']]: [alias, entityName],
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
if (projection) {
|
|
409
|
+
analyzeProjectionNode({ node: projection, path: './', entityName: entity, alias });
|
|
410
|
+
}
|
|
411
|
+
else if (aggregation) {
|
|
412
|
+
for (const k in aggregation) {
|
|
413
|
+
analyzeProjectionNode({
|
|
414
|
+
node: aggregation[k],
|
|
415
|
+
path: './',
|
|
416
|
+
entityName: entity,
|
|
417
|
+
alias,
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return {
|
|
422
|
+
aliasDict,
|
|
423
|
+
from,
|
|
424
|
+
projectionRefAlias,
|
|
425
|
+
filterRefAlias,
|
|
426
|
+
currentNumber: number,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
translateComparison(attr, value, type) {
|
|
430
|
+
const SQL_OP = {
|
|
431
|
+
$gt: '>',
|
|
432
|
+
$lt: '<',
|
|
433
|
+
$gte: '>=',
|
|
434
|
+
$lte: '<=',
|
|
435
|
+
$eq: '=',
|
|
436
|
+
$ne: '<>',
|
|
437
|
+
};
|
|
438
|
+
if (Object.keys(SQL_OP).includes(attr)) {
|
|
439
|
+
if (type) {
|
|
440
|
+
return ` ${SQL_OP[attr]} ${this.translateAttrValue(type, value)}`;
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
return ` ${SQL_OP[attr]} ${value}`;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
switch (attr) {
|
|
447
|
+
case '$startsWith': {
|
|
448
|
+
return ` like '${value}%'`;
|
|
449
|
+
}
|
|
450
|
+
case '$endsWith': {
|
|
451
|
+
return ` like '%${value}'`;
|
|
452
|
+
}
|
|
453
|
+
case '$includes': {
|
|
454
|
+
return ` like '%${value}%'`;
|
|
455
|
+
}
|
|
456
|
+
default: {
|
|
457
|
+
throw new Error(`unrecoganized comparison operator ${attr}`);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
translateEvaluation(attr, value, entity, alias, type, initialNumber, refAlias) {
|
|
462
|
+
switch (attr) {
|
|
463
|
+
case '$in':
|
|
464
|
+
case '$nin': {
|
|
465
|
+
const IN_OP = {
|
|
466
|
+
$in: 'in',
|
|
467
|
+
$nin: 'not in',
|
|
468
|
+
};
|
|
469
|
+
if (value instanceof Array) {
|
|
470
|
+
return {
|
|
471
|
+
stmt: this.translatePredicate(attr, value, type),
|
|
472
|
+
currentNumber: initialNumber,
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
(0, assert_1.default)(false, '子查询已经改写为一对多的形式');
|
|
477
|
+
// sub query
|
|
478
|
+
/* const { stmt: subQueryStmt, currentNumber } = this.translateSelectInner(value.entity, value, initialNumber, refAlias, undefined);
|
|
479
|
+
return {
|
|
480
|
+
stmt: ` ${IN_OP[attr]}(${subQueryStmt})`,
|
|
481
|
+
currentNumber,
|
|
482
|
+
}; */
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
default: {
|
|
486
|
+
throw new Error(`${attr} is not evaluation predicate`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
translatePredicate(predicate, value, type) {
|
|
491
|
+
if (['$gt', '$gte', '$lt', '$lte', '$eq', '$ne', '$startsWith', '$endsWith', '$includes'].includes(predicate)) {
|
|
492
|
+
return this.translateComparison(predicate, value, type);
|
|
493
|
+
}
|
|
494
|
+
else if (['$in', '$nin'].includes(predicate)) {
|
|
495
|
+
(0, assert_1.default)(value instanceof Array);
|
|
496
|
+
const IN_OP = {
|
|
497
|
+
$in: 'in',
|
|
498
|
+
$nin: 'not in',
|
|
499
|
+
};
|
|
500
|
+
const values = value.map((v) => {
|
|
501
|
+
if (type && ['varchar', 'char', 'text', 'nvarchar', 'ref', 'enum'].includes(type) || typeof v === 'string') {
|
|
502
|
+
return `'${v}'`;
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
return `${v}`;
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
if (values.length > 0) {
|
|
509
|
+
return ` ${IN_OP[predicate]}(${values.join(',')})`;
|
|
510
|
+
}
|
|
511
|
+
if (predicate === '$in') {
|
|
512
|
+
return ' in (null)';
|
|
513
|
+
}
|
|
514
|
+
return ' is not null';
|
|
515
|
+
}
|
|
516
|
+
else if (predicate === '$between') {
|
|
517
|
+
const values = value.map((v) => {
|
|
518
|
+
if (type && ['varchar', 'char', 'text', 'nvarchar', 'ref', 'enum'].includes(type) || typeof v === 'string') {
|
|
519
|
+
return `'${v}'`;
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
return `${v}`;
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
// between是所有数据库都支持的语法吗?
|
|
526
|
+
return ` between ${values[0]} and ${values[1]}`;
|
|
527
|
+
}
|
|
528
|
+
else if (predicate === '$mod') {
|
|
529
|
+
// %是所有数据库都支持的语法吗?
|
|
530
|
+
return ` % ${value[0]} = ${value[1]}`;
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
(0, assert_1.default)(predicate === '$exists');
|
|
534
|
+
if (value) {
|
|
535
|
+
return ' is not null';
|
|
536
|
+
}
|
|
537
|
+
return ' is null';
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
translateFilter(entity, filter, aliasDict, filterRefAlias, initialNumber, option) {
|
|
541
|
+
const { schema } = this;
|
|
542
|
+
let currentNumber = initialNumber;
|
|
543
|
+
const translateInner = (entity2, path, filter2, type) => {
|
|
544
|
+
const alias = aliasDict[path];
|
|
545
|
+
const { attributes } = schema[entity2];
|
|
546
|
+
let whereText = type ? '' : this.getDefaultSelectFilter(alias, option);
|
|
547
|
+
if (filter2) {
|
|
548
|
+
const attrs = Object.keys(filter2).filter(ele => !ele.startsWith('#'));
|
|
549
|
+
attrs.forEach((attr) => {
|
|
550
|
+
if (whereText) {
|
|
551
|
+
whereText += ' and ';
|
|
552
|
+
}
|
|
553
|
+
if (['$and', '$or', '$xor', '$not'].includes(attr)) {
|
|
554
|
+
whereText += '(';
|
|
555
|
+
switch (attr) {
|
|
556
|
+
case '$and':
|
|
557
|
+
case '$or':
|
|
558
|
+
case '$xor': {
|
|
559
|
+
const logicQueries = filter2[attr];
|
|
560
|
+
logicQueries.forEach((logicQuery, index) => {
|
|
561
|
+
const sql = translateInner(entity2, path, logicQuery, 'ref'); // 只要传个值就行了,应该无所谓
|
|
562
|
+
if (sql) {
|
|
563
|
+
whereText += ` (${sql})`;
|
|
564
|
+
if (index < logicQueries.length - 1) {
|
|
565
|
+
whereText += ` ${attr.slice(1)}`;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
break;
|
|
570
|
+
}
|
|
571
|
+
default: {
|
|
572
|
+
(0, assert_1.default)(attr === '$not');
|
|
573
|
+
const logicQuery = filter2[attr];
|
|
574
|
+
const sql = translateInner(entity2, path, logicQuery, 'ref'); // 只要传个值就行了,应该无所谓
|
|
575
|
+
if (sql) {
|
|
576
|
+
whereText += ` not (${sql})`;
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
whereText += ')';
|
|
582
|
+
}
|
|
583
|
+
else if (attr === '$text') {
|
|
584
|
+
whereText += `(${this.translateFullTextSearch(filter2[attr], entity2, alias)})`;
|
|
585
|
+
}
|
|
586
|
+
else if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
|
|
587
|
+
// expression
|
|
588
|
+
whereText += ` (${this.translateExpression(entity2, alias, filter2[attr], filterRefAlias)})`;
|
|
589
|
+
}
|
|
590
|
+
else {
|
|
591
|
+
const rel = (0, relation_1.judgeRelation)(this.schema, entity2, attr);
|
|
592
|
+
if (rel === 2) {
|
|
593
|
+
whereText += ` (${translateInner(attr, `${path}${attr}/`, filter2[attr])})`;
|
|
594
|
+
}
|
|
595
|
+
else if (typeof rel === 'string') {
|
|
596
|
+
whereText += ` (${translateInner(rel, `${path}${attr}/`, filter2[attr])})`;
|
|
597
|
+
}
|
|
598
|
+
else if (rel instanceof Array) {
|
|
599
|
+
const [subEntity, foreignKey] = rel;
|
|
600
|
+
const predicate = (filter2[attr]['#sqp'] || 'in');
|
|
601
|
+
/**
|
|
602
|
+
*
|
|
603
|
+
* in代表外键连接后至少有一行数据
|
|
604
|
+
* not in代表外键连接后一行也不能有
|
|
605
|
+
* all代表反连接条件的一行也不能有(符合的是否至少要有一行?直觉上没这个限制)
|
|
606
|
+
* not all 代表反连接条件的至少有一行
|
|
607
|
+
*
|
|
608
|
+
* 目前将这种子查询翻译成了exists查询,当外表很大而子查询结果集很小时可能有性能问题,取决于MySQL执行器的能力
|
|
609
|
+
* by Xc 20230726
|
|
610
|
+
*/
|
|
611
|
+
const refAlia = Object.keys(filterRefAlias).find(ele => filterRefAlias[ele][0] === alias);
|
|
612
|
+
const refAlia2 = refAlia || alias; // alias一定是唯一的,可以用来作为node id
|
|
613
|
+
if (!refAlia) {
|
|
614
|
+
(0, assert_1.default)(!filterRefAlias[refAlia2]);
|
|
615
|
+
Object.assign(filterRefAlias, {
|
|
616
|
+
[refAlia2]: [alias, entity2],
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
const fk = foreignKey || 'entityId';
|
|
620
|
+
const joinFilter = {
|
|
621
|
+
$expr12: {
|
|
622
|
+
$eq: [
|
|
623
|
+
{
|
|
624
|
+
'#attr': fk,
|
|
625
|
+
},
|
|
626
|
+
{
|
|
627
|
+
'#refId': refAlia2,
|
|
628
|
+
'#refAttr': 'id',
|
|
629
|
+
}
|
|
630
|
+
],
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
if (!foreignKey) {
|
|
634
|
+
Object.assign(joinFilter, {
|
|
635
|
+
entity: entity2,
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
const conditionalPredicate = ['all', 'not all'].includes(predicate) ? {
|
|
639
|
+
$not: filter2[attr],
|
|
640
|
+
} : filter2[attr];
|
|
641
|
+
const { stmt, currentNumber: ct2 } = this.translateSelectInner(subEntity, {
|
|
642
|
+
data: {
|
|
643
|
+
id: 1,
|
|
644
|
+
},
|
|
645
|
+
filter: (0, filter_1.combineFilters)(subEntity, this.schema, [joinFilter, conditionalPredicate]),
|
|
646
|
+
indexFrom: 0,
|
|
647
|
+
count: 1,
|
|
648
|
+
}, currentNumber, filterRefAlias, option);
|
|
649
|
+
currentNumber = ct2;
|
|
650
|
+
const PREDICATE_DICT = {
|
|
651
|
+
'in': 'exists',
|
|
652
|
+
'not in': 'not exists',
|
|
653
|
+
'all': 'not exists',
|
|
654
|
+
'not all': 'exists',
|
|
655
|
+
};
|
|
656
|
+
whereText += ` ${PREDICATE_DICT[predicate]} (${stmt})`;
|
|
657
|
+
}
|
|
658
|
+
else {
|
|
659
|
+
(0, assert_1.default)(attributes.hasOwnProperty(attr), `非法的属性${attr}`);
|
|
660
|
+
const { type: type2 } = attributes[attr];
|
|
661
|
+
// assert (type2 !== 'ref');
|
|
662
|
+
if (typeof filter2[attr] === 'object') {
|
|
663
|
+
if (['object', 'array'].includes(type2)) {
|
|
664
|
+
// 对object数据的深层次查询,这里调用数据库所支持的属性对象级查询,如mysql中的json查询
|
|
665
|
+
whereText += `(${this.translateObjectPredicate(filter2[attr], alias, attr)})`;
|
|
666
|
+
}
|
|
667
|
+
else {
|
|
668
|
+
(0, assert_1.default)(Object.keys(filter2[attr]).length === 1);
|
|
669
|
+
const predicate = Object.keys(filter2[attr])[0];
|
|
670
|
+
(0, assert_1.default)(predicate.startsWith('$'));
|
|
671
|
+
// 对属性上的谓词处理
|
|
672
|
+
whereText += ` (\`${alias}\`.\`${attr}\` ${this.translatePredicate(predicate, filter2[attr][predicate], type2)})`;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
whereText += ` (\`${alias}\`.\`${attr}\` = ${this.translateAttrValue(type2, filter2[attr])})`;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
if (!whereText) {
|
|
683
|
+
whereText = 'true'; // 如果为空就赋一个永真条件,以便处理and
|
|
684
|
+
}
|
|
685
|
+
return whereText;
|
|
686
|
+
};
|
|
687
|
+
const where = translateInner(entity, './', filter);
|
|
688
|
+
return {
|
|
689
|
+
stmt: where,
|
|
690
|
+
currentNumber,
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
translateSorter(entity, sorter, aliasDict) {
|
|
694
|
+
const translateInner = (entity2, sortAttr, path) => {
|
|
695
|
+
(0, assert_1.default)(Object.keys(sortAttr).length === 1);
|
|
696
|
+
const attr = Object.keys(sortAttr)[0];
|
|
697
|
+
const alias = aliasDict[path];
|
|
698
|
+
if (attr.toLocaleLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
|
|
699
|
+
return this.translateExpression(entity2, alias, sortAttr[attr], {});
|
|
700
|
+
}
|
|
701
|
+
else if (sortAttr[attr] === 1) {
|
|
702
|
+
return `\`${alias}\`.\`${attr}\``;
|
|
703
|
+
}
|
|
704
|
+
else {
|
|
705
|
+
const rel = (0, relation_1.judgeRelation)(this.schema, entity2, attr);
|
|
706
|
+
if (typeof rel === 'string') {
|
|
707
|
+
return translateInner(rel, sortAttr[attr], `${path}${attr}/`);
|
|
708
|
+
}
|
|
709
|
+
else {
|
|
710
|
+
(0, assert_1.default)(rel === 2);
|
|
711
|
+
return translateInner(attr, sortAttr[attr], `${path}${attr}/`);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
let sortText = '';
|
|
716
|
+
sorter.forEach((sortNode, index) => {
|
|
717
|
+
const { $attr, $direction } = sortNode;
|
|
718
|
+
sortText += translateInner(entity, $attr, './');
|
|
719
|
+
if ($direction) {
|
|
720
|
+
sortText += ` ${$direction}`;
|
|
721
|
+
}
|
|
722
|
+
if (index < sorter.length - 1) {
|
|
723
|
+
sortText += ',';
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
return sortText;
|
|
727
|
+
}
|
|
728
|
+
translateProjection(entity, projection, aliasDict, projectionRefAlias, commonPrefix, disableAs) {
|
|
729
|
+
const { schema } = this;
|
|
730
|
+
let as = '';
|
|
731
|
+
const translateInner = (entity2, projection2, path) => {
|
|
732
|
+
const alias = aliasDict[path];
|
|
733
|
+
const { attributes } = schema[entity2];
|
|
734
|
+
let projText = '';
|
|
735
|
+
let prefix = path.slice(2).replace(/\//g, '.');
|
|
736
|
+
const attrs = Object.keys(projection2).filter((attr) => {
|
|
737
|
+
if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
|
|
738
|
+
return true;
|
|
739
|
+
}
|
|
740
|
+
const rel = (0, relation_1.judgeRelation)(this.schema, entity2, attr);
|
|
741
|
+
return [1, 2].includes(rel) || typeof rel === 'string';
|
|
742
|
+
});
|
|
743
|
+
attrs.forEach((attr, idx) => {
|
|
744
|
+
const prefix2 = commonPrefix ? `${commonPrefix}.${prefix}` : prefix;
|
|
745
|
+
if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
|
|
746
|
+
const exprText = this.translateExpression(entity2, alias, projection2[attr], projectionRefAlias);
|
|
747
|
+
if (disableAs) {
|
|
748
|
+
projText += ` ${exprText}`;
|
|
749
|
+
}
|
|
750
|
+
else {
|
|
751
|
+
projText += ` ${exprText} as \`${prefix2}${attr}\``;
|
|
752
|
+
if (!as) {
|
|
753
|
+
as = `\`${prefix2}${attr}\``;
|
|
754
|
+
}
|
|
755
|
+
else {
|
|
756
|
+
as += `, \`${prefix2}${attr}\``;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
else {
|
|
761
|
+
const rel = (0, relation_1.judgeRelation)(this.schema, entity2, attr);
|
|
762
|
+
if (typeof rel === 'string') {
|
|
763
|
+
projText += translateInner(rel, projection2[attr], `${path}${attr}/`);
|
|
764
|
+
}
|
|
765
|
+
else if (rel === 2) {
|
|
766
|
+
projText += translateInner(attr, projection2[attr], `${path}${attr}/`);
|
|
767
|
+
}
|
|
768
|
+
else if (rel === 1) {
|
|
769
|
+
const { type } = attributes[attr];
|
|
770
|
+
if (projection2[attr] === 1) {
|
|
771
|
+
if (disableAs) {
|
|
772
|
+
projText += ` ${this.translateAttrProjection(type, alias, attr)}`;
|
|
773
|
+
}
|
|
774
|
+
else {
|
|
775
|
+
projText += ` ${this.translateAttrProjection(type, alias, attr)} as \`${prefix2}${attr}\``;
|
|
776
|
+
if (!as) {
|
|
777
|
+
as = `\`${prefix2}${attr}\``;
|
|
778
|
+
}
|
|
779
|
+
else {
|
|
780
|
+
as += `, \`${prefix2}${attr}\``;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
else if (typeof projection2[attr] === 'object') {
|
|
785
|
+
// 对JSON对象的取值
|
|
786
|
+
(0, assert_1.default)(!disableAs);
|
|
787
|
+
(0, assert_1.default)(['object', 'array'].includes(type));
|
|
788
|
+
projText += ` ${this.translateObjectProjection(projection2[attr], alias, attr, prefix2)}`;
|
|
789
|
+
}
|
|
790
|
+
else {
|
|
791
|
+
(0, assert_1.default)(typeof projection2 === 'string');
|
|
792
|
+
if (disableAs) {
|
|
793
|
+
projText += ` ${this.translateAttrProjection(type, alias, attr)}`;
|
|
794
|
+
}
|
|
795
|
+
else {
|
|
796
|
+
projText += ` ${this.translateAttrProjection(type, alias, attr)} as \`${prefix2}${projection2[attr]}\``;
|
|
797
|
+
if (!as) {
|
|
798
|
+
as = `\`${prefix2}${projection2[attr]}\``;
|
|
799
|
+
}
|
|
800
|
+
else {
|
|
801
|
+
as += `\`${prefix2}${projection2[attr]}\``;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
if (idx < attrs.length - 1) {
|
|
808
|
+
projText += ',';
|
|
809
|
+
}
|
|
810
|
+
});
|
|
811
|
+
return projText;
|
|
812
|
+
};
|
|
813
|
+
return {
|
|
814
|
+
projText: translateInner(entity, projection, './'),
|
|
815
|
+
as,
|
|
816
|
+
};
|
|
817
|
+
}
|
|
818
|
+
translateSelectInner(entity, selection, initialNumber, refAlias, option) {
|
|
819
|
+
const { data, filter, sorter, indexFrom, count, distinct } = selection;
|
|
820
|
+
const { from: fromText, aliasDict, projectionRefAlias, filterRefAlias, currentNumber } = this.analyzeJoin(entity, {
|
|
821
|
+
projection: data,
|
|
822
|
+
filter,
|
|
823
|
+
sorter,
|
|
824
|
+
}, initialNumber);
|
|
825
|
+
(0, assert_1.default)((0, lodash_1.intersection)((0, lodash_1.keys)(refAlias), (0, lodash_1.keys)(filterRefAlias)).length === 0, 'filter中的#node结点定义有重复');
|
|
826
|
+
(0, lodash_1.assign)(refAlias, filterRefAlias);
|
|
827
|
+
let { projText } = this.translateProjection(entity, data, aliasDict, projectionRefAlias);
|
|
828
|
+
if (distinct) {
|
|
829
|
+
projText = `distinct ${projText}`;
|
|
830
|
+
}
|
|
831
|
+
const { stmt: filterText, currentNumber: currentNumber2 } = this.translateFilter(entity, filter, aliasDict, refAlias, currentNumber, option);
|
|
832
|
+
const sorterText = sorter && this.translateSorter(entity, sorter, aliasDict);
|
|
833
|
+
return {
|
|
834
|
+
stmt: this.populateSelectStmt(projText, fromText, aliasDict, filterText, sorterText, undefined, indexFrom, count, option, selection),
|
|
835
|
+
currentNumber: currentNumber2,
|
|
836
|
+
filterStmt: filterText,
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
translateSelect(entity, selection, option) {
|
|
840
|
+
const { stmt } = this.translateSelectInner(entity, selection, 1, {}, option);
|
|
841
|
+
return stmt;
|
|
842
|
+
}
|
|
843
|
+
translateWhere(entity, selection, option) {
|
|
844
|
+
const { filterStmt } = this.translateSelectInner(entity, selection, 1, {}, option);
|
|
845
|
+
return filterStmt;
|
|
846
|
+
}
|
|
847
|
+
translateAggregate(entity, aggregation, option) {
|
|
848
|
+
const { data, filter, sorter, indexFrom, count, distinct } = aggregation;
|
|
849
|
+
const { from: fromText, aliasDict, projectionRefAlias, filterRefAlias, currentNumber } = this.analyzeJoin(entity, {
|
|
850
|
+
aggregation: data,
|
|
851
|
+
filter,
|
|
852
|
+
sorter,
|
|
853
|
+
}, 1);
|
|
854
|
+
let projText = '';
|
|
855
|
+
let groupByText = '';
|
|
856
|
+
for (const k in data) {
|
|
857
|
+
if (k === '#aggr') {
|
|
858
|
+
const { projText: projSubText, as } = this.translateProjection(entity, data[k], aliasDict, projectionRefAlias, '#data');
|
|
859
|
+
if (!projText) {
|
|
860
|
+
projText = projSubText;
|
|
861
|
+
}
|
|
862
|
+
else {
|
|
863
|
+
projText += `, ${projSubText}`;
|
|
864
|
+
}
|
|
865
|
+
groupByText = as;
|
|
866
|
+
}
|
|
867
|
+
else if (k.startsWith('#')) {
|
|
868
|
+
let { projText: projSubText } = this.translateProjection(entity, data[k], aliasDict, projectionRefAlias, undefined, true);
|
|
869
|
+
let projSubText2 = '';
|
|
870
|
+
if (k.startsWith('#max')) {
|
|
871
|
+
projSubText2 = `max(${projSubText}) as \`${k}\``;
|
|
872
|
+
}
|
|
873
|
+
else if (k.startsWith('#min')) {
|
|
874
|
+
projSubText2 = `min(${projSubText}) as \`${k}\``;
|
|
875
|
+
}
|
|
876
|
+
else if (k.startsWith('#count')) {
|
|
877
|
+
if (data.distinct) {
|
|
878
|
+
projSubText = `distinct ${projSubText}`;
|
|
879
|
+
}
|
|
880
|
+
projSubText2 = `count(${projSubText}) as \`${k}\``;
|
|
881
|
+
}
|
|
882
|
+
else if (k.startsWith('#sum')) {
|
|
883
|
+
if (data.distinct) {
|
|
884
|
+
projSubText = `distinct ${projSubText}`;
|
|
885
|
+
}
|
|
886
|
+
projSubText2 = `sum(${projSubText}) as \`${k}\``;
|
|
887
|
+
}
|
|
888
|
+
else {
|
|
889
|
+
if (data.distinct) {
|
|
890
|
+
projSubText = `distinct ${projSubText}`;
|
|
891
|
+
}
|
|
892
|
+
(0, assert_1.default)(k.startsWith('#avg'));
|
|
893
|
+
projSubText2 = `avg(${projSubText}) as \`${k}\``;
|
|
894
|
+
}
|
|
895
|
+
if (!projText) {
|
|
896
|
+
projText = projSubText2;
|
|
897
|
+
}
|
|
898
|
+
else {
|
|
899
|
+
projText += `, ${projSubText2}`;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
const { stmt: filterText } = this.translateFilter(entity, filter, aliasDict, {}, currentNumber, option);
|
|
904
|
+
const sorterText = sorter && this.translateSorter(entity, sorter, aliasDict);
|
|
905
|
+
return this.populateSelectStmt(projText, fromText, aliasDict, filterText, sorterText, groupByText, indexFrom, count, option, undefined, aggregation);
|
|
906
|
+
}
|
|
907
|
+
translateCount(entity, selection, option) {
|
|
908
|
+
const { filter, count } = selection;
|
|
909
|
+
const { from: fromText, aliasDict, filterRefAlias, currentNumber } = this.analyzeJoin(entity, {
|
|
910
|
+
filter,
|
|
911
|
+
});
|
|
912
|
+
const projText = 'count(1) cnt';
|
|
913
|
+
const { stmt: filterText } = this.translateFilter(entity, filter, aliasDict, filterRefAlias, currentNumber, option);
|
|
914
|
+
if (count && count > 0) {
|
|
915
|
+
const subQuerySql = this.populateSelectStmt('1', fromText, aliasDict, filterText, undefined, undefined, 0, count, option, Object.assign({}, selection, { indexFrom: 0, count }));
|
|
916
|
+
return `select count(1) cnt from (${subQuerySql}) __tmp`;
|
|
917
|
+
}
|
|
918
|
+
return this.populateSelectStmt(projText, fromText, aliasDict, filterText, undefined, undefined, undefined, undefined, option, selection);
|
|
919
|
+
}
|
|
920
|
+
translateRemove(entity, operation, option) {
|
|
921
|
+
const { data, filter, sorter, indexFrom, count } = operation;
|
|
922
|
+
(0, assert_1.default)(!sorter, '当前remove不支持sorter行为');
|
|
923
|
+
const { aliasDict, filterRefAlias, from: fromText, currentNumber } = this.analyzeJoin(entity, { filter, sorter });
|
|
924
|
+
const alias = aliasDict['./'];
|
|
925
|
+
// 这里原来includeDeleted传的是true,不知道原因,但不合理
|
|
926
|
+
const { stmt: filterText } = this.translateFilter(entity, filter, aliasDict, filterRefAlias, currentNumber, { includedDeleted: option?.includedDeleted });
|
|
927
|
+
// const sorterText = sorter && sorter.length > 0 ? this.translateSorter(entity, sorter, aliasDict) : undefined;
|
|
928
|
+
const { attributes } = this.schema[entity];
|
|
929
|
+
let updateText = '';
|
|
930
|
+
if (option?.deletePhysically) {
|
|
931
|
+
(0, assert_1.default)((0, lodash_1.difference)(Object.keys(data), [types_1.UpdateAtAttribute, types_1.DeleteAtAttribute]).length === 0);
|
|
932
|
+
}
|
|
933
|
+
else {
|
|
934
|
+
for (const attr in data) {
|
|
935
|
+
if (updateText) {
|
|
936
|
+
updateText += ',';
|
|
937
|
+
}
|
|
938
|
+
// delete只支持对volatile trigger的metadata域赋值
|
|
939
|
+
(0, assert_1.default)([types_1.TriggerDataAttribute, types_1.TriggerUuidAttribute, types_1.DeleteAtAttribute, types_1.UpdateAtAttribute].includes(attr));
|
|
940
|
+
const value = this.translateAttrValue(attributes[attr].type, data[attr]);
|
|
941
|
+
updateText += `\`${alias}\`.\`${attr}\` = ${value}`;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
return this.populateRemoveStmt(updateText, fromText, aliasDict, filterText, /* sorterText */ undefined, indexFrom, count, option);
|
|
945
|
+
}
|
|
946
|
+
translateUpdate(entity, operation, option) {
|
|
947
|
+
const { attributes } = this.schema[entity];
|
|
948
|
+
const { filter, sorter, indexFrom, count, data } = operation;
|
|
949
|
+
(0, assert_1.default)(!sorter, '当前update不支持sorter行为');
|
|
950
|
+
const { aliasDict, filterRefAlias, from: fromText, currentNumber } = this.analyzeJoin(entity, { filter, sorter });
|
|
951
|
+
const alias = aliasDict['./'];
|
|
952
|
+
let updateText = '';
|
|
953
|
+
for (const attr in data) {
|
|
954
|
+
if (updateText) {
|
|
955
|
+
updateText += ',';
|
|
956
|
+
}
|
|
957
|
+
(0, assert_1.default)(attributes.hasOwnProperty(attr));
|
|
958
|
+
const value = this.translateAttrValue(attributes[attr].type, data[attr]);
|
|
959
|
+
updateText += `\`${alias}\`.\`${attr}\` = ${value}`;
|
|
960
|
+
}
|
|
961
|
+
const { stmt: filterText } = this.translateFilter(entity, filter, aliasDict, filterRefAlias, currentNumber, option);
|
|
962
|
+
// const sorterText = sorter && this.translateSorter(entity, sorter, aliasDict);
|
|
963
|
+
return this.populateUpdateStmt(updateText, fromText, aliasDict, filterText, /* sorterText */ undefined, indexFrom, count, option);
|
|
964
|
+
}
|
|
965
|
+
translateDestroyEntity(entity, truncate) {
|
|
966
|
+
const { schema } = this;
|
|
967
|
+
const { storageName = entity, view } = schema[entity];
|
|
968
|
+
let sql;
|
|
969
|
+
if (view) {
|
|
970
|
+
sql = `drop view if exists \`${storageName}\``;
|
|
971
|
+
}
|
|
972
|
+
else {
|
|
973
|
+
sql = truncate ? `truncate table \`${storageName}\`` : `drop table if exists \`${storageName}\``;
|
|
974
|
+
}
|
|
975
|
+
return sql;
|
|
976
|
+
}
|
|
977
|
+
escapeStringValue(value) {
|
|
978
|
+
const result = sqlstring_1.default.escape(value);
|
|
979
|
+
return result;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
exports.SqlTranslator = SqlTranslator;
|