mirror of
https://github.com/DapperLib/Dapper.git
synced 2026-10-19 14:48:42 +08:00
(via lgsky) - multimap should auto-open/close as necessary
This commit is contained in:
@@ -1073,11 +1073,13 @@ this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TFourth, TRetu
|
||||
IDbCommand ownedCommand = null;
|
||||
IDataReader ownedReader = null;
|
||||
|
||||
bool wasClosed = cnn.State == ConnectionState.Closed;
|
||||
try
|
||||
{
|
||||
if (reader == null)
|
||||
{
|
||||
ownedCommand = SetupCommand(cnn, transaction, sql, cinfo.ParamReader, (object)param, commandTimeout, commandType);
|
||||
if (wasClosed) cnn.Open();
|
||||
ownedReader = ownedCommand.ExecuteReader();
|
||||
reader = ownedReader;
|
||||
}
|
||||
@@ -1118,6 +1120,7 @@ this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TFourth, TRetu
|
||||
{
|
||||
ownedCommand.Dispose();
|
||||
}
|
||||
if (wasClosed) cnn.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2186,6 +2186,38 @@ end");
|
||||
conn.State.IsEqualTo(ConnectionState.Closed);
|
||||
}
|
||||
}
|
||||
class Multi1
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
class Multi2
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
public void QueryMultimapFromClosed()
|
||||
{
|
||||
using (var conn = GetClosedConnection())
|
||||
{
|
||||
conn.State.IsEqualTo(ConnectionState.Closed);
|
||||
var i = conn.Query<Multi1, Multi2, int>("select 2 as [Id], 3 as [Id]", (x, y) => x.Id + y.Id).Single();
|
||||
conn.State.IsEqualTo(ConnectionState.Closed);
|
||||
i.IsEqualTo(5);
|
||||
}
|
||||
}
|
||||
public void QueryMultiple2FromClosed()
|
||||
{
|
||||
using (var conn = GetClosedConnection())
|
||||
{
|
||||
conn.State.IsEqualTo(ConnectionState.Closed);
|
||||
using (var multi = conn.QueryMultiple("select 1 select 2 select 3"))
|
||||
{
|
||||
multi.Read<int>().Single().IsEqualTo(1);
|
||||
multi.Read<int>().Single().IsEqualTo(2);
|
||||
// not reading 3 is intentional here
|
||||
}
|
||||
conn.State.IsEqualTo(ConnectionState.Closed);
|
||||
}
|
||||
}
|
||||
public void ExecuteInvalidFromClosed()
|
||||
{
|
||||
using (var conn = GetClosedConnection())
|
||||
|
||||
Reference in New Issue
Block a user