From c0c41c52ea23dfa82b3212b5c1d26b5f68fbb3cc Mon Sep 17 00:00:00 2001 From: Ben Edwards Date: Fri, 20 Dec 2024 12:30:08 +1100 Subject: [PATCH] Support named parameters in where fragments --- .../CustomizableWhereFragment.cs | 31 +++++++++++++++---- .../SqlGeneration/WhereFragment.cs | 3 +- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Weasel.Postgresql/SqlGeneration/CustomizableWhereFragment.cs b/src/Weasel.Postgresql/SqlGeneration/CustomizableWhereFragment.cs index d262bbb7..c08b4149 100644 --- a/src/Weasel.Postgresql/SqlGeneration/CustomizableWhereFragment.cs +++ b/src/Weasel.Postgresql/SqlGeneration/CustomizableWhereFragment.cs @@ -1,14 +1,15 @@ -using JasperFx.Core; +using System.Collections; +using JasperFx.Core.Reflection; namespace Weasel.Postgresql.SqlGeneration; public class CustomizableWhereFragment: ISqlFragment { - private readonly CommandParameter[] _parameters; + private readonly object[] _parameters; private readonly string _sql; private readonly char _token; - public CustomizableWhereFragment(string sql, string paramReplacementToken, params CommandParameter[] parameters) + public CustomizableWhereFragment(string sql, string paramReplacementToken, params object[] parameters) { _sql = sql; _parameters = parameters; @@ -17,13 +18,31 @@ public CustomizableWhereFragment(string sql, string paramReplacementToken, param public void Apply(ICommandBuilder builder) { + // backwards compatibility, old version of this class accepted CommandParameter[] + if (_parameters is [CommandParameter { Value: { } firstVal }] && (firstVal.IsAnonymousType() || firstVal is IDictionary { Keys: ICollection })) + { + builder.Append(_sql); + builder.AddParameters(firstVal); + return; + } + + if (_parameters is [{ } first] && (first.IsAnonymousType() || first is IDictionary { Keys: ICollection })) + { + builder.Append(_sql); + builder.AddParameters(first); + return; + } + + var parameters = builder.AppendWithParameters(_sql, _token); for (var i = 0; i < parameters.Length; i++) { - parameters[i].Value = _parameters[i].Value; - if (_parameters[i].DbType.HasValue) + // backwards compatibility, old version of this class accepted CommandParameter[] + var commandParameter = _parameters[i] as CommandParameter ?? new CommandParameter(_parameters[i]); + parameters[i].Value = commandParameter.Value; + if (commandParameter.DbType.HasValue) { - parameters[i].NpgsqlDbType = _parameters[i].DbType.Value; + parameters[i].NpgsqlDbType = commandParameter.DbType.Value; } } } diff --git a/src/Weasel.Postgresql/SqlGeneration/WhereFragment.cs b/src/Weasel.Postgresql/SqlGeneration/WhereFragment.cs index e46b93d8..cc79f40f 100644 --- a/src/Weasel.Postgresql/SqlGeneration/WhereFragment.cs +++ b/src/Weasel.Postgresql/SqlGeneration/WhereFragment.cs @@ -2,8 +2,7 @@ namespace Weasel.Postgresql.SqlGeneration; public class WhereFragment: CustomizableWhereFragment { - public WhereFragment(string sql, params object[] parameters): base(sql, "?", - parameters.Select(x => new CommandParameter(x)).ToArray()) + public WhereFragment(string sql, params object[] parameters): base(sql, "?", parameters) { } }