forked from JordanMarr/SqlHydra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKataBuilders.fs
More file actions
425 lines (355 loc) · 21.6 KB
/
Copy pathKataBuilders.fs
File metadata and controls
425 lines (355 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/// LINQ builders for SqlKata.Query
[<AutoOpen>]
module SqlHydra.Query.KataBuilders
open System
open System.Linq.Expressions
open SqlKata
type SelectExpressionBuilder<'Output>() =
let getQueryOrDefault (state: QuerySource<'Result>) = // 'Result allows 'T to vary as the result of joins
match state with
| :? QuerySource<'Result, Query> as qs -> qs.Query
| _ -> Query()
let mergeTableMappings (a: Map<FQ.FQName, TableMapping>, b: Map<FQ.FQName, TableMapping>) =
Map (Seq.concat [ (Map.toSeq a); (Map.toSeq b) ])
member this.For (state: QuerySource<'T>, f: 'T -> QuerySource<'T>) =
let tbl = state.GetOuterTableMapping()
let query = state |> getQueryOrDefault
QuerySource<'T, Query>(
query.From(match tbl.Schema with Some schema -> $"{schema}.{tbl.Name}" | None -> tbl.Name),
state.TableMappings)
member this.Yield _ =
QuerySource<'T>(Map.empty)
// Prevents errors while typing join statement if rest of query is not filled in yet.
member this.Zero _ =
QuerySource<'T>(Map.empty)
/// Sets the WHERE condition
[<CustomOperation("where", MaintainsVariableSpace = true)>]
member this.Where (state:QuerySource<'T>, [<ProjectionParameter>] whereExpression) =
let query = state |> getQueryOrDefault
let where = LinqExpressionVisitors.visitWhere<'T> whereExpression (FQ.fullyQualifyColumn state.TableMappings)
QuerySource<'T, Query>(query.Where(fun w -> where), state.TableMappings)
/// Sets the SELECT statement and filters the query to include only the selected tables
[<CustomOperation("select", MaintainsVariableSpace = true)>]
member this.Select (state: QuerySource<'T>, [<ProjectionParameter>] selectExpression: Expression<Func<'T, 'Transform>>) =
let query = state |> getQueryOrDefault
let selections = LinqExpressionVisitors.visitSelect<'T,'Transform> selectExpression
let queryWithSelectedColumns =
selections
|> List.fold (fun (q: Query) -> function
| LinqExpressionVisitors.SelectedTable tbl ->
// Select all columns in table
q.Select($"%s{FQ.fullyQualifyTable state.TableMappings tbl}.*")
| LinqExpressionVisitors.SelectedColumn col ->
// Select a single column
q.Select(FQ.fullyQualifyColumn state.TableMappings col)
| LinqExpressionVisitors.SelectedAggregateColumn (aggFn, col) ->
// Currently in v2.3.7, SqlKata doesn't support multiple inline aggregate functions.
// Use SelectRaw as a workaround until SqlKata supports multiple aggregates.
// https://github.com/sqlkata/querybuilder/pull/504
let fqCol = FQ.fullyQualifyColumn state.TableMappings col
// SqlKata will translate curly braces to dialect-specific characters (ex: [] for mssql, "" for postgres)
let fqColWithCurlyBraces =
fqCol.Split([|'.'|], StringSplitOptions.RemoveEmptyEntries)
|> Array.map (sprintf "{%s}")
|> fun parts -> String.Join(".", parts)
q.SelectRaw($"{aggFn}({fqColWithCurlyBraces})")
) query
QuerySource<'Transform, Query>(queryWithSelectedColumns, state.TableMappings)
/// Sets the ORDER BY for single column
[<CustomOperation("orderBy", MaintainsVariableSpace = true)>]
member this.OrderBy (state:QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
let query = state |> getQueryOrDefault
let orderedQuery =
LinqExpressionVisitors.visitOrderByPropertySelector<'T, 'Prop> propertySelector
|> function
| LinqExpressionVisitors.OrderByColumn p ->
query.OrderBy(FQ.fullyQualifyColumn state.TableMappings p)
| LinqExpressionVisitors.OrderByAggregateColumn (aggType, p) ->
query.OrderByRaw($"{aggType}({FQ.fullyQualifyColumn state.TableMappings p})")
QuerySource<'T, Query>(orderedQuery, state.TableMappings)
/// Sets the ORDER BY for single column
[<CustomOperation("thenBy", MaintainsVariableSpace = true)>]
member this.ThenBy (state:QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
let query = state |> getQueryOrDefault
let orderedQuery =
LinqExpressionVisitors.visitOrderByPropertySelector<'T, 'Prop> propertySelector
|> function
| LinqExpressionVisitors.OrderByColumn p ->
query.OrderBy(FQ.fullyQualifyColumn state.TableMappings p)
| LinqExpressionVisitors.OrderByAggregateColumn (aggType, p) ->
query.OrderByRaw($"{aggType}({FQ.fullyQualifyColumn state.TableMappings p})")
QuerySource<'T, Query>(orderedQuery, state.TableMappings)
/// Sets the ORDER BY DESC for single column
[<CustomOperation("orderByDescending", MaintainsVariableSpace = true)>]
member this.OrderByDescending (state:QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
let query = state |> getQueryOrDefault
let orderedQuery =
LinqExpressionVisitors.visitOrderByPropertySelector<'T, 'Prop> propertySelector
|> function
| LinqExpressionVisitors.OrderByColumn p ->
query.OrderByDesc(FQ.fullyQualifyColumn state.TableMappings p)
| LinqExpressionVisitors.OrderByAggregateColumn (aggType, p) ->
query.OrderByRaw($"{aggType}({FQ.fullyQualifyColumn state.TableMappings p}) DESC")
QuerySource<'T, Query>(orderedQuery, state.TableMappings)
/// Sets the ORDER BY DESC for single column
[<CustomOperation("thenByDescending", MaintainsVariableSpace = true)>]
member this.ThenByDescending (state:QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
let query = state |> getQueryOrDefault
let orderedQuery =
LinqExpressionVisitors.visitOrderByPropertySelector<'T, 'Prop> propertySelector
|> function
| LinqExpressionVisitors.OrderByColumn p ->
query.OrderByDesc(FQ.fullyQualifyColumn state.TableMappings p)
| LinqExpressionVisitors.OrderByAggregateColumn (aggType, p) ->
query.OrderByRaw($"{aggType}({FQ.fullyQualifyColumn state.TableMappings p}) DESC")
QuerySource<'T, Query>(orderedQuery, state.TableMappings)
/// Sets the SKIP value for query
[<CustomOperation("skip", MaintainsVariableSpace = true)>]
member this.Skip (state:QuerySource<'T>, skip) =
let query = state |> getQueryOrDefault
QuerySource<'T, Query>(query.Skip(skip), state.TableMappings)
/// Sets the TAKE value for query
[<CustomOperation("take", MaintainsVariableSpace = true)>]
member this.Take (state:QuerySource<'T>, take) =
let query = state |> getQueryOrDefault
QuerySource<'T, Query>(query.Take(take), state.TableMappings)
/// INNER JOIN table on one or more columns
[<CustomOperation("join", MaintainsVariableSpace = true, IsLikeJoin = true, JoinConditionWord = "on")>]
member this.Join (outerSource: QuerySource<'TOuter>,
innerSource: QuerySource<'TInner>,
outerKeySelector: Expression<Func<'TOuter,'Key>>,
innerKeySelector: Expression<Func<'TInner,'Key>>,
resultSelector: Expression<Func<'TOuter,'TInner,'Result>> ) =
let mergedTables = mergeTableMappings (outerSource.TableMappings, innerSource.TableMappings)
let outerProperties = LinqExpressionVisitors.visitJoin<'TOuter, 'Key> outerKeySelector
let innerProperties = LinqExpressionVisitors.visitJoin<'TInner, 'Key> innerKeySelector
let outerQuery = outerSource |> getQueryOrDefault
let innerTableName =
innerProperties
|> Seq.map (fun p -> mergedTables.[FQ.fqName p.DeclaringType])
|> Seq.map (fun tbl ->
match tbl.Schema with
| Some schema -> sprintf "%s.%s" schema tbl.Name
| None -> tbl.Name
)
|> Seq.head
let joinOn =
let fq = FQ.fullyQualifyColumn mergedTables
List.zip outerProperties innerProperties
|> List.fold (fun (j: Join) (outerProp, innerProp) -> j.On(fq outerProp, fq innerProp)) (Join())
QuerySource<'Result, Query>(outerQuery.Join(innerTableName, fun j -> joinOn), mergedTables)
/// LEFT JOIN table on one or more columns
[<CustomOperation("leftJoin", MaintainsVariableSpace = true, IsLikeJoin = true, JoinConditionWord = "on")>]
member this.LeftJoin (outerSource: QuerySource<'TOuter>,
innerSource: QuerySource<'TInner>,
outerKeySelector: Expression<Func<'TOuter,'Key>>,
innerKeySelector: Expression<Func<'TInner option,'Key>>,
resultSelector: Expression<Func<'TOuter,'TInner option,'Result>> ) =
let mergedTables = mergeTableMappings (outerSource.TableMappings, innerSource.TableMappings)
let outerProperties = LinqExpressionVisitors.visitJoin<'TOuter, 'Key> outerKeySelector
let innerProperties = LinqExpressionVisitors.visitJoin<'TInner option, 'Key> innerKeySelector
let outerQuery = outerSource |> getQueryOrDefault
let innerTableName =
innerProperties
|> Seq.map (fun p -> mergedTables.[FQ.fqName p.DeclaringType])
|> Seq.map (fun tbl ->
match tbl.Schema with
| Some schema -> sprintf "%s.%s" schema tbl.Name
| None -> tbl.Name
)
|> Seq.head
let joinOn =
let fq = FQ.fullyQualifyColumn mergedTables
List.zip outerProperties innerProperties
|> List.fold (fun (j: Join) (outerProp, innerProp) -> j.On(fq outerProp, fq innerProp)) (Join())
QuerySource<'Result, Query>(outerQuery.LeftJoin(innerTableName, fun j -> joinOn), mergedTables)
/// Sets the GROUP BY for one or more columns.
[<CustomOperation("groupBy", MaintainsVariableSpace = true)>]
member this.GroupBy (state:QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
let query = state |> getQueryOrDefault
let properties = LinqExpressionVisitors.visitGroupBy<'T, 'Prop> propertySelector (FQ.fullyQualifyColumn state.TableMappings)
QuerySource<'T, Query>(query.GroupBy(properties |> List.toArray), state.TableMappings)
/// Sets the HAVING condition.
[<CustomOperation("having", MaintainsVariableSpace = true)>]
member this.Having (state:QuerySource<'T>, [<ProjectionParameter>] havingExpression) =
let query = state |> getQueryOrDefault
let having = LinqExpressionVisitors.visitHaving<'T> havingExpression (FQ.fullyQualifyColumn state.TableMappings)
QuerySource<'T, Query>(query.Having(fun w -> having), state.TableMappings)
/// COUNT aggregate function
[<CustomOperation("count", MaintainsVariableSpace = true)>]
member this.Count (state:QuerySource<'T>) =
let query = state |> getQueryOrDefault
QuerySource<int, Query>(query.AsCount(), state.TableMappings)
/// Sets query to return DISTINCT values
[<CustomOperation("distinct", MaintainsVariableSpace = true)>]
member this.Distinct (state:QuerySource<'T>) =
let query = state |> getQueryOrDefault
QuerySource<'T, Query>(query.Distinct(), state.TableMappings)
/// Transforms the query
member this.Run (state: QuerySource<'T>) =
let query = getQueryOrDefault state
SelectQuery<'T>(query)
type DeleteExpressionBuilder<'T>() =
let getQueryOrDefault (state: QuerySource<'Result>) =
match state with
| :? QuerySource<'Result, Query> as qs -> qs.Query
| _ -> Query()
member this.For (state: QuerySource<'T>, f: 'T -> QuerySource<'T>) =
let tbl = state.GetOuterTableMapping()
let query = state |> getQueryOrDefault
QuerySource<'T, Query>(
query.From(match tbl.Schema with Some schema -> $"{schema}.{tbl.Name}" | None -> tbl.Name),
state.TableMappings)
member this.Yield _ =
QuerySource<'T>(Map.empty)
/// Sets the WHERE condition
[<CustomOperation("where", MaintainsVariableSpace = true)>]
member this.Where (state:QuerySource<'T>, [<ProjectionParameter>] whereExpression) =
let query = state |> getQueryOrDefault
let where = LinqExpressionVisitors.visitWhere<'T> whereExpression (FQ.fullyQualifyColumn state.TableMappings)
QuerySource<'T, Query>(query.Where(fun w -> where), state.TableMappings)
/// Deletes all records in the table (only when there are is no where clause)
[<CustomOperation("deleteAll", MaintainsVariableSpace = true)>]
member this.DeleteAll (state:QuerySource<'T>) =
state :?> QuerySource<'T, Query>
/// Unwraps the query
member this.Run (state: QuerySource<'T>) =
let query = state |> getQueryOrDefault
DeleteQuery(query.AsDelete())
type InsertExpressionBuilder<'T, 'InsertReturn when 'InsertReturn : struct>() =
let getQueryOrDefault (state: QuerySource<'T>) =
match state with
| :? QuerySource<'T, InsertQuerySpec<'T, 'IdentityReturn>> as qs -> qs.Query
| _ -> InsertQuerySpec.Default
member this.For (state: QuerySource<'T>, f: 'T -> QuerySource<'T>) =
let tbl = state.GetOuterTableMapping()
let query = state |> getQueryOrDefault
QuerySource<'T, InsertQuerySpec<'T, 'InsertReturn>>(
{ query with Table = match tbl.Schema with Some schema -> $"{schema}.{tbl.Name}" | None -> tbl.Name }
, state.TableMappings)
/// Sets the TABLE name for query.
[<CustomOperation("into")>]
member this.Into (state: QuerySource<'T>, table: QuerySource<'T>) =
let tbl = table.GetOuterTableMapping()
let query = state |> getQueryOrDefault
QuerySource<'T, InsertQuerySpec<'T, 'InsertReturn>>(
{ query with Table = match tbl.Schema with Some schema -> $"{schema}.{tbl.Name}" | None -> tbl.Name }
, state.TableMappings)
member this.Yield _ =
QuerySource<'T>(Map.empty)
/// Sets a single value for INSERT
[<CustomOperation("entity", MaintainsVariableSpace = true)>]
member this.Entity (state:QuerySource<'T>, value: 'T) =
let query = state |> getQueryOrDefault
QuerySource<'T, InsertQuerySpec<'T, 'InsertReturn>>(
{ query with Entities = [ value ] }
, state.TableMappings)
/// Sets multiple values for INSERT
[<CustomOperation("entities", MaintainsVariableSpace = true)>]
member this.Entities (state:QuerySource<'T>, values: 'T seq) =
let query = state |> getQueryOrDefault
QuerySource<'T, InsertQuerySpec<'T, 'InsertReturn>>(
{ query with Entities = values |> Seq.toList }
, state.TableMappings)
/// Includes a column in the insert query.
[<CustomOperation("includeColumn", MaintainsVariableSpace = true)>]
member this.IncludeColumn (state: QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
let query = state |> getQueryOrDefault
let prop = (propertySelector |> LinqExpressionVisitors.visitPropertySelector<'T, 'Prop>).Name
QuerySource<'T, InsertQuerySpec<'T, 'InsertReturn>>({ query with Fields = query.Fields @ [ prop ] }, state.TableMappings)
/// Excludes a column from the insert query.
[<CustomOperation("excludeColumn", MaintainsVariableSpace = true)>]
member this.ExcludeColumn (state: QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
let query = state |> getQueryOrDefault
let prop = LinqExpressionVisitors.visitPropertySelector<'T, 'Prop> propertySelector
let newQuery =
query.Fields
|> function
| [] -> FSharp.Reflection.FSharpType.GetRecordFields(typeof<'T>) |> Array.map (fun x -> x.Name) |> Array.toList
| fields -> fields
|> List.filter (fun f -> f <> prop.Name)
|> (fun x -> { query with Fields = x })
QuerySource<'T, InsertQuerySpec<'T, 'InsertReturn>>(newQuery, state.TableMappings)
/// Sets the identity field that should be returned from the insert and excludes it from the insert columns.
[<CustomOperation("getId", MaintainsVariableSpace = true)>]
member this.GetId (state: QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
// Exclude the identity column from the query
let state = this.ExcludeColumn(state, propertySelector)
// Set the identity property and the 'InsertReturn type
let spec = state.Query
let prop = LinqExpressionVisitors.visitPropertySelector<'T, 'InsertReturn> propertySelector :?> Reflection.PropertyInfo
let identitySpec = { Table = spec.Table; Entities = spec.Entities; Fields = spec.Fields; IdentityField = Some prop.Name }
// Sets both the identity field name (prop.Name) and its type ('InsertReturn)
QuerySource<'T, InsertQuerySpec<'T, 'InsertReturn>>(identitySpec, state.TableMappings)
member this.Run (state: QuerySource<'T>) =
let spec = getQueryOrDefault state
InsertQuery<'T, 'InsertReturn>(spec)
type UpdateExpressionBuilder<'T>() =
let getQueryOrDefault (state: QuerySource<'Result>) =
match state with
| :? QuerySource<'Result, UpdateQuerySpec<'T>> as qs -> qs.Query
| _ -> UpdateQuerySpec.Default
member this.For (state: QuerySource<'T>, f: 'T -> QuerySource<'T>) =
let tbl = state.GetOuterTableMapping()
let query = state |> getQueryOrDefault
QuerySource<'T, UpdateQuerySpec<'T>>(
{ query with Table = match tbl.Schema with Some schema -> $"{schema}.{tbl.Name}" | None -> tbl.Name }
, state.TableMappings)
member this.Yield _ =
QuerySource<'T>(Map.empty)
/// Sets the emtore entity ('T) to be updated
[<CustomOperation("entity", MaintainsVariableSpace = true)>]
member this.Entity (state: QuerySource<'T>, value: 'T) =
let query = state |> getQueryOrDefault
QuerySource<'T, UpdateQuerySpec<'T>>(
{ query with Entity = value |> Some}
, state.TableMappings)
/// Sets a property of the entity ('T) to be updated
[<CustomOperation("set", MaintainsVariableSpace = true)>]
member this.Set (state: QuerySource<'T>, [<ProjectionParameter>] propertySelector: Expression<Func<'T, 'Prop>>, value: 'Prop) =
let query = state |> getQueryOrDefault
let prop = LinqExpressionVisitors.visitPropertySelector<'T, 'Prop> propertySelector :?> Reflection.PropertyInfo
let value = KataUtils.getQueryParameterForValue prop value :> obj
QuerySource<'T, UpdateQuerySpec<'T>>(
{ query with SetValues = query.SetValues @ [ prop.Name, value ] }
, state.TableMappings)
/// Includes a column in the insert query.
[<CustomOperation("includeColumn", MaintainsVariableSpace = true)>]
member this.IncludeColumn (state: QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
let query = state |> getQueryOrDefault
let prop = (propertySelector |> LinqExpressionVisitors.visitPropertySelector<'T, 'Prop>).Name
QuerySource<'T, UpdateQuerySpec<'T>>({ query with Fields = query.Fields @ [ prop ] }, state.TableMappings)
/// Excludes a column from the insert query.
[<CustomOperation("excludeColumn", MaintainsVariableSpace = true)>]
member this.ExcludeColumn (state: QuerySource<'T>, [<ProjectionParameter>] propertySelector) =
let query = state |> getQueryOrDefault
let prop = LinqExpressionVisitors.visitPropertySelector<'T, 'Prop> propertySelector
let newQuery =
query.Fields
|> function
| [] -> FSharp.Reflection.FSharpType.GetRecordFields(typeof<'T>) |> Array.map (fun x -> x.Name) |> Array.toList
| fields -> fields
|> List.filter (fun f -> f <> prop.Name)
|> (fun x -> { query with Fields = x })
QuerySource<'T, UpdateQuerySpec<'T>>(newQuery, state.TableMappings)
/// Sets the WHERE condition
[<CustomOperation("where", MaintainsVariableSpace = true)>]
member this.Where (state: QuerySource<'T>, [<ProjectionParameter>] whereExpression) =
let query = state |> getQueryOrDefault
let where = LinqExpressionVisitors.visitWhere<'T> whereExpression (FQ.fullyQualifyColumn state.TableMappings)
QuerySource<'T, UpdateQuerySpec<'T>>({ query with Where = Some where }, state.TableMappings)
/// A safeguard that verifies that all records in the table should be updated.
[<CustomOperation("updateAll", MaintainsVariableSpace = true)>]
member this.UpdateAll (state:QuerySource<'T>) =
let query = state |> getQueryOrDefault
QuerySource<'T, UpdateQuerySpec<'T>>({ query with UpdateAll = true }, state.TableMappings)
/// Unwraps the query
member this.Run (state: QuerySource<'T>) =
let spec = state |> getQueryOrDefault
if spec.Where = None && spec.UpdateAll = false
then failwith "An `update` expression must either contain a `where` clause or `updateAll`."
UpdateQuery<'T>(spec)
let select<'T> = SelectExpressionBuilder<'T>()
let delete<'T> = DeleteExpressionBuilder<'T>()
let insert<'T, 'InsertReturn when 'InsertReturn : struct> = InsertExpressionBuilder<'T, 'InsertReturn>()
let update<'T> = UpdateExpressionBuilder<'T>()