diff --git a/PetaPoco.Tests.Unit/Utilities/ParametersHelperTests.cs b/PetaPoco.Tests.Unit/Utilities/ParametersHelperTests.cs index ca644214..c380a946 100644 --- a/PetaPoco.Tests.Unit/Utilities/ParametersHelperTests.cs +++ b/PetaPoco.Tests.Unit/Utilities/ParametersHelperTests.cs @@ -1,5 +1,12 @@ -using PetaPoco.Internal; +using Moq; +using PetaPoco.Internal; using Shouldly; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Reflection; using Xunit; namespace PetaPoco.Tests.Unit.Utilities @@ -36,5 +43,108 @@ public void ReplaceParamPrefix_ShouldWork(string prefix) var output = input.ReplaceParamPrefix(prefix); output.ShouldBe(expected); } + + [Fact] + public void ProcessQueryParams_PositionalParams_ShouldWork() + { + var sql = "select * from foo where a = @0 and b = @1"; + var args_src = new object[] { 5, "bird" }; + var args_dest = new List(); + var output = ParametersHelper.ProcessQueryParams(sql, args_src, args_dest); + + output.ShouldBe(sql); + args_dest.ShouldBe(args_src.ToList()); + } + + private void NamedQueryParamsTestHelper(object[] args_src) + { + var sql = "select * from foo where a = @first and b = @second"; + var args_dest = new List(); + + var expected_sql = "select * from foo where a = @0 and b = @1"; + var expected_args = new List() { 87, "Bob" }; + + var output = ParametersHelper.ProcessQueryParams(sql, args_src, args_dest); + + output.ShouldBe(expected_sql); + args_dest.ShouldBe(expected_args); + } + + [Fact] + public void ProcessQueryParams_ObjectWithProperties_ShouldWork() + { + var args_src = new[] { new { second = "Bob", first = 87 } }; + NamedQueryParamsTestHelper(args_src); + } + + [Fact] + public void ProcessQueryParams_Dictionary_ShouldWork() + { + var args_src = new[] { new Dictionary { ["second"] = "Bob", ["first"] = 87 } }; + NamedQueryParamsTestHelper(args_src); + } + + [Fact] + public void ProcessQueryParams_DictionaryAndObject_ShouldWork() + { + var args_src = new object[] { new Dictionary { ["second"] = "Bob" }, new { first = 87 } }; + NamedQueryParamsTestHelper(args_src); + } + + [Fact] + public void ProcessQueryParams_MissingParam_ShouldThrow() + { + var args_src = new[] { new {first = 87 } }; + Action act = () => NamedQueryParamsTestHelper(args_src); + + var ex = act.ShouldThrow(); + ex.Message.ShouldMatch(@"^Parameter '@second' specified"); + } + + private void NamedProcParamsTestHelper(object[] args_src) + { + Action setAction = (p, o, pi) => p.Value = o; + var cmd = new SqlCommand(); + + var expected = new [] { new SqlParameter("foo", 42), new SqlParameter("bar", "Dirk Gently") }; + var output = ParametersHelper.ProcessStoredProcParams(cmd, args_src, setAction) + .Cast() + .ToArray(); + + output.Count().ShouldBe(2); + output[0].ParameterName.ShouldBe(expected[0].ParameterName); + output[0].Value.ShouldBe(expected[0].Value); + output[1].ParameterName.ShouldBe(expected[1].ParameterName); + output[1].Value.ShouldBe(expected[1].Value); + + } + + [Fact] + public void ProcessStoredProcParams_Parameter_ShouldWork() + { + var args_src = new object[] { new SqlParameter("foo", 42), new SqlParameter("bar", "Dirk Gently") }; + NamedProcParamsTestHelper(args_src); + } + + [Fact] + public void ProcessStoredProcParams_ObjectWithProperties_ShouldWork() + { + var args_src = new object[] { new { foo = 42, bar = "Dirk Gently" } }; + NamedProcParamsTestHelper(args_src); + } + + [Fact] + public void ProcessStoredProcParams_Dictionary_ShouldWork() + { + var args_src = new[] { new Dictionary() { ["foo"] = 42, ["bar"] = "Dirk Gently" } }; + NamedProcParamsTestHelper(args_src); + } + + [Fact] + public void ProcessStoredProcParams_DictionaryAndObject_ShouldWork() + { + var args_src = new object[] { new Dictionary() { ["foo"] = 42 }, new { bar = "Dirk Gently" } }; + NamedProcParamsTestHelper(args_src); + } } } \ No newline at end of file diff --git a/PetaPoco/Utilities/ParametersHelper.cs b/PetaPoco/Utilities/ParametersHelper.cs index f1a861a3..a72310b4 100644 --- a/PetaPoco/Utilities/ParametersHelper.cs +++ b/PetaPoco/Utilities/ParametersHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; @@ -49,11 +50,27 @@ public static string ProcessQueryParams(string sql, object[] args_src, List p.CanRead); foreach (var prop in readableProps) { - var param = cmd.CreateParameter(); - param.ParameterName = prop.Name; - setParameterProperties(param, prop.GetValue(arg, null), null); - result.Add(param); - } + AddParameter(prop.Name, prop.GetValue(arg, null)); + } } } + void AddParameter(string name, object value) + { + var param = cmd.CreateParameter(); + param.ParameterName = name; + setParameterProperties(param, value, null); + result.Add(param); + } + foreach (var arg in args) { ProcessArg(arg);