Skip to content

Commit 73bed62

Browse files
committed
Allow async methods to be used with IDbConnection instances
1 parent 13fdd2b commit 73bed62

File tree

2 files changed

+93
-93
lines changed

2 files changed

+93
-93
lines changed

src/Sql/ConnectionExtensions.Async.cs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,171 +13,171 @@ public static partial class ConnectionExtensions {
1313
/// Executes a parameterized SQL statement.
1414
/// </summary>
1515
/// <param name="connection">The connection to the data source.</param>
16-
/// <param name="command">The SQL query to be executed.</param>
16+
/// <param name="sql">The SQL query to be executed.</param>
1717
/// <param name="parameters">The parameters of the SQL query.</param>
1818
/// <param name="options">The command options.</param>
1919
/// <param name="cancellationToken">The token to cancel the operation.</param>
2020
/// <returns>The number of rows affected.</returns>
21-
public static async Task<int> ExecuteAsync(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) {
22-
if (connection.State == ConnectionState.Closed) await connection.OpenAsync(cancellationToken);
23-
using var dbCommand = (DbCommand) CreateCommand(connection, command, parameters, options);
24-
return await dbCommand.ExecuteNonQueryAsync(cancellationToken);
21+
public static async Task<int> ExecuteAsync(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) {
22+
if (connection.State == ConnectionState.Closed) await ((DbConnection) connection).OpenAsync(cancellationToken);
23+
using var command = (DbCommand) CreateCommand(connection, sql, parameters, options);
24+
return await command.ExecuteNonQueryAsync(cancellationToken);
2525
}
2626

2727
/// <summary>
2828
/// Executes a parameterized SQL query and returns a data reader.
2929
/// </summary>
3030
/// <param name="connection">The connection to the data source.</param>
31-
/// <param name="command">The SQL query to be executed.</param>
31+
/// <param name="sql">The SQL query to be executed.</param>
3232
/// <param name="parameters">The parameters of the SQL query.</param>
3333
/// <param name="options">The command options.</param>
3434
/// <param name="cancellationToken">The token to cancel the operation.</param>
3535
/// <returns>The data reader that can be used to access the results.</returns>
36-
public static async Task<IDataReader> ExecuteReaderAsync(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) {
37-
if (connection.State == ConnectionState.Closed) await connection.OpenAsync(cancellationToken);
38-
using var dbCommand = (DbCommand) CreateCommand(connection, command, parameters, options);
39-
return await dbCommand.ExecuteReaderAsync(cancellationToken);
36+
public static async Task<IDataReader> ExecuteReaderAsync(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) {
37+
if (connection.State == ConnectionState.Closed) await ((DbConnection) connection).OpenAsync(cancellationToken);
38+
using var command = (DbCommand) CreateCommand(connection, sql, parameters, options);
39+
return await command.ExecuteReaderAsync(cancellationToken);
4040
}
4141

4242
/// <summary>
4343
/// Executes a parameterized SQL query that selects a single value.
4444
/// </summary>
4545
/// <param name="connection">The connection to the data source.</param>
46-
/// <param name="command">The SQL query to be executed.</param>
46+
/// <param name="sql">The SQL query to be executed.</param>
4747
/// <param name="parameters">The parameters of the SQL query.</param>
4848
/// <param name="options">The command options.</param>
4949
/// <param name="cancellationToken">The token to cancel the operation.</param>
5050
/// <returns>The first column of the first row.</returns>
51-
public static async Task<object?> ExecuteScalarAsync(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
52-
ExecuteScalarAsync<object>(connection, command, parameters, options, cancellationToken);
51+
public static async Task<object?> ExecuteScalarAsync(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
52+
ExecuteScalarAsync<object>(connection, sql, parameters, options, cancellationToken);
5353

5454
/// <summary>
5555
/// Executes a parameterized SQL query that selects a single value.
5656
/// </summary>
5757
/// <typeparam name="T">The type of object to return.</typeparam>
5858
/// <param name="connection">The connection to the data source.</param>
59-
/// <param name="command">The SQL query to be executed.</param>
59+
/// <param name="sql">The SQL query to be executed.</param>
6060
/// <param name="parameters">The parameters of the SQL query.</param>
6161
/// <param name="options">The command options.</param>
6262
/// <param name="cancellationToken">The token to cancel the operation.</param>
6363
/// <returns>The first column of the first row.</returns>
64-
public static async Task<T?> ExecuteScalarAsync<T>(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) {
65-
if (connection.State == ConnectionState.Closed) await connection.OpenAsync(cancellationToken);
66-
using var dbCommand = (DbCommand) CreateCommand(connection, command, parameters, options);
67-
var value = await dbCommand.ExecuteScalarAsync(cancellationToken);
64+
public static async Task<T?> ExecuteScalarAsync<T>(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) {
65+
if (connection.State == ConnectionState.Closed) await ((DbConnection) connection).OpenAsync(cancellationToken);
66+
using var command = (DbCommand) CreateCommand(connection, sql, parameters, options);
67+
var value = await command.ExecuteScalarAsync(cancellationToken);
6868
return value is null || value is DBNull ? default : (T?) mapper.ChangeType(value, typeof(T));
6969
}
7070

7171
/// <summary>
7272
/// Executes a parameterized SQL query and returns a sequence of objects whose properties correspond to the columns.
7373
/// </summary>
7474
/// <param name="connection">The connection to the data source.</param>
75-
/// <param name="command">The SQL query to be executed.</param>
75+
/// <param name="sql">The SQL query to be executed.</param>
7676
/// <param name="parameters">The parameters of the SQL query.</param>
7777
/// <param name="options">The command options.</param>
7878
/// <param name="cancellationToken">The token to cancel the operation.</param>
7979
/// <returns>The sequence of objects whose properties correspond to the columns.</returns>
80-
public static async Task<IEnumerable<dynamic>> QueryAsync(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
81-
await QueryAsync<ExpandoObject>(connection, command, parameters, options, cancellationToken);
80+
public static async Task<IEnumerable<dynamic>> QueryAsync(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
81+
await QueryAsync<ExpandoObject>(connection, sql, parameters, options, cancellationToken);
8282

8383
/// <summary>
8484
/// Executes a parameterized SQL query and returns a sequence of objects whose properties correspond to the columns.
8585
/// </summary>
8686
/// <typeparam name="T">The type of objects to return.</typeparam>
8787
/// <param name="connection">The connection to the data source.</param>
88-
/// <param name="command">The SQL query to be executed.</param>
88+
/// <param name="sql">The SQL query to be executed.</param>
8989
/// <param name="parameters">The parameters of the SQL query.</param>
9090
/// <param name="options">The command options.</param>
9191
/// <param name="cancellationToken">The token to cancel the operation.</param>
9292
/// <returns>The sequence of objects whose properties correspond to the columns.</returns>
93-
public static async Task<IEnumerable<T>> QueryAsync<T>(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() =>
94-
mapper.CreateInstances<T>(await ExecuteReaderAsync(connection, command, parameters, options, cancellationToken));
93+
public static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() =>
94+
mapper.CreateInstances<T>(await ExecuteReaderAsync(connection, sql, parameters, options, cancellationToken));
9595

9696
/// <summary>
9797
/// Executes a parameterized SQL query and returns the first row.
9898
/// </summary>
9999
/// <param name="connection">The connection to the data source.</param>
100-
/// <param name="command">The SQL query to be executed.</param>
100+
/// <param name="sql">The SQL query to be executed.</param>
101101
/// <param name="parameters">The parameters of the SQL query.</param>
102102
/// <param name="options">The command options.</param>
103103
/// <param name="cancellationToken">The token to cancel the operation.</param>
104104
/// <returns>The first row.</returns>
105105
/// <exception cref="InvalidOperationException">The result set is empty.</exception>
106-
public static async Task<dynamic> QueryFirstAsync(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
107-
await QueryFirstAsync<ExpandoObject>(connection, command, parameters, options, cancellationToken);
106+
public static async Task<dynamic> QueryFirstAsync(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
107+
await QueryFirstAsync<ExpandoObject>(connection, sql, parameters, options, cancellationToken);
108108

109109
/// <summary>
110110
/// Executes a parameterized SQL query and returns the first row.
111111
/// </summary>
112112
/// <typeparam name="T">The type of objects to return.</typeparam>
113113
/// <param name="connection">The connection to the data source.</param>
114-
/// <param name="command">The SQL query to be executed.</param>
114+
/// <param name="sql">The SQL query to be executed.</param>
115115
/// <param name="parameters">The parameters of the SQL query.</param>
116116
/// <param name="options">The command options.</param>
117117
/// <param name="cancellationToken">The token to cancel the operation.</param>
118118
/// <returns>The first row.</returns>
119119
/// <exception cref="InvalidOperationException">The result set is empty.</exception>
120-
public static async Task<T> QueryFirstAsync<T>(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() {
121-
using var reader = await ExecuteReaderAsync(connection, command, parameters, options, cancellationToken);
120+
public static async Task<T> QueryFirstAsync<T>(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() {
121+
using var reader = await ExecuteReaderAsync(connection, sql, parameters, options, cancellationToken);
122122
return reader.Read() ? mapper.CreateInstance<T>(reader) : throw new InvalidOperationException("The result set is empty.");
123123
}
124124

125125
/// <summary>
126126
/// Executes a parameterized SQL query and returns the first row.
127127
/// </summary>
128128
/// <param name="connection">The connection to the data source.</param>
129-
/// <param name="command">The SQL query to be executed.</param>
129+
/// <param name="sql">The SQL query to be executed.</param>
130130
/// <param name="parameters">The parameters of the SQL query.</param>
131131
/// <param name="options">The command options.</param>
132132
/// <param name="cancellationToken">The token to cancel the operation.</param>
133133
/// <returns>The first row, or <see langword="null"/> if not found.</returns>
134-
public static async Task<dynamic?> QueryFirstOrDefaultAsync(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
135-
await QueryFirstOrDefaultAsync<ExpandoObject>(connection, command, parameters, options, cancellationToken);
134+
public static async Task<dynamic?> QueryFirstOrDefaultAsync(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
135+
await QueryFirstOrDefaultAsync<ExpandoObject>(connection, sql, parameters, options, cancellationToken);
136136

137137
/// <summary>
138138
/// Executes a parameterized SQL query and returns the first row.
139139
/// </summary>
140140
/// <typeparam name="T">The type of objects to return.</typeparam>
141141
/// <param name="connection">The connection to the data source.</param>
142-
/// <param name="command">The SQL query to be executed.</param>
142+
/// <param name="sql">The SQL query to be executed.</param>
143143
/// <param name="parameters">The parameters of the SQL query.</param>
144144
/// <param name="options">The command options.</param>
145145
/// <param name="cancellationToken">The token to cancel the operation.</param>
146146
/// <returns>The first row, or <see langword="null"/> if not found.</returns>
147-
public static async Task<T?> QueryFirstOrDefaultAsync<T>(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() {
148-
using var reader = await ExecuteReaderAsync(connection, command, parameters, options, cancellationToken);
147+
public static async Task<T?> QueryFirstOrDefaultAsync<T>(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() {
148+
using var reader = await ExecuteReaderAsync(connection, sql, parameters, options, cancellationToken);
149149
return reader.Read() ? mapper.CreateInstance<T>(reader) : default;
150150
}
151151

152152
/// <summary>
153153
/// Executes a parameterized SQL query and returns the single row.
154154
/// </summary>
155155
/// <param name="connection">The connection to the data source.</param>
156-
/// <param name="command">The SQL query to be executed.</param>
156+
/// <param name="sql">The SQL query to be executed.</param>
157157
/// <param name="parameters">The parameters of the SQL query.</param>
158158
/// <param name="options">The command options.</param>
159159
/// <param name="cancellationToken">The token to cancel the operation.</param>
160160
/// <returns>The single row.</returns>
161161
/// <exception cref="InvalidOperationException">The result set is empty or contains more than one record.</exception>
162-
public static async Task<dynamic> QuerySingleAsync(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
163-
await QuerySingleAsync<ExpandoObject>(connection, command, parameters, options, cancellationToken);
162+
public static async Task<dynamic> QuerySingleAsync(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
163+
await QuerySingleAsync<ExpandoObject>(connection, sql, parameters, options, cancellationToken);
164164

165165
/// <summary>
166166
/// Executes a parameterized SQL query and returns the single row.
167167
/// </summary>
168168
/// <typeparam name="T">The type of objects to return.</typeparam>
169169
/// <param name="connection">The connection to the data source.</param>
170-
/// <param name="command">The SQL query to be executed.</param>
170+
/// <param name="sql">The SQL query to be executed.</param>
171171
/// <param name="parameters">The parameters of the SQL query.</param>
172172
/// <param name="options">The command options.</param>
173173
/// <param name="cancellationToken">The token to cancel the operation.</param>
174174
/// <returns>The single row.</returns>
175175
/// <exception cref="InvalidOperationException">The result set is empty or contains more than one record.</exception>
176-
public static async Task<T> QuerySingleAsync<T>(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() {
176+
public static async Task<T> QuerySingleAsync<T>(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() {
177177
T? record = default;
178178
var rowCount = 0;
179179

180-
using var reader = await ExecuteReaderAsync(connection, command, parameters, options, cancellationToken);
180+
using var reader = await ExecuteReaderAsync(connection, sql, parameters, options, cancellationToken);
181181
while (reader.Read()) {
182182
if (++rowCount > 1) break;
183183
record = mapper.CreateInstance<T>(reader);
@@ -190,29 +190,29 @@ record = mapper.CreateInstance<T>(reader);
190190
/// Executes a parameterized SQL query and returns the single row.
191191
/// </summary>
192192
/// <param name="connection">The connection to the data source.</param>
193-
/// <param name="command">The SQL query to be executed.</param>
193+
/// <param name="sql">The SQL query to be executed.</param>
194194
/// <param name="parameters">The parameters of the SQL query.</param>
195195
/// <param name="options">The command options.</param>
196196
/// <param name="cancellationToken">The token to cancel the operation.</param>
197197
/// <returns>The single row, or <see langword="null"/> if not found.</returns>
198-
public static async Task<dynamic?> QuerySingleOrDefaultAsync(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
199-
await QuerySingleOrDefaultAsync<ExpandoObject>(connection, command, parameters, options, cancellationToken);
198+
public static async Task<dynamic?> QuerySingleOrDefaultAsync(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) =>
199+
await QuerySingleOrDefaultAsync<ExpandoObject>(connection, sql, parameters, options, cancellationToken);
200200

201201
/// <summary>
202202
/// Executes a parameterized SQL query and returns the single row.
203203
/// </summary>
204204
/// <typeparam name="T">The type of objects to return.</typeparam>
205205
/// <param name="connection">The connection to the data source.</param>
206-
/// <param name="command">The SQL query to be executed.</param>
206+
/// <param name="sql">The SQL query to be executed.</param>
207207
/// <param name="parameters">The parameters of the SQL query.</param>
208208
/// <param name="options">The command options.</param>
209209
/// <param name="cancellationToken">The token to cancel the operation.</param>
210210
/// <returns>The single row, or <see langword="null"/> if not found.</returns>
211-
public static async Task<T?> QuerySingleOrDefaultAsync<T>(this DbConnection connection, string command, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() {
211+
public static async Task<T?> QuerySingleOrDefaultAsync<T>(this IDbConnection connection, string sql, ParameterCollection? parameters = null, CommandOptions? options = null, CancellationToken cancellationToken = default) where T: class, new() {
212212
T? record = default;
213213
var rowCount = 0;
214214

215-
using var reader = await ExecuteReaderAsync(connection, command, parameters, options, cancellationToken);
215+
using var reader = await ExecuteReaderAsync(connection, sql, parameters, options, cancellationToken);
216216
while (reader.Read()) {
217217
if (++rowCount > 1) break;
218218
record = mapper.CreateInstance<T>(reader);

0 commit comments

Comments
 (0)