effect-qb 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mysql.js +61270 -4259
- package/dist/postgres/metadata.js +176 -82
- package/dist/postgres.js +5903 -3316
- package/package.json +14 -1
- package/src/internal/aggregation-validation.ts +3 -3
- package/src/internal/case-analysis.d.ts +18 -0
- package/src/internal/case-analysis.ts +4 -4
- package/src/internal/coercion/analysis.d.ts +7 -0
- package/src/internal/{coercion-analysis.ts → coercion/analysis.ts} +3 -3
- package/src/internal/coercion/errors.d.ts +17 -0
- package/src/internal/{coercion-errors.ts → coercion/errors.ts} +1 -1
- package/src/internal/coercion/kind.d.ts +4 -0
- package/src/internal/{coercion-kind.ts → coercion/kind.ts} +2 -2
- package/src/internal/{coercion-normalize.ts → coercion/normalize.ts} +1 -1
- package/src/internal/coercion/rules.d.ts +6 -0
- package/src/internal/{coercion-rules.ts → coercion/rules.ts} +2 -2
- package/src/internal/column-state.d.ts +190 -0
- package/src/internal/column-state.ts +36 -47
- package/src/internal/column.ts +21 -305
- package/src/internal/datatypes/define.d.ts +17 -0
- package/src/internal/datatypes/define.ts +18 -4
- package/src/internal/datatypes/lookup.d.ts +44 -0
- package/src/internal/datatypes/lookup.ts +61 -152
- package/src/internal/datatypes/shape.d.ts +16 -0
- package/src/internal/datatypes/shape.ts +1 -1
- package/src/internal/derived-table.d.ts +4 -0
- package/src/internal/derived-table.ts +21 -16
- package/src/internal/dsl-mutation-runtime.ts +378 -0
- package/src/internal/dsl-plan-runtime.ts +387 -0
- package/src/internal/dsl-query-runtime.ts +160 -0
- package/src/internal/dsl-transaction-ddl-runtime.ts +263 -0
- package/src/internal/executor.ts +132 -31
- package/src/internal/expression-ast.ts +15 -5
- package/src/internal/grouping-key.d.ts +3 -0
- package/src/internal/grouping-key.ts +1 -1
- package/src/internal/implication-runtime.d.ts +15 -0
- package/src/internal/implication-runtime.ts +4 -4
- package/src/internal/json/ast.d.ts +30 -0
- package/src/internal/json/ast.ts +1 -1
- package/src/internal/json/errors.d.ts +8 -0
- package/src/internal/json/path.d.ts +75 -0
- package/src/internal/json/path.ts +1 -1
- package/src/internal/json/types.d.ts +62 -0
- package/src/internal/predicate/analysis.d.ts +20 -0
- package/src/internal/{predicate-analysis.ts → predicate/analysis.ts} +3 -3
- package/src/internal/predicate/atom.d.ts +28 -0
- package/src/internal/{predicate-branches.ts → predicate/branches.ts} +2 -2
- package/src/internal/predicate/context.d.ts +67 -0
- package/src/internal/{predicate-context.ts → predicate/context.ts} +8 -5
- package/src/internal/predicate/formula.d.ts +35 -0
- package/src/internal/{predicate-formula.ts → predicate/formula.ts} +1 -1
- package/src/internal/predicate/key.d.ts +11 -0
- package/src/internal/{predicate-key.ts → predicate/key.ts} +2 -2
- package/src/internal/{predicate-nnf.ts → predicate/nnf.ts} +2 -2
- package/src/internal/predicate/normalize.d.ts +53 -0
- package/src/internal/{predicate-normalize.ts → predicate/normalize.ts} +19 -19
- package/src/internal/predicate/runtime.d.ts +31 -0
- package/src/internal/{predicate-runtime.ts → predicate/runtime.ts} +9 -6
- package/src/internal/projection-alias.d.ts +13 -0
- package/src/internal/projections.d.ts +31 -0
- package/src/internal/projections.ts +1 -1
- package/src/internal/query-ast.d.ts +217 -0
- package/src/internal/query-ast.ts +1 -1
- package/src/internal/query-requirements.d.ts +20 -0
- package/src/internal/query.d.ts +775 -0
- package/src/internal/query.ts +389 -313
- package/src/internal/renderer.ts +7 -21
- package/src/internal/row-set.d.ts +53 -0
- package/src/internal/{plan.ts → row-set.ts} +11 -9
- package/src/internal/{runtime-normalize.ts → runtime/normalize.ts} +9 -31
- package/src/internal/{runtime-schema.ts → runtime/schema.ts} +13 -38
- package/src/internal/runtime/value.d.ts +22 -0
- package/src/internal/{runtime-value.ts → runtime/value.ts} +2 -2
- package/src/internal/scalar.d.ts +107 -0
- package/src/internal/scalar.ts +191 -0
- package/src/internal/schema-derivation.d.ts +105 -0
- package/src/internal/schema-expression.d.ts +18 -0
- package/src/internal/schema-expression.ts +38 -7
- package/src/internal/table-options.d.ts +94 -0
- package/src/internal/table-options.ts +8 -2
- package/src/internal/table.d.ts +173 -0
- package/src/internal/table.ts +32 -14
- package/src/mysql/column.ts +94 -18
- package/src/mysql/datatypes/index.ts +47 -7
- package/src/mysql/errors/generated.ts +57336 -0
- package/src/mysql/errors/index.ts +1 -0
- package/src/mysql/errors/normalize.ts +55 -53
- package/src/mysql/errors/types.ts +74 -0
- package/src/mysql/executor.ts +69 -7
- package/src/mysql/function/aggregate.ts +1 -5
- package/src/mysql/function/core.ts +1 -4
- package/src/mysql/function/index.ts +0 -1
- package/src/mysql/function/string.ts +1 -5
- package/src/mysql/function/temporal.ts +12 -15
- package/src/mysql/function/window.ts +1 -6
- package/src/{internal/mysql-dialect.ts → mysql/internal/dialect.ts} +1 -1
- package/src/{internal/mysql-query.ts → mysql/internal/dsl.ts} +1024 -2082
- package/src/{internal/mysql-renderer.ts → mysql/internal/renderer.ts} +6 -6
- package/src/mysql/internal/sql-expression-renderer.ts +1455 -0
- package/src/mysql/json.ts +2 -0
- package/src/mysql/query.ts +111 -91
- package/src/mysql/renderer.ts +1 -1
- package/src/mysql/table.ts +1 -1
- package/src/mysql.ts +6 -4
- package/src/postgres/cast.ts +11 -12
- package/src/postgres/column.ts +178 -46
- package/src/postgres/datatypes/index.d.ts +515 -0
- package/src/postgres/datatypes/index.ts +22 -13
- package/src/postgres/datatypes/spec.d.ts +412 -0
- package/src/postgres/errors/generated.ts +2636 -0
- package/src/postgres/errors/index.ts +1 -0
- package/src/postgres/errors/normalize.ts +47 -62
- package/src/postgres/errors/types.ts +92 -34
- package/src/postgres/executor.ts +37 -5
- package/src/postgres/function/aggregate.ts +1 -5
- package/src/postgres/function/core.ts +12 -6
- package/src/postgres/function/index.ts +0 -1
- package/src/postgres/function/string.ts +1 -5
- package/src/postgres/function/temporal.ts +12 -15
- package/src/postgres/function/window.ts +1 -6
- package/src/{internal/postgres-dialect.ts → postgres/internal/dialect.ts} +1 -1
- package/src/{internal/postgres-query.ts → postgres/internal/dsl.ts} +1085 -2089
- package/src/{internal/postgres-renderer.ts → postgres/internal/renderer.ts} +6 -6
- package/src/postgres/internal/schema-ddl.ts +108 -0
- package/src/{internal/postgres-schema-model.ts → postgres/internal/schema-model.ts} +12 -6
- package/src/{internal → postgres/internal}/sql-expression-renderer.ts +19 -17
- package/src/postgres/{function/json.ts → json.ts} +77 -85
- package/src/postgres/metadata.ts +2 -2
- package/src/postgres/query.ts +113 -89
- package/src/postgres/renderer.ts +3 -13
- package/src/postgres/schema-expression.ts +2 -1
- package/src/postgres/schema-management.ts +1 -1
- package/src/postgres/table.ts +12 -4
- package/src/postgres/type.ts +29 -2
- package/src/postgres.ts +6 -4
- package/src/internal/expression.ts +0 -327
- package/src/internal/schema-ddl.ts +0 -55
- package/src/mysql/function/json.ts +0 -4
- package/src/mysql/private/query.ts +0 -1
- package/src/postgres/private/query.ts +0 -1
- /package/src/internal/{predicate-atom.ts → predicate/atom.ts} +0 -0
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
import { type Pipeable } from "effect/Pipeable";
|
|
2
|
+
import type * as Schema from "effect/Schema";
|
|
3
|
+
import * as Expression from "./scalar.js";
|
|
4
|
+
import * as RowSet from "./row-set.js";
|
|
5
|
+
import * as Table from "./table.js";
|
|
6
|
+
import * as ExpressionAst from "./expression-ast.js";
|
|
7
|
+
import * as QueryAst from "./query-ast.js";
|
|
8
|
+
import type { JsonNode } from "./json/ast.js";
|
|
9
|
+
import type * as JsonPath from "./json/path.js";
|
|
10
|
+
import type { QueryCapability } from "./query-requirements.js";
|
|
11
|
+
import type { CaseBranchAssumeFalse, CaseBranchAssumeTrue, CaseBranchDecision } from "./case-analysis.js";
|
|
12
|
+
import type { ContradictsFormula, GuaranteedNonNullKeys, GuaranteedNullKeys, GuaranteedSourceNames } from "./predicate/analysis.js";
|
|
13
|
+
import type { ColumnKeyOfExpression } from "./predicate/key.js";
|
|
14
|
+
import type { PredicateFormula, TrueFormula } from "./predicate/formula.js";
|
|
15
|
+
export type { MergeCapabilities, MergeCapabilityTuple, QueryCapability, QueryRequirement } from "./query-requirements.js";
|
|
16
|
+
export type { ComparableDbType, RuntimeOfDbType, TextCompatibleDbType, CastableDbType } from "./coercion/analysis.js";
|
|
17
|
+
export type { CanonicalSegment as JsonPathSegment, DescendSegment as JsonPathDescendSegment, ExactSegment as JsonExactPathSegment, IndexSegment as JsonPathIndexSegment, IsExactPath as IsExactJsonPath, IsExactSegment as IsExactJsonPathSegment, KeySegment as JsonPathKeySegment, Path as JsonPath, SegmentsOf as JsonPathSegments, SliceSegment as JsonPathSliceSegment, WildcardSegment as JsonPathWildcardSegment } from "./json/path.js";
|
|
18
|
+
export type { JsonPathUsageError } from "./json/errors.js";
|
|
19
|
+
export type { JsonConcatResult, JsonDeleteAtPath, JsonInsertAtPath, JsonKeysResult, JsonLengthResult, JsonLiteralInput, JsonPrimitive, JsonSetAtPath, JsonTextResult, JsonTypeName, JsonValue, JsonValueAtPath, NormalizeJsonLiteral } from "./json/types.js";
|
|
20
|
+
export type { CoercionKind, CoercionKindOf } from "./coercion/kind.js";
|
|
21
|
+
export type { CanCastDbType, CanCompareDbTypes, CanContainDbTypes } from "./coercion/rules.js";
|
|
22
|
+
export type { ConflictClause, LockClause, QueryStatement, SetOperatorKind as SetOperator } from "./query-ast.js";
|
|
23
|
+
export { union_query_capabilities } from "./query-requirements.js";
|
|
24
|
+
/** Internal symbol used to preserve query-only phantom metadata through inference. */
|
|
25
|
+
export declare const QueryTypeId: unique symbol;
|
|
26
|
+
type InsertSourceState = "ready" | "missing";
|
|
27
|
+
/** Internal phantom state tracked on query plans. */
|
|
28
|
+
export interface QueryState<Outstanding extends string, AvailableNames extends string, Grouped extends string, Assumptions extends PredicateFormula, Capabilities extends QueryCapability, Statement extends QueryAst.QueryStatement, Target, InsertState extends InsertSourceState> {
|
|
29
|
+
readonly required: Outstanding;
|
|
30
|
+
readonly availableNames: AvailableNames;
|
|
31
|
+
readonly grouped: Grouped;
|
|
32
|
+
readonly assumptions: Assumptions;
|
|
33
|
+
readonly capabilities: Capabilities;
|
|
34
|
+
readonly statement: Statement;
|
|
35
|
+
readonly target: Target;
|
|
36
|
+
readonly insertSource: InsertState;
|
|
37
|
+
}
|
|
38
|
+
/** Effective SQL dialect carried by an expression. */
|
|
39
|
+
export type DialectOf<Value extends Expression.Any> = Value[typeof Expression.TypeId]["dialect"];
|
|
40
|
+
/** Source dependency union carried by an expression. */
|
|
41
|
+
export type DependenciesOf<Value extends Expression.Any> = Expression.DependenciesOf<Value>;
|
|
42
|
+
/** Aggregation kind carried by an expression. */
|
|
43
|
+
export type KindOf<Value extends Expression.Any> = Value[typeof Expression.TypeId]["kind"];
|
|
44
|
+
type AstOf<Value extends Expression.Any> = Value extends {
|
|
45
|
+
readonly [ExpressionAst.TypeId]: infer Ast extends ExpressionAst.Any;
|
|
46
|
+
} ? Ast : never;
|
|
47
|
+
/**
|
|
48
|
+
* Primitive values that can be lifted directly into constant SQL expressions.
|
|
49
|
+
*
|
|
50
|
+
* This is the explicit surface today. Later coercion helpers can accept these
|
|
51
|
+
* primitives and normalize them through `literal(...)`.
|
|
52
|
+
*/
|
|
53
|
+
export type LiteralValue = string | number | boolean | null | Date;
|
|
54
|
+
/** Runtime expression type produced by `literal(...)` for a primitive value. */
|
|
55
|
+
type LiteralExpression<Value extends LiteralValue> = Expression.Scalar<Value, LiteralDbType<Value>, LiteralNullability<Value>, string, "scalar", never>;
|
|
56
|
+
/**
|
|
57
|
+
* Values accepted by scalar query operators.
|
|
58
|
+
*
|
|
59
|
+
* Raw primitives are automatically lifted into constant SQL expressions at the
|
|
60
|
+
* operator boundary.
|
|
61
|
+
*/
|
|
62
|
+
export type ExpressionInput = Expression.Any | LiteralValue;
|
|
63
|
+
/** Input accepted by numeric clauses such as `limit(...)` and `offset(...)`. */
|
|
64
|
+
export type NumericExpressionInput = Expression.Scalar<number, Expression.DbType.Any, Expression.Nullability, string, "scalar", Expression.BindingId> | number;
|
|
65
|
+
/** Values accepted by mutation payload fields. */
|
|
66
|
+
export type MutationValueInput<Value> = Value | Expression.Scalar<Value, Expression.DbType.Any, Expression.Nullability, string, Expression.ScalarKind, Expression.BindingId>;
|
|
67
|
+
/** Maps a payload shape to values or expressions of the same runtime type. */
|
|
68
|
+
export type MutationInputOf<Shape> = {
|
|
69
|
+
readonly [K in keyof Shape]: MutationValueInput<Shape[K]>;
|
|
70
|
+
};
|
|
71
|
+
type Simplify<T> = {
|
|
72
|
+
readonly [K in keyof T]: T[K];
|
|
73
|
+
} & {};
|
|
74
|
+
/** Input accepted by boolean plan clauses such as `where(...)` and joins. */
|
|
75
|
+
export type PredicateInput = Expression.Scalar<boolean, Expression.DbType.Any, Expression.Nullability, string, "scalar", Expression.BindingId> | boolean;
|
|
76
|
+
/** Input accepted by `having(...)`. */
|
|
77
|
+
export type HavingPredicateInput = Expression.Scalar<boolean, Expression.DbType.Any, Expression.Nullability, string, "scalar" | "aggregate", Expression.BindingId> | boolean;
|
|
78
|
+
/** Input accepted by `GROUP BY`. */
|
|
79
|
+
export type GroupByInput = Expression.Scalar<any, Expression.DbType.Any, Expression.Nullability, string, "scalar", Expression.BindingId>;
|
|
80
|
+
/** Maps a literal runtime value to its SQL-level DB type descriptor. */
|
|
81
|
+
type LiteralDbType<Value extends LiteralValue> = Value extends string ? Expression.DbType.Base<string, "text"> : Value extends number ? Expression.DbType.Base<string, "numeric"> : Value extends boolean ? Expression.DbType.Base<string, "boolean"> : Value extends Date ? Expression.DbType.Base<string, "timestamp"> : Expression.DbType.Base<string, "null">;
|
|
82
|
+
/** Maps a literal runtime value to its static nullability state. */
|
|
83
|
+
type LiteralNullability<Value extends LiteralValue> = Value extends null ? "always" : "never";
|
|
84
|
+
/** Converts a supported input into its canonical expression type. */
|
|
85
|
+
type AsExpression<Value extends ExpressionInput> = Value extends Expression.Any ? Value : LiteralExpression<Extract<Value, LiteralValue>>;
|
|
86
|
+
/** Extracts dependencies from an operator input after coercion. */
|
|
87
|
+
type DependenciesOfInput<Value extends ExpressionInput> = DependenciesOf<AsExpression<Value>>;
|
|
88
|
+
/** Extracts required bindings from an operator input after coercion. */
|
|
89
|
+
type RequiredFromInput<Value extends ExpressionInput> = DependenciesOfInput<Value>;
|
|
90
|
+
/** String-valued expressions accepted by text operators. */
|
|
91
|
+
export type StringExpressionInput = Expression.Scalar<string | null, Expression.DbType.Any, Expression.Nullability, string, Expression.ScalarKind, Expression.BindingId> | string;
|
|
92
|
+
/** Extracts required bindings from an expression dependency union. */
|
|
93
|
+
export type RequiredFromDependencies<Dependencies extends Expression.BindingId> = Dependencies;
|
|
94
|
+
type LiteralGroupingKey<Value> = Value extends string ? `string:${Value}` : Value extends number ? `number:${Value}` : Value extends boolean ? `boolean:${Value}` : Value extends null ? "null" : Value extends Date ? `date:${string}` : "unknown";
|
|
95
|
+
type JoinGroupingKeys<Keys extends readonly string[]> = Keys extends readonly [] ? "" : Keys extends readonly [infer Head extends string] ? Head : Keys extends readonly [infer Head extends string, ...infer Tail extends readonly string[]] ? `${Head},${JoinGroupingKeys<Tail>}` : string;
|
|
96
|
+
type JsonSegmentGroupingKey<Segment> = Segment extends JsonPath.KeySegment<infer Key extends string> ? `key:${Key}` : Segment extends JsonPath.IndexSegment<infer Index extends number> ? `index:${Index}` : Segment extends JsonPath.WildcardSegment ? "wildcard" : Segment extends JsonPath.SliceSegment<infer Start extends number | undefined, infer End extends number | undefined> ? `slice:${Start extends number ? Start : ""}:${End extends number ? End : ""}` : Segment extends JsonPath.DescendSegment ? "descend" : Segment extends string ? `key:${Segment}` : Segment extends number ? `index:${Segment}` : "unknown";
|
|
97
|
+
type JsonPathGroupingKey<Segments extends readonly any[]> = Segments extends readonly [] ? "" : Segments extends readonly [infer Head] ? JsonSegmentGroupingKey<Head> : Segments extends readonly [infer Head, ...infer Tail extends readonly any[]] ? `${JsonSegmentGroupingKey<Head>},${JsonPathGroupingKey<Tail>}` : string;
|
|
98
|
+
type JsonOpaquePathGroupingKey<Value> = Value extends JsonPath.Path<infer Segments extends readonly JsonPath.CanonicalSegment[]> ? `jsonpath:${JsonPathGroupingKey<Segments>}` : Value extends string ? `jsonpath:${Value}` : Value extends Expression.Any ? `jsonpath:${GroupingKeyOfExpression<Value>}` : "jsonpath:unknown";
|
|
99
|
+
type JsonEntryGroupingKey<Entry> = Entry extends {
|
|
100
|
+
readonly key: infer Key extends string;
|
|
101
|
+
readonly value: infer Value extends Expression.Any;
|
|
102
|
+
} ? `${Key}=>${GroupingKeyOfExpression<Value>}` : "entry:unknown";
|
|
103
|
+
type JsonEntriesGroupingKey<Entries extends readonly {
|
|
104
|
+
readonly key: string;
|
|
105
|
+
readonly value: Expression.Any;
|
|
106
|
+
}[]> = Entries extends readonly [] ? "" : Entries extends readonly [infer Head] ? JsonEntryGroupingKey<Head> : Entries extends readonly [infer Head, ...infer Tail extends readonly {
|
|
107
|
+
readonly key: string;
|
|
108
|
+
readonly value: Expression.Any;
|
|
109
|
+
}[]] ? `${JsonEntryGroupingKey<Head>}|${JsonEntriesGroupingKey<Tail>}` : string;
|
|
110
|
+
type BranchGroupingKeys<Branches extends readonly ExpressionAst.CaseBranchNode[]> = Branches extends readonly [] ? "" : Branches extends readonly [infer Head extends ExpressionAst.CaseBranchNode] ? `when:${GroupingKeyOfExpression<Head["when"]>}=>${GroupingKeyOfExpression<Head["then"]>}` : Branches extends readonly [
|
|
111
|
+
infer Head extends ExpressionAst.CaseBranchNode,
|
|
112
|
+
...infer Tail extends readonly ExpressionAst.CaseBranchNode[]
|
|
113
|
+
] ? `when:${GroupingKeyOfExpression<Head["when"]>}=>${GroupingKeyOfExpression<Head["then"]>}|${BranchGroupingKeys<Tail>}` : string;
|
|
114
|
+
type GroupingKeyOfAst<Ast extends ExpressionAst.Any> = Ast extends ExpressionAst.ColumnNode<infer TableName extends string, infer ColumnName extends string> ? `column:${TableName}.${ColumnName}` : Ast extends ExpressionAst.LiteralNode<infer Value> ? `literal:${LiteralGroupingKey<Value>}` : Ast extends ExpressionAst.ExcludedNode<infer ColumnName extends string> ? `excluded:${ColumnName}` : Ast extends ExpressionAst.CastNode<infer Value extends Expression.Any, infer Target extends Expression.DbType.Any> ? `cast(${GroupingKeyOfExpression<Value>} as ${Target["dialect"]}:${Target["kind"]})` : Ast extends ExpressionAst.UnaryNode<infer Kind extends ExpressionAst.UnaryKind, infer Value extends Expression.Any> ? `${Kind}(${GroupingKeyOfExpression<Value>})` : Ast extends ExpressionAst.BinaryNode<infer Kind extends ExpressionAst.BinaryKind, infer Left extends Expression.Any, infer Right extends Expression.Any> ? `${Kind}(${GroupingKeyOfExpression<Left>},${GroupingKeyOfExpression<Right>})` : Ast extends ExpressionAst.VariadicNode<infer Kind extends ExpressionAst.VariadicKind, infer Values extends readonly Expression.Any[]> ? `${Kind}(${JoinGroupingKeys<{
|
|
115
|
+
readonly [K in keyof Values]: Values[K] extends Expression.Any ? GroupingKeyOfExpression<Values[K]> : never;
|
|
116
|
+
} & readonly string[]>})` : Ast extends JsonNode<infer Kind> ? Kind extends "jsonGet" | "jsonPath" | "jsonAccess" | "jsonTraverse" | "jsonGetText" | "jsonPathText" | "jsonAccessText" | "jsonTraverseText" ? `json(${Kind},${GroupingKeyOfExpression<Extract<Ast["value"] | Ast["base"] | Ast["left"], Expression.Any>>},${JsonPathGroupingKey<Extract<Ast["segments"] | Ast["path"], readonly any[]>>})` : Kind extends "jsonHasKey" | "jsonKeyExists" | "jsonHasAnyKeys" | "jsonHasAllKeys" ? `json(${Kind},${GroupingKeyOfExpression<Extract<Ast["value"] | Ast["base"] | Ast["left"], Expression.Any>>},${JoinGroupingKeys<Extract<Ast["keys"], readonly string[]> & readonly string[]>})` : Kind extends "jsonConcat" | "jsonMerge" | "jsonDelete" | "jsonDeletePath" | "jsonRemove" | "jsonSet" | "jsonInsert" ? `json(${Kind},${GroupingKeyOfExpression<Extract<Ast["left"] | Ast["base"] | Ast["value"], Expression.Any>>},${GroupingKeyOfExpression<Extract<Ast["right"] | Ast["newValue"] | Ast["insert"], Expression.Any>>},${JsonPathGroupingKey<Extract<Ast["segments"] | Ast["path"], readonly any[]>>})` : Kind extends "jsonPathExists" | "jsonPathMatch" ? `json(${Kind},${GroupingKeyOfExpression<Extract<Ast["value"] | Ast["base"], Expression.Any>>},${JsonOpaquePathGroupingKey<Ast["query"] | Ast["path"]>})` : Kind extends "jsonBuildObject" ? `json(${Kind},${JsonEntriesGroupingKey<Extract<Ast["entries"], readonly {
|
|
117
|
+
readonly key: string;
|
|
118
|
+
readonly value: Expression.Any;
|
|
119
|
+
}[]>>})` : Kind extends "jsonBuildArray" ? `json(${Kind},${JoinGroupingKeys<{
|
|
120
|
+
readonly [K in keyof Extract<Ast["values"], readonly Expression.Any[]>]: Extract<Ast["values"], readonly Expression.Any[]>[K] extends Expression.Any ? GroupingKeyOfExpression<Extract<Ast["values"], readonly Expression.Any[]>[K]> : never;
|
|
121
|
+
} & readonly string[]>})` : Kind extends "jsonToJson" | "jsonToJsonb" | "jsonTypeOf" | "jsonLength" | "jsonKeys" | "jsonStripNulls" ? `json(${Kind},${GroupingKeyOfExpression<Extract<Ast["value"], Expression.Any>>})` : never : Ast extends ExpressionAst.CaseNode<infer Branches extends readonly ExpressionAst.CaseBranchNode[], infer Else extends Expression.Any> ? `case(${BranchGroupingKeys<Branches>};else:${GroupingKeyOfExpression<Else>})` : Ast extends ExpressionAst.ExistsNode ? "exists(subquery)" : never;
|
|
122
|
+
/** Canonical grouping identity for an expression. */
|
|
123
|
+
export type GroupingKeyOfExpression<Value extends Expression.Any> = Expression.GroupKeyOf<Value> extends infer GroupKey extends string ? string extends GroupKey ? GroupingKeyOfAst<AstOf<Value>> : GroupKey : GroupingKeyOfAst<AstOf<Value>>;
|
|
124
|
+
/**
|
|
125
|
+
* Recursive selection tree accepted by `select(...)`.
|
|
126
|
+
*
|
|
127
|
+
* A selection can be either:
|
|
128
|
+
* - a leaf SQL expression
|
|
129
|
+
* - a nested object whose leaves are expressions
|
|
130
|
+
*/
|
|
131
|
+
export type SelectionShape = Expression.Any | {
|
|
132
|
+
readonly [key: string]: SelectionShape;
|
|
133
|
+
};
|
|
134
|
+
/** Walks a selection tree and unions the table names referenced by its leaves. */
|
|
135
|
+
export type ExtractRequired<Selection> = Selection extends Expression.Any ? RequiredFromDependencies<DependenciesOf<Selection>> : Selection extends Record<string, any> ? {
|
|
136
|
+
[K in keyof Selection]: ExtractRequired<Selection[K]>;
|
|
137
|
+
}[keyof Selection] : never;
|
|
138
|
+
/** Walks a selection tree and unions the dialects referenced by its leaves. */
|
|
139
|
+
export type ExtractDialect<Selection> = Selection extends Expression.Any ? DialectOf<Selection> : Selection extends Record<string, any> ? {
|
|
140
|
+
[K in keyof Selection]: ExtractDialect<Selection[K]>;
|
|
141
|
+
}[keyof Selection] : never;
|
|
142
|
+
/**
|
|
143
|
+
* Minimal table-like shape required by `from(...)` and joins.
|
|
144
|
+
*
|
|
145
|
+
* The query layer only needs the plan metadata and the static table name. It
|
|
146
|
+
* deliberately avoids depending on the full table-definition surface.
|
|
147
|
+
*/
|
|
148
|
+
export type TableLike<Name extends string = string, Dialect extends string = string> = RowSet.RowSet<any, any, Record<string, RowSet.AnySource>, Dialect> & {
|
|
149
|
+
readonly [Table.TypeId]: {
|
|
150
|
+
readonly name: Name;
|
|
151
|
+
readonly baseName: string;
|
|
152
|
+
readonly schemaName?: string;
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
/** Concrete schema table accepted by DDL builders. */
|
|
156
|
+
export type SchemaTableLike = Table.TableDefinition<any, any, any, "schema", any> | Table.TableClassStatic<any, any, any, any>;
|
|
157
|
+
/**
|
|
158
|
+
* Wrapper returned by `as(subquery, alias)` for derived-table composition.
|
|
159
|
+
*
|
|
160
|
+
* The derived source exposes the subquery output under the new alias and can
|
|
161
|
+
* be passed to `from(...)` or join builders.
|
|
162
|
+
*/
|
|
163
|
+
export type DerivedSource<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, Alias extends string> = DerivedSelectionOf<SelectionOfPlan<PlanValue>, Alias> & {
|
|
164
|
+
readonly kind: "derived";
|
|
165
|
+
readonly name: Alias;
|
|
166
|
+
readonly baseName: Alias;
|
|
167
|
+
readonly dialect: PlanDialectOf<PlanValue>;
|
|
168
|
+
readonly plan: CompletePlan<PlanValue>;
|
|
169
|
+
readonly required?: never;
|
|
170
|
+
readonly columns: DerivedSelectionOf<SelectionOfPlan<PlanValue>, Alias>;
|
|
171
|
+
};
|
|
172
|
+
/** Wrapper returned by `with(subquery, alias)` for common table expression composition. */
|
|
173
|
+
export type CteSource<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, Alias extends string> = DerivedSelectionOf<SelectionOfPlan<PlanValue>, Alias> & {
|
|
174
|
+
readonly kind: "cte";
|
|
175
|
+
readonly name: Alias;
|
|
176
|
+
readonly baseName: Alias;
|
|
177
|
+
readonly dialect: PlanDialectOf<PlanValue>;
|
|
178
|
+
readonly plan: CompletePlan<PlanValue>;
|
|
179
|
+
readonly recursive?: boolean;
|
|
180
|
+
readonly columns: DerivedSelectionOf<SelectionOfPlan<PlanValue>, Alias>;
|
|
181
|
+
};
|
|
182
|
+
/** Wrapper returned by `lateral(subquery, alias)` for correlated derived sources. */
|
|
183
|
+
export type LateralSource<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, Alias extends string> = DerivedSelectionOf<SelectionOfPlan<PlanValue>, Alias> & {
|
|
184
|
+
readonly kind: "lateral";
|
|
185
|
+
readonly name: Alias;
|
|
186
|
+
readonly baseName: Alias;
|
|
187
|
+
readonly dialect: PlanDialectOf<PlanValue>;
|
|
188
|
+
readonly plan: PlanValue;
|
|
189
|
+
readonly required: RequiredOfPlan<PlanValue>;
|
|
190
|
+
readonly columns: DerivedSelectionOf<SelectionOfPlan<PlanValue>, Alias>;
|
|
191
|
+
};
|
|
192
|
+
type ValuesRowInput = Record<string, ExpressionInput>;
|
|
193
|
+
/** Anonymous `values(...)` input that must be aliased through `as(...)` before use as a source. */
|
|
194
|
+
export type ValuesInput<Rows extends readonly [ValuesRowInput, ...ValuesRowInput[]], Selection extends SelectionShape, Dialect extends string> = Pipeable & {
|
|
195
|
+
readonly kind: "values";
|
|
196
|
+
readonly dialect: Dialect;
|
|
197
|
+
readonly rows: readonly [Record<string, Expression.Any>, ...Record<string, Expression.Any>[]];
|
|
198
|
+
readonly selection: Selection;
|
|
199
|
+
};
|
|
200
|
+
/** Wrapper returned by `as(values(rows), alias)` for standalone row sources and insert sources. */
|
|
201
|
+
export type ValuesSource<Rows extends readonly [ValuesRowInput, ...ValuesRowInput[]], Selection extends SelectionShape, Alias extends string, Dialect extends string> = DerivedSelectionOf<Selection, Alias> & {
|
|
202
|
+
readonly kind: "values";
|
|
203
|
+
readonly name: Alias;
|
|
204
|
+
readonly baseName: Alias;
|
|
205
|
+
readonly dialect: Dialect;
|
|
206
|
+
readonly rows: readonly [Record<string, Expression.Any>, ...Record<string, Expression.Any>[]];
|
|
207
|
+
readonly columns: DerivedSelectionOf<Selection, Alias>;
|
|
208
|
+
};
|
|
209
|
+
/** Broad structural shape for anonymous `values(...)` inputs before aliasing. */
|
|
210
|
+
export type AnyValuesInput = {
|
|
211
|
+
readonly kind: "values";
|
|
212
|
+
readonly dialect: string;
|
|
213
|
+
readonly rows: readonly Record<string, Expression.Any>[];
|
|
214
|
+
readonly selection: Record<string, Expression.Any>;
|
|
215
|
+
};
|
|
216
|
+
/** Broad structural shape for `values(...)` sources when used as composable inputs. */
|
|
217
|
+
export type AnyValuesSource = {
|
|
218
|
+
readonly kind: "values";
|
|
219
|
+
readonly name: string;
|
|
220
|
+
readonly baseName: string;
|
|
221
|
+
readonly dialect: string;
|
|
222
|
+
readonly rows: readonly Record<string, Expression.Any>[];
|
|
223
|
+
readonly columns: Record<string, Expression.Any>;
|
|
224
|
+
};
|
|
225
|
+
/** Wrapper returned by `unnest(columns, alias)` for standalone array sources. */
|
|
226
|
+
export type UnnestSource<Selection extends SelectionShape, Alias extends string, Dialect extends string> = DerivedSelectionOf<Selection, Alias> & {
|
|
227
|
+
readonly kind: "unnest";
|
|
228
|
+
readonly name: Alias;
|
|
229
|
+
readonly baseName: Alias;
|
|
230
|
+
readonly dialect: Dialect;
|
|
231
|
+
readonly values: Readonly<Record<string, readonly ExpressionInput[]>>;
|
|
232
|
+
readonly arrays: Readonly<Record<string, readonly Expression.Any[]>>;
|
|
233
|
+
readonly columns: DerivedSelectionOf<Selection, Alias>;
|
|
234
|
+
};
|
|
235
|
+
/** Wrapper returned by `generateSeries(...)` and similar table functions. */
|
|
236
|
+
export type TableFunctionSource<Selection extends SelectionShape, Alias extends string, Dialect extends string, FunctionName extends string = string> = DerivedSelectionOf<Selection, Alias> & {
|
|
237
|
+
readonly kind: "tableFunction";
|
|
238
|
+
readonly name: Alias;
|
|
239
|
+
readonly baseName: Alias;
|
|
240
|
+
readonly dialect: Dialect;
|
|
241
|
+
readonly functionName: FunctionName;
|
|
242
|
+
readonly args: readonly Expression.Any[];
|
|
243
|
+
readonly columns: DerivedSelectionOf<Selection, Alias>;
|
|
244
|
+
};
|
|
245
|
+
/** Accepts either a physical table or a derived table source. */
|
|
246
|
+
type DerivedSourceShape = {
|
|
247
|
+
readonly kind: "derived";
|
|
248
|
+
readonly name: string;
|
|
249
|
+
readonly baseName: string;
|
|
250
|
+
readonly dialect: string;
|
|
251
|
+
readonly plan: QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
252
|
+
readonly columns: Record<string, unknown>;
|
|
253
|
+
};
|
|
254
|
+
type CteSourceShape = {
|
|
255
|
+
readonly kind: "cte";
|
|
256
|
+
readonly name: string;
|
|
257
|
+
readonly baseName: string;
|
|
258
|
+
readonly dialect: string;
|
|
259
|
+
readonly plan: QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
260
|
+
readonly recursive?: boolean;
|
|
261
|
+
readonly required?: never;
|
|
262
|
+
readonly columns: Record<string, unknown>;
|
|
263
|
+
};
|
|
264
|
+
type LateralSourceShape = {
|
|
265
|
+
readonly kind: "lateral";
|
|
266
|
+
readonly name: string;
|
|
267
|
+
readonly baseName: string;
|
|
268
|
+
readonly dialect: string;
|
|
269
|
+
readonly plan: QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
270
|
+
readonly required: string;
|
|
271
|
+
readonly columns: Record<string, unknown>;
|
|
272
|
+
};
|
|
273
|
+
type ValuesSourceShape = {
|
|
274
|
+
readonly kind: "values";
|
|
275
|
+
readonly name: string;
|
|
276
|
+
readonly baseName: string;
|
|
277
|
+
readonly dialect: string;
|
|
278
|
+
readonly rows: readonly Record<string, Expression.Any>[];
|
|
279
|
+
readonly columns: Record<string, unknown>;
|
|
280
|
+
};
|
|
281
|
+
type UnnestSourceShape = {
|
|
282
|
+
readonly kind: "unnest";
|
|
283
|
+
readonly name: string;
|
|
284
|
+
readonly baseName: string;
|
|
285
|
+
readonly dialect: string;
|
|
286
|
+
readonly values: Readonly<Record<string, readonly ExpressionInput[]>>;
|
|
287
|
+
readonly arrays: Readonly<Record<string, readonly Expression.Any[]>>;
|
|
288
|
+
readonly columns: Record<string, unknown>;
|
|
289
|
+
};
|
|
290
|
+
/** Broad structural shape for `unnest(...)` sources when used as composable inputs. */
|
|
291
|
+
export type AnyUnnestSource = {
|
|
292
|
+
readonly kind: "unnest";
|
|
293
|
+
readonly name: string;
|
|
294
|
+
readonly baseName: string;
|
|
295
|
+
readonly dialect: string;
|
|
296
|
+
readonly values: Readonly<Record<string, readonly ExpressionInput[]>>;
|
|
297
|
+
readonly arrays: Readonly<Record<string, readonly Expression.Any[]>>;
|
|
298
|
+
readonly columns: Record<string, Expression.Any>;
|
|
299
|
+
};
|
|
300
|
+
type TableFunctionSourceShape = {
|
|
301
|
+
readonly kind: "tableFunction";
|
|
302
|
+
readonly name: string;
|
|
303
|
+
readonly baseName: string;
|
|
304
|
+
readonly dialect: string;
|
|
305
|
+
readonly functionName: string;
|
|
306
|
+
readonly args: readonly Expression.Any[];
|
|
307
|
+
readonly columns: Record<string, unknown>;
|
|
308
|
+
};
|
|
309
|
+
type DerivedSourceAliasError = DerivedSourceRequiredError<QueryPlan<any, any, any, any, any, any, any, any, any, any>>;
|
|
310
|
+
export type SourceLike = TableLike<any, any> | DerivedSourceShape | CteSourceShape | LateralSourceShape | ValuesSourceShape | UnnestSourceShape | TableFunctionSourceShape | DerivedSourceAliasError;
|
|
311
|
+
/** Concrete table sources that can be targeted by mutation statements. */
|
|
312
|
+
export type MutationTargetLike = Table.AnyTable;
|
|
313
|
+
export type MutationTargetTuple = readonly [MutationTargetLike, MutationTargetLike, ...MutationTargetLike[]];
|
|
314
|
+
export type MutationTargetInput = MutationTargetLike | MutationTargetTuple;
|
|
315
|
+
type JsonMutationDbKindError<ColumnName extends string, ExpectedKind extends string, ReceivedKind extends string> = {
|
|
316
|
+
readonly __effect_qb_error__: "effect-qb: incompatible json mutation value";
|
|
317
|
+
readonly __effect_qb_column__: ColumnName;
|
|
318
|
+
readonly __effect_qb_reason__: "json kinds do not match";
|
|
319
|
+
readonly __effect_qb_expected_json_kind__: ExpectedKind;
|
|
320
|
+
readonly __effect_qb_received_json_kind__: ReceivedKind;
|
|
321
|
+
readonly __effect_qb_hint__: "Use the dialect Json helpers that match the target json kind";
|
|
322
|
+
};
|
|
323
|
+
type JsonMutationShapeError<ColumnName extends string, Expected, Received> = {
|
|
324
|
+
readonly __effect_qb_error__: "effect-qb: incompatible json mutation value";
|
|
325
|
+
readonly __effect_qb_column__: ColumnName;
|
|
326
|
+
readonly __effect_qb_reason__: "json value is not assignable to the target column schema";
|
|
327
|
+
readonly __effect_qb_json_issues__: JsonShapeIssues<Expected, Received>;
|
|
328
|
+
readonly __effect_qb_hint__: "Inspect __effect_qb_json_issues__ or the __effect_qb_json_issue__... sentinel keys to find the failing nested paths";
|
|
329
|
+
} & JsonIssueSentinels<Expected, Received>;
|
|
330
|
+
type MutationValueShapeError<ColumnName extends string, Expected, Received> = {
|
|
331
|
+
readonly __effect_qb_error__: "effect-qb: incompatible mutation value";
|
|
332
|
+
readonly __effect_qb_column__: ColumnName;
|
|
333
|
+
readonly __effect_qb_reason__: "value is not assignable to the target column schema";
|
|
334
|
+
readonly __effect_qb_expected_type__: Expected;
|
|
335
|
+
readonly __effect_qb_received_type__: Received;
|
|
336
|
+
};
|
|
337
|
+
type MutationUnknownColumnError<ColumnName extends string> = {
|
|
338
|
+
readonly __effect_qb_error__: "effect-qb: unknown mutation column";
|
|
339
|
+
readonly __effect_qb_column__: ColumnName;
|
|
340
|
+
readonly __effect_qb_hint__: "Use only columns that exist on the target table";
|
|
341
|
+
};
|
|
342
|
+
type MutationRequiredKeys<Shape> = Extract<{
|
|
343
|
+
[K in keyof Shape]-?: {} extends Pick<Shape, K> ? never : K;
|
|
344
|
+
}[keyof Shape], string>;
|
|
345
|
+
type MutationMissingKeys<TargetShape, SourceShape> = Exclude<MutationRequiredKeys<TargetShape>, Extract<keyof SourceShape, string>>;
|
|
346
|
+
type JsonPathDisplay<Path extends string> = Path extends "" ? "(root)" : Path;
|
|
347
|
+
type JsonObjectKeyPath<Path extends string, Key extends string> = Path extends "" ? Key : `${Path}.${Key}`;
|
|
348
|
+
type JsonArrayPath<Path extends string> = Path extends "" ? "[number]" : `${Path}[number]`;
|
|
349
|
+
type JsonTupleIndexPath<Path extends string, Index extends string> = Path extends "" ? `[${Index}]` : `${Path}[${Index}]`;
|
|
350
|
+
type JsonIssueKeyPath<Path extends string> = Path extends "" ? "root" : Path;
|
|
351
|
+
type JsonShapeIssue<Path extends string, Kind extends string, Expected, Received> = {
|
|
352
|
+
readonly path: JsonPathDisplay<Path>;
|
|
353
|
+
readonly issue: Kind;
|
|
354
|
+
readonly expected: Expected;
|
|
355
|
+
readonly received: Received;
|
|
356
|
+
};
|
|
357
|
+
type JsonSharedKeys<Expected, Received> = Extract<Extract<keyof Expected, keyof Received>, string>;
|
|
358
|
+
type JsonIssueSentinelKeys<Issues> = Issues extends {
|
|
359
|
+
readonly path: infer Path extends string;
|
|
360
|
+
readonly issue: infer Kind extends string;
|
|
361
|
+
} ? `__effect_qb_json_issue__${JsonIssueKeyPath<Path>}__${Kind}` : never;
|
|
362
|
+
type JsonIssueSentinels<Expected, Received> = {
|
|
363
|
+
readonly [K in JsonIssueSentinelKeys<JsonShapeIssues<Expected, Received>>]: true;
|
|
364
|
+
};
|
|
365
|
+
type JsonObjectShapeIssues<Expected extends object, Received extends object, Path extends string> = {
|
|
366
|
+
readonly [K in MutationMissingKeys<Expected, Received>]: JsonShapeIssue<JsonObjectKeyPath<Path, K>, "missing_required_property", Expected[K], undefined>;
|
|
367
|
+
}[MutationMissingKeys<Expected, Received>] | {
|
|
368
|
+
readonly [K in JsonSharedKeys<Expected, Received>]: JsonShapeIssues<Expected[K], Received[K], JsonObjectKeyPath<Path, K>>;
|
|
369
|
+
}[JsonSharedKeys<Expected, Received>];
|
|
370
|
+
type JsonTupleIndices<Value extends readonly unknown[]> = Extract<keyof Value, `${number}`>;
|
|
371
|
+
type JsonTupleShapeIssues<Expected extends readonly unknown[], Received extends readonly unknown[], Path extends string> = {
|
|
372
|
+
readonly [K in Exclude<JsonTupleIndices<Expected>, JsonTupleIndices<Received>>]: JsonShapeIssue<JsonTupleIndexPath<Path, K>, "missing_required_property", Expected[K], undefined>;
|
|
373
|
+
}[Exclude<JsonTupleIndices<Expected>, JsonTupleIndices<Received>>] | {
|
|
374
|
+
readonly [K in Extract<JsonTupleIndices<Expected>, JsonTupleIndices<Received>>]: JsonShapeIssues<Expected[K], Received[K], JsonTupleIndexPath<Path, K>>;
|
|
375
|
+
}[Extract<JsonTupleIndices<Expected>, JsonTupleIndices<Received>>];
|
|
376
|
+
type JsonShapeIssues<Expected, Received, Path extends string = ""> = [Received] extends [Expected] ? never : [Expected] extends [readonly (infer ExpectedElement)[]] ? [Received] extends [readonly (infer ReceivedElement)[]] ? number extends Expected["length"] ? JsonShapeIssues<ExpectedElement, ReceivedElement, JsonArrayPath<Path>> : number extends Received["length"] ? JsonShapeIssues<ExpectedElement, ReceivedElement, JsonArrayPath<Path>> : JsonTupleShapeIssues<Expected, Received, Path> extends infer Issues ? [Issues] extends [never] ? JsonShapeIssue<Path, "type_mismatch", Expected, Received> : Issues : never : JsonShapeIssue<Path, "type_mismatch", Expected, Received> : [Expected] extends [object] ? [Expected] extends [Function] ? JsonShapeIssue<Path, "type_mismatch", Expected, Received> : [Received] extends [object] ? [Received] extends [Function] ? JsonShapeIssue<Path, "type_mismatch", Expected, Received> : JsonObjectShapeIssues<Expected & object, Received & object, Path> extends infer Issues ? [Issues] extends [never] ? JsonShapeIssue<Path, "type_mismatch", Expected, Received> : Issues : never : JsonShapeIssue<Path, "type_mismatch", Expected, Received> : JsonShapeIssue<Path, "type_mismatch", Expected, Received>;
|
|
377
|
+
type MutationAcceptedInput<Column> = Column extends Expression.Scalar<infer Runtime, any, any, any, any, any> ? MutationValueInput<Runtime> : never;
|
|
378
|
+
type MutationValueCompatibilityIssue<Column, Value, ColumnName extends string> = Column extends Expression.Scalar<any, infer ColumnDb extends Expression.DbType.Any, any, any, any, any> ? ColumnDb extends Expression.DbType.Json<infer ColumnDialect extends string, infer ExpectedKind> ? Value extends Expression.Scalar<any, infer ValueDb extends Expression.DbType.Any, any, any, any, any> ? ValueDb extends Expression.DbType.Json<ColumnDialect, infer ReceivedKind> ? [ExpectedKind] extends [ReceivedKind] ? [ReceivedKind] extends [ExpectedKind] ? Value extends Expression.Scalar<infer ReceivedRuntime, any, any, any, any, any> ? Column extends Expression.Scalar<infer ExpectedRuntime, any, any, any, any, any> ? [ReceivedRuntime] extends [ExpectedRuntime] ? never : JsonMutationShapeError<ColumnName, ExpectedRuntime, ReceivedRuntime> : never : never : JsonMutationDbKindError<ColumnName, ExpectedKind, ReceivedKind> : JsonMutationDbKindError<ColumnName, ExpectedKind, ReceivedKind> : Value extends Expression.Scalar<infer ReceivedRuntime, any, any, any, any, any> ? Column extends Expression.Scalar<infer ExpectedRuntime, any, any, any, any, any> ? [ReceivedRuntime] extends [ExpectedRuntime] ? never : JsonMutationShapeError<ColumnName, ExpectedRuntime, ReceivedRuntime> : never : Column extends Expression.Scalar<infer ExpectedRuntime, any, any, any, any, any> ? [Value] extends [ExpectedRuntime] ? never : JsonMutationShapeError<ColumnName, ExpectedRuntime, Value> : never : Column extends Expression.Scalar<infer ExpectedRuntime, any, any, any, any, any> ? [Value] extends [ExpectedRuntime] ? never : JsonMutationShapeError<ColumnName, ExpectedRuntime, Value> : never : Value extends Expression.Scalar<infer ReceivedRuntime, any, any, any, any, any> ? Column extends Expression.Scalar<infer ExpectedRuntime, any, any, any, any, any> ? [ReceivedRuntime] extends [ExpectedRuntime] ? never : MutationValueShapeError<ColumnName, ExpectedRuntime, ReceivedRuntime> : never : Column extends Expression.Scalar<infer ExpectedRuntime, any, any, any, any, any> ? [Value] extends [ExpectedRuntime] ? never : MutationValueShapeError<ColumnName, ExpectedRuntime, Value> : never : unknown;
|
|
379
|
+
type MutationExpectedValue<Column, Value, ColumnName extends string> = [MutationValueCompatibilityIssue<Column, Value, ColumnName>] extends [never] ? MutationAcceptedInput<Column> : MutationValueCompatibilityIssue<Column, Value, ColumnName>;
|
|
380
|
+
type MutationShapeOf<Statement extends "insert" | "update", Target extends MutationTargetLike> = Statement extends "insert" ? Table.InsertOf<Target> : Table.UpdateOf<Target>;
|
|
381
|
+
type MutationColumnAt<Target extends MutationTargetLike, Key extends string> = Target extends {
|
|
382
|
+
readonly [Table.TypeId]: {
|
|
383
|
+
readonly fields: Record<Key, infer Column>;
|
|
384
|
+
};
|
|
385
|
+
} ? Column : never;
|
|
386
|
+
type MutationValueShape<Shape, Target extends MutationTargetLike, Values extends Record<string, unknown>> = {
|
|
387
|
+
readonly [K in Extract<keyof Values, string>]: K extends Extract<keyof Shape, string> ? MutationExpectedValue<MutationColumnAt<Target, K>, Values[K], K> : MutationUnknownColumnError<K>;
|
|
388
|
+
};
|
|
389
|
+
type MutationKeyShape<Shape> = {
|
|
390
|
+
readonly [K in keyof Shape]: unknown;
|
|
391
|
+
};
|
|
392
|
+
export type MutationValuesInput<Statement extends "insert" | "update", Target extends MutationTargetLike, Values extends Record<string, unknown>, Shape = MutationShapeOf<Statement, Target>> = Simplify<Values & MutationKeyShape<Shape> & MutationValueShape<Shape, Target, Values>>;
|
|
393
|
+
/** Extracts a source name from either a table or a derived source. */
|
|
394
|
+
export type SourceNameOf<Source extends SourceLike> = Source extends TableLike<infer Name, any> ? Name : Source extends {
|
|
395
|
+
readonly kind: "derived";
|
|
396
|
+
readonly name: infer Alias extends string;
|
|
397
|
+
} ? Alias : Source extends {
|
|
398
|
+
readonly kind: "cte";
|
|
399
|
+
readonly name: infer Alias extends string;
|
|
400
|
+
} ? Alias : Source extends {
|
|
401
|
+
readonly kind: "lateral";
|
|
402
|
+
readonly name: infer Alias extends string;
|
|
403
|
+
} ? Alias : Source extends {
|
|
404
|
+
readonly kind: "values";
|
|
405
|
+
readonly name: infer Alias extends string;
|
|
406
|
+
} ? Alias : Source extends {
|
|
407
|
+
readonly kind: "unnest";
|
|
408
|
+
readonly name: infer Alias extends string;
|
|
409
|
+
} ? Alias : Source extends {
|
|
410
|
+
readonly kind: "tableFunction";
|
|
411
|
+
readonly name: infer Alias extends string;
|
|
412
|
+
} ? Alias : never;
|
|
413
|
+
type MutationTargetByName<Targets extends MutationTargetTuple, Name extends string> = Extract<Targets[number], {
|
|
414
|
+
readonly [Table.TypeId]: {
|
|
415
|
+
readonly name: Name;
|
|
416
|
+
};
|
|
417
|
+
}>;
|
|
418
|
+
export type MutationTargetNamesOf<Target extends MutationTargetInput> = Target extends MutationTargetLike ? SourceNameOf<Target> : Target extends MutationTargetTuple ? SourceNameOf<Target[number]> : never;
|
|
419
|
+
export type UpdateInputOfTarget<Target extends MutationTargetInput> = Target extends MutationTargetLike ? MutationInputOf<Table.UpdateOf<Target>> : Target extends MutationTargetTuple ? Simplify<{
|
|
420
|
+
readonly [K in MutationTargetNamesOf<Target>]?: MutationInputOf<Table.UpdateOf<MutationTargetByName<Target, K>>>;
|
|
421
|
+
}> : never;
|
|
422
|
+
/** Extracts the effective dialect from a source. */
|
|
423
|
+
export type SourceDialectOf<Source extends SourceLike> = Source extends TableLike<any, infer Dialect> ? Dialect : Source extends {
|
|
424
|
+
readonly kind: "derived";
|
|
425
|
+
readonly plan: infer PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
426
|
+
} ? PlanDialectOf<PlanValue> : Source extends {
|
|
427
|
+
readonly kind: "cte";
|
|
428
|
+
readonly plan: infer PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
429
|
+
} ? PlanDialectOf<PlanValue> : Source extends {
|
|
430
|
+
readonly kind: "lateral";
|
|
431
|
+
readonly plan: infer PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
432
|
+
} ? PlanDialectOf<PlanValue> : Source extends {
|
|
433
|
+
readonly dialect: infer Dialect extends string;
|
|
434
|
+
} ? Dialect : never;
|
|
435
|
+
/** Extracts the base table name from a source. */
|
|
436
|
+
export type SourceBaseNameOf<Source extends SourceLike> = Source extends TableLike<any, any> ? Source[typeof Table.TypeId]["baseName"] : Source extends {
|
|
437
|
+
readonly kind: "derived";
|
|
438
|
+
readonly baseName: infer BaseName extends string;
|
|
439
|
+
} ? BaseName : Source extends {
|
|
440
|
+
readonly kind: "cte";
|
|
441
|
+
readonly baseName: infer BaseName extends string;
|
|
442
|
+
} ? BaseName : Source extends {
|
|
443
|
+
readonly kind: "lateral";
|
|
444
|
+
readonly baseName: infer BaseName extends string;
|
|
445
|
+
} ? BaseName : Source extends {
|
|
446
|
+
readonly baseName: infer BaseName extends string;
|
|
447
|
+
} ? BaseName : never;
|
|
448
|
+
/** Extracts the outer-scope requirements carried by a source. */
|
|
449
|
+
export type SourceRequiredOf<Source extends SourceLike> = Source extends TableLike<any, any> ? never : Source extends {
|
|
450
|
+
readonly kind: "derived";
|
|
451
|
+
} ? never : Source extends {
|
|
452
|
+
readonly kind: "cte";
|
|
453
|
+
} ? never : Source extends {
|
|
454
|
+
readonly kind: "lateral";
|
|
455
|
+
readonly required: infer Required extends string;
|
|
456
|
+
} ? Required : never;
|
|
457
|
+
/** Helper type used when a correlated source is used before its outer dependencies are in scope. */
|
|
458
|
+
export type SourceRequirementError<Source extends SourceLike> = Source & {
|
|
459
|
+
readonly __effect_qb_error__: "effect-qb: correlated source requires outer-scope tables to be in scope first";
|
|
460
|
+
readonly __effect_qb_required_sources__: SourceRequiredOf<Source>;
|
|
461
|
+
readonly __effect_qb_hint__: "Join the outer tables first, then wrap the correlated plan in lateral(...)";
|
|
462
|
+
};
|
|
463
|
+
/** Helper type used when a raw plan is passed where `as(...)` is required. */
|
|
464
|
+
export type DerivedSourceRequiredError<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = PlanValue & {
|
|
465
|
+
readonly __effect_qb_error__: "effect-qb: subqueries must be aliased before they can be used as a source";
|
|
466
|
+
readonly __effect_qb_hint__: "Wrap the nested plan in as(subquery, alias) before passing it to from(...) or a join";
|
|
467
|
+
};
|
|
468
|
+
type JoinPath<Segments extends readonly string[]> = Segments extends readonly [] ? "" : Segments extends readonly [infer Head extends string] ? Head : Segments extends readonly [infer Head extends string, ...infer Tail extends readonly string[]] ? `${Head}__${JoinPath<Tail>}` : string;
|
|
469
|
+
type DerivedLeafExpression<Value extends Expression.Any, Alias extends string, ColumnName extends string> = Expression.Scalar<Expression.RuntimeOf<Value>, Expression.DbTypeOf<Value>, Expression.NullabilityOf<Value>, DialectOf<Value>, "scalar", Alias> & {
|
|
470
|
+
readonly [ExpressionAst.TypeId]: ExpressionAst.ColumnNode<Alias, ColumnName>;
|
|
471
|
+
};
|
|
472
|
+
/** Rebinds a nested selection tree to a derived-table alias. */
|
|
473
|
+
export type DerivedSelectionOf<Selection, Alias extends string, Path extends readonly string[] = []> = Selection extends Expression.Any ? DerivedLeafExpression<Selection, Alias, JoinPath<Path>> : Selection extends Record<string, any> ? {
|
|
474
|
+
readonly [K in keyof Selection]: DerivedSelectionOf<Selection[K], Alias, [...Path, Extract<K, string>]>;
|
|
475
|
+
} : never;
|
|
476
|
+
/** Extracts the static SQL table name from a table-like value. */
|
|
477
|
+
export type TableNameOf<T extends TableLike> = T[typeof Table.TypeId]["name"];
|
|
478
|
+
/** Extracts the effective dialect from a table-like value. */
|
|
479
|
+
export type TableDialectOf<T extends TableLike> = T[typeof RowSet.TypeId]["dialect"];
|
|
480
|
+
/** Names of sources already available to a plan. */
|
|
481
|
+
type AvailableNames<Available extends Record<string, RowSet.AnySource>> = Extract<keyof Available, string>;
|
|
482
|
+
/** Availability mode of a named source within the current plan scope. */
|
|
483
|
+
type SourceModeOf<Available extends Record<string, RowSet.AnySource>, Name extends string> = Name extends keyof Available ? Available[Name]["mode"] : never;
|
|
484
|
+
type TrueAssumptions = TrueFormula;
|
|
485
|
+
/** Extracts the public plan state carried under `RowSet.TypeId`. */
|
|
486
|
+
type QueryPlanParts<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = PlanValue[typeof RowSet.TypeId];
|
|
487
|
+
/** Extracts the internal phantom query state carried under `QueryTypeId`. */
|
|
488
|
+
type QueryPlanState<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = PlanValue[typeof QueryTypeId];
|
|
489
|
+
/** Extracts the selection carried by a query plan. */
|
|
490
|
+
export type SelectionOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanParts<PlanValue>["selection"];
|
|
491
|
+
/** Extracts the public required-source state carried by a query plan. */
|
|
492
|
+
export type RequiredOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanParts<PlanValue>["required"];
|
|
493
|
+
/** Extracts the available-source scope carried by a query plan. */
|
|
494
|
+
export type AvailableOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanParts<PlanValue>["available"];
|
|
495
|
+
/** Extracts the effective dialect carried by a query plan. */
|
|
496
|
+
export type PlanDialectOf<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanParts<PlanValue>["dialect"];
|
|
497
|
+
/** Extracts the grouped-source phantom carried by a query plan. */
|
|
498
|
+
export type GroupedOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanState<PlanValue>["grouped"];
|
|
499
|
+
/** Extracts the available-name phantom carried by a query plan. */
|
|
500
|
+
export type ScopedNamesOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanState<PlanValue>["availableNames"];
|
|
501
|
+
/** Extracts the outstanding-required-source phantom carried by a query plan. */
|
|
502
|
+
export type OutstandingOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanState<PlanValue>["required"];
|
|
503
|
+
export type AssumptionsOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanState<PlanValue>["assumptions"];
|
|
504
|
+
export type CapabilitiesOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanState<PlanValue>["capabilities"];
|
|
505
|
+
/** Extracts capabilities contributed by a source wrapper. */
|
|
506
|
+
export type SourceCapabilitiesOf<Source extends SourceLike> = Source extends TableLike<any, any> ? never : Source extends {
|
|
507
|
+
readonly kind: "derived";
|
|
508
|
+
readonly plan: infer PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
509
|
+
} ? CapabilitiesOfPlan<PlanValue> : Source extends {
|
|
510
|
+
readonly kind: "cte";
|
|
511
|
+
readonly plan: infer PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
512
|
+
} ? CapabilitiesOfPlan<PlanValue> : Source extends {
|
|
513
|
+
readonly kind: "lateral";
|
|
514
|
+
readonly plan: infer PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
515
|
+
} ? CapabilitiesOfPlan<PlanValue> : never;
|
|
516
|
+
/** Extracts the statement kind carried by a query plan. */
|
|
517
|
+
export type StatementOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanState<PlanValue>["statement"];
|
|
518
|
+
/** Extracts the mutation target phantom carried by a query plan. */
|
|
519
|
+
export type MutationTargetOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanState<PlanValue>["target"];
|
|
520
|
+
/** Extracts whether an insert plan still needs a source attached. */
|
|
521
|
+
export type InsertSourceStateOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanState<PlanValue>["insertSource"];
|
|
522
|
+
export type StateOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanParts<PlanValue>;
|
|
523
|
+
type PresenceWitnessKeysOfSelection<Selection> = Selection extends Expression.Any ? AstOf<Selection> extends ExpressionAst.ColumnNode<any, any> ? Expression.NullabilityOf<Selection> extends "never" ? ColumnKeyOfExpression<Selection> : never : never : Selection extends Record<string, any> ? {
|
|
524
|
+
readonly [K in keyof Selection]: PresenceWitnessKeysOfSelection<Selection[K]>;
|
|
525
|
+
}[keyof Selection] : never;
|
|
526
|
+
export type PresenceWitnessKeysOfSource<Source extends SourceLike> = Source extends TableLike<any, any> ? PresenceWitnessKeysOfSelection<Source[typeof RowSet.TypeId]["selection"]> : Source extends {
|
|
527
|
+
readonly columns: infer Columns;
|
|
528
|
+
} ? PresenceWitnessKeysOfSelection<Columns> : never;
|
|
529
|
+
/**
|
|
530
|
+
* Adds a single source entry to the set of available sources.
|
|
531
|
+
*
|
|
532
|
+
* This is used by `from(...)` and the join builders.
|
|
533
|
+
*/
|
|
534
|
+
export type AddAvailable<Available extends Record<string, RowSet.AnySource>, Name extends string, Mode extends RowSet.SourceMode = "required", PresentFormula extends PredicateFormula = TrueFormula, PresenceWitness extends string = never> = Available & Record<Name, RowSet.Source<Name, Mode, PresentFormula, PresenceWitness>>;
|
|
535
|
+
export type AddAvailableMany<Available extends Record<string, RowSet.AnySource>, Names extends string, Mode extends RowSet.SourceMode = "required", PresentFormula extends PredicateFormula = TrueFormula, PresenceWitness extends string = never> = Available & {
|
|
536
|
+
readonly [K in Names]: RowSet.Source<K, Mode, PresentFormula, PresenceWitness>;
|
|
537
|
+
};
|
|
538
|
+
/** Join mode projected into the plan's source-scope mode lattice. */
|
|
539
|
+
export type JoinSourceMode<Kind extends QueryAst.JoinKind> = Kind extends "left" | "full" ? "optional" : "required";
|
|
540
|
+
type DemoteAllAvailable<Available extends Record<string, RowSet.AnySource>> = {
|
|
541
|
+
readonly [K in keyof Available]: Available[K] extends RowSet.Source<infer Name extends string, any, infer PresentFormula extends PredicateFormula, infer PresenceWitness extends string> ? RowSet.Source<Name, "optional", PresentFormula, PresenceWitness> : never;
|
|
542
|
+
};
|
|
543
|
+
export type ExistingAvailableAfterJoin<Available extends Record<string, RowSet.AnySource>, Kind extends QueryAst.JoinKind> = Kind extends "right" | "full" ? DemoteAllAvailable<Available> : Available;
|
|
544
|
+
export type AvailableAfterJoin<Available extends Record<string, RowSet.AnySource>, JoinedName extends string, Kind extends QueryAst.JoinKind, PresentFormula extends PredicateFormula = TrueFormula, PresenceWitness extends string = never> = AddAvailable<ExistingAvailableAfterJoin<Available, Kind>, JoinedName, JoinSourceMode<Kind>, PresentFormula, PresenceWitness>;
|
|
545
|
+
/**
|
|
546
|
+
* Computes the next `required` set after introducing an additional expression.
|
|
547
|
+
*
|
|
548
|
+
* Any sources already present in `available` are considered satisfied.
|
|
549
|
+
*/
|
|
550
|
+
export type AddExpressionRequired<Required, Available extends Record<string, RowSet.AnySource>, Value extends ExpressionInput> = Exclude<Required | RequiredFromInput<Value>, AvailableNames<Available>>;
|
|
551
|
+
/**
|
|
552
|
+
* Computes the next `required` set after a join is applied.
|
|
553
|
+
*
|
|
554
|
+
* The joined table becomes available immediately, so references to it are
|
|
555
|
+
* removed from the outstanding requirement set.
|
|
556
|
+
*/
|
|
557
|
+
export type AddJoinRequired<Required, Available extends Record<string, RowSet.AnySource>, JoinedName extends string, Predicate extends PredicateInput | never, Kind extends QueryAst.JoinKind = "inner"> = Exclude<Required | (Predicate extends never ? never : RequiredFromInput<Predicate>), AvailableNames<AvailableAfterJoin<Available, JoinedName, Kind>>>;
|
|
558
|
+
/** Merges two aggregation kinds through a derived expression. */
|
|
559
|
+
export type MergeAggregation<Left extends Expression.ScalarKind, Right extends Expression.ScalarKind> = Left extends "window" ? "window" : Right extends "window" ? "window" : Left extends "aggregate" ? "aggregate" : Right extends "aggregate" ? "aggregate" : "scalar";
|
|
560
|
+
/** Merges two nullability states for null-propagating scalar operators. */
|
|
561
|
+
type MergeNullability<Left extends Expression.Nullability, Right extends Expression.Nullability> = Left extends "always" ? "always" : Right extends "always" ? "always" : Left extends "maybe" ? "maybe" : Right extends "maybe" ? "maybe" : "never";
|
|
562
|
+
/** Folds nullability across a tuple for null-propagating scalar operators. */
|
|
563
|
+
export type MergeNullabilityTuple<Values extends readonly Expression.Any[], Current extends Expression.Nullability = "never"> = Values extends readonly [infer Head extends Expression.Any, ...infer Tail extends readonly Expression.Any[]] ? MergeNullabilityTuple<Tail, MergeNullability<Current, Expression.NullabilityOf<Head>>> : Current;
|
|
564
|
+
/** Dialect union across a tuple of expressions. */
|
|
565
|
+
export type TupleDialect<Values extends readonly Expression.Any[]> = Values[number] extends never ? never : DialectOf<Values[number]>;
|
|
566
|
+
/** Dependency union across a tuple of scalar expressions. */
|
|
567
|
+
export type TupleDependencies<Values extends readonly Expression.Any[]> = Values[number] extends never ? never : DependenciesOf<Values[number]>;
|
|
568
|
+
/** Builds the canonical dependency union from a string union of table names. */
|
|
569
|
+
export type DependencyRecord<Keys extends string> = Keys;
|
|
570
|
+
/** Grouped expression identities carried by a tuple of scalar expressions. */
|
|
571
|
+
export type GroupedKeysFromValues<Values extends readonly Expression.Any[]> = Values[number] extends never ? never : {
|
|
572
|
+
[K in keyof Values]: Values[K] extends Expression.Any ? GroupingKeyOfExpression<Values[K]> : never;
|
|
573
|
+
}[number];
|
|
574
|
+
/** Whether a selection contains any aggregate expressions. */
|
|
575
|
+
type SelectionHasAggregate<Selection> = Selection extends Expression.Any ? KindOf<Selection> extends "aggregate" ? true : false : Selection extends Record<string, any> ? Extract<{
|
|
576
|
+
[K in keyof Selection]: SelectionHasAggregate<Selection[K]>;
|
|
577
|
+
}[keyof Selection], true> extends never ? false : true : false;
|
|
578
|
+
/** Whether a selection is valid for a specific grouped-column set. */
|
|
579
|
+
type IsGroupedSelectionValid<Selection, Grouped extends string> = Selection extends Expression.Any ? KindOf<Selection> extends "aggregate" ? true : KindOf<Selection> extends "window" ? false : RequiredFromDependencies<DependenciesOf<Selection>> extends never ? true : [GroupingKeyOfExpression<Selection>] extends [Grouped] ? true : false : Selection extends Record<string, any> ? Extract<{
|
|
580
|
+
[K in keyof Selection]: IsGroupedSelectionValid<Selection[K], Grouped> extends true ? never : true;
|
|
581
|
+
}[keyof Selection], true> extends never ? true : false : false;
|
|
582
|
+
/** Whether a selection is aggregation-safe for the current grouping state. */
|
|
583
|
+
type IsAggregationCompatibleSelection<Selection, Grouped extends string> = SelectionHasAggregate<Selection> extends true ? IsGroupedSelectionValid<Selection, Grouped> : [Grouped] extends [never] ? true : IsGroupedSelectionValid<Selection, Grouped>;
|
|
584
|
+
type FoldEffectiveNullability<Values extends readonly Expression.Any[], Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = Extract<{
|
|
585
|
+
[K in keyof Values]: Values[K] extends Expression.Any ? EffectiveNullability<Values[K], Available, Assumptions> : never;
|
|
586
|
+
}[number], "always"> extends never ? Extract<{
|
|
587
|
+
[K in keyof Values]: Values[K] extends Expression.Any ? EffectiveNullability<Values[K], Available, Assumptions> : never;
|
|
588
|
+
}[number], "maybe"> extends never ? "never" : "maybe" : "always";
|
|
589
|
+
type CoalesceEffectiveNullability<Values extends readonly Expression.Any[], Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = Extract<{
|
|
590
|
+
[K in keyof Values]: Values[K] extends Expression.Any ? EffectiveNullability<Values[K], Available, Assumptions> : never;
|
|
591
|
+
}[number], "never"> extends never ? Extract<{
|
|
592
|
+
[K in keyof Values]: Values[K] extends Expression.Any ? EffectiveNullability<Values[K], Available, Assumptions> : never;
|
|
593
|
+
}[number], "maybe"> extends never ? "always" : "maybe" : "never";
|
|
594
|
+
type NullabilityOfOutput<Output> = null extends Output ? Exclude<Output, null> extends never ? "always" : "maybe" : "never";
|
|
595
|
+
type PreciseFactSet<Value extends string> = string extends Value ? never : Value;
|
|
596
|
+
type KnownGuaranteedNullKeys<Assumptions extends PredicateFormula> = PreciseFactSet<GuaranteedNullKeys<Assumptions>>;
|
|
597
|
+
type KnownGuaranteedNonNullKeys<Assumptions extends PredicateFormula> = PreciseFactSet<GuaranteedNonNullKeys<Assumptions>>;
|
|
598
|
+
type KnownGuaranteedSourceNames<Assumptions extends PredicateFormula> = PreciseFactSet<GuaranteedSourceNames<Assumptions>>;
|
|
599
|
+
type ExplicitlyRequiredSourceNames<Available extends Record<string, RowSet.AnySource>> = Extract<{
|
|
600
|
+
readonly [K in keyof Available]: Available[K] extends RowSet.Source<any, infer Mode extends RowSet.SourceMode> ? Mode extends "required" ? K : never : never;
|
|
601
|
+
}[keyof Available], string>;
|
|
602
|
+
type PresentFormulaOfSource<Source extends RowSet.AnySource> = Source extends RowSet.Source<any, any, infer PresentFormula extends PredicateFormula, any> ? PresentFormula : TrueFormula;
|
|
603
|
+
type PresenceWitnessesOfSource<Source extends RowSet.AnySource> = Source extends RowSet.Source<any, any, any, infer PresenceWitness extends string> ? PresenceWitness : never;
|
|
604
|
+
type ImpliedSourceNamesFromRequired<Available extends Record<string, RowSet.AnySource>, Required extends string> = Extract<{
|
|
605
|
+
readonly [K in Extract<keyof Available, string>]: K extends Required ? PreciseFactSet<GuaranteedSourceNames<PresentFormulaOfSource<Available[K]>>> : never;
|
|
606
|
+
}[Extract<keyof Available, string>], string>;
|
|
607
|
+
type ExpandRequiredSourceNames<Available extends Record<string, RowSet.AnySource>, Required extends string> = ExpandRequiredSourceNamesStep<Available, Required, Required | ImpliedSourceNamesFromRequired<Available, Required>>;
|
|
608
|
+
type ExpandRequiredSourceNamesStep<Available extends Record<string, RowSet.AnySource>, Current extends string, Next extends string> = [Exclude<Next, Current>] extends [never] ? Current : ExpandRequiredSourceNames<Available, Next>;
|
|
609
|
+
type DirectlyAbsentSourceNames<Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = Extract<{
|
|
610
|
+
readonly [K in Extract<keyof Available, string>]: K extends string ? PresenceWitnessesOfSource<Available[K]> extends infer Witnesses extends string ? Extract<Witnesses, KnownGuaranteedNullKeys<Assumptions>> extends never ? ContradictsFormula<Assumptions, PresentFormulaOfSource<Available[K]>> extends true ? K : never : K : never : never;
|
|
611
|
+
}[Extract<keyof Available, string>], string>;
|
|
612
|
+
type ImpliedAbsentSourceNames<Available extends Record<string, RowSet.AnySource>, Absent extends string> = Extract<{
|
|
613
|
+
readonly [K in Extract<keyof Available, string>]: Extract<PreciseFactSet<GuaranteedSourceNames<PresentFormulaOfSource<Available[K]>>>, Absent> extends never ? never : K;
|
|
614
|
+
}[Extract<keyof Available, string>], string>;
|
|
615
|
+
type ExpandAbsentSourceNames<Available extends Record<string, RowSet.AnySource>, Current extends string> = ExpandAbsentSourceNamesStep<Available, Current, Current | ImpliedAbsentSourceNames<Available, Current>>;
|
|
616
|
+
type ExpandAbsentSourceNamesStep<Available extends Record<string, RowSet.AnySource>, Current extends string, Next extends string> = [Exclude<Next, Current>] extends [never] ? Current : ExpandAbsentSourceNames<Available, Next>;
|
|
617
|
+
type AbsentSourceNamesInScope<Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = ExpandAbsentSourceNames<Available, DirectlyAbsentSourceNames<Available, Assumptions>>;
|
|
618
|
+
type RequiredSourceNamesInScope<Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = Exclude<ExpandRequiredSourceNames<Available, ExplicitlyRequiredSourceNames<Available> | KnownGuaranteedSourceNames<Assumptions>>, AbsentSourceNamesInScope<Available, Assumptions>>;
|
|
619
|
+
type EffectiveAvailable<Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = {
|
|
620
|
+
readonly [K in keyof Available]: Available[K] extends RowSet.Source<infer Name extends string, infer Mode extends RowSet.SourceMode, infer PresentFormula extends PredicateFormula, infer PresenceWitness extends string> ? RowSet.Source<Name, K extends RequiredSourceNamesInScope<Available, Assumptions> ? "required" : Mode, PresentFormula, PresenceWitness> & {
|
|
621
|
+
readonly baseName?: Available[K]["baseName"];
|
|
622
|
+
} : never;
|
|
623
|
+
};
|
|
624
|
+
type HasAbsentSource<Dependencies extends Expression.BindingId, Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = Extract<Dependencies, AbsentSourceNamesInScope<Available, Assumptions>> extends never ? false : true;
|
|
625
|
+
type OptionalSourceNamesInScope<Available extends Record<string, RowSet.AnySource>> = Extract<{
|
|
626
|
+
[K in keyof Available & string]: SourceModeOf<Available, K> extends "optional" ? K : never;
|
|
627
|
+
}[keyof Available & string], string>;
|
|
628
|
+
type BaseEffectiveNullability<Value extends Expression.Any, Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = AstOf<Value> extends ExpressionAst.ColumnNode<infer TableName extends string, infer ColumnName extends string> ? TableName extends AbsentSourceNamesInScope<Available, Assumptions> ? "always" : `${TableName}.${ColumnName}` extends KnownGuaranteedNullKeys<Assumptions> ? "always" : `${TableName}.${ColumnName}` extends KnownGuaranteedNonNullKeys<Assumptions> ? "never" : Expression.NullabilityOf<Value> extends "always" ? "always" : HasAbsentSource<DependenciesOf<Value>, Available, Assumptions> extends true ? "always" : HasOptionalSource<DependenciesOf<Value>, Available> extends true ? "maybe" : Expression.NullabilityOf<Value> : Expression.NullabilityOf<Value> extends "always" ? "always" : HasAbsentSource<DependenciesOf<Value>, Available, Assumptions> extends true ? "always" : HasOptionalSource<DependenciesOf<Value>, Available> extends true ? "maybe" : Expression.NullabilityOf<Value>;
|
|
629
|
+
type CaseOutputOf<Branches extends readonly ExpressionAst.CaseBranchNode[], Else extends Expression.Any, Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = Branches extends readonly [
|
|
630
|
+
infer Head extends ExpressionAst.CaseBranchNode,
|
|
631
|
+
...infer Tail extends readonly ExpressionAst.CaseBranchNode[]
|
|
632
|
+
] ? Head extends ExpressionAst.CaseBranchNode<infer Predicate extends Expression.Any, infer Then extends Expression.Any> ? CaseBranchDecision<Assumptions, Predicate> extends "skip" ? CaseOutputOf<Tail, Else, Available, Assumptions> : CaseBranchDecision<Assumptions, Predicate> extends "take" ? ExpressionOutput<Then, EffectiveAvailable<Available, CaseBranchAssumeTrue<Assumptions, Predicate>>, CaseBranchAssumeTrue<Assumptions, Predicate>> : ExpressionOutput<Then, EffectiveAvailable<Available, CaseBranchAssumeTrue<Assumptions, Predicate>>, CaseBranchAssumeTrue<Assumptions, Predicate>> | CaseOutputOf<Tail, Else, Available, CaseBranchAssumeFalse<Assumptions, Predicate>> : never : ExpressionOutput<Else, EffectiveAvailable<Available, Assumptions>, Assumptions>;
|
|
633
|
+
/** Effective nullability of an expression after source-scope nullability is applied. */
|
|
634
|
+
type EffectiveNullabilityOfAst<Value extends Expression.Any, Ast extends ExpressionAst.Any, Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula = TrueAssumptions> = Ast extends ExpressionAst.ColumnNode<any, any> ? BaseEffectiveNullability<Value, Available, Assumptions> : Ast extends ExpressionAst.LiteralNode<any> ? Expression.NullabilityOf<Value> : Ast extends ExpressionAst.UnaryNode<infer Kind, infer UnaryValue extends Expression.Any> ? Kind extends "upper" | "lower" | "not" ? EffectiveNullability<UnaryValue, Available, Assumptions> : Kind extends "isNull" | "isNotNull" | "count" ? "never" : Expression.NullabilityOf<Value> : Ast extends ExpressionAst.BinaryNode<"eq", infer Left extends Expression.Any, infer Right extends Expression.Any> ? EffectiveNullability<Left, Available, Assumptions> extends "never" ? EffectiveNullability<Right, Available, Assumptions> extends "never" ? "never" : "maybe" : "maybe" : Ast extends ExpressionAst.VariadicNode<infer Kind, infer Values extends readonly Expression.Any[]> ? Kind extends "coalesce" ? CoalesceEffectiveNullability<Values, Available, Assumptions> : Kind extends "and" | "or" | "concat" ? FoldEffectiveNullability<Values, Available, Assumptions> : BaseEffectiveNullability<Value, Available, Assumptions> : Ast extends ExpressionAst.CaseNode<infer Branches extends readonly ExpressionAst.CaseBranchNode[], infer Else extends Expression.Any> ? NullabilityOfOutput<CaseOutputOf<Branches, Else, Available, Assumptions>> : BaseEffectiveNullability<Value, Available, Assumptions>;
|
|
635
|
+
export type EffectiveNullability<Value extends Expression.Any, Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula = TrueAssumptions> = AstOf<Value> extends infer Ast extends ExpressionAst.Any ? EffectiveNullabilityOfAst<Value, Ast, Available, Assumptions> : BaseEffectiveNullability<Value, Available, Assumptions>;
|
|
636
|
+
/** Result runtime type of an expression after effective nullability is resolved. */
|
|
637
|
+
export type ExpressionOutput<Value extends Expression.Any, Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula = TrueAssumptions> = AstOf<Value> extends infer Ast extends ExpressionAst.Any ? Ast extends ExpressionAst.CaseNode<infer Branches extends readonly ExpressionAst.CaseBranchNode[], infer Else extends Expression.Any> ? CaseOutputOf<Branches, Else, Available, Assumptions> : EffectiveNullabilityOfAst<Value, Ast, Available, Assumptions> extends infer Nullability ? Expression.NullabilityOf<Value> extends infer BaseNullability ? Expression.RuntimeOf<Value> extends infer Runtime ? Nullability extends "never" ? BaseNullability extends "never" ? Runtime : NonNullable<Runtime> : Nullability extends "always" ? null : Runtime | null : never : never : never : never;
|
|
638
|
+
/** Result runtime type of a nested selection after source-scope nullability is resolved. */
|
|
639
|
+
export type OutputOfSelection<Selection, Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula = TrueAssumptions> = Selection extends Expression.Any ? ExpressionOutput<Selection, Available, Assumptions> : Selection extends Record<string, any> ? {
|
|
640
|
+
readonly [K in keyof Selection]: OutputOfSelection<Selection[K], Available, Assumptions>;
|
|
641
|
+
} : never;
|
|
642
|
+
type ResolvedSelectionOutput<Selection, Available extends Record<string, RowSet.AnySource>, Assumptions extends PredicateFormula> = OutputOfSelection<Selection, EffectiveAvailable<Available, Assumptions>, Assumptions>;
|
|
643
|
+
/** Resolved row type produced by a concrete query plan. */
|
|
644
|
+
export type ResultRow<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = SelectionOfPlan<PlanValue> extends infer Selection ? AvailableOfPlan<PlanValue> extends infer Available extends Record<string, RowSet.AnySource> ? AssumptionsOfPlan<PlanValue> extends infer Assumptions extends PredicateFormula ? ResolvedSelectionOutput<Selection, Available, Assumptions> : never : never : never;
|
|
645
|
+
/** Resolved row collection type produced by a concrete query plan. */
|
|
646
|
+
export type ResultRows<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = ReadonlyArray<ResultRow<PlanValue>>;
|
|
647
|
+
/** Conservative runtime row shape produced by remapping projection aliases. */
|
|
648
|
+
export type RuntimeResultRow<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = SelectionOfPlan<PlanValue> extends infer Selection ? AvailableOfPlan<PlanValue> extends infer Available extends Record<string, RowSet.AnySource> ? OutputOfSelection<Selection, Available, TrueAssumptions> : never : never;
|
|
649
|
+
/** Conservative runtime row collection type. */
|
|
650
|
+
export type RuntimeResultRows<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = ReadonlyArray<RuntimeResultRow<PlanValue>>;
|
|
651
|
+
/** Narrows a query plan to aggregate-compatible selections. */
|
|
652
|
+
type HasKnownOutstanding<Required> = [Required] extends [never] ? false : string extends Required ? false : true;
|
|
653
|
+
type SourceCompletenessError<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, MissingSources extends string> = PlanValue & {
|
|
654
|
+
readonly __effect_qb_error__: "effect-qb: query references sources that are not yet in scope";
|
|
655
|
+
readonly __effect_qb_missing_sources__: MissingSources;
|
|
656
|
+
readonly __effect_qb_hint__: "Add from(...) or a join for each referenced source before render or execute";
|
|
657
|
+
};
|
|
658
|
+
type AggregationCompatibilityError<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = PlanValue & {
|
|
659
|
+
readonly __effect_qb_error__: "effect-qb: invalid grouped selection";
|
|
660
|
+
readonly __effect_qb_hint__: "Scalar selections must be covered by groupBy(...) when aggregates are present";
|
|
661
|
+
};
|
|
662
|
+
type DialectCompatibilityError<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = PlanValue & {
|
|
663
|
+
readonly __effect_qb_error__: "effect-qb: plan dialect is not compatible with the target renderer or executor";
|
|
664
|
+
readonly __effect_qb_plan_dialect__: PlanValue[typeof RowSet.TypeId]["dialect"];
|
|
665
|
+
readonly __effect_qb_target_dialect__: EngineDialect;
|
|
666
|
+
readonly __effect_qb_hint__: "Use the matching dialect module or renderer/executor";
|
|
667
|
+
};
|
|
668
|
+
type InsertSourceCompletenessError<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = PlanValue & {
|
|
669
|
+
readonly __effect_qb_error__: "effect-qb: insert plan is missing inline values or a source";
|
|
670
|
+
readonly __effect_qb_hint__: "Pass values directly to insert(...), or pipe the insert plan into from(...)";
|
|
671
|
+
};
|
|
672
|
+
type RequiredKeys<Shape> = Extract<{
|
|
673
|
+
[K in keyof Shape]-?: {} extends Pick<Shape, K> ? never : K;
|
|
674
|
+
}[keyof Shape], string>;
|
|
675
|
+
type InsertHasOptionalDefaults<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = MutationTargetOfPlan<PlanValue> extends infer Target extends MutationTargetLike ? RequiredKeys<Table.InsertOf<Target>> extends never ? true : false : false;
|
|
676
|
+
/** Narrows a query plan to aggregate-compatible selections. */
|
|
677
|
+
export type AggregationCompatiblePlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = IsAggregationCompatibleSelection<SelectionOfPlan<PlanValue>, GroupedOfPlan<PlanValue>> extends true ? PlanValue : AggregationCompatibilityError<PlanValue>;
|
|
678
|
+
/** Narrows a query plan to aggregate-compatible, source-complete plans. */
|
|
679
|
+
export type CompletePlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = StatementOfPlan<PlanValue> extends "insert" ? InsertSourceStateOfPlan<PlanValue> extends "missing" ? InsertHasOptionalDefaults<PlanValue> extends true ? PlanValue : InsertSourceCompletenessError<PlanValue> : HasKnownOutstanding<RequiredOfPlan<PlanValue>> extends true ? SourceCompletenessError<PlanValue, Extract<RequiredOfPlan<PlanValue>, string>> : IsAggregationCompatibleSelection<SelectionOfPlan<PlanValue>, GroupedOfPlan<PlanValue>> extends true ? PlanValue : AggregationCompatibilityError<PlanValue> : HasKnownOutstanding<RequiredOfPlan<PlanValue>> extends true ? SourceCompletenessError<PlanValue, Extract<RequiredOfPlan<PlanValue>, string>> : IsAggregationCompatibleSelection<SelectionOfPlan<PlanValue>, GroupedOfPlan<PlanValue>> extends true ? PlanValue : AggregationCompatibilityError<PlanValue>;
|
|
680
|
+
/** Whether a plan dialect is compatible with a target engine dialect. */
|
|
681
|
+
type IsDialectCompatible<PlanDialect extends string, EngineDialect extends string> = [PlanDialect] extends [never] ? true : Extract<PlanDialect, EngineDialect> extends never ? false : true;
|
|
682
|
+
/** Narrows a complete plan to those compatible with a target engine dialect. */
|
|
683
|
+
export type DialectCompatiblePlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = IsDialectCompatible<PlanValue[typeof RowSet.TypeId]["dialect"], EngineDialect> extends true ? CompletePlan<PlanValue> : DialectCompatibilityError<PlanValue, EngineDialect>;
|
|
684
|
+
/** Nested-plan compatibility used by subquery expressions such as `exists(...)`. */
|
|
685
|
+
export type DialectCompatibleNestedPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = IsDialectCompatible<PlanValue[typeof RowSet.TypeId]["dialect"], EngineDialect> extends true ? AggregationCompatiblePlan<PlanValue> : DialectCompatibilityError<PlanValue, EngineDialect>;
|
|
686
|
+
type SetOperandStatement = "select" | "set";
|
|
687
|
+
type IsUnion<Value, All = Value> = Value extends any ? ([All] extends [Value] ? false : true) : never;
|
|
688
|
+
type SingleSelectedExpressionError<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = PlanValue & {
|
|
689
|
+
readonly __effect_qb_error__: "effect-qb: scalar and quantified subqueries must project exactly one top-level expression";
|
|
690
|
+
readonly __effect_qb_hint__: "Project exactly one scalar expression like select({ value: expr }) before using this subquery as a scalar operand";
|
|
691
|
+
};
|
|
692
|
+
type SingleSelectedExpression<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = SelectionOfPlan<PlanValue> extends Record<string, infer Value> ? IsUnion<Extract<keyof SelectionOfPlan<PlanValue>, string>> extends true ? SingleSelectedExpressionError<PlanValue> : Value extends Expression.Any ? Value : SingleSelectedExpressionError<PlanValue> : SingleSelectedExpressionError<PlanValue>;
|
|
693
|
+
export type ScalarSubqueryPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = DialectCompatibleNestedPlan<PlanValue, EngineDialect> extends infer Compatible ? Compatible extends QueryPlan<any, any, any, any, any, any, any, any, any, any> ? SingleSelectedExpression<Compatible> extends Expression.Any ? Compatible : SingleSelectedExpression<Compatible> : Compatible : never;
|
|
694
|
+
export type ScalarOutputOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = SingleSelectedExpression<PlanValue> extends infer Value ? Value extends Expression.Any ? Value : never : never;
|
|
695
|
+
type SetOperandStatementError<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = PlanValue & {
|
|
696
|
+
readonly __effect_qb_error__: "effect-qb: set operators only accept select-like query plans";
|
|
697
|
+
readonly __effect_qb_statement__: StatementOfPlan<PlanValue>;
|
|
698
|
+
readonly __effect_qb_hint__: "Use select(...) or another set operator as each operand";
|
|
699
|
+
};
|
|
700
|
+
type SetOperandShapeError<Left extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, Right extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = Right & {
|
|
701
|
+
readonly __effect_qb_error__: "effect-qb: set operator operands must have matching result rows";
|
|
702
|
+
readonly __effect_qb_expected_selection__: SelectionOfPlan<Left>;
|
|
703
|
+
readonly __effect_qb_actual_selection__: SelectionOfPlan<Right>;
|
|
704
|
+
readonly __effect_qb_hint__: "Project the same nested object shape and compatible nullability from each operand";
|
|
705
|
+
};
|
|
706
|
+
type IsSameSelection<Left extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, Right extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = [SelectionOfPlan<Left>] extends [SelectionOfPlan<Right>] ? [SelectionOfPlan<Right>] extends [SelectionOfPlan<Left>] ? true : false : false;
|
|
707
|
+
/** Set-operator compatibility used by `union(...)`, `intersect(...)`, and `except(...)`. */
|
|
708
|
+
export type SetCompatiblePlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = StatementOfPlan<PlanValue> extends SetOperandStatement ? DialectCompatiblePlan<PlanValue, EngineDialect> : SetOperandStatementError<PlanValue>;
|
|
709
|
+
/** Right-hand operand compatibility for set operators. */
|
|
710
|
+
export type SetCompatibleRightPlan<Left extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, Right extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = StatementOfPlan<Right> extends SetOperandStatement ? IsSameSelection<Left, Right> extends true ? DialectCompatiblePlan<Right, EngineDialect> : SetOperandShapeError<Left, Right> : SetOperandStatementError<Right>;
|
|
711
|
+
/** True when any of an expression's dependencies are optional in the current scope. */
|
|
712
|
+
type HasOptionalSource<Dependencies extends Expression.BindingId, Available extends Record<string, RowSet.AnySource>> = Extract<Dependencies, OptionalSourceNamesInScope<Available>> extends never ? false : true;
|
|
713
|
+
/**
|
|
714
|
+
* Concrete query-plan value produced by the query DSL.
|
|
715
|
+
*
|
|
716
|
+
* `Selection` is the output shape being built, `Required` tracks referenced
|
|
717
|
+
* sources that are not yet in scope, `Available` tracks sources already in
|
|
718
|
+
* scope, and `Dialect` tracks the effective SQL dialect for the plan.
|
|
719
|
+
*/
|
|
720
|
+
export type QueryPlan<Selection, Required = never, Available extends Record<string, RowSet.AnySource> = {}, Dialect extends string = never, Grouped extends string = never, ScopedNames extends string = never, Outstanding extends string = never, Assumptions extends PredicateFormula = TrueAssumptions, Capabilities extends QueryCapability = "read", Statement extends QueryAst.QueryStatement = "select", Target = any, InsertState extends InsertSourceState = InsertSourceState> = RowSet.RowSet<Selection, Required, Available, Dialect> & {
|
|
721
|
+
readonly [QueryTypeId]: QueryState<Outstanding, ScopedNames, Grouped, Assumptions, Capabilities, Statement, Target, InsertState>;
|
|
722
|
+
readonly [QueryAst.TypeId]: QueryAst.Ast<Selection, Grouped, Statement>;
|
|
723
|
+
};
|
|
724
|
+
export declare namespace Plan {
|
|
725
|
+
type Any = QueryPlan<any, any, any, any, any, any, any, any, any, any>;
|
|
726
|
+
}
|
|
727
|
+
/** Merges two expression dependency records into a single normalized record. */
|
|
728
|
+
export declare const mergeDependencies: <Left extends string = never, Right extends string = never>(left: Record<Left, true> | undefined, right?: Record<Right, true> | undefined) => Record<Left | Right, true>;
|
|
729
|
+
/** Merges expression aggregation kinds at runtime. */
|
|
730
|
+
export declare const mergeAggregationRuntime: (left: Expression.ScalarKind, right?: Expression.ScalarKind) => Expression.ScalarKind;
|
|
731
|
+
/** Folds runtime aggregation across a list of expressions. */
|
|
732
|
+
export declare const mergeAggregationManyRuntime: (values: readonly Expression.Any[]) => Expression.ScalarKind;
|
|
733
|
+
/** Folds runtime nullability across a list of expressions. */
|
|
734
|
+
export declare const mergeNullabilityManyRuntime: (values: readonly Expression.Any[]) => Expression.Nullability;
|
|
735
|
+
/** Merges dependency maps across a variadic expression input list. */
|
|
736
|
+
export declare const mergeManyDependencies: <Values extends readonly Expression.Any[]>(values: Values) => Record<TupleDependencies<Values>, true>;
|
|
737
|
+
/**
|
|
738
|
+
* Creates a runtime expression object from fully computed static metadata.
|
|
739
|
+
*
|
|
740
|
+
* The query helpers use this instead of exposing ad hoc object shapes, so every
|
|
741
|
+
* produced expression has the same structural contract as bound columns.
|
|
742
|
+
*/
|
|
743
|
+
export declare const makeExpression: <Runtime, Db extends Expression.DbType.Any, Nullable extends Expression.Nullability, Dialect extends string, Kind extends Expression.ScalarKind, Deps extends string = never, Ast extends ExpressionAst.Any = ExpressionAst.Any, GroupKey extends string = GroupingKeyOfAst<Ast>>(state: {
|
|
744
|
+
readonly runtime: Runtime;
|
|
745
|
+
readonly dbType: Db;
|
|
746
|
+
readonly runtimeSchema?: Schema.Schema.Any | undefined;
|
|
747
|
+
readonly nullability: Nullable;
|
|
748
|
+
readonly dialect: Dialect;
|
|
749
|
+
readonly kind?: Kind | undefined;
|
|
750
|
+
readonly dependencies?: Record<string, true> | undefined;
|
|
751
|
+
}, ast: Ast) => Expression.Scalar<Runtime, Db, Nullable, Dialect, Kind, Deps, GroupKey> & {
|
|
752
|
+
readonly [ExpressionAst.TypeId]: Ast;
|
|
753
|
+
};
|
|
754
|
+
/**
|
|
755
|
+
* Creates a runtime query-plan value from public plan metadata plus the
|
|
756
|
+
* internal clause AST.
|
|
757
|
+
*/
|
|
758
|
+
export declare const makePlan: <Selection, Required, Available extends Record<string, RowSet.AnySource>, Dialect extends string, Grouped extends string = never, ScopedNames extends string = Extract<keyof Available, string>, Outstanding extends string = Extract<Required, string>, Assumptions extends PredicateFormula = TrueFormula, Capabilities extends QueryCapability = "read", Statement extends QueryAst.QueryStatement = "select", Target = any, InsertState extends InsertSourceState = InsertSourceState>(state: RowSet.State<Selection, Required, Available, Dialect>, ast: QueryAst.Ast<Selection, Grouped, Statement>, _assumptions?: Assumptions | undefined, _capabilities?: Capabilities | undefined, _statement?: Statement | undefined, _target?: Target | undefined, _insertState?: InsertState | undefined) => QueryPlan<Selection, Required, Available, Dialect, Grouped, ScopedNames, Outstanding, Assumptions, Capabilities, Statement, Target, InsertState>;
|
|
759
|
+
/** Returns the internal AST carried by a query plan. */
|
|
760
|
+
export declare const getAst: <Selection, Grouped extends string, Statement extends QueryAst.QueryStatement>(plan: QueryPlan<Selection, any, any, any, Grouped, any, any, any, any, Statement, any, InsertSourceState>) => QueryAst.Ast<Selection, Grouped, Statement>;
|
|
761
|
+
/** Returns the internal phantom query state carried by a query plan. */
|
|
762
|
+
export declare const getQueryState: (plan: QueryPlan<any, any, any, any, any, any, any, any, any, any, any, InsertSourceState>) => QueryState<any, any, any, any, any, any, any, any>;
|
|
763
|
+
/**
|
|
764
|
+
* Collects the required table names referenced by a runtime selection object.
|
|
765
|
+
*
|
|
766
|
+
* This mirrors the `ExtractRequired<...>` type-level computation so runtime plan
|
|
767
|
+
* metadata stays aligned with the static model.
|
|
768
|
+
*/
|
|
769
|
+
export declare const extractRequiredRuntime: (selection: SelectionShape) => readonly string[];
|
|
770
|
+
/** Extracts the single top-level expression from a scalar subquery selection. */
|
|
771
|
+
export declare const extractSingleSelectedExpressionRuntime: (selection: SelectionShape) => Expression.Any;
|
|
772
|
+
/** Converts the plan's runtime `required` metadata into a mutable string list. */
|
|
773
|
+
export declare const currentRequiredList: (required: unknown) => string[];
|
|
774
|
+
/** Sort direction accepted by `orderBy(...)`. */
|
|
775
|
+
export type OrderDirection = QueryAst.OrderDirection;
|