effect-qb 0.14.0 → 0.16.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.
Files changed (146) hide show
  1. package/dist/mysql.js +61555 -4252
  2. package/dist/postgres/metadata.js +728 -104
  3. package/dist/postgres.js +6906 -4023
  4. package/package.json +15 -2
  5. package/src/internal/aggregation-validation.ts +3 -3
  6. package/src/internal/case-analysis.d.ts +18 -0
  7. package/src/internal/case-analysis.ts +4 -4
  8. package/src/internal/coercion/analysis.d.ts +7 -0
  9. package/src/internal/{coercion-analysis.ts → coercion/analysis.ts} +3 -3
  10. package/src/internal/coercion/errors.d.ts +17 -0
  11. package/src/internal/{coercion-errors.ts → coercion/errors.ts} +1 -1
  12. package/src/internal/coercion/kind.d.ts +4 -0
  13. package/src/internal/{coercion-kind.ts → coercion/kind.ts} +2 -2
  14. package/src/internal/{coercion-normalize.ts → coercion/normalize.ts} +1 -1
  15. package/src/internal/coercion/rules.d.ts +6 -0
  16. package/src/internal/{coercion-rules.ts → coercion/rules.ts} +2 -2
  17. package/src/internal/column-state.d.ts +190 -0
  18. package/src/internal/column-state.ts +43 -47
  19. package/src/internal/column.ts +43 -305
  20. package/src/internal/datatypes/define.d.ts +17 -0
  21. package/src/internal/datatypes/define.ts +18 -4
  22. package/src/internal/datatypes/lookup.d.ts +44 -0
  23. package/src/internal/datatypes/lookup.ts +61 -152
  24. package/src/internal/datatypes/shape.d.ts +16 -0
  25. package/src/internal/datatypes/shape.ts +1 -1
  26. package/src/internal/derived-table.d.ts +4 -0
  27. package/src/internal/derived-table.ts +21 -16
  28. package/src/internal/dialect.ts +12 -1
  29. package/src/internal/dsl-mutation-runtime.ts +378 -0
  30. package/src/internal/dsl-plan-runtime.ts +387 -0
  31. package/src/internal/dsl-query-runtime.ts +160 -0
  32. package/src/internal/dsl-transaction-ddl-runtime.ts +263 -0
  33. package/src/internal/executor.ts +146 -34
  34. package/src/internal/expression-ast.ts +15 -5
  35. package/src/internal/grouping-key.d.ts +3 -0
  36. package/src/internal/grouping-key.ts +1 -1
  37. package/src/internal/implication-runtime.d.ts +15 -0
  38. package/src/internal/implication-runtime.ts +4 -4
  39. package/src/internal/json/ast.d.ts +30 -0
  40. package/src/internal/json/ast.ts +1 -1
  41. package/src/internal/json/errors.d.ts +8 -0
  42. package/src/internal/json/path.d.ts +75 -0
  43. package/src/internal/json/path.ts +1 -1
  44. package/src/internal/json/types.d.ts +62 -0
  45. package/src/internal/predicate/analysis.d.ts +20 -0
  46. package/src/internal/predicate/analysis.ts +183 -0
  47. package/src/internal/predicate/atom.d.ts +28 -0
  48. package/src/internal/{predicate-atom.ts → predicate/atom.ts} +7 -0
  49. package/src/internal/{predicate-branches.ts → predicate/branches.ts} +2 -2
  50. package/src/internal/predicate/context.d.ts +67 -0
  51. package/src/internal/{predicate-context.ts → predicate/context.ts} +163 -20
  52. package/src/internal/predicate/formula.d.ts +35 -0
  53. package/src/internal/{predicate-formula.ts → predicate/formula.ts} +1 -1
  54. package/src/internal/predicate/key.d.ts +11 -0
  55. package/src/internal/predicate/key.ts +73 -0
  56. package/src/internal/{predicate-nnf.ts → predicate/nnf.ts} +2 -2
  57. package/src/internal/predicate/normalize.d.ts +53 -0
  58. package/src/internal/{predicate-normalize.ts → predicate/normalize.ts} +130 -49
  59. package/src/internal/predicate/runtime.d.ts +31 -0
  60. package/src/internal/{predicate-runtime.ts → predicate/runtime.ts} +127 -17
  61. package/src/internal/projection-alias.d.ts +13 -0
  62. package/src/internal/projections.d.ts +31 -0
  63. package/src/internal/projections.ts +1 -1
  64. package/src/internal/query-ast.d.ts +217 -0
  65. package/src/internal/query-ast.ts +1 -1
  66. package/src/internal/query-requirements.d.ts +20 -0
  67. package/src/internal/query.d.ts +775 -0
  68. package/src/internal/query.ts +683 -369
  69. package/src/internal/renderer.ts +11 -21
  70. package/src/internal/row-set.d.ts +53 -0
  71. package/src/internal/{plan.ts → row-set.ts} +11 -9
  72. package/src/internal/runtime/driver-value-mapping.ts +186 -0
  73. package/src/internal/{runtime-normalize.ts → runtime/normalize.ts} +9 -31
  74. package/src/internal/{runtime-schema.ts → runtime/schema.ts} +13 -38
  75. package/src/internal/runtime/value.d.ts +22 -0
  76. package/src/internal/{runtime-value.ts → runtime/value.ts} +2 -2
  77. package/src/internal/scalar.d.ts +107 -0
  78. package/src/internal/scalar.ts +202 -0
  79. package/src/internal/schema-derivation.d.ts +105 -0
  80. package/src/internal/schema-expression.d.ts +18 -0
  81. package/src/internal/schema-expression.ts +38 -7
  82. package/src/internal/table-options.d.ts +94 -0
  83. package/src/internal/table-options.ts +8 -2
  84. package/src/internal/table.d.ts +173 -0
  85. package/src/internal/table.ts +32 -14
  86. package/src/mysql/column.ts +95 -18
  87. package/src/mysql/datatypes/index.ts +47 -7
  88. package/src/mysql/errors/generated.ts +57336 -0
  89. package/src/mysql/errors/index.ts +1 -0
  90. package/src/mysql/errors/normalize.ts +55 -53
  91. package/src/mysql/errors/types.ts +74 -0
  92. package/src/mysql/executor.ts +88 -11
  93. package/src/mysql/function/aggregate.ts +1 -5
  94. package/src/mysql/function/core.ts +1 -4
  95. package/src/mysql/function/index.ts +0 -1
  96. package/src/mysql/function/string.ts +1 -5
  97. package/src/mysql/function/temporal.ts +12 -15
  98. package/src/mysql/function/window.ts +1 -6
  99. package/src/{internal/mysql-dialect.ts → mysql/internal/dialect.ts} +12 -6
  100. package/src/{internal/mysql-query.ts → mysql/internal/dsl.ts} +1299 -2143
  101. package/src/mysql/internal/renderer.ts +46 -0
  102. package/src/mysql/internal/sql-expression-renderer.ts +1501 -0
  103. package/src/mysql/json.ts +2 -0
  104. package/src/mysql/query.ts +111 -91
  105. package/src/mysql/renderer.ts +8 -3
  106. package/src/mysql/table.ts +1 -1
  107. package/src/mysql.ts +6 -4
  108. package/src/postgres/cast.ts +30 -16
  109. package/src/postgres/column.ts +179 -46
  110. package/src/postgres/datatypes/index.d.ts +515 -0
  111. package/src/postgres/datatypes/index.ts +22 -13
  112. package/src/postgres/datatypes/spec.d.ts +412 -0
  113. package/src/postgres/errors/generated.ts +2636 -0
  114. package/src/postgres/errors/index.ts +1 -0
  115. package/src/postgres/errors/normalize.ts +47 -62
  116. package/src/postgres/errors/types.ts +92 -34
  117. package/src/postgres/executor.ts +54 -7
  118. package/src/postgres/function/aggregate.ts +1 -5
  119. package/src/postgres/function/core.ts +12 -6
  120. package/src/postgres/function/index.ts +0 -1
  121. package/src/postgres/function/string.ts +1 -5
  122. package/src/postgres/function/temporal.ts +12 -15
  123. package/src/postgres/function/window.ts +1 -6
  124. package/src/{internal/postgres-dialect.ts → postgres/internal/dialect.ts} +12 -6
  125. package/src/{internal/postgres-query.ts → postgres/internal/dsl.ts} +1356 -2133
  126. package/src/{internal/postgres-renderer.ts → postgres/internal/renderer.ts} +17 -8
  127. package/src/postgres/internal/schema-ddl.ts +108 -0
  128. package/src/{internal/postgres-schema-model.ts → postgres/internal/schema-model.ts} +12 -6
  129. package/src/{internal → postgres/internal}/sql-expression-renderer.ts +79 -25
  130. package/src/postgres/{function/json.ts → json.ts} +77 -85
  131. package/src/postgres/metadata.ts +2 -2
  132. package/src/postgres/query.ts +113 -89
  133. package/src/postgres/renderer.ts +8 -13
  134. package/src/postgres/schema-expression.ts +2 -1
  135. package/src/postgres/schema-management.ts +1 -1
  136. package/src/postgres/table.ts +12 -4
  137. package/src/postgres/type.ts +33 -2
  138. package/src/postgres.ts +6 -4
  139. package/src/internal/expression.ts +0 -327
  140. package/src/internal/mysql-renderer.ts +0 -37
  141. package/src/internal/predicate-analysis.ts +0 -81
  142. package/src/internal/predicate-key.ts +0 -28
  143. package/src/internal/schema-ddl.ts +0 -55
  144. package/src/mysql/function/json.ts +0 -4
  145. package/src/mysql/private/query.ts +0 -1
  146. package/src/postgres/private/query.ts +0 -1
@@ -1,14 +1,14 @@
1
1
  import { pipeArguments } from "effect/Pipeable"
2
2
  import * as Schema from "effect/Schema"
3
3
 
4
- import { mysqlDatatypes } from "../mysql/datatypes/index.js"
5
-
6
- import * as Expression from "./expression.js"
7
- import * as Plan from "./plan.js"
8
- import * as Table from "./table.js"
9
- import type { CastTargetError, OperandCompatibilityError } from "./coercion-errors.js"
10
- import type { RuntimeOfDbType } from "./coercion-analysis.js"
11
- import type { CanCastDbType, CanCompareDbTypes, CanContainDbTypes, CanTextuallyCoerceDbType } from "./coercion-rules.js"
4
+ import { mysqlDatatypes } from "../datatypes/index.js"
5
+
6
+ import * as Expression from "../../internal/scalar.js"
7
+ import * as Plan from "../../internal/row-set.js"
8
+ import * as Table from "../../internal/table.js"
9
+ import type { CastTargetError, OperandCompatibilityError } from "../../internal/coercion/errors.js"
10
+ import type { RuntimeOfDbType } from "../../internal/coercion/analysis.js"
11
+ import type { CanCastDbType, CanCompareDbTypes, CanContainDbTypes, CanTextuallyCoerceDbType } from "../../internal/coercion/rules.js"
12
12
  import {
13
13
  currentRequiredList,
14
14
  extractRequiredRuntime,
@@ -21,15 +21,12 @@ import {
21
21
  mergeAggregationRuntime,
22
22
  mergeDependencies,
23
23
  mergeManyDependencies,
24
- mergeManySources,
25
24
  mergeNullabilityManyRuntime,
26
- mergeSources,
27
25
  type AddAvailable,
28
26
  type AddAvailableMany,
29
27
  type AvailableAfterJoin,
30
28
  type AddExpressionRequired,
31
29
  type AddJoinRequired,
32
- type AggregationOf,
33
30
  type AssumptionsOfPlan,
34
31
  type AvailableOfPlan,
35
32
  type CapabilitiesOfPlan,
@@ -43,6 +40,7 @@ import {
43
40
  type ExpressionInput,
44
41
  type ExtractDialect,
45
42
  type ExtractRequired,
43
+ type FactsOfPlan,
46
44
  type GroupByInput,
47
45
  type GroupedOfPlan,
48
46
  type GroupedKeysFromValues,
@@ -58,6 +56,8 @@ import {
58
56
  type PlanDialectOf,
59
57
  type PresenceWitnessKeysOfSource,
60
58
  type PredicateInput,
59
+ type PredicateStateOfPlan,
60
+ type KindOf,
61
61
  type QueryPlan,
62
62
  type OutputOfSelection,
63
63
  type ScalarOutputOfPlan,
@@ -80,7 +80,6 @@ import {
80
80
  type MergeCapabilities,
81
81
  type MutationTargetInput,
82
82
  type MutationValuesInput,
83
- type SourceOf,
84
83
  type SourceDialectOf,
85
84
  type SourceLike,
86
85
  type SourceNameOf,
@@ -89,6 +88,7 @@ import {
89
88
  type ValuesInput,
90
89
  type AnyValuesSource,
91
90
  type AnyUnnestSource,
91
+ type AnyTableFunctionSource,
92
92
  type UnnestSource,
93
93
  type TableFunctionSource,
94
94
  type StringExpressionInput,
@@ -98,14 +98,13 @@ import {
98
98
  type MutationTargetTuple,
99
99
  type TupleDependencies,
100
100
  type TupleDialect,
101
- type TupleSource,
102
101
  type ResultRow
103
- } from "./query.js"
104
- import * as ExpressionAst from "./expression-ast.js"
105
- import { presenceWitnessesOfSourceLike } from "./implication-runtime.js"
106
- import type { JsonNode } from "./json/ast.js"
107
- import type { JsonPathUsageError } from "./json/errors.js"
108
- import * as JsonPath from "./json/path.js"
102
+ } from "../../internal/query.js"
103
+ import * as ExpressionAst from "../../internal/expression-ast.js"
104
+ import { presenceWitnessesOfSourceLike } from "../../internal/implication-runtime.js"
105
+ import type { JsonNode } from "../../internal/json/ast.js"
106
+ import type { JsonPathUsageError } from "../../internal/json/errors.js"
107
+ import * as JsonPath from "../../internal/json/path.js"
109
108
  import type {
110
109
  JsonConcatResult,
111
110
  JsonDeleteAtPath,
@@ -119,16 +118,20 @@ import type {
119
118
  JsonTypeName,
120
119
  JsonValueAtPath,
121
120
  NormalizeJsonLiteral
122
- } from "./json/types.js"
123
- import type { AssumeTrue } from "./predicate-analysis.js"
124
- import type { FormulaOfPredicate } from "./predicate-normalize.js"
125
- import type { TrueFormula } from "./predicate-formula.js"
126
- import { assumeFormulaTrue, formulaOfExpression as formulaOfExpressionRuntime, trueFormula } from "./predicate-runtime.js"
127
- import { dedupeGroupedExpressions } from "./grouping-key.js"
128
- import { makeCteSource, makeDerivedSource, makeLateralSource } from "./derived-table.js"
129
- import * as ProjectionAlias from "./projection-alias.js"
130
- import * as QueryAst from "./query-ast.js"
131
- import { normalizeColumnList } from "./table-options.js"
121
+ } from "../../internal/json/types.js"
122
+ import type { AssumePredicateStateTrue, EmptyFacts, PredicateStateFacts, PredicateStateFormula } from "../../internal/predicate/analysis.js"
123
+ import type { FormulaOfPredicate } from "../../internal/predicate/normalize.js"
124
+ import type { TrueFormula } from "../../internal/predicate/formula.js"
125
+ import { assumeFormulaTrue, formulaOfExpression as formulaOfExpressionRuntime, trueFormula } from "../../internal/predicate/runtime.js"
126
+ import { dedupeGroupedExpressions } from "../../internal/grouping-key.js"
127
+ import { makeDslMutationRuntime } from "../../internal/dsl-mutation-runtime.js"
128
+ import { makeDslPlanRuntime } from "../../internal/dsl-plan-runtime.js"
129
+ import { makeDslQueryRuntime } from "../../internal/dsl-query-runtime.js"
130
+ import { makeDslTransactionDdlRuntime } from "../../internal/dsl-transaction-ddl-runtime.js"
131
+ import { makeCteSource, makeDerivedSource, makeLateralSource } from "../../internal/derived-table.js"
132
+ import * as ProjectionAlias from "../../internal/projection-alias.js"
133
+ import * as QueryAst from "../../internal/query-ast.js"
134
+ import { normalizeColumnList } from "../../internal/table-options.js"
132
135
 
133
136
  /**
134
137
  * Dialect-specific DB type profile used to specialize the shared query
@@ -190,7 +193,7 @@ type DialectLiteralExpression<
190
193
  BoolDb extends Expression.DbType.Any,
191
194
  TimestampDb extends Expression.DbType.Any,
192
195
  NullDb extends Expression.DbType.Any
193
- > = Expression.Expression<
196
+ > = Expression.Scalar<
194
197
  DialectLiteralRuntime<Value, TimestampDb>,
195
198
  DialectLiteralDbType<Value, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
196
199
  LiteralNullability<Value>,
@@ -325,15 +328,13 @@ type CastResult<
325
328
  BoolDb extends Expression.DbType.Any,
326
329
  TimestampDb extends Expression.DbType.Any,
327
330
  NullDb extends Expression.DbType.Any
328
- > = Expression.Expression<
331
+ > = Expression.Scalar<
329
332
  RuntimeOfDbType<Target>,
330
333
  Target,
331
334
  Expression.NullabilityOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
332
335
  Dialect,
333
- AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
334
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
335
- DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
336
- Expression.SourceNullabilityOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
336
+ KindOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
337
+ DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
337
338
  > & {
338
339
  readonly [ExpressionAst.TypeId]: ExpressionAst.CastNode<
339
340
  DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
@@ -370,17 +371,39 @@ type ComparableGuard<
370
371
  TimestampDb extends Expression.DbType.Any,
371
372
  NullDb extends Expression.DbType.Any,
372
373
  Operator extends string
373
- > = CanCompareDbTypes<
374
- DialectDbTypeOfInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
375
- DialectDbTypeOfInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
376
- Dialect
377
- > extends true ? true : OperandCompatibilityError<
378
- Operator,
379
- DialectDbTypeOfInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
380
- DialectDbTypeOfInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
374
+ > = 0 extends (1 & Left)
375
+ ? true
376
+ : 0 extends (1 & Right)
377
+ ? true
378
+ : DialectDbTypeOfInput<
379
+ Left,
381
380
  Dialect,
382
- "the same db type family"
383
- >
381
+ TextDb,
382
+ NumericDb,
383
+ BoolDb,
384
+ TimestampDb,
385
+ NullDb
386
+ > extends infer LeftDb extends Expression.DbType.Any
387
+ ? DialectDbTypeOfInput<
388
+ Right,
389
+ Dialect,
390
+ TextDb,
391
+ NumericDb,
392
+ BoolDb,
393
+ TimestampDb,
394
+ NullDb
395
+ > extends infer RightDb extends Expression.DbType.Any
396
+ ? CanCompareDbTypes<LeftDb, RightDb, Dialect> extends true
397
+ ? true
398
+ : OperandCompatibilityError<
399
+ Operator,
400
+ LeftDb,
401
+ RightDb,
402
+ Dialect,
403
+ "the same db type family"
404
+ >
405
+ : never
406
+ : never
384
407
 
385
408
  type TextGuard<
386
409
  Value extends ExpressionInput,
@@ -465,9 +488,10 @@ type ComparableArgs<
465
488
  TimestampDb extends Expression.DbType.Any,
466
489
  NullDb extends Expression.DbType.Any,
467
490
  Operator extends string
468
- > = ComparableGuard<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator> extends true
469
- ? readonly [left: Left, right: Right]
470
- : readonly [ComparableGuard<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>]
491
+ > = readonly [
492
+ left: Left,
493
+ right: Right & ComparableInput<NoInfer<Left>, NoInfer<Right>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>
494
+ ]
471
495
 
472
496
  type ContainmentGuard<
473
497
  Left extends ExpressionInput,
@@ -515,11 +539,10 @@ type TextArgs<
515
539
  TimestampDb extends Expression.DbType.Any,
516
540
  NullDb extends Expression.DbType.Any,
517
541
  Operator extends string
518
- > = TextGuard<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator> extends true
519
- ? TextGuard<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator> extends true
520
- ? readonly [left: Left, right: Right]
521
- : readonly [TextGuard<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>]
522
- : readonly [TextGuard<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>]
542
+ > = readonly [
543
+ left: Left & TextInput<NoInfer<Left>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>,
544
+ right: Right & TextInput<NoInfer<Right>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>
545
+ ]
523
546
 
524
547
  type MembershipArgs<
525
548
  Values extends readonly [ExpressionInput, ExpressionInput, ...ExpressionInput[]],
@@ -590,17 +613,6 @@ type BetweenArgs<
590
613
  "between"
591
614
  >]
592
615
 
593
- /** Provenance carried by a dialect-specialized scalar input. */
594
- type SourceOfDialectInput<
595
- Value extends ExpressionInput,
596
- Dialect extends string,
597
- TextDb extends Expression.DbType.Any,
598
- NumericDb extends Expression.DbType.Any,
599
- BoolDb extends Expression.DbType.Any,
600
- TimestampDb extends Expression.DbType.Any,
601
- NullDb extends Expression.DbType.Any
602
- > = SourceOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
603
-
604
616
  /** Dialect carried by a dialect-specialized scalar input. */
605
617
  type DialectOfDialectInput<
606
618
  Value extends ExpressionInput,
@@ -634,17 +646,6 @@ type RequiredFromDialectInput<
634
646
  NullDb extends Expression.DbType.Any
635
647
  > = RequiredFromDependencies<DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
636
648
 
637
- /** Provenance carried by a dialect-specialized string input. */
638
- type SourceOfDialectStringInput<
639
- Value extends ExpressionInput,
640
- Dialect extends string,
641
- TextDb extends Expression.DbType.Any,
642
- NumericDb extends Expression.DbType.Any,
643
- BoolDb extends Expression.DbType.Any,
644
- TimestampDb extends Expression.DbType.Any,
645
- NullDb extends Expression.DbType.Any
646
- > = SourceOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
647
-
648
649
  /** Dialect carried by a dialect-specialized string input. */
649
650
  type DialectOfDialectStringInput<
650
651
  Value extends ExpressionInput,
@@ -714,9 +715,9 @@ type RequiredFromDialectNumericInput<
714
715
  /** Folds aggregation kinds across a tuple of expressions. */
715
716
  type MergeAggregationTuple<
716
717
  Values extends readonly Expression.Any[],
717
- Current extends Expression.AggregationKind = "scalar"
718
+ Current extends Expression.ScalarKind = "scalar"
718
719
  > = Values extends readonly [infer Head extends Expression.Any, ...infer Tail extends readonly Expression.Any[]]
719
- ? MergeAggregationTuple<Tail, MergeAggregation<Current, AggregationOf<Head>>>
720
+ ? MergeAggregationTuple<Tail, MergeAggregation<Current, KindOf<Head>>>
720
721
  : Current
721
722
 
722
723
  /** Result nullability for binary `coalesce(...)`. */
@@ -763,6 +764,18 @@ type DialectExpressionTuple<
763
764
  readonly [K in keyof Values]: DialectAsExpression<Values[K], Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
764
765
  }
765
766
 
767
+ type DialectExpressionArray<
768
+ Values extends readonly ExpressionInput[],
769
+ Dialect extends string,
770
+ TextDb extends Expression.DbType.Any,
771
+ NumericDb extends Expression.DbType.Any,
772
+ BoolDb extends Expression.DbType.Any,
773
+ TimestampDb extends Expression.DbType.Any,
774
+ NullDb extends Expression.DbType.Any
775
+ > = DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> extends infer Tuple extends readonly Expression.Any[]
776
+ ? Tuple
777
+ : never
778
+
766
779
  /** Normalized expression tuple for generic string operator inputs. */
767
780
  type DialectStringExpressionTuple<
768
781
  Values extends readonly ExpressionInput[],
@@ -779,7 +792,7 @@ type DialectStringExpressionTuple<
779
792
  /** Names of sources already available to a plan. */
780
793
  type AvailableNames<Available extends Record<string, Plan.AnySource>> = Extract<keyof Available, string>
781
794
 
782
- type PlanAssumptionsAfterWhere<
795
+ type PlanPredicateStateAfterWhere<
783
796
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
784
797
  Predicate extends PredicateInput,
785
798
  Dialect extends string,
@@ -788,12 +801,34 @@ type PlanAssumptionsAfterWhere<
788
801
  BoolDb extends Expression.DbType.Any,
789
802
  TimestampDb extends Expression.DbType.Any,
790
803
  NullDb extends Expression.DbType.Any
791
- > = AssumeTrue<
792
- AssumptionsOfPlan<PlanValue>,
804
+ > = AssumePredicateStateTrue<
805
+ PredicateStateOfPlan<PlanValue>,
793
806
  DialectAsExpression<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
794
807
  >
795
808
 
796
- type PlanAssumptionsAfterHaving<
809
+ type PlanAssumptionsAfterWhere<
810
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
811
+ Predicate extends PredicateInput,
812
+ Dialect extends string,
813
+ TextDb extends Expression.DbType.Any,
814
+ NumericDb extends Expression.DbType.Any,
815
+ BoolDb extends Expression.DbType.Any,
816
+ TimestampDb extends Expression.DbType.Any,
817
+ NullDb extends Expression.DbType.Any
818
+ > = PredicateStateFormula<PlanPredicateStateAfterWhere<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
819
+
820
+ type PlanFactsAfterWhere<
821
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
822
+ Predicate extends PredicateInput,
823
+ Dialect extends string,
824
+ TextDb extends Expression.DbType.Any,
825
+ NumericDb extends Expression.DbType.Any,
826
+ BoolDb extends Expression.DbType.Any,
827
+ TimestampDb extends Expression.DbType.Any,
828
+ NullDb extends Expression.DbType.Any
829
+ > = PredicateStateFacts<PlanPredicateStateAfterWhere<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
830
+
831
+ type PlanPredicateStateAfterHaving<
797
832
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
798
833
  Predicate extends HavingPredicateInput,
799
834
  Dialect extends string,
@@ -802,12 +837,34 @@ type PlanAssumptionsAfterHaving<
802
837
  BoolDb extends Expression.DbType.Any,
803
838
  TimestampDb extends Expression.DbType.Any,
804
839
  NullDb extends Expression.DbType.Any
805
- > = AssumeTrue<
806
- AssumptionsOfPlan<PlanValue>,
840
+ > = AssumePredicateStateTrue<
841
+ PredicateStateOfPlan<PlanValue>,
807
842
  DialectAsExpression<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
808
843
  >
809
844
 
810
- type PlanAssumptionsAfterJoin<
845
+ type PlanAssumptionsAfterHaving<
846
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
847
+ Predicate extends HavingPredicateInput,
848
+ Dialect extends string,
849
+ TextDb extends Expression.DbType.Any,
850
+ NumericDb extends Expression.DbType.Any,
851
+ BoolDb extends Expression.DbType.Any,
852
+ TimestampDb extends Expression.DbType.Any,
853
+ NullDb extends Expression.DbType.Any
854
+ > = PredicateStateFormula<PlanPredicateStateAfterHaving<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
855
+
856
+ type PlanFactsAfterHaving<
857
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
858
+ Predicate extends HavingPredicateInput,
859
+ Dialect extends string,
860
+ TextDb extends Expression.DbType.Any,
861
+ NumericDb extends Expression.DbType.Any,
862
+ BoolDb extends Expression.DbType.Any,
863
+ TimestampDb extends Expression.DbType.Any,
864
+ NullDb extends Expression.DbType.Any
865
+ > = PredicateStateFacts<PlanPredicateStateAfterHaving<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
866
+
867
+ type PlanPredicateStateAfterJoin<
811
868
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
812
869
  Predicate extends PredicateInput,
813
870
  Kind extends QueryAst.JoinKind,
@@ -818,11 +875,35 @@ type PlanAssumptionsAfterJoin<
818
875
  TimestampDb extends Expression.DbType.Any,
819
876
  NullDb extends Expression.DbType.Any
820
877
  > = Kind extends "inner"
821
- ? AssumeTrue<
822
- AssumptionsOfPlan<PlanValue>,
878
+ ? AssumePredicateStateTrue<
879
+ PredicateStateOfPlan<PlanValue>,
823
880
  DialectAsExpression<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
824
881
  >
825
- : AssumptionsOfPlan<PlanValue>
882
+ : PredicateStateOfPlan<PlanValue>
883
+
884
+ type PlanAssumptionsAfterJoin<
885
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
886
+ Predicate extends PredicateInput,
887
+ Kind extends QueryAst.JoinKind,
888
+ Dialect extends string,
889
+ TextDb extends Expression.DbType.Any,
890
+ NumericDb extends Expression.DbType.Any,
891
+ BoolDb extends Expression.DbType.Any,
892
+ TimestampDb extends Expression.DbType.Any,
893
+ NullDb extends Expression.DbType.Any
894
+ > = PredicateStateFormula<PlanPredicateStateAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
895
+
896
+ type PlanFactsAfterJoin<
897
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
898
+ Predicate extends PredicateInput,
899
+ Kind extends QueryAst.JoinKind,
900
+ Dialect extends string,
901
+ TextDb extends Expression.DbType.Any,
902
+ NumericDb extends Expression.DbType.Any,
903
+ BoolDb extends Expression.DbType.Any,
904
+ TimestampDb extends Expression.DbType.Any,
905
+ NullDb extends Expression.DbType.Any
906
+ > = PredicateStateFacts<PlanPredicateStateAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
826
907
 
827
908
  type ScalarSubqueryInput<
828
909
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
@@ -836,20 +917,16 @@ type AstBackedExpression<
836
917
  Db extends Expression.DbType.Any,
837
918
  Nullable extends Expression.Nullability,
838
919
  Dialect extends string,
839
- Aggregation extends Expression.AggregationKind,
840
- Source,
841
- Dependencies extends Expression.SourceDependencies,
842
- Ast extends ExpressionAst.Any,
843
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
844
- > = Expression.Expression<
920
+ Aggregation extends Expression.ScalarKind,
921
+ Dependencies extends Expression.BindingId,
922
+ Ast extends ExpressionAst.Any
923
+ > = Expression.Scalar<
845
924
  Runtime,
846
925
  Db,
847
926
  Nullable,
848
927
  Dialect,
849
928
  Aggregation,
850
- Source,
851
- Dependencies,
852
- SourceNullability
929
+ Dependencies
853
930
  > & {
854
931
  readonly [ExpressionAst.TypeId]: Ast
855
932
  }
@@ -882,8 +959,7 @@ type VariadicBooleanExpression<
882
959
  BoolDb,
883
960
  MergeNullabilityTuple<Values>,
884
961
  TupleDialect<Values>,
885
- MergeAggregationTuple<Values>,
886
- TupleSource<Values>,
962
+ "scalar",
887
963
  TupleDependencies<Values>,
888
964
  ExpressionAst.VariadicNode<Kind, Values>
889
965
  > & {
@@ -915,32 +991,26 @@ type JsonExpression<
915
991
  Db extends Expression.DbType.Any,
916
992
  Nullability extends Expression.Nullability,
917
993
  Dialect extends string,
918
- Aggregation extends Expression.AggregationKind,
919
- Source,
920
- Dependencies extends Expression.SourceDependencies,
921
- Ast extends ExpressionAst.Any,
922
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
994
+ Aggregation extends Expression.ScalarKind,
995
+ Dependencies extends Expression.BindingId,
996
+ Ast extends ExpressionAst.Any
923
997
  > = AstBackedExpression<
924
998
  Runtime,
925
999
  Db,
926
1000
  Nullability,
927
1001
  Dialect,
928
1002
  Aggregation,
929
- Source,
930
1003
  Dependencies,
931
- Ast,
932
- SourceNullability
1004
+ Ast
933
1005
  >
934
1006
 
935
- type JsonExpressionLike<Runtime = unknown> = Expression.Expression<
1007
+ type JsonExpressionLike<Runtime = unknown> = Expression.Scalar<
936
1008
  Runtime,
937
1009
  Expression.DbType.Json<any, any>,
938
1010
  Expression.Nullability,
939
1011
  string,
940
- Expression.AggregationKind,
941
- any,
942
- Expression.SourceDependencies,
943
- Expression.SourceNullabilityMode
1012
+ Expression.ScalarKind,
1013
+ Expression.BindingId
944
1014
  >
945
1015
 
946
1016
  type JsonDbOfExpression<Value extends JsonExpressionLike<any>> = Expression.DbTypeOf<Value>
@@ -1100,8 +1170,8 @@ type CaseResultTupleWithElse<
1100
1170
  > = [...CaseResultTuple<Branches>, Else]
1101
1171
 
1102
1172
  type MergeAggregationUnion<Value extends Expression.Any> =
1103
- Extract<AggregationOf<Value>, "window"> extends never
1104
- ? Extract<AggregationOf<Value>, "aggregate"> extends never ? "scalar" : "aggregate"
1173
+ Extract<KindOf<Value>, "window"> extends never
1174
+ ? Extract<KindOf<Value>, "aggregate"> extends never ? "scalar" : "aggregate"
1105
1175
  : "window"
1106
1176
 
1107
1177
  type CaseNullabilityOfUnion<Value extends Expression.Any> =
@@ -1149,10 +1219,8 @@ type CaseBuilder<
1149
1219
  CaseNullabilityOfUnion<CaseResultTupleWithElse<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>[number]>,
1150
1220
  TupleDialect<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1151
1221
  MergeAggregationTuple<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1152
- TupleSource<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1153
1222
  TupleDependencies<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1154
- ExpressionAst.CaseNode<CaseAstBranches<Branches>, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1155
- "resolved"
1223
+ ExpressionAst.CaseNode<CaseAstBranches<Branches>, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1156
1224
  >
1157
1225
  }
1158
1226
 
@@ -1167,7 +1235,7 @@ type MatchBuilder<
1167
1235
  NullDb extends Expression.DbType.Any
1168
1236
  > = {
1169
1237
  when<Compare extends ExpressionInput, Then extends ExpressionInput>(
1170
- compare: Compare & ComparableInput<Subject, Compare, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "match">,
1238
+ compare: Compare & ComparableInput<NoInfer<Subject>, NoInfer<Compare>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "match">,
1171
1239
  result: Then
1172
1240
  ): MatchBuilder<
1173
1241
  Subject,
@@ -1190,10 +1258,8 @@ type MatchBuilder<
1190
1258
  CaseNullabilityOfUnion<CaseResultTupleWithElse<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>[number]>,
1191
1259
  TupleDialect<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1192
1260
  MergeAggregationTuple<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1193
- TupleSource<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1194
1261
  TupleDependencies<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1195
- ExpressionAst.CaseNode<CaseAstBranches<Branches>, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1196
- "resolved"
1262
+ ExpressionAst.CaseNode<CaseAstBranches<Branches>, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1197
1263
  >
1198
1264
  }
1199
1265
 
@@ -1232,7 +1298,7 @@ type MatchStarter<
1232
1298
  NullDb extends Expression.DbType.Any
1233
1299
  > = {
1234
1300
  when<Compare extends ExpressionInput, Then extends ExpressionInput>(
1235
- compare: Compare & ComparableInput<Subject, Compare, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "match">,
1301
+ compare: Compare & ComparableInput<NoInfer<Subject>, NoInfer<Compare>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "match">,
1236
1302
  result: Then
1237
1303
  ): MatchBuilder<
1238
1304
  Subject,
@@ -1249,15 +1315,13 @@ type MatchStarter<
1249
1315
  >
1250
1316
  }
1251
1317
 
1252
- type WindowPartitionInput = Expression.Expression<
1318
+ type WindowPartitionInput = Expression.Scalar<
1253
1319
  any,
1254
1320
  Expression.DbType.Any,
1255
1321
  Expression.Nullability,
1256
1322
  string,
1257
1323
  "scalar",
1258
- any,
1259
- Expression.SourceDependencies,
1260
- Expression.SourceNullabilityMode
1324
+ Expression.BindingId
1261
1325
  >
1262
1326
 
1263
1327
  type WindowOrderInput = WindowPartitionInput
@@ -1316,12 +1380,6 @@ type WindowDialectOf<
1316
1380
  OrderBy extends readonly WindowOrderTermInput[]
1317
1381
  > = DialectOf<Value> | TupleDialect<PartitionBy> | TupleDialect<WindowOrderExpressionTuple<OrderBy>>
1318
1382
 
1319
- type WindowSourceOf<
1320
- Value extends Expression.Any,
1321
- PartitionBy extends readonly WindowPartitionInput[],
1322
- OrderBy extends readonly WindowOrderTermInput[]
1323
- > = SourceOf<Value> | TupleSource<PartitionBy> | TupleSource<WindowOrderExpressionTuple<OrderBy>>
1324
-
1325
1383
  type WindowDependenciesOf<
1326
1384
  Value extends Expression.Any,
1327
1385
  PartitionBy extends readonly WindowPartitionInput[],
@@ -1337,11 +1395,6 @@ type NumberWindowDialectOf<
1337
1395
  OrderBy extends readonly WindowOrderTermInput[]
1338
1396
  > = TupleDialect<PartitionBy> | TupleDialect<WindowOrderExpressionTuple<OrderBy>>
1339
1397
 
1340
- type NumberWindowSourceOf<
1341
- PartitionBy extends readonly WindowPartitionInput[],
1342
- OrderBy extends readonly WindowOrderTermInput[]
1343
- > = TupleSource<PartitionBy> | TupleSource<WindowOrderExpressionTuple<OrderBy>>
1344
-
1345
1398
  type NumberWindowDependenciesOf<
1346
1399
  PartitionBy extends readonly WindowPartitionInput[],
1347
1400
  OrderBy extends readonly WindowOrderTermInput[]
@@ -1360,10 +1413,8 @@ type WindowedExpression<
1360
1413
  Expression.NullabilityOf<Value>,
1361
1414
  WindowDialectOf<Value, PartitionBy, OrderBy>,
1362
1415
  "window",
1363
- WindowSourceOf<Value, PartitionBy, OrderBy>,
1364
1416
  WindowDependenciesOf<Value, PartitionBy, OrderBy>,
1365
- WindowNodeOf<"over", Value, PartitionBy, OrderBy>,
1366
- "resolved"
1417
+ WindowNodeOf<"over", Value, PartitionBy, OrderBy>
1367
1418
  >
1368
1419
 
1369
1420
  type NumberWindowExpression<
@@ -1376,10 +1427,8 @@ type NumberWindowExpression<
1376
1427
  "never",
1377
1428
  NumberWindowDialectOf<PartitionBy, OrderBy>,
1378
1429
  "window",
1379
- NumberWindowSourceOf<PartitionBy, OrderBy>,
1380
1430
  NumberWindowDependenciesOf<PartitionBy, OrderBy>,
1381
- WindowNodeOf<Kind, undefined, PartitionBy, OrderBy>,
1382
- "resolved"
1431
+ WindowNodeOf<Kind, undefined, PartitionBy, OrderBy>
1383
1432
  >
1384
1433
 
1385
1434
  /**
@@ -1389,22 +1438,33 @@ type NumberWindowExpression<
1389
1438
  * that manufacture new expressions are specialized to the supplied dialect DB
1390
1439
  * types instead of relying on the Postgres-default root module.
1391
1440
  */
1392
- export const mysqlQuery = (() => {
1393
1441
  type Dialect = "mysql"
1394
- type TextDb = Expression.DbType.MySqlText
1395
- type NumericDb = Expression.DbType.MySqlDouble
1396
- type BoolDb = Expression.DbType.MySqlBool
1397
- type TimestampDb = Expression.DbType.MySqlTimestamp
1398
- type NullDb = Expression.DbType.Base<"mysql", "null">
1442
+ type TextDb = ReturnType<typeof mysqlDatatypes.text>
1443
+ type NumericDb = ReturnType<typeof mysqlDatatypes.double>
1444
+ type BoolDb = ReturnType<typeof mysqlDatatypes.boolean>
1445
+ type TimestampDb = ReturnType<typeof mysqlDatatypes.timestamp>
1446
+ type NullDb = Expression.DbType.Base<"mysql", "null"> & {
1447
+ readonly family: "null"
1448
+ readonly runtime: "unknown"
1449
+ readonly compareGroup: "null"
1450
+ readonly traits: {}
1451
+ }
1399
1452
  type TypeWitnesses = typeof mysqlDatatypes
1400
1453
 
1401
1454
  const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, TypeWitnesses> = {
1402
1455
  dialect: "mysql",
1403
- textDb: { dialect: "mysql", kind: "text" } as TextDb,
1404
- numericDb: { dialect: "mysql", kind: "double" } as NumericDb,
1405
- boolDb: { dialect: "mysql", kind: "boolean" } as BoolDb,
1406
- timestampDb: { dialect: "mysql", kind: "timestamp" } as TimestampDb,
1407
- nullDb: { dialect: "mysql", kind: "null" } as NullDb,
1456
+ textDb: mysqlDatatypes.text() as TextDb,
1457
+ numericDb: mysqlDatatypes.double() as NumericDb,
1458
+ boolDb: mysqlDatatypes.boolean() as BoolDb,
1459
+ timestampDb: mysqlDatatypes.timestamp() as TimestampDb,
1460
+ nullDb: {
1461
+ dialect: "mysql",
1462
+ kind: "null",
1463
+ family: "null",
1464
+ runtime: "unknown",
1465
+ compareGroup: "null",
1466
+ traits: {}
1467
+ } as NullDb,
1408
1468
  type: mysqlDatatypes
1409
1469
  }
1410
1470
  const ValuesInputProto = {
@@ -1437,9 +1497,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1437
1497
  runtimeSchema: literalSchemaOf(value),
1438
1498
  nullability: (value === null ? "always" : "never") as LiteralNullability<Value>,
1439
1499
  dialect: profile.dialect as Dialect,
1440
- aggregation: "scalar",
1441
- source: undefined as never,
1442
- sourceNullability: "propagate" as const,
1500
+ kind: "scalar",
1501
+
1443
1502
  dependencies: {}
1444
1503
  }, {
1445
1504
  kind: "literal",
@@ -1453,15 +1512,13 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1453
1512
  name: Name,
1454
1513
  dbType: Db,
1455
1514
  nullable = false
1456
- ): Expression.Expression<
1515
+ ): Expression.Scalar<
1457
1516
  Expression.RuntimeOfDbType<Db> | null,
1458
1517
  Db,
1459
1518
  Expression.Nullability,
1460
1519
  Dialect,
1461
1520
  "scalar",
1462
- never,
1463
- {},
1464
- "resolved"
1521
+ never
1465
1522
  > & {
1466
1523
  readonly [ExpressionAst.TypeId]: ExpressionAst.ColumnNode<"", Name>
1467
1524
  } =>
@@ -1470,23 +1527,20 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1470
1527
  dbType,
1471
1528
  nullability: (nullable ? "maybe" : "never") as typeof nullable extends true ? "maybe" : "never",
1472
1529
  dialect: profile.dialect as Dialect,
1473
- aggregation: "scalar",
1474
- source: undefined as never,
1475
- sourceNullability: "resolved" as const,
1530
+ kind: "scalar",
1531
+
1476
1532
  dependencies: {}
1477
1533
  }, {
1478
1534
  kind: "column",
1479
1535
  tableName: "",
1480
1536
  columnName: name
1481
- }) as Expression.Expression<
1537
+ }) as Expression.Scalar<
1482
1538
  Expression.RuntimeOfDbType<Db> | null,
1483
1539
  Db,
1484
1540
  Expression.Nullability,
1485
1541
  Dialect,
1486
1542
  "scalar",
1487
- never,
1488
- {},
1489
- "resolved"
1543
+ never
1490
1544
  > & {
1491
1545
  readonly [ExpressionAst.TypeId]: ExpressionAst.ColumnNode<"", Name>
1492
1546
  }
@@ -1508,6 +1562,42 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1508
1562
  >
1509
1563
  }
1510
1564
 
1565
+ const retargetLiteralExpression = (
1566
+ value: Expression.Any,
1567
+ target: Expression.Any
1568
+ ): Expression.Any => {
1569
+ const ast = (value as unknown as { readonly [ExpressionAst.TypeId]: ExpressionAst.Any })[ExpressionAst.TypeId]
1570
+ if (ast.kind !== "literal") {
1571
+ return value
1572
+ }
1573
+ const targetState = target[Expression.TypeId]
1574
+ return makeExpression({
1575
+ runtime: value[Expression.TypeId].runtime,
1576
+ dbType: targetState.dbType,
1577
+ runtimeSchema: targetState.runtimeSchema,
1578
+ driverValueMapping: targetState.driverValueMapping,
1579
+ nullability: value[Expression.TypeId].nullability,
1580
+ dialect: targetState.dialect,
1581
+ kind: "scalar",
1582
+ dependencies: {}
1583
+ }, ast)
1584
+ }
1585
+
1586
+ const alignBinaryPredicateExpressions = (
1587
+ left: Expression.Any,
1588
+ right: Expression.Any
1589
+ ): readonly [Expression.Any, Expression.Any] => {
1590
+ const leftAst = (left as unknown as { readonly [ExpressionAst.TypeId]: ExpressionAst.Any })[ExpressionAst.TypeId]
1591
+ const rightAst = (right as unknown as { readonly [ExpressionAst.TypeId]: ExpressionAst.Any })[ExpressionAst.TypeId]
1592
+ if (leftAst.kind === "literal" && rightAst.kind !== "literal") {
1593
+ return [retargetLiteralExpression(left, right), right]
1594
+ }
1595
+ if (rightAst.kind === "literal" && leftAst.kind !== "literal") {
1596
+ return [left, retargetLiteralExpression(right, left)]
1597
+ }
1598
+ return [left, right]
1599
+ }
1600
+
1511
1601
  const toDialectStringExpression = <Value extends StringExpressionInput>(
1512
1602
  value: Value
1513
1603
  ): DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> =>
@@ -1554,10 +1644,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1554
1644
  dbType: profile.boolDb as BoolDb,
1555
1645
  nullability: mergeNullabilityManyRuntime(expressions) as MergeNullabilityTuple<Values>,
1556
1646
  dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as TupleDialect<Values>,
1557
- aggregation: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<Values>,
1558
- source: mergeManySources(expressions) as TupleSource<Values>,
1559
- sourceNullability: "propagate" as const,
1560
- dependencies: mergeManyDependencies(expressions) as TupleDependencies<Values>
1647
+ kind: "scalar",
1648
+
1649
+ dependencies: mergeManyDependencies(expressions)
1561
1650
  }, {
1562
1651
  kind,
1563
1652
  values: expressions
@@ -1624,39 +1713,32 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1624
1713
  return Object.keys(expression[Expression.TypeId].dependencies)
1625
1714
  }
1626
1715
 
1627
- type BinaryPredicateExpression<
1628
- Left extends ExpressionInput,
1629
- Right extends ExpressionInput,
1630
- Kind extends ExpressionAst.BinaryKind,
1631
- Nullability extends Expression.Nullability = "maybe",
1632
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
1633
- > = AstBackedExpression<
1634
- boolean,
1635
- BoolDb,
1636
- Nullability,
1637
- DialectOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | DialectOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1638
- MergeAggregation<
1639
- AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1640
- AggregationOf<DialectAsExpression<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1641
- >,
1642
- SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | SourceOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1643
- DependencyRecord<
1644
- RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> |
1645
- RequiredFromDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1646
- >,
1647
- ExpressionAst.BinaryNode<
1648
- Kind,
1649
- DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1650
- DialectAsExpression<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1651
- >,
1652
- SourceNullability
1716
+ type BinaryPredicateExpression<
1717
+ Left extends ExpressionInput,
1718
+ Right extends ExpressionInput,
1719
+ Kind extends ExpressionAst.BinaryKind,
1720
+ Nullability extends Expression.Nullability = "maybe"
1721
+ > = AstBackedExpression<
1722
+ boolean,
1723
+ BoolDb,
1724
+ Nullability,
1725
+ DialectOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | DialectOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1726
+ "scalar",
1727
+ DependencyRecord<
1728
+ RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> |
1729
+ RequiredFromDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1730
+ >,
1731
+ ExpressionAst.BinaryNode<
1732
+ Kind,
1733
+ DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1734
+ DialectAsExpression<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1653
1735
  >
1736
+ >
1654
1737
 
1655
1738
  type SubqueryPredicateExpression<
1656
1739
  Dialect extends string,
1657
- Aggregation extends Expression.AggregationKind,
1658
- Source,
1659
- Dependencies extends Expression.SourceDependencies,
1740
+ Aggregation extends Expression.ScalarKind,
1741
+ Dependencies extends Expression.BindingId,
1660
1742
  Ast extends ExpressionAst.Any
1661
1743
  > = AstBackedExpression<
1662
1744
  boolean,
@@ -1664,7 +1746,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1664
1746
  "maybe",
1665
1747
  Dialect,
1666
1748
  Aggregation,
1667
- Source,
1668
1749
  Dependencies,
1669
1750
  Ast
1670
1751
  >
@@ -1672,47 +1753,42 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1672
1753
  type VariadicPredicateExpression<
1673
1754
  Values extends readonly ExpressionInput[],
1674
1755
  Kind extends ExpressionAst.VariadicKind
1675
- > = AstBackedExpression<
1676
- boolean,
1677
- BoolDb,
1678
- "maybe",
1679
- TupleDialect<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1680
- MergeAggregationTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1681
- TupleSource<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1682
- TupleDependencies<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1683
- ExpressionAst.VariadicNode<Kind, DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1684
- >
1756
+ > = AstBackedExpression<
1757
+ boolean,
1758
+ BoolDb,
1759
+ "maybe",
1760
+ TupleDialect<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1761
+ "scalar",
1762
+ TupleDependencies<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1763
+ ExpressionAst.VariadicNode<Kind, DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1764
+ >
1685
1765
 
1686
1766
  const buildBinaryPredicate = <
1687
1767
  Left extends ExpressionInput,
1688
1768
  Right extends ExpressionInput,
1689
1769
  Kind extends ExpressionAst.BinaryKind,
1690
1770
  Nullability extends Expression.Nullability = "maybe",
1691
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
1771
+ SourceNullability extends string = "propagate"
1692
1772
  >(
1693
1773
  left: Left,
1694
1774
  right: Right,
1695
1775
  kind: Kind,
1696
1776
  nullability: Nullability = "maybe" as Nullability,
1697
- sourceNullability: SourceNullability = "propagate" as SourceNullability
1698
1777
  ): any => {
1699
- const leftExpression = toDialectExpression(left)
1700
- const rightExpression = toDialectExpression(right)
1778
+ const [leftExpression, rightExpression] = alignBinaryPredicateExpressions(
1779
+ toDialectExpression(left),
1780
+ toDialectExpression(right)
1781
+ )
1701
1782
  return (makeExpression as any)({
1702
1783
  runtime: true as boolean,
1703
1784
  dbType: profile.boolDb as BoolDb,
1704
1785
  nullability,
1705
1786
  dialect: (leftExpression[Expression.TypeId].dialect ?? rightExpression[Expression.TypeId].dialect) as DialectOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | DialectOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1706
- aggregation: mergeAggregationRuntime(
1707
- leftExpression[Expression.TypeId].aggregation,
1708
- rightExpression[Expression.TypeId].aggregation
1709
- ) as MergeAggregation<AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>, AggregationOf<DialectAsExpression<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1710
- source: mergeSources(leftExpression[Expression.TypeId].source, rightExpression[Expression.TypeId].source) as SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | SourceOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1711
- sourceNullability,
1787
+ kind: "scalar",
1712
1788
  dependencies: mergeDependencies(
1713
1789
  leftExpression[Expression.TypeId].dependencies,
1714
1790
  rightExpression[Expression.TypeId].dependencies
1715
- ) as DependencyRecord<RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | RequiredFromDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1791
+ )
1716
1792
  } as any, {
1717
1793
  kind,
1718
1794
  left: leftExpression,
@@ -1725,18 +1801,21 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1725
1801
  kind: ExpressionAst.VariadicKind
1726
1802
  ): Expression.Any => {
1727
1803
  const expressions = values.map((value) => toDialectExpression(value as any)) as readonly Expression.Any[]
1804
+ const [head, ...tail] = expressions
1805
+ const alignedExpressions = (head !== undefined && (kind === "in" || kind === "notIn" || kind === "between"))
1806
+ ? [head, ...tail.map((value) => retargetLiteralExpression(value, head))]
1807
+ : expressions
1728
1808
  return makeExpression({
1729
1809
  runtime: true as boolean,
1730
1810
  dbType: profile.boolDb as BoolDb,
1731
1811
  nullability: "maybe",
1732
- dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as Dialect,
1733
- aggregation: mergeAggregationManyRuntime(expressions) as Expression.AggregationKind,
1734
- source: mergeManySources(expressions),
1735
- sourceNullability: "propagate" as const,
1736
- dependencies: mergeManyDependencies(expressions)
1812
+ dialect: (alignedExpressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as Dialect,
1813
+ kind: "scalar",
1814
+
1815
+ dependencies: mergeManyDependencies(alignedExpressions)
1737
1816
  }, {
1738
1817
  kind,
1739
- values: expressions
1818
+ values: alignedExpressions
1740
1819
  })
1741
1820
  }
1742
1821
 
@@ -1746,7 +1825,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1746
1825
  >(
1747
1826
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "eq">
1748
1827
  ): BinaryPredicateExpression<Left, Right, "eq"> => {
1749
- const [left, right] = args as [Left, Right]
1828
+ const [left, right] = args as unknown as [Left, Right]
1750
1829
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "eq")
1751
1830
  }
1752
1831
 
@@ -1756,7 +1835,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1756
1835
  >(
1757
1836
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "neq">
1758
1837
  ): BinaryPredicateExpression<Left, Right, "neq"> => {
1759
- const [left, right] = args as [Left, Right]
1838
+ const [left, right] = args as unknown as [Left, Right]
1760
1839
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "neq")
1761
1840
  }
1762
1841
 
@@ -1766,7 +1845,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1766
1845
  >(
1767
1846
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "lt">
1768
1847
  ): BinaryPredicateExpression<Left, Right, "lt"> => {
1769
- const [left, right] = args as [Left, Right]
1848
+ const [left, right] = args as unknown as [Left, Right]
1770
1849
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "lt")
1771
1850
  }
1772
1851
 
@@ -1776,7 +1855,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1776
1855
  >(
1777
1856
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "lte">
1778
1857
  ): BinaryPredicateExpression<Left, Right, "lte"> => {
1779
- const [left, right] = args as [Left, Right]
1858
+ const [left, right] = args as unknown as [Left, Right]
1780
1859
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "lte")
1781
1860
  }
1782
1861
 
@@ -1786,7 +1865,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1786
1865
  >(
1787
1866
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "gt">
1788
1867
  ): BinaryPredicateExpression<Left, Right, "gt"> => {
1789
- const [left, right] = args as [Left, Right]
1868
+ const [left, right] = args as unknown as [Left, Right]
1790
1869
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "gt")
1791
1870
  }
1792
1871
 
@@ -1796,7 +1875,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1796
1875
  >(
1797
1876
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "gte">
1798
1877
  ): BinaryPredicateExpression<Left, Right, "gte"> => {
1799
- const [left, right] = args as [Left, Right]
1878
+ const [left, right] = args as unknown as [Left, Right]
1800
1879
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "gte")
1801
1880
  }
1802
1881
 
@@ -1806,7 +1885,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1806
1885
  >(
1807
1886
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "like">
1808
1887
  ): BinaryPredicateExpression<Left, Right, "like"> => {
1809
- const [left, right] = args as [Left, Right]
1888
+ const [left, right] = args as unknown as [Left, Right]
1810
1889
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "like")
1811
1890
  }
1812
1891
 
@@ -1816,7 +1895,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1816
1895
  >(
1817
1896
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "ilike">
1818
1897
  ): BinaryPredicateExpression<Left, Right, "ilike"> => {
1819
- const [left, right] = args as [Left, Right]
1898
+ const [left, right] = args as unknown as [Left, Right]
1820
1899
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "ilike")
1821
1900
  }
1822
1901
 
@@ -1826,7 +1905,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1826
1905
  >(
1827
1906
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "regexMatch">
1828
1907
  ): BinaryPredicateExpression<Left, Right, "regexMatch"> => {
1829
- const [left, right] = args as [Left, Right]
1908
+ const [left, right] = args as unknown as [Left, Right]
1830
1909
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "regexMatch")
1831
1910
  }
1832
1911
 
@@ -1836,7 +1915,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1836
1915
  >(
1837
1916
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "regexIMatch">
1838
1917
  ): BinaryPredicateExpression<Left, Right, "regexIMatch"> => {
1839
- const [left, right] = args as [Left, Right]
1918
+ const [left, right] = args as unknown as [Left, Right]
1840
1919
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "regexIMatch")
1841
1920
  }
1842
1921
 
@@ -1846,7 +1925,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1846
1925
  >(
1847
1926
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "regexNotMatch">
1848
1927
  ): BinaryPredicateExpression<Left, Right, "regexNotMatch"> => {
1849
- const [left, right] = args as [Left, Right]
1928
+ const [left, right] = args as unknown as [Left, Right]
1850
1929
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "regexNotMatch")
1851
1930
  }
1852
1931
 
@@ -1856,7 +1935,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1856
1935
  >(
1857
1936
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "regexNotIMatch">
1858
1937
  ): BinaryPredicateExpression<Left, Right, "regexNotIMatch"> => {
1859
- const [left, right] = args as [Left, Right]
1938
+ const [left, right] = args as unknown as [Left, Right]
1860
1939
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "regexNotIMatch")
1861
1940
  }
1862
1941
 
@@ -1865,9 +1944,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1865
1944
  Right extends ExpressionInput
1866
1945
  >(
1867
1946
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "isDistinctFrom">
1868
- ): BinaryPredicateExpression<Left, Right, "isDistinctFrom", "never", "resolved"> => {
1869
- const [left, right] = args as [Left, Right]
1870
- return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "isDistinctFrom", "never", "resolved")
1947
+ ): BinaryPredicateExpression<Left, Right, "isDistinctFrom", "never"> => {
1948
+ const [left, right] = args as unknown as [Left, Right]
1949
+ return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "isDistinctFrom", "never")
1871
1950
  }
1872
1951
 
1873
1952
  const isNotDistinctFrom = <
@@ -1875,9 +1954,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1875
1954
  Right extends ExpressionInput
1876
1955
  >(
1877
1956
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "isNotDistinctFrom">
1878
- ): BinaryPredicateExpression<Left, Right, "isNotDistinctFrom", "never", "resolved"> => {
1879
- const [left, right] = args as [Left, Right]
1880
- return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "isNotDistinctFrom", "never", "resolved")
1957
+ ): BinaryPredicateExpression<Left, Right, "isNotDistinctFrom", "never"> => {
1958
+ const [left, right] = args as unknown as [Left, Right]
1959
+ return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "isNotDistinctFrom", "never")
1881
1960
  }
1882
1961
 
1883
1962
  const isNull = <Value extends ExpressionInput>(
@@ -1887,11 +1966,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1887
1966
  BoolDb,
1888
1967
  "never",
1889
1968
  DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1890
- AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1891
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1969
+ "scalar",
1892
1970
  DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1893
- ExpressionAst.UnaryNode<"isNull", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1894
- "resolved"
1971
+ ExpressionAst.UnaryNode<"isNull", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1895
1972
  > => {
1896
1973
  const expression = toDialectExpression(value)
1897
1974
  return makeExpression({
@@ -1899,10 +1976,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1899
1976
  dbType: profile.boolDb as BoolDb,
1900
1977
  nullability: "never",
1901
1978
  dialect: expression[Expression.TypeId].dialect,
1902
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1903
- source: expression[Expression.TypeId].source as SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1904
- sourceNullability: "resolved" as const,
1905
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1979
+ kind: "scalar",
1980
+
1981
+ dependencies: expression[Expression.TypeId].dependencies
1906
1982
  }, {
1907
1983
  kind: "isNull",
1908
1984
  value: expression
@@ -1916,11 +1992,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1916
1992
  BoolDb,
1917
1993
  "never",
1918
1994
  DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1919
- AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1920
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1995
+ "scalar",
1921
1996
  DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1922
- ExpressionAst.UnaryNode<"isNotNull", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1923
- "resolved"
1997
+ ExpressionAst.UnaryNode<"isNotNull", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1924
1998
  > => {
1925
1999
  const expression = toDialectExpression(value)
1926
2000
  return makeExpression({
@@ -1928,10 +2002,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1928
2002
  dbType: profile.boolDb as BoolDb,
1929
2003
  nullability: "never",
1930
2004
  dialect: expression[Expression.TypeId].dialect,
1931
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1932
- source: expression[Expression.TypeId].source as SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1933
- sourceNullability: "resolved" as const,
1934
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
2005
+ kind: "scalar",
2006
+
2007
+ dependencies: expression[Expression.TypeId].dependencies
1935
2008
  }, {
1936
2009
  kind: "isNotNull",
1937
2010
  value: expression
@@ -1945,8 +2018,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1945
2018
  TextDb,
1946
2019
  NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1947
2020
  DialectOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1948
- AggregationOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1949
- SourceOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
2021
+ KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1950
2022
  DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1951
2023
  ExpressionAst.UnaryNode<"upper", DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1952
2024
  > => {
@@ -1956,10 +2028,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1956
2028
  dbType: profile.textDb as TextDb,
1957
2029
  nullability: expression[Expression.TypeId].nullability as NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1958
2030
  dialect: expression[Expression.TypeId].dialect,
1959
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1960
- source: expression[Expression.TypeId].source as SourceOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1961
- sourceNullability: "propagate" as const,
1962
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
2031
+ kind: expression[Expression.TypeId].kind as KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
2032
+
2033
+ dependencies: expression[Expression.TypeId].dependencies
1963
2034
  }, {
1964
2035
  kind: "upper",
1965
2036
  value: expression
@@ -1973,8 +2044,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1973
2044
  TextDb,
1974
2045
  NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1975
2046
  DialectOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1976
- AggregationOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1977
- SourceOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
2047
+ KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1978
2048
  DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1979
2049
  ExpressionAst.UnaryNode<"lower", DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1980
2050
  > => {
@@ -1984,10 +2054,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1984
2054
  dbType: profile.textDb as TextDb,
1985
2055
  nullability: expression[Expression.TypeId].nullability as NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1986
2056
  dialect: expression[Expression.TypeId].dialect,
1987
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1988
- source: expression[Expression.TypeId].source as SourceOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1989
- sourceNullability: "propagate" as const,
1990
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
2057
+ kind: expression[Expression.TypeId].kind as KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
2058
+
2059
+ dependencies: expression[Expression.TypeId].dependencies
1991
2060
  }, {
1992
2061
  kind: "lower",
1993
2062
  value: expression
@@ -2015,11 +2084,11 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2015
2084
  runtime: undefined as unknown as RuntimeOfDbType<Target>,
2016
2085
  dbType: target as Target,
2017
2086
  runtimeSchema: undefined,
2087
+ driverValueMapping: (target as Expression.DbType.Any).driverValueMapping,
2018
2088
  nullability: expression[Expression.TypeId].nullability,
2019
2089
  dialect: expression[Expression.TypeId].dialect,
2020
- aggregation: expression[Expression.TypeId].aggregation,
2021
- source: expression[Expression.TypeId].source,
2022
- sourceNullability: expression[Expression.TypeId].sourceNullability,
2090
+ kind: expression[Expression.TypeId].kind,
2091
+
2023
2092
  dependencies: expression[Expression.TypeId].dependencies
2024
2093
  }, {
2025
2094
  kind: "cast",
@@ -2095,6 +2164,14 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2095
2164
  kind
2096
2165
  })
2097
2166
 
2167
+ const driverValueMapping = <Db extends Expression.DbType.Any>(
2168
+ dbType: Db,
2169
+ mapping: Expression.DriverValueMapping
2170
+ ): Db => ({
2171
+ ...dbType,
2172
+ driverValueMapping: mapping
2173
+ })
2174
+
2098
2175
  const type = {
2099
2176
  ...profile.type,
2100
2177
  array,
@@ -2104,7 +2181,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2104
2181
  domain,
2105
2182
  enum: enum_,
2106
2183
  set,
2107
- custom
2184
+ custom,
2185
+ driverValueMapping
2108
2186
  }
2109
2187
 
2110
2188
  const makeJsonDb = <Kind extends string>(
@@ -2143,15 +2221,13 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2143
2221
  Runtime,
2144
2222
  Db extends Expression.DbType.Any,
2145
2223
  Nullability extends Expression.Nullability,
2146
- Ast extends ExpressionAst.Any,
2147
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
2224
+ Ast extends ExpressionAst.Any
2148
2225
  >(
2149
2226
  expressions: readonly Expression.Any[],
2150
2227
  state: {
2151
2228
  readonly runtime: Runtime
2152
2229
  readonly dbType: Db
2153
2230
  readonly nullability: Nullability
2154
- readonly sourceNullability?: SourceNullability
2155
2231
  },
2156
2232
  ast: Ast
2157
2233
  ): AstBackedExpression<
@@ -2160,29 +2236,24 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2160
2236
  Nullability,
2161
2237
  TupleDialect<typeof expressions>,
2162
2238
  MergeAggregationTuple<typeof expressions>,
2163
- TupleSource<typeof expressions>,
2164
2239
  TupleDependencies<typeof expressions>,
2165
- Ast,
2166
- SourceNullability
2240
+ Ast
2167
2241
  > => makeExpression({
2168
2242
  runtime: state.runtime,
2169
2243
  dbType: state.dbType,
2170
2244
  nullability: state.nullability,
2171
2245
  dialect: (expressions.find((expression) => expression[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as TupleDialect<typeof expressions>,
2172
- aggregation: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<typeof expressions>,
2173
- source: mergeManySources(expressions) as TupleSource<typeof expressions>,
2174
- sourceNullability: (state.sourceNullability ?? "propagate") as SourceNullability,
2175
- dependencies: mergeManyDependencies(expressions) as TupleDependencies<typeof expressions>
2246
+ kind: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<typeof expressions>,
2247
+
2248
+ dependencies: mergeManyDependencies(expressions)
2176
2249
  }, ast) as AstBackedExpression<
2177
2250
  Runtime,
2178
2251
  Db,
2179
2252
  Nullability,
2180
2253
  TupleDialect<typeof expressions>,
2181
2254
  MergeAggregationTuple<typeof expressions>,
2182
- TupleSource<typeof expressions>,
2183
2255
  TupleDependencies<typeof expressions>,
2184
- Ast,
2185
- SourceNullability
2256
+ Ast
2186
2257
  >
2187
2258
 
2188
2259
  const jsonDbTypeOf = <Base extends JsonExpressionLike<any>>(
@@ -2204,9 +2275,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2204
2275
  dbType,
2205
2276
  nullability: (value === null ? "always" : "never") as JsonNullabilityOf<Value>,
2206
2277
  dialect: profile.dialect as Dialect,
2207
- aggregation: "scalar",
2208
- source: undefined as never,
2209
- sourceNullability: "resolved" as const,
2278
+ kind: "scalar",
2279
+
2210
2280
  dependencies: {}
2211
2281
  }, {
2212
2282
  kind: "literal",
@@ -2223,7 +2293,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2223
2293
  runtime: undefined as unknown as JsonRuntime<Expression.RuntimeOf<typeof value>>,
2224
2294
  dbType,
2225
2295
  nullability: value[Expression.TypeId].nullability as JsonNullabilityOf<JsonRuntime<Expression.RuntimeOf<typeof value>>>,
2226
- sourceNullability: value[Expression.TypeId].sourceNullability
2227
2296
  },
2228
2297
  {
2229
2298
  kind,
@@ -2259,8 +2328,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2259
2328
  JsonDbOfExpression<Base>,
2260
2329
  JsonNullabilityOf<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.get">>,
2261
2330
  DialectOf<Base>,
2262
- AggregationOf<Base>,
2263
- SourceOf<Base>,
2331
+ KindOf<Base>,
2264
2332
  DependenciesOf<Base>,
2265
2333
  JsonNode
2266
2334
  > => {
@@ -2285,8 +2353,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2285
2353
  JsonDbOfExpression<Base>,
2286
2354
  JsonNullabilityOf<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.get">>,
2287
2355
  DialectOf<Base>,
2288
- AggregationOf<Base>,
2289
- SourceOf<Base>,
2356
+ KindOf<Base>,
2290
2357
  DependenciesOf<Base>,
2291
2358
  JsonNode
2292
2359
  >
@@ -2304,8 +2371,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2304
2371
  TextDb,
2305
2372
  JsonNullabilityOf<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.text">>,
2306
2373
  DialectOf<Base>,
2307
- AggregationOf<Base>,
2308
- SourceOf<Base>,
2374
+ KindOf<Base>,
2309
2375
  DependenciesOf<Base>,
2310
2376
  JsonNode
2311
2377
  > => {
@@ -2332,8 +2398,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2332
2398
  TextDb,
2333
2399
  JsonNullabilityOf<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.text">>,
2334
2400
  DialectOf<Base>,
2335
- AggregationOf<Base>,
2336
- SourceOf<Base>,
2401
+ KindOf<Base>,
2337
2402
  DependenciesOf<Base>,
2338
2403
  JsonNode
2339
2404
  >
@@ -2461,7 +2526,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2461
2526
  runtime: true as boolean,
2462
2527
  dbType: profile.boolDb as BoolDb,
2463
2528
  nullability: "never" as const,
2464
- sourceNullability: "resolved" as const
2465
2529
  },
2466
2530
  {
2467
2531
  kind: "jsonHasKey",
@@ -2482,7 +2546,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2482
2546
  runtime: true as boolean,
2483
2547
  dbType: profile.boolDb as BoolDb,
2484
2548
  nullability: "never" as const,
2485
- sourceNullability: "resolved" as const
2486
2549
  },
2487
2550
  {
2488
2551
  kind: "jsonHasAnyKeys",
@@ -2503,7 +2566,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2503
2566
  runtime: true as boolean,
2504
2567
  dbType: profile.boolDb as BoolDb,
2505
2568
  nullability: "never" as const,
2506
- sourceNullability: "resolved" as const
2507
2569
  },
2508
2570
  {
2509
2571
  kind: "jsonHasAllKeys",
@@ -2523,8 +2585,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2523
2585
  JsonDbOfExpression<Base>,
2524
2586
  JsonNullabilityOf<JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.delete">>,
2525
2587
  DialectOf<Base>,
2526
- AggregationOf<Base>,
2527
- SourceOf<Base>,
2588
+ KindOf<Base>,
2528
2589
  DependenciesOf<Base>,
2529
2590
  JsonNode
2530
2591
  > => {
@@ -2546,8 +2607,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2546
2607
  JsonDbOfExpression<Base>,
2547
2608
  JsonNullabilityOf<JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.delete">>,
2548
2609
  DialectOf<Base>,
2549
- AggregationOf<Base>,
2550
- SourceOf<Base>,
2610
+ KindOf<Base>,
2551
2611
  DependenciesOf<Base>,
2552
2612
  JsonNode
2553
2613
  >
@@ -2564,8 +2624,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2564
2624
  JsonDbOfExpression<Base>,
2565
2625
  JsonNullabilityOf<JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.remove">>,
2566
2626
  DialectOf<Base>,
2567
- AggregationOf<Base>,
2568
- SourceOf<Base>,
2627
+ KindOf<Base>,
2569
2628
  DependenciesOf<Base>,
2570
2629
  JsonNode
2571
2630
  > => {
@@ -2587,8 +2646,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2587
2646
  JsonDbOfExpression<Base>,
2588
2647
  JsonNullabilityOf<JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.remove">>,
2589
2648
  DialectOf<Base>,
2590
- AggregationOf<Base>,
2591
- SourceOf<Base>,
2649
+ KindOf<Base>,
2592
2650
  DependenciesOf<Base>,
2593
2651
  JsonNode
2594
2652
  >
@@ -2610,8 +2668,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2610
2668
  JsonDbOfExpression<Base>,
2611
2669
  JsonNullabilityOf<JsonSetOutputOf<Expression.RuntimeOf<Base>, Target, Next, "json.set">>,
2612
2670
  DialectOf<Base>,
2613
- AggregationOf<Base>,
2614
- SourceOf<Base>,
2671
+ KindOf<Base>,
2615
2672
  DependenciesOf<Base>,
2616
2673
  JsonNode
2617
2674
  > => {
@@ -2636,8 +2693,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2636
2693
  JsonDbOfExpression<Base>,
2637
2694
  JsonNullabilityOf<JsonSetOutputOf<Expression.RuntimeOf<Base>, Target, Next, "json.set">>,
2638
2695
  DialectOf<Base>,
2639
- AggregationOf<Base>,
2640
- SourceOf<Base>,
2696
+ KindOf<Base>,
2641
2697
  DependenciesOf<Base>,
2642
2698
  JsonNode
2643
2699
  >
@@ -2660,8 +2716,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2660
2716
  JsonDbOfExpression<Base>,
2661
2717
  JsonNullabilityOf<JsonInsertOutputOf<Expression.RuntimeOf<Base>, Target, Next, InsertAfter, "json.insert">>,
2662
2718
  DialectOf<Base>,
2663
- AggregationOf<Base>,
2664
- SourceOf<Base>,
2719
+ KindOf<Base>,
2665
2720
  DependenciesOf<Base>,
2666
2721
  JsonNode
2667
2722
  > => {
@@ -2687,8 +2742,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2687
2742
  JsonDbOfExpression<Base>,
2688
2743
  JsonNullabilityOf<JsonInsertOutputOf<Expression.RuntimeOf<Base>, Target, Next, InsertAfter, "json.insert">>,
2689
2744
  DialectOf<Base>,
2690
- AggregationOf<Base>,
2691
- SourceOf<Base>,
2745
+ KindOf<Base>,
2692
2746
  DependenciesOf<Base>,
2693
2747
  JsonNode
2694
2748
  >
@@ -2709,9 +2763,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2709
2763
  Db,
2710
2764
  "maybe",
2711
2765
  Dialect,
2712
- Expression.AggregationKind,
2766
+ Expression.ScalarKind,
2713
2767
  never,
2714
- {},
2715
2768
  JsonNode
2716
2769
  > => {
2717
2770
  const leftExpression = toJsonValueExpression(left)
@@ -2733,9 +2786,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2733
2786
  Db,
2734
2787
  "maybe",
2735
2788
  Dialect,
2736
- Expression.AggregationKind,
2789
+ Expression.ScalarKind,
2737
2790
  never,
2738
- {},
2739
2791
  JsonNode
2740
2792
  >
2741
2793
  }
@@ -2783,7 +2835,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2783
2835
  runtime: true as boolean,
2784
2836
  dbType: profile.boolDb as BoolDb,
2785
2837
  nullability: "never" as const,
2786
- sourceNullability: "resolved" as const
2787
2838
  },
2788
2839
  {
2789
2840
  kind: "jsonKeyExists",
@@ -2811,7 +2862,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2811
2862
  runtime: {} as JsonObjectOutput<Shape>,
2812
2863
  dbType,
2813
2864
  nullability: "never" as const,
2814
- sourceNullability: "resolved" as const
2815
2865
  },
2816
2866
  {
2817
2867
  kind: "jsonBuildObject",
@@ -2836,7 +2886,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2836
2886
  runtime: [] as JsonArrayOutput<Values>,
2837
2887
  dbType,
2838
2888
  nullability: "never" as const,
2839
- sourceNullability: "resolved" as const
2840
2889
  },
2841
2890
  {
2842
2891
  kind: "jsonBuildArray",
@@ -2857,9 +2906,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2857
2906
  JsonDb<Dialect>,
2858
2907
  JsonNullabilityOf<JsonOutputOfInput<Value>>,
2859
2908
  string,
2860
- Expression.AggregationKind,
2861
- unknown,
2862
- Expression.SourceDependencies,
2909
+ Expression.ScalarKind,
2910
+ Expression.BindingId,
2863
2911
  JsonNode
2864
2912
  >
2865
2913
 
@@ -2870,9 +2918,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2870
2918
  JsonDb<Dialect, JsonbKindForDialect<Dialect>>,
2871
2919
  JsonNullabilityOf<JsonOutputOfInput<Value>>,
2872
2920
  string,
2873
- Expression.AggregationKind,
2874
- unknown,
2875
- Expression.SourceDependencies,
2921
+ Expression.ScalarKind,
2922
+ Expression.BindingId,
2876
2923
  JsonNode
2877
2924
  >
2878
2925
 
@@ -2932,7 +2979,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2932
2979
  runtime: true as boolean,
2933
2980
  dbType: profile.boolDb as BoolDb,
2934
2981
  nullability: "never" as const,
2935
- sourceNullability: "resolved" as const
2936
2982
  },
2937
2983
  {
2938
2984
  kind: "jsonPathExists",
@@ -2948,7 +2994,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2948
2994
  runtime: true as boolean,
2949
2995
  dbType: profile.boolDb as BoolDb,
2950
2996
  nullability: "never" as const,
2951
- sourceNullability: "resolved" as const
2952
2997
  },
2953
2998
  {
2954
2999
  kind: "jsonPathExists",
@@ -2984,7 +3029,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2984
3029
  runtime: true as boolean,
2985
3030
  dbType: profile.boolDb as BoolDb,
2986
3031
  nullability: "never" as const,
2987
- sourceNullability: "resolved" as const
2988
3032
  },
2989
3033
  {
2990
3034
  kind: "jsonPathMatch",
@@ -3000,7 +3044,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3000
3044
  runtime: true as boolean,
3001
3045
  dbType: profile.boolDb as BoolDb,
3002
3046
  nullability: "never" as const,
3003
- sourceNullability: "resolved" as const
3004
3047
  },
3005
3048
  {
3006
3049
  kind: "jsonPathMatch",
@@ -3084,12 +3127,12 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3084
3127
  }
3085
3128
 
3086
3129
  const and = <
3087
- Values extends readonly [ExpressionInput, ...ExpressionInput[]]
3130
+ const Values extends readonly [ExpressionInput, ...ExpressionInput[]]
3088
3131
  >(
3089
3132
  ...values: Values
3090
3133
  ): VariadicBooleanExpression<
3091
3134
  "and",
3092
- { readonly [K in keyof Values]: DialectAsExpression<Values[K], Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> } & readonly Expression.Any[],
3135
+ DialectExpressionArray<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3093
3136
  Dialect,
3094
3137
  TextDb,
3095
3138
  NumericDb,
@@ -3099,16 +3142,16 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3099
3142
  > =>
3100
3143
  makeVariadicBooleanExpression(
3101
3144
  "and",
3102
- values.map((value) => toDialectExpression(value)) as { readonly [K in keyof Values]: DialectAsExpression<Values[K], Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> } & readonly Expression.Any[]
3145
+ values.map((value) => toDialectExpression(value)) as unknown as DialectExpressionArray<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
3103
3146
  )
3104
3147
 
3105
3148
  const or = <
3106
- Values extends readonly [ExpressionInput, ...ExpressionInput[]]
3149
+ const Values extends readonly [ExpressionInput, ...ExpressionInput[]]
3107
3150
  >(
3108
3151
  ...values: Values
3109
3152
  ): VariadicBooleanExpression<
3110
3153
  "or",
3111
- { readonly [K in keyof Values]: DialectAsExpression<Values[K], Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> } & readonly Expression.Any[],
3154
+ DialectExpressionArray<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3112
3155
  Dialect,
3113
3156
  TextDb,
3114
3157
  NumericDb,
@@ -3118,7 +3161,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3118
3161
  > =>
3119
3162
  makeVariadicBooleanExpression(
3120
3163
  "or",
3121
- values.map((value) => toDialectExpression(value)) as { readonly [K in keyof Values]: DialectAsExpression<Values[K], Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> } & readonly Expression.Any[]
3164
+ values.map((value) => toDialectExpression(value)) as unknown as DialectExpressionArray<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
3122
3165
  )
3123
3166
 
3124
3167
  const not = <Value extends ExpressionInput>(
@@ -3128,8 +3171,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3128
3171
  BoolDb,
3129
3172
  Expression.NullabilityOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3130
3173
  DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3131
- AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3132
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3174
+ KindOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3133
3175
  DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3134
3176
  ExpressionAst.UnaryNode<"not", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3135
3177
  > => {
@@ -3139,10 +3181,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3139
3181
  dbType: profile.boolDb as BoolDb,
3140
3182
  nullability: expression[Expression.TypeId].nullability as Expression.NullabilityOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3141
3183
  dialect: expression[Expression.TypeId].dialect,
3142
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3143
- source: expression[Expression.TypeId].source as SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3144
- sourceNullability: "propagate" as const,
3145
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
3184
+ kind: expression[Expression.TypeId].kind as KindOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3185
+
3186
+ dependencies: expression[Expression.TypeId].dependencies
3146
3187
  }, {
3147
3188
  kind: "not",
3148
3189
  value: expression
@@ -3156,8 +3197,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3156
3197
  head: Head,
3157
3198
  ...tail: {
3158
3199
  readonly [K in keyof Tail]: Tail[K] & ComparableInput<
3159
- Head,
3160
- Tail[K],
3200
+ NoInfer<Head>,
3201
+ NoInfer<Tail[K]>,
3161
3202
  Dialect,
3162
3203
  TextDb,
3163
3204
  NumericDb,
@@ -3177,8 +3218,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3177
3218
  head: Head,
3178
3219
  ...tail: {
3179
3220
  readonly [K in keyof Tail]: Tail[K] & ComparableInput<
3180
- Head,
3181
- Tail[K],
3221
+ NoInfer<Head>,
3222
+ NoInfer<Tail[K]>,
3182
3223
  Dialect,
3183
3224
  TextDb,
3184
3225
  NumericDb,
@@ -3240,7 +3281,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3240
3281
  MergeNullabilityTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3241
3282
  TupleDialect<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3242
3283
  MergeAggregationTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3243
- TupleSource<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3244
3284
  TupleDependencies<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3245
3285
  ExpressionAst.VariadicNode<"concat", DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3246
3286
  > => {
@@ -3250,10 +3290,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3250
3290
  dbType: profile.textDb as TextDb,
3251
3291
  nullability: mergeNullabilityManyRuntime(expressions) as MergeNullabilityTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3252
3292
  dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as TupleDialect<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3253
- aggregation: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3254
- source: mergeManySources(expressions) as TupleSource<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3255
- sourceNullability: "propagate" as const,
3256
- dependencies: mergeManyDependencies(expressions) as TupleDependencies<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3293
+ kind: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3294
+
3295
+ dependencies: mergeManyDependencies(expressions)
3257
3296
  }, {
3258
3297
  kind: "concat",
3259
3298
  values: expressions
@@ -3263,7 +3302,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3263
3302
  MergeNullabilityTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3264
3303
  TupleDialect<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3265
3304
  MergeAggregationTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3266
- TupleSource<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3267
3305
  TupleDependencies<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3268
3306
  ExpressionAst.VariadicNode<"concat", DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3269
3307
  >
@@ -3289,10 +3327,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3289
3327
  "never",
3290
3328
  DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3291
3329
  "aggregate",
3292
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3293
3330
  DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3294
- ExpressionAst.UnaryNode<"count", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3295
- "resolved"
3331
+ ExpressionAst.UnaryNode<"count", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3296
3332
  > => {
3297
3333
  const expression = toDialectExpression(value)
3298
3334
  return makeExpression({
@@ -3300,10 +3336,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3300
3336
  dbType: profile.numericDb as NumericDb,
3301
3337
  nullability: "never",
3302
3338
  dialect: expression[Expression.TypeId].dialect,
3303
- aggregation: "aggregate",
3304
- source: expression[Expression.TypeId].source as SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3305
- sourceNullability: "resolved" as const,
3306
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
3339
+ kind: "aggregate",
3340
+
3341
+ dependencies: expression[Expression.TypeId].dependencies
3307
3342
  }, {
3308
3343
  kind: "count",
3309
3344
  value: expression
@@ -3320,10 +3355,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3320
3355
  "never",
3321
3356
  Dialect,
3322
3357
  "scalar",
3323
- never,
3324
3358
  DependencyRecord<OutstandingOfPlan<PlanValue>>,
3325
- ExpressionAst.ExistsNode<PlanValue>,
3326
- "resolved"
3359
+ ExpressionAst.ExistsNode<PlanValue>
3327
3360
  > => {
3328
3361
  const dependencies = Object.fromEntries(
3329
3362
  currentRequiredList(plan[Plan.TypeId].required).map((name) => [name, true] as const)
@@ -3333,9 +3366,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3333
3366
  dbType: profile.boolDb as BoolDb,
3334
3367
  nullability: "never",
3335
3368
  dialect: profile.dialect as Dialect,
3336
- aggregation: "scalar",
3337
- source: undefined as never,
3338
- sourceNullability: "resolved" as const,
3369
+ kind: "scalar",
3370
+
3339
3371
  dependencies
3340
3372
  }, {
3341
3373
  kind: "exists",
@@ -3353,10 +3385,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3353
3385
  "maybe",
3354
3386
  Dialect,
3355
3387
  "scalar",
3356
- never,
3357
3388
  DependencyRecord<OutstandingOfPlan<PlanValue>>,
3358
- ExpressionAst.ScalarSubqueryNode<PlanValue>,
3359
- "resolved"
3389
+ ExpressionAst.ScalarSubqueryNode<PlanValue>
3360
3390
  > => {
3361
3391
  const dependencies = Object.fromEntries(
3362
3392
  currentRequiredList(plan[Plan.TypeId].required).map((name) => [name, true] as const)
@@ -3365,11 +3395,12 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3365
3395
  return makeExpression({
3366
3396
  runtime: undefined as Expression.RuntimeOf<ScalarOutputOfPlan<PlanValue>> | null,
3367
3397
  dbType: expression[Expression.TypeId].dbType as Expression.DbTypeOf<ScalarOutputOfPlan<PlanValue>>,
3398
+ runtimeSchema: expression[Expression.TypeId].runtimeSchema,
3399
+ driverValueMapping: expression[Expression.TypeId].driverValueMapping,
3368
3400
  nullability: "maybe",
3369
3401
  dialect: profile.dialect as Dialect,
3370
- aggregation: "scalar",
3371
- source: undefined as never,
3372
- sourceNullability: "resolved" as const,
3402
+ kind: "scalar",
3403
+
3373
3404
  dependencies
3374
3405
  }, {
3375
3406
  kind: "scalarSubquery",
@@ -3407,8 +3438,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3407
3438
  )
3408
3439
  ): SubqueryPredicateExpression<
3409
3440
  Dialect,
3410
- AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3411
- SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3441
+ KindOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3412
3442
  DependencyRecord<RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | OutstandingOfPlan<PlanValue>>,
3413
3443
  ExpressionAst.InSubqueryNode<
3414
3444
  DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
@@ -3424,12 +3454,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3424
3454
  dbType: profile.boolDb as BoolDb,
3425
3455
  nullability: "maybe",
3426
3456
  dialect: (leftExpression[Expression.TypeId].dialect ?? profile.dialect) as Dialect,
3427
- aggregation: leftExpression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3428
- source: leftExpression[Expression.TypeId].source as SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3429
- sourceNullability: "propagate" as const,
3430
- dependencies: mergeDependencies(leftExpression[Expression.TypeId].dependencies, dependencies) as DependencyRecord<
3431
- RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | OutstandingOfPlan<PlanValue>
3432
- >
3457
+ kind: leftExpression[Expression.TypeId].kind as KindOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3458
+
3459
+ dependencies: mergeDependencies(leftExpression[Expression.TypeId].dependencies, dependencies)
3433
3460
  }, {
3434
3461
  kind: "inSubquery",
3435
3462
  left: leftExpression,
@@ -3457,12 +3484,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3457
3484
  dbType: profile.boolDb as BoolDb,
3458
3485
  nullability: "maybe",
3459
3486
  dialect: (leftExpression[Expression.TypeId].dialect ?? profile.dialect) as Dialect,
3460
- aggregation: leftExpression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3461
- source: leftExpression[Expression.TypeId].source as SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3462
- sourceNullability: "propagate" as const,
3463
- dependencies: mergeDependencies(leftExpression[Expression.TypeId].dependencies, dependencies) as DependencyRecord<
3464
- RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | OutstandingOfPlan<PlanValue>
3465
- >
3487
+ kind: leftExpression[Expression.TypeId].kind as KindOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3488
+
3489
+ dependencies: mergeDependencies(leftExpression[Expression.TypeId].dependencies, dependencies)
3466
3490
  }, renderQuantifiedComparisonAst(leftExpression, plan, operator, quantifier) as ExpressionAst.QuantifiedComparisonNode<
3467
3491
  Quantifier extends "any" ? "comparisonAny" : "comparisonAll",
3468
3492
  Operator,
@@ -3492,15 +3516,13 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3492
3516
  ): Expression.Any => quantifiedComparison(left, plan, operator, "all") as Expression.Any
3493
3517
 
3494
3518
  const over = <
3495
- Value extends Expression.Expression<
3519
+ Value extends Expression.Scalar<
3496
3520
  any,
3497
3521
  Expression.DbType.Any,
3498
3522
  Expression.Nullability,
3499
3523
  string,
3500
3524
  "aggregate",
3501
- any,
3502
- Expression.SourceDependencies,
3503
- Expression.SourceNullabilityMode
3525
+ Expression.BindingId
3504
3526
  >,
3505
3527
  PartitionBy extends readonly WindowPartitionInput[] = [],
3506
3528
  OrderBy extends readonly WindowOrderTermInput[] = []
@@ -3515,10 +3537,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3515
3537
  dbType: value[Expression.TypeId].dbType as Expression.DbTypeOf<Value>,
3516
3538
  nullability: value[Expression.TypeId].nullability as Expression.NullabilityOf<Value>,
3517
3539
  dialect: (expressions.find((expression) => expression[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as WindowDialectOf<Value, PartitionBy, OrderBy>,
3518
- aggregation: "window",
3519
- source: mergeManySources(expressions) as WindowSourceOf<Value, PartitionBy, OrderBy>,
3520
- sourceNullability: "resolved" as const,
3521
- dependencies: mergeManyDependencies(expressions) as WindowDependenciesOf<Value, PartitionBy, OrderBy>
3540
+ kind: "window",
3541
+
3542
+ dependencies: mergeManyDependencies(expressions)
3522
3543
  }, {
3523
3544
  kind: "window",
3524
3545
  function: "over",
@@ -3543,13 +3564,12 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3543
3564
  dbType: profile.numericDb as Expression.DbType.Any,
3544
3565
  nullability: "never",
3545
3566
  dialect: (expressions.find((expression) => expression[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as NumberWindowDialectOf<PartitionBy, OrderBy>,
3546
- aggregation: "window",
3547
- source: mergeManySources(expressions) as NumberWindowSourceOf<PartitionBy, OrderBy>,
3548
- sourceNullability: "resolved" as const,
3549
- dependencies: mergeManyDependencies(expressions) as NumberWindowDependenciesOf<PartitionBy, OrderBy>
3550
- }, {
3551
3567
  kind: "window",
3552
- function: kind,
3568
+
3569
+ dependencies: mergeManyDependencies(expressions)
3570
+ }, {
3571
+ kind: "window",
3572
+ function: kind,
3553
3573
  partitionBy: normalized.partitionBy,
3554
3574
  orderBy: normalized.orderBy
3555
3575
  })
@@ -3587,20 +3607,17 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3587
3607
  "maybe",
3588
3608
  DialectOf<Value>,
3589
3609
  "aggregate",
3590
- SourceOf<Value>,
3591
3610
  DependenciesOf<Value>,
3592
- ExpressionAst.UnaryNode<"max", Value>,
3593
- "resolved"
3611
+ ExpressionAst.UnaryNode<"max", Value>
3594
3612
  > =>
3595
3613
  makeExpression({
3596
3614
  runtime: undefined as Expression.RuntimeOf<Value>,
3597
3615
  dbType: value[Expression.TypeId].dbType as Expression.DbTypeOf<Value>,
3598
3616
  nullability: "maybe",
3599
3617
  dialect: value[Expression.TypeId].dialect,
3600
- aggregation: "aggregate",
3601
- source: value[Expression.TypeId].source as SourceOf<Value>,
3602
- sourceNullability: "resolved" as const,
3603
- dependencies: value[Expression.TypeId].dependencies as DependenciesOf<Value>
3618
+ kind: "aggregate",
3619
+
3620
+ dependencies: value[Expression.TypeId].dependencies
3604
3621
  }, {
3605
3622
  kind: "max",
3606
3623
  value
@@ -3614,20 +3631,17 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3614
3631
  "maybe",
3615
3632
  DialectOf<Value>,
3616
3633
  "aggregate",
3617
- SourceOf<Value>,
3618
3634
  DependenciesOf<Value>,
3619
- ExpressionAst.UnaryNode<"min", Value>,
3620
- "resolved"
3635
+ ExpressionAst.UnaryNode<"min", Value>
3621
3636
  > =>
3622
3637
  makeExpression({
3623
3638
  runtime: undefined as Expression.RuntimeOf<Value>,
3624
3639
  dbType: value[Expression.TypeId].dbType as Expression.DbTypeOf<Value>,
3625
3640
  nullability: "maybe",
3626
3641
  dialect: value[Expression.TypeId].dialect,
3627
- aggregation: "aggregate",
3628
- source: value[Expression.TypeId].source as SourceOf<Value>,
3629
- sourceNullability: "resolved" as const,
3630
- dependencies: value[Expression.TypeId].dependencies as DependenciesOf<Value>
3642
+ kind: "aggregate",
3643
+
3644
+ dependencies: value[Expression.TypeId].dependencies
3631
3645
  }, {
3632
3646
  kind: "min",
3633
3647
  value
@@ -3652,10 +3666,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3652
3666
  CoalesceNullabilityTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3653
3667
  TupleDialect<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3654
3668
  MergeAggregationTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3655
- TupleSource<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3656
3669
  TupleDependencies<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3657
- ExpressionAst.VariadicNode<"coalesce", DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3658
- "resolved"
3670
+ ExpressionAst.VariadicNode<"coalesce", DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3659
3671
  > => {
3660
3672
  const expressions = values.map((value) => toDialectExpression(value)) as readonly Expression.Any[]
3661
3673
  const representative = expressions.find((value) =>
@@ -3665,10 +3677,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3665
3677
  dbType: representative[Expression.TypeId].dbType as any,
3666
3678
  nullability: resolveCoalesceNullabilityRuntime(expressions) as any,
3667
3679
  dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as any,
3668
- aggregation: mergeAggregationManyRuntime(expressions) as any,
3669
- source: mergeManySources(expressions) as any,
3670
- sourceNullability: "resolved" as const,
3671
- dependencies: mergeManyDependencies(expressions) as any
3680
+ kind: mergeAggregationManyRuntime(expressions) as any,
3681
+
3682
+ dependencies: mergeManyDependencies(expressions)
3672
3683
  }, {
3673
3684
  kind: "coalesce",
3674
3685
  values: expressions
@@ -3678,10 +3689,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3678
3689
  CoalesceNullabilityTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3679
3690
  TupleDialect<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3680
3691
  MergeAggregationTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3681
- TupleSource<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3682
3692
  TupleDependencies<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3683
- ExpressionAst.VariadicNode<"coalesce", DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3684
- "resolved"
3693
+ ExpressionAst.VariadicNode<"coalesce", DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3685
3694
  >
3686
3695
  }
3687
3696
 
@@ -3698,9 +3707,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3698
3707
  dbType: profile.textDb,
3699
3708
  nullability: "maybe",
3700
3709
  dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as Dialect,
3701
- aggregation: mergeAggregationManyRuntime(expressions),
3702
- source: mergeManySources(expressions),
3703
- sourceNullability: "resolved" as const,
3710
+ kind: mergeAggregationManyRuntime(expressions),
3711
+
3704
3712
  dependencies: mergeManyDependencies(expressions)
3705
3713
  }, {
3706
3714
  kind: "function",
@@ -3709,25 +3717,22 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3709
3717
  }) as Expression.Any
3710
3718
  }
3711
3719
 
3712
- const uuidGenerateV4 = (): Expression.Expression<
3720
+ const uuidGenerateV4 = (): Expression.Scalar<
3713
3721
  string,
3714
- Expression.DbType.PgUuid,
3722
+ Expression.DbType.Base<"mysql", "uuid">,
3715
3723
  "never",
3716
3724
  Dialect,
3717
3725
  "scalar",
3718
- never,
3719
- {},
3720
- "resolved"
3726
+ never
3721
3727
  > & {
3722
3728
  readonly [ExpressionAst.TypeId]: ExpressionAst.FunctionCallNode<"uuid_generate_v4", readonly []>
3723
3729
  } => makeExpression({
3724
3730
  runtime: undefined as unknown as string,
3725
- dbType: { dialect: "postgres", kind: "uuid" } as Expression.DbType.PgUuid,
3731
+ dbType: mysqlDatatypes.uuid(),
3726
3732
  nullability: "never",
3727
3733
  dialect: profile.dialect as Dialect,
3728
- aggregation: "scalar",
3729
- source: undefined as never,
3730
- sourceNullability: "resolved" as const,
3734
+ kind: "scalar",
3735
+
3731
3736
  dependencies: {}
3732
3737
  }, {
3733
3738
  kind: "function",
@@ -3737,25 +3742,22 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3737
3742
 
3738
3743
  const nextVal = <Value extends ExpressionInput>(
3739
3744
  value: Value
3740
- ): Expression.Expression<
3741
- Expression.RuntimeOfDbType<Expression.DbType.PgInt8>,
3742
- Expression.DbType.PgInt8,
3745
+ ): Expression.Scalar<
3746
+ Expression.RuntimeOfDbType<Expression.DbType.Base<"mysql", "bigint">>,
3747
+ Expression.DbType.Base<"mysql", "bigint">,
3743
3748
  "never",
3744
3749
  Dialect,
3745
3750
  "scalar",
3746
- never,
3747
- {},
3748
- "resolved"
3751
+ never
3749
3752
  > & {
3750
3753
  readonly [ExpressionAst.TypeId]: ExpressionAst.FunctionCallNode<"nextval", readonly [Expression.Any]>
3751
3754
  } => makeExpression({
3752
- runtime: undefined as unknown as Expression.RuntimeOfDbType<Expression.DbType.PgInt8>,
3753
- dbType: { dialect: "postgres", kind: "int8" } as Expression.DbType.PgInt8,
3755
+ runtime: undefined as unknown as Expression.RuntimeOfDbType<Expression.DbType.Base<"mysql", "bigint">>,
3756
+ dbType: mysqlDatatypes.bigint(),
3754
3757
  nullability: "never",
3755
3758
  dialect: profile.dialect as Dialect,
3756
- aggregation: "scalar",
3757
- source: undefined as never,
3758
- sourceNullability: "resolved" as const,
3759
+ kind: "scalar",
3760
+
3759
3761
  dependencies: {}
3760
3762
  }, {
3761
3763
  kind: "function",
@@ -3805,9 +3807,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3805
3807
  dbType: representative[Expression.TypeId].dbType,
3806
3808
  nullability: resolveCaseNullabilityRuntime(resultExpressions),
3807
3809
  dialect: (allExpressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect),
3808
- aggregation: mergeAggregationManyRuntime(allExpressions),
3809
- source: mergeManySources(allExpressions),
3810
- sourceNullability: "resolved" as const,
3810
+ kind: mergeAggregationManyRuntime(allExpressions),
3811
+
3811
3812
  dependencies: mergeManyDependencies(allExpressions)
3812
3813
  }, {
3813
3814
  kind: "case",
@@ -3912,15 +3913,13 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3912
3913
  }
3913
3914
 
3914
3915
  const excluded = <
3915
- Value extends Expression.Expression<
3916
+ Value extends Expression.Scalar<
3916
3917
  any,
3917
3918
  Expression.DbType.Any,
3918
3919
  Expression.Nullability,
3919
3920
  string,
3920
3921
  "scalar",
3921
- any,
3922
- Expression.SourceDependencies,
3923
- Expression.SourceNullabilityMode
3922
+ Expression.BindingId
3924
3923
  >
3925
3924
  >(
3926
3925
  value: Value
@@ -3931,9 +3930,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3931
3930
  Dialect,
3932
3931
  "scalar",
3933
3932
  never,
3934
- {},
3935
- ExpressionAst.ExcludedNode<AstOf<Value> extends ExpressionAst.ColumnNode<any, infer ColumnName extends string> ? ColumnName : string>,
3936
- "resolved"
3933
+ ExpressionAst.ExcludedNode<AstOf<Value> extends ExpressionAst.ColumnNode<any, infer ColumnName extends string> ? ColumnName : string>
3937
3934
  > => {
3938
3935
  const ast = ((value as unknown) as Expression.Any & { readonly [ExpressionAst.TypeId]: ExpressionAst.Any })[ExpressionAst.TypeId]
3939
3936
  if (ast.kind !== "column") {
@@ -3943,11 +3940,11 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3943
3940
  runtime: undefined as Expression.RuntimeOf<Value>,
3944
3941
  dbType: value[Expression.TypeId].dbType as Expression.DbTypeOf<Value>,
3945
3942
  runtimeSchema: value[Expression.TypeId].runtimeSchema,
3943
+ driverValueMapping: value[Expression.TypeId].driverValueMapping,
3946
3944
  nullability: value[Expression.TypeId].nullability as Expression.NullabilityOf<Value>,
3947
3945
  dialect: profile.dialect as Dialect,
3948
- aggregation: "scalar",
3949
- source: undefined as never,
3950
- sourceNullability: "resolved" as const,
3946
+ kind: "scalar",
3947
+
3951
3948
  dependencies: {}
3952
3949
  }, {
3953
3950
  kind: "excluded",
@@ -3959,9 +3956,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3959
3956
  Dialect,
3960
3957
  "scalar",
3961
3958
  never,
3962
- {},
3963
- ExpressionAst.ExcludedNode<AstOf<Value> extends ExpressionAst.ColumnNode<any, infer ColumnName extends string> ? ColumnName : string>,
3964
- "resolved"
3959
+ ExpressionAst.ExcludedNode<AstOf<Value> extends ExpressionAst.ColumnNode<any, infer ColumnName extends string> ? ColumnName : string>
3965
3960
  >
3966
3961
  }
3967
3962
 
@@ -3970,16 +3965,17 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3970
3965
  column: Expression.Any
3971
3966
  ): Expression.Any => {
3972
3967
  if (value !== null && typeof value === "object" && Expression.TypeId in value) {
3973
- return value as unknown as Expression.Any
3968
+ return retargetLiteralExpression(value as unknown as Expression.Any, column)
3974
3969
  }
3975
3970
  return makeExpression({
3976
3971
  runtime: value as Value,
3977
3972
  dbType: column[Expression.TypeId].dbType,
3973
+ runtimeSchema: column[Expression.TypeId].runtimeSchema,
3974
+ driverValueMapping: column[Expression.TypeId].driverValueMapping,
3978
3975
  nullability: value === null ? "always" : "never",
3979
3976
  dialect: column[Expression.TypeId].dialect,
3980
- aggregation: "scalar",
3981
- source: undefined as never,
3982
- sourceNullability: "propagate" as const,
3977
+ kind: "scalar",
3978
+
3983
3979
  dependencies: {}
3984
3980
  }, {
3985
3981
  kind: "literal",
@@ -4050,15 +4046,10 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
4050
4046
  runtime: undefined as never,
4051
4047
  dbType: state.dbType,
4052
4048
  runtimeSchema: state.runtimeSchema,
4049
+ driverValueMapping: state.driverValueMapping,
4053
4050
  nullability: state.nullability,
4054
4051
  dialect: state.dialect,
4055
- aggregation: "scalar",
4056
- source: {
4057
- tableName: alias,
4058
- columnName,
4059
- baseTableName: alias
4060
- },
4061
- sourceNullability: "propagate" as const,
4052
+ kind: "scalar",
4062
4053
  dependencies: {
4063
4054
  [alias]: true
4064
4055
  } as Record<Alias, true>
@@ -4693,8 +4684,6 @@ type AstOf<Value extends Expression.Any> =
4693
4684
  ? Ast
4694
4685
  : never
4695
4686
 
4696
- type AvailableNames<Available extends Record<string, Plan.AnySource>> = Extract<keyof Available, string>
4697
-
4698
4687
  type RequiredFromInput<Value extends ExpressionInput> =
4699
4688
  Value extends Expression.Any
4700
4689
  ? RequiredFromDependencies<DependenciesOf<Value>>
@@ -4758,7 +4747,10 @@ type SelectFromResult<
4758
4747
  Exclude<OutstandingOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4759
4748
  AssumptionsOfPlan<PlanValue>,
4760
4749
  MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentSource>>,
4761
- StatementOfPlan<PlanValue>
4750
+ StatementOfPlan<PlanValue>,
4751
+ MutationTargetOfPlan<PlanValue>,
4752
+ InsertSourceStateOfPlan<PlanValue>,
4753
+ FactsOfPlan<PlanValue>
4762
4754
  >
4763
4755
 
4764
4756
  type UpdateFromResult<
@@ -4780,7 +4772,10 @@ type UpdateFromResult<
4780
4772
  Exclude<OutstandingOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4781
4773
  AssumptionsOfPlan<PlanValue>,
4782
4774
  MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentSource>>,
4783
- StatementOfPlan<PlanValue>
4775
+ StatementOfPlan<PlanValue>,
4776
+ MutationTargetOfPlan<PlanValue>,
4777
+ InsertSourceStateOfPlan<PlanValue>,
4778
+ FactsOfPlan<PlanValue>
4784
4779
  >
4785
4780
 
4786
4781
  type InsertFromResult<
@@ -4801,7 +4796,8 @@ type InsertFromResult<
4801
4796
  : CapabilitiesOfPlan<PlanValue>,
4802
4797
  StatementOfPlan<PlanValue>,
4803
4798
  MutationTargetOfPlan<PlanValue>,
4804
- "ready"
4799
+ "ready",
4800
+ FactsOfPlan<PlanValue>
4805
4801
  >
4806
4802
 
4807
4803
  type FromPlanConstraint<
@@ -4846,6 +4842,74 @@ type FromPlanResult<
4846
4842
  : never
4847
4843
  : never
4848
4844
 
4845
+ export type PublicStructuredFromConstraint<
4846
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
4847
+ CurrentSource extends AnyValuesSource | AnyUnnestSource | AnyTableFunctionSource,
4848
+ Dialect extends string
4849
+ > =
4850
+ StatementOfPlan<PlanValue> extends "insert"
4851
+ ? CurrentSource extends AnyValuesSource | AnyUnnestSource
4852
+ ? RequirePendingInsertStatement<PlanValue>
4853
+ : FromPlanConstraint<PlanValue, CurrentSource, Dialect>
4854
+ : FromPlanConstraint<PlanValue, CurrentSource, Dialect>
4855
+
4856
+ export type PublicStructuredFromResult<
4857
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
4858
+ CurrentSource extends AnyValuesSource | AnyUnnestSource | AnyTableFunctionSource,
4859
+ Dialect extends string
4860
+ > =
4861
+ StatementOfPlan<PlanValue> extends "select"
4862
+ ? QueryPlan<
4863
+ SelectionOfPlan<PlanValue>,
4864
+ Exclude<RequiredOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4865
+ AddAvailable<{}, SourceNameOf<CurrentSource>>,
4866
+ PlanDialectOf<PlanValue> | SourceDialectOf<CurrentSource>,
4867
+ GroupedOfPlan<PlanValue>,
4868
+ SourceNameOf<CurrentSource>,
4869
+ Exclude<OutstandingOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4870
+ AssumptionsOfPlan<PlanValue>,
4871
+ MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentSource>>,
4872
+ StatementOfPlan<PlanValue>
4873
+ >
4874
+ : StatementOfPlan<PlanValue> extends "update"
4875
+ ? QueryPlan<
4876
+ SelectionOfPlan<PlanValue>,
4877
+ Exclude<RequiredOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4878
+ AddAvailable<AvailableOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4879
+ PlanDialectOf<PlanValue> | SourceDialectOf<CurrentSource>,
4880
+ GroupedOfPlan<PlanValue>,
4881
+ ScopedNamesOfPlan<PlanValue> | SourceNameOf<CurrentSource>,
4882
+ Exclude<OutstandingOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4883
+ AssumptionsOfPlan<PlanValue>,
4884
+ MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentSource>>,
4885
+ StatementOfPlan<PlanValue>
4886
+ >
4887
+ : StatementOfPlan<PlanValue> extends "insert"
4888
+ ? CurrentSource extends AnyValuesSource | AnyUnnestSource
4889
+ ? QueryPlan<
4890
+ SelectionOfPlan<PlanValue>,
4891
+ never,
4892
+ AvailableOfPlan<PlanValue>,
4893
+ PlanDialectOf<PlanValue>,
4894
+ GroupedOfPlan<PlanValue>,
4895
+ ScopedNamesOfPlan<PlanValue>,
4896
+ never,
4897
+ AssumptionsOfPlan<PlanValue>,
4898
+ CapabilitiesOfPlan<PlanValue>,
4899
+ StatementOfPlan<PlanValue>,
4900
+ MutationTargetOfPlan<PlanValue>,
4901
+ "ready"
4902
+ >
4903
+ : FromPlanResult<PlanValue, CurrentSource, Dialect>
4904
+ : FromPlanResult<PlanValue, CurrentSource, Dialect>
4905
+
4906
+ export type PublicNonStructuredFromApi = <CurrentSource extends Exclude<FromInput, AnyValuesSource | AnyUnnestSource | AnyTableFunctionSource>>(
4907
+ source: CurrentSource
4908
+ ) =>
4909
+ <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
4910
+ plan: PlanValue & FromPlanConstraint<PlanValue, CurrentSource, Dialect>
4911
+ ) => FromPlanResult<PlanValue, CurrentSource, Dialect>
4912
+
4849
4913
  type MergeRequiredFromPredicate<
4850
4914
  Predicate extends PredicateInput | undefined,
4851
4915
  Available extends Record<string, Plan.AnySource>
@@ -4923,7 +4987,7 @@ type AsCurriedResult<
4923
4987
  if (typeof value !== "object" || value === null || Expression.TypeId in value) {
4924
4988
  const expression = toDialectExpression(value as ExpressionInput)
4925
4989
  const projected = Object.create(Object.getPrototypeOf(expression)) as {
4926
- [Expression.TypeId]: Expression.State<any, any, any, any, any, any, any, Expression.SourceNullabilityMode>
4990
+ [Expression.TypeId]: Expression.State<any, any, any, any, any, any>
4927
4991
  [ExpressionAst.TypeId]: ExpressionAst.Any
4928
4992
  [ProjectionAlias.TypeId]: ProjectionAlias.State<string>
4929
4993
  schema?: unknown
@@ -4959,14 +5023,14 @@ type AsCurriedResult<
4959
5023
  alias: Alias
4960
5024
  ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
4961
5025
  value: CompletePlan<PlanValue>
4962
- ) => import("./query.js").CteSource<PlanValue, Alias>
5026
+ ) => import("../../internal/query.js").CteSource<PlanValue, Alias>
4963
5027
  function with_<
4964
5028
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
4965
5029
  Alias extends string
4966
5030
  >(
4967
5031
  value: CompletePlan<PlanValue>,
4968
5032
  alias: Alias
4969
- ): import("./query.js").CteSource<PlanValue, Alias>
5033
+ ): import("../../internal/query.js").CteSource<PlanValue, Alias>
4970
5034
  function with_(valueOrAlias: unknown, alias?: string): unknown {
4971
5035
  if (alias === undefined) {
4972
5036
  return (value: unknown) => with_(value as any, valueOrAlias as string)
@@ -4983,14 +5047,14 @@ type AsCurriedResult<
4983
5047
  alias: Alias
4984
5048
  ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
4985
5049
  value: CompletePlan<PlanValue>
4986
- ) => import("./query.js").CteSource<PlanValue, Alias>
5050
+ ) => import("../../internal/query.js").CteSource<PlanValue, Alias>
4987
5051
  function withRecursive_<
4988
5052
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
4989
5053
  Alias extends string
4990
5054
  >(
4991
5055
  value: CompletePlan<PlanValue>,
4992
5056
  alias: Alias
4993
- ): import("./query.js").CteSource<PlanValue, Alias>
5057
+ ): import("../../internal/query.js").CteSource<PlanValue, Alias>
4994
5058
  function withRecursive_(valueOrAlias: unknown, alias?: string): unknown {
4995
5059
  if (alias === undefined) {
4996
5060
  return (value: unknown) => withRecursive_(value as any, valueOrAlias as string)
@@ -5008,14 +5072,14 @@ type AsCurriedResult<
5008
5072
  alias: Alias
5009
5073
  ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5010
5074
  value: PlanValue
5011
- ) => import("./query.js").LateralSource<PlanValue, Alias>
5075
+ ) => import("../../internal/query.js").LateralSource<PlanValue, Alias>
5012
5076
  function lateral<
5013
5077
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5014
5078
  Alias extends string
5015
5079
  >(
5016
5080
  value: PlanValue,
5017
5081
  alias: Alias
5018
- ): import("./query.js").LateralSource<PlanValue, Alias>
5082
+ ): import("../../internal/query.js").LateralSource<PlanValue, Alias>
5019
5083
  function lateral(valueOrAlias: unknown, alias?: string): unknown {
5020
5084
  if (alias === undefined) {
5021
5085
  return (value: unknown) => lateral(value as any, valueOrAlias as string)
@@ -5026,105 +5090,29 @@ type AsCurriedResult<
5026
5090
  )
5027
5091
  }
5028
5092
 
5029
- const values = <
5093
+ export type ValuesApi = <
5030
5094
  Rows extends ValuesRowsInput
5031
5095
  >(
5032
5096
  rows: Rows
5033
- ): ValuesInput<
5097
+ ) => ValuesInput<
5034
5098
  Rows,
5035
5099
  ValuesOutputShape<Rows[0], Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5036
5100
  Dialect
5037
- > => {
5038
- if (rows.length === 0) {
5039
- throw new Error("values(...) requires at least one row")
5040
- }
5041
- const normalizedRows: readonly [
5042
- Record<string, Expression.Any>,
5043
- ...Record<string, Expression.Any>[]
5044
- ] = rows.map((row) => normalizeValuesRow(row)) as any
5045
- const columnNames = Object.keys(normalizedRows[0]!)
5046
- for (const row of normalizedRows) {
5047
- const rowKeys = Object.keys(row)
5048
- if (rowKeys.length !== columnNames.length || !rowKeys.every((key, index) => key === columnNames[index])) {
5049
- throw new Error("values(...) rows must project the same columns in the same order")
5050
- }
5051
- }
5052
- return Object.assign(Object.create(ValuesInputProto), {
5053
- kind: "values",
5054
- dialect: profile.dialect,
5055
- rows: normalizedRows,
5056
- selection: normalizedRows[0]! as ValuesOutputShape<
5057
- Rows[0],
5058
- Dialect,
5059
- TextDb,
5060
- NumericDb,
5061
- BoolDb,
5062
- TimestampDb,
5063
- NullDb
5064
- >
5065
- }) as ValuesInput<
5066
- Rows,
5067
- ValuesOutputShape<Rows[0], Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5068
- Dialect
5069
- >
5070
- }
5101
+ >
5071
5102
 
5072
- const unnest = <
5103
+ export type UnnestApi = <
5073
5104
  Columns extends UnnestColumnsInput,
5074
5105
  Alias extends string
5075
5106
  >(
5076
5107
  columns: Columns,
5077
5108
  alias: Alias
5078
- ): UnnestSource<
5109
+ ) => UnnestSource<
5079
5110
  UnnestOutputShape<Columns, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5080
5111
  Alias,
5081
5112
  Dialect
5082
- > => {
5083
- const normalizedColumns = normalizeUnnestColumns(columns)
5084
- const columnNames = Object.keys(normalizedColumns)
5085
- if (columnNames.length === 0) {
5086
- throw new Error("unnest(...) requires at least one column array")
5087
- }
5088
- const firstColumn = normalizedColumns[columnNames[0] as keyof typeof normalizedColumns]
5089
- const rowCount = firstColumn?.length ?? 0
5090
- if (rowCount === 0) {
5091
- throw new Error("unnest(...) requires at least one row")
5092
- }
5093
- for (const columnName of columnNames) {
5094
- const values = normalizedColumns[columnName]!
5095
- if (values.length !== rowCount) {
5096
- throw new Error("unnest(...) column arrays must have the same length")
5097
- }
5098
- }
5099
- const firstRow = Object.fromEntries(
5100
- columnNames.map((columnName) => [columnName, normalizedColumns[columnName]![0]!])
5101
- ) as Record<string, Expression.Any>
5102
- const columnsSelection = makeColumnReferenceSelection(alias, firstRow) as any as UnnestOutputShape<
5103
- Columns,
5104
- Dialect,
5105
- TextDb,
5106
- NumericDb,
5107
- BoolDb,
5108
- TimestampDb,
5109
- NullDb
5110
- >
5111
- const source = {
5112
- kind: "unnest",
5113
- name: alias,
5114
- baseName: alias,
5115
- dialect: profile.dialect,
5116
- values: columns,
5117
- arrays: normalizedColumns,
5118
- columns: columnsSelection
5119
- }
5120
- return Object.assign(source, columnsSelection) as unknown as UnnestSource<
5121
- UnnestOutputShape<Columns, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5122
- Alias,
5123
- Dialect
5124
- >
5125
- }
5113
+ >
5126
5114
 
5127
- const generateSeries = <
5115
+ export type GenerateSeriesApi = <
5128
5116
  Start extends NumericExpressionInput,
5129
5117
  Stop extends NumericExpressionInput,
5130
5118
  Step extends NumericExpressionInput | undefined = undefined,
@@ -5133,54 +5121,19 @@ type AsCurriedResult<
5133
5121
  start: Start,
5134
5122
  stop: Stop,
5135
5123
  step?: Step,
5136
- alias: Alias = "series" as Alias
5137
- ): Dialect extends "postgres"
5124
+ alias?: Alias
5125
+ ) => Dialect extends "postgres"
5138
5126
  ? TableFunctionSource<
5139
5127
  GenerateSeriesOutputShape<Start, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5140
5128
  Alias,
5141
5129
  Dialect,
5142
5130
  "generate_series"
5143
5131
  >
5144
- : GenerateSeriesUnsupportedError<Dialect> => {
5145
- const startExpression = toDialectNumericExpression(start)
5146
- const stopExpression = toDialectNumericExpression(stop)
5147
- const stepExpression = step === undefined ? undefined : toDialectNumericExpression(step)
5148
- const valueSelection = {
5149
- value: startExpression
5150
- } as Record<string, Expression.Any>
5151
- const columns = makeColumnReferenceSelection(alias, valueSelection) as any as GenerateSeriesOutputShape<
5152
- Start,
5153
- Dialect,
5154
- TextDb,
5155
- NumericDb,
5156
- BoolDb,
5157
- TimestampDb,
5158
- NullDb
5159
- >
5160
- const source = {
5161
- kind: "tableFunction",
5162
- name: alias,
5163
- baseName: alias,
5164
- dialect: profile.dialect,
5165
- functionName: "generate_series",
5166
- args: stepExpression === undefined
5167
- ? [startExpression, stopExpression] as readonly Expression.Any[]
5168
- : [startExpression, stopExpression, stepExpression] as readonly Expression.Any[],
5169
- columns
5170
- }
5171
- return Object.assign(source, columns) as unknown as Dialect extends "postgres"
5172
- ? TableFunctionSource<
5173
- GenerateSeriesOutputShape<Start, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5174
- Alias,
5175
- Dialect,
5176
- "generate_series"
5177
- >
5178
- : GenerateSeriesUnsupportedError<Dialect>
5179
- }
5132
+ : GenerateSeriesUnsupportedError<Dialect>
5180
5133
 
5181
- const select = <Selection extends SelectionShape>(
5134
+ export type SelectApi = <Selection extends SelectionShape>(
5182
5135
  selection: Selection
5183
- ): QueryPlan<
5136
+ ) => QueryPlan<
5184
5137
  Selection,
5185
5138
  ExtractRequired<Selection>,
5186
5139
  {},
@@ -5190,95 +5143,45 @@ type AsCurriedResult<
5190
5143
  ExtractRequired<Selection>,
5191
5144
  TrueFormula,
5192
5145
  "read",
5193
- "select"
5194
- > =>
5195
- makePlan({
5196
- selection,
5197
- required: extractRequiredRuntime(selection) as ExtractRequired<Selection>,
5198
- available: {},
5199
- dialect: profile.dialect as ExtractDialect<Selection> extends never ? Dialect : ExtractDialect<Selection>
5200
- }, {
5201
- kind: "select",
5202
- select: selection,
5203
- where: [],
5204
- having: [],
5205
- joins: [],
5206
- groupBy: [],
5207
- orderBy: []
5208
- }, undefined as unknown as TrueFormula, "read", "select")
5209
-
5210
- const buildSetOperation = <
5211
- Operator extends QueryAst.SetOperatorKind,
5212
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5213
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5214
- >(
5215
- kind: Operator,
5216
- all: boolean,
5217
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5218
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5219
- ): QueryPlan<
5220
- SelectionOfPlan<LeftPlanValue>,
5221
- never,
5222
- {},
5223
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5224
- GroupedOfPlan<LeftPlanValue>,
5225
- never,
5226
- never,
5227
- TrueFormula,
5228
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5229
- "set"
5230
- > => {
5231
- const leftState = left[Plan.TypeId]
5232
- const leftAst = getAst(left)
5233
- const basePlan = leftAst.kind === "set"
5234
- ? leftAst.setBase ?? left
5235
- : left
5236
- const leftOperations = leftAst.kind === "set"
5237
- ? [...(leftAst.setOperations ?? [])]
5238
- : []
5239
- return makePlan({
5240
- selection: leftState.selection as SelectionOfPlan<LeftPlanValue>,
5241
- required: undefined as never,
5242
- available: {},
5243
- dialect: (leftState.dialect ?? right[Plan.TypeId].dialect) as PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>
5244
- }, {
5245
- kind: "set",
5246
- select: leftState.selection,
5247
- where: [],
5248
- having: [],
5249
- joins: [],
5250
- groupBy: [],
5251
- orderBy: [],
5252
- setBase: basePlan,
5253
- setOperations: [
5254
- ...leftOperations,
5255
- {
5256
- kind,
5257
- all,
5258
- query: right
5259
- }
5260
- ]
5261
- }, undefined as unknown as TrueFormula, undefined as unknown as CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>, "set")
5146
+ "select",
5147
+ any,
5148
+ "ready",
5149
+ EmptyFacts
5150
+ >
5151
+
5152
+ const {
5153
+ values,
5154
+ unnest,
5155
+ generateSeries,
5156
+ select,
5157
+ groupBy,
5158
+ returning
5159
+ } = makeDslQueryRuntime({
5160
+ profile,
5161
+ ValuesInputProto,
5162
+ normalizeValuesRow,
5163
+ normalizeUnnestColumns,
5164
+ makeColumnReferenceSelection,
5165
+ toDialectNumericExpression,
5166
+ extractRequiredRuntime,
5167
+ makePlan,
5168
+ getAst,
5169
+ getQueryState,
5170
+ currentRequiredList,
5171
+ dedupeGroupedExpressions
5172
+ }) as {
5173
+ readonly values: ValuesApi
5174
+ readonly unnest: UnnestApi
5175
+ readonly generateSeries: GenerateSeriesApi
5176
+ readonly select: SelectApi
5177
+ readonly groupBy: GroupByApi
5178
+ readonly returning: ReturningApi
5262
5179
  }
5263
5180
 
5264
- const union = <
5181
+ type SetOperationResult<
5265
5182
  LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5266
5183
  RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5267
- >(
5268
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5269
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5270
- ): QueryPlan<
5271
- SelectionOfPlan<LeftPlanValue>,
5272
- never,
5273
- {},
5274
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5275
- GroupedOfPlan<LeftPlanValue>,
5276
- never,
5277
- never,
5278
- TrueFormula,
5279
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5280
- "set"
5281
- > => buildSetOperation("union", false, left as never, right as never) as QueryPlan<
5184
+ > = QueryPlan<
5282
5185
  SelectionOfPlan<LeftPlanValue>,
5283
5186
  never,
5284
5187
  {},
@@ -5291,297 +5194,48 @@ type AsCurriedResult<
5291
5194
  "set"
5292
5195
  >
5293
5196
 
5294
- const unionAll = <
5197
+ type SetOperationApi = <
5295
5198
  LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5296
5199
  RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5297
5200
  >(
5298
5201
  left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5299
5202
  right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5300
- ): QueryPlan<
5301
- SelectionOfPlan<LeftPlanValue>,
5302
- never,
5303
- {},
5304
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5305
- GroupedOfPlan<LeftPlanValue>,
5306
- never,
5307
- never,
5308
- TrueFormula,
5309
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5310
- "set"
5311
- > => buildSetOperation("union", true, left as never, right as never) as QueryPlan<
5312
- SelectionOfPlan<LeftPlanValue>,
5313
- never,
5314
- {},
5315
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5316
- GroupedOfPlan<LeftPlanValue>,
5317
- never,
5318
- never,
5319
- TrueFormula,
5320
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5321
- "set"
5322
- >
5203
+ ) => SetOperationResult<LeftPlanValue, RightPlanValue>
5323
5204
 
5324
- const intersect = <
5325
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5326
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5327
- >(
5328
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5329
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5330
- ): QueryPlan<
5331
- SelectionOfPlan<LeftPlanValue>,
5332
- never,
5333
- {},
5334
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5335
- GroupedOfPlan<LeftPlanValue>,
5336
- never,
5337
- never,
5338
- TrueFormula,
5339
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5340
- "set"
5341
- > => buildSetOperation("intersect", false, left as never, right as never) as QueryPlan<
5342
- SelectionOfPlan<LeftPlanValue>,
5343
- never,
5344
- {},
5345
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5346
- GroupedOfPlan<LeftPlanValue>,
5347
- never,
5348
- never,
5349
- TrueFormula,
5350
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5351
- "set"
5352
- >
5205
+ type WhereApi = <Predicate extends PredicateInput>(
5206
+ predicate: Predicate
5207
+ ) =>
5208
+ <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5209
+ plan: PlanValue & RequireWhereStatement<PlanValue>
5210
+ ) => QueryPlan<
5211
+ SelectionOfPlan<PlanValue>,
5212
+ AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5213
+ AvailableOfPlan<PlanValue>,
5214
+ PlanDialectOf<PlanValue> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5215
+ GroupedOfPlan<PlanValue>,
5216
+ ScopedNamesOfPlan<PlanValue>,
5217
+ AddExpressionRequired<OutstandingOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5218
+ PlanAssumptionsAfterWhere<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5219
+ CapabilitiesOfPlan<PlanValue>,
5220
+ StatementOfPlan<PlanValue>,
5221
+ MutationTargetOfPlan<PlanValue>,
5222
+ InsertSourceStateOfPlan<PlanValue>,
5223
+ PlanFactsAfterWhere<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5224
+ >
5353
5225
 
5354
- const intersectAll = <
5355
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5356
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5357
- >(
5358
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5359
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5360
- ): QueryPlan<
5361
- SelectionOfPlan<LeftPlanValue>,
5362
- never,
5363
- {},
5364
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5365
- GroupedOfPlan<LeftPlanValue>,
5366
- never,
5367
- never,
5368
- TrueFormula,
5369
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5370
- "set"
5371
- > => buildSetOperation("intersect", true, left as never, right as never) as QueryPlan<
5372
- SelectionOfPlan<LeftPlanValue>,
5373
- never,
5374
- {},
5375
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5376
- GroupedOfPlan<LeftPlanValue>,
5377
- never,
5378
- never,
5379
- TrueFormula,
5380
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5381
- "set"
5382
- >
5226
+ export type FromApi = <CurrentSource extends FromInput>(
5227
+ source: CurrentSource
5228
+ ) =>
5229
+ <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5230
+ plan: PlanValue & FromPlanConstraint<PlanValue, CurrentSource, Dialect>
5231
+ ) => FromPlanResult<PlanValue, CurrentSource, Dialect>
5383
5232
 
5384
- const except = <
5385
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5386
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5387
- >(
5388
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5389
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5390
- ): QueryPlan<
5391
- SelectionOfPlan<LeftPlanValue>,
5392
- never,
5393
- {},
5394
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5395
- GroupedOfPlan<LeftPlanValue>,
5396
- never,
5397
- never,
5398
- TrueFormula,
5399
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5400
- "set"
5401
- > => buildSetOperation("except", false, left as never, right as never) as QueryPlan<
5402
- SelectionOfPlan<LeftPlanValue>,
5403
- never,
5404
- {},
5405
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5406
- GroupedOfPlan<LeftPlanValue>,
5407
- never,
5408
- never,
5409
- TrueFormula,
5410
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5411
- "set"
5412
- >
5413
-
5414
- const exceptAll = <
5415
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5416
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5417
- >(
5418
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5419
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5420
- ): QueryPlan<
5421
- SelectionOfPlan<LeftPlanValue>,
5422
- never,
5423
- {},
5424
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5425
- GroupedOfPlan<LeftPlanValue>,
5426
- never,
5427
- never,
5428
- TrueFormula,
5429
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5430
- "set"
5431
- > => buildSetOperation("except", true, left as never, right as never) as QueryPlan<
5432
- SelectionOfPlan<LeftPlanValue>,
5433
- never,
5434
- {},
5435
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5436
- GroupedOfPlan<LeftPlanValue>,
5437
- never,
5438
- never,
5439
- TrueFormula,
5440
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5441
- "set"
5442
- >
5443
-
5444
- const where = <Predicate extends PredicateInput>(
5445
- predicate: Predicate
5446
- ) =>
5447
- <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5448
- plan: PlanValue & RequireWhereStatement<PlanValue>
5449
- ): QueryPlan<
5450
- SelectionOfPlan<PlanValue>,
5451
- AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5452
- AvailableOfPlan<PlanValue>,
5453
- PlanDialectOf<PlanValue> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5454
- GroupedOfPlan<PlanValue>,
5455
- ScopedNamesOfPlan<PlanValue>,
5456
- AddExpressionRequired<OutstandingOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5457
- PlanAssumptionsAfterWhere<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5458
- CapabilitiesOfPlan<PlanValue>,
5459
- StatementOfPlan<PlanValue>
5460
- > => {
5461
- const current = plan[Plan.TypeId]
5462
- const currentAst = getAst(plan)
5463
- const currentQuery = getQueryState(plan)
5464
- const predicateExpression = toDialectExpression(predicate)
5465
- const predicateRequired = extractRequiredFromDialectInputRuntime(predicate)
5466
- return makePlan({
5467
- selection: current.selection,
5468
- required: [...currentRequiredList(current.required), ...predicateRequired].filter((name, index, values) =>
5469
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5470
- available: current.available,
5471
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5472
- }, {
5473
- ...currentAst,
5474
- where: [...currentAst.where, {
5475
- kind: "where",
5476
- predicate: predicateExpression
5477
- }]
5478
- },
5479
- assumeFormulaTrue(
5480
- currentQuery.assumptions,
5481
- formulaOfExpressionRuntime(predicateExpression)
5482
- ) as PlanAssumptionsAfterWhere<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5483
- currentQuery.capabilities,
5484
- currentQuery.statement as StatementOfPlan<PlanValue>)
5485
- }
5486
-
5487
- const from = <CurrentSource extends FromInput>(
5488
- source: CurrentSource
5489
- ) =>
5490
- <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5491
- plan: PlanValue & FromPlanConstraint<PlanValue, CurrentSource, Dialect>
5492
- ): FromPlanResult<PlanValue, CurrentSource, Dialect> => {
5493
- const current = plan[Plan.TypeId]
5494
- const currentAst = getAst(plan)
5495
- const currentQuery = getQueryState(plan)
5496
-
5497
- if (currentQuery.statement === "insert") {
5498
- return attachInsertSource(
5499
- plan as QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "missing">,
5500
- source as AnyValuesInput | AnyValuesSource | AnyUnnestSource | CompletePlan<QueryPlan<any, any, any, any, any, any, any, any, any, any>>
5501
- ) as FromPlanResult<PlanValue, CurrentSource, Dialect>
5502
- }
5503
-
5504
- if (
5505
- typeof source !== "object" ||
5506
- source === null ||
5507
- ("kind" in source && source.kind === "values" && !("name" in source)) ||
5508
- (!(Table.TypeId in source) && !("name" in source && "baseName" in source))
5509
- ) {
5510
- throw new Error("from(...) requires an aliased source in select/update statements")
5511
- }
5512
-
5513
- const sourceLike = source as SourceLike
5514
- const { sourceName, sourceBaseName } = sourceDetails(sourceLike)
5515
- const presenceWitnesses = presenceWitnessesOfSourceLike(sourceLike)
5516
-
5517
- if (currentQuery.statement === "select") {
5518
- const nextAst = {
5519
- ...currentAst,
5520
- from: {
5521
- kind: "from",
5522
- tableName: sourceName,
5523
- baseTableName: sourceBaseName,
5524
- source: sourceLike
5525
- }
5526
- } as QueryAst.Ast<Record<string, unknown>, any, "select">
5527
- return makePlan({
5528
- selection: current.selection,
5529
- required: currentRequiredList(current.required).filter((name) =>
5530
- name !== sourceName),
5531
- available: {
5532
- [sourceName]: {
5533
- name: sourceName,
5534
- mode: "required",
5535
- baseName: sourceBaseName,
5536
- _presentFormula: trueFormula(),
5537
- _presenceWitnesses: presenceWitnesses
5538
- }
5539
- } as AddAvailable<{}, string, "required", TrueFormula, PresenceWitnessKeysOfSource<Extract<CurrentSource, SourceLike>>>,
5540
- dialect: current.dialect
5541
- }, nextAst, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement) as FromPlanResult<PlanValue, CurrentSource, Dialect>
5542
- }
5543
-
5544
- if (currentQuery.statement === "update") {
5545
- const nextAvailable = {
5546
- ...current.available,
5547
- [sourceName]: {
5548
- name: sourceName,
5549
- mode: "required" as const,
5550
- baseName: sourceBaseName,
5551
- _presentFormula: trueFormula(),
5552
- _presenceWitnesses: presenceWitnesses
5553
- }
5554
- }
5555
- const nextAst = {
5556
- ...currentAst,
5557
- fromSources: [
5558
- ...(currentAst.fromSources ?? []),
5559
- {
5560
- kind: "from" as const,
5561
- tableName: sourceName,
5562
- baseTableName: sourceBaseName,
5563
- source: sourceLike
5564
- }
5565
- ]
5566
- } as QueryAst.Ast<Record<string, unknown>, any, "update">
5567
- return makePlan({
5568
- selection: current.selection,
5569
- required: currentRequiredList(current.required).filter((name) =>
5570
- !(name in nextAvailable)),
5571
- available: nextAvailable,
5572
- dialect: current.dialect
5573
- }, nextAst, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement) as FromPlanResult<PlanValue, CurrentSource, Dialect>
5574
- }
5575
-
5576
- throw new Error(`from(...) is not supported for ${currentQuery.statement} statements`)
5577
- }
5578
-
5579
- const having = <Predicate extends HavingPredicateInput>(
5233
+ type HavingApi = <Predicate extends HavingPredicateInput>(
5580
5234
  predicate: Predicate
5581
5235
  ) =>
5582
5236
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5583
5237
  plan: PlanValue & RequireSelectStatement<PlanValue>
5584
- ): QueryPlan<
5238
+ ) => QueryPlan<
5585
5239
  SelectionOfPlan<PlanValue>,
5586
5240
  AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5587
5241
  AvailableOfPlan<PlanValue>,
@@ -5591,58 +5245,13 @@ type AsCurriedResult<
5591
5245
  AddExpressionRequired<OutstandingOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5592
5246
  PlanAssumptionsAfterHaving<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5593
5247
  CapabilitiesOfPlan<PlanValue>,
5594
- StatementOfPlan<PlanValue>
5595
- > => {
5596
- const current = plan[Plan.TypeId]
5597
- const currentAst = getAst(plan)
5598
- const currentQuery = getQueryState(plan)
5599
- const predicateExpression = toDialectExpression(predicate)
5600
- const predicateRequired = extractRequiredFromDialectInputRuntime(predicate)
5601
- return makePlan({
5602
- selection: current.selection,
5603
- required: [...currentRequiredList(current.required), ...predicateRequired].filter((name, index, values) =>
5604
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5605
- available: current.available,
5606
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5607
- }, {
5608
- ...currentAst,
5609
- having: [...currentAst.having, {
5610
- kind: "having",
5611
- predicate: predicateExpression
5612
- }]
5613
- }, assumeFormulaTrue(
5614
- currentQuery.assumptions,
5615
- formulaOfExpressionRuntime(predicateExpression)
5616
- ) as PlanAssumptionsAfterHaving<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5617
- currentQuery.capabilities,
5618
- currentQuery.statement as StatementOfPlan<PlanValue>)
5619
- }
5620
-
5621
- const innerJoin = <CurrentTable extends SourceLike, Predicate extends PredicateInput>(
5622
- table: CurrentTable,
5623
- on: Predicate
5624
- ) =>
5625
- join("inner", table, on)
5626
-
5627
- const leftJoin = <CurrentTable extends SourceLike, Predicate extends PredicateInput>(
5628
- table: CurrentTable,
5629
- on: Predicate
5630
- ) =>
5631
- join("left", table, on)
5632
-
5633
- const rightJoin = <CurrentTable extends SourceLike, Predicate extends PredicateInput>(
5634
- table: CurrentTable,
5635
- on: Predicate
5636
- ) =>
5637
- join("right", table, on)
5638
-
5639
- const fullJoin = <CurrentTable extends SourceLike, Predicate extends PredicateInput>(
5640
- table: CurrentTable,
5641
- on: Predicate
5642
- ) =>
5643
- join("full", table, on)
5248
+ StatementOfPlan<PlanValue>,
5249
+ MutationTargetOfPlan<PlanValue>,
5250
+ InsertSourceStateOfPlan<PlanValue>,
5251
+ PlanFactsAfterHaving<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5252
+ >
5644
5253
 
5645
- const crossJoin = <CurrentTable extends SourceLike>(
5254
+ type CrossJoinApi = <CurrentTable extends SourceLike>(
5646
5255
  table: CurrentTable
5647
5256
  ) =>
5648
5257
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
@@ -5651,7 +5260,7 @@ type AsCurriedResult<
5651
5260
  ) & (
5652
5261
  SourceNameOf<CurrentTable> extends ScopedNamesOfPlan<PlanValue> ? never : unknown
5653
5262
  )
5654
- ): QueryPlan<
5263
+ ) => QueryPlan<
5655
5264
  SelectionOfPlan<PlanValue>,
5656
5265
  AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, never, "cross">,
5657
5266
  AddAvailable<
@@ -5668,49 +5277,9 @@ type AsCurriedResult<
5668
5277
  AssumptionsOfPlan<PlanValue>,
5669
5278
  MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>,
5670
5279
  StatementOfPlan<PlanValue>
5671
- > => {
5672
- const current = plan[Plan.TypeId]
5673
- const currentAst = getAst(plan)
5674
- const currentQuery = getQueryState(plan)
5675
- const { sourceName, sourceBaseName } = sourceDetails(table)
5676
- const presenceWitnesses = presenceWitnessesOfSourceLike(table)
5677
- const nextAvailable = Object.assign(
5678
- {},
5679
- current.available as AvailableOfPlan<PlanValue>,
5680
- {
5681
- [sourceName]: {
5682
- name: sourceName,
5683
- mode: "required",
5684
- baseName: sourceBaseName,
5685
- _presentFormula: trueFormula(),
5686
- _presenceWitnesses: presenceWitnesses
5687
- }
5688
- }
5689
- ) as AddAvailable<
5690
- AvailableOfPlan<PlanValue>,
5691
- SourceNameOf<CurrentTable>,
5692
- "required",
5693
- TrueFormula,
5694
- PresenceWitnessKeysOfSource<CurrentTable>
5695
- >
5696
- return makePlan({
5697
- selection: current.selection,
5698
- required: currentRequiredList(current.required).filter((name) =>
5699
- !(name in nextAvailable)) as AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, never, "cross">,
5700
- available: nextAvailable,
5701
- dialect: current.dialect as PlanDialectOf<PlanValue> | SourceDialectOf<CurrentTable>
5702
- }, {
5703
- ...currentAst,
5704
- joins: [...currentAst.joins, {
5705
- kind: "cross",
5706
- tableName: sourceName,
5707
- baseTableName: sourceBaseName,
5708
- source: table
5709
- }]
5710
- }, currentQuery.assumptions, currentQuery.capabilities as MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>, currentQuery.statement as StatementOfPlan<PlanValue>)
5711
- }
5280
+ >
5712
5281
 
5713
- const join = <
5282
+ type JoinApi = <
5714
5283
  Kind extends QueryAst.JoinKind,
5715
5284
  CurrentTable extends SourceLike,
5716
5285
  Predicate extends PredicateInput
@@ -5725,7 +5294,7 @@ type AsCurriedResult<
5725
5294
  ) & (
5726
5295
  SourceNameOf<CurrentTable> extends ScopedNamesOfPlan<PlanValue> ? never : unknown
5727
5296
  )
5728
- ): QueryPlan<
5297
+ ) => QueryPlan<
5729
5298
  SelectionOfPlan<PlanValue>,
5730
5299
  AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, Predicate, Kind>,
5731
5300
  AvailableAfterJoin<
@@ -5741,73 +5310,54 @@ type AsCurriedResult<
5741
5310
  AddJoinRequired<OutstandingOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, Predicate, Kind>,
5742
5311
  PlanAssumptionsAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5743
5312
  MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>,
5744
- StatementOfPlan<PlanValue>
5745
- > => {
5746
- const current = plan[Plan.TypeId]
5747
- const currentAst = getAst(plan)
5748
- const currentQuery = getQueryState(plan)
5749
- const onExpression = toDialectExpression(on)
5750
- const onFormula = formulaOfExpressionRuntime(onExpression)
5751
- const { sourceName, sourceBaseName } = sourceDetails(table)
5752
- const presenceWitnesses = presenceWitnessesOfSourceLike(table)
5753
- const baseAvailable = (kind === "right" || kind === "full"
5754
- ? Object.fromEntries(
5755
- Object.entries(current.available as Record<string, Plan.AnySource>).map(([name, source]) => [name, {
5756
- name: source.name,
5757
- mode: "optional" as const,
5758
- baseName: source.baseName,
5759
- _presentFormula: source._presentFormula,
5760
- _presenceWitnesses: source._presenceWitnesses
5761
- }])
5762
- )
5763
- : current.available) as AvailableOfPlan<PlanValue>
5764
- const nextAvailable = {
5765
- ...baseAvailable,
5766
- [sourceName]: {
5767
- name: sourceName,
5768
- mode: (kind === "left" || kind === "full" ? "optional" : "required") as JoinSourceMode<Kind>,
5769
- baseName: sourceBaseName,
5770
- _presentFormula: (kind === "inner" || kind === "left") ? onFormula : trueFormula(),
5771
- _presenceWitnesses: presenceWitnesses
5772
- }
5773
- } as AvailableAfterJoin<
5313
+ StatementOfPlan<PlanValue>,
5314
+ MutationTargetOfPlan<PlanValue>,
5315
+ InsertSourceStateOfPlan<PlanValue>,
5316
+ PlanFactsAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5317
+ >
5318
+
5319
+ type BinaryJoinApi<Kind extends QueryAst.JoinKind> = <
5320
+ CurrentTable extends SourceLike,
5321
+ Predicate extends PredicateInput
5322
+ >(
5323
+ table: CurrentTable,
5324
+ on: Predicate
5325
+ ) =>
5326
+ <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5327
+ plan: PlanValue & RequireJoinStatement<PlanValue> & (
5328
+ keyof AvailableOfPlan<PlanValue> extends never ? never : unknown
5329
+ ) & (
5330
+ SourceNameOf<CurrentTable> extends ScopedNamesOfPlan<PlanValue> ? never : unknown
5331
+ )
5332
+ ) => QueryPlan<
5333
+ SelectionOfPlan<PlanValue>,
5334
+ AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, Predicate, Kind>,
5335
+ AvailableAfterJoin<
5774
5336
  AvailableOfPlan<PlanValue>,
5775
5337
  SourceNameOf<CurrentTable>,
5776
5338
  Kind,
5777
5339
  JoinPresenceFormula<Kind, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5778
5340
  PresenceWitnessKeysOfSource<CurrentTable>
5779
- >
5780
- return makePlan({
5781
- selection: current.selection,
5782
- required: [...currentRequiredList(current.required), ...extractRequiredFromDialectInputRuntime(on)].filter((name, index, values) =>
5783
- !(name in nextAvailable) && values.indexOf(name) === index) as AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, Predicate, Kind>,
5784
- available: nextAvailable,
5785
- dialect: current.dialect as PlanDialectOf<PlanValue> | SourceDialectOf<CurrentTable> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5786
- }, {
5787
- ...currentAst,
5788
- joins: [...currentAst.joins, {
5789
- kind,
5790
- tableName: sourceName,
5791
- baseTableName: sourceBaseName,
5792
- source: table,
5793
- on: onExpression
5794
- }]
5795
- }, (
5796
- kind === "inner"
5797
- ? assumeFormulaTrue(currentQuery.assumptions, onFormula)
5798
- : currentQuery.assumptions
5799
- ) as PlanAssumptionsAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5800
- currentQuery.capabilities as MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>,
5801
- currentQuery.statement as StatementOfPlan<PlanValue>)
5802
- }
5341
+ >,
5342
+ PlanDialectOf<PlanValue> | SourceDialectOf<CurrentTable> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5343
+ GroupedOfPlan<PlanValue>,
5344
+ ScopedNamesOfPlan<PlanValue> | SourceNameOf<CurrentTable>,
5345
+ AddJoinRequired<OutstandingOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, Predicate, Kind>,
5346
+ PlanAssumptionsAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5347
+ MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>,
5348
+ StatementOfPlan<PlanValue>,
5349
+ MutationTargetOfPlan<PlanValue>,
5350
+ InsertSourceStateOfPlan<PlanValue>,
5351
+ PlanFactsAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5352
+ >
5803
5353
 
5804
- const orderBy = <Value extends ExpressionInput>(
5354
+ type OrderByApi = <Value extends ExpressionInput>(
5805
5355
  value: Value,
5806
- direction: OrderDirection = "asc"
5356
+ direction?: OrderDirection
5807
5357
  ) =>
5808
5358
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5809
5359
  plan: PlanValue & MutationOrderLimitSupported<PlanValue, Dialect>
5810
- ): QueryPlan<
5360
+ ) => QueryPlan<
5811
5361
  SelectionOfPlan<PlanValue>,
5812
5362
  AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Value>,
5813
5363
  AvailableOfPlan<PlanValue>,
@@ -5817,80 +5367,16 @@ type AsCurriedResult<
5817
5367
  AddExpressionRequired<OutstandingOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Value>,
5818
5368
  AssumptionsOfPlan<PlanValue>,
5819
5369
  CapabilitiesOfPlan<PlanValue>,
5820
- StatementOfPlan<PlanValue>
5821
- > => {
5822
- const current = plan[Plan.TypeId]
5823
- const currentAst = getAst(plan)
5824
- const currentQuery = getQueryState(plan)
5825
- const expression = toDialectExpression(value)
5826
- const required = extractRequiredFromDialectInputRuntime(value)
5827
- return makePlan({
5828
- selection: current.selection,
5829
- required: [...currentRequiredList(current.required), ...required].filter((name, index, values) =>
5830
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Value>,
5831
- available: current.available,
5832
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5833
- }, {
5834
- ...currentAst,
5835
- orderBy: [...currentAst.orderBy, {
5836
- kind: "orderBy",
5837
- value: expression,
5838
- direction
5839
- }]
5840
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
5841
- }
5370
+ StatementOfPlan<PlanValue>,
5371
+ MutationTargetOfPlan<PlanValue>,
5372
+ InsertSourceStateOfPlan<PlanValue>,
5373
+ FactsOfPlan<PlanValue>
5374
+ >
5842
5375
 
5843
- function lock(
5844
- mode: "update" | "share",
5845
- options?: LockOptions
5846
- ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5847
- plan: PlanValue & (StatementOfPlan<PlanValue> extends "select" ? unknown : never)
5848
- ) => QueryPlan<
5849
- SelectionOfPlan<PlanValue>,
5850
- RequiredOfPlan<PlanValue>,
5851
- AvailableOfPlan<PlanValue>,
5852
- PlanDialectOf<PlanValue>,
5853
- GroupedOfPlan<PlanValue>,
5854
- ScopedNamesOfPlan<PlanValue>,
5855
- OutstandingOfPlan<PlanValue>,
5856
- AssumptionsOfPlan<PlanValue>,
5857
- MergeCapabilities<CapabilitiesOfPlan<PlanValue>, "transaction">,
5858
- StatementOfPlan<PlanValue>
5859
- >
5860
- function lock<
5861
- Mode extends Dialect extends "mysql" ? "lowPriority" | "ignore" | "quick" : never
5862
- >(
5863
- mode: Mode,
5864
- options?: LockOptions
5865
- ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5866
- plan: PlanValue & (
5867
- Dialect extends "mysql"
5868
- ? StatementOfPlan<PlanValue> extends "update"
5869
- ? Mode extends MutationLockModeForStatement<"update", Dialect> ? unknown : never
5870
- : StatementOfPlan<PlanValue> extends "delete"
5871
- ? Mode extends MutationLockModeForStatement<"delete", Dialect> ? unknown : never
5872
- : never
5873
- : never
5874
- )
5875
- ) => QueryPlan<
5876
- SelectionOfPlan<PlanValue>,
5877
- RequiredOfPlan<PlanValue>,
5878
- AvailableOfPlan<PlanValue>,
5879
- PlanDialectOf<PlanValue>,
5880
- GroupedOfPlan<PlanValue>,
5881
- ScopedNamesOfPlan<PlanValue>,
5882
- OutstandingOfPlan<PlanValue>,
5883
- AssumptionsOfPlan<PlanValue>,
5884
- MergeCapabilities<CapabilitiesOfPlan<PlanValue>, "transaction">,
5885
- StatementOfPlan<PlanValue>
5886
- >
5887
- function lock(
5888
- mode: QueryAst.LockClause["mode"],
5889
- options: LockOptions = {}
5890
- ) {
5891
- return <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5892
- plan: PlanValue
5893
- ): QueryPlan<
5376
+ interface LockApi {
5377
+ (mode: "update" | "share", options?: LockOptions): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5378
+ plan: PlanValue & (StatementOfPlan<PlanValue> extends "select" ? unknown : never)
5379
+ ) => QueryPlan<
5894
5380
  SelectionOfPlan<PlanValue>,
5895
5381
  RequiredOfPlan<PlanValue>,
5896
5382
  AvailableOfPlan<PlanValue>,
@@ -5900,32 +5386,45 @@ type AsCurriedResult<
5900
5386
  OutstandingOfPlan<PlanValue>,
5901
5387
  AssumptionsOfPlan<PlanValue>,
5902
5388
  MergeCapabilities<CapabilitiesOfPlan<PlanValue>, "transaction">,
5903
- StatementOfPlan<PlanValue>
5904
- > => {
5905
- const current = plan[Plan.TypeId]
5906
- const currentAst = getAst(plan)
5907
- const currentQuery = getQueryState(plan)
5908
- return makePlan({
5909
- selection: current.selection,
5910
- required: current.required as RequiredOfPlan<PlanValue>,
5911
- available: current.available,
5912
- dialect: current.dialect as PlanDialectOf<PlanValue>
5913
- }, {
5914
- ...currentAst,
5915
- lock: {
5916
- kind: "lock",
5917
- mode,
5918
- nowait: options.nowait ?? false,
5919
- skipLocked: options.skipLocked ?? false
5920
- }
5921
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
5922
- }
5389
+ StatementOfPlan<PlanValue>,
5390
+ MutationTargetOfPlan<PlanValue>,
5391
+ InsertSourceStateOfPlan<PlanValue>,
5392
+ FactsOfPlan<PlanValue>
5393
+ >
5394
+ <Mode extends Dialect extends "mysql" ? "lowPriority" | "ignore" | "quick" : never>(
5395
+ mode: Mode,
5396
+ options?: LockOptions
5397
+ ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5398
+ plan: PlanValue & (
5399
+ Dialect extends "mysql"
5400
+ ? StatementOfPlan<PlanValue> extends "update"
5401
+ ? Mode extends MutationLockModeForStatement<"update", Dialect> ? unknown : never
5402
+ : StatementOfPlan<PlanValue> extends "delete"
5403
+ ? Mode extends MutationLockModeForStatement<"delete", Dialect> ? unknown : never
5404
+ : never
5405
+ : never
5406
+ )
5407
+ ) => QueryPlan<
5408
+ SelectionOfPlan<PlanValue>,
5409
+ RequiredOfPlan<PlanValue>,
5410
+ AvailableOfPlan<PlanValue>,
5411
+ PlanDialectOf<PlanValue>,
5412
+ GroupedOfPlan<PlanValue>,
5413
+ ScopedNamesOfPlan<PlanValue>,
5414
+ OutstandingOfPlan<PlanValue>,
5415
+ AssumptionsOfPlan<PlanValue>,
5416
+ MergeCapabilities<CapabilitiesOfPlan<PlanValue>, "transaction">,
5417
+ StatementOfPlan<PlanValue>,
5418
+ MutationTargetOfPlan<PlanValue>,
5419
+ InsertSourceStateOfPlan<PlanValue>,
5420
+ FactsOfPlan<PlanValue>
5421
+ >
5923
5422
  }
5924
5423
 
5925
- const distinct = () =>
5424
+ type DistinctApi = () =>
5926
5425
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5927
5426
  plan: PlanValue & RequireSelectStatement<PlanValue>
5928
- ): QueryPlan<
5427
+ ) => QueryPlan<
5929
5428
  SelectionOfPlan<PlanValue>,
5930
5429
  RequiredOfPlan<PlanValue>,
5931
5430
  AvailableOfPlan<PlanValue>,
@@ -5935,34 +5434,18 @@ type AsCurriedResult<
5935
5434
  OutstandingOfPlan<PlanValue>,
5936
5435
  AssumptionsOfPlan<PlanValue>,
5937
5436
  CapabilitiesOfPlan<PlanValue>,
5938
- StatementOfPlan<PlanValue>
5939
- > => {
5940
- const current = plan[Plan.TypeId]
5941
- const currentAst = getAst(plan)
5942
- const currentQuery = getQueryState(plan)
5943
- return makePlan({
5944
- selection: current.selection,
5945
- required: current.required as RequiredOfPlan<PlanValue>,
5946
- available: current.available,
5947
- dialect: current.dialect as PlanDialectOf<PlanValue>
5948
- }, {
5949
- ...currentAst,
5950
- distinct: true
5951
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
5952
- }
5953
-
5954
- const distinctOn = {
5955
- __effect_qb_error__: "effect-qb: distinctOn(...) is only supported by the postgres dialect",
5956
- __effect_qb_dialect__: profile.dialect,
5957
- __effect_qb_hint__: "Use postgres.Query.distinctOn(...) or regular distinct()/grouping logic"
5958
- } as DistinctOnApi<Dialect>
5437
+ StatementOfPlan<PlanValue>,
5438
+ MutationTargetOfPlan<PlanValue>,
5439
+ InsertSourceStateOfPlan<PlanValue>,
5440
+ FactsOfPlan<PlanValue>
5441
+ >
5959
5442
 
5960
- const limit = <Value extends NumericExpressionInput>(
5443
+ type LimitApi = <Value extends NumericExpressionInput>(
5961
5444
  value: Value
5962
5445
  ) =>
5963
5446
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5964
5447
  plan: PlanValue & MutationOrderLimitSupported<PlanValue, Dialect>
5965
- ): QueryPlan<
5448
+ ) => QueryPlan<
5966
5449
  SelectionOfPlan<PlanValue>,
5967
5450
  AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
5968
5451
  AvailableOfPlan<PlanValue>,
@@ -5972,31 +5455,18 @@ type AsCurriedResult<
5972
5455
  AddExpressionRequired<OutstandingOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
5973
5456
  AssumptionsOfPlan<PlanValue>,
5974
5457
  CapabilitiesOfPlan<PlanValue>,
5975
- StatementOfPlan<PlanValue>
5976
- > => {
5977
- const current = plan[Plan.TypeId]
5978
- const currentAst = getAst(plan)
5979
- const currentQuery = getQueryState(plan)
5980
- const expression = toDialectNumericExpression(value)
5981
- const required = extractRequiredFromDialectNumericInputRuntime(value)
5982
- return makePlan({
5983
- selection: current.selection,
5984
- required: [...currentRequiredList(current.required), ...required].filter((name, index, values) =>
5985
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
5986
- available: current.available,
5987
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectNumericInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5988
- }, {
5989
- ...currentAst,
5990
- limit: expression
5991
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
5992
- }
5458
+ StatementOfPlan<PlanValue>,
5459
+ MutationTargetOfPlan<PlanValue>,
5460
+ InsertSourceStateOfPlan<PlanValue>,
5461
+ FactsOfPlan<PlanValue>
5462
+ >
5993
5463
 
5994
- const offset = <Value extends NumericExpressionInput>(
5464
+ type OffsetApi = <Value extends NumericExpressionInput>(
5995
5465
  value: Value
5996
5466
  ) =>
5997
5467
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5998
5468
  plan: PlanValue & RequireSelectStatement<PlanValue>
5999
- ): QueryPlan<
5469
+ ) => QueryPlan<
6000
5470
  SelectionOfPlan<PlanValue>,
6001
5471
  AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
6002
5472
  AvailableOfPlan<PlanValue>,
@@ -6006,31 +5476,92 @@ type AsCurriedResult<
6006
5476
  AddExpressionRequired<OutstandingOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
6007
5477
  AssumptionsOfPlan<PlanValue>,
6008
5478
  CapabilitiesOfPlan<PlanValue>,
6009
- StatementOfPlan<PlanValue>
6010
- > => {
6011
- const current = plan[Plan.TypeId]
6012
- const currentAst = getAst(plan)
6013
- const currentQuery = getQueryState(plan)
6014
- const expression = toDialectNumericExpression(value)
6015
- const required = extractRequiredFromDialectNumericInputRuntime(value)
6016
- return makePlan({
6017
- selection: current.selection,
6018
- required: [...currentRequiredList(current.required), ...required].filter((name, index, values) =>
6019
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
6020
- available: current.available,
6021
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectNumericInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
6022
- }, {
6023
- ...currentAst,
6024
- offset: expression
6025
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
6026
- }
5479
+ StatementOfPlan<PlanValue>,
5480
+ MutationTargetOfPlan<PlanValue>,
5481
+ InsertSourceStateOfPlan<PlanValue>,
5482
+ FactsOfPlan<PlanValue>
5483
+ >
5484
+
5485
+ const {
5486
+ buildSetOperation,
5487
+ where,
5488
+ from,
5489
+ having,
5490
+ crossJoin,
5491
+ join,
5492
+ orderBy,
5493
+ lock,
5494
+ distinct,
5495
+ limit,
5496
+ offset
5497
+ } = makeDslPlanRuntime({
5498
+ profile,
5499
+ makePlan,
5500
+ getAst,
5501
+ getQueryState,
5502
+ currentRequiredList,
5503
+ toDialectExpression,
5504
+ toDialectNumericExpression,
5505
+ extractRequiredFromDialectInputRuntime,
5506
+ extractRequiredFromDialectNumericInputRuntime,
5507
+ formulaOfExpressionRuntime,
5508
+ assumeFormulaTrue,
5509
+ trueFormula,
5510
+ sourceDetails,
5511
+ presenceWitnessesOfSourceLike,
5512
+ attachInsertSource: (...args) => attachInsertSource(args[0], args[1])
5513
+ }) as {
5514
+ readonly buildSetOperation: (kind: QueryAst.SetOperatorKind, all: boolean, left: any, right: any) => any
5515
+ readonly where: WhereApi
5516
+ readonly from: FromApi
5517
+ readonly having: HavingApi
5518
+ readonly crossJoin: CrossJoinApi
5519
+ readonly join: JoinApi
5520
+ readonly orderBy: OrderByApi
5521
+ readonly lock: LockApi
5522
+ readonly distinct: DistinctApi
5523
+ readonly limit: LimitApi
5524
+ readonly offset: OffsetApi
5525
+ }
5526
+
5527
+ const union = ((left, right) =>
5528
+ buildSetOperation("union", false, left as never, right as never)) as SetOperationApi
5529
+
5530
+ const unionAll = ((left, right) =>
5531
+ buildSetOperation("union", true, left as never, right as never)) as SetOperationApi
5532
+
5533
+ const intersect = ((left, right) =>
5534
+ buildSetOperation("intersect", false, left as never, right as never)) as SetOperationApi
5535
+
5536
+ const intersectAll = ((left, right) =>
5537
+ buildSetOperation("intersect", true, left as never, right as never)) as SetOperationApi
5538
+
5539
+ const except = ((left, right) =>
5540
+ buildSetOperation("except", false, left as never, right as never)) as SetOperationApi
5541
+
5542
+ const exceptAll = ((left, right) =>
5543
+ buildSetOperation("except", true, left as never, right as never)) as SetOperationApi
5544
+
5545
+ const innerJoin = ((table, on) => (join as any)("inner", table, on)) as BinaryJoinApi<"inner">
5546
+
5547
+ const leftJoin = ((table, on) => (join as any)("left", table, on)) as BinaryJoinApi<"left">
6027
5548
 
6028
- const groupBy = <Values extends readonly [GroupByInput, ...GroupByInput[]]>(
5549
+ const rightJoin = ((table, on) => (join as any)("right", table, on)) as BinaryJoinApi<"right">
5550
+
5551
+ const fullJoin = ((table, on) => (join as any)("full", table, on)) as BinaryJoinApi<"full">
5552
+
5553
+ const distinctOn = {
5554
+ __effect_qb_error__: "effect-qb: distinctOn(...) is only supported by the postgres dialect",
5555
+ __effect_qb_dialect__: profile.dialect,
5556
+ __effect_qb_hint__: "Use postgres.Query.distinctOn(...) or regular distinct()/grouping logic"
5557
+ } as DistinctOnApi<Dialect>
5558
+
5559
+ type GroupByApi = <Values extends readonly [GroupByInput, ...GroupByInput[]]>(
6029
5560
  ...values: Values
6030
5561
  ) =>
6031
5562
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
6032
5563
  plan: PlanValue & RequireSelectStatement<PlanValue>
6033
- ): QueryPlan<
5564
+ ) => QueryPlan<
6034
5565
  SelectionOfPlan<PlanValue>,
6035
5566
  Exclude<RequiredOfPlan<PlanValue> | RequiredFromDependencies<TupleDependencies<Values>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6036
5567
  AvailableOfPlan<PlanValue>,
@@ -6040,31 +5571,18 @@ type AsCurriedResult<
6040
5571
  Exclude<OutstandingOfPlan<PlanValue> | RequiredFromDependencies<TupleDependencies<Values>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6041
5572
  AssumptionsOfPlan<PlanValue>,
6042
5573
  CapabilitiesOfPlan<PlanValue>,
6043
- StatementOfPlan<PlanValue>
6044
- > => {
6045
- const current = plan[Plan.TypeId]
6046
- const currentAst = getAst(plan)
6047
- const currentQuery = getQueryState(plan)
6048
- const required = [...values.flatMap((value) => Object.keys(value[Expression.TypeId].dependencies))].filter((name, index, list) =>
6049
- !(name in current.available) && list.indexOf(name) === index)
6050
- return makePlan({
6051
- selection: current.selection,
6052
- required: [...currentRequiredList(current.required), ...required].filter((name, index, list) =>
6053
- !(name in current.available) && list.indexOf(name) === index) as Exclude<RequiredOfPlan<PlanValue> | RequiredFromDependencies<TupleDependencies<Values>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6054
- available: current.available,
6055
- dialect: current.dialect as PlanDialectOf<PlanValue> | TupleDialect<Values>
6056
- }, {
6057
- ...currentAst,
6058
- groupBy: dedupeGroupedExpressions([...currentAst.groupBy, ...values])
6059
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
6060
- }
5574
+ StatementOfPlan<PlanValue>,
5575
+ MutationTargetOfPlan<PlanValue>,
5576
+ InsertSourceStateOfPlan<PlanValue>,
5577
+ FactsOfPlan<PlanValue>
5578
+ >
6061
5579
 
6062
- const returning = <Selection extends SelectionShape>(
5580
+ type ReturningApi = <Selection extends SelectionShape>(
6063
5581
  selection: Selection
6064
5582
  ) =>
6065
5583
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
6066
5584
  plan: PlanValue & RequireMutationStatement<PlanValue>
6067
- ): QueryPlan<
5585
+ ) => QueryPlan<
6068
5586
  Selection,
6069
5587
  Exclude<RequiredOfPlan<PlanValue> | ExtractRequired<Selection>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6070
5588
  AvailableOfPlan<PlanValue>,
@@ -6076,170 +5594,288 @@ type AsCurriedResult<
6076
5594
  CapabilitiesOfPlan<PlanValue>,
6077
5595
  StatementOfPlan<PlanValue>,
6078
5596
  MutationTargetOfPlan<PlanValue>,
6079
- InsertSourceStateOfPlan<PlanValue>
6080
- > => {
6081
- const current = plan[Plan.TypeId]
6082
- const currentAst = getAst(plan)
6083
- const currentQuery = getQueryState(plan)
6084
- return makePlan({
6085
- selection,
6086
- required: [...currentRequiredList(current.required), ...extractRequiredRuntime(selection)].filter((name, index, list) =>
6087
- !(name in current.available) && list.indexOf(name) === index) as Exclude<RequiredOfPlan<PlanValue> | ExtractRequired<Selection>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6088
- available: current.available,
6089
- dialect: current.dialect as PlanDialectOf<PlanValue> | ExtractDialect<Selection>
6090
- }, {
6091
- ...currentAst,
6092
- select: selection
6093
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>, currentQuery.target, currentQuery.insertSource)
6094
- }
5597
+ InsertSourceStateOfPlan<PlanValue>,
5598
+ FactsOfPlan<PlanValue>
5599
+ >
6095
5600
 
6096
- function insert<
6097
- Target extends MutationTargetLike
5601
+ export interface InsertApi {
5602
+ <Target extends MutationTargetLike>(
5603
+ target: Target
5604
+ ): QueryPlan<
5605
+ {},
5606
+ never,
5607
+ AddAvailable<{}, SourceNameOf<Target>>,
5608
+ TableDialectOf<Target>,
5609
+ never,
5610
+ SourceNameOf<Target>,
5611
+ never,
5612
+ TrueFormula,
5613
+ "write",
5614
+ "insert",
5615
+ Target,
5616
+ "missing",
5617
+ EmptyFacts
5618
+ >
5619
+ <Target extends MutationTargetLike, Values extends Record<string, unknown>>(
5620
+ target: Target,
5621
+ values: MutationValuesInput<"insert", Target, Values>
5622
+ ): QueryPlan<
5623
+ {},
5624
+ Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5625
+ AddAvailable<{}, SourceNameOf<Target>>,
5626
+ TableDialectOf<Target>,
5627
+ never,
5628
+ SourceNameOf<Target>,
5629
+ Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5630
+ TrueFormula,
5631
+ "write",
5632
+ "insert",
5633
+ Target,
5634
+ "ready",
5635
+ EmptyFacts
5636
+ >
5637
+ }
5638
+
5639
+ type AttachInsertSourceApi = (
5640
+ plan: QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "missing">,
5641
+ source: AnyValuesInput | AnyValuesSource | AnyUnnestSource | CompletePlan<QueryPlan<any, any, any, any, any, any, any, any, any, any>>
5642
+ ) => QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "ready">
5643
+
5644
+ type OnConflictApi = <
5645
+ Target extends MutationTargetLike,
5646
+ const Columns extends DdlColumnInput,
5647
+ UpdateValues extends MutationInputOf<Table.UpdateOf<Target>> | undefined = MutationInputOf<Table.UpdateOf<Target>> | undefined,
5648
+ Options extends ConflictActionInput<Target, Dialect, UpdateValues> = ConflictActionInput<Target, Dialect, UpdateValues>
6098
5649
  >(
6099
- target: Target
6100
- ): QueryPlan<
5650
+ target: ConflictTargetInput<Target, Dialect, Columns>,
5651
+ options?: Options
5652
+ ) =>
5653
+ <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5654
+ plan: PlanValue & RequireInsertStatement<PlanValue>
5655
+ ) => QueryPlan<
5656
+ SelectionOfPlan<PlanValue>,
5657
+ Exclude<RequiredOfPlan<PlanValue> | MutationRequiredFromValues<Exclude<UpdateValues, undefined>> | RequiredFromInput<Extract<Options["where"], PredicateInput>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
5658
+ AvailableOfPlan<PlanValue>,
5659
+ PlanDialectOf<PlanValue>,
5660
+ GroupedOfPlan<PlanValue>,
5661
+ ScopedNamesOfPlan<PlanValue>,
5662
+ Exclude<OutstandingOfPlan<PlanValue> | MutationRequiredFromValues<Exclude<UpdateValues, undefined>> | RequiredFromInput<Extract<Options["where"], PredicateInput>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
5663
+ AssumptionsOfPlan<PlanValue>,
5664
+ CapabilitiesOfPlan<PlanValue>,
5665
+ StatementOfPlan<PlanValue>,
5666
+ MutationTargetOfPlan<PlanValue>,
5667
+ InsertSourceStateOfPlan<PlanValue>,
5668
+ FactsOfPlan<PlanValue>
5669
+ >
5670
+
5671
+ interface UpdateApi {
5672
+ <Targets extends MutationTargetTuple, Values extends UpdateInputOfTarget<Targets>>(
5673
+ target: Dialect extends "mysql" ? Targets : never,
5674
+ values: Values
5675
+ ): QueryPlan<
5676
+ {},
5677
+ Exclude<NestedMutationRequiredFromValues<Values>, MutationTargetNamesOf<Targets>>,
5678
+ AddAvailableMany<{}, MutationTargetNamesOf<Targets>>,
5679
+ TableDialectOf<Targets[0]>,
5680
+ never,
5681
+ MutationTargetNamesOf<Targets>,
5682
+ Exclude<NestedMutationRequiredFromValues<Values>, MutationTargetNamesOf<Targets>>,
5683
+ TrueFormula,
5684
+ "write",
5685
+ "update",
5686
+ any,
5687
+ "ready",
5688
+ EmptyFacts
5689
+ >
5690
+ <Target extends MutationTargetLike, Values extends Record<string, unknown>>(
5691
+ target: Target,
5692
+ values: MutationValuesInput<"update", Target, Values>
5693
+ ): QueryPlan<
5694
+ {},
5695
+ Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5696
+ AddAvailable<{}, SourceNameOf<Target>>,
5697
+ TableDialectOf<Target>,
5698
+ never,
5699
+ SourceNameOf<Target>,
5700
+ Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5701
+ TrueFormula,
5702
+ "write",
5703
+ "update",
5704
+ any,
5705
+ "ready",
5706
+ EmptyFacts
5707
+ >
5708
+ }
5709
+
5710
+ type UpsertApi = <
5711
+ Target extends MutationTargetLike,
5712
+ Values extends MutationInputOf<Table.InsertOf<Target>>,
5713
+ const Columns extends DdlColumnInput,
5714
+ UpdateValues extends MutationInputOf<Table.UpdateOf<Target>>
5715
+ >(
5716
+ target: Target,
5717
+ values: Values,
5718
+ conflictColumns: ValidateTargetColumns<Target, NormalizeDdlColumns<Columns>>,
5719
+ updateValues?: UpdateValues
5720
+ ) => QueryPlan<
5721
+ {},
5722
+ Exclude<MutationRequiredFromValues<Values> | MutationRequiredFromValues<UpdateValues>, SourceNameOf<Target>>,
5723
+ AddAvailable<{}, SourceNameOf<Target>>,
5724
+ TableDialectOf<Target>,
5725
+ never,
5726
+ SourceNameOf<Target>,
5727
+ Exclude<MutationRequiredFromValues<Values> | MutationRequiredFromValues<UpdateValues>, SourceNameOf<Target>>,
5728
+ TrueFormula,
5729
+ "write",
5730
+ "insert",
5731
+ Target,
5732
+ "ready",
5733
+ EmptyFacts
5734
+ >
5735
+
5736
+ interface DeleteApi {
5737
+ <Target extends MutationTargetLike>(
5738
+ target: Target
5739
+ ): QueryPlan<
5740
+ {},
5741
+ never,
5742
+ AddAvailable<{}, SourceNameOf<Target>>,
5743
+ TableDialectOf<Target>,
5744
+ never,
5745
+ SourceNameOf<Target>,
5746
+ never,
5747
+ TrueFormula,
5748
+ "write",
5749
+ "delete",
5750
+ any,
5751
+ "ready",
5752
+ EmptyFacts
5753
+ >
5754
+ <Targets extends MutationTargetTuple>(
5755
+ target: Dialect extends "mysql" ? Targets : never
5756
+ ): QueryPlan<
5757
+ {},
5758
+ never,
5759
+ AddAvailableMany<{}, MutationTargetNamesOf<Targets>>,
5760
+ TableDialectOf<Targets[0]>,
5761
+ never,
5762
+ MutationTargetNamesOf<Targets>,
5763
+ never,
5764
+ TrueFormula,
5765
+ "write",
5766
+ "delete",
5767
+ any,
5768
+ "ready",
5769
+ EmptyFacts
5770
+ >
5771
+ }
5772
+
5773
+ type TruncateApi = <Target extends MutationTargetLike>(
5774
+ target: Target,
5775
+ options?: TruncateOptions
5776
+ ) => QueryPlan<
6101
5777
  {},
6102
5778
  never,
6103
- AddAvailable<{}, SourceNameOf<Target>>,
5779
+ {},
6104
5780
  TableDialectOf<Target>,
6105
5781
  never,
6106
- SourceNameOf<Target>,
5782
+ never,
6107
5783
  never,
6108
5784
  TrueFormula,
6109
5785
  "write",
6110
- "insert",
6111
- Target,
6112
- "missing"
5786
+ "truncate",
5787
+ any,
5788
+ "ready",
5789
+ EmptyFacts
6113
5790
  >
6114
- function insert<
5791
+
5792
+ type MergeApi = <
6115
5793
  Target extends MutationTargetLike,
6116
- Values extends Record<string, unknown>
5794
+ Source extends SourceLike,
5795
+ On extends PredicateInput,
5796
+ MatchedValues extends MutationInputOf<Table.UpdateOf<Target>> = MutationInputOf<Table.UpdateOf<Target>>,
5797
+ InsertValues extends MutationInputOf<Table.InsertOf<Target>> = MutationInputOf<Table.InsertOf<Target>>,
5798
+ MatchedPredicate extends PredicateInput | undefined = undefined,
5799
+ NotMatchedPredicate extends PredicateInput | undefined = undefined
6117
5800
  >(
6118
5801
  target: Target,
6119
- values: MutationValuesInput<"insert", Target, Values>
6120
- ): QueryPlan<
5802
+ source: Source & (
5803
+ SourceRequiredOf<Source> extends never ? unknown : SourceRequirementError<Source>
5804
+ ),
5805
+ on: On,
5806
+ options?: MergeOptions<Target, MatchedValues, InsertValues, MatchedPredicate, NotMatchedPredicate>
5807
+ ) => QueryPlan<
6121
5808
  {},
6122
- Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
6123
- AddAvailable<{}, SourceNameOf<Target>>,
6124
- TableDialectOf<Target>,
5809
+ Exclude<
5810
+ AddExpressionRequired<
5811
+ MergeRequiredFromPredicate<
5812
+ MatchedPredicate,
5813
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
5814
+ > | MergeRequiredFromPredicate<
5815
+ NotMatchedPredicate,
5816
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
5817
+ > | MutationRequiredFromValues<MatchedValues> | MutationRequiredFromValues<InsertValues>,
5818
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
5819
+ On
5820
+ >,
5821
+ SourceNameOf<Target> | SourceNameOf<Source>
5822
+ >,
5823
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
5824
+ TableDialectOf<Target> | SourceDialectOf<Source>,
6125
5825
  never,
6126
- SourceNameOf<Target>,
6127
- Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5826
+ SourceNameOf<Target> | SourceNameOf<Source>,
5827
+ Exclude<
5828
+ AddExpressionRequired<
5829
+ MergeRequiredFromPredicate<
5830
+ MatchedPredicate,
5831
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
5832
+ > | MergeRequiredFromPredicate<
5833
+ NotMatchedPredicate,
5834
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
5835
+ > | MutationRequiredFromValues<MatchedValues> | MutationRequiredFromValues<InsertValues>,
5836
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
5837
+ On
5838
+ >,
5839
+ SourceNameOf<Target> | SourceNameOf<Source>
5840
+ >,
6128
5841
  TrueFormula,
6129
- "write",
6130
- "insert",
6131
- Target,
6132
- "ready"
5842
+ MergeCapabilities<"write", SourceCapabilitiesOf<Source>>,
5843
+ "merge",
5844
+ any,
5845
+ "ready",
5846
+ EmptyFacts
6133
5847
  >
6134
- function insert(
5848
+
5849
+ const mutationRuntime = makeDslMutationRuntime({
5850
+ makePlan,
5851
+ getAst,
5852
+ getQueryState,
5853
+ currentRequiredList,
5854
+ toDialectExpression,
5855
+ buildMutationAssignments,
5856
+ buildInsertValuesRows,
5857
+ normalizeInsertUnnestValues,
5858
+ normalizeInsertSelectColumns,
5859
+ buildConflictTarget,
5860
+ mutationTargetClauses,
5861
+ mutationAvailableSources,
5862
+ normalizeColumnList,
5863
+ targetSourceDetails,
5864
+ sourceDetails
5865
+ })
5866
+
5867
+ const insert: InsertApi = ((
6135
5868
  target: MutationTargetLike,
6136
5869
  values?: Record<string, unknown>
6137
- ): QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "missing" | "ready"> {
6138
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6139
- const assignments = values === undefined
6140
- ? []
6141
- : buildMutationAssignments(target, values)
6142
- const required = assignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies))
6143
- const insertState = values === undefined ? "missing" : "ready"
6144
- return makePlan({
6145
- selection: {},
6146
- required: required.filter((name, index, list) => name !== sourceName && list.indexOf(name) === index),
6147
- available: {
6148
- [sourceName]: {
6149
- name: sourceName,
6150
- mode: "required",
6151
- baseName: sourceBaseName
6152
- }
6153
- } as AddAvailable<{}, string>,
6154
- dialect: target[Plan.TypeId].dialect
6155
- }, {
6156
- kind: "insert",
6157
- select: {},
6158
- into: {
6159
- kind: "from",
6160
- tableName: sourceName,
6161
- baseTableName: sourceBaseName,
6162
- source: target
6163
- },
6164
- values: assignments,
6165
- conflict: undefined,
6166
- where: [],
6167
- having: [],
6168
- joins: [],
6169
- groupBy: [],
6170
- orderBy: []
6171
- }, undefined as unknown as TrueFormula, "write", "insert", target, insertState)
6172
- }
5870
+ ) => mutationRuntime.insert(target, values)) as InsertApi
6173
5871
 
6174
5872
  const attachInsertSource = (
6175
5873
  plan: QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "missing">,
6176
5874
  source: AnyValuesInput | AnyValuesSource | AnyUnnestSource | CompletePlan<QueryPlan<any, any, any, any, any, any, any, any, any, any>>
6177
- ): QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "ready"> => {
6178
- const current = plan[Plan.TypeId]
6179
- const currentAst = getAst(plan)
6180
- const currentQuery = getQueryState(plan)
6181
- const target = currentQuery.target as MutationTargetLike
6182
- const targetSource = currentAst.into!
6183
- const sourceName = targetSource.tableName
6184
-
6185
- if (typeof source === "object" && source !== null && "kind" in source && source.kind === "values") {
6186
- const valuesSource = source as AnyValuesInput | AnyValuesSource
6187
- const normalized = buildInsertValuesRows(target, valuesSource.rows as readonly [InsertRowInput<MutationTargetLike>, ...InsertRowInput<MutationTargetLike>[]])
6188
- return makePlan({
6189
- selection: current.selection,
6190
- required: normalized.required.filter((name) => name !== sourceName),
6191
- available: current.available,
6192
- dialect: current.dialect
6193
- }, {
6194
- ...currentAst,
6195
- values: [],
6196
- insertSource: {
6197
- kind: "values",
6198
- columns: normalized.columns,
6199
- rows: normalized.rows
6200
- }
6201
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, "ready")
6202
- }
6203
-
6204
- if (typeof source === "object" && source !== null && "kind" in source && source.kind === "unnest") {
6205
- const unnestSource = source as AnyUnnestSource
6206
- const normalized = normalizeInsertUnnestValues(target, unnestSource.values as any)
6207
- return makePlan({
6208
- selection: current.selection,
6209
- required: [] as never,
6210
- available: current.available,
6211
- dialect: current.dialect
6212
- }, {
6213
- ...currentAst,
6214
- values: [],
6215
- insertSource: {
6216
- kind: "unnest",
6217
- columns: normalized.columns,
6218
- values: normalized.values
6219
- }
6220
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, "ready")
6221
- }
6222
-
6223
- const sourcePlan = source as CompletePlan<QueryPlan<any, any, any, any, any, any, any, any, any, any>>
6224
- const selection = sourcePlan[Plan.TypeId].selection as Record<string, Expression.Any>
6225
- const columns = normalizeInsertSelectColumns(selection)
6226
- return makePlan({
6227
- selection: current.selection,
6228
- required: currentRequiredList(sourcePlan[Plan.TypeId].required).filter((name) => name !== sourceName),
6229
- available: current.available,
6230
- dialect: current.dialect
6231
- }, {
6232
- ...currentAst,
6233
- values: [],
6234
- insertSource: {
6235
- kind: "query",
6236
- columns,
6237
- query: sourcePlan
6238
- }
6239
- }, currentQuery.assumptions, currentQuery.capabilities as MergeCapabilities<typeof currentQuery.capabilities, CapabilitiesOfPlan<typeof sourcePlan>>, currentQuery.statement, currentQuery.target, "ready")
6240
- }
5875
+ ): QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "ready"> =>
5876
+ mutationRuntime.attachInsertSource(plan, source)
6241
5877
 
6242
- const onConflict = <
5878
+ const onConflict: OnConflictApi = <
6243
5879
  Target extends MutationTargetLike,
6244
5880
  const Columns extends DdlColumnInput,
6245
5881
  UpdateValues extends MutationInputOf<Table.UpdateOf<Target>> | undefined = MutationInputOf<Table.UpdateOf<Target>> | undefined,
@@ -6262,109 +5898,16 @@ type AsCurriedResult<
6262
5898
  CapabilitiesOfPlan<PlanValue>,
6263
5899
  StatementOfPlan<PlanValue>,
6264
5900
  MutationTargetOfPlan<PlanValue>,
6265
- InsertSourceStateOfPlan<PlanValue>
6266
- > => {
6267
- const current = plan[Plan.TypeId]
6268
- const currentAst = getAst(plan)
6269
- const currentQuery = getQueryState(plan)
6270
- const insertTarget = currentAst.into!.source as Target
6271
- const conflictTarget = buildConflictTarget(insertTarget, target as readonly string[] | { readonly columns: readonly string[]; readonly where?: PredicateInput } | { readonly constraint: string })
6272
- const updateAssignments = options.update
6273
- ? buildMutationAssignments(insertTarget, options.update)
6274
- : []
6275
- const updateWhere = options.where === undefined
6276
- ? undefined
6277
- : toDialectExpression(options.where as PredicateInput)
6278
- const required = [
6279
- ...currentRequiredList(current.required),
6280
- ...updateAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
6281
- ...(updateWhere ? Object.keys(updateWhere[Expression.TypeId].dependencies) : [])
6282
- ].filter((name, index, list) =>
6283
- !(name in current.available) && list.indexOf(name) === index)
6284
- return makePlan({
6285
- selection: current.selection,
6286
- required: required as Exclude<RequiredOfPlan<PlanValue> | MutationRequiredFromValues<Exclude<UpdateValues, undefined>> | RequiredFromInput<Extract<Options["where"], PredicateInput>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6287
- available: current.available,
6288
- dialect: current.dialect as PlanDialectOf<PlanValue>
6289
- }, {
6290
- ...currentAst,
6291
- conflict: {
6292
- kind: "conflict",
6293
- target: conflictTarget,
6294
- action: updateAssignments.length === 0 ? "doNothing" : "doUpdate",
6295
- values: updateAssignments.length === 0 ? undefined : updateAssignments,
6296
- where: updateWhere
6297
- }
6298
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>, currentQuery.target, currentQuery.insertSource)
6299
- }
5901
+ InsertSourceStateOfPlan<PlanValue>,
5902
+ FactsOfPlan<PlanValue>
5903
+ > => mutationRuntime.onConflict(target, options)(plan)
6300
5904
 
6301
- function update<
6302
- Targets extends MutationTargetTuple,
6303
- Values extends UpdateInputOfTarget<Targets>
6304
- >(
6305
- target: Dialect extends "mysql" ? Targets : never,
6306
- values: Values
6307
- ): QueryPlan<
6308
- {},
6309
- Exclude<NestedMutationRequiredFromValues<Values>, MutationTargetNamesOf<Targets>>,
6310
- AddAvailableMany<{}, MutationTargetNamesOf<Targets>>,
6311
- TableDialectOf<Targets[0]>,
6312
- never,
6313
- MutationTargetNamesOf<Targets>,
6314
- Exclude<NestedMutationRequiredFromValues<Values>, MutationTargetNamesOf<Targets>>,
6315
- TrueFormula,
6316
- "write",
6317
- "update"
6318
- >
6319
- function update<
6320
- Target extends MutationTargetLike,
6321
- Values extends Record<string, unknown>
6322
- >(
6323
- target: Target,
6324
- values: MutationValuesInput<"update", Target, Values>
6325
- ): QueryPlan<
6326
- {},
6327
- Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
6328
- AddAvailable<{}, SourceNameOf<Target>>,
6329
- TableDialectOf<Target>,
6330
- never,
6331
- SourceNameOf<Target>,
6332
- Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
6333
- TrueFormula,
6334
- "write",
6335
- "update"
6336
- >
6337
- function update(
5905
+ const update: UpdateApi = ((
6338
5906
  target: MutationTargetInput,
6339
5907
  values: Record<string, unknown>
6340
- ): QueryPlan<any, any, any, any, any, any, any, any, any, "update"> {
6341
- const targets = mutationTargetClauses(target)
6342
- const primaryTarget = targets[0]!
6343
- const assignments = buildMutationAssignments(target, values)
6344
- const targetNames = new Set(targets.map((entry) => entry.tableName))
6345
- const required = assignments
6346
- .flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies))
6347
- .filter((name, index, list) => !targetNames.has(name) && list.indexOf(name) === index)
6348
- return makePlan({
6349
- selection: {},
6350
- required,
6351
- available: mutationAvailableSources(target),
6352
- dialect: (primaryTarget.source as MutationTargetLike)[Plan.TypeId].dialect
6353
- }, {
6354
- kind: "update",
6355
- select: {},
6356
- target: primaryTarget,
6357
- targets,
6358
- set: assignments,
6359
- where: [],
6360
- having: [],
6361
- joins: [],
6362
- groupBy: [],
6363
- orderBy: []
6364
- }, undefined as unknown as TrueFormula, "write", "update")
6365
- }
5908
+ ) => mutationRuntime.update(target, values)) as UpdateApi
6366
5909
 
6367
- const upsert = <
5910
+ const upsert: UpsertApi = <
6368
5911
  Target extends MutationTargetLike,
6369
5912
  Values extends MutationInputOf<Table.InsertOf<Target>>,
6370
5913
  const Columns extends DdlColumnInput,
@@ -6386,109 +5929,15 @@ type AsCurriedResult<
6386
5929
  "write",
6387
5930
  "insert",
6388
5931
  Target,
6389
- "ready"
6390
- > => {
6391
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6392
- const assignments = buildMutationAssignments(target, values)
6393
- const updateAssignments = updateValues ? buildMutationAssignments(target, updateValues) : []
6394
- const required = [
6395
- ...assignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
6396
- ...updateAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies))
6397
- ]
6398
- return makePlan({
6399
- selection: {},
6400
- required: required.filter((name, index, list) => name !== sourceName && list.indexOf(name) === index) as unknown as Exclude<MutationRequiredFromValues<Values> | MutationRequiredFromValues<UpdateValues>, SourceNameOf<Target>>,
6401
- available: {
6402
- [sourceName]: {
6403
- name: sourceName,
6404
- mode: "required",
6405
- baseName: sourceBaseName
6406
- }
6407
- } as AddAvailable<{}, SourceNameOf<Target>>,
6408
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
6409
- }, {
6410
- kind: "insert",
6411
- select: {},
6412
- into: {
6413
- kind: "from",
6414
- tableName: sourceName,
6415
- baseTableName: sourceBaseName,
6416
- source: target
6417
- },
6418
- values: assignments,
6419
- conflict: {
6420
- kind: "conflict",
6421
- target: {
6422
- kind: "columns",
6423
- columns: normalizeColumnList(conflictColumns as string | readonly string[]) as readonly [string, ...string[]]
6424
- },
6425
- action: updateAssignments.length > 0 ? "doUpdate" : "doNothing",
6426
- values: updateAssignments.length > 0 ? updateAssignments : undefined
6427
- },
6428
- where: [],
6429
- having: [],
6430
- joins: [],
6431
- groupBy: [],
6432
- orderBy: []
6433
- }, undefined as unknown as TrueFormula, "write", "insert", target, "ready")
6434
- }
5932
+ "ready",
5933
+ EmptyFacts
5934
+ > => mutationRuntime.upsert(target, values, conflictColumns as string | readonly string[], updateValues)
6435
5935
 
6436
- function delete_<
6437
- Target extends MutationTargetLike
6438
- >(
6439
- target: Target
6440
- ): QueryPlan<
6441
- {},
6442
- never,
6443
- AddAvailable<{}, SourceNameOf<Target>>,
6444
- TableDialectOf<Target>,
6445
- never,
6446
- SourceNameOf<Target>,
6447
- never,
6448
- TrueFormula,
6449
- "write",
6450
- "delete"
6451
- >
6452
- function delete_<
6453
- Targets extends MutationTargetTuple
6454
- >(
6455
- target: Dialect extends "mysql" ? Targets : never
6456
- ): QueryPlan<
6457
- {},
6458
- never,
6459
- AddAvailableMany<{}, MutationTargetNamesOf<Targets>>,
6460
- TableDialectOf<Targets[0]>,
6461
- never,
6462
- MutationTargetNamesOf<Targets>,
6463
- never,
6464
- TrueFormula,
6465
- "write",
6466
- "delete"
6467
- >
6468
- function delete_(
5936
+ const delete_: DeleteApi = ((
6469
5937
  target: MutationTargetInput
6470
- ): QueryPlan<any, any, any, any, any, any, any, any, any, "delete"> {
6471
- const targets = mutationTargetClauses(target)
6472
- const primaryTarget = targets[0]!
6473
- return makePlan({
6474
- selection: {},
6475
- required: [] as never,
6476
- available: mutationAvailableSources(target),
6477
- dialect: (primaryTarget.source as MutationTargetLike)[Plan.TypeId].dialect
6478
- }, {
6479
- kind: "delete",
6480
- select: {},
6481
- target: primaryTarget,
6482
- targets,
6483
- where: [],
6484
- having: [],
6485
- joins: [],
6486
- groupBy: [],
6487
- orderBy: []
6488
- }, undefined as unknown as TrueFormula, "write", "delete")
6489
- }
5938
+ ) => mutationRuntime.delete_(target)) as DeleteApi
6490
5939
 
6491
- const truncate = <
5940
+ const truncate: TruncateApi = <
6492
5941
  Target extends MutationTargetLike
6493
5942
  >(
6494
5943
  target: Target,
@@ -6503,37 +5952,13 @@ type AsCurriedResult<
6503
5952
  never,
6504
5953
  TrueFormula,
6505
5954
  "write",
6506
- "truncate"
6507
- > => {
6508
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6509
- return makePlan({
6510
- selection: {},
6511
- required: [] as never,
6512
- available: {},
6513
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
6514
- }, {
6515
- kind: "truncate",
6516
- select: {},
6517
- target: {
6518
- kind: "from",
6519
- tableName: sourceName,
6520
- baseTableName: sourceBaseName,
6521
- source: target
6522
- },
6523
- truncate: {
6524
- kind: "truncate",
6525
- restartIdentity: options.restartIdentity ?? false,
6526
- cascade: options.cascade ?? false
6527
- },
6528
- where: [],
6529
- having: [],
6530
- joins: [],
6531
- groupBy: [],
6532
- orderBy: []
6533
- }, undefined as unknown as TrueFormula, "write", "truncate")
6534
- }
5955
+ "truncate",
5956
+ any,
5957
+ "ready",
5958
+ EmptyFacts
5959
+ > => mutationRuntime.truncate(target, options)
6535
5960
 
6536
- const merge = <
5961
+ const merge: MergeApi = <
6537
5962
  Target extends MutationTargetLike,
6538
5963
  Source extends SourceLike,
6539
5964
  On extends PredicateInput,
@@ -6584,110 +6009,13 @@ type AsCurriedResult<
6584
6009
  >,
6585
6010
  TrueFormula,
6586
6011
  MergeCapabilities<"write", SourceCapabilitiesOf<Source>>,
6587
- "merge"
6588
- > => {
6589
- const { sourceName: targetName, sourceBaseName: targetBaseName } = targetSourceDetails(target)
6590
- const { sourceName: usingName, sourceBaseName: usingBaseName } = sourceDetails(source)
6591
- const onExpression = toDialectExpression(on)
6592
- const matched = options.whenMatched
6593
- const notMatched = options.whenNotMatched
6594
- if (matched && "delete" in matched && "update" in matched) {
6595
- throw new Error("merge whenMatched cannot specify both update and delete")
6596
- }
6597
- const matchedPredicate = matched?.predicate ? toDialectExpression(matched.predicate) : undefined
6598
- const matchedAssignments = matched && "update" in matched && matched.update
6599
- ? buildMutationAssignments(target, matched.update)
6600
- : []
6601
- const notMatchedPredicate = notMatched?.predicate ? toDialectExpression(notMatched.predicate) : undefined
6602
- const notMatchedAssignments = notMatched
6603
- ? buildMutationAssignments(target, notMatched.values)
6604
- : []
6605
- const required = [
6606
- ...Object.keys(onExpression[Expression.TypeId].dependencies),
6607
- ...matchedAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
6608
- ...notMatchedAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
6609
- ...(matchedPredicate ? Object.keys(matchedPredicate[Expression.TypeId].dependencies) : []),
6610
- ...(notMatchedPredicate ? Object.keys(notMatchedPredicate[Expression.TypeId].dependencies) : [])
6611
- ].filter((name, index, values) =>
6612
- name !== targetName && name !== usingName && values.indexOf(name) === index)
6613
- return makePlan({
6614
- selection: {},
6615
- required: required as unknown as Exclude<
6616
- AddExpressionRequired<
6617
- MergeRequiredFromPredicate<
6618
- MatchedPredicate,
6619
- AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
6620
- > | MergeRequiredFromPredicate<
6621
- NotMatchedPredicate,
6622
- AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
6623
- > | MutationRequiredFromValues<MatchedValues> | MutationRequiredFromValues<InsertValues>,
6624
- AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
6625
- On
6626
- >,
6627
- SourceNameOf<Target> | SourceNameOf<Source>
6628
- >,
6629
- available: {
6630
- [targetName]: {
6631
- name: targetName,
6632
- mode: "required",
6633
- baseName: targetBaseName
6634
- },
6635
- [usingName]: {
6636
- name: usingName,
6637
- mode: "required",
6638
- baseName: usingBaseName
6639
- }
6640
- } as AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
6641
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target> | SourceDialectOf<Source>
6642
- }, {
6643
- kind: "merge",
6644
- select: {},
6645
- target: {
6646
- kind: "from",
6647
- tableName: targetName,
6648
- baseTableName: targetBaseName,
6649
- source: target
6650
- },
6651
- using: {
6652
- kind: "from",
6653
- tableName: usingName,
6654
- baseTableName: usingBaseName,
6655
- source
6656
- },
6657
- merge: {
6658
- kind: "merge",
6659
- on: onExpression,
6660
- whenMatched: matched
6661
- ? ("delete" in matched && matched.delete
6662
- ? {
6663
- kind: "delete",
6664
- predicate: matchedPredicate
6665
- }
6666
- : {
6667
- kind: "update",
6668
- values: matchedAssignments,
6669
- predicate: matchedPredicate
6670
- })
6671
- : undefined,
6672
- whenNotMatched: notMatched
6673
- ? {
6674
- kind: "insert",
6675
- values: notMatchedAssignments,
6676
- predicate: notMatchedPredicate
6677
- }
6678
- : undefined
6679
- },
6680
- where: [],
6681
- having: [],
6682
- joins: [],
6683
- groupBy: [],
6684
- orderBy: []
6685
- }, undefined as unknown as TrueFormula, "write" as MergeCapabilities<"write", SourceCapabilitiesOf<Source>>, "merge")
6686
- }
6012
+ "merge",
6013
+ any,
6014
+ "ready",
6015
+ EmptyFacts
6016
+ > => mutationRuntime.merge(target, source, on, options)
6687
6017
 
6688
- const transaction = (
6689
- options: TransactionOptions = {}
6690
- ): QueryPlan<
6018
+ type TransactionApi = (options?: TransactionOptions) => QueryPlan<
6691
6019
  {},
6692
6020
  never,
6693
6021
  {},
@@ -6698,28 +6026,9 @@ type AsCurriedResult<
6698
6026
  TrueFormula,
6699
6027
  "transaction",
6700
6028
  "transaction"
6701
- > =>
6702
- makePlan({
6703
- selection: {},
6704
- required: [] as never,
6705
- available: {},
6706
- dialect: profile.dialect as Dialect
6707
- }, {
6708
- kind: "transaction",
6709
- select: {},
6710
- transaction: {
6711
- kind: "transaction",
6712
- isolationLevel: options.isolationLevel,
6713
- readOnly: options.readOnly
6714
- },
6715
- where: [],
6716
- having: [],
6717
- joins: [],
6718
- groupBy: [],
6719
- orderBy: []
6720
- }, undefined as unknown as TrueFormula, "transaction", "transaction")
6721
-
6722
- const commit = (): QueryPlan<
6029
+ >
6030
+
6031
+ type CommitApi = () => QueryPlan<
6723
6032
  {},
6724
6033
  never,
6725
6034
  {},
@@ -6730,26 +6039,9 @@ type AsCurriedResult<
6730
6039
  TrueFormula,
6731
6040
  "transaction",
6732
6041
  "commit"
6733
- > =>
6734
- makePlan({
6735
- selection: {},
6736
- required: [] as never,
6737
- available: {},
6738
- dialect: profile.dialect as Dialect
6739
- }, {
6740
- kind: "commit",
6741
- select: {},
6742
- transaction: {
6743
- kind: "commit"
6744
- },
6745
- where: [],
6746
- having: [],
6747
- joins: [],
6748
- groupBy: [],
6749
- orderBy: []
6750
- }, undefined as unknown as TrueFormula, "transaction", "commit")
6751
-
6752
- const rollback = (): QueryPlan<
6042
+ >
6043
+
6044
+ type RollbackApi = () => QueryPlan<
6753
6045
  {},
6754
6046
  never,
6755
6047
  {},
@@ -6760,28 +6052,9 @@ type AsCurriedResult<
6760
6052
  TrueFormula,
6761
6053
  "transaction",
6762
6054
  "rollback"
6763
- > =>
6764
- makePlan({
6765
- selection: {},
6766
- required: [] as never,
6767
- available: {},
6768
- dialect: profile.dialect as Dialect
6769
- }, {
6770
- kind: "rollback",
6771
- select: {},
6772
- transaction: {
6773
- kind: "rollback"
6774
- },
6775
- where: [],
6776
- having: [],
6777
- joins: [],
6778
- groupBy: [],
6779
- orderBy: []
6780
- }, undefined as unknown as TrueFormula, "transaction", "rollback")
6781
-
6782
- const savepoint = <Name extends string>(
6783
- name: Name
6784
- ): QueryPlan<
6055
+ >
6056
+
6057
+ type SavepointApi = <Name extends string>(name: Name) => QueryPlan<
6785
6058
  {},
6786
6059
  never,
6787
6060
  {},
@@ -6792,29 +6065,9 @@ type AsCurriedResult<
6792
6065
  TrueFormula,
6793
6066
  "transaction",
6794
6067
  "savepoint"
6795
- > =>
6796
- makePlan({
6797
- selection: {},
6798
- required: [] as never,
6799
- available: {},
6800
- dialect: profile.dialect as Dialect
6801
- }, {
6802
- kind: "savepoint",
6803
- select: {},
6804
- transaction: {
6805
- kind: "savepoint",
6806
- name
6807
- },
6808
- where: [],
6809
- having: [],
6810
- joins: [],
6811
- groupBy: [],
6812
- orderBy: []
6813
- }, undefined as unknown as TrueFormula, "transaction", "savepoint")
6814
-
6815
- const rollbackTo = <Name extends string>(
6816
- name: Name
6817
- ): QueryPlan<
6068
+ >
6069
+
6070
+ type RollbackToApi = <Name extends string>(name: Name) => QueryPlan<
6818
6071
  {},
6819
6072
  never,
6820
6073
  {},
@@ -6825,29 +6078,9 @@ type AsCurriedResult<
6825
6078
  TrueFormula,
6826
6079
  "transaction",
6827
6080
  "rollbackTo"
6828
- > =>
6829
- makePlan({
6830
- selection: {},
6831
- required: [] as never,
6832
- available: {},
6833
- dialect: profile.dialect as Dialect
6834
- }, {
6835
- kind: "rollbackTo",
6836
- select: {},
6837
- transaction: {
6838
- kind: "rollbackTo",
6839
- name
6840
- },
6841
- where: [],
6842
- having: [],
6843
- joins: [],
6844
- groupBy: [],
6845
- orderBy: []
6846
- }, undefined as unknown as TrueFormula, "transaction", "rollbackTo")
6847
-
6848
- const releaseSavepoint = <Name extends string>(
6849
- name: Name
6850
- ): QueryPlan<
6081
+ >
6082
+
6083
+ type ReleaseSavepointApi = <Name extends string>(name: Name) => QueryPlan<
6851
6084
  {},
6852
6085
  never,
6853
6086
  {},
@@ -6858,32 +6091,12 @@ type AsCurriedResult<
6858
6091
  TrueFormula,
6859
6092
  "transaction",
6860
6093
  "releaseSavepoint"
6861
- > =>
6862
- makePlan({
6863
- selection: {},
6864
- required: [] as never,
6865
- available: {},
6866
- dialect: profile.dialect as Dialect
6867
- }, {
6868
- kind: "releaseSavepoint",
6869
- select: {},
6870
- transaction: {
6871
- kind: "releaseSavepoint",
6872
- name
6873
- },
6874
- where: [],
6875
- having: [],
6876
- joins: [],
6877
- groupBy: [],
6878
- orderBy: []
6879
- }, undefined as unknown as TrueFormula, "transaction", "releaseSavepoint")
6880
-
6881
- const createTable = <
6882
- Target extends SchemaTableLike
6883
- >(
6094
+ >
6095
+
6096
+ type CreateTableApi = <Target extends SchemaTableLike>(
6884
6097
  target: Target,
6885
- options: CreateTableOptions = {}
6886
- ): QueryPlan<
6098
+ options?: CreateTableOptions
6099
+ ) => QueryPlan<
6887
6100
  {},
6888
6101
  never,
6889
6102
  {},
@@ -6894,40 +6107,12 @@ type AsCurriedResult<
6894
6107
  TrueFormula,
6895
6108
  "ddl",
6896
6109
  "createTable"
6897
- > => {
6898
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6899
- return makePlan({
6900
- selection: {},
6901
- required: [] as never,
6902
- available: {},
6903
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
6904
- }, {
6905
- kind: "createTable",
6906
- select: {},
6907
- target: {
6908
- kind: "from",
6909
- tableName: sourceName,
6910
- baseTableName: sourceBaseName,
6911
- source: target
6912
- },
6913
- ddl: {
6914
- kind: "createTable",
6915
- ifNotExists: options.ifNotExists ?? false
6916
- },
6917
- where: [],
6918
- having: [],
6919
- joins: [],
6920
- groupBy: [],
6921
- orderBy: []
6922
- }, undefined as unknown as TrueFormula, "ddl", "createTable")
6923
- }
6110
+ >
6924
6111
 
6925
- const dropTable = <
6926
- Target extends SchemaTableLike
6927
- >(
6112
+ type DropTableApi = <Target extends SchemaTableLike>(
6928
6113
  target: Target,
6929
- options: DropTableOptions = {}
6930
- ): QueryPlan<
6114
+ options?: DropTableOptions
6115
+ ) => QueryPlan<
6931
6116
  {},
6932
6117
  never,
6933
6118
  {},
@@ -6938,42 +6123,13 @@ type AsCurriedResult<
6938
6123
  TrueFormula,
6939
6124
  "ddl",
6940
6125
  "dropTable"
6941
- > => {
6942
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6943
- return makePlan({
6944
- selection: {},
6945
- required: [] as never,
6946
- available: {},
6947
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
6948
- }, {
6949
- kind: "dropTable",
6950
- select: {},
6951
- target: {
6952
- kind: "from",
6953
- tableName: sourceName,
6954
- baseTableName: sourceBaseName,
6955
- source: target
6956
- },
6957
- ddl: {
6958
- kind: "dropTable",
6959
- ifExists: options.ifExists ?? false
6960
- },
6961
- where: [],
6962
- having: [],
6963
- joins: [],
6964
- groupBy: [],
6965
- orderBy: []
6966
- }, undefined as unknown as TrueFormula, "ddl", "dropTable")
6967
- }
6126
+ >
6968
6127
 
6969
- const createIndex = <
6970
- Target extends SchemaTableLike,
6971
- const Columns extends DdlColumnInput
6972
- >(
6128
+ type CreateIndexApi = <Target extends SchemaTableLike, const Columns extends DdlColumnInput>(
6973
6129
  target: Target,
6974
6130
  columns: Columns & ValidateDdlColumns<Target, NormalizeDdlColumns<Columns>>,
6975
- options: CreateIndexOptions = {}
6976
- ): QueryPlan<
6131
+ options?: CreateIndexOptions
6132
+ ) => QueryPlan<
6977
6133
  {},
6978
6134
  never,
6979
6135
  {},
@@ -6984,46 +6140,13 @@ type AsCurriedResult<
6984
6140
  TrueFormula,
6985
6141
  "ddl",
6986
6142
  "createIndex"
6987
- > => {
6988
- const normalizedColumns = normalizeColumnList(columns as string | readonly string[])
6989
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6990
- return makePlan({
6991
- selection: {},
6992
- required: [] as never,
6993
- available: {},
6994
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
6995
- }, {
6996
- kind: "createIndex",
6997
- select: {},
6998
- target: {
6999
- kind: "from",
7000
- tableName: sourceName,
7001
- baseTableName: sourceBaseName,
7002
- source: target
7003
- },
7004
- ddl: {
7005
- kind: "createIndex",
7006
- name: options.name ?? defaultIndexName(sourceBaseName, normalizedColumns, options.unique ?? false),
7007
- columns: normalizedColumns,
7008
- unique: options.unique ?? false,
7009
- ifNotExists: options.ifNotExists ?? false
7010
- },
7011
- where: [],
7012
- having: [],
7013
- joins: [],
7014
- groupBy: [],
7015
- orderBy: []
7016
- }, undefined as unknown as TrueFormula, "ddl", "createIndex")
7017
- }
6143
+ >
7018
6144
 
7019
- const dropIndex = <
7020
- Target extends SchemaTableLike,
7021
- const Columns extends DdlColumnInput
7022
- >(
6145
+ type DropIndexApi = <Target extends SchemaTableLike, const Columns extends DdlColumnInput>(
7023
6146
  target: Target,
7024
6147
  columns: Columns & ValidateDdlColumns<Target, NormalizeDdlColumns<Columns>>,
7025
- options: DropIndexOptions = {}
7026
- ): QueryPlan<
6148
+ options?: DropIndexOptions
6149
+ ) => QueryPlan<
7027
6150
  {},
7028
6151
  never,
7029
6152
  {},
@@ -7034,107 +6157,9 @@ type AsCurriedResult<
7034
6157
  TrueFormula,
7035
6158
  "ddl",
7036
6159
  "dropIndex"
7037
- > => {
7038
- const normalizedColumns = normalizeColumnList(columns as string | readonly string[])
7039
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
7040
- return makePlan({
7041
- selection: {},
7042
- required: [] as never,
7043
- available: {},
7044
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
7045
- }, {
7046
- kind: "dropIndex",
7047
- select: {},
7048
- target: {
7049
- kind: "from",
7050
- tableName: sourceName,
7051
- baseTableName: sourceBaseName,
7052
- source: target
7053
- },
7054
- ddl: {
7055
- kind: "dropIndex",
7056
- name: options.name ?? defaultIndexName(sourceBaseName, normalizedColumns, false),
7057
- ifExists: options.ifExists ?? false
7058
- },
7059
- where: [],
7060
- having: [],
7061
- joins: [],
7062
- groupBy: [],
7063
- orderBy: []
7064
- }, undefined as unknown as TrueFormula, "ddl", "dropIndex")
7065
- }
6160
+ >
7066
6161
 
7067
- const api = {
7068
- literal,
7069
- column,
7070
- cast,
7071
- type,
7072
- json,
7073
- jsonb,
7074
- eq,
7075
- neq,
7076
- lt,
7077
- lte,
7078
- gt,
7079
- gte,
7080
- isNull,
7081
- isNotNull,
7082
- upper,
7083
- lower,
7084
- like,
7085
- ilike,
7086
- regexMatch,
7087
- regexIMatch,
7088
- regexNotMatch,
7089
- regexNotIMatch,
7090
- and,
7091
- or,
7092
- not,
7093
- all: all_,
7094
- any: any_,
7095
- case: case_,
7096
- match,
7097
- coalesce,
7098
- call,
7099
- uuidGenerateV4,
7100
- nextVal,
7101
- in: in_,
7102
- notIn,
7103
- between,
7104
- contains,
7105
- containedBy,
7106
- overlaps,
7107
- concat,
7108
- exists,
7109
- over,
7110
- rowNumber,
7111
- rank,
7112
- denseRank,
7113
- count,
7114
- max,
7115
- min,
7116
- isDistinctFrom,
7117
- isNotDistinctFrom,
7118
- excluded,
7119
- as,
7120
- with: with_,
7121
- withRecursive: withRecursive_,
7122
- lateral,
7123
- scalar,
7124
- inSubquery,
7125
- compareAny,
7126
- compareAll,
7127
- values,
7128
- unnest,
7129
- generateSeries,
7130
- returning,
7131
- onConflict,
7132
- insert,
7133
- update,
7134
- upsert,
7135
- delete: delete_,
7136
- truncate,
7137
- merge,
6162
+ const {
7138
6163
  transaction,
7139
6164
  commit,
7140
6165
  rollback,
@@ -7144,30 +6169,161 @@ type AsCurriedResult<
7144
6169
  createTable,
7145
6170
  dropTable,
7146
6171
  createIndex,
7147
- dropIndex,
7148
- union,
7149
- unionAll,
7150
- intersect,
7151
- intersectAll,
7152
- except,
7153
- exceptAll,
6172
+ dropIndex
6173
+ } = makeDslTransactionDdlRuntime({
6174
+ profile,
6175
+ makePlan,
6176
+ targetSourceDetails,
6177
+ normalizeColumnList,
6178
+ defaultIndexName
6179
+ }) as {
6180
+ readonly transaction: TransactionApi
6181
+ readonly commit: CommitApi
6182
+ readonly rollback: RollbackApi
6183
+ readonly savepoint: SavepointApi
6184
+ readonly rollbackTo: RollbackToApi
6185
+ readonly releaseSavepoint: ReleaseSavepointApi
6186
+ readonly createTable: CreateTableApi
6187
+ readonly dropTable: DropTableApi
6188
+ readonly createIndex: CreateIndexApi
6189
+ readonly dropIndex: DropIndexApi
6190
+ }
6191
+
6192
+ const {
6193
+ values: exportedValues,
6194
+ unnest: exportedUnnest,
6195
+ select: exportedSelect,
6196
+ from: exportedFrom,
6197
+ insert: exportedInsert
6198
+ } = {
6199
+ values,
6200
+ unnest,
7154
6201
  select,
7155
- where,
7156
- having,
7157
6202
  from,
7158
- innerJoin,
7159
- leftJoin,
7160
- rightJoin,
7161
- fullJoin,
7162
- crossJoin,
7163
- distinct,
7164
- distinctOn,
7165
- limit,
7166
- offset,
7167
- lock,
7168
- orderBy,
7169
- groupBy
6203
+ insert
6204
+ } as {
6205
+ readonly values: ValuesApi
6206
+ readonly unnest: UnnestApi
6207
+ readonly select: SelectApi
6208
+ readonly from: FromApi
6209
+ readonly insert: InsertApi
7170
6210
  }
7171
6211
 
7172
- return api
7173
- })()
6212
+ export const mysqlDsl = {
6213
+ values: exportedValues,
6214
+ unnest: exportedUnnest,
6215
+ select: exportedSelect,
6216
+ from: exportedFrom,
6217
+ insert: exportedInsert
6218
+ } as {
6219
+ readonly values: ValuesApi
6220
+ readonly unnest: UnnestApi
6221
+ readonly select: SelectApi
6222
+ readonly from: FromApi
6223
+ readonly insert: InsertApi
6224
+ }
6225
+
6226
+ export {
6227
+ literal,
6228
+ column,
6229
+ cast,
6230
+ type,
6231
+ json,
6232
+ jsonb,
6233
+ eq,
6234
+ neq,
6235
+ lt,
6236
+ lte,
6237
+ gt,
6238
+ gte,
6239
+ isNull,
6240
+ isNotNull,
6241
+ upper,
6242
+ lower,
6243
+ like,
6244
+ ilike,
6245
+ regexMatch,
6246
+ regexIMatch,
6247
+ regexNotMatch,
6248
+ regexNotIMatch,
6249
+ and,
6250
+ or,
6251
+ not,
6252
+ all_ as all,
6253
+ any_ as any,
6254
+ case_,
6255
+ match,
6256
+ coalesce,
6257
+ call,
6258
+ uuidGenerateV4,
6259
+ nextVal,
6260
+ in_,
6261
+ notIn,
6262
+ between,
6263
+ contains,
6264
+ containedBy,
6265
+ overlaps,
6266
+ concat,
6267
+ exists,
6268
+ over,
6269
+ rowNumber,
6270
+ rank,
6271
+ denseRank,
6272
+ count,
6273
+ max,
6274
+ min,
6275
+ isDistinctFrom,
6276
+ isNotDistinctFrom,
6277
+ excluded,
6278
+ as,
6279
+ with_,
6280
+ withRecursive_ as withRecursive,
6281
+ lateral,
6282
+ scalar,
6283
+ inSubquery,
6284
+ compareAny,
6285
+ compareAll,
6286
+ exportedValues as values,
6287
+ exportedUnnest as unnest,
6288
+ generateSeries,
6289
+ returning,
6290
+ onConflict,
6291
+ exportedInsert as insert,
6292
+ update,
6293
+ upsert,
6294
+ delete_,
6295
+ truncate,
6296
+ merge,
6297
+ transaction,
6298
+ commit,
6299
+ rollback,
6300
+ savepoint,
6301
+ rollbackTo,
6302
+ releaseSavepoint,
6303
+ createTable,
6304
+ dropTable,
6305
+ createIndex,
6306
+ dropIndex,
6307
+ union,
6308
+ unionAll,
6309
+ intersect,
6310
+ intersectAll,
6311
+ except,
6312
+ exceptAll,
6313
+ exportedSelect as select,
6314
+ where,
6315
+ having,
6316
+ exportedFrom as from,
6317
+ innerJoin,
6318
+ leftJoin,
6319
+ rightJoin,
6320
+ fullJoin,
6321
+ crossJoin,
6322
+ distinct,
6323
+ distinctOn,
6324
+ limit,
6325
+ offset,
6326
+ lock,
6327
+ orderBy,
6328
+ groupBy
6329
+ }