1
+ using System . Data ;
2
+ using System . Data . Common ;
3
+
4
+ using Dapper ;
5
+
6
+ using static Dapper . SqlMapper ;
7
+
1
8
namespace FluentCommand ;
2
9
3
10
/// <summary>
4
11
/// Extension methods for <see cref="IDataCommand"/>
5
12
/// </summary>
6
13
public static class DataCommandExtensions
7
14
{
8
- /// <summary>
9
- /// Executes the command against the connection and converts the results to dynamic objects.
10
- /// </summary>
11
- /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
12
- /// <returns>
13
- /// An <see cref="T:System.Collections.Generic.IEnumerable`1" /> of dynamic objects.
14
- /// </returns>
15
- public static IEnumerable < dynamic > Query ( this IDataQuery dataQuery )
16
- {
17
- return dataQuery . Query ( ReaderFactory . DynamicFactory ) ;
18
- }
19
15
20
16
/// <summary>
21
17
/// Executes the command against the connection and converts the results to <typeparamref name="TEntity" /> objects.
@@ -28,20 +24,20 @@ public static IEnumerable<dynamic> Query(this IDataQuery dataQuery)
28
24
public static IEnumerable < TEntity > Query < TEntity > ( this IDataQuery dataQuery )
29
25
where TEntity : class , new ( )
30
26
{
31
- return dataQuery . Query ( ReaderFactory . EntityFactory < TEntity > ) ;
32
- }
27
+ var results = new List < TEntity > ( ) ;
33
28
29
+ dataQuery . Read ( reader =>
30
+ {
31
+ var parser = reader . GetRowParser < TEntity > ( ) ;
34
32
35
- /// <summary>
36
- /// Executes the query and returns the first row in the result as a dynamic object.
37
- /// </summary>
38
- /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
39
- /// <returns>
40
- /// A instance of a dynamic object if row exists; otherwise null.
41
- /// </returns>
42
- public static dynamic QuerySingle ( this IDataQuery dataQuery )
43
- {
44
- return dataQuery . QuerySingle ( ReaderFactory . DynamicFactory ) ;
33
+ while ( reader . Read ( ) )
34
+ {
35
+ var entity = parser ( reader ) ;
36
+ results . Add ( entity ) ;
37
+ }
38
+ } , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult ) ;
39
+
40
+ return results ;
45
41
}
46
42
47
43
/// <summary>
@@ -55,23 +51,20 @@ public static dynamic QuerySingle(this IDataQuery dataQuery)
55
51
public static TEntity QuerySingle < TEntity > ( this IDataQuery dataQuery )
56
52
where TEntity : class
57
53
{
58
- return dataQuery . QuerySingle ( ReaderFactory . EntityFactory < TEntity > ) ;
59
- }
54
+ TEntity result = default ;
60
55
56
+ dataQuery . Read ( reader =>
57
+ {
58
+ var parser = reader . GetRowParser < TEntity > ( ) ;
59
+ if ( reader . Read ( ) )
60
+ result = parser ( reader ) ;
61
61
62
- /// <summary>
63
- /// Executes the command against the connection and converts the results to dynamic objects asynchronously.
64
- /// </summary>
65
- /// <param name="dataQuery">The <see cref="IDataQueryAsync"/> for this extension method.</param>
66
- /// <param name="cancellationToken">The cancellation instruction.</param>
67
- /// <returns>
68
- /// An <see cref="T:System.Collections.Generic.IEnumerable`1" /> of dynamic objects.
69
- /// </returns>
70
- public static Task < IEnumerable < dynamic > > QueryAsync ( this IDataQueryAsync dataQuery , CancellationToken cancellationToken = default ( CancellationToken ) )
71
- {
72
- return dataQuery . QueryAsync ( ReaderFactory . DynamicFactory , cancellationToken ) ;
62
+ } , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult | CommandBehavior . SingleRow ) ;
63
+
64
+ return result ;
73
65
}
74
66
67
+
75
68
/// <summary>
76
69
/// Executes the command against the connection and converts the results to <typeparamref name="TEntity" /> objects asynchronously.
77
70
/// </summary>
@@ -81,24 +74,34 @@ public static TEntity QuerySingle<TEntity>(this IDataQuery dataQuery)
81
74
/// <returns>
82
75
/// An <see cref="T:System.Collections.Generic.IEnumerable`1" /> of <typeparamref name="TEntity" /> objects.
83
76
/// </returns>
84
- public static Task < IEnumerable < TEntity > > QueryAsync < TEntity > ( this IDataQueryAsync dataQuery , CancellationToken cancellationToken = default ( CancellationToken ) )
77
+ public static async Task < IEnumerable < TEntity > > QueryAsync < TEntity > ( this IDataQueryAsync dataQuery , CancellationToken cancellationToken = default ( CancellationToken ) )
85
78
where TEntity : class
86
79
{
87
- return dataQuery . QueryAsync ( ReaderFactory . EntityFactory < TEntity > , cancellationToken ) ;
88
- }
80
+ var results = new List < TEntity > ( ) ;
89
81
82
+ await dataQuery . ReadAsync ( async ( reader , token ) =>
83
+ {
84
+ var parser = reader . GetRowParser < TEntity > ( ) ;
90
85
91
- /// <summary>
92
- /// Executes the query and returns the first row in the result as a dynamic object asynchronously.
93
- /// </summary>
94
- /// <param name="dataQuery">The <see cref="IDataQueryAsync"/> for this extension method.</param>
95
- /// <param name="cancellationToken">The cancellation instruction.</param>
96
- /// <returns>
97
- /// A instance of a dynamic object if row exists; otherwise null.
98
- /// </returns>
99
- public static Task < dynamic > QuerySingleAsync ( this IDataQueryAsync dataQuery , CancellationToken cancellationToken = default ( CancellationToken ) )
100
- {
101
- return dataQuery . QuerySingleAsync ( ReaderFactory . DynamicFactory , cancellationToken ) ;
86
+ if ( reader is DbDataReader dataReader )
87
+ {
88
+ while ( await dataReader . ReadAsync ( token ) )
89
+ {
90
+ var entity = parser ( reader ) ;
91
+ results . Add ( entity ) ;
92
+ }
93
+ }
94
+ else
95
+ {
96
+ while ( reader . Read ( ) )
97
+ {
98
+ var entity = parser ( reader ) ;
99
+ results . Add ( entity ) ;
100
+ }
101
+ }
102
+ } , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult , cancellationToken ) ;
103
+
104
+ return results ;
102
105
}
103
106
104
107
/// <summary>
@@ -110,9 +113,29 @@ public static TEntity QuerySingle<TEntity>(this IDataQuery dataQuery)
110
113
/// <returns>
111
114
/// A instance of <typeparamref name="TEntity" /> if row exists; otherwise null.
112
115
/// </returns>
113
- public static Task < TEntity > QuerySingleAsync < TEntity > ( this IDataQueryAsync dataQuery , CancellationToken cancellationToken = default ( CancellationToken ) )
116
+ public static async Task < TEntity > QuerySingleAsync < TEntity > ( this IDataQueryAsync dataQuery , CancellationToken cancellationToken = default ( CancellationToken ) )
114
117
where TEntity : class
115
118
{
116
- return dataQuery . QuerySingleAsync ( ReaderFactory . EntityFactory < TEntity > , cancellationToken ) ;
119
+ TEntity result = default ;
120
+
121
+ await dataQuery . ReadAsync ( async ( reader , token ) =>
122
+ {
123
+ var parser = reader . GetRowParser < TEntity > ( ) ;
124
+
125
+ if ( reader is DbDataReader dataReader )
126
+ {
127
+ if ( await dataReader . ReadAsync ( token ) )
128
+ result = parser ( reader ) ;
129
+ }
130
+ else
131
+ {
132
+ if ( reader . Read ( ) )
133
+ result = parser ( reader ) ;
134
+ }
135
+
136
+
137
+ } , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult | CommandBehavior . SingleRow , cancellationToken ) ;
138
+
139
+ return result ;
117
140
}
118
141
}
0 commit comments