Skip to content

Commit

Permalink
Add GroupedParameterBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawxy authored and jeremydmiller committed Jul 24, 2024
1 parent a837b3e commit 290f549
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
26 changes: 24 additions & 2 deletions src/Weasel.Postgresql/BatchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ public NpgsqlParameter AppendParameter<T>(T value)
return param;
}

public NpgsqlParameter AppendParameter<T>(T value, NpgsqlDbType dbType)
{
_current ??= appendCommand();
var param = new NpgsqlParameter<T>() {
TypedValue = value,
NpgsqlDbType = dbType
};

_current.Parameters.Add(param);

_builder.Append('$');
_builder.Append(_current.Parameters.Count);

return param;
}


public NpgsqlParameter AppendParameter(object value)
{
_current ??= appendCommand();
Expand Down Expand Up @@ -97,6 +114,11 @@ public void AppendParameters(params object[] parameters)
}
}

public IGroupedParameterBuilder CreateGroupedParameterBuilder(char? seperator = null)
{
return new GroupedParameterBuilder(this, seperator);
}

/// <summary>
/// Append a SQL string with user defined placeholder characters for new parameters, and returns an
/// array of the newly created parameters
Expand All @@ -119,7 +141,7 @@ public NpgsqlParameter[] AppendWithParameters(string text, char placeholder)
for (var i = 0; i < parameters.Length; i++)
{
// Just need a placeholder parameter type and value
var parameter = AppendParameter(DBNull.Value, NpgsqlDbType.Text);
var parameter = AppendParameter<object>(DBNull.Value, NpgsqlDbType.Text);
parameters[i] = parameter;
_builder.Append(split[i + 1]);
}
Expand All @@ -146,7 +168,7 @@ public NpgsqlParameter[] AppendWithParameters(string text, char separator)
}

// Just need a placeholder parameter type and value
var parameter = AppendParameter(DBNull.Value, NpgsqlDbType.Text);
var parameter = AppendParameter<object>(DBNull.Value, NpgsqlDbType.Text);
parameters[pos] = parameter;
_builder.Append(span[range]);
pos++;
Expand Down
19 changes: 15 additions & 4 deletions src/Weasel.Postgresql/CommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,30 @@ public CommandBuilder(NpgsqlCommand command): base(PostgresqlProvider.Instance,
/// <param name="dbType"></param>
public void AppendParameter(string[] values)
{
AppendParameter(values, NpgsqlDbType.Varchar | NpgsqlDbType.Array);
base.AppendParameter(values, NpgsqlDbType.Varchar | NpgsqlDbType.Array);
}

NpgsqlParameter ICommandBuilder.AppendParameter<T>(T value)
{
AppendParameter(value);
base.AppendParameter(value);
return _command.Parameters[^1];
}

public NpgsqlParameter AppendParameter<T>(T value, NpgsqlDbType dbType)
{
base.AppendParameter(value, dbType);
return _command.Parameters[^1];
}

NpgsqlParameter ICommandBuilder.AppendParameter(object value)
{
AppendParameter(value);
base.AppendParameter(value);
return _command.Parameters[^1];
}

NpgsqlParameter ICommandBuilder.AppendParameter(object? value, NpgsqlDbType? dbType)
{
AppendParameter(value, dbType);
base.AppendParameter(value, dbType);
return _command.Parameters[^1];
}

Expand All @@ -65,6 +71,11 @@ void ICommandBuilder.AppendParameters(params object[] parameters)
}
}

public IGroupedParameterBuilder CreateGroupedParameterBuilder(char? seperator = null)
{
return new GroupedParameterBuilder(this, seperator);
}

public void StartNewCommand()
{
// do nothing!
Expand Down
41 changes: 41 additions & 0 deletions src/Weasel.Postgresql/GroupedParameterBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Npgsql;
using NpgsqlTypes;

namespace Weasel.Postgresql;

public interface IGroupedParameterBuilder
{
NpgsqlParameter AppendParameter<T>(T? value) where T : notnull;
NpgsqlParameter AppendParameter<T>(T? value, NpgsqlDbType dbType) where T : notnull;
}

public sealed class GroupedParameterBuilder: IGroupedParameterBuilder
{
private readonly ICommandBuilder _commandBuilder;
private readonly char? _seperator;
private int _count = 0;

public GroupedParameterBuilder(ICommandBuilder commandBuilder, char? seperator)
{
_commandBuilder = commandBuilder;
_seperator = seperator;
}

public NpgsqlParameter AppendParameter<T>(T? value) where T : notnull
{
if(_count > 0 && _seperator.HasValue)
_commandBuilder.Append(_seperator.Value);

_count++;
return _commandBuilder.AppendParameter(value);
}

public NpgsqlParameter AppendParameter<T>(T? value, NpgsqlDbType dbType) where T : notnull
{
if(_count > 0 && _seperator.HasValue)
_commandBuilder.Append(_seperator.Value);

_count++;
return _commandBuilder.AppendParameter(value, dbType);
}
}
4 changes: 4 additions & 0 deletions src/Weasel.Postgresql/ICommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ public interface ICommandBuilder

void Append(string sql);
void Append(char character);

NpgsqlParameter AppendParameter<T>(T value);
NpgsqlParameter AppendParameter<T>(T value, NpgsqlDbType dbType);
NpgsqlParameter AppendParameter(object value);
NpgsqlParameter AppendParameter(object? value, NpgsqlDbType? dbType);
void AppendParameters(params object[] parameters);

IGroupedParameterBuilder CreateGroupedParameterBuilder(char? seperator = null);

/// <summary>
/// Append a SQL string with user defined placeholder characters for new parameters, and returns an
/// array of the newly created parameters
Expand Down

0 comments on commit 290f549

Please sign in to comment.