mythix-orm-sql-base 1.4.1 → 1.4.4

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.
@@ -65,6 +65,14 @@ class SQLConnectionBase extends ConnectionBase {
65
65
  }
66
66
  }
67
67
 
68
+ databaseSupportsLimitInSubQuery() {
69
+ return false;
70
+ }
71
+
72
+ databaseSupportsOrderInSubQuery() {
73
+ return false;
74
+ }
75
+
68
76
  prepareArrayValuesForSQL(_array) {
69
77
  let array = Nife.arrayFlatten(_array);
70
78
 
@@ -84,17 +92,6 @@ class SQLConnectionBase extends ConnectionBase {
84
92
  return Nife.uniq(array);
85
93
  }
86
94
 
87
- getDefaultOrder(Model, options) {
88
- let order = Nife.toArray(Model.getDefaultOrder(options));
89
-
90
- order = Nife.arrayFlatten(order).filter(Boolean);
91
-
92
- if (Nife.isEmpty(order))
93
- return;
94
-
95
- return order;
96
- }
97
-
98
95
  generateSavePointName() {
99
96
  let id = UUID.v4();
100
97
 
@@ -126,12 +123,29 @@ class SQLConnectionBase extends ConnectionBase {
126
123
  if (!result)
127
124
  return {};
128
125
 
129
- let rows = result.rows;
126
+ let {
127
+ rows,
128
+ columns,
129
+ } = result;
130
130
  if (Nife.isEmpty(rows))
131
131
  return {};
132
132
 
133
+ const generateIDForModelFields = (data) => {
134
+ let keys = Object.keys(data || {}).sort();
135
+ let parts = [];
136
+
137
+ for (let i = 0, il = keys.length; i < il; i++) {
138
+ let key = keys[i];
139
+ let value = data[key];
140
+
141
+ parts.push(`${key}:${value}`);
142
+ }
143
+
144
+ return (!parts.length) ? null : parts.join(',');
145
+ };
146
+
133
147
  let context = queryEngine._getRawQueryContext();
134
- let fields = this.findAllFieldsFromFieldProjectionMap(result.columns);
148
+ let fields = this.findAllFieldsFromFieldProjectionMap(columns);
135
149
  let rootModelName = context.rootModelName;
136
150
  let modelData = {};
137
151
  let alreadyVisited = {};
@@ -200,20 +214,22 @@ class SQLConnectionBase extends ConnectionBase {
200
214
  if (!models)
201
215
  models = modelData[modelName] = [];
202
216
 
203
- if (pkFieldName) {
204
- let id = model[pkFieldName];
217
+ let id = model[pkFieldName];
218
+ if (id == null)
219
+ id = generateIDForModelFields(model);
205
220
 
206
- if (id != null) {
207
- let idKey = `${modelName}:${pkFieldName}:${id}`;
221
+ if (id != null) {
222
+ let idKey = `${modelName}:${pkFieldName}:${id}`;
208
223
 
209
- if (alreadyVisited[idKey] != null) {
210
- index = alreadyVisited[idKey];
211
- } else {
212
- index = alreadyVisited[idKey] = models.length;
213
- models.push(model);
214
- }
224
+ if (alreadyVisited[idKey] != null) {
225
+ index = alreadyVisited[idKey];
226
+ model = models[index];
227
+ } else {
228
+ index = alreadyVisited[idKey] = models.length;
229
+ models.push(model);
215
230
  }
216
231
  } else {
232
+ index = models.length;
217
233
  models.push(model);
218
234
  continue;
219
235
  }
@@ -527,21 +527,32 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
527
527
  let sqlParts = [];
528
528
 
529
529
  if (Nife.isNotEmpty(order)) {
530
- let result = this.generateOrderClause(order, options);
531
- if (result)
532
- sqlParts.push(result);
530
+ if (!(options && options.isSubQuery) || (options && options.isSubQuery && this.connection.databaseSupportsOrderInSubQuery())) {
531
+ let result = this.generateOrderClause(order, options);
532
+ if (result)
533
+ sqlParts.push(result);
534
+
535
+ if (!(Nife.instanceOf(limit, 'number') && isFinite(limit)) && options && options.forceLimit) {
536
+ limit = options.forceLimit;
537
+ offset = 0;
538
+ }
539
+ }
533
540
  }
534
541
 
535
542
  if (!Object.is(limit, Infinity) && Nife.isNotEmpty(limit)) {
536
- let result = this.generateLimitClause(limit, options);
537
- if (result)
538
- sqlParts.push(result);
543
+ if (!(options && options.isSubQuery) || (options && options.isSubQuery && this.connection.databaseSupportsLimitInSubQuery())) {
544
+ let result = this.generateLimitClause(limit, options);
545
+ if (result)
546
+ sqlParts.push(result);
547
+ }
539
548
  }
540
549
 
541
550
  if (Nife.isNotEmpty(offset)) {
542
- let result = this.generateOffsetClause(offset, options);
543
- if (result)
544
- sqlParts.push(result);
551
+ if (!(options && options.isSubQuery) || (options && options.isSubQuery && this.connection.databaseSupportsLimitInSubQuery())) {
552
+ let result = this.generateOffsetClause(offset, options);
553
+ if (result)
554
+ sqlParts.push(result);
555
+ }
545
556
  }
546
557
 
547
558
  return sqlParts.join(' ');
@@ -1071,9 +1082,10 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
1071
1082
  queryEngine = queryEngine.PROJECT(`${Model.getModelName()}:${pkField.fieldName}`);
1072
1083
 
1073
1084
  let innerSelect = this.generateSelectStatement(queryEngine, this.stackAssign(options, { isSubQuery: true, noProjectionAliases: true }));
1085
+ let orderLimitOffset = this.generateSelectOrderLimitOffset(queryEngine, this.stackAssign(options, { forceLimit: 4294967294 }));
1074
1086
  let escapedColumnName = this.getEscapedColumnName(Model, pkField, options);
1075
1087
 
1076
- return `DELETE FROM ${escapedTableName} WHERE ${escapedColumnName} IN (${innerSelect})`;
1088
+ return `DELETE FROM ${escapedTableName} WHERE ${escapedColumnName} IN (${innerSelect})${(orderLimitOffset) ? ` ${orderLimitOffset}` : ''}`;
1077
1089
  } else {
1078
1090
  return `DELETE FROM ${escapedTableName}${(where) ? ` ${where}` : ''}`;
1079
1091
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mythix-orm-sql-base",
3
- "version": "1.4.1",
3
+ "version": "1.4.4",
4
4
  "description": "SQL base support for Mythix ORM",
5
5
  "main": "lib/index.js",
6
6
  "type": "commonjs",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "homepage": "https://github.com/th317erd/mythix-orm-sql-base#readme",
35
35
  "peerDependencies": {
36
- "mythix-orm": "^1.5.1"
36
+ "mythix-orm": "^1.5.5"
37
37
  },
38
38
  "dependencies": {
39
39
  "nife": "^1.11.3",