-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuery.fs
More file actions
251 lines (221 loc) · 11.3 KB
/
Query.fs
File metadata and controls
251 lines (221 loc) · 11.3 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
namespace DbFun.Npgsql.Builders
open System
open System.Data
open DbFun.Core.Diagnostics
open DbFun.Core.Builders
open DbFun.Npgsql.Builders
type QueryConfig<'DbKey> =
{
Common : DbFun.Core.Builders.QueryConfig<'DbKey>
PgArrayBuilders : PgArrayParamsImpl.IBuilder list
}
with
/// <summary>
/// Creates default configuration.
/// </summary>
/// <param name="createConnection">
/// The function creating database connection (with proper connection string, but not open).
/// </param>
static member Default(createConnection: 'DbKey -> IDbConnection) =
let common = DbFun.Core.Builders.QueryConfig<'DbKey>.Default(createConnection)
{ Common = common; PgArrayBuilders = PgArrayParamsImpl.getDefaultBuilders() }
/// <summary>
/// Adds Postgres array support.
/// </summary>
member this.UsePostgresArrayParams() =
let pgArrayProvider = ParamsImpl.BaseSetterProvider(PgArrayParamsImpl.getDefaultBuilders(), this.Common.Compiler)
let pgArrayBuilder = ParamsImpl.PgArrayBuilder(pgArrayProvider)
{ this with Common = { this.Common with ParamBuilders = pgArrayBuilder :: this.Common.ParamBuilders } }
/// <summary>
/// Adds Postgres array support.
/// </summary>
member this.UsePostgresArrayResults() =
{ this with
Common =
{ this.Common with
RowBuilders =
RowsImpl.ArrayItemConverter<DateTime, DateOnly>(DateOnly.FromDateTime) ::
RowsImpl.ArrayItemConverter<TimeSpan, TimeOnly>(TimeOnly.FromTimeSpan) ::
RowsImpl.ArrayColumnBuilder() ::
RowsImpl.ArrayCollectionConverter() ::
RowsImpl.EnumArrayConverter<int>() ::
RowsImpl.EnumArrayConverter<char>() ::
RowsImpl.UnionArrayConverter() ::
this.Common.RowBuilders
}
}
/// <summary>
/// Adds Postgres array support.
/// </summary>
member this.UsePostgresArrays() =
this.UsePostgresArrayParams().UsePostgresArrayResults()
/// <summary>
/// Adds a converter mapping application values of a given type to ptoper database parameter values.
/// </summary>
/// <param name="convert">
/// Function converting application values to database parameter values.
/// </param>
member this.AddRowConverter(converter: 'Source -> 'Target) =
{ this with
Common =
{ this.Common with
RowBuilders = RowsImpl.ArrayItemConverter(converter) :: this.Common.RowBuilders
}.AddRowConverter(converter)
}
//.AddRowConverter(converter)
/// <summary>
/// Adds builder for table-valued parameters.
/// </summary>
/// <param name="builder">
/// The builder.
/// </param>
member this.AddPgArrayBuilder(builder: PgArrayParamsImpl.IBuilder) =
let pgArrayBuilders = builder :: this.PgArrayBuilders
let pgArrayProvider = ParamsImpl.BaseSetterProvider(pgArrayBuilders, this.Common.Compiler)
let arrayBuilder = ParamsImpl.PgArrayBuilder(pgArrayProvider) :> DbFun.Core.Builders.ParamsImpl.IBuilder
let paramBuilders = this.Common.ParamBuilders |> List.map (function :? ParamsImpl.PgArrayBuilder -> arrayBuilder | b -> b)
{ this with
Common = { this.Common with ParamBuilders = paramBuilders }
PgArrayBuilders = pgArrayBuilders
}
/// <summary>
/// Adds a converter mapping database values to application values.
/// </summary>
/// <param name="convert">
/// Function converting database column values to application values.
/// </param>
member this.AddParamConverter(converter: 'Source -> 'Target) =
let arrayBuilder = DbFun.Npgsql.Builders.ParamsImpl.Converter<'Source, 'Target>(converter)
{ this with Common = this.Common.AddParamConverter(converter) }
.AddPgArrayBuilder(arrayBuilder)
/// <summary>
/// Adds a configurator for parameter builders of types determined by canBuild function.
/// </summary>
/// <param name="getConfig">
/// Creates a configuration object.
/// </param>
/// <param name="canBuild">
/// Function determining whether a given type is handled by the configurator.
/// </param>
member this.AddParamConfigurator(getConfig: string -> 'Config, canBuild: Type -> bool) =
{ this with Common = this.Common.AddRowConfigurator(getConfig, canBuild) }
.AddPgArrayBuilder(ParamsImpl.Configurator<'Config>(getConfig, canBuild))
/// <summary>
/// Adds a configurator for both parameter and row builders of types determined by canBuild function.
/// </summary>
/// <param name="getConfig">
/// Creates a configuration object.
/// </param>
/// <param name="canBuild">
/// Function determining whether a given type is handled by the configurator.
/// </param>
member this.AddConfigurator(getConfig: string -> 'Config, canBuild: Type -> bool) =
this.AddParamConfigurator(getConfig, canBuild)
.AddRowConfigurator(getConfig, canBuild)
/// <summary>
/// Adds a mapper that creates parameters for properties of the specified type.
/// </summary>
/// <param name="ownerType">
/// The type containing properties.
/// </param>
/// <param name="excluded">
/// The list of excluded properties.
/// </param>
/// <param name="path">
/// Determines, whether the name of parent property shold be added to property names when creating parameters.
/// </param>
member this.AddParamPropertyMapper(ownerType: Type, ?excluded: string seq, ?path: bool) =
let pgArrBuilder = ParamsImpl.PropertiesBuilder(ownerType, ?excluded = excluded, ?path = path)
{ this with Common = this.Common.AddParamPropertyMapper(ownerType, ?excluded = excluded, ?path = path) }
.AddPgArrayBuilder(pgArrBuilder)
/// <summary>
/// Adds a mapper that creates parameters for properties of the specified type.
/// </summary>
/// <param name="excluded">
/// The list of excluded properties.
/// </param>
/// <param name="path">
/// Determines, whether the name of parent property shold be added to property names when creating parameters.
/// </param>
member this.AddParamPropertyMapper<'Type>(?excluded: string seq, ?path: bool) =
this.AddParamPropertyMapper(typeof<'Type>, ?excluded = excluded, ?path = path)
/// <summary>
/// Adds a mapper of the specified class.
/// </summary>
/// <param name="path">
/// Determines, whether the name of parent parameter shold be added to parameter names when accessing result columns.
/// </param>
member this.AddRowClassMapper<'Class>(?path: bool) =
{ this with Common = this.Common.AddRowClassMapper<'Class>(?path = path) }
interface DbFun.Core.Builders.IDerivedConfig<QueryConfig<'DbKey>, 'DbKey> with
member this.MapCommon(map: DbFun.Core.Builders.QueryConfig<'DbKey> -> DbFun.Core.Builders.QueryConfig<'DbKey>): QueryConfig<'DbKey> =
{ this with Common = map(this.Common) }
type QueryConfig = QueryConfig<unit>
/// <summary>
/// Provides methods creating various query functions.
/// </summary>
type QueryBuilder<'DbKey>(dbKey: 'DbKey, config: QueryConfig<'DbKey>, ?compileTimeErrorLog: Ref<CompileTimeErrorLog>) =
inherit DbFun.Core.Builders.QueryBuilder<'DbKey>(dbKey, config.Common, ?compileTimeErrorLog = compileTimeErrorLog)
/// <summary>
/// The configuration of the query builder.
/// </summary>
member __.Config = config
/// <summary>
/// Creates query builder object with default configuration
/// </summary>
/// <param name="createConnection">
/// Function creating connection, assigned with a proper connection string, but not open.
/// </param>
new(dbKey: 'DbKey, createConnection: 'DbKey -> IDbConnection) =
QueryBuilder<'DbKey>(dbKey, QueryConfig<'DbKey>.Default(createConnection))
/// <summary>
/// Creates new builder with the specified command timeout.
/// </summary>
/// <param name="timeout">
/// The timeout value in seconds.
/// </param>
member this.Timeout(timeout: int) =
QueryBuilder<'DbKey>(dbKey, { this.Config with Common = { this.Config.Common with Timeout = Some timeout } }, ?compileTimeErrorLog = this.RawCompileTimeErrorLog)
/// <summary>
/// Creates new builder with compile-time error logging and deferred exceptions.
/// </summary>
member this.LogCompileTimeErrors() =
QueryBuilder<'DbKey>(dbKey, { this.Config with Common = { this.Config.Common with LogCompileTimeErrors = true } }, ?compileTimeErrorLog = this.RawCompileTimeErrorLog)
/// <summary>
/// Creates new builder generating query functions without discovering resultset structure using SchemaOnly calls.
/// </summary>
member this.DisablePrototypeCalls() =
QueryBuilder<'DbKey>(dbKey, { this.Config with Common = this.Config.Common.DisablePrototypeCalls() }, ?compileTimeErrorLog = this.RawCompileTimeErrorLog)
/// <summary>
/// Allows to handle collections by generating parameters for each item with name modified by adding item index.
/// </summary>
member __.HandleCollectionParams() =
QueryBuilder<'DbKey>(dbKey, config.HandleCollectionParams(), ?compileTimeErrorLog = compileTimeErrorLog)
/// <summary>
/// Handles collections as array parameters.
/// </summary>
member __.UsePostgresArrayParamss() =
QueryBuilder<'DbKey>(dbKey, config.UsePostgresArrayParams(), ?compileTimeErrorLog = compileTimeErrorLog)
/// <summary>
/// Handles collections as array results.
/// </summary>
member __.UsePostgresArrayResults() =
QueryBuilder<'DbKey>(dbKey, config.UsePostgresArrayResults(), ?compileTimeErrorLog = compileTimeErrorLog)
/// <summary>
/// Provides full support for Postgress arrays.
/// </summary>
member __.UsePostgresArrays() =
QueryBuilder<'DbKey>(dbKey, config.UsePostgresArrays(), ?compileTimeErrorLog = compileTimeErrorLog)
/// <summary>
/// Provides methods creating various query functions.
/// </summary>
type QueryBuilder(config: QueryConfig, ?compileTimeErrorLog: Ref<CompileTimeErrorLog>) =
inherit QueryBuilder<unit>((), config, ?compileTimeErrorLog = compileTimeErrorLog)
/// <summary>
/// Creates query builder object with default configuration
/// </summary>
/// <param name="createConnection">
/// Function creating connection, assigned with a proper connection string, but not open.
/// </param>
new(createConnection: unit -> IDbConnection) =
QueryBuilder(QueryConfig.Default(createConnection))