Skip to content

Commit 956dafe

Browse files
authored
Merge pull request #952 from jamescrowley/clearParameters
2 parents e9bdcb2 + 3387019 commit 956dafe

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

Dapper/SqlMapper.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,8 @@ private static GridReader QueryMultipleImpl(this IDbConnection cnn, ref CommandD
10421042
}
10431043
reader.Dispose();
10441044
}
1045+
1046+
cmd?.Parameters.Clear();
10451047
cmd?.Dispose();
10461048
if (wasClosed) cnn.Close();
10471049
throw;
@@ -1122,6 +1124,8 @@ private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefini
11221124
reader.Dispose();
11231125
}
11241126
if (wasClosed) cnn.Close();
1127+
1128+
cmd?.Parameters.Clear();
11251129
cmd?.Dispose();
11261130
}
11271131
}
@@ -1212,6 +1216,7 @@ private static T QueryRowImpl<T>(IDbConnection cnn, Row row, ref CommandDefiniti
12121216
reader.Dispose();
12131217
}
12141218
if (wasClosed) cnn.Close();
1219+
cmd?.Parameters.Clear();
12151220
cmd?.Dispose();
12161221
}
12171222
}
@@ -1483,6 +1488,7 @@ private static IEnumerable<TReturn> MultiMapImpl<TFirst, TSecond, TThird, TFourt
14831488
}
14841489
finally
14851490
{
1491+
ownedCommand?.Parameters.Clear();
14861492
ownedCommand?.Dispose();
14871493
if (wasClosed) cnn.Close();
14881494
}
@@ -1553,6 +1559,7 @@ private static IEnumerable<TReturn> MultiMapImpl<TReturn>(this IDbConnection cnn
15531559
}
15541560
finally
15551561
{
1562+
ownedCommand?.Parameters.Clear();
15561563
ownedCommand?.Dispose();
15571564
if (wasClosed) cnn.Close();
15581565
}
@@ -2817,6 +2824,7 @@ private static int ExecuteCommand(IDbConnection cnn, ref CommandDefinition comma
28172824
finally
28182825
{
28192826
if (wasClosed) cnn.Close();
2827+
cmd?.Parameters.Clear();
28202828
cmd?.Dispose();
28212829
}
28222830
}
@@ -2844,6 +2852,7 @@ private static T ExecuteScalarImpl<T>(IDbConnection cnn, ref CommandDefinition c
28442852
finally
28452853
{
28462854
if (wasClosed) cnn.Close();
2855+
cmd?.Parameters.Clear();
28472856
cmd?.Dispose();
28482857
}
28492858
return Parse<T>(result);
@@ -2867,7 +2876,11 @@ private static IDataReader ExecuteReaderImpl(IDbConnection cnn, ref CommandDefin
28672876
finally
28682877
{
28692878
if (wasClosed) cnn.Close();
2870-
if (cmd != null && disposeCommand) cmd.Dispose();
2879+
if (cmd != null && disposeCommand)
2880+
{
2881+
cmd.Parameters.Clear();
2882+
cmd.Dispose();
2883+
}
28712884
}
28722885
}
28732886

tests/Dapper.Tests/ParameterTests.cs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public sealed class MicrosoftSqlClientParameterTests : ParameterTests<MicrosoftS
2626
#endif
2727
public abstract class ParameterTests<TProvider> : TestBase<TProvider> where TProvider : DatabaseProvider
2828
{
29-
public class DbParams : SqlMapper.IDynamicParameters, IEnumerable<IDbDataParameter>
29+
public class DbDynamicParams : SqlMapper.IDynamicParameters, IEnumerable<IDbDataParameter>
3030
{
3131
private readonly List<IDbDataParameter> parameters = new List<IDbDataParameter>();
3232
public IEnumerator<IDbDataParameter> GetEnumerator() { return parameters.GetEnumerator(); }
@@ -42,6 +42,21 @@ void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Id
4242
command.Parameters.Add(parameter);
4343
}
4444
}
45+
46+
public class DbCustomParam : SqlMapper.ICustomQueryParameter
47+
{
48+
private readonly IDbDataParameter _sqlParameter;
49+
50+
public DbCustomParam(IDbDataParameter sqlParameter)
51+
{
52+
_sqlParameter = sqlParameter;
53+
}
54+
55+
public void AddParameter(IDbCommand command, string name)
56+
{
57+
command.Parameters.Add(_sqlParameter);
58+
}
59+
}
4560

4661
private static IEnumerable<IDataRecord> CreateSqlDataRecordList(IDbCommand command, IEnumerable<int> numbers)
4762
{
@@ -799,9 +814,9 @@ public class HazSqlHierarchy
799814
#endif
800815

801816
[Fact]
802-
public void TestCustomParameters()
817+
public void TestDynamicParameters()
803818
{
804-
var args = new DbParams {
819+
var args = new DbDynamicParams {
805820
Provider.CreateRawParameter("foo", 123),
806821
Provider.CreateRawParameter("bar", "abc")
807822
};
@@ -811,7 +826,53 @@ public void TestCustomParameters()
811826
Assert.Equal(123, foo);
812827
Assert.Equal("abc", bar);
813828
}
829+
830+
[Fact]
831+
public void TestDynamicParametersReuse()
832+
{
833+
var args = new DbDynamicParams {
834+
Provider.CreateRawParameter("foo", 123),
835+
Provider.CreateRawParameter("bar", "abc")
836+
};
837+
var result1 = connection.Query("select Foo=@foo, Bar=@bar", args).Single();
838+
var result2 = connection.Query("select Foo=@foo, Bar=@bar", args).Single();
839+
Assert.Equal(123, result1.Foo);
840+
Assert.Equal("abc", result1.Bar);
841+
Assert.Equal(123, result2.Foo);
842+
Assert.Equal("abc", result2.Bar);
843+
}
814844

845+
846+
[Fact]
847+
public void TestCustomParameter()
848+
{
849+
var args = new {
850+
foo = new DbCustomParam(Provider.CreateRawParameter("foo", 123)),
851+
bar = "abc"
852+
};
853+
var result = connection.Query("select Foo=@foo, Bar=@bar", args).Single();
854+
int foo = result.Foo;
855+
string bar = result.Bar;
856+
Assert.Equal(123, foo);
857+
Assert.Equal("abc", bar);
858+
}
859+
860+
[Fact]
861+
public void TestCustomParameterReuse()
862+
{
863+
var args = new {
864+
foo = new DbCustomParam(Provider.CreateRawParameter("foo", 123)),
865+
bar = "abc"
866+
};
867+
var result1 = connection.Query("select Foo=@foo, Bar=@bar", args).Single();
868+
var result2 = connection.Query("select Foo=@foo, Bar=@bar", args).Single();
869+
Assert.Equal(123, result1.Foo);
870+
Assert.Equal("abc", result1.Bar);
871+
Assert.Equal(123, result2.Foo);
872+
Assert.Equal("abc", result2.Bar);
873+
}
874+
875+
815876
[Fact]
816877
public void TestDynamicParamNullSupport()
817878
{

0 commit comments

Comments
 (0)