forked from Avanade/Beef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseCommand.cs
993 lines (852 loc) · 48.6 KB
/
DatabaseCommand.cs
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using Beef.Entities;
using Beef.Mapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
using System.Web;
namespace Beef.Data.Database
{
/// <summary>
/// Encapsulates the <see cref="DbCommand"/> adding additional features to support the likes of method chaining (fluent interface).
/// </summary>
public class DatabaseCommand
{
private const string _mapperNullResultMessage = "Mapper must result in a non-null result.";
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseCommand"/> class.
/// </summary>
/// <param name="database">The <see cref="DatabaseBase"/>.</param>
/// <param name="commandText">The command text.</param>
/// <param name="commandType">The <see cref="CommandType"/>.</param>
public DatabaseCommand(DatabaseBase database, string commandText, CommandType commandType = CommandType.StoredProcedure)
{
if (string.IsNullOrEmpty(commandText))
throw new ArgumentNullException(nameof(commandText));
Database = database ?? throw new ArgumentNullException(nameof(database));
DbCommand = Database.Provider.CreateCommand();
DbCommand.CommandText = commandText;
DbCommand.CommandType = commandType;
Parameters = new DatabaseParameters(this);
}
/// <summary>
/// Gets the <see cref="DatabaseBase"/>.
/// </summary>
public DatabaseBase Database { get; private set; }
/// <summary>
/// Gets the underlying <see cref="DbCommand"/>.
/// </summary>
public DbCommand DbCommand { get; private set; }
/// <summary>
/// Gets the <see cref="DatabaseParameters"/>.
/// </summary>
public DatabaseParameters Parameters { get; private set; }
#region Param
/// <summary>
/// Adds a named parameter and value.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
public DatabaseCommand Param(string name, object value, ParameterDirection direction = ParameterDirection.Input)
{
Parameters.AddParameter(name, value, direction);
return this;
}
/// <summary>
/// Adds a named parameter, <see cref="DbType"/> and value.
/// </summary>
/// <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 current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
public DatabaseCommand Param(string name, object value, DbType dbType, ParameterDirection direction = ParameterDirection.Input)
{
Parameters.AddParameter(name, value, dbType, direction);
return this;
}
/// <summary>
/// Adds a named parameter, <see cref="SqlDbType"/> and value.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <param name="sqlDbType">The parameter <see cref="SqlDbType"/>.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (default to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
/// <remarks>This specifically implies that the <see cref="Microsoft.Data.SqlClient.SqlParameter"/> is being used; if not then an exception will be thrown.</remarks>
public DatabaseCommand Param(string name, object value, SqlDbType sqlDbType, ParameterDirection direction = ParameterDirection.Input)
{
Parameters.AddParameter(name, value, sqlDbType, direction);
return this;
}
/// <summary>
/// Adds a named parameter with specified <see cref="DbType"/>.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="dbType">The <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 current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
public DatabaseCommand Param(string name, DbType dbType, int size = 0, ParameterDirection direction = ParameterDirection.Input)
{
Parameters.AddParameter(name, dbType, size, direction);
return this;
}
/// <summary>
/// Adds a named parameter with specified <see cref="SqlDbType"/>.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="sqlDbType">The <see cref="SqlDbType"/>.</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 current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
/// <remarks>This specifically implies that the <see cref="Microsoft.Data.SqlClient.SqlParameter"/> is being used; if not then an exception will be thrown.</remarks>
public DatabaseCommand Param(string name, SqlDbType sqlDbType, int size = 0, ParameterDirection direction = ParameterDirection.Input)
{
Parameters.AddParameter(name, sqlDbType, size, direction);
return this;
}
/// <summary>
/// Adds a named <b>SQL Server</b> <see cref="TableValuedParameter"/>.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="tvp">The <see cref="TableValuedParameter"/>.</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
public DatabaseCommand Param(string name, TableValuedParameter tvp)
{
Parameters.AddTableValuedParameter(name, tvp);
return this;
}
#endregion
#region RowVersionParam
/// <summary>
/// Adds a named <b>RowVersion</b> parameter.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="value">The <b>RowVersion</b> parameter value.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (defaults to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
/// <remarks>The <b>RowVersion</b> <see cref="byte"/> array will be converted from an <see cref="HttpUtility.UrlDecode(string)">encoded</see> <see cref="string"/> value.</remarks>
public DatabaseCommand RowVersionParam(string name, string value, ParameterDirection direction = ParameterDirection.Input)
{
Parameters.AddRowVersionParameter(name, value, direction);
return this;
}
/// <summary>
/// Adds a <b>RowVersion</b> (named <see cref="DatabaseColumns.RowVersionName"/>) parameter.
/// </summary>
/// <param name="value">The <b>RowVersion</b> parameter value.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (defaults to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
/// <remarks>The <b>RowVersion</b> <see cref="byte"/> array will be converted from an <see cref="HttpUtility.UrlDecode(string)">encoded</see> <see cref="string"/> value.</remarks>
public DatabaseCommand RowVersionParam(string value, ParameterDirection direction = ParameterDirection.Input)
{
Parameters.AddRowVersionParameter(value, direction);
return this;
}
/// <summary>
/// Adds a named <b>RowVersion</b> parameter where the <paramref name="value"/> implements <see cref="IETag"/>.
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="name">The parameter name.</param>
/// <param name="value">The value.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (defaults to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
/// <remarks>The <b>RowVersion</b> <see cref="byte"/> array will be converted from an <see cref="HttpUtility.UrlDecode(string)">encoded</see> <see cref="string"/> value.</remarks>
public DatabaseCommand RowVersionParam<T>(string name, T value, ParameterDirection direction = ParameterDirection.Input) where T : class
{
Parameters.AddRowVersionParameter<T>(name, value, direction);
return this;
}
/// <summary>
/// Adds a named <b>RowVersion</b> parameter (named <see cref="DatabaseColumns.RowVersionName"/>) where the <paramref name="value"/> implements <see cref="IETag"/>.
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="value">The value.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (defaults to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
/// <remarks>The <b>RowVersion</b> <see cref="byte"/> array will be converted from an <see cref="HttpUtility.UrlDecode(string)">encoded</see> <see cref="string"/> value.</remarks>
public DatabaseCommand RowVersionParam<T>(T value, ParameterDirection direction = ParameterDirection.Input) where T : class
{
Parameters.AddRowVersionParameter<T>(value, direction);
return this;
}
#endregion
#region ReturnValueParam
/// <summary>
/// Adds a <see cref="ParameterDirection.ReturnValue"/> (named <see cref="DatabaseColumns.ReturnValueName"/>) parameter to the command.
/// </summary>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
public DatabaseCommand ReturnValueParam()
{
Parameters.AddReturnValueParameter();
return this;
}
#endregion
#region ChangeLogParams
/// <summary>
/// Adds the <see cref="ChangeLog"/> parameters.
/// </summary>
/// <param name="changeLog">The <see cref="ChangeLog"/> value.</param>
/// <param name="addCreatedParams">Indicates whether to add the <b>created</b> <see cref="IChangeLog"/> parameters.</param>
/// <param name="addUpdatedParams">Indicates whether to add the <b>updated</b> <see cref="IChangeLog"/> parameters.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (defaults to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
/// <remarks>Uses the following parameter names: <see cref="DatabaseColumns.CreatedByName"/>, <see cref="DatabaseColumns.CreatedDateName"/>,
/// <see cref="DatabaseColumns.UpdatedByName"/> and <see cref="DatabaseColumns.UpdatedDateName"/>.</remarks>
public DatabaseCommand ChangeLogParams(ChangeLog changeLog, bool addCreatedParams = false, bool addUpdatedParams = false, ParameterDirection direction = ParameterDirection.Input)
{
Parameters.AddChangeLogParameters(changeLog, addCreatedParams, addUpdatedParams, direction);
return this;
}
/// <summary>
/// Adds the <see cref="ChangeLog"/> parameters where the <paramref name="value"/> implements <see cref="IChangeLog"/>.
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="value">The value.</param>
/// <param name="addCreatedParams">Indicates whether to add the <b>created</b> <see cref="IChangeLog"/> parameters.</param>
/// <param name="addUpdatedParams">Indicates whether to add the <b>updated</b> <see cref="IChangeLog"/> parameters.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (defaults to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
/// <remarks>Uses the following parameter names: <see cref="DatabaseColumns.CreatedByName"/>, <see cref="DatabaseColumns.CreatedDateName"/>,
/// <see cref="DatabaseColumns.UpdatedByName"/> and <see cref="DatabaseColumns.UpdatedDateName"/>.</remarks>
public DatabaseCommand ChangeLogParams<T>(T value, bool addCreatedParams = false, bool addUpdatedParams = false, ParameterDirection direction = ParameterDirection.Input) where T : class
{
Parameters.AddChangeLogParameters<T>(value, addCreatedParams, addUpdatedParams, direction);
return this;
}
#endregion
#region PagingsParams
/// <summary>
/// Adds the <see cref="PagingArgs"/> parameters.
/// </summary>
/// <param name="paging">The <see cref="PagingArgs"/> value.</param>
/// <param name="direction">The <see cref="ParameterDirection"/> (defaults to <see cref="ParameterDirection.Input"/>).</param>
/// <returns>An <see cref="DbParameter"/> array for those that were added.</returns>
/// <remarks>Uses the following parameter names: <see cref="DatabaseColumns.PagingSkipName"/> and <see cref="DatabaseColumns.PagingTakeName"/>.</remarks>
public DatabaseCommand PagingParams(PagingArgs paging, ParameterDirection direction = ParameterDirection.Input)
{
Parameters.AddPagingParameters(paging, direction);
return this;
}
#endregion
#region TableValuedParam
/// <summary>
/// Adds a <b>SQL Server</b> <see cref="TableValuedParameter"/>.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="tvp">The <see cref="TableValuedParameter"/>.</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
/// <remarks>This specifically implies that the <see cref="SqlParameter"/> is being used; if not then an exception will be thrown.</remarks>
public DatabaseCommand TableValuedParam(string name, TableValuedParameter tvp)
{
Parameters.TableValuedParam(name, tvp);
return this;
}
#endregion
#region ReselectRecordParam
/// <summary>
/// Adds a <b>ReselectRecord</b> (named <see cref="DatabaseColumns.ReselectRecordName"/>) parameter to the command.
/// </summary>
/// <param name="reselect">Indicates whether to reselect after the operation (defaults to <c>true</c>).</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
public DatabaseCommand ReselectRecordParam(bool reselect = true)
{
Parameters.ReselectRecordParam(reselect);
return this;
}
#endregion
#region Params
/// <summary>
/// Add one or more parameters by invoking a delegate.
/// </summary>
/// <param name="action">The delegate to enable parameter addition.</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
public DatabaseCommand Params(Action<DatabaseParameters> action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
action.Invoke(new DatabaseParameters(this));
return this;
}
/// <summary>
/// Add one or more parameters by invoking a delegate using the passed value.
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="value">The value.</param>
/// <param name="action">The delegate to enable parameter addition.</param>
/// <param name="operationType">The single <see cref="Beef.Mapper.OperationTypes"/> being performed to enable selection.</param>
/// <returns>The current <see cref="DatabaseCommand"/> instance to support chaining (fluent interface).</returns>
public DatabaseCommand Params<T>(T value, Action<T, DatabaseParameters, OperationTypes, object> action, OperationTypes operationType)
{
action?.Invoke(value, new DatabaseParameters(this), operationType, null!);
return this;
}
#endregion
#region DbCommandWrapper
/// <summary>
/// Wraps the "DbCommand" invocation.
/// </summary>
private Task<T> NonQueryScalarDbCommandWrapperAsync<T>(Func<CommandBehavior, Task<T>> func)
{
CommandBehavior behavior = CommandBehavior.Default;
return Database.Invoker.InvokeAsync(this, async () =>
{
try
{
behavior = DetermineBehavior(DbCommand);
return await func(behavior).ConfigureAwait(false);
}
finally
{
// Close the connection where specified in behavior.
if (behavior == CommandBehavior.CloseConnection)
{
Database.Logger.LogInformation("Database connection is being closed (CommandBehavior.CloseConnection).");
DbCommand.Connection.Close();
}
DbCommand.Dispose();
}
}, Database);
}
/// <summary>
/// Wraps the "DbDataReader" invocation.
/// </summary>
private Task<T> DbDataReaderWrapperWithResultAsync<T>(Func<DbDataReader, Task<T>> func)
{
DbDataReader? dr = null;
CommandBehavior behavior = CommandBehavior.Default;
return Database.Invoker.InvokeAsync(this, async () =>
{
try
{
// Execute the data reader!
dr = await DbCommand.ExecuteReaderAsync(DetermineBehavior(DbCommand)).ConfigureAwait(false);
// Execute the specific action.
return await func(dr).ConfigureAwait(false);
}
finally
{
// Close and dispose the data reader.
if (dr != null)
dr.Dispose();
// Close the connection where specified in behavior.
if (behavior == CommandBehavior.CloseConnection)
DbCommand.Connection.Close();
DbCommand.Dispose();
}
}, Database);
}
/// <summary>
/// Wraps the "DbDataReader" invocation.
/// </summary>
private async Task DbDataReaderWrapperNoResultAsync(Func<DbDataReader, Task> func)
{
DbDataReader? dr = null;
CommandBehavior behavior = CommandBehavior.Default;
await Database.Invoker.InvokeAsync(this, async () =>
{
try
{
// Execute the data reader!
dr = await DbCommand.ExecuteReaderAsync(DetermineBehavior(DbCommand)).ConfigureAwait(false);
// Execute the specific action.
await func(dr).ConfigureAwait(false);
}
finally
{
// Close and dispose the data reader.
if (dr != null)
dr.Dispose();
// Close the connection where specified in behavior.
if (behavior == CommandBehavior.CloseConnection)
{
Database.Logger.LogInformation("Database connection is being closed (CommandBehavior.CloseConnection).");
DbCommand.Connection.Close();
}
DbCommand.Dispose();
}
}, Database).ConfigureAwait(false);
}
/// <summary>
/// Determine CommandBehavior from the DbCommand state.
/// </summary>
private CommandBehavior DetermineBehavior(DbCommand dbCommand)
{
// Check if there is a connection and whether it is already open and set behavior accordingly.
if (dbCommand.Connection == null)
dbCommand.Connection = Database.GetConnection();
// Where not open, we'll open and immediately close after the command has executed.
if (dbCommand.Connection.State != ConnectionState.Open)
{
Database.Logger.LogDebug("The Command Connection is not Open, is being opened and will be set to automatic CloseConnection. DatabaseId: {0}", Database.DatabaseId);
dbCommand.Connection.Open();
return CommandBehavior.CloseConnection;
}
// Leave the connection in its current state.
return CommandBehavior.Default;
}
#endregion
#region SelectSingle/SelectFirst
/// <summary>
/// Selects a single item.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for the record.</param>
/// <returns>The single item.</returns>
public async Task<T> SelectSingleAsync<T>(Func<DatabaseRecord, T> mapFromDb)
{
T item = await SelectSingleFirstInternalAsync(mapFromDb, true).ConfigureAwait(false);
if (Comparer<T>.Default.Compare(item, default!) == 0)
throw new InvalidOperationException("SelectSingle request has not returned a row.");
return item;
}
/// <summary>
/// Selects a single item using a <paramref name="mapper"/>.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
/// <returns>The single item.</returns>
public async Task<T> SelectSingleAsync<T>(IDatabaseMapper<T> mapper) where T : class, new()
{
Check.NotNull(mapper, nameof(mapper));
return await SelectSingleAsync((dr) => mapper.MapFromDb(dr, OperationTypes.Get, null) ?? throw new InvalidOperationException(_mapperNullResultMessage)).ConfigureAwait(false);
}
/// <summary>
/// Selects a single item or default.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for the record.</param>
/// <returns>The single item or default.</returns>
public async Task<T> SelectSingleOrDefaultAsync<T>(Func<DatabaseRecord, T> mapFromDb)
{
return await SelectSingleFirstInternalAsync(mapFromDb, true).ConfigureAwait(false);
}
/// <summary>
/// Selects a single item or default using a <paramref name="mapper"/>.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
/// <returns>The single item.</returns>
public async Task<T?> SelectSingleOrDefaultAsync<T>(IDatabaseMapper<T> mapper) where T : class, new()
{
Check.NotNull(mapper, nameof(mapper));
return await SelectSingleOrDefaultAsync((dr) => mapper.MapFromDb(dr, OperationTypes.Get, null!) ?? throw new InvalidOperationException(_mapperNullResultMessage)).ConfigureAwait(false);
}
/// <summary>
/// Selects first item.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for the record.</param>
/// <returns>The first item.</returns>
public async Task<T> SelectFirstAsync<T>(Func<DatabaseRecord, T> mapFromDb)
{
var item = await SelectSingleFirstInternalAsync(mapFromDb, false).ConfigureAwait(false);
if (item == null)
throw new InvalidOperationException("SelectFirst request has not returned a row.");
return item;
}
/// <summary>
/// Selects first item using a <paramref name="mapper"/>.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
/// <returns>The single item.</returns>
public async Task<T> SelectFirstAsync<T>(IDatabaseMapper<T> mapper) where T : class, new()
{
Check.NotNull(mapper, nameof(mapper));
return await SelectFirstAsync((dr) => mapper.MapFromDb(dr, OperationTypes.Get, null!) ?? throw new InvalidOperationException(_mapperNullResultMessage)).ConfigureAwait(false);
}
/// <summary>
/// Selects first item or default.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for the record.</param>
/// <returns>The single item or default.</returns>
public async Task<T> SelectFirstOrDefaultAsync<T>(Func<DatabaseRecord, T> mapFromDb)
{
return await SelectSingleFirstInternalAsync(mapFromDb, false).ConfigureAwait(false);
}
/// <summary>
/// Selects first item or default using a <paramref name="mapper"/>.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
/// <returns>The single item.</returns>
public async Task<T?> SelectFirstOrDefaultAsync<T>(IDatabaseMapper<T> mapper) where T : class, new()
{
Check.NotNull(mapper, nameof(mapper));
return await SelectFirstOrDefaultAsync((dr) => mapper.MapFromDb(dr, OperationTypes.Get, null!) ?? throw new InvalidOperationException(_mapperNullResultMessage)).ConfigureAwait(false);
}
/// <summary>
/// Performs the database select single command.
/// </summary>
private Task<T> SelectSingleFirstInternalAsync<T>(Func<DatabaseRecord, T> mapFromDb, bool throwWhereMulti)
{
if (mapFromDb == null)
throw new ArgumentNullException(nameof(mapFromDb));
return DbDataReaderWrapperWithResultAsync(async dr =>
{
T item = default!;
int i = 0;
while (await dr.ReadAsync().ConfigureAwait(false))
{
if (i++ > 0)
{
if (throwWhereMulti)
throw new InvalidOperationException("SelectSingle request has returned more than one row.");
return item;
}
item = mapFromDb(new DatabaseRecord(this, (IDataRecord)dr));
}
return item;
});
}
#endregion
#region SelectQuery
/// <summary>
/// Executes a query command.
/// </summary>
/// <param name="databaseRecord">The <see cref="DatabaseRecord"/> delegate invoked for each record.</param>
public async Task SelectQueryAsync(Action<DatabaseRecord> databaseRecord)
{
Check.NotNull(databaseRecord, nameof(databaseRecord));
await DbDataReaderWrapperNoResultAsync(async dr =>
{
while (await dr.ReadAsync().ConfigureAwait(false))
{
databaseRecord(new DatabaseRecord(this, dr));
}
}).ConfigureAwait(false);
}
/// <summary>
/// Executes a query command; whilst also returning the resulting <see cref="ParameterDirection.ReturnValue"/>.
/// </summary>
/// <param name="databaseRecord">The <see cref="DatabaseRecord"/> delegate invoked for each record.</param>
/// <returns>The resultant return value.</returns>
public async Task<int> SelectQueryWithValueAsync(Action<DatabaseRecord> databaseRecord)
{
var rvp = Parameters.AddReturnValueParameter();
await SelectQueryAsync(databaseRecord).ConfigureAwait(false);
return rvp.Value == null ? -1 : (int)rvp.Value;
}
/// <summary>
/// Executes a query command.
/// </summary>
/// <typeparam name="T">The result <see cref="Type"/>.</typeparam>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for each record.</param>
/// <returns>An <see cref="IEnumerable{T}"/>.</returns>
public async Task<IEnumerable<T>> SelectQueryAsync<T>(Func<DatabaseRecord, T> mapFromDb)
{
if (mapFromDb == null)
throw new ArgumentNullException(nameof(mapFromDb));
return await DbDataReaderWrapperWithResultAsync(async dr =>
{
var coll = new List<T>();
while (await dr.ReadAsync().ConfigureAwait(false))
{
coll.Add(mapFromDb(new DatabaseRecord(this, dr)) ?? throw new InvalidOperationException(_mapperNullResultMessage));
}
return coll;
}).ConfigureAwait(false);
}
/// <summary>
/// Executes a query command using a <paramref name="mapper"/>.
/// </summary>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
/// <returns>An <see cref="IEnumerable{TItem}"/>.</returns>
public async Task<IEnumerable<TItem>> SelectQueryAsync<TItem>(IDatabaseMapper<TItem> mapper) where TItem : class, new()
{
return await SelectQueryAsync((dr) => mapper.MapFromDb(dr, OperationTypes.Get, null!) ?? throw new InvalidOperationException(_mapperNullResultMessage)).ConfigureAwait(false);
}
/// <summary>
/// Executes a query command; whilst also outputing the resulting <see cref="ParameterDirection.ReturnValue"/>.
/// </summary>
/// <typeparam name="T">The result <see cref="Type"/>.</typeparam>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for each record.</param>
/// <returns>An <see cref="IEnumerable{TItem}"/> collection and resultant return value.</returns>
public async Task<(IEnumerable<T> items, int returnValue)> SelectQueryWithValueAsync<T>(Func<DatabaseRecord, T> mapFromDb)
{
var rvp = Parameters.AddReturnValueParameter();
var coll = await SelectQueryAsync<T>(mapFromDb).ConfigureAwait(false);
return (coll, rvp.Value == null ? -1 : (int)rvp.Value);
}
/// <summary>
/// Executes a query command using a <paramref name="mapper"/>; whilst also outputing the resulting <see cref="ParameterDirection.ReturnValue"/>.
/// </summary>
/// <typeparam name="TItem">The record <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
/// <returns>An <see cref="IEnumerable{TItem}"/> collection and resultant return value.</returns>
public async Task<(IEnumerable<TItem> coll, int returnValue)> SelectQueryWithValueAsync<TItem>(IDatabaseMapper<TItem> mapper) where TItem : class, new()
{
return await SelectQueryWithValueAsync((dr) => mapper.MapFromDb(dr, OperationTypes.Get, null!)).ConfigureAwait(false);
}
/// <summary>
/// Executes a query command creating a resultant collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for each record.</param>
/// <returns>The resultant collection.</returns>
public async Task<TColl> SelectQueryAsync<TColl, TItem>(Func<DatabaseRecord, TItem> mapFromDb) where TColl : ICollection<TItem>, new()
{
var coll = new TColl();
await SelectQueryAsync(coll, mapFromDb).ConfigureAwait(false);
return coll;
}
/// <summary>
/// Executes a query command using a <paramref name="mapper"/> creating a resultant collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
/// <returns>The resultant collection.</returns>
public async Task<TColl> SelectQueryAsync<TColl, TItem>(IDatabaseMapper<TItem> mapper) where TItem : class, new() where TColl : ICollection<TItem>, new()
{
if (mapper == null)
throw new ArgumentNullException(nameof(mapper));
return await SelectQueryAsync<TColl, TItem>((dr) => mapper.MapFromDb(dr, OperationTypes.Get, null!)!).ConfigureAwait(false);
}
/// <summary>
/// Executes a query command creating a resultant collection; whilst also outputing the resulting <see cref="ParameterDirection.ReturnValue"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for each record.</param>
/// <returns>The resultant collection and return value.</returns>
public async Task<(TColl coll, int returnValue)> SelectQueryWithValueAsync<TColl, TItem>(Func<DatabaseRecord, TItem> mapFromDb) where TColl : ICollection<TItem>, new()
{
var coll = new TColl();
var value = await SelectQueryWithValueAsync(coll, mapFromDb).ConfigureAwait(false);
return (coll, value);
}
/// <summary>
/// Executes a query command using a <paramref name="mapper"/> creating a resultant collection; whilst also outputing the resulting <see cref="ParameterDirection.ReturnValue"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
/// <returns>The resultant collection and return value.</returns>
public async Task<(TColl coll, int returnValue)> SelectQueryWithValueAsync<TColl, TItem>(IDatabaseMapper<TItem> mapper) where TItem : class, new() where TColl : ICollection<TItem>, new()
{
if (mapper == null)
throw new ArgumentNullException(nameof(mapper));
return await SelectQueryWithValueAsync<TColl, TItem>((dr) => mapper.MapFromDb(dr, OperationTypes.Get, null!)!).ConfigureAwait(false);
}
/// <summary>
/// Executes a query command adding to the passed collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="coll">The collection to add items to.</param>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for each record.</param>
public Task SelectQueryAsync<TColl, TItem>(TColl coll, Func<DatabaseRecord, TItem> mapFromDb) where TColl : ICollection<TItem>
{
if (coll == null)
throw new ArgumentNullException(nameof(coll));
if (mapFromDb == null)
throw new ArgumentNullException(nameof(mapFromDb));
return DbDataReaderWrapperNoResultAsync(async dr =>
{
while (await dr.ReadAsync().ConfigureAwait(false))
{
var item = mapFromDb(new DatabaseRecord(this, (IDataRecord)dr));
if (item != null)
coll.Add(item);
}
});
}
/// <summary>
/// Executes a query command using a <paramref name="mapper"/> adding to the passed collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="coll">The collection to add items to.</param>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
public Task SelectQueryAsync<TColl, TItem>(TColl coll, IDatabaseMapper<TItem> mapper) where TItem : class, new() where TColl : ICollection<TItem>
{
if (mapper == null)
throw new ArgumentNullException(nameof(mapper));
return SelectQueryAsync(coll, (dr) => mapper.MapFromDb(dr, OperationTypes.Get, null!)!);
}
/// <summary>
/// Executes a query command adding to the passed collection; whilst also outputing the resulting <see cref="ParameterDirection.ReturnValue"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="coll">The collection to add items to.</param>
/// <param name="mapFromDb">The <see cref="DatabaseRecord"/> delegate invoked for each record.</param>
/// <returns>An <see cref="IEnumerable{TItem}"/> collection and resultant return value.</returns>
public async Task<int> SelectQueryWithValueAsync<TColl, TItem>(TColl coll, Func<DatabaseRecord, TItem> mapFromDb) where TColl : ICollection<TItem>
{
var rvp = Parameters.AddReturnValueParameter();
await SelectQueryAsync(coll, mapFromDb).ConfigureAwait(false);
return rvp.Value == null ? -1 : (int)rvp.Value;
}
/// <summary>
/// Executes a query command using a <paramref name="mapper"/> adding to the passed collection; whilst also outputing the resulting <see cref="ParameterDirection.ReturnValue"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="coll">The collection to add items to.</param>
/// <param name="mapper">The <see cref="IDatabaseMapper{T}"/>.</param>
/// <returns>The resultant return value.</returns>
public async Task<int> SelectQueryWithValueAsync<TColl, TItem>(TColl coll, IDatabaseMapper<TItem> mapper) where TItem : class, new() where TColl : ICollection<TItem>
{
if (mapper == null)
throw new ArgumentNullException(nameof(mapper));
return await SelectQueryWithValueAsync(coll, (dr) => mapper.MapFromDb(dr, OperationTypes.Get, null!)!).ConfigureAwait(false);
}
#endregion
#region SelectQueryMultiSet
/// <summary>
/// Executes a multi-dataset query command.
/// </summary>
/// <param name="datasetRecord">An array of <see cref="DatabaseRecord"/> delegates invoked for each record for their respective dataset.</param>
/// <remarks>The number of delegates specified must match the number of returned datasets. A null dataset indicates to ignore (skip) a dataset.</remarks>
public async Task SelectQueryMultiSetAsync(params Action<DatabaseRecord>[] datasetRecord)
{
if (datasetRecord == null)
throw new ArgumentNullException(nameof(datasetRecord));
await DbDataReaderWrapperNoResultAsync(async dr =>
{
var index = 0;
do
{
if (index >= datasetRecord.Length)
throw new InvalidOperationException($"SelectQueryMultiSet has returned more record sets than expected ({datasetRecord.Length}).");
if (datasetRecord[index] != null)
{
while (await dr.ReadAsync().ConfigureAwait(false))
{
datasetRecord[index](new DatabaseRecord(this, dr));
}
}
index++;
} while (await dr.NextResultAsync().ConfigureAwait(false));
if (index < datasetRecord.Length)
throw new InvalidOperationException($"SelectQueryMultiSet has returned less ({index}) record sets than expected ({datasetRecord.Length}).");
}).ConfigureAwait(false);
}
/// <summary>
/// Executes a multi-dataset query command; whilst also outputing the resulting <see cref="ParameterDirection.ReturnValue"/>.
/// </summary>
/// <param name="datasetRecord">An array of <see cref="DatabaseRecord"/> delegates invoked for each record for their respective dataset.</param>
/// <returns>The resultant return value.</returns>
/// <remarks>The number of delegates specified must match the number of returned datasets. A null dataset indicates to ignore (skip) a dataset.</remarks>
public async Task<int> SelectQueryMultiSetWithValueAsync(params Action<DatabaseRecord>[] datasetRecord)
{
var rvp = Parameters.AddReturnValueParameter();
await SelectQueryMultiSetAsync(datasetRecord).ConfigureAwait(false);
return rvp.Value == null ? -1 : (int)rvp.Value;
}
/// <summary>
/// Executes a multi-dataset query command with one or more <see cref="IMultiSetArgs"/>.
/// </summary>
/// <param name="multiSetArgs">One or more <see cref="IMultiSetArgs"/>.</param>
/// <remarks>The number of <see cref="IMultiSetArgs"/> specified must match the number of returned datasets. A null dataset indicates to ignore (skip) a dataset.</remarks>
public Task SelectQueryMultiSetAsync(params IMultiSetArgs[] multiSetArgs)
{
Check.NotNull(multiSetArgs, nameof(multiSetArgs));
return DbDataReaderWrapperNoResultAsync(async dr =>
{
var index = 0;
var records = 0;
IMultiSetArgs? multiSetArg = null;
do
{
if (index >= multiSetArgs.Length)
throw new InvalidOperationException($"SelectQueryMultiSet has returned more record sets than expected ({multiSetArgs.Length}).");
if (multiSetArgs[index] != null)
{
records = 0;
multiSetArg = multiSetArgs[index];
while (await dr.ReadAsync().ConfigureAwait(false))
{
records++;
if (multiSetArg.MaxRows.HasValue && records > multiSetArg.MaxRows.Value)
throw new InvalidOperationException($"SelectQueryMultiSet (multiSetArgs[{index}]) has returned more records than expected ({multiSetArg.MaxRows.Value}).");
var databaseRecord = new DatabaseRecord(this, dr);
if (multiSetArg.StopOnPredicate != null && multiSetArg.StopOnPredicate(databaseRecord))
return;
multiSetArg.DatasetRecord(databaseRecord);
}
if (records < multiSetArg.MinRows)
throw new InvalidOperationException($"SelectQueryMultiSet (multiSetArgs[{index}]) has returned less records ({records}) than expected ({multiSetArg.MinRows}).");
if (records == 0 && multiSetArg.StopOnNull)
return;
multiSetArg.InvokeResult();
}
index++;
} while (dr.NextResult());
if (index < multiSetArgs.Length && !multiSetArgs[index].StopOnNull)
throw new InvalidOperationException($"SelectQueryMultiSet has returned less ({index}) record sets than expected ({multiSetArgs.Length}).");
});
}
/// <summary>
/// Executes a multi-dataset query command with one or more <see cref="IMultiSetArgs"/>; whilst also outputing the resulting <see cref="ParameterDirection.ReturnValue"/>.
/// </summary>
/// <param name="multiSetArgs">One or more <see cref="IMultiSetArgs"/>.</param>
/// <returns>The resultant return value.</returns>
/// <remarks>The number of <see cref="IMultiSetArgs"/> specified must match the number of returned datasets. A null dataset indicates to ignore (skip) a dataset.</remarks>
public async Task<int> SelectQueryMultiSetWithValueAsync(params IMultiSetArgs[] multiSetArgs)
{
var rvp = Parameters.AddReturnValueParameter();
await SelectQueryMultiSetAsync(multiSetArgs).ConfigureAwait(false);
return rvp.Value == null ? -1 : (int)rvp.Value;
}
/// <summary>
/// Executes a multi-dataset query command with one or more <see cref="IMultiSetArgs"/> that supports <paramref name="paging"/>.
/// </summary>
/// <param name="paging">The <see cref="PagingResult"/>.</param>
/// <param name="multiSetArgs">One or more <see cref="IMultiSetArgs"/>.</param>
/// <returns>The resulting <see cref="PagingResult"/>.</returns>
public async Task SelectQueryMultiSetAsync(PagingResult paging, params IMultiSetArgs[] multiSetArgs)
{
Parameters.PagingParams(paging);
var rv = await SelectQueryMultiSetWithValueAsync(multiSetArgs).ConfigureAwait(false);
if (paging != null && paging.IsGetCount && rv > 0)
paging.TotalCount = rv;
}
#endregion
#region NonQuery/Scalar
/// <summary>
/// Executes a non-query command.
/// </summary>
/// <param name="action">The post-execution delegate to enable parameter access.</param>
/// <returns>The number of rows affected.</returns>
public Task<int> NonQueryAsync(Action<DbParameterCollection>? action = null)
{
return NonQueryScalarDbCommandWrapperAsync(async behaviour =>
{
int result = await DbCommand.ExecuteNonQueryAsync().ConfigureAwait(false);
action?.Invoke(DbCommand.Parameters);
return result;
});
}
/// <summary>
/// Executes the query and returns the first column of the first row in the result set returned by the query.
/// </summary>
/// <typeparam name="T">The result <see cref="Type"/>.</typeparam>
/// <param name="action">The post-execution delegate to enable parameter access.</param>
/// <returns>The value of the first column of the first row in the result set.</returns>
public Task<T> ScalarAsync<T>(Action<DbParameterCollection>? action = null)
{
return NonQueryScalarDbCommandWrapperAsync(async behaviour =>
{
var result = await DbCommand.ExecuteScalarAsync().ConfigureAwait(false);
var value = result is DBNull ? default : (T)result;
action?.Invoke(DbCommand.Parameters);
return value!;
});
}
#endregion
}
}