diff --git a/src/Weasel.Postgresql/BatchBuilder.cs b/src/Weasel.Postgresql/BatchBuilder.cs index ae443bf..b71c252 100644 --- a/src/Weasel.Postgresql/BatchBuilder.cs +++ b/src/Weasel.Postgresql/BatchBuilder.cs @@ -65,6 +65,23 @@ public NpgsqlParameter AppendParameter(T value) return param; } + public NpgsqlParameter AppendParameter(T value, NpgsqlDbType dbType) + { + _current ??= appendCommand(); + var param = new NpgsqlParameter() { + TypedValue = value, + NpgsqlDbType = dbType + }; + + _current.Parameters.Add(param); + + _builder.Append('$'); + _builder.Append(_current.Parameters.Count); + + return param; + } + + public NpgsqlParameter AppendParameter(object value) { _current ??= appendCommand(); @@ -97,6 +114,11 @@ public void AppendParameters(params object[] parameters) } } + public IGroupedParameterBuilder CreateGroupedParameterBuilder(char? seperator = null) + { + return new GroupedParameterBuilder(this, seperator); + } + /// /// Append a SQL string with user defined placeholder characters for new parameters, and returns an /// array of the newly created parameters @@ -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(DBNull.Value, NpgsqlDbType.Text); parameters[i] = parameter; _builder.Append(split[i + 1]); } @@ -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(DBNull.Value, NpgsqlDbType.Text); parameters[pos] = parameter; _builder.Append(span[range]); pos++; diff --git a/src/Weasel.Postgresql/CommandBuilder.cs b/src/Weasel.Postgresql/CommandBuilder.cs index f30ddf5..e556944 100644 --- a/src/Weasel.Postgresql/CommandBuilder.cs +++ b/src/Weasel.Postgresql/CommandBuilder.cs @@ -29,24 +29,30 @@ public CommandBuilder(NpgsqlCommand command): base(PostgresqlProvider.Instance, /// public void AppendParameter(string[] values) { - AppendParameter(values, NpgsqlDbType.Varchar | NpgsqlDbType.Array); + base.AppendParameter(values, NpgsqlDbType.Varchar | NpgsqlDbType.Array); } NpgsqlParameter ICommandBuilder.AppendParameter(T value) { - AppendParameter(value); + base.AppendParameter(value); + return _command.Parameters[^1]; + } + + public NpgsqlParameter AppendParameter(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]; } @@ -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! diff --git a/src/Weasel.Postgresql/GroupedParameterBuilder.cs b/src/Weasel.Postgresql/GroupedParameterBuilder.cs new file mode 100644 index 0000000..17d8b66 --- /dev/null +++ b/src/Weasel.Postgresql/GroupedParameterBuilder.cs @@ -0,0 +1,41 @@ +using Npgsql; +using NpgsqlTypes; + +namespace Weasel.Postgresql; + +public interface IGroupedParameterBuilder +{ + NpgsqlParameter AppendParameter(T? value) where T : notnull; + NpgsqlParameter AppendParameter(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? value) where T : notnull + { + if(_count > 0 && _seperator.HasValue) + _commandBuilder.Append(_seperator.Value); + + _count++; + return _commandBuilder.AppendParameter(value); + } + + public NpgsqlParameter AppendParameter(T? value, NpgsqlDbType dbType) where T : notnull + { + if(_count > 0 && _seperator.HasValue) + _commandBuilder.Append(_seperator.Value); + + _count++; + return _commandBuilder.AppendParameter(value, dbType); + } +} diff --git a/src/Weasel.Postgresql/ICommandBuilder.cs b/src/Weasel.Postgresql/ICommandBuilder.cs index d8ff9fd..791acf7 100644 --- a/src/Weasel.Postgresql/ICommandBuilder.cs +++ b/src/Weasel.Postgresql/ICommandBuilder.cs @@ -18,11 +18,15 @@ public interface ICommandBuilder void Append(string sql); void Append(char character); + NpgsqlParameter AppendParameter(T value); + NpgsqlParameter AppendParameter(T value, NpgsqlDbType dbType); NpgsqlParameter AppendParameter(object value); NpgsqlParameter AppendParameter(object? value, NpgsqlDbType? dbType); void AppendParameters(params object[] parameters); + IGroupedParameterBuilder CreateGroupedParameterBuilder(char? seperator = null); + /// /// Append a SQL string with user defined placeholder characters for new parameters, and returns an /// array of the newly created parameters