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
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_queries_find_buildFindQuery = require("../find/buildFindQuery.cjs");
|
|
3
|
+
const require_queries_renderConditions = require("../renderConditions.cjs");
|
|
4
|
+
const require_queries_sql_keywords = require("./keywords.cjs");
|
|
5
|
+
const require_queries_sql_ParsedQuery = require("./ParsedQuery.cjs");
|
|
6
|
+
const require_queries_sql_ParsedSqlExpression = require("./ParsedSqlExpression.cjs");
|
|
7
|
+
const require_queries_sql_Tables = require("./Tables.cjs");
|
|
8
|
+
const require_utils = require("../../utils.cjs");
|
|
9
|
+
// src/queries/sql/renderSql.ts
|
|
10
|
+
/** Prunes unused joins/CTEs, validates joins, and renders a read statement. */
|
|
11
|
+
function renderQuery(query) {
|
|
12
|
+
return renderSql({
|
|
13
|
+
kind: "query",
|
|
14
|
+
query
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/** Prunes unused joins/CTEs, validates joins, and renders a read or mutation statement. */
|
|
18
|
+
function renderStatement(query) {
|
|
19
|
+
return query.kind === "select" || query.kind === "set" ? renderQuery(query) : renderSql(buildStatementSql(query));
|
|
20
|
+
}
|
|
21
|
+
/** Renders an expression and its bindings, pruning and validating any embedded queries. */
|
|
22
|
+
function renderSql(node) {
|
|
23
|
+
let result;
|
|
24
|
+
switch (node.kind) {
|
|
25
|
+
case "column":
|
|
26
|
+
result = {
|
|
27
|
+
sql: `${require_queries_sql_keywords.safeKq(node.alias)}.${require_queries_sql_keywords.safeKq(node.column)}`,
|
|
28
|
+
bindings: []
|
|
29
|
+
};
|
|
30
|
+
break;
|
|
31
|
+
case "star":
|
|
32
|
+
result = {
|
|
33
|
+
sql: `${require_queries_sql_keywords.safeKq(node.alias)}.*`,
|
|
34
|
+
bindings: []
|
|
35
|
+
};
|
|
36
|
+
break;
|
|
37
|
+
case "joined":
|
|
38
|
+
case "raw": {
|
|
39
|
+
const parts = node.parts.map((part) => typeof part === "string" ? {
|
|
40
|
+
sql: part,
|
|
41
|
+
bindings: []
|
|
42
|
+
} : "kind" in part ? renderSql(part) : {
|
|
43
|
+
sql: "?",
|
|
44
|
+
bindings: [part.value]
|
|
45
|
+
});
|
|
46
|
+
result = {
|
|
47
|
+
sql: parts.map((part) => part.sql).join(""),
|
|
48
|
+
bindings: parts.flatMap((part) => part.bindings)
|
|
49
|
+
};
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case "query":
|
|
53
|
+
result = renderSql(buildQuerySql(node.query));
|
|
54
|
+
break;
|
|
55
|
+
case "condition": {
|
|
56
|
+
const condition = renderCondition(node.condition);
|
|
57
|
+
const rendered = require_queries_renderConditions.buildWhereClause(condition, node.topLevel, (query) => require_queries_find_buildFindQuery.buildRawQuery(query, {}));
|
|
58
|
+
result = {
|
|
59
|
+
sql: rendered?.[0] ?? "",
|
|
60
|
+
bindings: rendered?.[1] ?? []
|
|
61
|
+
};
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
default: return require_utils.assertNever(node);
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
/** Converts expression leaves to rendered predicates for the shared condition renderer. */
|
|
69
|
+
function renderCondition(condition) {
|
|
70
|
+
if (condition.kind === "exp") return {
|
|
71
|
+
...condition,
|
|
72
|
+
conditions: condition.conditions.map((child) => renderCondition(child))
|
|
73
|
+
};
|
|
74
|
+
if (condition.kind === "expression") {
|
|
75
|
+
const rendered = renderSql(condition.expression);
|
|
76
|
+
return {
|
|
77
|
+
kind: "raw",
|
|
78
|
+
condition: rendered.sql,
|
|
79
|
+
bindings: rendered.bindings,
|
|
80
|
+
aliases: [],
|
|
81
|
+
pruneable: false
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return condition;
|
|
85
|
+
}
|
|
86
|
+
/** Prunes unused joins/CTEs, validates joins, and builds the SELECT SQL. */
|
|
87
|
+
function buildQuerySql(query) {
|
|
88
|
+
if (query.kind === "set") return renderSetQuery(query);
|
|
89
|
+
const { from, selects, groupBys, orderBys, where, having } = query;
|
|
90
|
+
const { joins: kept, ctes: keptCtes } = pruneJoins(query, from, query.joins, query.ctes, [
|
|
91
|
+
...selects,
|
|
92
|
+
...groupBys,
|
|
93
|
+
...orderBys,
|
|
94
|
+
where,
|
|
95
|
+
having
|
|
96
|
+
].filter((expression) => expression !== void 0));
|
|
97
|
+
const laterAliases = new Set(kept.map((j) => j.source.alias));
|
|
98
|
+
for (const j of kept) {
|
|
99
|
+
if (!j.on) require_utils.fail(`Join ${describeSource(j.source)} has no ON condition left (they all pruned), but the query still references it`);
|
|
100
|
+
laterAliases.delete(j.source.alias);
|
|
101
|
+
const forward = require_queries_sql_ParsedSqlExpression.sqlRefs(j.on).find((r) => laterAliases.has(r));
|
|
102
|
+
if (forward) require_utils.fail(`Join ${describeSource(j.source)} references '${forward}', which is joined later; move that join earlier in the join array`);
|
|
103
|
+
}
|
|
104
|
+
const out = [];
|
|
105
|
+
if (keptCtes.length > 0) out.push(withFragment(keptCtes));
|
|
106
|
+
out.push(`SELECT ${query.distinct ? "DISTINCT " : ""}`);
|
|
107
|
+
out.push(require_queries_sql_ParsedSqlExpression.joinSql(selects, ", "));
|
|
108
|
+
out.push(require_queries_sql_ParsedSqlExpression.joinSql([" FROM ", sourceSql(from)]));
|
|
109
|
+
for (const j of kept) {
|
|
110
|
+
const keyword = j.kind === "inner" ? "JOIN" : "LEFT OUTER JOIN";
|
|
111
|
+
out.push(require_queries_sql_ParsedSqlExpression.joinSql([
|
|
112
|
+
` ${keyword} `,
|
|
113
|
+
sourceSql(j.source),
|
|
114
|
+
" ON ",
|
|
115
|
+
j.on
|
|
116
|
+
]));
|
|
117
|
+
}
|
|
118
|
+
if (where) out.push(require_queries_sql_ParsedSqlExpression.joinSql([" WHERE ", where]));
|
|
119
|
+
if (groupBys.length > 0) out.push(require_queries_sql_ParsedSqlExpression.joinSql([" GROUP BY ", require_queries_sql_ParsedSqlExpression.joinSql(groupBys, ", ")]));
|
|
120
|
+
if (having) out.push(require_queries_sql_ParsedSqlExpression.joinSql([" HAVING ", having]));
|
|
121
|
+
if (orderBys.length > 0) out.push(require_queries_sql_ParsedSqlExpression.joinSql([" ORDER BY ", require_queries_sql_ParsedSqlExpression.joinSql(orderBys, ", ")]));
|
|
122
|
+
if (query.limit !== void 0) out.push(require_queries_sql_ParsedSqlExpression.joinSql([" LIMIT ", { value: query.limit }]));
|
|
123
|
+
if (query.offset !== void 0) out.push(require_queries_sql_ParsedSqlExpression.joinSql([" OFFSET ", { value: query.offset }]));
|
|
124
|
+
return require_queries_sql_ParsedSqlExpression.joinSql(out);
|
|
125
|
+
}
|
|
126
|
+
/** Builds INSERT, UPDATE, or DELETE SQL from the rewritten AST. */
|
|
127
|
+
function buildStatementSql(query) {
|
|
128
|
+
const parts = [`${query.kind === "delete" ? "DELETE FROM" : query.kind === "insert" ? "INSERT INTO" : "UPDATE"} ${require_queries_sql_Tables.tableSqlName({
|
|
129
|
+
tableName: query.target.table,
|
|
130
|
+
schema: query.target.schema
|
|
131
|
+
})} AS ${require_queries_sql_keywords.kq(query.target.alias)}`];
|
|
132
|
+
if (query.kind === "insert") {
|
|
133
|
+
parts.push(` (${query.columns.map(require_queries_sql_keywords.kq).join(", ")})`);
|
|
134
|
+
if (query.values) {
|
|
135
|
+
const rows = query.values.map((row) => require_queries_sql_ParsedSqlExpression.joinSql([
|
|
136
|
+
"(",
|
|
137
|
+
require_queries_sql_ParsedSqlExpression.joinSql(row, ", "),
|
|
138
|
+
")"
|
|
139
|
+
]));
|
|
140
|
+
parts.push(" VALUES ", require_queries_sql_ParsedSqlExpression.joinSql(rows, ", "));
|
|
141
|
+
} else {
|
|
142
|
+
const source = query.source;
|
|
143
|
+
const alias = require_queries_sql_keywords.safeKq(source.alias);
|
|
144
|
+
const columns = source.columns.map((key) => `${alias}.${require_queries_sql_keywords.safeKq(key)}`).join(", ");
|
|
145
|
+
parts.push(` SELECT ${columns} FROM (`, {
|
|
146
|
+
kind: "query",
|
|
147
|
+
query: source.query
|
|
148
|
+
}, `) AS ${alias}`);
|
|
149
|
+
}
|
|
150
|
+
} else if (query.kind === "update") {
|
|
151
|
+
const assignments = query.assignments.map((entry) => require_queries_sql_ParsedSqlExpression.joinSql([`${require_queries_sql_keywords.kq(entry.column)} = `, entry.value]));
|
|
152
|
+
parts.push(" SET ", require_queries_sql_ParsedSqlExpression.joinSql(assignments, ", "));
|
|
153
|
+
}
|
|
154
|
+
if (query.conditions.length) {
|
|
155
|
+
const conditions = query.conditions.map((condition) => require_queries_sql_ParsedSqlExpression.joinSql([
|
|
156
|
+
"(",
|
|
157
|
+
condition,
|
|
158
|
+
")"
|
|
159
|
+
]));
|
|
160
|
+
parts.push(" WHERE ", require_queries_sql_ParsedSqlExpression.joinSql(conditions, " AND "));
|
|
161
|
+
}
|
|
162
|
+
if (query.returning.length) parts.push(" RETURNING ", require_queries_sql_ParsedSqlExpression.joinSql(query.returning, ", "));
|
|
163
|
+
const body = require_queries_sql_ParsedSqlExpression.joinSql(parts);
|
|
164
|
+
const ctes = pruneCtes(query.ctes, new Set(require_queries_sql_ParsedSqlExpression.sqlRefs(body)));
|
|
165
|
+
return ctes.length ? require_queries_sql_ParsedSqlExpression.joinSql([withFragment(ctes), body]) : body;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Pruning: em.find's paradigm, on a flat join list.
|
|
169
|
+
*
|
|
170
|
+
* A condition given `undefined` was already dropped by `ConditionBuilder`. Now a join that nothing
|
|
171
|
+
* references anymore drops with it: a join is required if the source, a select, a surviving condition,
|
|
172
|
+
* a group-by, an order-by, or another required join's ON references it, or if it is pinned with
|
|
173
|
+
* `keep: true`. Marking follows ON dependencies transitively, exactly like `pruneUnusedJoins`'s
|
|
174
|
+
* `DependencyTracker`.
|
|
175
|
+
*
|
|
176
|
+
* em.find's joins almost never filter rows by themselves, so pruning them is semantics-preserving. An
|
|
177
|
+
* explicit `{ inner: b, on }` here does filter rows, so pruning it when unreferenced drops that filter;
|
|
178
|
+
* that matches `{ books: { title: undefined } }` in em.find and is deliberate. `keep: true` pins it, and
|
|
179
|
+
* a pure existence filter is better written as `a.id.in(query({ ... }))`, which is never `undefined`.
|
|
180
|
+
*
|
|
181
|
+
* CTEs prune on the same rule and through the same dependency map: a `with` entry nothing reads
|
|
182
|
+
* anymore drops with the join that read it, and a CTE read only by another CTE survives with it.
|
|
183
|
+
* A CTE joined into the query shares its alias with that join, so their dependencies are merged.
|
|
184
|
+
*
|
|
185
|
+
* The one join that never prunes is a recursive term's reference to its own CTE: PostgreSQL requires
|
|
186
|
+
* it, so it is not the caller's optional filter (see `Ctx.recursiveSelf`).
|
|
187
|
+
*/
|
|
188
|
+
function pruneJoins(q, from, joins, ctes, used) {
|
|
189
|
+
if (q.pruneJoins === false) return {
|
|
190
|
+
joins,
|
|
191
|
+
ctes
|
|
192
|
+
};
|
|
193
|
+
const deps = /* @__PURE__ */ new Map();
|
|
194
|
+
function addDeps(alias, refs) {
|
|
195
|
+
const own = refs.filter((r) => r !== alias);
|
|
196
|
+
const existing = deps.get(alias);
|
|
197
|
+
if (existing) existing.push(...own);
|
|
198
|
+
else deps.set(alias, own);
|
|
199
|
+
}
|
|
200
|
+
for (const j of joins) addDeps(j.source.alias, [...j.on ? require_queries_sql_ParsedSqlExpression.sqlRefs(j.on) : [], ...require_queries_sql_ParsedSqlExpression.sourceRefs(j.source)]);
|
|
201
|
+
for (const c of ctes) addDeps(c.alias, require_queries_sql_ParsedQuery.queryRefs(c.query));
|
|
202
|
+
const required = /* @__PURE__ */ new Set();
|
|
203
|
+
function markRequired(alias) {
|
|
204
|
+
if (required.has(alias)) return;
|
|
205
|
+
required.add(alias);
|
|
206
|
+
for (const dep of deps.get(alias) ?? []) markRequired(dep);
|
|
207
|
+
}
|
|
208
|
+
markRequired(from.alias);
|
|
209
|
+
for (const ref of require_queries_sql_ParsedSqlExpression.sourceRefs(from)) markRequired(ref);
|
|
210
|
+
for (const r of used.flatMap(require_queries_sql_ParsedSqlExpression.sqlRefs)) markRequired(r);
|
|
211
|
+
for (const j of joins) if (j.keep) markRequired(j.source.alias);
|
|
212
|
+
if (q.recursiveAlias) markRequired(q.recursiveAlias);
|
|
213
|
+
return {
|
|
214
|
+
joins: joins.filter((j) => required.has(j.source.alias)),
|
|
215
|
+
ctes: ctes.filter((c) => required.has(c.alias))
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Drops the CTEs that nothing reads, given the aliases the rest of the statement referenced.
|
|
220
|
+
*
|
|
221
|
+
* A CTE can only read earlier siblings, so going last to first means every CTE that could read this one
|
|
222
|
+
* was already visited, and its own reads are already in `referenced`. That makes one pass enough; first
|
|
223
|
+
* to last would have to repeat until the set stopped growing. `unshift` restores WITH order.
|
|
224
|
+
*/
|
|
225
|
+
function pruneCtes(ctes, referenced) {
|
|
226
|
+
const kept = [];
|
|
227
|
+
for (let i = ctes.length - 1; i >= 0; i--) {
|
|
228
|
+
const cte = ctes[i];
|
|
229
|
+
if (!referenced.has(cte.alias)) continue;
|
|
230
|
+
for (const ref of require_queries_sql_ParsedQuery.queryRefs(cte.query)) referenced.add(ref);
|
|
231
|
+
kept.unshift(cte);
|
|
232
|
+
}
|
|
233
|
+
return kept;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Renders `WITH a AS (...), b AS (...) `, whose bindings lead the statement, as their SQL does.
|
|
237
|
+
*
|
|
238
|
+
* One recursive CTE makes the whole clause `WITH RECURSIVE`, PostgreSQL's rule: the keyword is on the
|
|
239
|
+
* clause, not on the entry that needs it, and it does not force the other entries to be recursive.
|
|
240
|
+
*/
|
|
241
|
+
function withFragment(ctes) {
|
|
242
|
+
const recursive = ctes.some((c) => c.recursive) ? " RECURSIVE" : "";
|
|
243
|
+
const bodies = ctes.map((c) => require_queries_sql_ParsedSqlExpression.joinSql([
|
|
244
|
+
`${require_queries_sql_keywords.safeKq(c.alias)} AS (`,
|
|
245
|
+
{
|
|
246
|
+
kind: "query",
|
|
247
|
+
query: c.query
|
|
248
|
+
},
|
|
249
|
+
")"
|
|
250
|
+
]));
|
|
251
|
+
return require_queries_sql_ParsedSqlExpression.joinSql([
|
|
252
|
+
`WITH${recursive} `,
|
|
253
|
+
require_queries_sql_ParsedSqlExpression.joinSql(bodies, ", "),
|
|
254
|
+
" "
|
|
255
|
+
]);
|
|
256
|
+
}
|
|
257
|
+
/** Composes set operands after their SELECT nodes have been rewritten. */
|
|
258
|
+
function renderSetQuery(query) {
|
|
259
|
+
let body;
|
|
260
|
+
for (const operand of query.operands) {
|
|
261
|
+
let branch = {
|
|
262
|
+
kind: "query",
|
|
263
|
+
query: operand.query
|
|
264
|
+
};
|
|
265
|
+
if (operand.alignment) {
|
|
266
|
+
const alias = require_queries_sql_keywords.safeKq(operand.alignment.alias);
|
|
267
|
+
const columns = operand.alignment.columns.map((key) => `${alias}.${require_queries_sql_keywords.safeKq(key)} AS ${require_queries_sql_keywords.safeKq(key)}`).join(", ");
|
|
268
|
+
branch = require_queries_sql_ParsedSqlExpression.joinSql([
|
|
269
|
+
`SELECT ${columns} FROM (`,
|
|
270
|
+
branch,
|
|
271
|
+
`) AS ${alias}`
|
|
272
|
+
]);
|
|
273
|
+
}
|
|
274
|
+
body = body ? require_queries_sql_ParsedSqlExpression.joinSql([
|
|
275
|
+
"(",
|
|
276
|
+
body,
|
|
277
|
+
`) ${query.operation} (`,
|
|
278
|
+
branch,
|
|
279
|
+
")"
|
|
280
|
+
]) : branch;
|
|
281
|
+
}
|
|
282
|
+
const ctes = pruneCtes(query.ctes, new Set(query.operands.flatMap((operand) => require_queries_sql_ParsedQuery.queryRefs(operand.query))));
|
|
283
|
+
const parts = ctes.length ? [withFragment(ctes), body] : [body];
|
|
284
|
+
if (query.orderBys.length) parts.push(` ORDER BY ${query.orderBys.join(", ")}`);
|
|
285
|
+
if (query.limit !== void 0) parts.push(require_queries_sql_ParsedSqlExpression.joinSql([" LIMIT ", { value: query.limit }]));
|
|
286
|
+
if (query.offset !== void 0) parts.push(require_queries_sql_ParsedSqlExpression.joinSql([" OFFSET ", { value: query.offset }]));
|
|
287
|
+
return require_queries_sql_ParsedSqlExpression.joinSql(parts);
|
|
288
|
+
}
|
|
289
|
+
/** Builds source syntax after plugins have rewritten its table, CTE, or query. */
|
|
290
|
+
function sourceSql(source) {
|
|
291
|
+
switch (source.kind) {
|
|
292
|
+
case "table": return require_queries_sql_ParsedSqlExpression.joinSql([`${require_queries_sql_Tables.tableSqlName({
|
|
293
|
+
tableName: source.table,
|
|
294
|
+
schema: source.schema
|
|
295
|
+
})} AS ${require_queries_sql_keywords.safeKq(source.alias)}`]);
|
|
296
|
+
case "cte": return require_queries_sql_ParsedSqlExpression.joinSql([require_queries_sql_keywords.safeKq(source.cte), ...source.cte === source.alias ? [] : [` AS ${require_queries_sql_keywords.safeKq(source.alias)}`]]);
|
|
297
|
+
case "query": return require_queries_sql_ParsedSqlExpression.joinSql([
|
|
298
|
+
"(",
|
|
299
|
+
{
|
|
300
|
+
kind: "query",
|
|
301
|
+
query: source.query
|
|
302
|
+
},
|
|
303
|
+
`) AS ${require_queries_sql_keywords.safeKq(source.alias)}`
|
|
304
|
+
]);
|
|
305
|
+
default: return require_utils.assertNever(source);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/** Names the rewritten source in join-validation errors. */
|
|
309
|
+
function describeSource(source) {
|
|
310
|
+
switch (source.kind) {
|
|
311
|
+
case "table": return `table ${source.schema ? `${source.schema}.` : ""}${source.table} AS ${source.alias}`;
|
|
312
|
+
case "cte": return `CTE ${source.cte} AS ${source.alias}`;
|
|
313
|
+
case "query": return `subquery AS ${source.alias}`;
|
|
314
|
+
default: return require_utils.assertNever(source);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
exports.renderQuery = renderQuery;
|
|
318
|
+
exports.renderSql = renderSql;
|
|
319
|
+
exports.renderStatement = renderStatement;
|
|
320
|
+
|
|
321
|
+
//# sourceMappingURL=renderSql.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderSql.cjs","names":["safeKq","buildWhereClause","buildRawQuery","assertNever","sqlRefs","joinSql","tableSqlName","kq","sourceRefs","queryRefs"],"sources":["../../../src/queries/sql/renderSql.ts"],"sourcesContent":["import { buildRawQuery } from \"src/queries/find/buildFindQuery.ts\";\nimport type { ParsedExpressionCondition, ParsedExpressionFilter } from \"src/queries/parsedConditions.ts\";\nimport { buildWhereClause } from \"src/queries/renderConditions.ts\";\nimport { kq, safeKq } from \"src/queries/sql/keywords.ts\";\nimport {\n type ParsedExecuteQuery,\n type ParsedReadQuery,\n type ParsedSelectQuery,\n type ParsedSetQuery,\n queryRefs,\n} from \"src/queries/sql/ParsedQuery.ts\";\nimport {\n type ParsedSql,\n type ParsedSqlCondition,\n joinSql,\n sourceRefs,\n sqlRefs,\n} from \"src/queries/sql/ParsedSqlExpression.ts\";\nimport type { ParsedCte, ParsedJoin, ParsedSource } from \"src/queries/sql/query.ts\";\nimport { tableSqlName } from \"src/queries/sql/Tables.ts\";\nimport { assertNever, fail } from \"src/utils.ts\";\n\n/** Rendered SQL and its positional bindings. */\nexport interface SqlFragment {\n sql: string;\n bindings: unknown[];\n}\n\n/** Prunes unused joins/CTEs, validates joins, and renders a read statement. */\nexport function renderQuery(query: ParsedReadQuery): SqlFragment {\n return renderSql({ kind: \"query\", query });\n}\n\n/** Prunes unused joins/CTEs, validates joins, and renders a read or mutation statement. */\nexport function renderStatement(query: ParsedReadQuery | ParsedExecuteQuery): SqlFragment {\n return query.kind === \"select\" || query.kind === \"set\" ? renderQuery(query) : renderSql(buildStatementSql(query));\n}\n\n/** Renders an expression and its bindings, pruning and validating any embedded queries. */\nexport function renderSql(node: ParsedSql): SqlFragment {\n let result: SqlFragment;\n switch (node.kind) {\n case \"column\":\n // Both identifiers can be user-provided: a subquery's `as` name or an `a.column(...)` argument.\n result = { sql: `${safeKq(node.alias)}.${safeKq(node.column)}`, bindings: [] };\n break;\n case \"star\":\n result = { sql: `${safeKq(node.alias)}.*`, bindings: [] };\n break;\n case \"joined\":\n case \"raw\": {\n const parts = node.parts.map((part) =>\n typeof part === \"string\"\n ? { sql: part, bindings: [] }\n : \"kind\" in part\n ? renderSql(part)\n : { sql: \"?\", bindings: [part.value] },\n );\n result = {\n sql: parts.map((part) => part.sql).join(\"\"),\n bindings: parts.flatMap((part) => part.bindings),\n };\n break;\n }\n case \"query\":\n result = renderSql(buildQuerySql(node.query));\n break;\n case \"condition\": {\n const condition = renderCondition(node.condition) as ParsedExpressionFilter;\n const rendered = buildWhereClause(condition, node.topLevel, (query) => buildRawQuery(query, {}));\n result = { sql: rendered?.[0] ?? \"\", bindings: rendered?.[1] ?? [] };\n break;\n }\n default:\n return assertNever(node);\n }\n\n return result;\n}\n\n/** Converts expression leaves to rendered predicates for the shared condition renderer. */\nfunction renderCondition(condition: ParsedSqlCondition): ParsedExpressionCondition {\n if (condition.kind === \"exp\") {\n return { ...condition, conditions: condition.conditions.map((child) => renderCondition(child)) };\n }\n if (condition.kind === \"expression\") {\n const rendered = renderSql(condition.expression);\n return {\n kind: \"raw\",\n condition: rendered.sql,\n bindings: rendered.bindings,\n // Pruning has finished; the shared condition renderer only needs SQL and bindings.\n aliases: [],\n pruneable: false,\n };\n }\n return condition;\n}\n\n/** Prunes unused joins/CTEs, validates joins, and builds the SELECT SQL. */\nfunction buildQuerySql(query: ParsedReadQuery): ParsedSql {\n if (query.kind === \"set\") return renderSetQuery(query);\n const { from, selects, groupBys, orderBys, where, having } = query;\n const { joins: kept, ctes: keptCtes } = pruneJoins(\n query,\n from,\n query.joins,\n query.ctes,\n [...selects, ...groupBys, ...orderBys, where, having].filter((expression) => expression !== undefined),\n );\n // Joins emit in declaration order, so an ON may only reference sources declared before it; a forward\n // reference would reach PG as invalid SQL (\"missing FROM-clause entry\"). Reordering is not offered:\n // it is not semantics-preserving once INNER and LEFT joins mix, and the caller's fix is trivial.\n const laterAliases = new Set(kept.map((j) => j.source.alias));\n for (const j of kept) {\n if (!j.on) {\n fail(\n `Join ${describeSource(j.source)} has no ON condition left (they all pruned), but the query still references it`,\n );\n }\n laterAliases.delete(j.source.alias);\n const forward = sqlRefs(j.on).find((r) => laterAliases.has(r));\n if (forward) {\n fail(\n `Join ${describeSource(j.source)} references '${forward}', which is joined later; move that join earlier in the join array`,\n );\n }\n }\n\n // Assemble the SELECT after pruning and join validation.\n const out: (string | ParsedSql)[] = [];\n // WITH leads the statement, so its bindings must be pushed before the SELECT's.\n if (keptCtes.length > 0) out.push(withFragment(keptCtes));\n out.push(`SELECT ${query.distinct ? \"DISTINCT \" : \"\"}`);\n out.push(joinSql(selects, \", \"));\n out.push(joinSql([\" FROM \", sourceSql(from)]));\n for (const j of kept) {\n const keyword = j.kind === \"inner\" ? \"JOIN\" : \"LEFT OUTER JOIN\";\n out.push(joinSql([` ${keyword} `, sourceSql(j.source), \" ON \", j.on!]));\n }\n if (where) out.push(joinSql([\" WHERE \", where]));\n if (groupBys.length > 0) out.push(joinSql([\" GROUP BY \", joinSql(groupBys, \", \")]));\n if (having) out.push(joinSql([\" HAVING \", having]));\n if (orderBys.length > 0) out.push(joinSql([\" ORDER BY \", joinSql(orderBys, \", \")]));\n if (query.limit !== undefined) out.push(joinSql([\" LIMIT \", { value: query.limit }]));\n if (query.offset !== undefined) out.push(joinSql([\" OFFSET \", { value: query.offset }]));\n return joinSql(out);\n}\n\n/** Builds INSERT, UPDATE, or DELETE SQL from the rewritten AST. */\nfunction buildStatementSql(query: ParsedExecuteQuery): ParsedSql {\n const operation = query.kind === \"delete\" ? \"DELETE FROM\" : query.kind === \"insert\" ? \"INSERT INTO\" : \"UPDATE\";\n const target = tableSqlName({ tableName: query.target.table, schema: query.target.schema });\n const parts: (string | ParsedSql)[] = [`${operation} ${target} AS ${kq(query.target.alias)}`];\n if (query.kind === \"insert\") {\n parts.push(` (${query.columns.map(kq).join(\", \")})`);\n if (query.values) {\n const rows = query.values.map((row) => joinSql([\"(\", joinSql(row, \", \"), \")\"]));\n parts.push(\" VALUES \", joinSql(rows, \", \"));\n } else {\n const source = query.source!;\n const alias = safeKq(source.alias);\n const columns = source.columns.map((key) => `${alias}.${safeKq(key)}`).join(\", \");\n parts.push(` SELECT ${columns} FROM (`, { kind: \"query\", query: source.query }, `) AS ${alias}`);\n }\n } else if (query.kind === \"update\") {\n const assignments = query.assignments.map((entry) => joinSql([`${kq(entry.column)} = `, entry.value]));\n parts.push(\" SET \", joinSql(assignments, \", \"));\n }\n\n if (query.conditions.length) {\n const conditions = query.conditions.map((condition) => joinSql([\"(\", condition, \")\"]));\n parts.push(\" WHERE \", joinSql(conditions, \" AND \"));\n }\n if (query.returning.length) parts.push(\" RETURNING \", joinSql(query.returning, \", \"));\n const body = joinSql(parts);\n const ctes = pruneCtes(query.ctes, new Set(sqlRefs(body)));\n return ctes.length ? joinSql([withFragment(ctes), body]) : body;\n}\n\n/**\n * Pruning: em.find's paradigm, on a flat join list.\n *\n * A condition given `undefined` was already dropped by `ConditionBuilder`. Now a join that nothing\n * references anymore drops with it: a join is required if the source, a select, a surviving condition,\n * a group-by, an order-by, or another required join's ON references it, or if it is pinned with\n * `keep: true`. Marking follows ON dependencies transitively, exactly like `pruneUnusedJoins`'s\n * `DependencyTracker`.\n *\n * em.find's joins almost never filter rows by themselves, so pruning them is semantics-preserving. An\n * explicit `{ inner: b, on }` here does filter rows, so pruning it when unreferenced drops that filter;\n * that matches `{ books: { title: undefined } }` in em.find and is deliberate. `keep: true` pins it, and\n * a pure existence filter is better written as `a.id.in(query({ ... }))`, which is never `undefined`.\n *\n * CTEs prune on the same rule and through the same dependency map: a `with` entry nothing reads\n * anymore drops with the join that read it, and a CTE read only by another CTE survives with it.\n * A CTE joined into the query shares its alias with that join, so their dependencies are merged.\n *\n * The one join that never prunes is a recursive term's reference to its own CTE: PostgreSQL requires\n * it, so it is not the caller's optional filter (see `Ctx.recursiveSelf`).\n */\nfunction pruneJoins(\n q: ParsedSelectQuery,\n from: ParsedSource,\n joins: ParsedJoin[],\n ctes: ParsedCte[],\n used: ParsedSql[],\n): { joins: ParsedJoin[]; ctes: ParsedCte[] } {\n if (q.pruneJoins === false) return { joins, ctes };\n const deps = new Map<string, string[]>();\n function addDeps(alias: string, refs: readonly string[]): void {\n const own = refs.filter((r) => r !== alias);\n const existing = deps.get(alias);\n if (existing) existing.push(...own);\n else deps.set(alias, own);\n }\n for (const j of joins) {\n addDeps(j.source.alias, [...(j.on ? sqlRefs(j.on) : []), ...sourceRefs(j.source)]);\n }\n for (const c of ctes) addDeps(c.alias, queryRefs(c.query));\n const required = new Set<string>();\n function markRequired(alias: string): void {\n if (required.has(alias)) return;\n required.add(alias);\n for (const dep of deps.get(alias) ?? []) markRequired(dep);\n }\n markRequired(from.alias);\n for (const ref of sourceRefs(from)) markRequired(ref);\n for (const r of used.flatMap(sqlRefs)) markRequired(r);\n for (const j of joins) if (j.keep) markRequired(j.source.alias);\n // A recursive term must keep its reference to its own CTE; PostgreSQL rejects a term without one,\n // and pruning it would quietly turn the recursion into a plain select over the base term's table.\n if (q.recursiveAlias) markRequired(q.recursiveAlias);\n return {\n joins: joins.filter((j) => required.has(j.source.alias)),\n ctes: ctes.filter((c) => required.has(c.alias)),\n };\n}\n\n/**\n * Drops the CTEs that nothing reads, given the aliases the rest of the statement referenced.\n *\n * A CTE can only read earlier siblings, so going last to first means every CTE that could read this one\n * was already visited, and its own reads are already in `referenced`. That makes one pass enough; first\n * to last would have to repeat until the set stopped growing. `unshift` restores WITH order.\n */\nfunction pruneCtes(ctes: ParsedCte[], referenced: Set<string>): ParsedCte[] {\n const kept: ParsedCte[] = [];\n for (let i = ctes.length - 1; i >= 0; i--) {\n const cte = ctes[i];\n if (!referenced.has(cte.alias)) continue;\n for (const ref of queryRefs(cte.query)) referenced.add(ref);\n kept.unshift(cte);\n }\n return kept;\n}\n\n/**\n * Renders `WITH a AS (...), b AS (...) `, whose bindings lead the statement, as their SQL does.\n *\n * One recursive CTE makes the whole clause `WITH RECURSIVE`, PostgreSQL's rule: the keyword is on the\n * clause, not on the entry that needs it, and it does not force the other entries to be recursive.\n */\nfunction withFragment(ctes: ParsedCte[]): ParsedSql {\n const recursive = ctes.some((c) => c.recursive) ? \" RECURSIVE\" : \"\";\n const bodies = ctes.map((c) => joinSql([`${safeKq(c.alias)} AS (`, { kind: \"query\", query: c.query }, \")\"]));\n return joinSql([`WITH${recursive} `, joinSql(bodies, \", \"), \" \"]);\n}\n\n/** Composes set operands after their SELECT nodes have been rewritten. */\nfunction renderSetQuery(query: ParsedSetQuery): ParsedSql {\n let body: ParsedSql | undefined;\n for (const operand of query.operands) {\n let branch: ParsedSql = { kind: \"query\", query: operand.query };\n if (operand.alignment) {\n const alias = safeKq(operand.alignment.alias);\n const columns = operand.alignment.columns.map((key) => `${alias}.${safeKq(key)} AS ${safeKq(key)}`).join(\", \");\n branch = joinSql([`SELECT ${columns} FROM (`, branch, `) AS ${alias}`]);\n }\n body = body ? joinSql([\"(\", body, `) ${query.operation} (`, branch, \")\"]) : branch;\n }\n const ctes = pruneCtes(query.ctes, new Set(query.operands.flatMap((operand) => queryRefs(operand.query))));\n const parts: (string | ParsedSql)[] = ctes.length ? [withFragment(ctes), body!] : [body!];\n if (query.orderBys.length) parts.push(` ORDER BY ${query.orderBys.join(\", \")}`);\n if (query.limit !== undefined) parts.push(joinSql([\" LIMIT \", { value: query.limit }]));\n if (query.offset !== undefined) parts.push(joinSql([\" OFFSET \", { value: query.offset }]));\n return joinSql(parts);\n}\n\n/** Builds source syntax after plugins have rewritten its table, CTE, or query. */\nfunction sourceSql(source: ParsedSource): ParsedSql {\n switch (source.kind) {\n case \"table\":\n return joinSql([\n `${tableSqlName({ tableName: source.table, schema: source.schema })} AS ${safeKq(source.alias)}`,\n ]);\n case \"cte\":\n return joinSql([safeKq(source.cte), ...(source.cte === source.alias ? [] : [` AS ${safeKq(source.alias)}`])]);\n case \"query\":\n return joinSql([\"(\", { kind: \"query\", query: source.query }, `) AS ${safeKq(source.alias)}`]);\n default:\n return assertNever(source);\n }\n}\n\n/** Names the rewritten source in join-validation errors. */\nfunction describeSource(source: ParsedSource): string {\n switch (source.kind) {\n case \"table\":\n return `table ${source.schema ? `${source.schema}.` : \"\"}${source.table} AS ${source.alias}`;\n case \"cte\":\n return `CTE ${source.cte} AS ${source.alias}`;\n case \"query\":\n return `subquery AS ${source.alias}`;\n default:\n return assertNever(source);\n }\n}\n"],"mappings":";;;;;;;;;;AA6BA,SAAgB,YAAY,OAAqC;CAC/D,OAAO,UAAU;EAAE,MAAM;EAAS;CAAM,CAAC;AAC3C;;AAGA,SAAgB,gBAAgB,OAA0D;CACxF,OAAO,MAAM,SAAS,YAAY,MAAM,SAAS,QAAQ,YAAY,KAAK,IAAI,UAAU,kBAAkB,KAAK,CAAC;AAClH;;AAGA,SAAgB,UAAU,MAA8B;CACtD,IAAI;CACJ,QAAQ,KAAK,MAAb;EACE,KAAK;GAEH,SAAS;IAAE,KAAK,GAAGA,6BAAAA,OAAO,KAAK,KAAK,EAAE,GAAGA,6BAAAA,OAAO,KAAK,MAAM;IAAK,UAAU,CAAC;GAAE;GAC7E;EACF,KAAK;GACH,SAAS;IAAE,KAAK,GAAGA,6BAAAA,OAAO,KAAK,KAAK,EAAE;IAAK,UAAU,CAAC;GAAE;GACxD;EACF,KAAK;EACL,KAAK,OAAO;GACV,MAAM,QAAQ,KAAK,MAAM,KAAK,SAC5B,OAAO,SAAS,WACZ;IAAE,KAAK;IAAM,UAAU,CAAC;GAAE,IAC1B,UAAU,OACR,UAAU,IAAI,IACd;IAAE,KAAK;IAAK,UAAU,CAAC,KAAK,KAAK;GAAE,CAC3C;GACA,SAAS;IACP,KAAK,MAAM,KAAK,SAAS,KAAK,GAAG,CAAC,CAAC,KAAK,EAAE;IAC1C,UAAU,MAAM,SAAS,SAAS,KAAK,QAAQ;GACjD;GACA;EACF;EACA,KAAK;GACH,SAAS,UAAU,cAAc,KAAK,KAAK,CAAC;GAC5C;EACF,KAAK,aAAa;GAChB,MAAM,YAAY,gBAAgB,KAAK,SAAS;GAChD,MAAM,WAAWC,iCAAAA,iBAAiB,WAAW,KAAK,WAAW,UAAUC,oCAAAA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G,CAAE,CAAC;EAC9G,KAAK,SACH,OAAOK,wCAAAA,QAAQ;GAAC;GAAK;IAAE,MAAM;IAAS,OAAO,OAAO;GAAM;GAAG,QAAQL,6BAAAA,OAAO,OAAO,KAAK;EAAG,CAAC;EAC9F,SACE,OAAOG,cAAAA,YAAY,MAAM;CAC7B;AACF;;AAGA,SAAS,eAAe,QAA8B;CACpD,QAAQ,OAAO,MAAf;EACE,KAAK,SACH,OAAO,SAAS,OAAO,SAAS,GAAG,OAAO,OAAO,KAAK,KAAK,OAAO,MAAM,MAAM,OAAO;EACvF,KAAK,OACH,OAAO,OAAO,OAAO,IAAI,MAAM,OAAO;EACxC,KAAK,SACH,OAAO,eAAe,OAAO;EAC/B,SACE,OAAOA,cAAAA,YAAY,MAAM;CAC7B;AACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ParsedExecuteQuery, ParsedReadQuery } from "./ParsedQuery.cjs";
|
|
2
|
+
import { ParsedSql } from "./ParsedSqlExpression.cjs";
|
|
3
|
+
/** Rendered SQL and its positional bindings. */
|
|
4
|
+
export interface SqlFragment {
|
|
5
|
+
sql: string;
|
|
6
|
+
bindings: unknown[];
|
|
7
|
+
}
|
|
8
|
+
/** Prunes unused joins/CTEs, validates joins, and renders a read statement. */
|
|
9
|
+
export declare function renderQuery(query: ParsedReadQuery): SqlFragment;
|
|
10
|
+
/** Prunes unused joins/CTEs, validates joins, and renders a read or mutation statement. */
|
|
11
|
+
export declare function renderStatement(query: ParsedReadQuery | ParsedExecuteQuery): SqlFragment;
|
|
12
|
+
/** Renders an expression and its bindings, pruning and validating any embedded queries. */
|
|
13
|
+
export declare function renderSql(node: ParsedSql): SqlFragment;
|
|
14
|
+
//# sourceMappingURL=renderSql.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderSql.d.cts","names":[],"sources":["../../../src/queries/sql/renderSql.ts"],"mappings":";;;iBAuBiB;EACf;EACA;;;wBAIc,YAAY,OAAO,kBAAkB;;wBAKrC,gBAAgB,OAAO,kBAAkB,qBAAqB;;wBAK9D,UAAU,MAAM,YAAY"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ParsedExecuteQuery, ParsedReadQuery } from "./ParsedQuery.mjs";
|
|
2
|
+
import { ParsedSql } from "./ParsedSqlExpression.mjs";
|
|
3
|
+
/** Rendered SQL and its positional bindings. */
|
|
4
|
+
export interface SqlFragment {
|
|
5
|
+
sql: string;
|
|
6
|
+
bindings: unknown[];
|
|
7
|
+
}
|
|
8
|
+
/** Prunes unused joins/CTEs, validates joins, and renders a read statement. */
|
|
9
|
+
export declare function renderQuery(query: ParsedReadQuery): SqlFragment;
|
|
10
|
+
/** Prunes unused joins/CTEs, validates joins, and renders a read or mutation statement. */
|
|
11
|
+
export declare function renderStatement(query: ParsedReadQuery | ParsedExecuteQuery): SqlFragment;
|
|
12
|
+
/** Renders an expression and its bindings, pruning and validating any embedded queries. */
|
|
13
|
+
export declare function renderSql(node: ParsedSql): SqlFragment;
|
|
14
|
+
//# sourceMappingURL=renderSql.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderSql.d.mts","names":[],"sources":["../../../src/queries/sql/renderSql.ts"],"mappings":";;;iBAuBiB;EACf;EACA;;;wBAIc,YAAY,OAAO,kBAAkB;;wBAKrC,gBAAgB,OAAO,kBAAkB,qBAAqB;;wBAK9D,UAAU,MAAM,YAAY"}
|