Commit 7cdecbc7 authored by Marc Gravell's avatar Marc Gravell

async literal replacement and multi-exec tests

parent 9afb2fca
......@@ -855,27 +855,34 @@ private static int ExecuteImpl(this IDbConnection cnn, ref CommandDefinition com
{
bool isFirst = true;
int total = 0;
using (var cmd = command.SetupCommand(cnn, null))
bool wasClosed = cnn.State == ConnectionState.Closed;
try
{
string masterSql = null;
foreach (var obj in multiExec)
if (wasClosed) cnn.Open();
using (var cmd = command.SetupCommand(cnn, null))
{
if (isFirst)
string masterSql = null;
foreach (var obj in multiExec)
{
masterSql = cmd.CommandText;
isFirst = false;
identity = new Identity(command.CommandText, cmd.CommandType, cnn, null, obj.GetType(), null);
info = GetCacheInfo(identity, obj);
}
else
{
cmd.CommandText = masterSql; // because we do magic replaces on "in" etc
cmd.Parameters.Clear(); // current code is Add-tastic
if (isFirst)
{
masterSql = cmd.CommandText;
isFirst = false;
identity = new Identity(command.CommandText, cmd.CommandType, cnn, null, obj.GetType(), null);
info = GetCacheInfo(identity, obj);
}
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 += cmd.ExecuteNonQuery();
}
info.ParamReader(cmd, obj);
total += cmd.ExecuteNonQuery();
}
} finally
{
if (wasClosed) cnn.Close();
}
return total;
}
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
......@@ -57,9 +58,58 @@ public static Task<int> ExecuteAsync(this IDbConnection cnn, string sql, dynamic
/// <summary>
/// Execute a command asynchronously using .NET 4.5 Task.
/// </summary>
public static async Task<int> ExecuteAsync(this IDbConnection cnn, CommandDefinition command)
public static Task<int> ExecuteAsync(this IDbConnection cnn, CommandDefinition command)
{
object param = command.Parameters;
IEnumerable multiExec = param as IEnumerable;
if (multiExec != null && !(param is string))
{
return ExecuteMultiImplAsync(cnn, command, multiExec);
}
else
{
return ExecuteImplAsync(cnn, command, param);
}
}
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();
using (var cmd = (DbCommand)command.SetupCommand(cnn, null))
{
string masterSql = null;
CacheInfo info = 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);
}
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();
}
}
}
finally
{
if (wasClosed) cnn.Close();
}
return total;
}
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);
bool wasClosed = cnn.State == ConnectionState.Closed;
......
......@@ -98,9 +98,9 @@ public void TestMultiMapWithSplitClosedConnAsync()
public void TestMultiAsync()
{
using(var conn = Program.GetOpenConnection())
using (var conn = Program.GetOpenConnection())
{
using(Dapper.SqlMapper.GridReader multi = conn.QueryMultipleAsync("select 1; select 2").Result)
using (Dapper.SqlMapper.GridReader multi = conn.QueryMultipleAsync("select 1; select 2").Result)
{
multi.Read<int>().Single().IsEqualTo(1);
multi.Read<int>().Single().IsEqualTo(2);
......@@ -148,6 +148,49 @@ public void ExecuteReaderClosedAsync()
}
}
public void LiteralReplacementOpen()
{
using (var conn = Program.GetOpenConnection()) LiteralReplacement(conn);
}
public void LiteralReplacementClosed()
{
using (var conn = Program.GetClosedConnection()) LiteralReplacement(conn);
}
private void LiteralReplacement(IDbConnection connection)
{
try { connection.ExecuteAsync("drop table literal1").Wait(); } catch { }
connection.ExecuteAsync("create table literal1 (id int not null, foo int not null)").Wait();
connection.ExecuteAsync("insert literal1 (id,foo) values ({=id}, @foo)", new { id = 123, foo = 456 }).Wait();
var rows = new[] { new { id = 1, foo = 2 }, new { id = 3, foo = 4 } };
connection.ExecuteAsync("insert literal1 (id,foo) values ({=id}, @foo)", rows).Wait();
var count = connection.QueryAsync<int>("select count(1) from literal1 where id={=foo}", new { foo = 123 }).Result.Single();
count.IsEqualTo(1);
int sum = connection.QueryAsync<int>("select sum(id) + sum(foo) from literal1").Result.Single();
sum.IsEqualTo(123 + 456 + 1 + 2 + 3 + 4);
}
public void LiteralReplacementDynamicOpen()
{
using (var conn = Program.GetOpenConnection()) LiteralReplacementDynamic(conn);
}
public void LiteralReplacementDynamicClosed()
{
using (var conn = Program.GetClosedConnection()) LiteralReplacementDynamic(conn);
}
private void LiteralReplacementDynamic(IDbConnection connection)
{
var args = new DynamicParameters();
args.Add("id", 123);
try { connection.ExecuteAsync("drop table literal2").Wait(); } catch { }
connection.ExecuteAsync("create table literal2 (id int not null)").Wait();
connection.ExecuteAsync("insert literal2 (id) values ({=id})", args).Wait();
args = new DynamicParameters();
args.Add("foo", 123);
var count = connection.QueryAsync<int>("select count(1) from literal2 where id={=foo}", args).Result.Single();
count.IsEqualTo(1);
}
class Product
{
public int Id { get; set; }
......
......@@ -2670,10 +2670,14 @@ public void TestParameterInclusionNotSensitiveToCurrentCulture()
}
public void LiteralReplacement()
{
connection.Execute("create table #literal1 (id int not null)");
connection.Execute("insert #literal1 (id) values ({=id})", new { id = 123 });
connection.Execute("create table #literal1 (id int not null, foo int not null)");
connection.Execute("insert #literal1 (id,foo) values ({=id}, @foo)", new { id = 123, foo = 456});
var rows = new[] { new { id = 1, foo = 2 }, new { id = 3, foo = 4 } };
connection.Execute("insert #literal1 (id,foo) values ({=id}, @foo)", rows);
var count = connection.Query<int>("select count(1) from #literal1 where id={=foo}", new { foo = 123 }).Single();
count.IsEqualTo(1);
int sum = connection.Query<int>("select sum(id) + sum(foo) from #literal1").Single();
sum.IsEqualTo(123 + 456 + 1 + 2 + 3 + 4);
}
public void LiteralReplacementDynamic()
{
......@@ -2688,6 +2692,7 @@ public void LiteralReplacementDynamic()
count.IsEqualTo(1);
}
public void TestProcedureWithTimeParameter()
{
var p = new DynamicParameters();
......
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