Skip to content

Commit

Permalink
Allow certain types to not participate in IEnumerable expansion #636
Browse files Browse the repository at this point in the history
  • Loading branch information
schotime committed Dec 29, 2023
1 parent 81e80e7 commit 1600540
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/NPoco.Abstractions/ParameterHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace NPoco
{
public class ParameterHelper
{
public static List<Type> ExcludedIEnumerableTypes = new();

// Helper to handle named parameters from object properties
public static Regex rxParamsPrefix = new Regex(@"(?<!@)@\w+", RegexOptions.Compiled);

Expand Down Expand Up @@ -92,7 +94,8 @@ private static string ProcessParam(ref string sql, string rawParam, object[] arg
// Expand collections to parameter lists
if ((arg_val as System.Collections.IEnumerable) != null &&
(arg_val as string) == null &&
(arg_val as byte[]) == null)
(arg_val as byte[]) == null &&
!ExcludedIEnumerableTypes.Contains(arg_val.GetTheType()))
{
var sb = new StringBuilder();
foreach (var i in arg_val as System.Collections.IEnumerable)
Expand Down
16 changes: 16 additions & 0 deletions test/NPoco.Tests/ParameterHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

namespace NPoco.Tests
Expand Down Expand Up @@ -118,5 +119,20 @@ public void RewriteTheSameParameters()
Assert.AreEqual(2, args.Count);
Assert.AreEqual(resultSql, "SELECT * FROM test WHERE testID in (@0, @1, @0, @0, @1)");
}

[Test]
public void ExludedIEnumerableTypeParameters()
{
var sql = "UPDATE test SET name = @0, data = @1 WHERE id = @2";
var args = new List<object>();
var data = JToken.Parse("{\"Test\":\"Test\",\"Quantity\":5}");
var args_src = new object[] { "test", data, Guid.NewGuid() };
ParameterHelper.ExcludedIEnumerableTypes.AddRange(new[] { typeof(JToken), typeof(JObject) });
var resultSql = ParameterHelper.ProcessParams(sql, args_src, args);

Assert.AreEqual(3, args.Count);
Assert.AreEqual(resultSql, "UPDATE test SET name = @0, data = @1 WHERE id = @2");
ParameterHelper.ExcludedIEnumerableTypes.Clear();
}
}
}

0 comments on commit 1600540

Please sign in to comment.