joist-core 2.5.0-next.21 → 2.5.0-next.23
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/build/BaseEntity.cjs +5 -2
- package/build/BaseEntity.cjs.map +1 -1
- package/build/BaseEntity.d.cts +2 -0
- package/build/BaseEntity.d.cts.map +1 -1
- package/build/BaseEntity.d.mts +2 -0
- package/build/BaseEntity.d.mts.map +1 -1
- package/build/BaseEntity.js +5 -2
- package/build/BaseEntity.js.map +1 -1
- package/build/Entity.cjs.map +1 -1
- package/build/Entity.d.cts +6 -0
- package/build/Entity.d.cts.map +1 -1
- package/build/Entity.d.mts +6 -0
- package/build/Entity.d.mts.map +1 -1
- package/build/Entity.js.map +1 -1
- package/build/EntityManager.cjs +83 -22
- package/build/EntityManager.cjs.map +1 -1
- package/build/EntityManager.d.cts +7 -4
- package/build/EntityManager.d.cts.map +1 -1
- package/build/EntityManager.d.mts +7 -4
- package/build/EntityManager.d.mts.map +1 -1
- package/build/EntityManager.js +83 -22
- package/build/EntityManager.js.map +1 -1
- package/build/InstanceData.cjs +2 -2
- package/build/InstanceData.cjs.map +1 -1
- package/build/InstanceData.d.cts +2 -2
- package/build/InstanceData.d.mts +2 -2
- package/build/InstanceData.js +2 -2
- package/build/InstanceData.js.map +1 -1
- package/build/PluginManager.cjs +6 -0
- package/build/PluginManager.cjs.map +1 -1
- package/build/PluginManager.d.cts +10 -0
- package/build/PluginManager.d.cts.map +1 -1
- package/build/PluginManager.d.mts +10 -0
- package/build/PluginManager.d.mts.map +1 -1
- package/build/PluginManager.js +6 -0
- package/build/PluginManager.js.map +1 -1
- package/build/clientIds.cjs +47 -0
- package/build/clientIds.cjs.map +1 -0
- package/build/clientIds.d.cts +26 -0
- package/build/clientIds.d.cts.map +1 -0
- package/build/clientIds.d.mts +26 -0
- package/build/clientIds.d.mts.map +1 -0
- package/build/clientIds.js +42 -0
- package/build/clientIds.js.map +1 -0
- package/build/configure.cjs +3 -0
- package/build/configure.cjs.map +1 -1
- package/build/configure.d.cts.map +1 -1
- package/build/configure.d.mts.map +1 -1
- package/build/configure.js +3 -0
- package/build/configure.js.map +1 -1
- package/build/getProperties.cjs +4 -1
- package/build/getProperties.cjs.map +1 -1
- package/build/getProperties.js +4 -1
- package/build/getProperties.js.map +1 -1
- package/build/index.cjs +15 -4
- package/build/index.cjs.map +1 -1
- package/build/index.d.cts +9 -7
- package/build/index.d.cts.map +1 -1
- package/build/index.d.mts +9 -7
- package/build/index.d.mts.map +1 -1
- package/build/index.js +11 -5
- package/build/index.js.map +1 -1
- package/build/keys.cjs +4 -0
- package/build/keys.cjs.map +1 -1
- package/build/keys.d.cts.map +1 -1
- package/build/keys.d.mts.map +1 -1
- package/build/keys.js +4 -0
- package/build/keys.js.map +1 -1
- package/build/newTestInstance.cjs +8 -8
- package/build/newTestInstance.cjs.map +1 -1
- package/build/newTestInstance.d.cts.map +1 -1
- package/build/newTestInstance.d.mts.map +1 -1
- package/build/newTestInstance.js +8 -8
- package/build/newTestInstance.js.map +1 -1
- package/build/queries/ConditionBuilder.cjs +2 -1
- package/build/queries/ConditionBuilder.cjs.map +1 -1
- package/build/queries/ConditionBuilder.d.cts +9 -1
- package/build/queries/ConditionBuilder.d.cts.map +1 -1
- package/build/queries/ConditionBuilder.d.mts +9 -1
- package/build/queries/ConditionBuilder.d.mts.map +1 -1
- package/build/queries/ConditionBuilder.js +2 -2
- package/build/queries/ConditionBuilder.js.map +1 -1
- package/build/queries/conditions.cjs.map +1 -1
- package/build/queries/conditions.d.cts +4 -1
- package/build/queries/conditions.d.cts.map +1 -1
- package/build/queries/conditions.d.mts +4 -1
- package/build/queries/conditions.d.mts.map +1 -1
- package/build/queries/conditions.js.map +1 -1
- package/build/queries/sql/Expr.cjs +79 -134
- package/build/queries/sql/Expr.cjs.map +1 -1
- package/build/queries/sql/Expr.d.cts +26 -34
- package/build/queries/sql/Expr.d.cts.map +1 -1
- package/build/queries/sql/Expr.d.mts +26 -34
- package/build/queries/sql/Expr.d.mts.map +1 -1
- package/build/queries/sql/Expr.js +80 -133
- package/build/queries/sql/Expr.js.map +1 -1
- package/build/queries/sql/ParsedQuery.cjs +119 -0
- package/build/queries/sql/ParsedQuery.cjs.map +1 -0
- package/build/queries/sql/ParsedQuery.d.cts +105 -0
- package/build/queries/sql/ParsedQuery.d.cts.map +1 -0
- package/build/queries/sql/ParsedQuery.d.mts +105 -0
- package/build/queries/sql/ParsedQuery.d.mts.map +1 -0
- package/build/queries/sql/ParsedQuery.js +116 -0
- package/build/queries/sql/ParsedQuery.js.map +1 -0
- package/build/queries/sql/ParsedSqlExpression.cjs +74 -0
- package/build/queries/sql/ParsedSqlExpression.cjs.map +1 -0
- package/build/queries/sql/ParsedSqlExpression.d.cts +65 -0
- package/build/queries/sql/ParsedSqlExpression.d.cts.map +1 -0
- package/build/queries/sql/ParsedSqlExpression.d.mts +65 -0
- package/build/queries/sql/ParsedSqlExpression.d.mts.map +1 -0
- package/build/queries/sql/ParsedSqlExpression.js +69 -0
- package/build/queries/sql/ParsedSqlExpression.js.map +1 -0
- package/build/queries/sql/Tables.cjs +61 -46
- package/build/queries/sql/Tables.cjs.map +1 -1
- package/build/queries/sql/Tables.d.cts.map +1 -1
- package/build/queries/sql/Tables.d.mts.map +1 -1
- package/build/queries/sql/Tables.js +61 -46
- package/build/queries/sql/Tables.js.map +1 -1
- package/build/queries/sql/execute.cjs +45 -62
- package/build/queries/sql/execute.cjs.map +1 -1
- package/build/queries/sql/execute.d.cts +4 -3
- package/build/queries/sql/execute.d.cts.map +1 -1
- package/build/queries/sql/execute.d.mts +4 -3
- package/build/queries/sql/execute.d.mts.map +1 -1
- package/build/queries/sql/execute.js +47 -64
- package/build/queries/sql/execute.js.map +1 -1
- package/build/queries/sql/expressions/arrayAgg.cjs +14 -15
- package/build/queries/sql/expressions/arrayAgg.cjs.map +1 -1
- package/build/queries/sql/expressions/arrayAgg.d.cts +3 -2
- package/build/queries/sql/expressions/arrayAgg.d.cts.map +1 -1
- package/build/queries/sql/expressions/arrayAgg.d.mts +3 -2
- package/build/queries/sql/expressions/arrayAgg.d.mts.map +1 -1
- package/build/queries/sql/expressions/arrayAgg.js +15 -16
- package/build/queries/sql/expressions/arrayAgg.js.map +1 -1
- package/build/queries/sql/expressions/case.cjs +11 -16
- package/build/queries/sql/expressions/case.cjs.map +1 -1
- package/build/queries/sql/expressions/case.d.cts +3 -2
- package/build/queries/sql/expressions/case.d.cts.map +1 -1
- package/build/queries/sql/expressions/case.d.mts +3 -2
- package/build/queries/sql/expressions/case.d.mts.map +1 -1
- package/build/queries/sql/expressions/case.js +11 -16
- package/build/queries/sql/expressions/case.js.map +1 -1
- package/build/queries/sql/expressions/coalesce.cjs.map +1 -1
- package/build/queries/sql/expressions/coalesce.d.cts +3 -2
- package/build/queries/sql/expressions/coalesce.d.cts.map +1 -1
- package/build/queries/sql/expressions/coalesce.d.mts +3 -2
- package/build/queries/sql/expressions/coalesce.d.mts.map +1 -1
- package/build/queries/sql/expressions/coalesce.js.map +1 -1
- package/build/queries/sql/expressions/expression.cjs +10 -12
- package/build/queries/sql/expressions/expression.cjs.map +1 -1
- package/build/queries/sql/expressions/expression.d.cts +4 -3
- package/build/queries/sql/expressions/expression.d.cts.map +1 -1
- package/build/queries/sql/expressions/expression.d.mts +4 -3
- package/build/queries/sql/expressions/expression.d.mts.map +1 -1
- package/build/queries/sql/expressions/expression.js +11 -13
- package/build/queries/sql/expressions/expression.js.map +1 -1
- package/build/queries/sql/expressions/greatest.cjs.map +1 -1
- package/build/queries/sql/expressions/greatest.d.cts +3 -2
- package/build/queries/sql/expressions/greatest.d.cts.map +1 -1
- package/build/queries/sql/expressions/greatest.d.mts +3 -2
- package/build/queries/sql/expressions/greatest.d.mts.map +1 -1
- package/build/queries/sql/expressions/greatest.js.map +1 -1
- package/build/queries/sql/expressions/least.cjs.map +1 -1
- package/build/queries/sql/expressions/least.d.cts +3 -2
- package/build/queries/sql/expressions/least.d.cts.map +1 -1
- package/build/queries/sql/expressions/least.d.mts +3 -2
- package/build/queries/sql/expressions/least.d.mts.map +1 -1
- package/build/queries/sql/expressions/least.js.map +1 -1
- package/build/queries/sql/expressions/nullIf.cjs.map +1 -1
- package/build/queries/sql/expressions/nullIf.d.cts +3 -2
- package/build/queries/sql/expressions/nullIf.d.cts.map +1 -1
- package/build/queries/sql/expressions/nullIf.d.mts +3 -2
- package/build/queries/sql/expressions/nullIf.d.mts.map +1 -1
- package/build/queries/sql/expressions/nullIf.js.map +1 -1
- package/build/queries/sql/query.cjs +168 -381
- package/build/queries/sql/query.cjs.map +1 -1
- package/build/queries/sql/query.d.cts +44 -45
- package/build/queries/sql/query.d.cts.map +1 -1
- package/build/queries/sql/query.d.mts +44 -45
- package/build/queries/sql/query.d.mts.map +1 -1
- package/build/queries/sql/query.js +172 -383
- package/build/queries/sql/query.js.map +1 -1
- package/build/queries/sql/renderSql.cjs +321 -0
- package/build/queries/sql/renderSql.cjs.map +1 -0
- package/build/queries/sql/renderSql.d.cts +14 -0
- package/build/queries/sql/renderSql.d.cts.map +1 -0
- package/build/queries/sql/renderSql.d.mts +14 -0
- package/build/queries/sql/renderSql.d.mts.map +1 -0
- package/build/queries/sql/renderSql.js +318 -0
- package/build/queries/sql/renderSql.js.map +1 -0
- package/build/resurrection.cjs +5 -3
- package/build/resurrection.cjs.map +1 -1
- package/build/resurrection.d.cts.map +1 -1
- package/build/resurrection.d.mts.map +1 -1
- package/build/resurrection.js +5 -3
- package/build/resurrection.js.map +1 -1
- package/build/upsert.cjs +6 -3
- package/build/upsert.cjs.map +1 -1
- package/build/upsert.d.cts +6 -3
- package/build/upsert.d.cts.map +1 -1
- package/build/upsert.d.mts +6 -3
- package/build/upsert.d.mts.map +1 -1
- package/build/upsert.js +6 -3
- package/build/upsert.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { getBaseMeta } from "../../EntityMetadata.js";
|
|
2
|
-
import {
|
|
2
|
+
import { parseExpression } from "../ConditionBuilder.js";
|
|
3
3
|
import { predicateBrand } from "../conditions.js";
|
|
4
|
-
import { filterSoftDeletes,
|
|
4
|
+
import { filterSoftDeletes, stiSubtypeFilter } from "../entityQueryUtils.js";
|
|
5
5
|
import { isDeferredAliasCondition } from "../find/DeferredAlias.js";
|
|
6
|
-
import { deepFindConditions } from "../parsedConditions.js";
|
|
7
|
-
import { buildWhereClause } from "../renderConditions.js";
|
|
8
6
|
import { skipCondition } from "../skipCondition.js";
|
|
9
7
|
import { AliasAssigner } from "./AliasAssigner.js";
|
|
10
|
-
import { BaseExpr, TemplateExpr, asNode, deferredCondition, deferredSym, isExpr, orderByToSql,
|
|
8
|
+
import { BaseExpr, TemplateExpr, asNode, deferredCondition, deferredSym, isDeferredCondition, isExpr, orderByToSql, selectKeyBrand } from "./Expr.js";
|
|
11
9
|
import { buildExpr } from "./expressions/expression.js";
|
|
12
10
|
import { compileJoinTree } from "./JoinTree.js";
|
|
13
|
-
import { kq,
|
|
14
|
-
import {
|
|
11
|
+
import { kq, safeKq } from "./keywords.js";
|
|
12
|
+
import { columnSql, joinSql } from "./ParsedSqlExpression.js";
|
|
13
|
+
import { JoinTableHandle, collectionJoin, getTableMetadata, getTableMgmt, inQueryCondition, isEntityTable, isTable, m2mJoinTable, referenceJoinSource } from "./Tables.js";
|
|
15
14
|
import { fail } from "../../utils.js";
|
|
16
15
|
// src/queries/sql/query.ts
|
|
17
16
|
/**
|
|
@@ -159,7 +158,7 @@ sql.booleanArray = typedSql;
|
|
|
159
158
|
sql.booleanArrayOrNull = typedSql;
|
|
160
159
|
/** A raw condition for `where`, `having`, or `on`. */
|
|
161
160
|
sql.condition = function condition(strings, ...values) {
|
|
162
|
-
return deferredCondition((ctx) => new TemplateExpr(strings, values).
|
|
161
|
+
return deferredCondition((ctx) => new TemplateExpr(strings, values).parseSql(ctx));
|
|
163
162
|
};
|
|
164
163
|
/**
|
|
165
164
|
* Parses `arg` (a `Query` POJO or `query(...)` value) into a runnable `Plan`.
|
|
@@ -169,7 +168,8 @@ sql.condition = function condition(strings, ...values) {
|
|
|
169
168
|
*/
|
|
170
169
|
function parseRootQuery(arg) {
|
|
171
170
|
if (arg instanceof SubqueryExpr) fail("Scalar query values are expressions, not executable read inputs");
|
|
172
|
-
|
|
171
|
+
const assigner = new AliasAssigner();
|
|
172
|
+
return parseQuery(toQuery(arg), void 0, assigner);
|
|
173
173
|
}
|
|
174
174
|
/**
|
|
175
175
|
* Parses a read that sits inside another statement, i.e. an INSERT's SELECT source, so it resolves that
|
|
@@ -191,12 +191,9 @@ function projectionToSql(select, ctx, joins) {
|
|
|
191
191
|
const output = joinedOutput(projectionOutput(select), joins);
|
|
192
192
|
if (output.kind === "scalar") {
|
|
193
193
|
const expr = output.columns[0][1];
|
|
194
|
-
const fragment = expr.
|
|
194
|
+
const fragment = expr.parseSql(ctx);
|
|
195
195
|
return {
|
|
196
|
-
selects: [
|
|
197
|
-
...fragment,
|
|
198
|
-
sql: `${fragment.sql} AS value`
|
|
199
|
-
}],
|
|
196
|
+
selects: [joinSql([fragment, " AS value"])],
|
|
200
197
|
decodeRows: (_, rows) => rows.map((row) => decodeValue(expr, row.value)),
|
|
201
198
|
output
|
|
202
199
|
};
|
|
@@ -205,11 +202,8 @@ function projectionToSql(select, ctx, joins) {
|
|
|
205
202
|
return {
|
|
206
203
|
selects: entries.map((entry) => {
|
|
207
204
|
const [key, expr] = entry;
|
|
208
|
-
const fragment = expr.
|
|
209
|
-
return {
|
|
210
|
-
...fragment,
|
|
211
|
-
sql: `${fragment.sql} AS ${safeKq(key)}`
|
|
212
|
-
};
|
|
205
|
+
const fragment = expr.parseSql(ctx);
|
|
206
|
+
return joinSql([fragment, ` AS ${safeKq(key)}`]);
|
|
213
207
|
}),
|
|
214
208
|
decodeRows: (_, rows) => rows.map((row) => decodeRow(row, entries)),
|
|
215
209
|
output
|
|
@@ -290,13 +284,9 @@ var SubqueryColumnExpr = class extends BaseExpr {
|
|
|
290
284
|
get [selectKeyBrand]() {
|
|
291
285
|
return this.key;
|
|
292
286
|
}
|
|
293
|
-
|
|
287
|
+
parseSql(ctx) {
|
|
294
288
|
const alias = ctx.aliasFor(this.handle);
|
|
295
|
-
return
|
|
296
|
-
sql: `${safeKq(alias)}.${safeKq(this.key)}`,
|
|
297
|
-
bindings: [],
|
|
298
|
-
refs: [alias]
|
|
299
|
-
};
|
|
289
|
+
return columnSql(alias, this.key);
|
|
300
290
|
}
|
|
301
291
|
decode(value) {
|
|
302
292
|
return this.inner.decode(value);
|
|
@@ -337,21 +327,17 @@ var SubqueryExpr = class extends BaseExpr {
|
|
|
337
327
|
get sqlNullable() {
|
|
338
328
|
return true;
|
|
339
329
|
}
|
|
340
|
-
|
|
341
|
-
const bare = this.
|
|
342
|
-
return
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
330
|
+
parseSql(ctx) {
|
|
331
|
+
const bare = this.parseSqlBare(ctx);
|
|
332
|
+
return joinSql([
|
|
333
|
+
"(",
|
|
334
|
+
bare,
|
|
335
|
+
")"
|
|
336
|
+
]);
|
|
346
337
|
}
|
|
347
|
-
|
|
338
|
+
parseSqlBare(ctx) {
|
|
348
339
|
const parent = ctx instanceof Ctx ? ctx : fail("Subqueries need the query parser's context");
|
|
349
|
-
|
|
350
|
-
return {
|
|
351
|
-
sql: plan.sql,
|
|
352
|
-
bindings: plan.bindings,
|
|
353
|
-
refs: plan.outerRefs
|
|
354
|
-
};
|
|
340
|
+
return queryFragment(parseQuery(this.handle.q, parent, parent.assigner));
|
|
355
341
|
}
|
|
356
342
|
decode(value) {
|
|
357
343
|
return this.subquerySelect.decode(value);
|
|
@@ -621,8 +607,8 @@ var OutputExpr = class extends BaseExpr {
|
|
|
621
607
|
get sqlNullable() {
|
|
622
608
|
return this.nullable;
|
|
623
609
|
}
|
|
624
|
-
|
|
625
|
-
return this.inner.
|
|
610
|
+
parseSql(ctx) {
|
|
611
|
+
return this.inner.parseSql(ctx);
|
|
626
612
|
}
|
|
627
613
|
decode(value) {
|
|
628
614
|
return this.inner.decode(value);
|
|
@@ -643,33 +629,22 @@ function parseSetQuery(q, parent, assigner, recursiveSelf) {
|
|
|
643
629
|
if (recursiveSelf) ctx.setRecursiveSelf(recursiveSelf);
|
|
644
630
|
const ctes = registerCtes(q, ctx, assigner);
|
|
645
631
|
const plans = operands.map((operand) => parseQuery(toQuery(operand), ctx, assigner));
|
|
646
|
-
let sql = "";
|
|
647
|
-
for (const plan of plans) {
|
|
648
|
-
let branch = plan.sql;
|
|
649
|
-
if (output.columns.some(([key], i) => plan.output.columns[i][0] !== key)) {
|
|
650
|
-
const alias = safeKq(assigner.getLiteralAlias("sq"));
|
|
651
|
-
branch = `SELECT ${output.columns.map(([key]) => `${alias}.${safeKq(key)} AS ${safeKq(key)}`).join(", ")} FROM (${branch}) AS ${alias}`;
|
|
652
|
-
}
|
|
653
|
-
sql = sql ? `(${sql}) ${SET_OPERATIONS[operation]} (${branch})` : branch;
|
|
654
|
-
}
|
|
655
|
-
const orderBys = setOrderBys(q, output);
|
|
656
|
-
if (orderBys.length > 0) sql += ` ORDER BY ${orderBys.join(", ")}`;
|
|
657
|
-
const bindings = plans.flatMap((plan) => plan.bindings);
|
|
658
|
-
if (q.limit !== void 0) {
|
|
659
|
-
sql += " LIMIT ?";
|
|
660
|
-
bindings.push(q.limit);
|
|
661
|
-
}
|
|
662
|
-
if (q.offset !== void 0) {
|
|
663
|
-
sql += " OFFSET ?";
|
|
664
|
-
bindings.push(q.offset);
|
|
665
|
-
}
|
|
666
|
-
const withCtes = prependCtes(ctes, new Set(plans.flatMap((plan) => plan.outerRefs)), sql, bindings);
|
|
667
|
-
const cteAliases = new Set(ctes.map((cte) => cte.alias));
|
|
668
|
-
const outerRefs = [...ctx.outerRefs, ...plans.flatMap((plan) => plan.outerRefs)];
|
|
669
632
|
return {
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
633
|
+
query: {
|
|
634
|
+
kind: "set",
|
|
635
|
+
operation: SET_OPERATIONS[operation],
|
|
636
|
+
operands: plans.map((plan) => ({
|
|
637
|
+
query: plan.query,
|
|
638
|
+
alignment: output.columns.some(([key], i) => plan.output.columns[i][0] !== key) ? {
|
|
639
|
+
alias: assigner.getLiteralAlias("sq"),
|
|
640
|
+
columns: output.columns.map(([key]) => key)
|
|
641
|
+
} : void 0
|
|
642
|
+
})),
|
|
643
|
+
ctes,
|
|
644
|
+
orderBys: setOrderBys(q, output),
|
|
645
|
+
limit: q.limit,
|
|
646
|
+
offset: q.offset
|
|
647
|
+
},
|
|
673
648
|
output,
|
|
674
649
|
decodeRows: (_, rows) => rows.map((row) => decodeRow(row, output.columns))
|
|
675
650
|
};
|
|
@@ -692,9 +667,7 @@ function setOrderBys(q, output) {
|
|
|
692
667
|
/**
|
|
693
668
|
* What an expression needs from the query it is generating SQL for.
|
|
694
669
|
*
|
|
695
|
-
* Each (sub)query gets its own `Ctx`; a lookup that misses locally walks up to the enclosing query
|
|
696
|
-
* records the hit in `outerRefs`, which is how a correlated subquery reports the outer aliases it
|
|
697
|
-
* depends on (the way `ExistsCondition.outerAliases` does), so join pruning keeps them.
|
|
670
|
+
* Each (sub)query gets its own `Ctx`; a lookup that misses locally walks up to the enclosing query.
|
|
698
671
|
*/
|
|
699
672
|
var Ctx = class {
|
|
700
673
|
assigner;
|
|
@@ -716,7 +689,6 @@ var Ctx = class {
|
|
|
716
689
|
usedCtes = /* @__PURE__ */ new Set();
|
|
717
690
|
/** The recursive CTE whose body this query is part of, if any; see `recursiveSelf`. */
|
|
718
691
|
ownRecursiveSelf;
|
|
719
|
-
outerRefs = /* @__PURE__ */ new Set();
|
|
720
692
|
constructor(assigner, parent) {
|
|
721
693
|
this.assigner = assigner;
|
|
722
694
|
this.parent = parent;
|
|
@@ -743,9 +715,6 @@ var Ctx = class {
|
|
|
743
715
|
/**
|
|
744
716
|
* The CTE name for `handle`, looking in the enclosing queries too, because a CTE is in scope for the
|
|
745
717
|
* whole statement.
|
|
746
|
-
*
|
|
747
|
-
* Unlike `aliasFor`, a hit in an enclosing query is not added to `outerRefs`: reading a CTE by name
|
|
748
|
-
* is not a correlated reference, so it must not keep an enclosing join alive.
|
|
749
718
|
*/
|
|
750
719
|
cteAliasFor(handle) {
|
|
751
720
|
return this.ctes.get(handle) ?? this.parent?.cteAliasFor(handle);
|
|
@@ -781,17 +750,14 @@ var Ctx = class {
|
|
|
781
750
|
aliasFor(handle) {
|
|
782
751
|
const local = this.aliases.get(handle);
|
|
783
752
|
if (local) return local;
|
|
784
|
-
if (this.parent)
|
|
785
|
-
const outer = this.parent.aliasFor(handle);
|
|
786
|
-
this.outerRefs.add(outer);
|
|
787
|
-
return outer;
|
|
788
|
-
}
|
|
753
|
+
if (this.parent) return this.parent.aliasFor(handle);
|
|
789
754
|
return fail(`${describeHandle(handle)} is not in this query's from/join`);
|
|
790
755
|
}
|
|
791
|
-
|
|
792
|
-
return
|
|
756
|
+
parseCondition(cond) {
|
|
757
|
+
return parseCondition(cond, this, false);
|
|
793
758
|
}
|
|
794
759
|
};
|
|
760
|
+
/** Names a source in parsing errors. */
|
|
795
761
|
function describeHandle(handle) {
|
|
796
762
|
if (handle instanceof SubqueryHandle) return `Query value ${handle.describe()}`;
|
|
797
763
|
if (handle instanceof JoinTableHandle) return `Join table ${handle.joinTableName}`;
|
|
@@ -799,17 +765,19 @@ function describeHandle(handle) {
|
|
|
799
765
|
return "Table";
|
|
800
766
|
}
|
|
801
767
|
/**
|
|
802
|
-
* Parses one
|
|
768
|
+
* Parses one query occurrence into a resolved tree.
|
|
803
769
|
*
|
|
804
770
|
* 1. Register every source's runtime identity with its SQL alias in this parse's context.
|
|
805
|
-
* 2.
|
|
806
|
-
*
|
|
807
|
-
*
|
|
808
|
-
* 4. Assemble the SQL from the kept fragments, so pruned bindings disappear with their SQL.
|
|
771
|
+
* 2. Resolve sources, selects, conditions, group-bys, and order-bys against the context.
|
|
772
|
+
* 3. Return the complete AST to the statement-level plugin hook.
|
|
773
|
+
* Pruning, join validation, and SQL assembly happen later in renderQuery.
|
|
809
774
|
*/
|
|
810
775
|
function parseQuery(q, parent, assigner, recursiveSelf) {
|
|
811
776
|
validateReadQuery(q);
|
|
812
|
-
|
|
777
|
+
return isSetQuery(q) ? parseSetQuery(q, parent, assigner, recursiveSelf) : parseSelectQuery(q, parent, assigner, recursiveSelf);
|
|
778
|
+
}
|
|
779
|
+
/** Resolves SELECT clauses once, retaining child queries for the statement-level rewrite hook. */
|
|
780
|
+
function parseSelectQuery(q, parent, assigner, recursiveSelf) {
|
|
813
781
|
q = normalizeQueryJoins(q);
|
|
814
782
|
const ctx = new Ctx(assigner, parent);
|
|
815
783
|
if (recursiveSelf) ctx.setRecursiveSelf(recursiveSelf);
|
|
@@ -842,15 +810,14 @@ function parseQuery(q, parent, assigner, recursiveSelf) {
|
|
|
842
810
|
const from = parseFrom();
|
|
843
811
|
const userJoins = pendingJoins.map((j) => {
|
|
844
812
|
const source = j.parseSource();
|
|
845
|
-
const userOn =
|
|
813
|
+
const userOn = parseCondition(j.on, ctx, true);
|
|
846
814
|
const injected = injectedConditions(source, j.softDeletes ? softDeletes : "include");
|
|
847
|
-
const
|
|
815
|
+
const on = userOn && injected.length > 0 ? parseCondition({ and: [j.on, ...injected] }, ctx, true) : userOn;
|
|
848
816
|
return {
|
|
849
817
|
kind: j.kind,
|
|
850
818
|
keep: j.keep,
|
|
851
819
|
source,
|
|
852
|
-
|
|
853
|
-
fullOn
|
|
820
|
+
on
|
|
854
821
|
};
|
|
855
822
|
});
|
|
856
823
|
const cti = ctiEntityPlan(q, from, assigner);
|
|
@@ -859,77 +826,24 @@ function parseQuery(q, parent, assigner, recursiveSelf) {
|
|
|
859
826
|
const { selects, decodeRows, output } = selectsToSql(q, ctx, from, cti.selects ?? sti.selects);
|
|
860
827
|
const fromInjected = [...injectedConditions(from, softDeletes), ...sti.conditions];
|
|
861
828
|
const predicate = normalizeWhere(q.where);
|
|
862
|
-
const where = conditionToSql(fromInjected.length > 0 ? { and: [predicate, ...fromInjected] } : predicate, ctx, true);
|
|
863
|
-
const having = conditionToSql(q.having, ctx, true);
|
|
864
|
-
const groupBys = (q.groupBy ?? []).map((g) => asExpr(g, "groupBy").toSql(ctx));
|
|
865
|
-
const orderBys = orderBysToSql(q, ctx);
|
|
866
|
-
const { joins: kept, ctes: keptCtes } = pruneJoins(q, ctx, from, joins, ctes, [
|
|
867
|
-
...selects,
|
|
868
|
-
...groupBys,
|
|
869
|
-
...orderBys,
|
|
870
|
-
where,
|
|
871
|
-
having
|
|
872
|
-
].filter(isDefined));
|
|
873
|
-
const laterAliases = new Set(kept.map((j) => j.source.alias));
|
|
874
|
-
for (const j of kept) {
|
|
875
|
-
if (!j.userOn) fail(`Join ${describeHandle(j.source.handle)} has no ON condition left (they all pruned), but the query still references it`);
|
|
876
|
-
laterAliases.delete(j.source.alias);
|
|
877
|
-
const forward = j.fullOn.refs.find((r) => laterAliases.has(r));
|
|
878
|
-
if (forward) fail(`Join ${describeHandle(j.source.handle)} references '${forward}', which is joined later; move that join earlier in the join array`);
|
|
879
|
-
}
|
|
880
|
-
const out = [];
|
|
881
|
-
if (keptCtes.length > 0) out.push(withFragment(keptCtes));
|
|
882
|
-
out.push({
|
|
883
|
-
sql: `SELECT ${q.distinct ? "DISTINCT " : ""}`,
|
|
884
|
-
bindings: [],
|
|
885
|
-
refs: []
|
|
886
|
-
});
|
|
887
|
-
out.push(joinFragmentParts(selects, ", "));
|
|
888
|
-
out.push({
|
|
889
|
-
sql: ` FROM ${from.sql}`,
|
|
890
|
-
bindings: from.bindings,
|
|
891
|
-
refs: []
|
|
892
|
-
});
|
|
893
|
-
for (const j of kept) {
|
|
894
|
-
const keyword = j.kind === "inner" ? "JOIN" : "LEFT OUTER JOIN";
|
|
895
|
-
out.push({
|
|
896
|
-
sql: ` ${keyword} ${j.source.sql} ON ${j.fullOn.sql}`,
|
|
897
|
-
bindings: [...j.source.bindings, ...j.fullOn.bindings],
|
|
898
|
-
refs: []
|
|
899
|
-
});
|
|
900
|
-
}
|
|
901
|
-
if (where) out.push({
|
|
902
|
-
sql: ` WHERE ${where.sql}`,
|
|
903
|
-
bindings: where.bindings,
|
|
904
|
-
refs: []
|
|
905
|
-
});
|
|
906
|
-
if (groupBys.length > 0) out.push({
|
|
907
|
-
...joinFragmentParts(groupBys, ", "),
|
|
908
|
-
sql: ` GROUP BY ${groupBys.map((g) => g.sql).join(", ")}`
|
|
909
|
-
});
|
|
910
|
-
if (having) out.push({
|
|
911
|
-
sql: ` HAVING ${having.sql}`,
|
|
912
|
-
bindings: having.bindings,
|
|
913
|
-
refs: []
|
|
914
|
-
});
|
|
915
|
-
if (orderBys.length > 0) out.push({
|
|
916
|
-
...joinFragmentParts(orderBys, ", "),
|
|
917
|
-
sql: ` ORDER BY ${orderBys.map((o) => o.sql).join(", ")}`
|
|
918
|
-
});
|
|
919
|
-
if (q.limit !== void 0) out.push({
|
|
920
|
-
sql: ` LIMIT ?`,
|
|
921
|
-
bindings: [q.limit],
|
|
922
|
-
refs: []
|
|
923
|
-
});
|
|
924
|
-
if (q.offset !== void 0) out.push({
|
|
925
|
-
sql: ` OFFSET ?`,
|
|
926
|
-
bindings: [q.offset],
|
|
927
|
-
refs: []
|
|
928
|
-
});
|
|
929
829
|
return {
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
830
|
+
query: {
|
|
831
|
+
kind: "select",
|
|
832
|
+
selects,
|
|
833
|
+
from,
|
|
834
|
+
joins,
|
|
835
|
+
ctes,
|
|
836
|
+
where: parseCondition(fromInjected.length > 0 ? { and: [predicate, ...fromInjected] } : predicate, ctx, true),
|
|
837
|
+
having: parseCondition(q.having, ctx, true),
|
|
838
|
+
groupBys: (q.groupBy ?? []).map((g) => asExpr(g, "groupBy").parseSql(ctx)),
|
|
839
|
+
orderBys: orderBysToSql(q, ctx),
|
|
840
|
+
entityMetadata: output.kind === "entity" ? from.meta : void 0,
|
|
841
|
+
distinct: q.distinct ?? false,
|
|
842
|
+
limit: q.limit,
|
|
843
|
+
offset: q.offset,
|
|
844
|
+
pruneJoins: q.pruneJoins !== false,
|
|
845
|
+
recursiveAlias: [from, ...joins.map((join) => join.source)].find((source) => source.handle === ctx.recursiveSelf)?.alias
|
|
846
|
+
},
|
|
933
847
|
output,
|
|
934
848
|
decodeRows
|
|
935
849
|
};
|
|
@@ -946,12 +860,10 @@ function registerSource(source, ctx, assigner) {
|
|
|
946
860
|
if (cteAlias) {
|
|
947
861
|
ctx.useCte(handle);
|
|
948
862
|
return () => ({
|
|
863
|
+
kind: "cte",
|
|
949
864
|
handle,
|
|
950
865
|
alias: cteAlias,
|
|
951
|
-
|
|
952
|
-
bindings: [],
|
|
953
|
-
refs: [],
|
|
954
|
-
entitySelects: [],
|
|
866
|
+
cte: cteAlias,
|
|
955
867
|
meta: void 0
|
|
956
868
|
});
|
|
957
869
|
}
|
|
@@ -962,12 +874,10 @@ function registerSource(source, ctx, assigner) {
|
|
|
962
874
|
return () => {
|
|
963
875
|
const inner = parseQuery(handle.q, ctx, assigner);
|
|
964
876
|
return {
|
|
877
|
+
kind: "query",
|
|
965
878
|
handle,
|
|
966
879
|
alias,
|
|
967
|
-
|
|
968
|
-
bindings: inner.bindings,
|
|
969
|
-
refs: inner.outerRefs,
|
|
970
|
-
entitySelects: [],
|
|
880
|
+
query: inner.query,
|
|
971
881
|
meta: void 0
|
|
972
882
|
};
|
|
973
883
|
};
|
|
@@ -977,18 +887,14 @@ function registerSource(source, ctx, assigner) {
|
|
|
977
887
|
const meta = "meta" in mgmt ? mgmt.meta : void 0;
|
|
978
888
|
const alias = assigner.getAlias(tableName);
|
|
979
889
|
ctx.register(handle, alias);
|
|
980
|
-
return () => {
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
entitySelects,
|
|
989
|
-
meta
|
|
990
|
-
};
|
|
991
|
-
};
|
|
890
|
+
return () => ({
|
|
891
|
+
kind: "table",
|
|
892
|
+
handle,
|
|
893
|
+
alias,
|
|
894
|
+
table: tableName,
|
|
895
|
+
schema: "schema" in mgmt ? mgmt.schema : void 0,
|
|
896
|
+
meta
|
|
897
|
+
});
|
|
992
898
|
}
|
|
993
899
|
}
|
|
994
900
|
/**
|
|
@@ -1022,12 +928,10 @@ function registerJoinTable(handle, ctx, assigner) {
|
|
|
1022
928
|
const alias = assigner.getAlias(handle.joinTableName);
|
|
1023
929
|
ctx.register(handle, alias);
|
|
1024
930
|
return () => ({
|
|
931
|
+
kind: "table",
|
|
1025
932
|
handle,
|
|
1026
933
|
alias,
|
|
1027
|
-
|
|
1028
|
-
bindings: [],
|
|
1029
|
-
refs: [],
|
|
1030
|
-
entitySelects: [],
|
|
934
|
+
table: handle.joinTableName,
|
|
1031
935
|
meta: void 0
|
|
1032
936
|
});
|
|
1033
937
|
}
|
|
@@ -1037,14 +941,9 @@ function selectsToSql(q, ctx, from, entitySelects) {
|
|
|
1037
941
|
if (isTable(select) && !isEntityTable(select)) fail("A custom table cannot be selected as an entity; select its columns individually");
|
|
1038
942
|
if (isEntityTable(select)) {
|
|
1039
943
|
if (from.handle !== getTableMgmt(select)) fail("Selecting a joined table is not supported yet; select the from table, or select its columns individually");
|
|
1040
|
-
const alias = ctx.aliasFor(getTableMgmt(select));
|
|
1041
944
|
const meta = getTableMetadata(select);
|
|
1042
945
|
return {
|
|
1043
|
-
selects: entitySelects ?? from.
|
|
1044
|
-
sql: s,
|
|
1045
|
-
bindings: [],
|
|
1046
|
-
refs: [alias]
|
|
1047
|
-
})),
|
|
946
|
+
selects: entitySelects ?? entitySelectFragments(meta, from.alias),
|
|
1048
947
|
decodeRows: (em, rows) => em.hydrate(meta.cstr, rows),
|
|
1049
948
|
output: {
|
|
1050
949
|
kind: "entity",
|
|
@@ -1057,11 +956,7 @@ function selectsToSql(q, ctx, from, entitySelects) {
|
|
|
1057
956
|
const alias = ctx.aliasFor(handle);
|
|
1058
957
|
const output = handle.output();
|
|
1059
958
|
return {
|
|
1060
|
-
selects: output.columns.map(([k]) => ({
|
|
1061
|
-
sql: `${safeKq(alias)}.${safeKq(k)} AS ${safeKq(k)}`,
|
|
1062
|
-
bindings: [],
|
|
1063
|
-
refs: [alias]
|
|
1064
|
-
})),
|
|
959
|
+
selects: output.columns.map(([k]) => joinSql([columnSql(alias, k), ` AS ${safeKq(k)}`])),
|
|
1065
960
|
decodeRows: (_, rows) => rows.map((row) => decodeRow(row, output.columns)),
|
|
1066
961
|
output
|
|
1067
962
|
};
|
|
@@ -1087,11 +982,7 @@ function ctiEntityPlan(q, from, assigner) {
|
|
|
1087
982
|
selects: void 0
|
|
1088
983
|
};
|
|
1089
984
|
const joins = [];
|
|
1090
|
-
const selects = from.
|
|
1091
|
-
sql,
|
|
1092
|
-
bindings: [],
|
|
1093
|
-
refs: [from.alias]
|
|
1094
|
-
}));
|
|
985
|
+
const selects = entitySelectFragments(meta, from.alias);
|
|
1095
986
|
for (const [i, baseMeta] of meta.baseTypes.entries()) {
|
|
1096
987
|
const alias = assigner.getLiteralAlias(`${from.alias}_b${i}`);
|
|
1097
988
|
joins.push(ctiJoin(from, baseMeta, alias));
|
|
@@ -1111,26 +1002,27 @@ function ctiEntityPlan(q, from, assigner) {
|
|
|
1111
1002
|
}
|
|
1112
1003
|
}
|
|
1113
1004
|
}
|
|
1114
|
-
selects.push({
|
|
1115
|
-
sql: `${safeKq(from.alias)}.${kq("id")} AS ${kq("id")}`,
|
|
1116
|
-
bindings: [],
|
|
1117
|
-
refs: [from.alias]
|
|
1118
|
-
});
|
|
1005
|
+
selects.push(joinSql([columnSql(from.alias, "id"), ` AS ${kq("id")}`]));
|
|
1119
1006
|
for (const [column, aliases] of subtypeColumns) {
|
|
1120
1007
|
if (aliases.length < 2) continue;
|
|
1121
|
-
selects.push(
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1008
|
+
selects.push(joinSql([
|
|
1009
|
+
"COALESCE(",
|
|
1010
|
+
joinSql(aliases.map((alias) => columnSql(alias, column)), ", "),
|
|
1011
|
+
`) AS ${kq(column)}`
|
|
1012
|
+
]));
|
|
1126
1013
|
}
|
|
1127
1014
|
if (meta.subTypes.length > 0) {
|
|
1128
|
-
const
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1015
|
+
const branches = joins.slice(meta.baseTypes.length).map((join) => join.source.alias).map((alias, i) => joinSql([
|
|
1016
|
+
"WHEN ",
|
|
1017
|
+
columnSql(alias, "id"),
|
|
1018
|
+
" IS NOT NULL THEN ",
|
|
1019
|
+
{ value: meta.subTypes[i].type }
|
|
1020
|
+
]));
|
|
1021
|
+
selects.push(joinSql([
|
|
1022
|
+
"CASE ",
|
|
1023
|
+
joinSql(branches, " "),
|
|
1024
|
+
` ELSE '_' END AS ${kq("__class")}`
|
|
1025
|
+
]));
|
|
1134
1026
|
}
|
|
1135
1027
|
return {
|
|
1136
1028
|
joins,
|
|
@@ -1148,18 +1040,10 @@ function stiEntityPlan(q, from) {
|
|
|
1148
1040
|
selects: void 0,
|
|
1149
1041
|
conditions: []
|
|
1150
1042
|
};
|
|
1151
|
-
const selects = from.
|
|
1152
|
-
|
|
1153
|
-
bindings: [],
|
|
1154
|
-
refs: [from.alias]
|
|
1155
|
-
}));
|
|
1156
|
-
if (!(from.entitySelects.length === 1 && from.entitySelects[0] === kqStar(from.alias))) {
|
|
1043
|
+
const selects = entitySelectFragments(meta, from.alias);
|
|
1044
|
+
if (!(selects.length === 1 && selects[0].kind === "star")) {
|
|
1157
1045
|
const discriminator = getBaseMeta(meta).stiDiscriminatorColumnName;
|
|
1158
|
-
selects.push({
|
|
1159
|
-
sql: `${safeKq(from.alias)}.${kq(discriminator)} AS ${kq(discriminator)}`,
|
|
1160
|
-
bindings: [],
|
|
1161
|
-
refs: [from.alias]
|
|
1162
|
-
});
|
|
1046
|
+
selects.push(joinSql([columnSql(from.alias, discriminator), ` AS ${kq(discriminator)}`]));
|
|
1163
1047
|
}
|
|
1164
1048
|
const condition = stiSubtypeFilter(meta, from.alias);
|
|
1165
1049
|
return {
|
|
@@ -1169,37 +1053,37 @@ function stiEntityPlan(q, from) {
|
|
|
1169
1053
|
}
|
|
1170
1054
|
/** Creates one mandatory CTI join through the selected source's primary key. */
|
|
1171
1055
|
function ctiJoin(from, meta, alias) {
|
|
1172
|
-
const on =
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1056
|
+
const on = joinSql([
|
|
1057
|
+
columnSql(from.alias, "id"),
|
|
1058
|
+
" = ",
|
|
1059
|
+
columnSql(alias, "id")
|
|
1060
|
+
]);
|
|
1177
1061
|
return {
|
|
1178
1062
|
kind: "left",
|
|
1179
1063
|
source: {
|
|
1064
|
+
kind: "table",
|
|
1180
1065
|
handle: {
|
|
1181
1066
|
tableName: meta.tableName,
|
|
1182
1067
|
meta
|
|
1183
1068
|
},
|
|
1184
1069
|
alias,
|
|
1185
|
-
|
|
1186
|
-
bindings: [],
|
|
1187
|
-
refs: [],
|
|
1188
|
-
entitySelects: [],
|
|
1070
|
+
table: meta.tableName,
|
|
1189
1071
|
meta
|
|
1190
1072
|
},
|
|
1191
|
-
|
|
1192
|
-
fullOn: on,
|
|
1073
|
+
on,
|
|
1193
1074
|
keep: true
|
|
1194
1075
|
};
|
|
1195
1076
|
}
|
|
1196
1077
|
/** Selects one physical CTI table while preserving lazy-field exclusions. */
|
|
1197
1078
|
function entitySelectFragments(meta, alias) {
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1079
|
+
if (!meta.hasLazyColumns) return [{
|
|
1080
|
+
kind: "star",
|
|
1081
|
+
alias
|
|
1082
|
+
}];
|
|
1083
|
+
return Object.values(meta.fields).flatMap((field) => {
|
|
1084
|
+
if (!field.serde || field.kind === "primitive" && field.lazy) return [];
|
|
1085
|
+
return field.serde.columns.map((column) => columnSql(alias, column.columnName));
|
|
1086
|
+
});
|
|
1203
1087
|
}
|
|
1204
1088
|
function decodeRow(row, decoders) {
|
|
1205
1089
|
const result = {};
|
|
@@ -1251,19 +1135,12 @@ function orderBysToSql(q, ctx) {
|
|
|
1251
1135
|
if (isTable(select)) {
|
|
1252
1136
|
const column = select[key];
|
|
1253
1137
|
if (!isExpr(column)) return fail(`orderBy key '${key}' is not a sortable column of the table`);
|
|
1254
|
-
const fragment = asNode(column).
|
|
1255
|
-
result.push({
|
|
1256
|
-
...fragment,
|
|
1257
|
-
sql: `${fragment.sql} ${dir}`
|
|
1258
|
-
});
|
|
1138
|
+
const fragment = asNode(column).parseSql(ctx);
|
|
1139
|
+
result.push(joinSql([fragment, ` ${dir}`]));
|
|
1259
1140
|
} else {
|
|
1260
1141
|
if (isExpr(select)) return fail(`the keyed orderBy form needs a POJO or entity select`);
|
|
1261
1142
|
if (!(isSubqueryValue(select) ? select[subqueryBrand].columnKeys() : projectionOutput(select).columns.map(([key]) => key)).includes(key)) return fail(`orderBy key '${key}' is not a key of select`);
|
|
1262
|
-
result.push({
|
|
1263
|
-
sql: `${safeKq(key)} ${dir}`,
|
|
1264
|
-
bindings: [],
|
|
1265
|
-
refs: []
|
|
1266
|
-
});
|
|
1143
|
+
result.push(joinSql([`${safeKq(key)} ${dir}`]));
|
|
1267
1144
|
}
|
|
1268
1145
|
}
|
|
1269
1146
|
}
|
|
@@ -1275,23 +1152,15 @@ function orderBysToSql(q, ctx) {
|
|
|
1275
1152
|
* `pruneIfUndefined` applies unchanged. Deferred SQL expression conditions are resolved
|
|
1276
1153
|
* against the context first.
|
|
1277
1154
|
*/
|
|
1278
|
-
function
|
|
1155
|
+
function parseCondition(cond, ctx, topLevel) {
|
|
1279
1156
|
checkCondition(cond);
|
|
1280
1157
|
if (cond === void 0) return void 0;
|
|
1281
|
-
const resolved =
|
|
1158
|
+
const resolved = resolveQueryCondition(cond, ctx);
|
|
1282
1159
|
if (resolved === void 0) return void 0;
|
|
1283
1160
|
const filter = isFilter(resolved) ? resolved : { and: [resolved] };
|
|
1284
|
-
const
|
|
1285
|
-
cb.maybeAddExpression(filter);
|
|
1286
|
-
const parsed = cb.toExpressionFilter();
|
|
1161
|
+
const parsed = parseExpression(filter);
|
|
1287
1162
|
if (!parsed) return void 0;
|
|
1288
|
-
|
|
1289
|
-
if (!where) return void 0;
|
|
1290
|
-
return {
|
|
1291
|
-
sql: where[0],
|
|
1292
|
-
bindings: where[1],
|
|
1293
|
-
refs: refsOf(parsed)
|
|
1294
|
-
};
|
|
1163
|
+
return conditionFragment(parsed, topLevel);
|
|
1295
1164
|
}
|
|
1296
1165
|
/**
|
|
1297
1166
|
* Rejects malformed predicates throughout a condition tree before pruning can discard a restriction.
|
|
@@ -1301,6 +1170,15 @@ function conditionToSql(cond, ctx, topLevel) {
|
|
|
1301
1170
|
function checkCondition(value) {
|
|
1302
1171
|
if (isDeferredAliasCondition(value) || value && typeof value === "object" && predicateBrand in value && value[predicateBrand] === "domain") fail("Domain alias conditions are only supported by em.find; use table(...) predicates in SQL queries and mutations.");
|
|
1303
1172
|
if (value === void 0 || value === skipCondition) return;
|
|
1173
|
+
if (isDeferredCondition(value)) {
|
|
1174
|
+
checkPojoKeys(value, [
|
|
1175
|
+
"kind",
|
|
1176
|
+
deferredSym,
|
|
1177
|
+
predicateBrand
|
|
1178
|
+
], "Query deferred condition");
|
|
1179
|
+
if (value.kind !== "deferred" || typeof value[deferredSym] !== "function") fail("Malformed query deferred condition");
|
|
1180
|
+
return;
|
|
1181
|
+
}
|
|
1304
1182
|
if (isInQueryCondition(value)) {
|
|
1305
1183
|
checkCondition(normalizeWhere(normalizeQueryJoins(toQuery(value[inQueryCondition].query)).where));
|
|
1306
1184
|
return;
|
|
@@ -1399,13 +1277,17 @@ function checkPojoKeys(value, allowed, description) {
|
|
|
1399
1277
|
* Keeps each subquery's projection and reports its outer aliases for join pruning.
|
|
1400
1278
|
*/
|
|
1401
1279
|
function resolveQueryCondition(cond, ctx) {
|
|
1402
|
-
if (cond === void 0) return void 0;
|
|
1280
|
+
if (cond === void 0 || cond === skipCondition) return void 0;
|
|
1281
|
+
if (isDeferredCondition(cond)) return {
|
|
1282
|
+
kind: "expression",
|
|
1283
|
+
expression: cond[deferredSym](ctx)
|
|
1284
|
+
};
|
|
1403
1285
|
if (isInQueryCondition(cond)) {
|
|
1404
1286
|
const { column, query } = cond[inQueryCondition];
|
|
1405
1287
|
const value = createMaybeQueryValue(query);
|
|
1406
1288
|
if (value === void 0) return void 0;
|
|
1407
1289
|
if (!isExpr(value)) return fail("An in query literal must select one expression");
|
|
1408
|
-
return column.in(value);
|
|
1290
|
+
return resolveQueryCondition(column.in(value), ctx);
|
|
1409
1291
|
}
|
|
1410
1292
|
if ("and" in cond && cond.and) return {
|
|
1411
1293
|
and: cond.and.map((child) => resolveQueryCondition(child, ctx)),
|
|
@@ -1421,11 +1303,12 @@ function resolveQueryCondition(cond, ctx) {
|
|
|
1421
1303
|
if (subquery === void 0) return void 0;
|
|
1422
1304
|
const plan = parseQuery(toQuery(subquery), ctx, ctx.assigner);
|
|
1423
1305
|
return {
|
|
1424
|
-
kind: "
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1306
|
+
kind: "expression",
|
|
1307
|
+
expression: joinSql([
|
|
1308
|
+
`${positive ? "EXISTS" : "NOT EXISTS"} (`,
|
|
1309
|
+
queryFragment(plan),
|
|
1310
|
+
")"
|
|
1311
|
+
])
|
|
1429
1312
|
};
|
|
1430
1313
|
}
|
|
1431
1314
|
return cond;
|
|
@@ -1454,60 +1337,6 @@ function isPrunedQueryCondition(cond) {
|
|
|
1454
1337
|
function isInQueryCondition(value) {
|
|
1455
1338
|
return typeof value === "object" && value !== null && inQueryCondition in value;
|
|
1456
1339
|
}
|
|
1457
|
-
/** The physical source aliases a parsed condition tree references. */
|
|
1458
|
-
function refsOf(parsed) {
|
|
1459
|
-
return deepFindConditions(parsed, false).flatMap((c) => c.kind === "column" ? [c.alias] : c.kind === "raw" ? c.aliases : c.outerAliases);
|
|
1460
|
-
}
|
|
1461
|
-
/**
|
|
1462
|
-
* Pruning: em.find's paradigm, on a flat join list.
|
|
1463
|
-
*
|
|
1464
|
-
* A condition given `undefined` was already dropped by `ConditionBuilder`. Now a join that nothing
|
|
1465
|
-
* references anymore drops with it: a join is required if the source, a select, a surviving condition,
|
|
1466
|
-
* a group-by, an order-by, or another required join's ON references it, or if it is pinned with
|
|
1467
|
-
* `keep: true`. Marking follows ON dependencies transitively, exactly like `pruneUnusedJoins`'s
|
|
1468
|
-
* `DependencyTracker`.
|
|
1469
|
-
*
|
|
1470
|
-
* em.find's joins almost never filter rows by themselves, so pruning them is semantics-preserving. An
|
|
1471
|
-
* explicit `{ inner: b, on }` here does filter rows, so pruning it when unreferenced drops that filter;
|
|
1472
|
-
* that matches `{ books: { title: undefined } }` in em.find and is deliberate. `keep: true` pins it, and
|
|
1473
|
-
* a pure existence filter is better written as `a.id.in(query({ ... }))`, which is never `undefined`.
|
|
1474
|
-
*
|
|
1475
|
-
* CTEs prune on the same rule and through the same dependency map: a `with` entry nothing reads
|
|
1476
|
-
* anymore drops with the join that read it, and a CTE read only by another CTE survives with it.
|
|
1477
|
-
* A CTE joined into the query shares its alias with that join, so their dependencies are merged.
|
|
1478
|
-
*
|
|
1479
|
-
* The one join that never prunes is a recursive term's reference to its own CTE: PostgreSQL requires
|
|
1480
|
-
* it, so it is not the caller's optional filter (see `Ctx.recursiveSelf`).
|
|
1481
|
-
*/
|
|
1482
|
-
function pruneJoins(q, ctx, from, joins, ctes, used) {
|
|
1483
|
-
if (q.pruneJoins === false) return {
|
|
1484
|
-
joins,
|
|
1485
|
-
ctes
|
|
1486
|
-
};
|
|
1487
|
-
const deps = /* @__PURE__ */ new Map();
|
|
1488
|
-
function addDeps(alias, refs) {
|
|
1489
|
-
const own = refs.filter((r) => r !== alias);
|
|
1490
|
-
const existing = deps.get(alias);
|
|
1491
|
-
if (existing) existing.push(...own);
|
|
1492
|
-
else deps.set(alias, own);
|
|
1493
|
-
}
|
|
1494
|
-
for (const j of joins) addDeps(j.source.alias, [...j.userOn?.refs ?? [], ...j.source.refs]);
|
|
1495
|
-
for (const c of ctes) addDeps(c.alias, c.plan.outerRefs);
|
|
1496
|
-
const required = /* @__PURE__ */ new Set();
|
|
1497
|
-
function markRequired(alias) {
|
|
1498
|
-
if (required.has(alias)) return;
|
|
1499
|
-
required.add(alias);
|
|
1500
|
-
for (const dep of deps.get(alias) ?? []) markRequired(dep);
|
|
1501
|
-
}
|
|
1502
|
-
markRequired(from.alias);
|
|
1503
|
-
for (const r of used.flatMap((u) => u.refs)) markRequired(r);
|
|
1504
|
-
for (const j of joins) if (j.keep) markRequired(j.source.alias);
|
|
1505
|
-
for (const j of joins) if (j.source.handle === ctx.recursiveSelf) markRequired(j.source.alias);
|
|
1506
|
-
return {
|
|
1507
|
-
joins: joins.filter((j) => required.has(j.source.alias)),
|
|
1508
|
-
ctes: ctes.filter((c) => required.has(c.alias))
|
|
1509
|
-
};
|
|
1510
|
-
}
|
|
1511
1340
|
/**
|
|
1512
1341
|
* Names and parses each `with` entry, in declaration order.
|
|
1513
1342
|
*
|
|
@@ -1527,7 +1356,7 @@ function registerCtes(q, ctx, assigner) {
|
|
|
1527
1356
|
if (!handle.recursive) ctx.promoteCte(handle);
|
|
1528
1357
|
return {
|
|
1529
1358
|
alias: aliases[i],
|
|
1530
|
-
plan,
|
|
1359
|
+
query: plan.query,
|
|
1531
1360
|
recursive: handle.recursive
|
|
1532
1361
|
};
|
|
1533
1362
|
});
|
|
@@ -1537,54 +1366,6 @@ function withEntryHandle(entry) {
|
|
|
1537
1366
|
if (!isSubqueryValue(entry)) fail(entry instanceof SubqueryExpr || isEntityQueryValue(entry) ? "A `with` entry needs named columns; entity and scalar query(...) values have none" : "A `with` entry must be a query(...) value");
|
|
1538
1367
|
return readValueHandle(entry);
|
|
1539
1368
|
}
|
|
1540
|
-
/**
|
|
1541
|
-
* Puts a statement's surviving `with` entries in front of it, dropping the ones nothing reads.
|
|
1542
|
-
*
|
|
1543
|
-
* `referenced` is the aliases the rest of the statement used. The WITH clause leads the statement, so
|
|
1544
|
-
* its bindings lead the statement's bindings too.
|
|
1545
|
-
*/
|
|
1546
|
-
function prependCtes(ctes, referenced, sql, bindings) {
|
|
1547
|
-
const kept = pruneCtes(ctes, referenced);
|
|
1548
|
-
if (kept.length === 0) return {
|
|
1549
|
-
sql,
|
|
1550
|
-
bindings
|
|
1551
|
-
};
|
|
1552
|
-
const clause = withFragment(kept);
|
|
1553
|
-
return {
|
|
1554
|
-
sql: clause.sql + sql,
|
|
1555
|
-
bindings: [...clause.bindings, ...bindings]
|
|
1556
|
-
};
|
|
1557
|
-
}
|
|
1558
|
-
/**
|
|
1559
|
-
* Drops the CTEs that nothing reads, given the aliases the rest of the statement referenced.
|
|
1560
|
-
*
|
|
1561
|
-
* A CTE can only read earlier siblings, so going last to first means every CTE that could read this one
|
|
1562
|
-
* was already visited, and its own reads are already in `referenced`. That makes one pass enough; first
|
|
1563
|
-
* to last would have to repeat until the set stopped growing. `unshift` restores WITH order.
|
|
1564
|
-
*/
|
|
1565
|
-
function pruneCtes(ctes, referenced) {
|
|
1566
|
-
const kept = [];
|
|
1567
|
-
for (let i = ctes.length - 1; i >= 0; i--) {
|
|
1568
|
-
const cte = ctes[i];
|
|
1569
|
-
if (!referenced.has(cte.alias)) continue;
|
|
1570
|
-
for (const ref of cte.plan.outerRefs) referenced.add(ref);
|
|
1571
|
-
kept.unshift(cte);
|
|
1572
|
-
}
|
|
1573
|
-
return kept;
|
|
1574
|
-
}
|
|
1575
|
-
/**
|
|
1576
|
-
* Renders `WITH a AS (...), b AS (...) `, whose bindings lead the statement, as their SQL does.
|
|
1577
|
-
*
|
|
1578
|
-
* One recursive CTE makes the whole clause `WITH RECURSIVE`, PostgreSQL's rule: the keyword is on the
|
|
1579
|
-
* clause, not on the entry that needs it, and it does not force the other entries to be recursive.
|
|
1580
|
-
*/
|
|
1581
|
-
function withFragment(ctes) {
|
|
1582
|
-
return {
|
|
1583
|
-
sql: `WITH${ctes.some((c) => c.recursive) ? " RECURSIVE" : ""} ${ctes.map((c) => `${safeKq(c.alias)} AS (${c.plan.sql})`).join(", ")} `,
|
|
1584
|
-
bindings: ctes.flatMap((c) => c.plan.bindings),
|
|
1585
|
-
refs: []
|
|
1586
|
-
};
|
|
1587
|
-
}
|
|
1588
1369
|
function asExpr(value, where) {
|
|
1589
1370
|
if (value instanceof BaseExpr) return value;
|
|
1590
1371
|
return fail(`${where} must be an expression, i.e. a table column, aggregate, sql\`...\`, or scalar query(...)`);
|
|
@@ -1595,13 +1376,6 @@ function asSelectExpr(value, where) {
|
|
|
1595
1376
|
if (isPlainSelect(value)) return buildExpr(value);
|
|
1596
1377
|
return fail(`${where} must be a column, an expression, or an expression literal`);
|
|
1597
1378
|
}
|
|
1598
|
-
function joinFragmentParts(parts, sep) {
|
|
1599
|
-
return {
|
|
1600
|
-
sql: parts.map((p) => p.sql).join(sep),
|
|
1601
|
-
bindings: parts.flatMap((p) => p.bindings),
|
|
1602
|
-
refs: []
|
|
1603
|
-
};
|
|
1604
|
-
}
|
|
1605
1379
|
function isDefined(value) {
|
|
1606
1380
|
return value !== void 0;
|
|
1607
1381
|
}
|
|
@@ -1631,6 +1405,21 @@ function createQueryValue(q) {
|
|
|
1631
1405
|
function typedSql(strings, ...values) {
|
|
1632
1406
|
return new TemplateExpr(strings, values);
|
|
1633
1407
|
}
|
|
1634
|
-
|
|
1408
|
+
/** Retains the parsed condition tree until nested query rewrites are complete. */
|
|
1409
|
+
function conditionFragment(filter, topLevel) {
|
|
1410
|
+
return {
|
|
1411
|
+
kind: "condition",
|
|
1412
|
+
condition: filter,
|
|
1413
|
+
topLevel
|
|
1414
|
+
};
|
|
1415
|
+
}
|
|
1416
|
+
/** Embeds a parsed child by reference for rendering after the statement hook. */
|
|
1417
|
+
function queryFragment(plan) {
|
|
1418
|
+
return {
|
|
1419
|
+
kind: "query",
|
|
1420
|
+
query: plan.query
|
|
1421
|
+
};
|
|
1422
|
+
}
|
|
1423
|
+
export { Ctx, SubqueryHandle, checkPojoKeys, entityQueryBrand, injectedConditions, isReadQueryValue, parseCondition, parseNestedQuery, parseRootQuery, projectionToSql, query, queryFragment, queryMaybe, recursiveQuery, registerCtes, scalarQueryBrand, sql, subqueryBrand };
|
|
1635
1424
|
|
|
1636
1425
|
//# sourceMappingURL=query.js.map
|