oak-db 3.3.7 → 3.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -617,43 +617,58 @@ class SqlTranslator {
617
617
  });
618
618
  }
619
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
- ],
620
+ // 对in和not in优化,不要用exists查询
621
+ if (['in', 'not in'].includes(predicate)) {
622
+ const { stmt, currentNumber: ct2 } = this.translateSelectInner(subEntity, {
623
+ data: {
624
+ [fk]: 1,
625
+ },
626
+ filter: filter2[attr]
627
+ }, currentNumber, filterRefAlias, option);
628
+ currentNumber = ct2;
629
+ whereText += `(${refAlia2}.id ${predicate} (${stmt}))`;
630
+ }
631
+ else {
632
+ /**
633
+ * 只剩下all/not all
634
+ * 翻译成键值不等条件下的not exist/exist
635
+ */
636
+ const joinFilter = {
637
+ $expr83: {
638
+ [['all', 'not all'].includes(predicate) ? '$ne' : '$eq']: [
639
+ {
640
+ '#attr': fk,
641
+ },
642
+ {
643
+ '#refId': refAlia2,
644
+ '#refAttr': 'id',
645
+ }
646
+ ],
647
+ }
648
+ };
649
+ if (!foreignKey) {
650
+ Object.assign(joinFilter, {
651
+ entity: entity2,
652
+ });
631
653
  }
632
- };
633
- if (!foreignKey) {
634
- Object.assign(joinFilter, {
635
- entity: entity2,
636
- });
654
+ const { stmt, currentNumber: ct2 } = this.translateSelectInner(subEntity, {
655
+ data: {
656
+ id: 1,
657
+ },
658
+ filter: (0, filter_1.combineFilters)(subEntity, this.schema, [joinFilter, filter2[attr]]),
659
+ indexFrom: 0,
660
+ count: 1,
661
+ }, currentNumber, filterRefAlias, option);
662
+ currentNumber = ct2;
663
+ const PREDICATE_DICT = {
664
+ // in 和 not in已经不会命中了(优化到上面的路径中)
665
+ 'in': 'exists',
666
+ 'not in': 'not exists',
667
+ 'all': 'not exists',
668
+ 'not all': 'exists',
669
+ };
670
+ whereText += ` ${PREDICATE_DICT[predicate]} (${stmt})`;
637
671
  }
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
672
  }
658
673
  else {
659
674
  (0, assert_1.default)(attributes.hasOwnProperty(attr), `非法的属性${attr}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-db",
3
- "version": "3.3.7",
3
+ "version": "3.3.9",
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": "^5.1.24",
21
+ "oak-domain": "^5.1.27",
22
22
  "uuid": "^8.3.2"
23
23
  },
24
24
  "license": "ISC",