@balena/lf-to-abstract-sql 4.3.0 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,14 @@
1
+ - commits:
2
+ - subject: Support the join optimization for `AND` clauses
3
+ hash: 968b6755664f48d5d84b8af63597986adbfbbc36
4
+ body: ''
5
+ footer:
6
+ Change-type: minor
7
+ change-type: minor
8
+ author: Pagan Gazzard
9
+ nested: []
10
+ version: 4.4.0
11
+ date: 2021-12-06T16:58:41.542Z
1
12
  - commits:
2
13
  - subject: Add an automatic `is of` reverse relationship for concept types
3
14
  hash: f84a80730b68b34203bfec29f97e5dac6b464857
@@ -8,7 +19,7 @@
8
19
  author: Pagan Gazzard
9
20
  nested: []
10
21
  version: 4.3.0
11
- date: 2021-07-08T10:38:03.633Z
22
+ date: 2021-07-06T16:36:07.964Z
12
23
  - commits:
13
24
  - subject: Delete CODEOWNERS
14
25
  hash: cb88bffb06f6b1cc21a2df9ef22cfb4886fb0696
package/CHANGELOG.md CHANGED
@@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file
4
4
  automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY!
5
5
  This project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
- ## 4.3.0 - 2021-07-08
7
+ ## 4.4.0 - 2021-12-06
8
+
9
+ * Support the join optimization for `AND` clauses [Pagan Gazzard]
10
+
11
+ ## 4.3.0 - 2021-07-06
8
12
 
9
13
  * Add an automatic `is of` reverse relationship for concept types [Pagan Gazzard]
10
14
 
@@ -1244,17 +1244,29 @@ LF2AbstractSQL.FactTypeFieldName = function(factType) {
1244
1244
  return 2 === factType.length ? factType[1][1] : "has" === factType[1][1] ? factType[2][1] : factType[1][1] + "-" + factType[2][1];
1245
1245
  };
1246
1246
 
1247
+ LF2AbstractSQL.JoinOptimization = function(query, whereBody) {
1248
+ if ("And" == whereBody[0]) {
1249
+ for (var i = 1; i < whereBody.length; i++) if (this.JoinOptimization(query, whereBody[i])) {
1250
+ whereBody.splice(i, 1);
1251
+ i--;
1252
+ }
1253
+ if (1 === whereBody.length) return !0;
1254
+ } else if ("Exists" == whereBody[0] && ("SelectQuery" == whereBody[1][0] || "InsertQuery" == whereBody[1][0] || "UpdateQuery" == whereBody[1][0] || "UpsertQuery" == whereBody[1][0])) {
1255
+ whereBody = whereBody[1].slice(1);
1256
+ for (var i = 0; i < whereBody.length; i++) "From" == whereBody[i][0] && query.push(whereBody[i]);
1257
+ for (var i = 0; i < whereBody.length; i++) "Where" == whereBody[i][0] && this.AddWhereClause(query, whereBody[i][1]);
1258
+ return !0;
1259
+ }
1260
+ return !1;
1261
+ };
1262
+
1247
1263
  LF2AbstractSQL.AddWhereClause = function(query, whereBody) {
1248
- if (!_.isEqual(whereBody, [ "Equals", [ "Boolean", !0 ], [ "Boolean", !0 ] ])) if ("Exists" != whereBody[0] || "SelectQuery" != whereBody[1][0] && "InsertQuery" != whereBody[1][0] && "UpdateQuery" != whereBody[1][0] && "UpsertQuery" != whereBody[1][0]) {
1264
+ if (!_.isEqual(whereBody, [ "Equals", [ "Boolean", !0 ], [ "Boolean", !0 ] ])) if (this.JoinOptimization(query, whereBody)) ; else {
1249
1265
  for (var i = 1; i < query.length; i++) if ("Where" == query[i][0]) {
1250
1266
  query[i][1] = [ "And", query[i][1], whereBody ];
1251
1267
  return;
1252
1268
  }
1253
1269
  query.push([ "Where", whereBody ]);
1254
- } else {
1255
- whereBody = whereBody[1].slice(1);
1256
- for (var i = 0; i < whereBody.length; i++) "From" == whereBody[i][0] && query.push(whereBody[i]);
1257
- for (var i = 0; i < whereBody.length; i++) "Where" == whereBody[i][0] && this.AddWhereClause(query, whereBody[i][1]);
1258
1270
  }
1259
1271
  };
1260
1272
 
@@ -1015,29 +1015,56 @@ LF2AbstractSQL.FactTypeFieldName = function(factType) {
1015
1015
  return factType[1][1] + '-' + factType[2][1];
1016
1016
  };
1017
1017
 
1018
- LF2AbstractSQL.AddWhereClause = function(query, whereBody) {
1019
- // TODO: Move these to optimisation passes?
1020
- // Check if it's true == true
1021
- if (_.isEqual(whereBody, ['Equals', ['Boolean', true], ['Boolean', true]])) {
1022
- return;
1023
- }
1024
- // Check if it's a place where we can do a join optimisation.
1025
- if(whereBody[0] == 'Exists' && (
1026
- whereBody[1][0] == 'SelectQuery' || whereBody[1][0] == 'InsertQuery' ||
1027
- whereBody[1][0] == 'UpdateQuery' || whereBody[1][0] == 'UpsertQuery')) {
1018
+ LF2AbstractSQL.JoinOptimization = function(query, whereBody) {
1019
+ if (whereBody[0] == 'And') {
1020
+ // If the where clause is an `AND` then check through each branch to see if it can be optimized
1021
+ for (var i = 1; i < whereBody.length; i++) {
1022
+ if (this.JoinOptimization(query, whereBody[i])) {
1023
+ // If we optimized it away then remove it
1024
+ whereBody.splice(i, 1);
1025
+ // And decrement i to recognize that we removed it
1026
+ i--;
1027
+ }
1028
+ }
1029
+ if (whereBody.length === 1) {
1030
+ return true;
1031
+ }
1032
+ } else if (
1033
+ whereBody[0] == 'Exists' &&
1034
+ (whereBody[1][0] == 'SelectQuery' ||
1035
+ whereBody[1][0] == 'InsertQuery' ||
1036
+ whereBody[1][0] == 'UpdateQuery' ||
1037
+ whereBody[1][0] == 'UpsertQuery')
1038
+ ) {
1039
+ // If the where clause is an `EXISTS($query)` then it can be optimized into a join for LF
1028
1040
  whereBody = whereBody[1].slice(1);
1029
- for(var i=0; i < whereBody.length; i++) {
1030
- if(whereBody[i][0] == 'From') {
1041
+ for (var i = 0; i < whereBody.length; i++) {
1042
+ if (whereBody[i][0] == 'From') {
1031
1043
  query.push(whereBody[i]);
1032
1044
  }
1033
1045
  }
1034
- for(var i=0; i < whereBody.length; i++) {
1035
- if(whereBody[i][0] == 'Where') {
1046
+ for (var i = 0; i < whereBody.length; i++) {
1047
+ if (whereBody[i][0] == 'Where') {
1036
1048
  this.AddWhereClause(query, whereBody[i][1]);
1037
1049
  }
1038
1050
  }
1051
+ return true;
1052
+ }
1053
+ return false;
1054
+ }
1055
+
1056
+ LF2AbstractSQL.AddWhereClause = function(query, whereBody) {
1057
+ // TODO: Move these to optimisation passes?
1058
+ // Check if it's true == true
1059
+ if (_.isEqual(whereBody, ['Equals', ['Boolean', true], ['Boolean', true]])) {
1060
+ return;
1061
+ }
1062
+ // Check if it's a place where we can do a join optimisation.
1063
+ if (this.JoinOptimization(query, whereBody)) {
1064
+ // We optimized away the entire thing so we don't need to add it at all
1039
1065
  }
1040
1066
  else {
1067
+ // Some is left so we add it
1041
1068
  for(var i=1; i < query.length; i++) {
1042
1069
  if(query[i][0] == 'Where') {
1043
1070
  query[i][1] = ['And', query[i][1], whereBody];
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@balena/lf-to-abstract-sql",
3
- "version": "4.3.0",
3
+ "version": "4.4.0",
4
4
  "description": "LF to Abstract SQL translator.",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "lint": "balena-lint --typescript -e js test index.js",
8
- "lint-fix": "balena-lint --typescript --fix -e js test index.js",
7
+ "lint": "balena-lint -e js test index.js",
8
+ "lint-fix": "balena-lint --fix -e js test index.js",
9
9
  "pretest": "npm run prepare",
10
10
  "test": "mocha",
11
11
  "posttest": "npm run lint",
@@ -21,10 +21,10 @@
21
21
  "ometa-js": "^1.5.4"
22
22
  },
23
23
  "devDependencies": {
24
- "@balena/lint": "^5.4.2",
25
- "@balena/sbvr-types": "^3.4.4",
24
+ "@balena/lint": "^6.2.0",
25
+ "@balena/sbvr-types": "^3.4.6",
26
26
  "chai": "^4.3.4",
27
- "mocha": "^8.4.0",
27
+ "mocha": "^9.1.3",
28
28
  "require-npm4-to-publish": "^1.0.0"
29
29
  },
30
30
  "mocha": {
@@ -35,6 +35,6 @@
35
35
  "_": "test/**/*.js"
36
36
  },
37
37
  "versionist": {
38
- "publishedAt": "2021-07-08T10:38:03.698Z"
38
+ "publishedAt": "2021-12-06T16:58:41.621Z"
39
39
  }
40
40
  }