diff --git a/src/Marten.CommandLine/Marten.CommandLine.csproj b/src/Marten.CommandLine/Marten.CommandLine.csproj
index 97888786e3..6f46518b82 100644
--- a/src/Marten.CommandLine/Marten.CommandLine.csproj
+++ b/src/Marten.CommandLine/Marten.CommandLine.csproj
@@ -36,7 +36,7 @@
-
+
diff --git a/src/Marten/Events/CodeGeneration/CodeGenerationExtensions.cs b/src/Marten/Events/CodeGeneration/CodeGenerationExtensions.cs
index f393f1ea00..11264c42b9 100644
--- a/src/Marten/Events/CodeGeneration/CodeGenerationExtensions.cs
+++ b/src/Marten/Events/CodeGeneration/CodeGenerationExtensions.cs
@@ -1,8 +1,11 @@
using System;
using System.Linq;
using System.Reflection;
+using JasperFx.CodeGeneration;
+using JasperFx.CodeGeneration.Frames;
using JasperFx.Core.Reflection;
using Marten.Internal;
+using Weasel.Postgresql;
namespace Marten.Events.CodeGeneration;
@@ -42,4 +45,14 @@ public static Type GetEventType(this MethodInfo method, Type aggregateType)
return parameterInfo.ParameterType;
}
+
+ public static void AppendSql(this FramesCollection collection, string sql)
+ {
+ collection.Code($"{{0}}.{nameof(CommandBuilder.Append)}(\"{sql}\");", Use.Type());
+ }
+
+ public static void AppendSql(this FramesCollection collection, char sql)
+ {
+ collection.Code($"{{0}}.{nameof(CommandBuilder.Append)}('{sql}');", Use.Type());
+ }
}
diff --git a/src/Marten/Events/CodeGeneration/EventDocumentStorageGenerator.cs b/src/Marten/Events/CodeGeneration/EventDocumentStorageGenerator.cs
index 3bb9768114..4279c18099 100644
--- a/src/Marten/Events/CodeGeneration/EventDocumentStorageGenerator.cs
+++ b/src/Marten/Events/CodeGeneration/EventDocumentStorageGenerator.cs
@@ -109,23 +109,20 @@ private static GeneratedType buildUpdateStreamVersion(GeneratedType builderType,
{
var operationType = assembly.AddType(UpdateStreamVersionOperationName, typeof(UpdateStreamVersion));
- var sql = $"update {graph.DatabaseSchemaName}.mt_streams set version = ? where id = ? and version = ?";
- if (graph.TenancyStyle == TenancyStyle.Conjoined)
- {
- sql += $" and {TenantIdColumn.Name} = ?";
- }
-
- var setter = operationType.AddStringConstant("SQL", sql);
+ var sql = $"update {graph.DatabaseSchemaName}.mt_streams ";
var configureCommand = operationType.MethodFor("ConfigureCommand");
configureCommand.DerivedVariables.Add(
new Variable(typeof(StreamAction), nameof(UpdateStreamVersion.Stream)));
- configureCommand.Frames.Code($"var parameters = {{0}}.{nameof(CommandBuilder.AppendWithParameters)}(SQL);",
- Use.Type());
+ configureCommand.Frames.AppendSql(sql);
+
+ configureCommand.Frames.Code($"var parameterBuilder = {{0}}.{nameof(CommandBuilder.CreateGroupedParameterBuilder)}();", Use.Type());
+ configureCommand.Frames.AppendSql("set version = ");
configureCommand.SetParameterFromMember(0, x => x.Version);
+ configureCommand.Frames.AppendSql("where id = ");
if (graph.StreamIdentity == StreamIdentity.AsGuid)
{
configureCommand.SetParameterFromMember(1, x => x.Id);
@@ -135,10 +132,12 @@ private static GeneratedType buildUpdateStreamVersion(GeneratedType builderType,
configureCommand.SetParameterFromMember(1, x => x.Key);
}
+ configureCommand.Frames.AppendSql("and version = ");
configureCommand.SetParameterFromMember(2, x => x.ExpectedVersionOnServer);
if (graph.TenancyStyle == TenancyStyle.Conjoined)
{
+ configureCommand.Frames.AppendSql($" and {TenantIdColumn.Name} = ");
new TenantIdColumn().As().GenerateAppendCode(configureCommand, 3);
}
@@ -231,31 +230,26 @@ private static GeneratedType buildStreamQueryHandlerType(EventGraph graph, Gener
private static void buildConfigureCommandMethodForStreamState(EventGraph graph,
GeneratedType streamQueryHandlerType)
{
- var sql =
- $"select id, version, type, timestamp, created as timestamp, is_archived from {graph.DatabaseSchemaName}.mt_streams where id = ?";
if (graph.TenancyStyle == TenancyStyle.Conjoined)
{
streamQueryHandlerType.AllInjectedFields.Add(new InjectedField(typeof(string), "tenantId"));
- sql += $" and {TenantIdColumn.Name} = ?";
}
- var setter = streamQueryHandlerType.AddStringConstant("SQL", sql);
-
var configureCommand = streamQueryHandlerType.MethodFor("ConfigureCommand");
- configureCommand.Frames.Call(x => x.AppendWithParameters(""), call =>
- {
- call.Arguments[0] = setter;
- call.ReturnAction = ReturnAction.Initialize;
- });
+ var sql =
+ $"select id, version, type, timestamp, created as timestamp, is_archived from {graph.DatabaseSchemaName}.mt_streams where id = ";
+
+ configureCommand.Frames.AppendSql(sql);
var idDbType = graph.StreamIdentity == StreamIdentity.AsGuid ? DbType.Guid : DbType.String;
- configureCommand.Frames.Code("{0}[0].Value = _streamId;", Use.Type());
- configureCommand.Frames.Code("{0}[0].DbType = {1};", Use.Type(), idDbType);
+ configureCommand.Frames.Code($"var parameter1 = builder.{nameof(CommandBuilder.AppendParameter)}(_streamId);");
+ configureCommand.Frames.Code("parameter1.DbType = {0};", idDbType);
if (graph.TenancyStyle == TenancyStyle.Conjoined)
{
- configureCommand.Frames.Code("{0}[1].Value = _tenantId;", Use.Type());
- configureCommand.Frames.Code("{0}[1].DbType = {1};", Use.Type(), DbType.String);
+ configureCommand.Frames.AppendSql($" and {TenantIdColumn.Name} = ");
+ configureCommand.Frames.Code($"var parameter2 = builder.{nameof(CommandBuilder.AppendParameter)}(_tenantId);");
+ configureCommand.Frames.Code("parameter2.DbType = {0};", DbType.String);
}
}
@@ -286,18 +280,22 @@ private static GeneratedType buildAppendEventOperation(EventGraph graph, Generat
columns.Add(sequence);
var sql =
- $"insert into {graph.DatabaseSchemaName}.mt_events ({columns.Select(x => x.Name).Join(", ")}) values ({columns.Select(c => c.ValueSql(graph, mode)).Join(", ")})";
+ $"insert into {graph.DatabaseSchemaName}.mt_events ({columns.Select(x => x.Name).Join(", ")}) values (";
- operationType.AddStringConstant("SQL", sql);
+ configure.Frames.AppendSql(sql);
- configure.Frames.Code($"var parameters = {{0}}.{nameof(CommandBuilder.AppendWithParameters)}(SQL);",
- Use.Type());
+ configure.Frames.Code($"var parameterBuilder = {{0}}.{nameof(CommandBuilder.CreateGroupedParameterBuilder)}(',');", Use.Type());
for (var i = 0; i < columns.Count; i++)
{
columns[i].GenerateAppendCode(configure, graph, i, mode);
+ var valueSql = columns[i].ValueSql(graph, mode);
+ if (valueSql != "?")
+ configure.Frames.AppendSql($"{(i > 0 ? "," : string.Empty)}{valueSql}");
}
+ configure.Frames.AppendSql(')');
+
return operationType;
}
@@ -306,68 +304,44 @@ private static GeneratedType buildQuickAppendOperation(EventGraph graph, Generat
var operationType = assembly.AddType("QuickAppendEventsOperation", typeof(QuickAppendEventsOperationBase));
var table = new EventsTable(graph);
- var parameterList = "";
-
- var index = 6;
- int causationIndex = 0;
- int correlationIndex = 0;
- int headerIndex = 0;
- if (table.Columns.OfType().Any())
- {
- parameterList += ", ?";
- causationIndex = ++index;
- }
-
- if (table.Columns.OfType().Any())
- {
- parameterList += ", ?";
- correlationIndex = ++index;
- }
-
- if (table.Columns.OfType().Any())
- {
- parameterList += ", ?";
- headerIndex = ++index;
- }
-
- var sql =
- $"select {graph.DatabaseSchemaName}.mt_quick_append_events(?, ?, ?, ?, ?, ?, ?{parameterList})";
-
- operationType.AddStringConstant("SQL", sql);
+ var sql = $"select {graph.DatabaseSchemaName}.mt_quick_append_events(";
var configure = operationType.MethodFor(nameof(QuickAppendEventsOperationBase.ConfigureCommand));
configure.DerivedVariables.Add(new Variable(typeof(StreamAction), nameof(QuickAppendEventsOperationBase.Stream)));
- configure.Frames.Code($"var parameters = {{0}}.{nameof(CommandBuilder.AppendWithParameters)}(SQL);",
- Use.Type());
+ configure.Frames.AppendSql(sql);
+
+ configure.Frames.Code($"var parameterBuilder = {{0}}.{nameof(CommandBuilder.CreateGroupedParameterBuilder)}(',');", Use.Type());
if (graph.StreamIdentity == StreamIdentity.AsGuid)
{
- configure.Frames.Code("writeId(parameters);");
+ configure.Frames.Code("writeId(parameterBuilder);");
}
else
{
- configure.Frames.Code("writeKey(parameters);");
+ configure.Frames.Code("writeKey(parameterBuilder);");
}
- configure.Frames.Code("writeBasicParameters(parameters, session);");
+ configure.Frames.Code("writeBasicParameters(parameterBuilder, session);");
- if (causationIndex > 0)
+ if (table.Columns.OfType().Any())
{
- configure.Frames.Code($"writeCausationIds({causationIndex}, parameters);");
+ configure.Frames.Code("writeCausationIds(parameterBuilder);");
}
- if (correlationIndex > 0)
+ if (table.Columns.OfType().Any())
{
- configure.Frames.Code($"writeCorrelationIds({correlationIndex}, parameters);");
+ configure.Frames.Code("writeCorrelationIds(parameterBuilder);");
}
- if (headerIndex > 0)
+ if (table.Columns.OfType().Any())
{
- configure.Frames.Code($"writeHeaders({headerIndex}, parameters, session);");
+ configure.Frames.Code("writeHeaders(parameterBuilder, session);");
}
+ configure.Frames.AppendSql(')');
+
return operationType;
}
@@ -383,20 +357,23 @@ private static GeneratedType buildInsertStream(GeneratedType builderType, Genera
.ToArray();
var sql =
- $"insert into {graph.DatabaseSchemaName}.mt_streams ({columns.Select(x => x.Name).Join(", ")}) values ({columns.Select(_ => "?").Join(", ")})";
- operationType.AddStringConstant("SQL", sql);
+ $"insert into {graph.DatabaseSchemaName}.mt_streams ({columns.Select(x => x.Name).Join(", ")}) values (";
+
var configureCommand = operationType.MethodFor("ConfigureCommand");
configureCommand.DerivedVariables.Add(new Variable(typeof(StreamAction), nameof(InsertStreamBase.Stream)));
- configureCommand.Frames.Code($"var parameters = {{0}}.{nameof(CommandBuilder.AppendWithParameters)}(SQL);",
- Use.Type());
+ configureCommand.Frames.AppendSql(sql);
+
+ configureCommand.Frames.Code($"var parameterBuilder = {{0}}.{nameof(CommandBuilder.CreateGroupedParameterBuilder)}(',');", Use.Type());
for (var i = 0; i < columns.Length; i++)
{
columns[i].GenerateAppendCode(configureCommand, i);
}
+ configureCommand.Frames.AppendSql(')');
+
builderType.MethodFor(nameof(EventDocumentStorage.InsertStream))
.Frames.ReturnNewGeneratedTypeObject(operationType, "stream");
diff --git a/src/Marten/Events/Daemon/Internals/EventTypeFilter.cs b/src/Marten/Events/Daemon/Internals/EventTypeFilter.cs
index 1103f3864e..ec2469dc8e 100644
--- a/src/Marten/Events/Daemon/Internals/EventTypeFilter.cs
+++ b/src/Marten/Events/Daemon/Internals/EventTypeFilter.cs
@@ -28,9 +28,10 @@ public EventTypeFilter(EventGraph graph, IReadOnlyCollection eventTypes)
public void Apply(ICommandBuilder builder)
{
- var parameters = builder.AppendWithParameters("d.type = ANY(?)");
- parameters[0].NpgsqlDbType = NpgsqlDbType.Varchar | NpgsqlDbType.Array;
- parameters[0].Value = _typeNames;
+ builder.Append("d.type = ANY(");
+ var parameter = builder.AppendParameter(_typeNames);
+ parameter.NpgsqlDbType = NpgsqlDbType.Varchar | NpgsqlDbType.Array;
+ builder.Append(')');
}
}
diff --git a/src/Marten/Events/Operations/QuickAppendEventsOperationBase.cs b/src/Marten/Events/Operations/QuickAppendEventsOperationBase.cs
index 7cd1128179..195f9bba0b 100644
--- a/src/Marten/Events/Operations/QuickAppendEventsOperationBase.cs
+++ b/src/Marten/Events/Operations/QuickAppendEventsOperationBase.cs
@@ -52,50 +52,55 @@ public void Postprocess(DbDataReader reader, IList exceptions)
}
}
- protected void writeId(NpgsqlParameter[] parameters)
+ protected void writeId(IGroupedParameterBuilder builder)
{
- parameters[0].NpgsqlDbType = NpgsqlDbType.Uuid;
- parameters[0].Value = Stream.Id;
+ var param = builder.AppendParameter(Stream.Id);
+ param.NpgsqlDbType = NpgsqlDbType.Uuid;
}
- protected void writeKey(NpgsqlParameter[] parameters)
+ protected void writeKey(IGroupedParameterBuilder builder)
{
- parameters[0].NpgsqlDbType = NpgsqlDbType.Varchar;
- parameters[0].Value = Stream.Key;
+ var param = builder.AppendParameter(Stream.Key);
+ param.NpgsqlDbType = NpgsqlDbType.Varchar;
}
- protected void writeBasicParameters(NpgsqlParameter[] parameters, IMartenSession session)
+ protected void writeBasicParameters(IGroupedParameterBuilder builder, IMartenSession session)
{
- parameters[1].NpgsqlDbType = NpgsqlDbType.Varchar;
- parameters[1].Value = Stream.AggregateTypeName.IsEmpty() ? DBNull.Value : Stream.AggregateTypeName;
- parameters[2].NpgsqlDbType = NpgsqlDbType.Varchar;
- parameters[2].Value = Stream.TenantId;
- parameters[3].NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Uuid;
- parameters[3].Value = Stream.Events.Select(x => x.Id).ToArray();
- parameters[4].NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Varchar;
- parameters[4].Value = Stream.Events.Select(x => x.EventTypeName).ToArray();
- parameters[5].NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Varchar;
- parameters[5].Value = Stream.Events.Select(x => x.DotNetTypeName).ToArray();
- parameters[6].NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Jsonb;
- parameters[6].Value = Stream.Events.Select(e => session.Serializer.ToJson(e.Data)).ToArray();
+ var param1 = Stream.AggregateTypeName.IsEmpty() ? builder.AppendParameter