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)
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>
......@@ -263,6 +263,17 @@ public interface IParameterLookup : IDynamicParameters
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>
/// Implement this interface to pass an arbitrary db specific parameter to Dapper
/// </summary>
......@@ -1251,7 +1262,7 @@ private static int ExecuteImpl(this IDbConnection cnn, ref CommandDefinition com
total += cmd.ExecuteNonQuery();
}
}
command.FireOutputCallbacks();
command.OnCompleted();
} finally
{
if (wasClosed) cnn.Close();
......@@ -1532,7 +1543,7 @@ private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefini
reader.Dispose();
reader = null;
command.FireOutputCallbacks();
command.OnCompleted();
}
finally
{
......@@ -1785,7 +1796,7 @@ partial class DontMap { }
if(finalize)
{
while (reader.NextResult()) { }
command.FireOutputCallbacks();
command.OnCompleted();
}
}
}
......@@ -1856,7 +1867,7 @@ static IEnumerable<TReturn> MultiMapImpl<TReturn>(this IDbConnection cnn, Comman
if (finalize)
{
while (reader.NextResult()) { }
command.FireOutputCallbacks();
command.OnCompleted();
}
}
}
......@@ -3163,7 +3174,7 @@ private static int ExecuteCommand(IDbConnection cnn, ref CommandDefinition comma
cmd = command.SetupCommand(cnn, paramReader);
if (wasClosed) cnn.Open();
int result = cmd.ExecuteNonQuery();
command.FireOutputCallbacks();
command.OnCompleted();
return result;
}
finally
......@@ -3191,7 +3202,7 @@ private static T ExecuteScalarImpl<T>(IDbConnection cnn, ref CommandDefinition c
cmd = command.SetupCommand(cnn, paramReader);
if (wasClosed) cnn.Open();
result =cmd.ExecuteScalar();
command.FireOutputCallbacks();
command.OnCompleted();
}
finally
{
......@@ -3915,12 +3926,12 @@ public partial class GridReader : IDisposable
private IDbCommand command;
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.reader = reader;
this.identity = identity;
this.dynamicParams = dynamicParams;
this.callbacks = callbacks;
}
#if !CSHARP30
......@@ -4127,7 +4138,7 @@ private IEnumerable<T> ReadDeferred<T>(int index, Func<IDataReader, object> dese
}
private int gridIndex, readCount;
private bool consumed;
private DynamicParameters dynamicParams;
private SqlMapper.IParameterCallbacks callbacks;
/// <summary>
/// Has the underlying reader been consumed?
......@@ -4153,7 +4164,7 @@ private void NextResult()
// need for "Cancel" etc
reader.Dispose();
reader = null;
if (dynamicParams != null) dynamicParams.FireOutputCallbacks();
if (callbacks != null) callbacks.OnCompleted();
Dispose();
}
}
......@@ -4214,7 +4225,7 @@ public static string GetTypeName(this DataTable table)
/// <summary>
/// A bag of parameters that can be passed to the Dapper Query and Execute methods
/// </summary>
partial class DynamicParameters : SqlMapper.IDynamicParameters, SqlMapper.IParameterLookup
partial class DynamicParameters : SqlMapper.IDynamicParameters, SqlMapper.IParameterLookup, SqlMapper.IParameterCallbacks
{
internal const DbType EnumerableMultiParameter = (DbType)(-1);
static Dictionary<SqlMapper.Identity, Action<IDbCommand, object>> paramReaderCache = new Dictionary<SqlMapper.Identity, Action<IDbCommand, object>>();
......@@ -4707,7 +4718,7 @@ internal static class CachedOutputSetters<T>
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))
{
......
......@@ -96,7 +96,7 @@ private static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn,
buffer.Add((T)func(reader));
}
while (await reader.NextResultAsync().ConfigureAwait(false)) { }
command.FireOutputCallbacks();
command.OnCompleted();
return buffer;
}
else
......@@ -238,7 +238,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
}
}
command.FireOutputCallbacks();
command.OnCompleted();
}
finally
{
......@@ -257,7 +257,7 @@ private static async Task<int> ExecuteImplAsync(IDbConnection cnn, CommandDefini
{
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
var result = await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false);
command.FireOutputCallbacks();
command.OnCompleted();
return result;
}
finally
......@@ -514,8 +514,8 @@ private static IEnumerable<T> ExecuteReaderSync<T>(IDataReader reader, Func<IDat
yield return (T)func(reader);
}
while (reader.NextResult()) { }
if (parameters is DynamicParameters)
((DynamicParameters)parameters).FireOutputCallbacks();
if (parameters is SqlMapper.IParameterCallbacks)
((SqlMapper.IParameterCallbacks)parameters).OnCompleted();
}
}
......@@ -580,7 +580,7 @@ private async Task NextResultAsync()
// need for "Cancel" etc
reader.Dispose();
reader = null;
if (dynamicParams != null) dynamicParams.FireOutputCallbacks();
if (callbacks != null) callbacks.OnCompleted();
Dispose();
}
}
......@@ -814,7 +814,7 @@ private async static Task<T> ExecuteScalarImplAsync<T>(IDbConnection cnn, Comman
cmd = (DbCommand)command.SetupCommand(cnn, paramReader);
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
result = await cmd.ExecuteScalarAsync(command.CancellationToken).ConfigureAwait(false);
command.FireOutputCallbacks();
command.OnCompleted();
}
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