/Dapper/SqlMapper.Async.cs
http://github.com/SamSaffron/dapper-dot-net · C# · 1233 lines · 543 code · 80 blank · 610 comment · 88 complexity · 1a6a5f7a54b7ebf4862c0b2d84fa5f01 MD5 · raw file
Large files are truncated click here to view the full file
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
- using System.Globalization;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
-
- namespace Dapper
- {
- public static partial class SqlMapper
- {
- /// <summary>
- /// Execute a query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- /// <remarks>Note: each row can be accessed via "dynamic", or by casting to an IDictionary<string,object></remarks>
- public static Task<IEnumerable<dynamic>> QueryAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryAsync<dynamic>(cnn, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.Buffered, default(CancellationToken)));
-
- /// <summary>
- /// Execute a query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- /// <remarks>Note: each row can be accessed via "dynamic", or by casting to an IDictionary<string,object></remarks>
- public static Task<IEnumerable<dynamic>> QueryAsync(this IDbConnection cnn, CommandDefinition command) =>
- QueryAsync<dynamic>(cnn, typeof(DapperRow), command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- /// <remarks>Note: the row can be accessed via "dynamic", or by casting to an IDictionary<string,object></remarks>
- public static Task<dynamic> QueryFirstAsync(this IDbConnection cnn, CommandDefinition command) =>
- QueryRowAsync<dynamic>(cnn, Row.First, typeof(DapperRow), command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- /// <remarks>Note: the row can be accessed via "dynamic", or by casting to an IDictionary<string,object></remarks>
- public static Task<dynamic> QueryFirstOrDefaultAsync(this IDbConnection cnn, CommandDefinition command) =>
- QueryRowAsync<dynamic>(cnn, Row.FirstOrDefault, typeof(DapperRow), command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- /// <remarks>Note: the row can be accessed via "dynamic", or by casting to an IDictionary<string,object></remarks>
- public static Task<dynamic> QuerySingleAsync(this IDbConnection cnn, CommandDefinition command) =>
- QueryRowAsync<dynamic>(cnn, Row.Single, typeof(DapperRow), command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- /// <remarks>Note: the row can be accessed via "dynamic", or by casting to an IDictionary<string,object></remarks>
- public static Task<dynamic> QuerySingleOrDefaultAsync(this IDbConnection cnn, CommandDefinition command) =>
- QueryRowAsync<dynamic>(cnn, Row.SingleOrDefault, typeof(DapperRow), command);
-
- /// <summary>
- /// Execute a query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type of results to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- /// <returns>
- /// A sequence of data of <typeparamref name="T"/>; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is
- /// created per row, and a direct column-name===member-name mapping is assumed (case insensitive).
- /// </returns>
- public static Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryAsync<T>(cnn, typeof(T), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.Buffered, default(CancellationToken)));
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type of result to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- public static Task<T> QueryFirstAsync<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryRowAsync<T>(cnn, Row.First, typeof(T), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type of result to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- public static Task<T> QueryFirstOrDefaultAsync<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryRowAsync<T>(cnn, Row.FirstOrDefault, typeof(T), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type of result to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- public static Task<T> QuerySingleAsync<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryRowAsync<T>(cnn, Row.Single, typeof(T), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- public static Task<T> QuerySingleOrDefaultAsync<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryRowAsync<T>(cnn, Row.SingleOrDefault, typeof(T), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- public static Task<dynamic> QueryFirstAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryRowAsync<dynamic>(cnn, Row.First, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- public static Task<dynamic> QueryFirstOrDefaultAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryRowAsync<dynamic>(cnn, Row.FirstOrDefault, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- public static Task<dynamic> QuerySingleAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryRowAsync<dynamic>(cnn, Row.Single, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- public static Task<dynamic> QuerySingleOrDefaultAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- QueryRowAsync<dynamic>(cnn, Row.SingleOrDefault, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
-
- /// <summary>
- /// Execute a query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
- public static Task<IEnumerable<object>> QueryAsync(this IDbConnection cnn, Type type, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
- {
- if (type == null) throw new ArgumentNullException(nameof(type));
- return QueryAsync<object>(cnn, type, new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.Buffered, default(CancellationToken)));
- }
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
- public static Task<object> QueryFirstAsync(this IDbConnection cnn, Type type, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
- {
- if (type == null) throw new ArgumentNullException(nameof(type));
- return QueryRowAsync<object>(cnn, Row.First, type, new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
- }
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
- public static Task<object> QueryFirstOrDefaultAsync(this IDbConnection cnn, Type type, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
- {
- if (type == null) throw new ArgumentNullException(nameof(type));
- return QueryRowAsync<object>(cnn, Row.FirstOrDefault, type, new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
- }
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
- public static Task<object> QuerySingleAsync(this IDbConnection cnn, Type type, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
- {
- if (type == null) throw new ArgumentNullException(nameof(type));
- return QueryRowAsync<object>(cnn, Row.Single, type, new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
- }
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="sql">The SQL to execute for the query.</param>
- /// <param name="param">The parameters to pass, if any.</param>
- /// <param name="transaction">The transaction to use, if any.</param>
- /// <param name="commandTimeout">The command timeout (in seconds).</param>
- /// <param name="commandType">The type of command to execute.</param>
- /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
- public static Task<object> QuerySingleOrDefaultAsync(this IDbConnection cnn, Type type, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
- {
- if (type == null) throw new ArgumentNullException(nameof(type));
- return QueryRowAsync<object>(cnn, Row.SingleOrDefault, type, new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));
- }
-
- /// <summary>
- /// Execute a query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- /// <returns>
- /// A sequence of data of <typeparamref name="T"/>; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is
- /// created per row, and a direct column-name===member-name mapping is assumed (case insensitive).
- /// </returns>
- public static Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn, CommandDefinition command) =>
- QueryAsync<T>(cnn, typeof(T), command);
-
- /// <summary>
- /// Execute a query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="command">The command used to query on this connection.</param>
- public static Task<IEnumerable<object>> QueryAsync(this IDbConnection cnn, Type type, CommandDefinition command) =>
- QueryAsync<object>(cnn, type, command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="command">The command used to query on this connection.</param>
- public static Task<object> QueryFirstAsync(this IDbConnection cnn, Type type, CommandDefinition command) =>
- QueryRowAsync<object>(cnn, Row.First, type, command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- public static Task<T> QueryFirstAsync<T>(this IDbConnection cnn, CommandDefinition command) =>
- QueryRowAsync<T>(cnn, Row.First, typeof(T), command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="command">The command used to query on this connection.</param>
- public static Task<object> QueryFirstOrDefaultAsync(this IDbConnection cnn, Type type, CommandDefinition command) =>
- QueryRowAsync<object>(cnn, Row.FirstOrDefault, type, command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- public static Task<T> QueryFirstOrDefaultAsync<T>(this IDbConnection cnn, CommandDefinition command) =>
- QueryRowAsync<T>(cnn, Row.FirstOrDefault, typeof(T), command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="command">The command used to query on this connection.</param>
- public static Task<object> QuerySingleAsync(this IDbConnection cnn, Type type, CommandDefinition command) =>
- QueryRowAsync<object>(cnn, Row.Single, type, command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- public static Task<T> QuerySingleAsync<T>(this IDbConnection cnn, CommandDefinition command) =>
- QueryRowAsync<T>(cnn, Row.Single, typeof(T), command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="type">The type to return.</param>
- /// <param name="command">The command used to query on this connection.</param>
- public static Task<object> QuerySingleOrDefaultAsync(this IDbConnection cnn, Type type, CommandDefinition command) =>
- QueryRowAsync<object>(cnn, Row.SingleOrDefault, type, command);
-
- /// <summary>
- /// Execute a single-row query asynchronously using Task.
- /// </summary>
- /// <typeparam name="T">The type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="command">The command used to query on this connection.</param>
- public static Task<T> QuerySingleOrDefaultAsync<T>(this IDbConnection cnn, CommandDefinition command) =>
- QueryRowAsync<T>(cnn, Row.SingleOrDefault, typeof(T), command);
-
- private static Task<DbDataReader> ExecuteReaderWithFlagsFallbackAsync(DbCommand cmd, bool wasClosed, CommandBehavior behavior, CancellationToken cancellationToken)
- {
- var task = cmd.ExecuteReaderAsync(GetBehavior(wasClosed, behavior), cancellationToken);
- if (task.Status == TaskStatus.Faulted && Settings.DisableCommandBehaviorOptimizations(behavior, task.Exception.InnerException))
- { // we can retry; this time it will have different flags
- return cmd.ExecuteReaderAsync(GetBehavior(wasClosed, behavior), cancellationToken);
- }
- return task;
- }
-
- /// <summary>
- /// Attempts to open a connection asynchronously, with a better error message for unsupported usages.
- /// </summary>
- private static Task TryOpenAsync(this IDbConnection cnn, CancellationToken cancel)
- {
- if (cnn is DbConnection dbConn)
- {
- return dbConn.OpenAsync(cancel);
- }
- else
- {
- throw new InvalidOperationException("Async operations require use of a DbConnection or an already-open IDbConnection");
- }
- }
-
- /// <summary>
- /// Attempts setup a <see cref="DbCommand"/> on a <see cref="DbConnection"/>, with a better error message for unsupported usages.
- /// </summary>
- private static DbCommand TrySetupAsyncCommand(this CommandDefinition command, IDbConnection cnn, Action<IDbCommand, object> paramReader)
- {
- if (command.SetupCommand(cnn, paramReader) is DbCommand dbCommand)
- {
- return dbCommand;
- }
- else
- {
- throw new InvalidOperationException("Async operations require use of a DbConnection or an IDbConnection where .CreateCommand() returns a DbCommand");
- }
- }
-
- private static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn, Type effectiveType, CommandDefinition command)
- {
- object param = command.Parameters;
- var identity = new Identity(command.CommandText, command.CommandType, cnn, effectiveType, param?.GetType());
- var info = GetCacheInfo(identity, param, command.AddToCache);
- bool wasClosed = cnn.State == ConnectionState.Closed;
- var cancel = command.CancellationToken;
- using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
- {
- DbDataReader reader = null;
- try
- {
- if (wasClosed) await cnn.TryOpenAsync(cancel).ConfigureAwait(false);
- reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, cancel).ConfigureAwait(false);
-
- var tuple = info.Deserializer;
- int hash = GetColumnHash(reader);
- if (tuple.Func == null || tuple.Hash != hash)
- {
- if (reader.FieldCount == 0)
- return Enumerable.Empty<T>();
- tuple = info.Deserializer = new DeserializerState(hash, GetDeserializer(effectiveType, reader, 0, -1, false));
- if (command.AddToCache) SetQueryCache(identity, info);
- }
-
- var func = tuple.Func;
-
- if (command.Buffered)
- {
- var buffer = new List<T>();
- var convertToType = Nullable.GetUnderlyingType(effectiveType) ?? effectiveType;
- while (await reader.ReadAsync(cancel).ConfigureAwait(false))
- {
- object val = func(reader);
- buffer.Add(GetValue<T>(reader, effectiveType, val));
- }
- while (await reader.NextResultAsync(cancel).ConfigureAwait(false)) { /* ignore subsequent result sets */ }
- command.OnCompleted();
- return buffer;
- }
- else
- {
- // can't use ReadAsync / cancellation; but this will have to do
- wasClosed = false; // don't close if handing back an open reader; rely on the command-behavior
- var deferred = ExecuteReaderSync<T>(reader, func, command.Parameters);
- reader = null; // to prevent it being disposed before the caller gets to see it
- return deferred;
- }
- }
- finally
- {
- using (reader) { /* dispose if non-null */ }
- if (wasClosed) cnn.Close();
- }
- }
- }
-
- private static async Task<T> QueryRowAsync<T>(this IDbConnection cnn, Row row, Type effectiveType, CommandDefinition command)
- {
- object param = command.Parameters;
- var identity = new Identity(command.CommandText, command.CommandType, cnn, effectiveType, param?.GetType());
- var info = GetCacheInfo(identity, param, command.AddToCache);
- bool wasClosed = cnn.State == ConnectionState.Closed;
- var cancel = command.CancellationToken;
- using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
- {
- DbDataReader reader = null;
- try
- {
- if (wasClosed) await cnn.TryOpenAsync(cancel).ConfigureAwait(false);
- reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, (row & Row.Single) != 0
- ? CommandBehavior.SequentialAccess | CommandBehavior.SingleResult // need to allow multiple rows, to check fail condition
- : CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow, cancel).ConfigureAwait(false);
-
- T result = default;
- if (await reader.ReadAsync(cancel).ConfigureAwait(false) && reader.FieldCount != 0)
- {
- result = ReadRow<T>(info, identity, ref command, effectiveType, reader);
-
- if ((row & Row.Single) != 0 && await reader.ReadAsync(cancel).ConfigureAwait(false)) ThrowMultipleRows(row);
- while (await reader.ReadAsync(cancel).ConfigureAwait(false)) { /* ignore rows after the first */ }
- }
- else if ((row & Row.FirstOrDefault) == 0) // demanding a row, and don't have one
- {
- ThrowZeroRows(row);
- }
- while (await reader.NextResultAsync(cancel).ConfigureAwait(false)) { /* ignore result sets after the first */ }
- return result;
- }
- finally
- {
- using (reader) { /* dispose if non-null */ }
- if (wasClosed) cnn.Close();
- }
- }
- }
-
- /// <summary>
- /// Execute a command asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for this query.</param>
- /// <param name="param">The parameters to use for this query.</param>
- /// <param name="transaction">The transaction to use for this query.</param>
- /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
- /// <param name="commandType">Is it a stored proc or a batch?</param>
- /// <returns>The number of rows affected.</returns>
- public static Task<int> ExecuteAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
- ExecuteAsync(cnn, new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.Buffered, default(CancellationToken)));
-
- /// <summary>
- /// Execute a command asynchronously using Task.
- /// </summary>
- /// <param name="cnn">The connection to execute on.</param>
- /// <param name="command">The command to execute on this connection.</param>
- /// <returns>The number of rows affected.</returns>
- public static Task<int> ExecuteAsync(this IDbConnection cnn, CommandDefinition command)
- {
- object param = command.Parameters;
- IEnumerable multiExec = GetMultiExec(param);
- if (multiExec != null)
- {
- return ExecuteMultiImplAsync(cnn, command, multiExec);
- }
- else
- {
- return ExecuteImplAsync(cnn, command, param);
- }
- }
-
- private struct AsyncExecState
- {
- public readonly DbCommand Command;
- public readonly Task<int> Task;
- public AsyncExecState(DbCommand command, Task<int> task)
- {
- Command = command;
- Task = task;
- }
- }
-
- private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandDefinition command, IEnumerable multiExec)
- {
- bool isFirst = true;
- int total = 0;
- bool wasClosed = cnn.State == ConnectionState.Closed;
- try
- {
- if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
-
- CacheInfo info = null;
- string masterSql = null;
- if ((command.Flags & CommandFlags.Pipelined) != 0)
- {
- const int MAX_PENDING = 100;
- var pending = new Queue<AsyncExecState>(MAX_PENDING);
- DbCommand cmd = null;
- try
- {
- foreach (var obj in multiExec)
- {
- if (isFirst)
- {
- isFirst = false;
- cmd = command.TrySetupAsyncCommand(cnn, null);
- masterSql = cmd.CommandText;
- var identity = new Identity(command.CommandText, cmd.CommandType, cnn, null, obj.GetType());
- info = GetCacheInfo(identity, obj, command.AddToCache);
- }
- else if (pending.Count >= MAX_PENDING)
- {
- var recycled = pending.Dequeue();
- total += await recycled.Task.ConfigureAwait(false);
- cmd = recycled.Command;
- cmd.CommandText = masterSql; // because we do magic replaces on "in" etc
- cmd.Parameters.Clear(); // current code is Add-tastic
- }
- else
- {
- cmd = command.TrySetupAsyncCommand(cnn, null);
- }
- info.ParamReader(cmd, obj);
-
- var task = cmd.ExecuteNonQueryAsync(command.CancellationToken);
- pending.Enqueue(new AsyncExecState(cmd, task));
- cmd = null; // note the using in the finally: this avoids a double-dispose
- }
- while (pending.Count != 0)
- {
- var pair = pending.Dequeue();
- using (pair.Command) { /* dispose commands */ }
- total += await pair.Task.ConfigureAwait(false);
- }
- }
- finally
- {
- // this only has interesting work to do if there are failures
- using (cmd) { /* dispose commands */ }
- while (pending.Count != 0)
- { // dispose tasks even in failure
- using (pending.Dequeue().Command) { /* dispose commands */ }
- }
- }
- }
- else
- {
- using (var cmd = command.TrySetupAsyncCommand(cnn, null))
- {
- foreach (var obj in multiExec)
- {
- if (isFirst)
- {
- masterSql = cmd.CommandText;
- isFirst = false;
- var identity = new Identity(command.CommandText, cmd.CommandType, cnn, null, obj.GetType());
- info = GetCacheInfo(identity, obj, command.AddToCache);
- }
- else
- {
- cmd.CommandText = masterSql; // because we do magic replaces on "in" etc
- cmd.Parameters.Clear(); // current code is Add-tastic
- }
- info.ParamReader(cmd, obj);
- total += await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false);
- }
- }
- }
-
- command.OnCompleted();
- }
- finally
- {
- if (wasClosed) cnn.Close();
- }
- return total;
- }
-
- private static async Task<int> ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, object param)
- {
- var identity = new Identity(command.CommandText, command.CommandType, cnn, null, param?.GetType());
- var info = GetCacheInfo(identity, param, command.AddToCache);
- bool wasClosed = cnn.State == ConnectionState.Closed;
- using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
- {
- try
- {
- if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
- var result = await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false);
- command.OnCompleted();
- return result;
- }
- finally
- {
- if (wasClosed) cnn.Close();
- }
- }
- }
-
- /// <summary>
- /// Perform a asynchronous multi-mapping query with 2 input types.
- /// This returns a single type, combined from the raw types via <paramref name="map"/>.
- /// </summary>
- /// <typeparam name="TFirst">The first type in the recordset.</typeparam>
- /// <typeparam name="TSecond">The second type in the recordset.</typeparam>
- /// <typeparam name="TReturn">The combined type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for this query.</param>
- /// <param name="map">The function to map row types to the return type.</param>
- /// <param name="param">The parameters to use for this query.</param>
- /// <param name="transaction">The transaction to use for this query.</param>
- /// <param name="buffered">Whether to buffer the results in memory.</param>
- /// <param name="splitOn">The field we should split and read the second object from (default: "Id").</param>
- /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
- /// <param name="commandType">Is it a stored proc or a batch?</param>
- /// <returns>An enumerable of <typeparamref name="TReturn"/>.</returns>
- public static Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) =>
- MultiMapAsync<TFirst, TSecond, DontMap, DontMap, DontMap, DontMap, DontMap, TReturn>(cnn,
- new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default(CancellationToken)), map, splitOn);
-
- /// <summary>
- /// Perform a asynchronous multi-mapping query with 2 input types.
- /// This returns a single type, combined from the raw types via <paramref name="map"/>.
- /// </summary>
- /// <typeparam name="TFirst">The first type in the recordset.</typeparam>
- /// <typeparam name="TSecond">The second type in the recordset.</typeparam>
- /// <typeparam name="TReturn">The combined type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="splitOn">The field we should split and read the second object from (default: "Id").</param>
- /// <param name="command">The command to execute.</param>
- /// <param name="map">The function to map row types to the return type.</param>
- /// <returns>An enumerable of <typeparamref name="TReturn"/>.</returns>
- public static Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TReturn>(this IDbConnection cnn, CommandDefinition command, Func<TFirst, TSecond, TReturn> map, string splitOn = "Id") =>
- MultiMapAsync<TFirst, TSecond, DontMap, DontMap, DontMap, DontMap, DontMap, TReturn>(cnn, command, map, splitOn);
-
- /// <summary>
- /// Perform a asynchronous multi-mapping query with 3 input types.
- /// This returns a single type, combined from the raw types via <paramref name="map"/>.
- /// </summary>
- /// <typeparam name="TFirst">The first type in the recordset.</typeparam>
- /// <typeparam name="TSecond">The second type in the recordset.</typeparam>
- /// <typeparam name="TThird">The third type in the recordset.</typeparam>
- /// <typeparam name="TReturn">The combined type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for this query.</param>
- /// <param name="map">The function to map row types to the return type.</param>
- /// <param name="param">The parameters to use for this query.</param>
- /// <param name="transaction">The transaction to use for this query.</param>
- /// <param name="buffered">Whether to buffer the results in memory.</param>
- /// <param name="splitOn">The field we should split and read the second object from (default: "Id").</param>
- /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
- /// <param name="commandType">Is it a stored proc or a batch?</param>
- /// <returns>An enumerable of <typeparamref name="TReturn"/>.</returns>
- public static Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) =>
- MultiMapAsync<TFirst, TSecond, TThird, DontMap, DontMap, DontMap, DontMap, TReturn>(cnn,
- new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default(CancellationToken)), map, splitOn);
-
- /// <summary>
- /// Perform a asynchronous multi-mapping query with 3 input types.
- /// This returns a single type, combined from the raw types via <paramref name="map"/>.
- /// </summary>
- /// <typeparam name="TFirst">The first type in the recordset.</typeparam>
- /// <typeparam name="TSecond">The second type in the recordset.</typeparam>
- /// <typeparam name="TThird">The third type in the recordset.</typeparam>
- /// <typeparam name="TReturn">The combined type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="splitOn">The field we should split and read the second object from (default: "Id").</param>
- /// <param name="command">The command to execute.</param>
- /// <param name="map">The function to map row types to the return type.</param>
- /// <returns>An enumerable of <typeparamref name="TReturn"/>.</returns>
- public static Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TReturn>(this IDbConnection cnn, CommandDefinition command, Func<TFirst, TSecond, TThird, TReturn> map, string splitOn = "Id") =>
- MultiMapAsync<TFirst, TSecond, TThird, DontMap, DontMap, DontMap, DontMap, TReturn>(cnn, command, map, splitOn);
-
- /// <summary>
- /// Perform a asynchronous multi-mapping query with 4 input types.
- /// This returns a single type, combined from the raw types via <paramref name="map"/>.
- /// </summary>
- /// <typeparam name="TFirst">The first type in the recordset.</typeparam>
- /// <typeparam name="TSecond">The second type in the recordset.</typeparam>
- /// <typeparam name="TThird">The third type in the recordset.</typeparam>
- /// <typeparam name="TFourth">The fourth type in the recordset.</typeparam>
- /// <typeparam name="TReturn">The combined type to return.</typeparam>
- /// <param name="cnn">The connection to query on.</param>
- /// <param name="sql">The SQL to execute for this query.</param>
- /// <param name="map">The function to map row types to the return type.</param>
- /// <param name="param">The parameters to use for this query.</param>
- /// <param name="transaction">The transaction to use for this query.</param>
- /// <param name="buffered">Whether to buffer the results in memory.</param>
- /// <param name="splitOn">The field we should split and read the second object from (default: "Id").</param>
- /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
- /// <param name="commandType">Is it a stored proc or a batch?</param>
- /// <returns>An enumerable of <typeparamref name="TReturn"/>.</returns>
- public static Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TFourth, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TFourth, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) =>
- MultiMapAsync<TFirst, TSecond, TThird, TFourth, DontMap, DontMap, DontMap, TReturn>(cnn,
- new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default(CancellationToken)), map, splitOn);
-
- /// <summary>
- /// Perform a asynchronous multi-mapping query with 4 input types.
- /// This returns a single type, combined from the raw types via <paramref name="map"/>.
- /// </summary>
- /// <typeparam name="TFirst">The first type in the recordset.</typeparam>
- /// <typeparam name="TSecond">The second type in the recordset.</typeparam>
- /// <typeparam name="TThird">The third type in the recordset.</typeparam>
- /// <typeparam name="TFourth">The fourth type in the recordset.</typeparam>
- /// <typeparam name="TReturn">The combined type to return.</typeparam>…