private static async Task <IEnumerable <TReturn> > MultiMapAsync <TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(this IDbConnection cnn, CommandDefinition command, Delegate map, string splitOn) { object param = command.Parameters; var identity = new Identity(command.CommandText, command.CommandType, cnn, typeof(TFirst), param == null ? null : param.GetType(), new[] { typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh) }); var info = GetCacheInfo(identity, param, command.AddToCache); bool wasClosed = cnn.State == ConnectionState.Closed; try { if (wasClosed) { await((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false); } using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader)) using (var reader = await cmd.ExecuteReaderAsync(wasClosed ? CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess : CommandBehavior.SequentialAccess, command.CancellationToken).ConfigureAwait(false)) { if (!command.Buffered) { wasClosed = false; // handing back open reader; rely on command-behavior } var results = MultiMapImpl <TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(null, CommandDefinition.ForCallback(command.Parameters), map, splitOn, reader, identity, true); return(command.Buffered ? results.ToList() : results); } } finally { if (wasClosed) { cnn.Close(); } } }
private static async Task <IEnumerable <TReturn> > MultiMapAsync <TReturn>(this IDbConnection cnn, CommandDefinition command, Type[] types, Func <object[], TReturn> map, string splitOn) { if (types.Length < 1) { throw new ArgumentException("you must provide at least one type to deserialize"); } object param = command.Parameters; var identity = new Identity(command.CommandText, command.CommandType, cnn, types[0], param == null ? null : param.GetType(), types); var info = GetCacheInfo(identity, param, command.AddToCache); bool wasClosed = cnn.State == ConnectionState.Closed; try { if (wasClosed) { await((DbConnection)cnn).OpenAsync().ConfigureAwait(false); } using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader)) using (var reader = await cmd.ExecuteReaderAsync(command.CancellationToken).ConfigureAwait(false)) { var results = MultiMapImpl <TReturn>(null, default(CommandDefinition), types, map, splitOn, reader, identity, true); return(command.Buffered ? results.ToList() : results); } } finally { if (wasClosed) { cnn.Close(); } } }
private static async Task <int> ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, object param) { var identity = new Identity(command.CommandText, command.CommandType, cnn, null, param == null ? null : param.GetType(), null); var info = GetCacheInfo(identity, param, command.AddToCache); bool wasClosed = cnn.State == ConnectionState.Closed; using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader)) { try { if (wasClosed) { await((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false); } var result = await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false); command.OnCompleted(); return(result); } finally { if (wasClosed) { cnn.Close(); } } } }
/// <summary> /// Execute a query asynchronously using .NET 4.5 Task. /// </summary> public static async Task <IEnumerable <T> > QueryAsync <T>(this IDbConnection cnn, CommandDefinition command) { object param = command.Parameters; var identity = new Identity(command.CommandText, command.CommandType, cnn, typeof(T), param == null ? null : param.GetType(), null); var info = GetCacheInfo(identity, param); bool wasClosed = cnn.State == ConnectionState.Closed; using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader)) { try { if (wasClosed) { await((DbConnection)cnn).OpenAsync().ConfigureAwait(false); } using (var reader = await cmd.ExecuteReaderAsync(command.CancellationToken).ConfigureAwait(false)) { return(ExecuteReader <T>(reader, identity, info).ToList()); } } finally { if (wasClosed) { cnn.Close(); } } } }
/* * Common */ /// <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, DynamicParameters> 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"); } }
/// <summary> /// Creates a new instance of an <see cref="System.Data.IDbDataAdapter"/> object. /// </summary> /// <param name="selectCommandText">the text command to run against the data source</param> /// <param name="param">the collection of parameters</param> /// <param name="commandTimeout">the wait time before terminating the attempt to execute a command and generating an error</param> /// <param name="commandType">the type indicates or specifies how the command is interpreted</param> /// <exception cref="System.ArgumentNullException"></exception> /// <exception cref="System.InvalidOperationException"></exception> /// <returns><see cref="System.Data.IDbDataAdapter"/></returns> public IDbDataAdapter CreateDataAdapter(String selectCommandText, Object param = null, Int32?commandTimeout = null, CommandType?commandType = null) { CommandDefinition command = new CommandDefinition(selectCommandText, param, Transaction, commandTimeout, commandType, CommandFlags.Buffered); Action <IDbCommand, Object> paramReader = null; if (param != null) { SqlMapper.Identity identity = new SqlMapper.Identity(command.CommandText, command.CommandType, Connection, null, param.GetType(), null); paramReader = SqlMapper.GetCacheInfo(identity, command.Parameters, command.AddToCache).ParamReader; } IDbDataAdapter ada = Factory.DbProviderFactory.CreateDataAdapter(); ada.SelectCommand = command.SetupCommand(Connection, paramReader); return(ada); }
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 == null ? null : param.GetType(), null); var info = GetCacheInfo(identity, param, command.AddToCache); bool wasClosed = cnn.State == ConnectionState.Closed; var cancel = command.CancellationToken; using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader)) { DbDataReader reader = null; try { if (wasClosed) { await((DbConnection)cnn).OpenAsync(cancel).ConfigureAwait(false); } reader = await cmd.ExecuteReaderAsync(wasClosed?CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess : CommandBehavior.SequentialAccess, cancel).ConfigureAwait(false); var tuple = info.Deserializer; int hash = GetColumnHash(reader); if (tuple.Func == null || tuple.Hash != hash) { 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) { List <T> buffer = new List <T>(); while (await reader.ReadAsync(cancel).ConfigureAwait(false)) { buffer.Add((T)func(reader)); } while (await reader.NextResultAsync().ConfigureAwait(false)) { } 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 <int> ExecuteMultiImplAsync(IDbConnection cnn, CommandDefinition command, IEnumerable multiExec) { bool isFirst = true; int total = 0; bool wasClosed = cnn.State == ConnectionState.Closed; try { if (wasClosed) { await((DbConnection)cnn).OpenAsync(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 = (DbCommand)command.SetupCommand(cnn, null); masterSql = cmd.CommandText; var identity = new Identity(command.CommandText, cmd.CommandType, cnn, null, obj.GetType(), null); 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 = (DbCommand)command.SetupCommand(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 = (DbCommand)command.SetupCommand(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(), null); 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 IEnumerable QueryInternal(Type type, Mapping.Table table, System.Data.IDbConnection connection, String sql, Object param, IDbTransaction transaction, CommandType?commandType, Int32?commandTimeout) { var command = new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.Buffered); var identity = new SqlMapper.Identity(sql, commandType, connection, type, param == null ? null : param.GetType(), null); var info = SqlMapper.GetCacheInfo(identity, param, command.AddToCache); IDbCommand cmd = null; IDataReader reader = null; Boolean wasClosed = connection.State == ConnectionState.Closed; try { cmd = command.SetupCommand(connection, info.ParamReader); if (wasClosed) { connection.Open(); } reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess : CommandBehavior.SequentialAccess); wasClosed = false; // *if* the connection was closed and we got this far, then we now have a reader // with the CloseConnection flag, so the reader will deal with the connection; we // still need something in the "finally" to ensure that broken SQL still results // in the connection closing itself var tuple = info.Deserializer; int hash = SqlMapper.GetColumnHash(reader); if (tuple.Func == null || tuple.Hash != hash) { if (reader.FieldCount == 0) //https://code.google.com/p/dapper-dot-net/issues/detail?id=57 { yield break; } tuple = info.Deserializer = new SqlMapper.DeserializerState(hash, GetDeserializer(type, reader, 0, -1, false, table)); if (command.AddToCache) { SqlMapper.SetQueryCache(identity, info); } } var func = tuple.Func; while (reader.Read()) { yield return(func(reader)); } while (reader.NextResult()) { } // happy path; close the reader cleanly - no // need for "Cancel" etc reader.Dispose(); reader = null; command.OnCompleted(); } finally { if (reader != null) { if (!reader.IsClosed) { try { cmd.Cancel(); } catch { /* don't spoil the existing exception */ } } reader.Dispose(); } if (wasClosed) { connection.Close(); } if (cmd != null) { cmd.Dispose(); } } }