Commit 6ae42c29 authored by Marc Gravell's avatar Marc Gravell

Add: feature 106; dapper takes control of the connection if needeed

parent 293a8edc
...@@ -786,16 +786,28 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn ...@@ -786,16 +786,28 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn
IDbCommand cmd = null; IDbCommand cmd = null;
IDataReader reader = null; IDataReader reader = null;
bool wasClosed = cnn.State == ConnectionState.Closed;
try try
{ {
if (wasClosed) cnn.Open();
cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, (object)param, commandTimeout, commandType); cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, (object)param, commandTimeout, commandType);
reader = cmd.ExecuteReader(); reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection : CommandBehavior.Default);
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
return new GridReader(cmd, reader, identity); return new GridReader(cmd, reader, identity);
} }
catch catch
{ {
if (reader != null) reader.Dispose(); if (reader != null)
{
if (!reader.IsClosed) try { cmd.Cancel(); }
catch { /* don't spol the existing exception */ }
reader.Dispose();
}
if (cmd != null) cmd.Dispose(); if (cmd != null) cmd.Dispose();
if (wasClosed) cnn.Close();
throw; throw;
} }
} }
...@@ -808,36 +820,45 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq ...@@ -808,36 +820,45 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq
var identity = new Identity(sql, commandType, cnn, typeof(T), param == null ? null : param.GetType(), null); var identity = new Identity(sql, commandType, cnn, typeof(T), param == null ? null : param.GetType(), null);
var info = GetCacheInfo(identity); var info = GetCacheInfo(identity);
using (var cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param, commandTimeout, commandType)) IDbCommand cmd = null;
IDataReader reader = null;
bool wasClosed = cnn.State == ConnectionState.Closed;
try
{ {
IDataReader reader = null; cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param, commandTimeout, commandType);
try
if (wasClosed) cnn.Open();
reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection : CommandBehavior.Default);
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 = GetColumnHash(reader);
if (tuple.Func == null || tuple.Hash != hash)
{ {
reader = cmd.ExecuteReader(); tuple = info.Deserializer = new DeserializerState(hash, GetDeserializer(typeof(T), reader, 0, -1, false));
var tuple = info.Deserializer; SetQueryCache(identity, info);
int hash = GetColumnHash(reader); }
if (tuple.Func == null || tuple.Hash != hash)
{
tuple = info.Deserializer = new DeserializerState(hash, GetDeserializer(typeof(T), reader, 0, -1, false));
SetQueryCache(identity, info);
}
var func = tuple.Func; var func = tuple.Func;
while (reader.Read()) while (reader.Read())
{ {
yield return (T)func(reader); yield return (T)func(reader);
}
} }
finally }
finally
{
if (reader != null)
{ {
if (reader != null) if (!reader.IsClosed) try { cmd.Cancel(); }
{ catch { /* don't spol the existing exception */ }
if (!reader.IsClosed) try { cmd.Cancel(); } reader.Dispose();
catch { /* don't spol the existing exception */ }
reader.Dispose();
}
} }
if (wasClosed) cnn.Close();
if (cmd != null) cmd.Dispose();
} }
} }
...@@ -1671,10 +1692,19 @@ private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transac ...@@ -1671,10 +1692,19 @@ private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transac
private static int ExecuteCommand(IDbConnection cnn, IDbTransaction transaction, string sql, Action<IDbCommand, object> paramReader, object obj, int? commandTimeout, CommandType? commandType) private static int ExecuteCommand(IDbConnection cnn, IDbTransaction transaction, string sql, Action<IDbCommand, object> paramReader, object obj, int? commandTimeout, CommandType? commandType)
{ {
using (var cmd = SetupCommand(cnn, transaction, sql, paramReader, obj, commandTimeout, commandType)) IDbCommand cmd = null;
bool wasClosed = cnn.State == ConnectionState.Closed;
try
{ {
cmd = SetupCommand(cnn, transaction, sql, paramReader, obj, commandTimeout, commandType);
if (wasClosed) cnn.Open();
return cmd.ExecuteNonQuery(); return cmd.ExecuteNonQuery();
} }
finally
{
if (wasClosed) cnn.Close();
if (cmd != null) cmd.Dispose();
}
} }
private static Func<IDataReader, object> GetStructDeserializer(Type type, Type effectiveType, int index) private static Func<IDataReader, object> GetStructDeserializer(Type type, Type effectiveType, int index)
......
...@@ -2015,6 +2015,87 @@ public Issue40_User() ...@@ -2015,6 +2015,87 @@ public Issue40_User()
public bool Active { get; set; } public bool Active { get; set; }
} }
SqlConnection GetClosedConnection()
{
var conn = new SqlConnection(connection.ConnectionString);
if (conn.State != ConnectionState.Closed) throw new InvalidOperationException("should be closed!");
return conn;
}
public void ExecuteFromClosed()
{
using (var conn = GetClosedConnection())
{
conn.Execute("-- nop");
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
public void ExecuteInvalidFromClosed()
{
using (var conn = GetClosedConnection())
{
try
{
conn.Execute("nop");
false.IsEqualTo(true); // shouldn't have got here
}
catch
{
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
}
public void QueryFromClosed()
{
using (var conn = GetClosedConnection())
{
var i = conn.Query<int>("select 1").Single();
conn.State.IsEqualTo(ConnectionState.Closed);
i.IsEqualTo(1);
}
}
public void QueryInvalidFromClosed()
{
using (var conn = GetClosedConnection())
{
try
{
conn.Query<int>("select gibberish").Single();
false.IsEqualTo(true); // shouldn't have got here
}
catch
{
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
}
public void QueryMultipleFromClosed()
{
using (var conn = GetClosedConnection())
{
using (var multi = conn.QueryMultiple("select 1; select 'abc';"))
{
multi.Read<int>().Single().IsEqualTo(1);
multi.Read<string>().Single().IsEqualTo("abc");
}
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
public void QueryMultipleInvalidFromClosed()
{
using (var conn = GetClosedConnection())
{
try
{
conn.QueryMultiple("select gibberish");
false.IsEqualTo(true); // shouldn't have got here
}
catch
{
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
}
class TransactedConnection : IDbConnection class TransactedConnection : IDbConnection
{ {
IDbConnection _conn; IDbConnection _conn;
......
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