-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDatabaseExtensions.Parameters.cs
More file actions
386 lines (339 loc) · 23 KB
/
Copy pathDatabaseExtensions.Parameters.cs
File metadata and controls
386 lines (339 loc) · 23 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
namespace CoreEx.Database;
public static partial class DatabaseExtensions
{
/// <summary>
/// Add one or more parameters by invoking a delegate.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="action">The delegate to enable parameter addition.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf Params<TSelf>(this IDatabaseParameters<TSelf> parameters, Action<DatabaseParameterCollection> action)
{
action.ThrowIfNull()(parameters.ThrowIfNull().Parameters);
return (TSelf)parameters;
}
/// <summary>
/// Adds the <see cref="DbParameter"/> <paramref name="list"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="list">The <see cref="DbParameter"/> list.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf Params<TSelf>(this IDatabaseParameters<TSelf> parameters, params IEnumerable<DbParameter> list)
{
if (list is not null && list != parameters.Parameters)
parameters.Parameters.AddRange(list);
return (TSelf)parameters;
}
#region Param
/// <summary>
/// Adds the named parameter and value, using the specified <paramref name="direction"/>, to the <see cref="DbCommand.Parameters"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="name">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <param name="dbType">The parameter <see cref="DbType"/>.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf Param<TSelf>(this IDatabaseParameters<TSelf> parameters, string name, object? value = null, DbType? dbType = null, ParameterDirection direction = ParameterDirection.Input)
{
parameters.ThrowIfNull().Parameters.AddParameter(name, value, dbType, direction);
return (TSelf)parameters;
}
/// <summary>
/// Adds the named parameter and value, using the specified <paramref name="direction"/>, to the <see cref="DbCommand.Parameters"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <typeparam name="T">The parameter <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="name">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <param name="dbType">The parameter <see cref="DbType"/>.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf Param<TSelf, T>(this IDatabaseParameters<TSelf> parameters, string name, T? value, DbType? dbType = null, ParameterDirection direction = ParameterDirection.Input)
{
parameters.ThrowIfNull().Parameters.AddParameter(name, value, dbType, direction);
return (TSelf)parameters;
}
/// <summary>
/// Adds the named parameter using the specified <paramref name="dbType"/>, <paramref name="size"/> and <paramref name="direction"/>, to the <see cref="DbCommand.Parameters"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="name">The parameter name.</param>
/// <param name="dbType">The parameter <see cref="DbType"/>.</param>
/// <param name="size">The maximum size (in bytes).</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf Param<TSelf>(this IDatabaseParameters<TSelf> parameters, string name, DbType dbType, int size, ParameterDirection direction = ParameterDirection.Input)
{
parameters.ThrowIfNull().Parameters.AddParameter(name, dbType, size, direction);
return (TSelf)parameters;
}
/// <summary>
/// Adds the named parameter and value serialized as a JSON <see cref="string"/> to the <see cref="DbCommand.Parameters"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="name">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf JsonParam<TSelf>(this IDatabaseParameters<TSelf> parameters, string name, object? value)
{
parameters.ThrowIfNull().Parameters.AddJsonParameter(name, value);
return (TSelf)parameters;
}
/// <summary>
/// Adds the named <paramref name="wildcard"/> parameter to the <see cref="DbCommand.Parameters"/>.
/// </summary>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="name">The parameter name.</param>
/// <param name="wildcard">The wildcard value.</param>
/// <returns>The <see cref="DatabaseParameterCollection"/> to support fluent-style method-chaining.</returns>
public static TSelf WildCardParam<TSelf>(this IDatabaseParameters<TSelf> parameters, string name, string? wildcard)
{
parameters.ThrowIfNull().Parameters.AddWildcardParameter(name, wildcard);
return (TSelf)parameters;
}
#endregion
#region ParamWhen
/// <summary>
/// Adds a named parameter and value <paramref name="when"/> <see langword="true"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <typeparam name="T">The parameter <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="when">Adds the parameter when <see langword="true"/>.</param>
/// <param name="name">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <param name="dbType">The parameter <see cref="DbType"/>.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf ParamWhen<TSelf, T>(this IDatabaseParameters<TSelf> parameters, bool? when, string name, Func<T> value, DbType? dbType = null, ParameterDirection direction = ParameterDirection.Input)
{
value.ThrowIfNull();
if (when is true)
parameters.ThrowIfNull().Parameters.AddParameter(name, value(), dbType, direction);
return (TSelf)parameters;
}
/// <summary>
/// Adds a named parameter and value serialized as a JSON <see cref="string"/> <paramref name="when"/> <see langword="true"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <typeparam name="T">The parameter <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="when">Adds the parameter when <see langword="true"/>.</param>
/// <param name="name">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf JsonParamWhen<TSelf, T>(this IDatabaseParameters<TSelf> parameters, bool? when, string name, Func<T?> value)
{
value.ThrowIfNull();
if (when is true)
parameters.ThrowIfNull().Parameters.AddJsonParameter(name, value());
return (TSelf)parameters;
}
/// <summary>
/// Adds a named <paramref name="wildcard"/> parameter <paramref name="when"/> <see langword="true"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="when">Adds the parameter when <see langword="true"/>.</param>
/// <param name="name">The parameter name.</param>
/// <param name="wildcard">The wildcard parameter value.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf WildcardParamWhen<TSelf>(this IDatabaseParameters<TSelf> parameters, bool? when, string name, Func<string?> wildcard)
{
wildcard.ThrowIfNull();
if (when is true)
parameters.ThrowIfNull().Parameters.AddWildcardParameter(name, wildcard());
return (TSelf)parameters;
}
#endregion
#region ParamWith
/// <summary>
/// Adds a named parameter when invoked <paramref name="with"/> a non-default value.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <typeparam name="T">The parameter <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="with">The value <b>with</b> which to verify is non-default that is also used as the parameter value.</param>
/// <param name="name">The parameter name.</param>
/// <param name="dbType">The parameter <see cref="DbType"/>.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf ParamWith<TSelf, T>(this IDatabaseParameters<TSelf> parameters, T? with, string name, DbType? dbType = null, ParameterDirection direction = ParameterDirection.Input)
=> ParamWhen(parameters, with is not null && Comparer<T>.Default.Compare(with, default!) != 0, name, () => with!, dbType, direction);
/// <summary>
/// Adds a named parameter when invoked <paramref name="with"/> a non-default value.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <typeparam name="TWith">The with <see cref="Type"/>.</typeparam>
/// <typeparam name="TValue">The parameter <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="with">The value <b>with</b> which to verify is non-default.</param>
/// <param name="name">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <param name="dbType">The parameter <see cref="DbType"/>.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf ParamWith<TSelf, TWith, TValue>(this IDatabaseParameters<TSelf> parameters, TWith? with, string name, Func<TValue?> value, DbType? dbType = null, ParameterDirection direction = ParameterDirection.Input)
=> ParamWhen(parameters, with is not null && Comparer<TWith>.Default.Compare(with, default!) != 0, name, value, dbType, direction);
/// <summary>
/// Adds a named parameter when invoked <paramref name="with"/> a non-default value serialized as a JSON <see cref="string"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <typeparam name="T">The parameter value <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="with">The value <b>with</b> which to verify is non-default that is also used as the parameter value.</param>
/// <param name="name">The parameter name.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf JsonParamWith<TSelf, T>(this IDatabaseParameters<TSelf> parameters, T? with, string name)
=> JsonParamWhen(parameters, with is not null && Comparer<T>.Default.Compare(with, default!) != 0, name, () => with);
/// <summary>
/// Adds a named parameter when invoked <paramref name="with"/> a non-default value serialized as a JSON <see cref="string"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <typeparam name="TWith">The with value <see cref="Type"/>.</typeparam>
/// <typeparam name="TValue">The parameter value <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="with">The value <b>with</b> which to verify is non-default.</param>
/// <param name="name">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf JsonParamWith<TSelf, TWith, TValue>(this IDatabaseParameters<TSelf> parameters, TWith? with, string name, Func<TValue?> value)
=> JsonParamWhen(parameters, with is not null && Comparer<TWith>.Default.Compare(with, default!) != 0, name, value);
/// <summary>
/// Adds a named parameter when invoked with a non-default <paramref name="wildcard"/> (converted for the database).
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="wildcard">The wildcard <b>with</b> which to verify is non-default that is also used as the parameter value.</param>
/// <param name="name">The parameter name.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf WildcardParamWith<TSelf>(this IDatabaseParameters<TSelf> parameters, string? wildcard, string name)
=> WildcardParamWhen(parameters, !string.IsNullOrEmpty(wildcard), name, () => wildcard);
/// <summary>
/// Adds a named parameter when invoked with a non-default <paramref name="wildcard"/> (converted for the database).
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <typeparam name="TWith">The with value <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="with">The value <b>with</b> which to verify is non-default.</param>
/// <param name="wildcard">The wildcard parameter value.</param>
/// <param name="name">The parameter name.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf WildcardParamWith<TSelf, TWith>(this IDatabaseParameters<TSelf> parameters, TWith? with, string name, Func<string?> wildcard)
=> WildcardParamWhen(parameters, with is not null && Comparer<TWith>.Default.Compare(with, default!) != 0, name, wildcard);
#endregion
#region RowVersionParam
/// <summary>
/// Adds a named (<see cref="DatabaseColumns.RowVersionName"/>) parameter using the <see cref="IReadOnlyETag.ETag"/> <see cref="IDatabase.RowVersionConverter"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="value">The <see langword="string"/>-based <see cref="IReadOnlyETag.ETag"/> representation of the row version.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf RowVersionParam<TSelf>(this IDatabaseParameters<TSelf> parameters, string? value, ParameterDirection direction = ParameterDirection.Input)
{
parameters.ThrowIfNull().Parameters.AddRowVersionParam(value, direction);
return (TSelf)parameters;
}
/// <summary>
/// Adds a named (<see cref="DatabaseColumns.RowVersionName"/>) parameter using the <see cref="IReadOnlyETag.ETag"/> <see cref="IDatabase.RowVersionConverter"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="when">Adds the parameter when <see langword="true"/>.</param>
/// <param name="value">The <see langword="string"/>-based <see cref="IReadOnlyETag.ETag"/> representation of the row version.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf RowVersionParamWhen<TSelf>(this IDatabaseParameters<TSelf> parameters, bool? when, string? value, ParameterDirection direction = ParameterDirection.Input)
{
if (when is true)
parameters.ThrowIfNull().Parameters.AddRowVersionParam(value, direction);
return (TSelf)parameters;
}
/// <summary>
/// Adds a named (<see cref="DatabaseColumns.RowVersionName"/>) parameter when invoked with a non-default value using the <see cref="IReadOnlyETag.ETag"/> <see cref="IDatabase.RowVersionConverter"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="value">The <see langword="string"/>-based <see cref="IReadOnlyETag.ETag"/> representation of the row version which to verify is non-default that is also used as the parameter value.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf RowVersionParamWith<TSelf>(this IDatabaseParameters<TSelf> parameters, string? value, ParameterDirection direction = ParameterDirection.Input)
{
if (!string.IsNullOrEmpty(value))
parameters.ThrowIfNull().Parameters.AddRowVersionParam(value, direction);
return (TSelf)parameters;
}
#endregion
#region ReselectRecordParam
/// <summary>
/// Adds a named parameter (<see cref="Extended.DatabaseColumns.ReselectRecordName"/>) to <paramref name="reselect"/> the data.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="reselect">Indicates whether to reselect after the operation.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf ReselectRecordParam<TSelf>(this IDatabaseParameters<TSelf> parameters, bool reselect = true)
{
parameters.ThrowIfNull().Parameters.AddReselectRecordParam(reselect);
return (TSelf)parameters;
}
#endregion
#region PagingParams
/// <summary>
/// Adds the <see cref="PagingArgs"/> as parameters.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="paging">The <see cref="PagingArgs"/>.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf PagingParams<TSelf>(this IDatabaseParameters<TSelf> parameters, PagingArgs? paging)
{
if (paging is not null)
{
parameters.Param(parameters.Database.NamedColumns.PagingSkipName, paging.Skip);
parameters.Param(parameters.Database.NamedColumns.PagingTakeName, paging.Take);
parameters.ParamWhen(paging.IsCountRequested, parameters.Database.NamedColumns.PagingCountName, () => paging.IsCountRequested);
}
return (TSelf)parameters;
}
#endregion
#region ReturnValue
/// <summary>
/// Adds a <see cref="ParameterDirection.ReturnValue"/> parameter to the <see cref="IDatabaseParameters{TSelf}"/>.
/// </summary>
/// <typeparam name="TSelf">The owning <see cref="Type"/>.</typeparam>
/// <param name="parameters">The <see cref="IDatabaseParameters{TSelf}"/>.</param>
/// <param name="returnValueParameter">The resulting <see cref="DbParameter"/>.</param>
/// <param name="dbType">The parameter <see cref="DbType"/>; defaults to <see cref="DbType.Int32"/>.</param>
/// <returns>The <paramref name="parameters"/> to support fluent-style method-chaining.</returns>
public static TSelf ReturnValue<TSelf>(this IDatabaseParameters<TSelf> parameters, out DbParameter returnValueParameter, DbType dbType = DbType.Int32)
{
returnValueParameter = parameters.ThrowIfNull().Parameters.AddParameter(parameters.Database.NamedColumns.ReturnValueName, dbType, direction: ParameterDirection.ReturnValue);
return (TSelf)parameters;
}
#endregion
/// <summary>
/// Sets the <see cref="DbParameter.Direction"/> to the <paramref name="direction"/> when the <paramref name="operationType"/> is as expressed by the <paramref name="when"/>.
/// </summary>
/// <param name="parameter">The <see cref="DbParameter"/>.</param>
/// <param name="operationType">The single <see cref="OperationType"/> value being performed to enable conditional execution where appropriate.</param>
/// <param name="when">The single or multi <see cref="OperationType"/> expression.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The <paramref name="parameter"/> to support fluent-style method-chaining.</returns>
/// <remarks>When the <paramref name="operationType"/> is <i>not</i> as expressed by the <paramref name="when"/> then the existing <see cref="DbParameter.Direction"/> will remain unchanged.</remarks>
public static DbParameter SetDirectionWhenOperationType(this DbParameter parameter, OperationType operationType, OperationType when, ParameterDirection direction = ParameterDirection.Output)
{
if (when.HasFlag(operationType))
parameter.Direction = direction;
return parameter;
}
}