Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support named parameters in where fragments #159

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/Weasel.Postgresql/SqlGeneration/CustomizableWhereFragment.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<string> }))
{
builder.Append(_sql);
builder.AddParameters(firstVal);
return;
}

if (_parameters is [{ } first] && (first.IsAnonymousType() || first is IDictionary { Keys: ICollection<string> }))
{
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;
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Weasel.Postgresql/SqlGeneration/WhereFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
}
Expand Down
Loading