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.
- package/dist/mysql.js +61555 -4252
- package/dist/postgres/metadata.js +728 -104
- package/dist/postgres.js +6906 -4023
- package/package.json +15 -2
- package/src/internal/aggregation-validation.ts +3 -3
- package/src/internal/case-analysis.d.ts +18 -0
- package/src/internal/case-analysis.ts +4 -4
- package/src/internal/coercion/analysis.d.ts +7 -0
- package/src/internal/{coercion-analysis.ts → coercion/analysis.ts} +3 -3
- package/src/internal/coercion/errors.d.ts +17 -0
- package/src/internal/{coercion-errors.ts → coercion/errors.ts} +1 -1
- package/src/internal/coercion/kind.d.ts +4 -0
- package/src/internal/{coercion-kind.ts → coercion/kind.ts} +2 -2
- package/src/internal/{coercion-normalize.ts → coercion/normalize.ts} +1 -1
- package/src/internal/coercion/rules.d.ts +6 -0
- package/src/internal/{coercion-rules.ts → coercion/rules.ts} +2 -2
- package/src/internal/column-state.d.ts +190 -0
- package/src/internal/column-state.ts +43 -47
- package/src/internal/column.ts +43 -305
- package/src/internal/datatypes/define.d.ts +17 -0
- package/src/internal/datatypes/define.ts +18 -4
- package/src/internal/datatypes/lookup.d.ts +44 -0
- package/src/internal/datatypes/lookup.ts +61 -152
- package/src/internal/datatypes/shape.d.ts +16 -0
- package/src/internal/datatypes/shape.ts +1 -1
- package/src/internal/derived-table.d.ts +4 -0
- package/src/internal/derived-table.ts +21 -16
- package/src/internal/dialect.ts +12 -1
- package/src/internal/dsl-mutation-runtime.ts +378 -0
- package/src/internal/dsl-plan-runtime.ts +387 -0
- package/src/internal/dsl-query-runtime.ts +160 -0
- package/src/internal/dsl-transaction-ddl-runtime.ts +263 -0
- package/src/internal/executor.ts +146 -34
- package/src/internal/expression-ast.ts +15 -5
- package/src/internal/grouping-key.d.ts +3 -0
- package/src/internal/grouping-key.ts +1 -1
- package/src/internal/implication-runtime.d.ts +15 -0
- package/src/internal/implication-runtime.ts +4 -4
- package/src/internal/json/ast.d.ts +30 -0
- package/src/internal/json/ast.ts +1 -1
- package/src/internal/json/errors.d.ts +8 -0
- package/src/internal/json/path.d.ts +75 -0
- package/src/internal/json/path.ts +1 -1
- package/src/internal/json/types.d.ts +62 -0
- package/src/internal/predicate/analysis.d.ts +20 -0
- package/src/internal/predicate/analysis.ts +183 -0
- package/src/internal/predicate/atom.d.ts +28 -0
- package/src/internal/{predicate-atom.ts → predicate/atom.ts} +7 -0
- package/src/internal/{predicate-branches.ts → predicate/branches.ts} +2 -2
- package/src/internal/predicate/context.d.ts +67 -0
- package/src/internal/{predicate-context.ts → predicate/context.ts} +163 -20
- package/src/internal/predicate/formula.d.ts +35 -0
- package/src/internal/{predicate-formula.ts → predicate/formula.ts} +1 -1
- package/src/internal/predicate/key.d.ts +11 -0
- package/src/internal/predicate/key.ts +73 -0
- package/src/internal/{predicate-nnf.ts → predicate/nnf.ts} +2 -2
- package/src/internal/predicate/normalize.d.ts +53 -0
- package/src/internal/{predicate-normalize.ts → predicate/normalize.ts} +130 -49
- package/src/internal/predicate/runtime.d.ts +31 -0
- package/src/internal/{predicate-runtime.ts → predicate/runtime.ts} +127 -17
- package/src/internal/projection-alias.d.ts +13 -0
- package/src/internal/projections.d.ts +31 -0
- package/src/internal/projections.ts +1 -1
- package/src/internal/query-ast.d.ts +217 -0
- package/src/internal/query-ast.ts +1 -1
- package/src/internal/query-requirements.d.ts +20 -0
- package/src/internal/query.d.ts +775 -0
- package/src/internal/query.ts +683 -369
- package/src/internal/renderer.ts +11 -21
- package/src/internal/row-set.d.ts +53 -0
- package/src/internal/{plan.ts → row-set.ts} +11 -9
- package/src/internal/runtime/driver-value-mapping.ts +186 -0
- package/src/internal/{runtime-normalize.ts → runtime/normalize.ts} +9 -31
- package/src/internal/{runtime-schema.ts → runtime/schema.ts} +13 -38
- package/src/internal/runtime/value.d.ts +22 -0
- package/src/internal/{runtime-value.ts → runtime/value.ts} +2 -2
- package/src/internal/scalar.d.ts +107 -0
- package/src/internal/scalar.ts +202 -0
- package/src/internal/schema-derivation.d.ts +105 -0
- package/src/internal/schema-expression.d.ts +18 -0
- package/src/internal/schema-expression.ts +38 -7
- package/src/internal/table-options.d.ts +94 -0
- package/src/internal/table-options.ts +8 -2
- package/src/internal/table.d.ts +173 -0
- package/src/internal/table.ts +32 -14
- package/src/mysql/column.ts +95 -18
- package/src/mysql/datatypes/index.ts +47 -7
- package/src/mysql/errors/generated.ts +57336 -0
- package/src/mysql/errors/index.ts +1 -0
- package/src/mysql/errors/normalize.ts +55 -53
- package/src/mysql/errors/types.ts +74 -0
- package/src/mysql/executor.ts +88 -11
- package/src/mysql/function/aggregate.ts +1 -5
- package/src/mysql/function/core.ts +1 -4
- package/src/mysql/function/index.ts +0 -1
- package/src/mysql/function/string.ts +1 -5
- package/src/mysql/function/temporal.ts +12 -15
- package/src/mysql/function/window.ts +1 -6
- package/src/{internal/mysql-dialect.ts → mysql/internal/dialect.ts} +12 -6
- package/src/{internal/mysql-query.ts → mysql/internal/dsl.ts} +1299 -2143
- package/src/mysql/internal/renderer.ts +46 -0
- package/src/mysql/internal/sql-expression-renderer.ts +1501 -0
- package/src/mysql/json.ts +2 -0
- package/src/mysql/query.ts +111 -91
- package/src/mysql/renderer.ts +8 -3
- package/src/mysql/table.ts +1 -1
- package/src/mysql.ts +6 -4
- package/src/postgres/cast.ts +30 -16
- package/src/postgres/column.ts +179 -46
- package/src/postgres/datatypes/index.d.ts +515 -0
- package/src/postgres/datatypes/index.ts +22 -13
- package/src/postgres/datatypes/spec.d.ts +412 -0
- package/src/postgres/errors/generated.ts +2636 -0
- package/src/postgres/errors/index.ts +1 -0
- package/src/postgres/errors/normalize.ts +47 -62
- package/src/postgres/errors/types.ts +92 -34
- package/src/postgres/executor.ts +54 -7
- package/src/postgres/function/aggregate.ts +1 -5
- package/src/postgres/function/core.ts +12 -6
- package/src/postgres/function/index.ts +0 -1
- package/src/postgres/function/string.ts +1 -5
- package/src/postgres/function/temporal.ts +12 -15
- package/src/postgres/function/window.ts +1 -6
- package/src/{internal/postgres-dialect.ts → postgres/internal/dialect.ts} +12 -6
- package/src/{internal/postgres-query.ts → postgres/internal/dsl.ts} +1356 -2133
- package/src/{internal/postgres-renderer.ts → postgres/internal/renderer.ts} +17 -8
- package/src/postgres/internal/schema-ddl.ts +108 -0
- package/src/{internal/postgres-schema-model.ts → postgres/internal/schema-model.ts} +12 -6
- package/src/{internal → postgres/internal}/sql-expression-renderer.ts +79 -25
- package/src/postgres/{function/json.ts → json.ts} +77 -85
- package/src/postgres/metadata.ts +2 -2
- package/src/postgres/query.ts +113 -89
- package/src/postgres/renderer.ts +8 -13
- package/src/postgres/schema-expression.ts +2 -1
- package/src/postgres/schema-management.ts +1 -1
- package/src/postgres/table.ts +12 -4
- package/src/postgres/type.ts +33 -2
- package/src/postgres.ts +6 -4
- package/src/internal/expression.ts +0 -327
- package/src/internal/mysql-renderer.ts +0 -37
- package/src/internal/predicate-analysis.ts +0 -81
- package/src/internal/predicate-key.ts +0 -28
- package/src/internal/schema-ddl.ts +0 -55
- package/src/mysql/function/json.ts +0 -4
- package/src/mysql/private/query.ts +0 -1
- package/src/postgres/private/query.ts +0 -1
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import * as Expression from "./scalar.js"
|
|
2
|
+
import * as Plan from "./row-set.js"
|
|
3
|
+
|
|
4
|
+
type DslMutationRuntimeContext = {
|
|
5
|
+
readonly makePlan: (...args: readonly any[]) => any
|
|
6
|
+
readonly getAst: (plan: any) => any
|
|
7
|
+
readonly getQueryState: (plan: any) => any
|
|
8
|
+
readonly currentRequiredList: (required: any) => readonly string[]
|
|
9
|
+
readonly toDialectExpression: (value: any) => Expression.Any
|
|
10
|
+
readonly buildMutationAssignments: (target: any, values: Record<string, unknown>) => readonly any[]
|
|
11
|
+
readonly buildInsertValuesRows: (target: any, rows: readonly [Record<string, unknown>, ...Record<string, unknown>[]]) => any
|
|
12
|
+
readonly normalizeInsertUnnestValues: (target: any, values: any) => any
|
|
13
|
+
readonly normalizeInsertSelectColumns: (selection: Record<string, Expression.Any>) => readonly string[]
|
|
14
|
+
readonly buildConflictTarget: (target: any, input: any) => any
|
|
15
|
+
readonly mutationTargetClauses: (target: any) => readonly any[]
|
|
16
|
+
readonly mutationAvailableSources: (target: any) => Record<string, any>
|
|
17
|
+
readonly normalizeColumnList: (columns: string | readonly string[]) => readonly string[]
|
|
18
|
+
readonly targetSourceDetails: (target: any) => { readonly sourceName: string; readonly sourceBaseName: string }
|
|
19
|
+
readonly sourceDetails: (source: any) => { readonly sourceName: string; readonly sourceBaseName: string }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const makeDslMutationRuntime = (ctx: DslMutationRuntimeContext) => {
|
|
23
|
+
const insert = (target: any, values?: Record<string, unknown>) => {
|
|
24
|
+
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target)
|
|
25
|
+
const assignments = values === undefined
|
|
26
|
+
? []
|
|
27
|
+
: ctx.buildMutationAssignments(target, values)
|
|
28
|
+
const required = assignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies))
|
|
29
|
+
const insertState = values === undefined ? "missing" : "ready"
|
|
30
|
+
return ctx.makePlan({
|
|
31
|
+
selection: {},
|
|
32
|
+
required: required.filter((name, index, list) => name !== sourceName && list.indexOf(name) === index),
|
|
33
|
+
available: {
|
|
34
|
+
[sourceName]: {
|
|
35
|
+
name: sourceName,
|
|
36
|
+
mode: "required",
|
|
37
|
+
baseName: sourceBaseName
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
dialect: target[Plan.TypeId].dialect
|
|
41
|
+
}, {
|
|
42
|
+
kind: "insert",
|
|
43
|
+
select: {},
|
|
44
|
+
into: {
|
|
45
|
+
kind: "from",
|
|
46
|
+
tableName: sourceName,
|
|
47
|
+
baseTableName: sourceBaseName,
|
|
48
|
+
source: target
|
|
49
|
+
},
|
|
50
|
+
values: assignments,
|
|
51
|
+
conflict: undefined,
|
|
52
|
+
where: [],
|
|
53
|
+
having: [],
|
|
54
|
+
joins: [],
|
|
55
|
+
groupBy: [],
|
|
56
|
+
orderBy: []
|
|
57
|
+
}, undefined, "write", "insert", target, insertState)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const attachInsertSource = (plan: any, source: any) => {
|
|
61
|
+
const current = plan[Plan.TypeId]
|
|
62
|
+
const currentAst = ctx.getAst(plan)
|
|
63
|
+
const currentQuery = ctx.getQueryState(plan)
|
|
64
|
+
const target = currentQuery.target
|
|
65
|
+
const sourceName = currentAst.into!.tableName
|
|
66
|
+
|
|
67
|
+
if (typeof source === "object" && source !== null && "kind" in source && source.kind === "values") {
|
|
68
|
+
const normalized = ctx.buildInsertValuesRows(target, source.rows)
|
|
69
|
+
return ctx.makePlan({
|
|
70
|
+
selection: current.selection,
|
|
71
|
+
required: normalized.required.filter((name: string) => name !== sourceName),
|
|
72
|
+
available: current.available,
|
|
73
|
+
dialect: current.dialect
|
|
74
|
+
}, {
|
|
75
|
+
...currentAst,
|
|
76
|
+
values: [],
|
|
77
|
+
insertSource: {
|
|
78
|
+
kind: "values",
|
|
79
|
+
columns: normalized.columns,
|
|
80
|
+
rows: normalized.rows
|
|
81
|
+
}
|
|
82
|
+
}, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, "ready")
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (typeof source === "object" && source !== null && "kind" in source && source.kind === "unnest") {
|
|
86
|
+
const normalized = ctx.normalizeInsertUnnestValues(target, source.values)
|
|
87
|
+
return ctx.makePlan({
|
|
88
|
+
selection: current.selection,
|
|
89
|
+
required: [],
|
|
90
|
+
available: current.available,
|
|
91
|
+
dialect: current.dialect
|
|
92
|
+
}, {
|
|
93
|
+
...currentAst,
|
|
94
|
+
values: [],
|
|
95
|
+
insertSource: {
|
|
96
|
+
kind: "unnest",
|
|
97
|
+
columns: normalized.columns,
|
|
98
|
+
values: normalized.values
|
|
99
|
+
}
|
|
100
|
+
}, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, "ready")
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const sourcePlan = source
|
|
104
|
+
const selection = sourcePlan[Plan.TypeId].selection as Record<string, Expression.Any>
|
|
105
|
+
const columns = ctx.normalizeInsertSelectColumns(selection)
|
|
106
|
+
return ctx.makePlan({
|
|
107
|
+
selection: current.selection,
|
|
108
|
+
required: ctx.currentRequiredList(sourcePlan[Plan.TypeId].required).filter((name) => name !== sourceName),
|
|
109
|
+
available: current.available,
|
|
110
|
+
dialect: current.dialect
|
|
111
|
+
}, {
|
|
112
|
+
...currentAst,
|
|
113
|
+
values: [],
|
|
114
|
+
insertSource: {
|
|
115
|
+
kind: "query",
|
|
116
|
+
columns,
|
|
117
|
+
query: sourcePlan
|
|
118
|
+
}
|
|
119
|
+
}, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, "ready")
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const onConflict = (target: any, options: any = {}) =>
|
|
123
|
+
(plan: any) => {
|
|
124
|
+
const current = plan[Plan.TypeId]
|
|
125
|
+
const currentAst = ctx.getAst(plan)
|
|
126
|
+
const currentQuery = ctx.getQueryState(plan)
|
|
127
|
+
const insertTarget = currentAst.into!.source
|
|
128
|
+
const conflictTarget = ctx.buildConflictTarget(insertTarget, target)
|
|
129
|
+
const updateAssignments = options.update
|
|
130
|
+
? ctx.buildMutationAssignments(insertTarget, options.update)
|
|
131
|
+
: []
|
|
132
|
+
const updateWhere = options.where === undefined
|
|
133
|
+
? undefined
|
|
134
|
+
: ctx.toDialectExpression(options.where)
|
|
135
|
+
const required = [
|
|
136
|
+
...ctx.currentRequiredList(current.required),
|
|
137
|
+
...updateAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
|
|
138
|
+
...(updateWhere ? Object.keys(updateWhere[Expression.TypeId].dependencies) : [])
|
|
139
|
+
].filter((name, index, list) =>
|
|
140
|
+
!(name in current.available) && list.indexOf(name) === index)
|
|
141
|
+
return ctx.makePlan({
|
|
142
|
+
selection: current.selection,
|
|
143
|
+
required,
|
|
144
|
+
available: current.available,
|
|
145
|
+
dialect: current.dialect
|
|
146
|
+
}, {
|
|
147
|
+
...currentAst,
|
|
148
|
+
conflict: {
|
|
149
|
+
kind: "conflict",
|
|
150
|
+
target: conflictTarget,
|
|
151
|
+
action: updateAssignments.length === 0 ? "doNothing" : "doUpdate",
|
|
152
|
+
values: updateAssignments.length === 0 ? undefined : updateAssignments,
|
|
153
|
+
where: updateWhere
|
|
154
|
+
}
|
|
155
|
+
}, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, currentQuery.insertSource)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const update = (target: any, values: Record<string, unknown>) => {
|
|
159
|
+
const targets = ctx.mutationTargetClauses(target)
|
|
160
|
+
const primaryTarget = targets[0]!
|
|
161
|
+
const assignments = ctx.buildMutationAssignments(target, values)
|
|
162
|
+
const targetNames = new Set(targets.map((entry: any) => entry.tableName))
|
|
163
|
+
const required = assignments
|
|
164
|
+
.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies))
|
|
165
|
+
.filter((name, index, list) => !targetNames.has(name) && list.indexOf(name) === index)
|
|
166
|
+
return ctx.makePlan({
|
|
167
|
+
selection: {},
|
|
168
|
+
required,
|
|
169
|
+
available: ctx.mutationAvailableSources(target),
|
|
170
|
+
dialect: primaryTarget.source[Plan.TypeId].dialect
|
|
171
|
+
}, {
|
|
172
|
+
kind: "update",
|
|
173
|
+
select: {},
|
|
174
|
+
target: primaryTarget,
|
|
175
|
+
targets,
|
|
176
|
+
set: assignments,
|
|
177
|
+
where: [],
|
|
178
|
+
having: [],
|
|
179
|
+
joins: [],
|
|
180
|
+
groupBy: [],
|
|
181
|
+
orderBy: []
|
|
182
|
+
}, undefined, "write", "update")
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const upsert = (target: any, values: Record<string, unknown>, conflictColumns: string | readonly string[], updateValues?: Record<string, unknown>) => {
|
|
186
|
+
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target)
|
|
187
|
+
const assignments = ctx.buildMutationAssignments(target, values)
|
|
188
|
+
const updateAssignments = updateValues ? ctx.buildMutationAssignments(target, updateValues) : []
|
|
189
|
+
const required = [
|
|
190
|
+
...assignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
|
|
191
|
+
...updateAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies))
|
|
192
|
+
]
|
|
193
|
+
return ctx.makePlan({
|
|
194
|
+
selection: {},
|
|
195
|
+
required: required.filter((name, index, list) => name !== sourceName && list.indexOf(name) === index),
|
|
196
|
+
available: {
|
|
197
|
+
[sourceName]: {
|
|
198
|
+
name: sourceName,
|
|
199
|
+
mode: "required",
|
|
200
|
+
baseName: sourceBaseName
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
dialect: target[Plan.TypeId].dialect
|
|
204
|
+
}, {
|
|
205
|
+
kind: "insert",
|
|
206
|
+
select: {},
|
|
207
|
+
into: {
|
|
208
|
+
kind: "from",
|
|
209
|
+
tableName: sourceName,
|
|
210
|
+
baseTableName: sourceBaseName,
|
|
211
|
+
source: target
|
|
212
|
+
},
|
|
213
|
+
values: assignments,
|
|
214
|
+
conflict: {
|
|
215
|
+
kind: "conflict",
|
|
216
|
+
target: {
|
|
217
|
+
kind: "columns",
|
|
218
|
+
columns: ctx.normalizeColumnList(conflictColumns) as readonly [string, ...string[]]
|
|
219
|
+
},
|
|
220
|
+
action: updateAssignments.length > 0 ? "doUpdate" : "doNothing",
|
|
221
|
+
values: updateAssignments.length > 0 ? updateAssignments : undefined
|
|
222
|
+
},
|
|
223
|
+
where: [],
|
|
224
|
+
having: [],
|
|
225
|
+
joins: [],
|
|
226
|
+
groupBy: [],
|
|
227
|
+
orderBy: []
|
|
228
|
+
}, undefined, "write", "insert", target, "ready")
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const delete_ = (target: any) => {
|
|
232
|
+
const targets = ctx.mutationTargetClauses(target)
|
|
233
|
+
const primaryTarget = targets[0]!
|
|
234
|
+
return ctx.makePlan({
|
|
235
|
+
selection: {},
|
|
236
|
+
required: [],
|
|
237
|
+
available: ctx.mutationAvailableSources(target),
|
|
238
|
+
dialect: primaryTarget.source[Plan.TypeId].dialect
|
|
239
|
+
}, {
|
|
240
|
+
kind: "delete",
|
|
241
|
+
select: {},
|
|
242
|
+
target: primaryTarget,
|
|
243
|
+
targets,
|
|
244
|
+
where: [],
|
|
245
|
+
having: [],
|
|
246
|
+
joins: [],
|
|
247
|
+
groupBy: [],
|
|
248
|
+
orderBy: []
|
|
249
|
+
}, undefined, "write", "delete")
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const truncate = (target: any, options: { readonly restartIdentity?: boolean; readonly cascade?: boolean } = {}) => {
|
|
253
|
+
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target)
|
|
254
|
+
return ctx.makePlan({
|
|
255
|
+
selection: {},
|
|
256
|
+
required: [],
|
|
257
|
+
available: {},
|
|
258
|
+
dialect: target[Plan.TypeId].dialect
|
|
259
|
+
}, {
|
|
260
|
+
kind: "truncate",
|
|
261
|
+
select: {},
|
|
262
|
+
target: {
|
|
263
|
+
kind: "from",
|
|
264
|
+
tableName: sourceName,
|
|
265
|
+
baseTableName: sourceBaseName,
|
|
266
|
+
source: target
|
|
267
|
+
},
|
|
268
|
+
truncate: {
|
|
269
|
+
kind: "truncate",
|
|
270
|
+
restartIdentity: options.restartIdentity ?? false,
|
|
271
|
+
cascade: options.cascade ?? false
|
|
272
|
+
},
|
|
273
|
+
where: [],
|
|
274
|
+
having: [],
|
|
275
|
+
joins: [],
|
|
276
|
+
groupBy: [],
|
|
277
|
+
orderBy: []
|
|
278
|
+
}, undefined, "write", "truncate")
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const merge = (target: any, source: any, on: any, options: any = {}) => {
|
|
282
|
+
const { sourceName: targetName, sourceBaseName: targetBaseName } = ctx.targetSourceDetails(target)
|
|
283
|
+
const { sourceName: usingName, sourceBaseName: usingBaseName } = ctx.sourceDetails(source)
|
|
284
|
+
const onExpression = ctx.toDialectExpression(on)
|
|
285
|
+
const matched = options.whenMatched
|
|
286
|
+
const notMatched = options.whenNotMatched
|
|
287
|
+
if (matched && "delete" in matched && "update" in matched) {
|
|
288
|
+
throw new Error("merge whenMatched cannot specify both update and delete")
|
|
289
|
+
}
|
|
290
|
+
const matchedPredicate = matched?.predicate ? ctx.toDialectExpression(matched.predicate) : undefined
|
|
291
|
+
const matchedAssignments = matched && "update" in matched && matched.update
|
|
292
|
+
? ctx.buildMutationAssignments(target, matched.update)
|
|
293
|
+
: []
|
|
294
|
+
const notMatchedPredicate = notMatched?.predicate ? ctx.toDialectExpression(notMatched.predicate) : undefined
|
|
295
|
+
const notMatchedAssignments = notMatched
|
|
296
|
+
? ctx.buildMutationAssignments(target, notMatched.values)
|
|
297
|
+
: []
|
|
298
|
+
const required = [
|
|
299
|
+
...Object.keys(onExpression[Expression.TypeId].dependencies),
|
|
300
|
+
...matchedAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
|
|
301
|
+
...notMatchedAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
|
|
302
|
+
...(matchedPredicate ? Object.keys(matchedPredicate[Expression.TypeId].dependencies) : []),
|
|
303
|
+
...(notMatchedPredicate ? Object.keys(notMatchedPredicate[Expression.TypeId].dependencies) : [])
|
|
304
|
+
].filter((name, index, values) =>
|
|
305
|
+
name !== targetName && name !== usingName && values.indexOf(name) === index)
|
|
306
|
+
return ctx.makePlan({
|
|
307
|
+
selection: {},
|
|
308
|
+
required,
|
|
309
|
+
available: {
|
|
310
|
+
[targetName]: {
|
|
311
|
+
name: targetName,
|
|
312
|
+
mode: "required",
|
|
313
|
+
baseName: targetBaseName
|
|
314
|
+
},
|
|
315
|
+
[usingName]: {
|
|
316
|
+
name: usingName,
|
|
317
|
+
mode: "required",
|
|
318
|
+
baseName: usingBaseName
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
dialect: target[Plan.TypeId].dialect
|
|
322
|
+
}, {
|
|
323
|
+
kind: "merge",
|
|
324
|
+
select: {},
|
|
325
|
+
target: {
|
|
326
|
+
kind: "from",
|
|
327
|
+
tableName: targetName,
|
|
328
|
+
baseTableName: targetBaseName,
|
|
329
|
+
source: target
|
|
330
|
+
},
|
|
331
|
+
using: {
|
|
332
|
+
kind: "from",
|
|
333
|
+
tableName: usingName,
|
|
334
|
+
baseTableName: usingBaseName,
|
|
335
|
+
source
|
|
336
|
+
},
|
|
337
|
+
merge: {
|
|
338
|
+
kind: "merge",
|
|
339
|
+
on: onExpression,
|
|
340
|
+
whenMatched: matched
|
|
341
|
+
? ("delete" in matched && matched.delete
|
|
342
|
+
? {
|
|
343
|
+
kind: "delete",
|
|
344
|
+
predicate: matchedPredicate
|
|
345
|
+
}
|
|
346
|
+
: {
|
|
347
|
+
kind: "update",
|
|
348
|
+
values: matchedAssignments,
|
|
349
|
+
predicate: matchedPredicate
|
|
350
|
+
})
|
|
351
|
+
: undefined,
|
|
352
|
+
whenNotMatched: notMatched
|
|
353
|
+
? {
|
|
354
|
+
kind: "insert",
|
|
355
|
+
values: notMatchedAssignments,
|
|
356
|
+
predicate: notMatchedPredicate
|
|
357
|
+
}
|
|
358
|
+
: undefined
|
|
359
|
+
},
|
|
360
|
+
where: [],
|
|
361
|
+
having: [],
|
|
362
|
+
joins: [],
|
|
363
|
+
groupBy: [],
|
|
364
|
+
orderBy: []
|
|
365
|
+
}, undefined, "write", "merge")
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return {
|
|
369
|
+
insert,
|
|
370
|
+
attachInsertSource,
|
|
371
|
+
onConflict,
|
|
372
|
+
update,
|
|
373
|
+
upsert,
|
|
374
|
+
delete_,
|
|
375
|
+
truncate,
|
|
376
|
+
merge
|
|
377
|
+
}
|
|
378
|
+
}
|