Commit 79e1e3ab authored by Marc Gravell's avatar Marc Gravell

move the parameter callbacks onto a method that custom implementations can also use

parent 9226076f
...@@ -73,11 +73,11 @@ internal static CommandDefinition ForCallback(object parameters) ...@@ -73,11 +73,11 @@ internal static CommandDefinition ForCallback(object parameters)
private readonly CommandFlags flags; private readonly CommandFlags flags;
internal void FireOutputCallbacks() internal void OnCompleted()
{ {
if (parameters is DynamicParameters) if (parameters is SqlMapper.IParameterCallbacks)
{ {
((DynamicParameters)parameters).FireOutputCallbacks(); ((SqlMapper.IParameterCallbacks)parameters).OnCompleted();
} }
} }
/// <summary> /// <summary>
...@@ -263,6 +263,17 @@ public interface IParameterLookup : IDynamicParameters ...@@ -263,6 +263,17 @@ public interface IParameterLookup : IDynamicParameters
object this[string name] { get; } object this[string name] { get; }
} }
/// <summary>
/// Extends IDynamicParameters with facitilies for executing callbacks after commands have completed
/// </summary>
public partial interface IParameterCallbacks : IDynamicParameters
{
/// <summary>
/// Invoked when the command has executed
/// </summary>
void OnCompleted();
}
/// <summary> /// <summary>
/// Implement this interface to pass an arbitrary db specific parameter to Dapper /// Implement this interface to pass an arbitrary db specific parameter to Dapper
/// </summary> /// </summary>
...@@ -1251,7 +1262,7 @@ private static int ExecuteImpl(this IDbConnection cnn, ref CommandDefinition com ...@@ -1251,7 +1262,7 @@ private static int ExecuteImpl(this IDbConnection cnn, ref CommandDefinition com
total += cmd.ExecuteNonQuery(); total += cmd.ExecuteNonQuery();
} }
} }
command.FireOutputCallbacks(); command.OnCompleted();
} finally } finally
{ {
if (wasClosed) cnn.Close(); if (wasClosed) cnn.Close();
...@@ -1532,7 +1543,7 @@ private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefini ...@@ -1532,7 +1543,7 @@ private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefini
reader.Dispose(); reader.Dispose();
reader = null; reader = null;
command.FireOutputCallbacks(); command.OnCompleted();
} }
finally finally
{ {
...@@ -1785,7 +1796,7 @@ partial class DontMap { } ...@@ -1785,7 +1796,7 @@ partial class DontMap { }
if(finalize) if(finalize)
{ {
while (reader.NextResult()) { } while (reader.NextResult()) { }
command.FireOutputCallbacks(); command.OnCompleted();
} }
} }
} }
...@@ -1856,7 +1867,7 @@ static IEnumerable<TReturn> MultiMapImpl<TReturn>(this IDbConnection cnn, Comman ...@@ -1856,7 +1867,7 @@ static IEnumerable<TReturn> MultiMapImpl<TReturn>(this IDbConnection cnn, Comman
if (finalize) if (finalize)
{ {
while (reader.NextResult()) { } while (reader.NextResult()) { }
command.FireOutputCallbacks(); command.OnCompleted();
} }
} }
} }
...@@ -3163,7 +3174,7 @@ private static int ExecuteCommand(IDbConnection cnn, ref CommandDefinition comma ...@@ -3163,7 +3174,7 @@ private static int ExecuteCommand(IDbConnection cnn, ref CommandDefinition comma
cmd = command.SetupCommand(cnn, paramReader); cmd = command.SetupCommand(cnn, paramReader);
if (wasClosed) cnn.Open(); if (wasClosed) cnn.Open();
int result = cmd.ExecuteNonQuery(); int result = cmd.ExecuteNonQuery();
command.FireOutputCallbacks(); command.OnCompleted();
return result; return result;
} }
finally finally
...@@ -3191,7 +3202,7 @@ private static T ExecuteScalarImpl<T>(IDbConnection cnn, ref CommandDefinition c ...@@ -3191,7 +3202,7 @@ private static T ExecuteScalarImpl<T>(IDbConnection cnn, ref CommandDefinition c
cmd = command.SetupCommand(cnn, paramReader); cmd = command.SetupCommand(cnn, paramReader);
if (wasClosed) cnn.Open(); if (wasClosed) cnn.Open();
result =cmd.ExecuteScalar(); result =cmd.ExecuteScalar();
command.FireOutputCallbacks(); command.OnCompleted();
} }
finally finally
{ {
...@@ -3915,12 +3926,12 @@ public partial class GridReader : IDisposable ...@@ -3915,12 +3926,12 @@ public partial class GridReader : IDisposable
private IDbCommand command; private IDbCommand command;
private Identity identity; private Identity identity;
internal GridReader(IDbCommand command, IDataReader reader, Identity identity, DynamicParameters dynamicParams) internal GridReader(IDbCommand command, IDataReader reader, Identity identity, SqlMapper.IParameterCallbacks callbacks)
{ {
this.command = command; this.command = command;
this.reader = reader; this.reader = reader;
this.identity = identity; this.identity = identity;
this.dynamicParams = dynamicParams; this.callbacks = callbacks;
} }
#if !CSHARP30 #if !CSHARP30
...@@ -4127,7 +4138,7 @@ private IEnumerable<T> ReadDeferred<T>(int index, Func<IDataReader, object> dese ...@@ -4127,7 +4138,7 @@ private IEnumerable<T> ReadDeferred<T>(int index, Func<IDataReader, object> dese
} }
private int gridIndex, readCount; private int gridIndex, readCount;
private bool consumed; private bool consumed;
private DynamicParameters dynamicParams; private SqlMapper.IParameterCallbacks callbacks;
/// <summary> /// <summary>
/// Has the underlying reader been consumed? /// Has the underlying reader been consumed?
...@@ -4153,7 +4164,7 @@ private void NextResult() ...@@ -4153,7 +4164,7 @@ private void NextResult()
// need for "Cancel" etc // need for "Cancel" etc
reader.Dispose(); reader.Dispose();
reader = null; reader = null;
if (dynamicParams != null) dynamicParams.FireOutputCallbacks(); if (callbacks != null) callbacks.OnCompleted();
Dispose(); Dispose();
} }
} }
...@@ -4214,7 +4225,7 @@ public static string GetTypeName(this DataTable table) ...@@ -4214,7 +4225,7 @@ public static string GetTypeName(this DataTable table)
/// <summary> /// <summary>
/// A bag of parameters that can be passed to the Dapper Query and Execute methods /// A bag of parameters that can be passed to the Dapper Query and Execute methods
/// </summary> /// </summary>
partial class DynamicParameters : SqlMapper.IDynamicParameters, SqlMapper.IParameterLookup partial class DynamicParameters : SqlMapper.IDynamicParameters, SqlMapper.IParameterLookup, SqlMapper.IParameterCallbacks
{ {
internal const DbType EnumerableMultiParameter = (DbType)(-1); internal const DbType EnumerableMultiParameter = (DbType)(-1);
static Dictionary<SqlMapper.Identity, Action<IDbCommand, object>> paramReaderCache = new Dictionary<SqlMapper.Identity, Action<IDbCommand, object>>(); static Dictionary<SqlMapper.Identity, Action<IDbCommand, object>> paramReaderCache = new Dictionary<SqlMapper.Identity, Action<IDbCommand, object>>();
...@@ -4707,7 +4718,7 @@ internal static class CachedOutputSetters<T> ...@@ -4707,7 +4718,7 @@ internal static class CachedOutputSetters<T>
public static readonly Hashtable Cache = new Hashtable(); public static readonly Hashtable Cache = new Hashtable();
} }
internal void FireOutputCallbacks() void SqlMapper.IParameterCallbacks.OnCompleted()
{ {
foreach (var param in (from p in parameters select p.Value)) foreach (var param in (from p in parameters select p.Value))
{ {
......
...@@ -96,7 +96,7 @@ private static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn, ...@@ -96,7 +96,7 @@ private static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn,
buffer.Add((T)func(reader)); buffer.Add((T)func(reader));
} }
while (await reader.NextResultAsync().ConfigureAwait(false)) { } while (await reader.NextResultAsync().ConfigureAwait(false)) { }
command.FireOutputCallbacks(); command.OnCompleted();
return buffer; return buffer;
} }
else else
...@@ -238,7 +238,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD ...@@ -238,7 +238,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
} }
} }
command.FireOutputCallbacks(); command.OnCompleted();
} }
finally finally
{ {
...@@ -257,7 +257,7 @@ private static async Task<int> ExecuteImplAsync(IDbConnection cnn, CommandDefini ...@@ -257,7 +257,7 @@ private static async Task<int> ExecuteImplAsync(IDbConnection cnn, CommandDefini
{ {
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false); if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
var result = await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false); var result = await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false);
command.FireOutputCallbacks(); command.OnCompleted();
return result; return result;
} }
finally finally
...@@ -514,8 +514,8 @@ private static IEnumerable<T> ExecuteReaderSync<T>(IDataReader reader, Func<IDat ...@@ -514,8 +514,8 @@ private static IEnumerable<T> ExecuteReaderSync<T>(IDataReader reader, Func<IDat
yield return (T)func(reader); yield return (T)func(reader);
} }
while (reader.NextResult()) { } while (reader.NextResult()) { }
if (parameters is DynamicParameters) if (parameters is SqlMapper.IParameterCallbacks)
((DynamicParameters)parameters).FireOutputCallbacks(); ((SqlMapper.IParameterCallbacks)parameters).OnCompleted();
} }
} }
...@@ -580,7 +580,7 @@ private async Task NextResultAsync() ...@@ -580,7 +580,7 @@ private async Task NextResultAsync()
// need for "Cancel" etc // need for "Cancel" etc
reader.Dispose(); reader.Dispose();
reader = null; reader = null;
if (dynamicParams != null) dynamicParams.FireOutputCallbacks(); if (callbacks != null) callbacks.OnCompleted();
Dispose(); Dispose();
} }
} }
...@@ -814,7 +814,7 @@ private async static Task<T> ExecuteScalarImplAsync<T>(IDbConnection cnn, Comman ...@@ -814,7 +814,7 @@ private async static Task<T> ExecuteScalarImplAsync<T>(IDbConnection cnn, Comman
cmd = (DbCommand)command.SetupCommand(cnn, paramReader); cmd = (DbCommand)command.SetupCommand(cnn, paramReader);
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false); if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
result = await cmd.ExecuteScalarAsync(command.CancellationToken).ConfigureAwait(false); result = await cmd.ExecuteScalarAsync(command.CancellationToken).ConfigureAwait(false);
command.FireOutputCallbacks(); command.OnCompleted();
} }
finally finally
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment