oak-db 2.2.0 → 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.
@@ -97,7 +97,8 @@ export declare class MySqlTranslator<ED extends EntityDict> extends SqlTranslato
97
97
  replace?: boolean;
98
98
  }): string[];
99
99
  private translateFnName;
100
- protected translateExpression<T extends keyof ED>(alias: string, expression: RefOrExpression<keyof ED[T]["OpSchema"]>, refDict: Record<string, string>): string;
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;
101
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;
@@ -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
- return '%s + %s';
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
- return '%s * %s';
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.translateExpression = function (alias, expression, refDict) {
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 (typeof constant === 'string') {
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);
@@ -18,7 +18,7 @@ export declare abstract class SqlTranslator<ED extends EntityDict> {
18
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
  /**
@@ -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
  };
@@ -368,7 +370,7 @@ var SqlTranslator = /** @class */ (function () {
368
370
  if (node['#id']) {
369
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
  };
@@ -546,7 +548,7 @@ var SqlTranslator = /** @class */ (function () {
546
548
  }
547
549
  else if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
548
550
  // expression
549
- whereText += " (".concat(_this.translateExpression(alias, filter2[attr], filterRefAlias), ")");
551
+ whereText += " (".concat(_this.translateExpression(entity2, alias, filter2[attr], filterRefAlias), ")");
550
552
  }
551
553
  else if (['$gt', '$gte', '$lt', '$lte', '$eq', '$ne', '$startsWith', '$endsWith', '$includes'].includes(attr)) {
552
554
  whereText += _this.translateComparison(attr, filter2[attr], type);
@@ -606,7 +608,7 @@ var SqlTranslator = /** @class */ (function () {
606
608
  var attr = Object.keys(sortAttr)[0];
607
609
  var alias = aliasDict[path];
608
610
  if (attr.toLocaleLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
609
- return _this.translateExpression(alias, sortAttr[attr], {});
611
+ return _this.translateExpression(entity2, alias, sortAttr[attr], {});
610
612
  }
611
613
  else if (sortAttr[attr] === 1) {
612
614
  return "`".concat(alias, "`.`").concat(attr, "`");
@@ -654,7 +656,7 @@ var SqlTranslator = /** @class */ (function () {
654
656
  attrs.forEach(function (attr, idx) {
655
657
  var prefix2 = commonPrefix ? "".concat(commonPrefix, ".").concat(prefix) : prefix;
656
658
  if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
657
- var exprText = _this.translateExpression(alias, projection2[attr], projectionRefAlias);
659
+ var exprText = _this.translateExpression(entity2, alias, projection2[attr], projectionRefAlias);
658
660
  if (disableAs) {
659
661
  projText += " ".concat(exprText);
660
662
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-db",
3
- "version": "2.2.0",
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.3.0",
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.3.0",
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"