oak-db 2.1.1 → 2.2.1
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/store.d.ts +4 -1
- package/lib/MySQL/store.js +30 -3
- package/lib/MySQL/translator.d.ts +3 -2
- package/lib/MySQL/translator.js +46 -11
- package/lib/sqlTranslator.d.ts +3 -2
- package/lib/sqlTranslator.js +117 -16
- package/package.json +3 -3
package/lib/MySQL/store.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EntityDict, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, TxnOption, StorageSchema, DeduceCreateMultipleOperation, SelectOption } from 'oak-domain/lib/types';
|
|
1
|
+
import { EntityDict, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, TxnOption, StorageSchema, DeduceCreateMultipleOperation, SelectOption, AggregationResult } from 'oak-domain/lib/types';
|
|
2
2
|
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
|
3
3
|
import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
|
|
4
4
|
import { MySQLConfiguration } from './types/Configuration';
|
|
@@ -7,11 +7,14 @@ import { MySqlTranslator, MySqlSelectOption, MysqlOperateOption } from './transl
|
|
|
7
7
|
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
|
|
8
8
|
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
|
|
9
9
|
export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends CascadeStore<ED> implements AsyncRowStore<ED, Cxt> {
|
|
10
|
+
protected aggregateSync<T extends keyof ED, OP extends SelectOption, Cxt extends SyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): AggregationResult<ED[T]['Schema']>;
|
|
10
11
|
protected selectAbjointRow<T extends keyof ED, OP extends SelectOption>(entity: T, selection: ED[T]['Selection'], context: SyncContext<ED>, option: OP): Partial<ED[T]['Schema']>[];
|
|
11
12
|
protected updateAbjointRow<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: SyncContext<ED>, option: OP): number;
|
|
12
13
|
connector: MySqlConnector;
|
|
13
14
|
translator: MySqlTranslator<ED>;
|
|
14
15
|
constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
|
|
16
|
+
protected aggregateAsync<T extends keyof ED, OP extends SelectOption, Cxt extends AsyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): Promise<AggregationResult<ED[T]['Schema']>>;
|
|
17
|
+
aggregate<T extends keyof ED, OP extends SelectOption>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): Promise<AggregationResult<ED[T]['Schema']>>;
|
|
15
18
|
protected supportManyToOneJoin(): boolean;
|
|
16
19
|
protected supportMultipleCreate(): boolean;
|
|
17
20
|
private formResult;
|
package/lib/MySQL/store.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MysqlStore = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
|
+
var selection_1 = require("oak-domain/lib/store/selection");
|
|
5
6
|
var CascadeStore_1 = require("oak-domain/lib/store/CascadeStore");
|
|
6
7
|
var connector_1 = require("./connector");
|
|
7
8
|
var translator_1 = require("./translator");
|
|
@@ -28,12 +29,33 @@ var MysqlStore = /** @class */ (function (_super) {
|
|
|
28
29
|
_this.translator = new translator_1.MySqlTranslator(storageSchema);
|
|
29
30
|
return _this;
|
|
30
31
|
}
|
|
32
|
+
MysqlStore.prototype.aggregateSync = function (entity, aggregation, context, option) {
|
|
33
|
+
throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
|
|
34
|
+
};
|
|
31
35
|
MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
|
|
32
36
|
throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
|
|
33
37
|
};
|
|
34
38
|
MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
|
|
35
39
|
throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
|
|
36
40
|
};
|
|
41
|
+
MysqlStore.prototype.aggregateAsync = function (entity, aggregation, context, option) {
|
|
42
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
43
|
+
var sql, result;
|
|
44
|
+
return tslib_1.__generator(this, function (_a) {
|
|
45
|
+
switch (_a.label) {
|
|
46
|
+
case 0:
|
|
47
|
+
sql = this.translator.translateAggregate(entity, aggregation, option);
|
|
48
|
+
return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
|
|
49
|
+
case 1:
|
|
50
|
+
result = _a.sent();
|
|
51
|
+
return [2 /*return*/, this.formResult(entity, result)];
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
MysqlStore.prototype.aggregate = function (entity, aggregation, context, option) {
|
|
57
|
+
return this.aggregateAsync(entity, aggregation, context, option);
|
|
58
|
+
};
|
|
37
59
|
MysqlStore.prototype.supportManyToOneJoin = function () {
|
|
38
60
|
return true;
|
|
39
61
|
};
|
|
@@ -54,7 +76,10 @@ var MysqlStore = /** @class */ (function (_super) {
|
|
|
54
76
|
r[attrHead] = {};
|
|
55
77
|
}
|
|
56
78
|
var rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
|
|
57
|
-
if (rel ===
|
|
79
|
+
if (rel === 0) {
|
|
80
|
+
resolveAttribute(entity2, r[attrHead], attrTail, value);
|
|
81
|
+
}
|
|
82
|
+
else if (rel === 2) {
|
|
58
83
|
resolveAttribute(attrHead, r[attrHead], attrTail, value);
|
|
59
84
|
}
|
|
60
85
|
else {
|
|
@@ -133,7 +158,7 @@ var MysqlStore = /** @class */ (function (_super) {
|
|
|
133
158
|
}
|
|
134
159
|
}
|
|
135
160
|
function removeNullObjects(r, e) {
|
|
136
|
-
|
|
161
|
+
// assert(r.id && typeof r.id === 'string', `对象${<string>e}取数据时发现id为非法值${r.id},rowId是${r.id}`)
|
|
137
162
|
for (var attr in r) {
|
|
138
163
|
var rel = (0, relation_1.judgeRelation)(schema, e, attr);
|
|
139
164
|
if (rel === 2) {
|
|
@@ -270,7 +295,9 @@ var MysqlStore = /** @class */ (function (_super) {
|
|
|
270
295
|
var result;
|
|
271
296
|
return tslib_1.__generator(this, function (_a) {
|
|
272
297
|
switch (_a.label) {
|
|
273
|
-
case 0:
|
|
298
|
+
case 0:
|
|
299
|
+
(0, selection_1.reinforceSelection)(this.storageSchema, entity, selection);
|
|
300
|
+
return [4 /*yield*/, this.cascadeSelectAsync(entity, selection, context, option)];
|
|
274
301
|
case 1:
|
|
275
302
|
result = _a.sent();
|
|
276
303
|
return [2 /*return*/, result];
|
|
@@ -97,8 +97,9 @@ export declare class MySqlTranslator<ED extends EntityDict> extends SqlTranslato
|
|
|
97
97
|
replace?: boolean;
|
|
98
98
|
}): string[];
|
|
99
99
|
private translateFnName;
|
|
100
|
-
|
|
101
|
-
protected
|
|
100
|
+
private translateAttrInExpression;
|
|
101
|
+
protected translateExpression<T extends keyof ED>(entity: T, alias: string, expression: RefOrExpression<keyof ED[T]["OpSchema"]>, refDict: Record<string, [string, keyof ED]>): string;
|
|
102
|
+
protected populateSelectStmt<T extends keyof ED>(projectionText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, groupByText?: string, indexFrom?: number, count?: number, option?: MySqlSelectOption): string;
|
|
102
103
|
protected populateUpdateStmt(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: MysqlOperateOption): string;
|
|
103
104
|
protected populateRemoveStmt(removeText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: MysqlOperateOption): string;
|
|
104
105
|
}
|
package/lib/MySQL/translator.js
CHANGED
|
@@ -357,21 +357,32 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
357
357
|
MySqlTranslator.prototype.translateFnName = function (fnName, argumentNumber) {
|
|
358
358
|
switch (fnName) {
|
|
359
359
|
case '$add': {
|
|
360
|
-
|
|
360
|
+
var result = '%s';
|
|
361
|
+
while (--argumentNumber > 0) {
|
|
362
|
+
result += ' + %s';
|
|
363
|
+
}
|
|
364
|
+
return result;
|
|
361
365
|
}
|
|
362
366
|
case '$subtract': {
|
|
367
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
363
368
|
return '%s - %s';
|
|
364
369
|
}
|
|
365
370
|
case '$multiply': {
|
|
366
|
-
|
|
371
|
+
var result = '%s';
|
|
372
|
+
while (--argumentNumber > 0) {
|
|
373
|
+
result += ' * %s';
|
|
374
|
+
}
|
|
375
|
+
return result;
|
|
367
376
|
}
|
|
368
377
|
case '$divide': {
|
|
378
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
369
379
|
return '%s / %s';
|
|
370
380
|
}
|
|
371
381
|
case '$abs': {
|
|
372
382
|
return 'ABS(%s)';
|
|
373
383
|
}
|
|
374
384
|
case '$round': {
|
|
385
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
375
386
|
return 'ROUND(%s, %s)';
|
|
376
387
|
}
|
|
377
388
|
case '$ceil': {
|
|
@@ -381,33 +392,42 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
381
392
|
return 'FLOOR(%s)';
|
|
382
393
|
}
|
|
383
394
|
case '$pow': {
|
|
395
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
384
396
|
return 'POW(%s, %s)';
|
|
385
397
|
}
|
|
386
398
|
case '$gt': {
|
|
399
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
387
400
|
return '%s > %s';
|
|
388
401
|
}
|
|
389
402
|
case '$gte': {
|
|
403
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
390
404
|
return '%s >= %s';
|
|
391
405
|
}
|
|
392
406
|
case '$lt': {
|
|
407
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
393
408
|
return '%s < %s';
|
|
394
409
|
}
|
|
395
410
|
case '$lte': {
|
|
396
411
|
return '%s <= %s';
|
|
397
412
|
}
|
|
398
413
|
case '$eq': {
|
|
414
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
399
415
|
return '%s = %s';
|
|
400
416
|
}
|
|
401
417
|
case '$ne': {
|
|
418
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
402
419
|
return '%s <> %s';
|
|
403
420
|
}
|
|
404
421
|
case '$startsWith': {
|
|
422
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
405
423
|
return '%s like CONCAT(%s, \'%\')';
|
|
406
424
|
}
|
|
407
425
|
case '$endsWith': {
|
|
426
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
408
427
|
return '%s like CONCAT(\'%\', %s)';
|
|
409
428
|
}
|
|
410
429
|
case '$includes': {
|
|
430
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
411
431
|
return '%s like CONCAT(\'%\', %s, \'%\')';
|
|
412
432
|
}
|
|
413
433
|
case '$true': {
|
|
@@ -464,12 +484,15 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
464
484
|
return 'DAYOFYEAR(%s)';
|
|
465
485
|
}
|
|
466
486
|
case '$dateDiff': {
|
|
487
|
+
(0, assert_1.default)(argumentNumber === 3);
|
|
467
488
|
return 'DATEDIFF(%s, %s, %s)';
|
|
468
489
|
}
|
|
469
490
|
case '$contains': {
|
|
491
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
470
492
|
return 'ST_CONTAINS(%s, %s)';
|
|
471
493
|
}
|
|
472
494
|
case '$distance': {
|
|
495
|
+
(0, assert_1.default)(argumentNumber === 2);
|
|
473
496
|
return 'ST_DISTANCE(%s, %s)';
|
|
474
497
|
}
|
|
475
498
|
default: {
|
|
@@ -477,15 +500,24 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
477
500
|
}
|
|
478
501
|
}
|
|
479
502
|
};
|
|
480
|
-
MySqlTranslator.prototype.
|
|
503
|
+
MySqlTranslator.prototype.translateAttrInExpression = function (entity, attr, exprText) {
|
|
504
|
+
var attributes = this.schema[entity].attributes;
|
|
505
|
+
var type = attributes[attr].type;
|
|
506
|
+
if (['date', 'time', 'datetime'].includes(type)) {
|
|
507
|
+
// 从unix时间戵转成date类型参加expr的运算
|
|
508
|
+
return "from_unixtime(".concat(exprText, " / 1000)");
|
|
509
|
+
}
|
|
510
|
+
return exprText;
|
|
511
|
+
};
|
|
512
|
+
MySqlTranslator.prototype.translateExpression = function (entity, alias, expression, refDict) {
|
|
481
513
|
var _this = this;
|
|
482
514
|
var translateConstant = function (constant) {
|
|
483
|
-
if (
|
|
484
|
-
return " ".concat(new Date(constant).valueOf());
|
|
485
|
-
}
|
|
486
|
-
else if (constant instanceof Date) {
|
|
515
|
+
if (constant instanceof Date) {
|
|
487
516
|
return " ".concat(constant.valueOf());
|
|
488
517
|
}
|
|
518
|
+
else if (typeof constant === 'string') {
|
|
519
|
+
return " '".concat(constant, "'");
|
|
520
|
+
}
|
|
489
521
|
else {
|
|
490
522
|
(0, assert_1.default)(typeof constant === 'number');
|
|
491
523
|
return " ".concat(constant);
|
|
@@ -496,14 +528,14 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
496
528
|
var result;
|
|
497
529
|
if (k.includes('#attr')) {
|
|
498
530
|
var attrText = "`".concat(alias, "`.`").concat((expr)['#attr'], "`");
|
|
499
|
-
result = attrText;
|
|
531
|
+
result = _this.translateAttrInExpression(entity, (expr)['#attr'], attrText);
|
|
500
532
|
}
|
|
501
533
|
else if (k.includes('#refId')) {
|
|
502
534
|
var refId = (expr)['#refId'];
|
|
503
535
|
var refAttr = (expr)['#refAttr'];
|
|
504
536
|
(0, assert_1.default)(refDict[refId]);
|
|
505
|
-
var attrText = "`".concat(refDict[refId], "`.`").concat(refAttr, "`");
|
|
506
|
-
result = attrText;
|
|
537
|
+
var attrText = "`".concat(refDict[refId][0], "`.`").concat(refAttr, "`");
|
|
538
|
+
result = _this.translateAttrInExpression(entity, (expr)['#attr'], attrText);
|
|
507
539
|
}
|
|
508
540
|
else {
|
|
509
541
|
(0, assert_1.default)(k.length === 1);
|
|
@@ -537,7 +569,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
537
569
|
};
|
|
538
570
|
return translateInner(expression);
|
|
539
571
|
};
|
|
540
|
-
MySqlTranslator.prototype.populateSelectStmt = function (projectionText, fromText,
|
|
572
|
+
MySqlTranslator.prototype.populateSelectStmt = function (projectionText, fromText, aliasDict, filterText, sorterText, groupByText, indexFrom, count, option) {
|
|
541
573
|
// todo hint of use index
|
|
542
574
|
var sql = "select ".concat(projectionText, " from ").concat(fromText);
|
|
543
575
|
if (filterText) {
|
|
@@ -546,6 +578,9 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
546
578
|
if (sorterText) {
|
|
547
579
|
sql += " order by ".concat(sorterText);
|
|
548
580
|
}
|
|
581
|
+
if (groupByText) {
|
|
582
|
+
sql += " group by ".concat(groupByText);
|
|
583
|
+
}
|
|
549
584
|
if (typeof indexFrom === 'number') {
|
|
550
585
|
(0, assert_1.default)(typeof count === 'number');
|
|
551
586
|
sql += " limit ".concat(indexFrom, ", ").concat(count);
|
package/lib/sqlTranslator.d.ts
CHANGED
|
@@ -15,10 +15,10 @@ export declare abstract class SqlTranslator<ED extends EntityDict> {
|
|
|
15
15
|
abstract translateCreateEntity<T extends keyof ED>(entity: T, option: {
|
|
16
16
|
replace?: boolean;
|
|
17
17
|
}): string[];
|
|
18
|
-
protected abstract populateSelectStmt<T extends keyof ED, OP extends SqlSelectOption>(projectionText: string, fromText: string,
|
|
18
|
+
protected abstract populateSelectStmt<T extends keyof ED, OP extends SqlSelectOption>(projectionText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, groupByText?: string, indexFrom?: number, count?: number, option?: OP, selection?: ED[T]['Selection'], aggregation?: ED[T]['Aggregation']): string;
|
|
19
19
|
protected abstract populateUpdateStmt<OP extends SqlOperateOption>(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: OP): string;
|
|
20
20
|
protected abstract populateRemoveStmt<OP extends SqlOperateOption>(removeText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: OP): string;
|
|
21
|
-
protected abstract translateExpression<T extends keyof ED>(alias: string, expression: RefOrExpression<keyof ED[T]['OpSchema']>, refDict: Record<string, string>): string;
|
|
21
|
+
protected abstract translateExpression<T extends keyof ED>(entity: T, alias: string, expression: RefOrExpression<keyof ED[T]['OpSchema']>, refDict: Record<string, [string, keyof ED]>): string;
|
|
22
22
|
private getStorageName;
|
|
23
23
|
translateInsert<T extends keyof ED>(entity: T, data: DeduceCreateOperationData<ED[T]['OpSchema']>[]): string;
|
|
24
24
|
/**
|
|
@@ -43,6 +43,7 @@ export declare abstract class SqlTranslator<ED extends EntityDict> {
|
|
|
43
43
|
private translateProjection;
|
|
44
44
|
private translateSelectInner;
|
|
45
45
|
translateSelect<T extends keyof ED, OP extends SqlSelectOption>(entity: T, selection: ED[T]['Selection'], option?: OP): string;
|
|
46
|
+
translateAggregate<T extends keyof ED, OP extends SqlSelectOption>(entity: T, aggregation: ED[T]['Aggregation'], option?: OP): string;
|
|
46
47
|
translateCount<T extends keyof ED, OP extends SqlSelectOption>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, option?: OP): string;
|
|
47
48
|
translateRemove<T extends keyof ED, OP extends SqlOperateOption>(entity: T, operation: ED[T]['Remove'], option?: OP): string;
|
|
48
49
|
translateUpdate<T extends keyof ED, OP extends SqlOperateOption>(entity: T, operation: ED[T]['Update'], option?: OP): string;
|
package/lib/sqlTranslator.js
CHANGED
|
@@ -163,7 +163,7 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
163
163
|
*/
|
|
164
164
|
SqlTranslator.prototype.analyzeJoin = function (entity, _a, initialNumber) {
|
|
165
165
|
var _this = this;
|
|
166
|
-
var projection = _a.projection, filter = _a.filter, sorter = _a.sorter,
|
|
166
|
+
var projection = _a.projection, filter = _a.filter, sorter = _a.sorter, aggregation = _a.aggregation;
|
|
167
167
|
var schema = this.schema;
|
|
168
168
|
var number = initialNumber || 1;
|
|
169
169
|
var projectionRefAlias = {};
|
|
@@ -195,6 +195,8 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
195
195
|
alias: alias,
|
|
196
196
|
});
|
|
197
197
|
}
|
|
198
|
+
else if (['$text'].includes(op)) {
|
|
199
|
+
}
|
|
198
200
|
else {
|
|
199
201
|
var rel = (0, relation_1.judgeRelation)(_this.schema, entityName, op);
|
|
200
202
|
if (typeof rel === 'string') {
|
|
@@ -246,7 +248,7 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
246
248
|
if (node['#id']) {
|
|
247
249
|
(0, assert_1.default)(!filterRefAlias[node['#id']]);
|
|
248
250
|
(0, lodash_1.assign)(filterRefAlias, (_b = {},
|
|
249
|
-
_b[node['#id']] = alias,
|
|
251
|
+
_b[node['#id']] = [alias, entityName],
|
|
250
252
|
_b));
|
|
251
253
|
}
|
|
252
254
|
};
|
|
@@ -366,15 +368,25 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
366
368
|
}
|
|
367
369
|
});
|
|
368
370
|
if (node['#id']) {
|
|
369
|
-
(0, assert_1.default)(!projectionRefAlias[node['#id']]);
|
|
371
|
+
(0, assert_1.default)(!projectionRefAlias[node['#id']], "projection\u4E0A\u6709\u91CD\u590D\u7684#id\u5B9A\u4E49\u300C".concat(node['#id'], "\u300D"));
|
|
370
372
|
(0, lodash_1.assign)(projectionRefAlias, (_b = {},
|
|
371
|
-
_b[node['#id']] = alias,
|
|
373
|
+
_b[node['#id']] = [alias, entityName],
|
|
372
374
|
_b));
|
|
373
375
|
}
|
|
374
376
|
};
|
|
375
377
|
if (projection) {
|
|
376
378
|
analyzeProjectionNode({ node: projection, path: './', entityName: entity, alias: alias });
|
|
377
379
|
}
|
|
380
|
+
else if (aggregation) {
|
|
381
|
+
for (var k in aggregation) {
|
|
382
|
+
analyzeProjectionNode({
|
|
383
|
+
node: aggregation[k],
|
|
384
|
+
path: './',
|
|
385
|
+
entityName: entity,
|
|
386
|
+
alias: alias,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
}
|
|
378
390
|
return {
|
|
379
391
|
aliasDict: aliasDict,
|
|
380
392
|
from: from,
|
|
@@ -536,7 +548,7 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
536
548
|
}
|
|
537
549
|
else if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
|
|
538
550
|
// expression
|
|
539
|
-
whereText += " (".concat(_this.translateExpression(alias, filter2[attr], filterRefAlias), ")");
|
|
551
|
+
whereText += " (".concat(_this.translateExpression(entity2, alias, filter2[attr], filterRefAlias), ")");
|
|
540
552
|
}
|
|
541
553
|
else if (['$gt', '$gte', '$lt', '$lte', '$eq', '$ne', '$startsWith', '$endsWith', '$includes'].includes(attr)) {
|
|
542
554
|
whereText += _this.translateComparison(attr, filter2[attr], type);
|
|
@@ -596,7 +608,7 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
596
608
|
var attr = Object.keys(sortAttr)[0];
|
|
597
609
|
var alias = aliasDict[path];
|
|
598
610
|
if (attr.toLocaleLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
|
|
599
|
-
return _this.translateExpression(alias, sortAttr[attr], {});
|
|
611
|
+
return _this.translateExpression(entity2, alias, sortAttr[attr], {});
|
|
600
612
|
}
|
|
601
613
|
else if (sortAttr[attr] === 1) {
|
|
602
614
|
return "`".concat(alias, "`.`").concat(attr, "`");
|
|
@@ -625,9 +637,10 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
625
637
|
});
|
|
626
638
|
return sortText;
|
|
627
639
|
};
|
|
628
|
-
SqlTranslator.prototype.translateProjection = function (entity, projection, aliasDict, projectionRefAlias) {
|
|
640
|
+
SqlTranslator.prototype.translateProjection = function (entity, projection, aliasDict, projectionRefAlias, commonPrefix, disableAs) {
|
|
629
641
|
var _this = this;
|
|
630
642
|
var schema = this.schema;
|
|
643
|
+
var as = '';
|
|
631
644
|
var translateInner = function (entity2, projection2, path) {
|
|
632
645
|
var alias = aliasDict[path];
|
|
633
646
|
var attributes = schema[entity2].attributes;
|
|
@@ -641,9 +654,21 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
641
654
|
return [1, 2].includes(rel) || typeof rel === 'string';
|
|
642
655
|
});
|
|
643
656
|
attrs.forEach(function (attr, idx) {
|
|
657
|
+
var prefix2 = commonPrefix ? "".concat(commonPrefix, ".").concat(prefix) : prefix;
|
|
644
658
|
if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
|
|
645
|
-
var exprText = _this.translateExpression(alias, projection2[attr], projectionRefAlias);
|
|
646
|
-
|
|
659
|
+
var exprText = _this.translateExpression(entity2, alias, projection2[attr], projectionRefAlias);
|
|
660
|
+
if (disableAs) {
|
|
661
|
+
projText += " ".concat(exprText);
|
|
662
|
+
}
|
|
663
|
+
else {
|
|
664
|
+
projText += " ".concat(exprText, " as `").concat(prefix2).concat(attr, "`");
|
|
665
|
+
if (!as) {
|
|
666
|
+
as = "`".concat(prefix2).concat(attr, "`");
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
as += ", `".concat(prefix2).concat(attr, "`");
|
|
670
|
+
}
|
|
671
|
+
}
|
|
647
672
|
}
|
|
648
673
|
else {
|
|
649
674
|
var rel = (0, relation_1.judgeRelation)(_this.schema, entity2, attr);
|
|
@@ -656,11 +681,33 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
656
681
|
else if (rel === 1) {
|
|
657
682
|
var type = attributes[attr].type;
|
|
658
683
|
if (projection2[attr] === 1) {
|
|
659
|
-
|
|
684
|
+
if (disableAs) {
|
|
685
|
+
projText += " ".concat(_this.translateAttrProjection(type, alias, attr));
|
|
686
|
+
}
|
|
687
|
+
else {
|
|
688
|
+
projText += " ".concat(_this.translateAttrProjection(type, alias, attr), " as `").concat(prefix2).concat(attr, "`");
|
|
689
|
+
if (!as) {
|
|
690
|
+
as = "`".concat(prefix2).concat(attr, "`");
|
|
691
|
+
}
|
|
692
|
+
else {
|
|
693
|
+
as += ", `".concat(prefix2).concat(attr, "`");
|
|
694
|
+
}
|
|
695
|
+
}
|
|
660
696
|
}
|
|
661
697
|
else {
|
|
662
698
|
(0, assert_1.default)(typeof projection2 === 'string');
|
|
663
|
-
|
|
699
|
+
if (disableAs) {
|
|
700
|
+
projText += " ".concat(_this.translateAttrProjection(type, alias, attr));
|
|
701
|
+
}
|
|
702
|
+
else {
|
|
703
|
+
projText += " ".concat(_this.translateAttrProjection(type, alias, attr), " as `").concat(prefix2).concat(projection2[attr], "`");
|
|
704
|
+
if (!as) {
|
|
705
|
+
as = "`".concat(prefix2).concat(projection2[attr], "`");
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
as += "`".concat(prefix2).concat(projection2[attr], "`");
|
|
709
|
+
}
|
|
710
|
+
}
|
|
664
711
|
}
|
|
665
712
|
}
|
|
666
713
|
}
|
|
@@ -670,7 +717,10 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
670
717
|
});
|
|
671
718
|
return projText;
|
|
672
719
|
};
|
|
673
|
-
return
|
|
720
|
+
return {
|
|
721
|
+
projText: translateInner(entity, projection, './'),
|
|
722
|
+
as: as,
|
|
723
|
+
};
|
|
674
724
|
};
|
|
675
725
|
SqlTranslator.prototype.translateSelectInner = function (entity, selection, initialNumber, refAlias, option) {
|
|
676
726
|
var data = selection.data, filter = selection.filter, sorter = selection.sorter, indexFrom = selection.indexFrom, count = selection.count;
|
|
@@ -681,11 +731,11 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
681
731
|
}, initialNumber), fromText = _a.from, aliasDict = _a.aliasDict, projectionRefAlias = _a.projectionRefAlias, extraWhere = _a.extraWhere, filterRefAlias = _a.filterRefAlias, currentNumber = _a.currentNumber;
|
|
682
732
|
(0, assert_1.default)((0, lodash_1.intersection)((0, lodash_1.keys)(refAlias), (0, lodash_1.keys)(filterRefAlias)).length === 0, 'filter中的#node结点定义有重复');
|
|
683
733
|
(0, lodash_1.assign)(refAlias, filterRefAlias);
|
|
684
|
-
var projText = this.translateProjection(entity, data, aliasDict, projectionRefAlias);
|
|
734
|
+
var projText = this.translateProjection(entity, data, aliasDict, projectionRefAlias).projText;
|
|
685
735
|
var _b = this.translateFilter(entity, selection, aliasDict, refAlias, currentNumber, extraWhere, option), filterText = _b.stmt, currentNumber2 = _b.currentNumber;
|
|
686
736
|
var sorterText = sorter && this.translateSorter(entity, sorter, aliasDict);
|
|
687
737
|
return {
|
|
688
|
-
stmt: this.populateSelectStmt(projText, fromText,
|
|
738
|
+
stmt: this.populateSelectStmt(projText, fromText, aliasDict, filterText, sorterText, undefined, indexFrom, count, option, selection),
|
|
689
739
|
currentNumber: currentNumber2,
|
|
690
740
|
};
|
|
691
741
|
};
|
|
@@ -693,6 +743,57 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
693
743
|
var stmt = this.translateSelectInner(entity, selection, 1, {}, option).stmt;
|
|
694
744
|
return stmt;
|
|
695
745
|
};
|
|
746
|
+
SqlTranslator.prototype.translateAggregate = function (entity, aggregation, option) {
|
|
747
|
+
var data = aggregation.data, filter = aggregation.filter, sorter = aggregation.sorter, indexFrom = aggregation.indexFrom, count = aggregation.count;
|
|
748
|
+
var _a = this.analyzeJoin(entity, {
|
|
749
|
+
aggregation: data,
|
|
750
|
+
filter: filter,
|
|
751
|
+
sorter: sorter,
|
|
752
|
+
}, 1), fromText = _a.from, aliasDict = _a.aliasDict, projectionRefAlias = _a.projectionRefAlias, extraWhere = _a.extraWhere, filterRefAlias = _a.filterRefAlias, currentNumber = _a.currentNumber;
|
|
753
|
+
var projText = '';
|
|
754
|
+
var groupByText = '';
|
|
755
|
+
for (var k in data) {
|
|
756
|
+
if (k === '#aggr') {
|
|
757
|
+
var _b = this.translateProjection(entity, data[k], aliasDict, projectionRefAlias, '#data'), projSubText = _b.projText, as = _b.as;
|
|
758
|
+
if (!projText) {
|
|
759
|
+
projText = projSubText;
|
|
760
|
+
}
|
|
761
|
+
else {
|
|
762
|
+
projText += ", ".concat(projSubText);
|
|
763
|
+
}
|
|
764
|
+
groupByText = as;
|
|
765
|
+
}
|
|
766
|
+
else {
|
|
767
|
+
var projSubText = this.translateProjection(entity, data[k], aliasDict, projectionRefAlias, undefined, true).projText;
|
|
768
|
+
var projSubText2 = '';
|
|
769
|
+
if (k.startsWith('#max')) {
|
|
770
|
+
projSubText2 = "max(".concat(projSubText, ") as `").concat(k, "`");
|
|
771
|
+
}
|
|
772
|
+
else if (k.startsWith('#min')) {
|
|
773
|
+
projSubText2 = "min(".concat(projSubText, ") as `").concat(k, "`");
|
|
774
|
+
}
|
|
775
|
+
else if (k.startsWith('#count')) {
|
|
776
|
+
projSubText2 = "count(".concat(projSubText, ") as `").concat(k, "`");
|
|
777
|
+
}
|
|
778
|
+
else if (k.startsWith('#sum')) {
|
|
779
|
+
projSubText2 = "sum(".concat(projSubText, ") as `").concat(k, "`");
|
|
780
|
+
}
|
|
781
|
+
else {
|
|
782
|
+
(0, assert_1.default)(k.startsWith('#avg'));
|
|
783
|
+
projSubText2 = "avg(".concat(projSubText, ") as `").concat(k, "`");
|
|
784
|
+
}
|
|
785
|
+
if (!projText) {
|
|
786
|
+
projText = projSubText2;
|
|
787
|
+
}
|
|
788
|
+
else {
|
|
789
|
+
projText += ", ".concat(projSubText2);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
var filterText = this.translateFilter(entity, aggregation, aliasDict, {}, currentNumber, extraWhere, option).stmt;
|
|
794
|
+
var sorterText = sorter && this.translateSorter(entity, sorter, aliasDict);
|
|
795
|
+
return this.populateSelectStmt(projText, fromText, aliasDict, filterText, sorterText, groupByText, indexFrom, count, option, undefined, aggregation);
|
|
796
|
+
};
|
|
696
797
|
SqlTranslator.prototype.translateCount = function (entity, selection, option) {
|
|
697
798
|
var filter = selection.filter, count = selection.count;
|
|
698
799
|
var _a = this.analyzeJoin(entity, {
|
|
@@ -701,10 +802,10 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
701
802
|
var projText = 'count(1) cnt';
|
|
702
803
|
var filterText = this.translateFilter(entity, selection, aliasDict, filterRefAlias, currentNumber, extraWhere, option).stmt;
|
|
703
804
|
if (count) {
|
|
704
|
-
var subQuerySql = this.populateSelectStmt('1', fromText, Object.assign({}, selection, { indexFrom: 0, count: count })
|
|
805
|
+
var subQuerySql = this.populateSelectStmt('1', fromText, aliasDict, filterText, undefined, undefined, undefined, undefined, option, Object.assign({}, selection, { indexFrom: 0, count: count }));
|
|
705
806
|
return "select count(1) cnt from (".concat(subQuerySql, ") __tmp");
|
|
706
807
|
}
|
|
707
|
-
return this.populateSelectStmt(projText, fromText,
|
|
808
|
+
return this.populateSelectStmt(projText, fromText, aliasDict, filterText, undefined, undefined, undefined, undefined, option, selection);
|
|
708
809
|
};
|
|
709
810
|
SqlTranslator.prototype.translateRemove = function (entity, operation, option) {
|
|
710
811
|
var filter = operation.filter, sorter = operation.sorter, indexFrom = operation.indexFrom, count = operation.count;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oak-db",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "oak-db",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"author": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"lodash": "^4.17.21",
|
|
19
19
|
"mysql": "^2.18.1",
|
|
20
20
|
"mysql2": "^2.3.3",
|
|
21
|
-
"oak-domain": "^2.
|
|
21
|
+
"oak-domain": "^2.3.2",
|
|
22
22
|
"uuid": "^8.3.2"
|
|
23
23
|
},
|
|
24
24
|
"license": "ISC",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"@types/node": "^17.0.42",
|
|
30
30
|
"@types/uuid": "^8.3.4",
|
|
31
31
|
"mocha": "^10.0.0",
|
|
32
|
-
"oak-general-business": "^2.1
|
|
32
|
+
"oak-general-business": "^2.3.1",
|
|
33
33
|
"ts-node": "~10.9.1",
|
|
34
34
|
"tslib": "^2.4.0",
|
|
35
35
|
"typescript": "~4.7.4"
|