joist-core 2.5.0-next.21 → 2.5.0-next.22
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/EntityManager.cjs +15 -3
- package/build/EntityManager.cjs.map +1 -1
- package/build/EntityManager.d.cts +3 -3
- package/build/EntityManager.d.cts.map +1 -1
- package/build/EntityManager.d.mts +3 -3
- package/build/EntityManager.d.mts.map +1 -1
- package/build/EntityManager.js +15 -3
- package/build/EntityManager.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/index.cjs +10 -3
- 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 +6 -4
- package/build/index.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/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { PredicateBrand, SqlCondition } from "../conditions.cjs";
|
|
2
|
+
import { ParsedSql } from "./ParsedSqlExpression.cjs";
|
|
3
3
|
import { ExpressionOrderBy, QueryCondition } from "./query.cjs";
|
|
4
4
|
import { TypeInfo } from "../../serde/TypeInfo.cjs";
|
|
5
5
|
/**
|
|
@@ -118,12 +118,6 @@ export interface LeftJoin<A, C = SqlCondition> {
|
|
|
118
118
|
readonly on: C;
|
|
119
119
|
readonly keep?: boolean;
|
|
120
120
|
}
|
|
121
|
-
/** SQL plus its `?` bindings plus the SQL aliases it references, i.e. for join pruning. */
|
|
122
|
-
export interface SqlFragment {
|
|
123
|
-
sql: string;
|
|
124
|
-
bindings: any[];
|
|
125
|
-
refs: string[];
|
|
126
|
-
}
|
|
127
121
|
/**
|
|
128
122
|
* What an expression needs from the query it is generating SQL for: the SQL alias assigned to each
|
|
129
123
|
* source, and a way to turn nested conditions into SQL (which needs `ConditionBuilder`, so it lives in `query.ts`).
|
|
@@ -131,8 +125,8 @@ export interface SqlFragment {
|
|
|
131
125
|
export interface ExprContext {
|
|
132
126
|
/** Returns the SQL alias for a table's `TableMgmt` or a subquery handle, searching enclosing queries. */
|
|
133
127
|
aliasFor(handle: object): string;
|
|
134
|
-
/**
|
|
135
|
-
|
|
128
|
+
/** Resolves a user-facing predicate; `undefined` if it was pruned away entirely. */
|
|
129
|
+
parseCondition(cond: QueryCondition): ParsedSql | undefined;
|
|
136
130
|
}
|
|
137
131
|
export declare function isExpr(value: unknown): value is ExprLike<any>;
|
|
138
132
|
/** Every `Expr` is a `BaseExpr` at runtime; this cast keeps `isExpr` a plain type guard so unions narrow. */
|
|
@@ -142,33 +136,31 @@ export declare const deferredSym: unique symbol;
|
|
|
142
136
|
* A condition whose SQL depends on aliases that are only known once the query is parsed, i.e.
|
|
143
137
|
* `bookStats.bookCount.gt(1)` or `bs.authorId.eq(a.id)`.
|
|
144
138
|
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* use a separate protocol in the `em.find` parser.
|
|
139
|
+
* Parsing resolves the expression against its query's aliases. Domain alias conditions use a
|
|
140
|
+
* separate protocol in the `em.find` parser.
|
|
148
141
|
*/
|
|
149
|
-
export interface DeferredCondition extends
|
|
150
|
-
|
|
142
|
+
export interface DeferredCondition extends PredicateBrand<"sql"> {
|
|
143
|
+
kind: "deferred";
|
|
144
|
+
[deferredSym]: (ctx: ExprContext) => ParsedSql;
|
|
151
145
|
}
|
|
152
146
|
export declare function isDeferredCondition(cond: unknown): cond is DeferredCondition;
|
|
153
|
-
/** Creates a `DeferredCondition` that generates its SQL with `fn` once the query's aliases are known. */
|
|
154
|
-
export declare function deferredCondition(fn: (ctx: ExprContext) => SqlFragment): DeferredCondition;
|
|
155
147
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
148
|
+
* Defers alias resolution until this predicate is parsed in a particular query.
|
|
149
|
+
*
|
|
150
|
+
* A condition can be created once and reused across em.query calls, where its sources may be
|
|
151
|
+
* assigned different SQL aliases.
|
|
158
152
|
*/
|
|
159
|
-
export declare function
|
|
160
|
-
/** Concatenates SQL fragments with `sep`, keeping bindings and refs in order. */
|
|
161
|
-
export declare function joinFragments(parts: SqlFragment[], sep: string): SqlFragment;
|
|
153
|
+
export declare function deferredCondition(fn: (ctx: ExprContext) => ParsedSql): DeferredCondition;
|
|
162
154
|
/**
|
|
163
|
-
* The methods every expression shares. Subclasses provide `
|
|
155
|
+
* The methods every expression shares. Subclasses provide `parseSql`, and usually `decode`/`encode`.
|
|
164
156
|
*
|
|
165
157
|
* Table columns can override comparisons to apply column-specific conversions. Domain aliases do not
|
|
166
158
|
* implement this protocol.
|
|
167
159
|
*/
|
|
168
160
|
export declare abstract class BaseExpr {
|
|
169
161
|
readonly [exprBrand]: any;
|
|
170
|
-
/**
|
|
171
|
-
abstract
|
|
162
|
+
/** Resolves this expression into the SQL AST, i.e. a scalar subquery includes parentheses. */
|
|
163
|
+
abstract parseSql(ctx: ExprContext): ParsedSql;
|
|
172
164
|
/** Only an actual scalar/IN subquery exposes its selected expression, not an ordinary ID expression. */
|
|
173
165
|
get subquerySelect(): BaseExpr | undefined;
|
|
174
166
|
/** Known SQL representation and logical domain; raw SQL and unmodeled refs remain unknown. */
|
|
@@ -178,7 +170,7 @@ export declare abstract class BaseExpr {
|
|
|
178
170
|
/** The source of a direct column reference, whose value becomes NULL under an unmatched LEFT join. */
|
|
179
171
|
get sqlSource(): object | undefined;
|
|
180
172
|
/** Produces the SQL without the outer parens a subquery normally gets; only differs for subqueries. */
|
|
181
|
-
|
|
173
|
+
parseSqlBare(ctx: ExprContext): ParsedSql;
|
|
182
174
|
/** Converts a result-set value into the domain value, i.e. an int into a tagged id. */
|
|
183
175
|
decode(value: unknown): unknown;
|
|
184
176
|
/** Converts a domain value into the database value, i.e. a tagged id into an int, for bindings. */
|
|
@@ -236,7 +228,7 @@ export declare class FnExpr extends BaseExpr {
|
|
|
236
228
|
* Renders function arguments, aggregate ordering, and the filter in SQL binding order.
|
|
237
229
|
* Keep references from all three so aggregate-only joins are retained.
|
|
238
230
|
*/
|
|
239
|
-
|
|
231
|
+
parseSql(ctx: ExprContext): ParsedSql;
|
|
240
232
|
decode(value: unknown): unknown;
|
|
241
233
|
encode(value: unknown): unknown;
|
|
242
234
|
}
|
|
@@ -257,14 +249,14 @@ export declare class OperatorExpr extends BaseExpr {
|
|
|
257
249
|
get outputType(): TypeInfo | undefined;
|
|
258
250
|
get sqlNullable(): boolean | undefined;
|
|
259
251
|
get sqlSource(): object | undefined;
|
|
260
|
-
|
|
252
|
+
parseSql(ctx: ExprContext): ParsedSql;
|
|
261
253
|
}
|
|
262
254
|
/** A bound literal, i.e. the `?` in `coalesce(x, ?)`. */
|
|
263
255
|
export declare class BindingExpr extends BaseExpr {
|
|
264
256
|
private value;
|
|
265
257
|
constructor(value: unknown);
|
|
266
258
|
get sqlNullable(): boolean;
|
|
267
|
-
|
|
259
|
+
parseSql(): ParsedSql;
|
|
268
260
|
}
|
|
269
261
|
/** An unmodeled column on a known source, i.e. `a.column("ts_search")`. */
|
|
270
262
|
export declare class RefExpr extends BaseExpr {
|
|
@@ -272,7 +264,7 @@ export declare class RefExpr extends BaseExpr {
|
|
|
272
264
|
private column;
|
|
273
265
|
constructor(handle: object, column: string);
|
|
274
266
|
get sqlSource(): object;
|
|
275
|
-
|
|
267
|
+
parseSql(ctx: ExprContext): ParsedSql;
|
|
276
268
|
}
|
|
277
269
|
/**
|
|
278
270
|
* A `sql` tagged template.
|
|
@@ -289,12 +281,12 @@ export declare class TemplateExpr extends BaseExpr {
|
|
|
289
281
|
private strings;
|
|
290
282
|
private values;
|
|
291
283
|
constructor(strings: TemplateStringsArray, values: unknown[]);
|
|
292
|
-
|
|
284
|
+
parseSql(ctx: ExprContext): ParsedSql;
|
|
293
285
|
}
|
|
294
286
|
/** Turns one `${...}` of a `sql` template into SQL: an expression, a condition, or a bound value. */
|
|
295
|
-
export declare function interpolationToSql(value: unknown, ctx: ExprContext):
|
|
296
|
-
/**
|
|
287
|
+
export declare function interpolationToSql(value: unknown, ctx: ExprContext): ParsedSql;
|
|
288
|
+
/** Recognizes boolean groups, deferred predicates, and explicit column/raw conditions. */
|
|
297
289
|
export declare function isConditionLike(value: unknown): boolean;
|
|
298
290
|
/** Renders an expression order for either a query or an aggregate, or prunes an undefined order. */
|
|
299
|
-
export declare function orderByToSql(o: ExpressionOrderBy, ctx: ExprContext):
|
|
291
|
+
export declare function orderByToSql(o: ExpressionOrderBy, ctx: ExprContext): ParsedSql | undefined;
|
|
300
292
|
//# sourceMappingURL=Expr.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Expr.d.cts","names":[],"sources":["../../../src/queries/sql/Expr.ts"],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"Expr.d.cts","names":[],"sources":["../../../src/queries/sql/Expr.ts"],"mappings":"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"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { PredicateBrand, SqlCondition } from "../conditions.mjs";
|
|
2
|
+
import { ParsedSql } from "./ParsedSqlExpression.mjs";
|
|
3
3
|
import { ExpressionOrderBy, QueryCondition } from "./query.mjs";
|
|
4
4
|
import { TypeInfo } from "../../serde/TypeInfo.mjs";
|
|
5
5
|
/**
|
|
@@ -118,12 +118,6 @@ export interface LeftJoin<A, C = SqlCondition> {
|
|
|
118
118
|
readonly on: C;
|
|
119
119
|
readonly keep?: boolean;
|
|
120
120
|
}
|
|
121
|
-
/** SQL plus its `?` bindings plus the SQL aliases it references, i.e. for join pruning. */
|
|
122
|
-
export interface SqlFragment {
|
|
123
|
-
sql: string;
|
|
124
|
-
bindings: any[];
|
|
125
|
-
refs: string[];
|
|
126
|
-
}
|
|
127
121
|
/**
|
|
128
122
|
* What an expression needs from the query it is generating SQL for: the SQL alias assigned to each
|
|
129
123
|
* source, and a way to turn nested conditions into SQL (which needs `ConditionBuilder`, so it lives in `query.ts`).
|
|
@@ -131,8 +125,8 @@ export interface SqlFragment {
|
|
|
131
125
|
export interface ExprContext {
|
|
132
126
|
/** Returns the SQL alias for a table's `TableMgmt` or a subquery handle, searching enclosing queries. */
|
|
133
127
|
aliasFor(handle: object): string;
|
|
134
|
-
/**
|
|
135
|
-
|
|
128
|
+
/** Resolves a user-facing predicate; `undefined` if it was pruned away entirely. */
|
|
129
|
+
parseCondition(cond: QueryCondition): ParsedSql | undefined;
|
|
136
130
|
}
|
|
137
131
|
export declare function isExpr(value: unknown): value is ExprLike<any>;
|
|
138
132
|
/** Every `Expr` is a `BaseExpr` at runtime; this cast keeps `isExpr` a plain type guard so unions narrow. */
|
|
@@ -142,33 +136,31 @@ export declare const deferredSym: unique symbol;
|
|
|
142
136
|
* A condition whose SQL depends on aliases that are only known once the query is parsed, i.e.
|
|
143
137
|
* `bookStats.bookCount.gt(1)` or `bs.authorId.eq(a.id)`.
|
|
144
138
|
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* use a separate protocol in the `em.find` parser.
|
|
139
|
+
* Parsing resolves the expression against its query's aliases. Domain alias conditions use a
|
|
140
|
+
* separate protocol in the `em.find` parser.
|
|
148
141
|
*/
|
|
149
|
-
export interface DeferredCondition extends
|
|
150
|
-
|
|
142
|
+
export interface DeferredCondition extends PredicateBrand<"sql"> {
|
|
143
|
+
kind: "deferred";
|
|
144
|
+
[deferredSym]: (ctx: ExprContext) => ParsedSql;
|
|
151
145
|
}
|
|
152
146
|
export declare function isDeferredCondition(cond: unknown): cond is DeferredCondition;
|
|
153
|
-
/** Creates a `DeferredCondition` that generates its SQL with `fn` once the query's aliases are known. */
|
|
154
|
-
export declare function deferredCondition(fn: (ctx: ExprContext) => SqlFragment): DeferredCondition;
|
|
155
147
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
148
|
+
* Defers alias resolution until this predicate is parsed in a particular query.
|
|
149
|
+
*
|
|
150
|
+
* A condition can be created once and reused across em.query calls, where its sources may be
|
|
151
|
+
* assigned different SQL aliases.
|
|
158
152
|
*/
|
|
159
|
-
export declare function
|
|
160
|
-
/** Concatenates SQL fragments with `sep`, keeping bindings and refs in order. */
|
|
161
|
-
export declare function joinFragments(parts: SqlFragment[], sep: string): SqlFragment;
|
|
153
|
+
export declare function deferredCondition(fn: (ctx: ExprContext) => ParsedSql): DeferredCondition;
|
|
162
154
|
/**
|
|
163
|
-
* The methods every expression shares. Subclasses provide `
|
|
155
|
+
* The methods every expression shares. Subclasses provide `parseSql`, and usually `decode`/`encode`.
|
|
164
156
|
*
|
|
165
157
|
* Table columns can override comparisons to apply column-specific conversions. Domain aliases do not
|
|
166
158
|
* implement this protocol.
|
|
167
159
|
*/
|
|
168
160
|
export declare abstract class BaseExpr {
|
|
169
161
|
readonly [exprBrand]: any;
|
|
170
|
-
/**
|
|
171
|
-
abstract
|
|
162
|
+
/** Resolves this expression into the SQL AST, i.e. a scalar subquery includes parentheses. */
|
|
163
|
+
abstract parseSql(ctx: ExprContext): ParsedSql;
|
|
172
164
|
/** Only an actual scalar/IN subquery exposes its selected expression, not an ordinary ID expression. */
|
|
173
165
|
get subquerySelect(): BaseExpr | undefined;
|
|
174
166
|
/** Known SQL representation and logical domain; raw SQL and unmodeled refs remain unknown. */
|
|
@@ -178,7 +170,7 @@ export declare abstract class BaseExpr {
|
|
|
178
170
|
/** The source of a direct column reference, whose value becomes NULL under an unmatched LEFT join. */
|
|
179
171
|
get sqlSource(): object | undefined;
|
|
180
172
|
/** Produces the SQL without the outer parens a subquery normally gets; only differs for subqueries. */
|
|
181
|
-
|
|
173
|
+
parseSqlBare(ctx: ExprContext): ParsedSql;
|
|
182
174
|
/** Converts a result-set value into the domain value, i.e. an int into a tagged id. */
|
|
183
175
|
decode(value: unknown): unknown;
|
|
184
176
|
/** Converts a domain value into the database value, i.e. a tagged id into an int, for bindings. */
|
|
@@ -236,7 +228,7 @@ export declare class FnExpr extends BaseExpr {
|
|
|
236
228
|
* Renders function arguments, aggregate ordering, and the filter in SQL binding order.
|
|
237
229
|
* Keep references from all three so aggregate-only joins are retained.
|
|
238
230
|
*/
|
|
239
|
-
|
|
231
|
+
parseSql(ctx: ExprContext): ParsedSql;
|
|
240
232
|
decode(value: unknown): unknown;
|
|
241
233
|
encode(value: unknown): unknown;
|
|
242
234
|
}
|
|
@@ -257,14 +249,14 @@ export declare class OperatorExpr extends BaseExpr {
|
|
|
257
249
|
get outputType(): TypeInfo | undefined;
|
|
258
250
|
get sqlNullable(): boolean | undefined;
|
|
259
251
|
get sqlSource(): object | undefined;
|
|
260
|
-
|
|
252
|
+
parseSql(ctx: ExprContext): ParsedSql;
|
|
261
253
|
}
|
|
262
254
|
/** A bound literal, i.e. the `?` in `coalesce(x, ?)`. */
|
|
263
255
|
export declare class BindingExpr extends BaseExpr {
|
|
264
256
|
private value;
|
|
265
257
|
constructor(value: unknown);
|
|
266
258
|
get sqlNullable(): boolean;
|
|
267
|
-
|
|
259
|
+
parseSql(): ParsedSql;
|
|
268
260
|
}
|
|
269
261
|
/** An unmodeled column on a known source, i.e. `a.column("ts_search")`. */
|
|
270
262
|
export declare class RefExpr extends BaseExpr {
|
|
@@ -272,7 +264,7 @@ export declare class RefExpr extends BaseExpr {
|
|
|
272
264
|
private column;
|
|
273
265
|
constructor(handle: object, column: string);
|
|
274
266
|
get sqlSource(): object;
|
|
275
|
-
|
|
267
|
+
parseSql(ctx: ExprContext): ParsedSql;
|
|
276
268
|
}
|
|
277
269
|
/**
|
|
278
270
|
* A `sql` tagged template.
|
|
@@ -289,12 +281,12 @@ export declare class TemplateExpr extends BaseExpr {
|
|
|
289
281
|
private strings;
|
|
290
282
|
private values;
|
|
291
283
|
constructor(strings: TemplateStringsArray, values: unknown[]);
|
|
292
|
-
|
|
284
|
+
parseSql(ctx: ExprContext): ParsedSql;
|
|
293
285
|
}
|
|
294
286
|
/** Turns one `${...}` of a `sql` template into SQL: an expression, a condition, or a bound value. */
|
|
295
|
-
export declare function interpolationToSql(value: unknown, ctx: ExprContext):
|
|
296
|
-
/**
|
|
287
|
+
export declare function interpolationToSql(value: unknown, ctx: ExprContext): ParsedSql;
|
|
288
|
+
/** Recognizes boolean groups, deferred predicates, and explicit column/raw conditions. */
|
|
297
289
|
export declare function isConditionLike(value: unknown): boolean;
|
|
298
290
|
/** Renders an expression order for either a query or an aggregate, or prunes an undefined order. */
|
|
299
|
-
export declare function orderByToSql(o: ExpressionOrderBy, ctx: ExprContext):
|
|
291
|
+
export declare function orderByToSql(o: ExpressionOrderBy, ctx: ExprContext): ParsedSql | undefined;
|
|
300
292
|
//# sourceMappingURL=Expr.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Expr.d.mts","names":[],"sources":["../../../src/queries/sql/Expr.ts"],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"Expr.d.mts","names":[],"sources":["../../../src/queries/sql/Expr.ts"],"mappings":"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"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { brandPredicate } from "../conditions.js";
|
|
2
2
|
import { skipCondition } from "../skipCondition.js";
|
|
3
|
-
import {
|
|
3
|
+
import { columnSql, joinSql } from "./ParsedSqlExpression.js";
|
|
4
4
|
import { arrayOutputType } from "../../serde/TypeInfo.js";
|
|
5
5
|
// src/queries/sql/Expr.ts
|
|
6
6
|
/**
|
|
@@ -28,53 +28,20 @@ const deferredSym = Symbol("joist.deferredCondition");
|
|
|
28
28
|
function isDeferredCondition(cond) {
|
|
29
29
|
return typeof cond === "object" && cond !== null && deferredSym in cond;
|
|
30
30
|
}
|
|
31
|
-
/** Creates a `DeferredCondition` that generates its SQL with `fn` once the query's aliases are known. */
|
|
32
|
-
function deferredCondition(fn) {
|
|
33
|
-
const cond = brandPredicate({
|
|
34
|
-
kind: "raw",
|
|
35
|
-
aliases: [],
|
|
36
|
-
condition: "<unresolved>",
|
|
37
|
-
bindings: [],
|
|
38
|
-
pruneable: false,
|
|
39
|
-
[deferredSym]: (ctx) => {
|
|
40
|
-
const { sql, bindings, refs } = fn(ctx);
|
|
41
|
-
return {
|
|
42
|
-
...cond,
|
|
43
|
-
condition: sql,
|
|
44
|
-
bindings,
|
|
45
|
-
aliases: refs
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
}, "sql");
|
|
49
|
-
return cond;
|
|
50
|
-
}
|
|
51
31
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
32
|
+
* Defers alias resolution until this predicate is parsed in a particular query.
|
|
33
|
+
*
|
|
34
|
+
* A condition can be created once and reused across em.query calls, where its sources may be
|
|
35
|
+
* assigned different SQL aliases.
|
|
54
36
|
*/
|
|
55
|
-
function
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
and: cond.and.map((c) => resolveDeferredConditions(c, ctx))
|
|
61
|
-
};
|
|
62
|
-
else if ("or" in cond && cond.or) return {
|
|
63
|
-
...cond,
|
|
64
|
-
or: cond.or.map((c) => resolveDeferredConditions(c, ctx))
|
|
65
|
-
};
|
|
66
|
-
return cond;
|
|
67
|
-
}
|
|
68
|
-
/** Concatenates SQL fragments with `sep`, keeping bindings and refs in order. */
|
|
69
|
-
function joinFragments(parts, sep) {
|
|
70
|
-
return {
|
|
71
|
-
sql: parts.map((p) => p.sql).join(sep),
|
|
72
|
-
bindings: parts.flatMap((p) => p.bindings),
|
|
73
|
-
refs: parts.flatMap((p) => p.refs)
|
|
74
|
-
};
|
|
37
|
+
function deferredCondition(fn) {
|
|
38
|
+
return brandPredicate({
|
|
39
|
+
kind: "deferred",
|
|
40
|
+
[deferredSym]: fn
|
|
41
|
+
}, "sql");
|
|
75
42
|
}
|
|
76
43
|
/**
|
|
77
|
-
* The methods every expression shares. Subclasses provide `
|
|
44
|
+
* The methods every expression shares. Subclasses provide `parseSql`, and usually `decode`/`encode`.
|
|
78
45
|
*
|
|
79
46
|
* Table columns can override comparisons to apply column-specific conversions. Domain aliases do not
|
|
80
47
|
* implement this protocol.
|
|
@@ -90,8 +57,8 @@ var BaseExpr = class {
|
|
|
90
57
|
/** The source of a direct column reference, whose value becomes NULL under an unmatched LEFT join. */
|
|
91
58
|
get sqlSource() {}
|
|
92
59
|
/** Produces the SQL without the outer parens a subquery normally gets; only differs for subqueries. */
|
|
93
|
-
|
|
94
|
-
return this.
|
|
60
|
+
parseSqlBare(ctx) {
|
|
61
|
+
return this.parseSql(ctx);
|
|
95
62
|
}
|
|
96
63
|
/** Converts a result-set value into the domain value, i.e. an int into a tagged id. */
|
|
97
64
|
decode(value) {
|
|
@@ -128,7 +95,7 @@ var BaseExpr = class {
|
|
|
128
95
|
/** Prefixes this expression to a SQL template, retaining bindings and referenced aliases. */
|
|
129
96
|
is(strings, ...values) {
|
|
130
97
|
const suffix = new TemplateExpr(strings, values);
|
|
131
|
-
return deferredCondition((ctx) =>
|
|
98
|
+
return deferredCondition((ctx) => joinSql([this.parseSql(ctx), suffix.parseSql(ctx)], " "));
|
|
132
99
|
}
|
|
133
100
|
/** Orders by this expression in ascending order. */
|
|
134
101
|
asc() {
|
|
@@ -223,43 +190,42 @@ var BaseExpr = class {
|
|
|
223
190
|
if (value === null) {
|
|
224
191
|
const not = op === "=" ? "" : op === "!=" ? "NOT " : fail(`Cannot compare ${op} to null`);
|
|
225
192
|
return deferredCondition((ctx) => {
|
|
226
|
-
const left = this.
|
|
227
|
-
return {
|
|
228
|
-
...left,
|
|
229
|
-
sql: `${left.sql} IS ${not}NULL`
|
|
230
|
-
};
|
|
193
|
+
const left = this.parseSql(ctx);
|
|
194
|
+
return joinSql([left, ` IS ${not}NULL`]);
|
|
231
195
|
});
|
|
232
196
|
}
|
|
233
|
-
if (isExpr(value)) return deferredCondition((ctx) =>
|
|
197
|
+
if (isExpr(value)) return deferredCondition((ctx) => joinSql([this.parseSql(ctx), asNode(value).parseSql(ctx)], ` ${op} `));
|
|
234
198
|
return deferredCondition((ctx) => {
|
|
235
|
-
const left = this.
|
|
236
|
-
return
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
199
|
+
const left = this.parseSql(ctx);
|
|
200
|
+
return joinSql([
|
|
201
|
+
left,
|
|
202
|
+
` ${op} `,
|
|
203
|
+
{ value: this.encode(value) }
|
|
204
|
+
]);
|
|
241
205
|
});
|
|
242
206
|
}
|
|
243
207
|
/** `this IN (subquery)` or `this = ANY(?)` for a list; `NOT IN` / `!= ALL(?)` for `nin`. */
|
|
244
208
|
inList(op, values) {
|
|
245
209
|
if (values === void 0) return skipCondition;
|
|
246
210
|
if (isExpr(values)) return deferredCondition((ctx) => {
|
|
247
|
-
const left = this.
|
|
248
|
-
const right = asNode(values).
|
|
249
|
-
return
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
211
|
+
const left = this.parseSql(ctx);
|
|
212
|
+
const right = asNode(values).parseSqlBare(ctx);
|
|
213
|
+
return joinSql([left, joinSql([
|
|
214
|
+
"(",
|
|
215
|
+
right,
|
|
216
|
+
")"
|
|
217
|
+
])], ` ${op} `);
|
|
253
218
|
});
|
|
254
219
|
if (!Array.isArray(values)) fail(`Expected an array or subquery for ${op}, got ${values}`);
|
|
255
|
-
const fn = op === "IN" ? "= ANY
|
|
220
|
+
const fn = op === "IN" ? "= ANY" : "!= ALL";
|
|
256
221
|
return deferredCondition((ctx) => {
|
|
257
|
-
const left = this.
|
|
258
|
-
return
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
222
|
+
const left = this.parseSql(ctx);
|
|
223
|
+
return joinSql([
|
|
224
|
+
left,
|
|
225
|
+
` ${fn}(`,
|
|
226
|
+
{ value: values.map((v) => this.encode(v)) },
|
|
227
|
+
")"
|
|
228
|
+
]);
|
|
263
229
|
});
|
|
264
230
|
}
|
|
265
231
|
};
|
|
@@ -300,28 +266,27 @@ var FnExpr = class extends BaseExpr {
|
|
|
300
266
|
* Renders function arguments, aggregate ordering, and the filter in SQL binding order.
|
|
301
267
|
* Keep references from all three so aggregate-only joins are retained.
|
|
302
268
|
*/
|
|
303
|
-
|
|
304
|
-
const args =
|
|
305
|
-
const ordering =
|
|
269
|
+
parseSql(ctx) {
|
|
270
|
+
const args = joinSql(this.args.map((a) => a.parseSql(ctx)), ", ");
|
|
271
|
+
const ordering = joinSql((this.opts.aggregate?.orderBy ?? []).flatMap((entry) => {
|
|
306
272
|
if (entry === void 0) return [];
|
|
307
273
|
const fragment = orderByToSql(entry, ctx);
|
|
308
274
|
return fragment ? [fragment] : [];
|
|
309
275
|
}), ", ");
|
|
310
276
|
const filter = this.opts.aggregate?.filter;
|
|
311
|
-
const condition = filter === void 0 ? void 0 : ctx.
|
|
312
|
-
return
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
};
|
|
277
|
+
const condition = filter === void 0 ? void 0 : ctx.parseCondition(filter);
|
|
278
|
+
return joinSql([
|
|
279
|
+
`${this.name}(${this.opts.prefix ?? ""}`,
|
|
280
|
+
args,
|
|
281
|
+
...ordering.parts.length ? [" ORDER BY ", ordering] : [],
|
|
282
|
+
")",
|
|
283
|
+
...condition ? [
|
|
284
|
+
" FILTER (WHERE ",
|
|
285
|
+
condition,
|
|
286
|
+
")"
|
|
287
|
+
] : [],
|
|
288
|
+
this.opts.suffix ?? ""
|
|
289
|
+
]);
|
|
325
290
|
}
|
|
326
291
|
decode(value) {
|
|
327
292
|
if (value === null || value === void 0) return value;
|
|
@@ -360,12 +325,14 @@ var OperatorExpr = class extends BaseExpr {
|
|
|
360
325
|
get sqlSource() {
|
|
361
326
|
return this.left.sqlSource;
|
|
362
327
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
328
|
+
parseSql(ctx) {
|
|
329
|
+
return joinSql([
|
|
330
|
+
"(",
|
|
331
|
+
this.left.parseSql(ctx),
|
|
332
|
+
` ${this.op} `,
|
|
333
|
+
this.right.parseSql(ctx),
|
|
334
|
+
")"
|
|
335
|
+
]);
|
|
369
336
|
}
|
|
370
337
|
};
|
|
371
338
|
/** A bound literal, i.e. the `?` in `coalesce(x, ?)`. */
|
|
@@ -378,12 +345,8 @@ var BindingExpr = class extends BaseExpr {
|
|
|
378
345
|
get sqlNullable() {
|
|
379
346
|
return this.value === null || this.value === void 0;
|
|
380
347
|
}
|
|
381
|
-
|
|
382
|
-
return {
|
|
383
|
-
sql: "?",
|
|
384
|
-
bindings: [this.value],
|
|
385
|
-
refs: []
|
|
386
|
-
};
|
|
348
|
+
parseSql() {
|
|
349
|
+
return joinSql([{ value: this.value }]);
|
|
387
350
|
}
|
|
388
351
|
};
|
|
389
352
|
/** An unmodeled column on a known source, i.e. `a.column("ts_search")`. */
|
|
@@ -398,13 +361,9 @@ var RefExpr = class extends BaseExpr {
|
|
|
398
361
|
get sqlSource() {
|
|
399
362
|
return this.handle;
|
|
400
363
|
}
|
|
401
|
-
|
|
364
|
+
parseSql(ctx) {
|
|
402
365
|
const alias = ctx.aliasFor(this.handle);
|
|
403
|
-
return
|
|
404
|
-
sql: `${safeKq(alias)}.${safeKq(this.column)}`,
|
|
405
|
-
bindings: [],
|
|
406
|
-
refs: [alias]
|
|
407
|
-
};
|
|
366
|
+
return columnSql(alias, this.column);
|
|
408
367
|
}
|
|
409
368
|
};
|
|
410
369
|
/**
|
|
@@ -426,38 +385,29 @@ var TemplateExpr = class extends BaseExpr {
|
|
|
426
385
|
this.strings = strings;
|
|
427
386
|
this.values = values;
|
|
428
387
|
}
|
|
429
|
-
|
|
388
|
+
parseSql(ctx) {
|
|
430
389
|
const parts = [];
|
|
431
390
|
this.strings.forEach((s, i) => {
|
|
432
|
-
parts.push(
|
|
433
|
-
sql: s,
|
|
434
|
-
bindings: [],
|
|
435
|
-
refs: []
|
|
436
|
-
});
|
|
391
|
+
parts.push(s);
|
|
437
392
|
if (i < this.values.length) parts.push(interpolationToSql(this.values[i], ctx));
|
|
438
393
|
});
|
|
439
|
-
return
|
|
394
|
+
return {
|
|
395
|
+
kind: "raw",
|
|
396
|
+
parts
|
|
397
|
+
};
|
|
440
398
|
}
|
|
441
399
|
};
|
|
442
400
|
/** Turns one `${...}` of a `sql` template into SQL: an expression, a condition, or a bound value. */
|
|
443
401
|
function interpolationToSql(value, ctx) {
|
|
444
|
-
if (isExpr(value)) return asNode(value).
|
|
445
|
-
else if (isConditionLike(value)) return ctx.
|
|
446
|
-
|
|
447
|
-
bindings: [],
|
|
448
|
-
refs: []
|
|
449
|
-
};
|
|
450
|
-
else return {
|
|
451
|
-
sql: "?",
|
|
452
|
-
bindings: [value],
|
|
453
|
-
refs: []
|
|
454
|
-
};
|
|
402
|
+
if (isExpr(value)) return asNode(value).parseSql(ctx);
|
|
403
|
+
else if (isConditionLike(value)) return ctx.parseCondition(value) ?? joinSql(["true"]);
|
|
404
|
+
else return joinSql([{ value }]);
|
|
455
405
|
}
|
|
456
|
-
/**
|
|
406
|
+
/** Recognizes boolean groups, deferred predicates, and explicit column/raw conditions. */
|
|
457
407
|
function isConditionLike(value) {
|
|
458
408
|
if (typeof value !== "object" || value === null) return false;
|
|
459
409
|
const v = value;
|
|
460
|
-
return "and" in v || "or" in v || v.kind === "column" || v.kind === "raw" || v.kind === "exists";
|
|
410
|
+
return "and" in v || "or" in v || v.kind === "deferred" || v.kind === "column" || v.kind === "raw" || v.kind === "exists";
|
|
461
411
|
}
|
|
462
412
|
/**
|
|
463
413
|
* Resolves the SQL result type of supported numeric aggregates; other overloads remain unknown.
|
|
@@ -530,12 +480,9 @@ function orderByToSql(o, ctx) {
|
|
|
530
480
|
if (o.order === void 0) return void 0;
|
|
531
481
|
if (o.order !== "ASC" && o.order !== "DESC") return fail(`Invalid orderBy direction '${o.order}'`);
|
|
532
482
|
if (o.nulls !== void 0 && o.nulls !== "first" && o.nulls !== "last") return fail(`Invalid orderBy nulls '${o.nulls}'`);
|
|
533
|
-
const fragment = o.sort.
|
|
483
|
+
const fragment = o.sort.parseSql(ctx);
|
|
534
484
|
const nulls = o.nulls ? ` NULLS ${o.nulls.toUpperCase()}` : "";
|
|
535
|
-
return {
|
|
536
|
-
...fragment,
|
|
537
|
-
sql: `${fragment.sql} ${o.order}${nulls}`
|
|
538
|
-
};
|
|
485
|
+
return joinSql([fragment, ` ${o.order}${nulls}`]);
|
|
539
486
|
}
|
|
540
487
|
function identity(value) {
|
|
541
488
|
return value;
|
|
@@ -543,6 +490,6 @@ function identity(value) {
|
|
|
543
490
|
function fail(message) {
|
|
544
491
|
throw new Error(message);
|
|
545
492
|
}
|
|
546
|
-
export { BaseExpr, BindingExpr, FnExpr, OperatorExpr, RefExpr, TemplateExpr, asNode, deferredCondition, deferredSym, exprBrand, interpolationToSql, isConditionLike, isDeferredCondition, isExpr,
|
|
493
|
+
export { BaseExpr, BindingExpr, FnExpr, OperatorExpr, RefExpr, TemplateExpr, asNode, deferredCondition, deferredSym, exprBrand, interpolationToSql, isConditionLike, isDeferredCondition, isExpr, orderByToSql, selectKeyBrand };
|
|
547
494
|
|
|
548
495
|
//# sourceMappingURL=Expr.js.map
|