Skip to content

Commit

Permalink
Fix npgsql parameter boxing within codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawxy committed Jul 22, 2024
1 parent af67c57 commit eef1383
Show file tree
Hide file tree
Showing 25 changed files with 340 additions and 227 deletions.
173 changes: 127 additions & 46 deletions src/EventSourcingTests/Internal/Generated/EventStore/EventStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ public override async System.Threading.Tasks.Task ApplyReaderDataToEventAsync(Sy
e.IsArchived = isArchived;
}


public override Marten.Internal.Operations.IStorageOperation QuickAppendEventWithVersion(Marten.Events.EventGraph events, Marten.Internal.IMartenSession session, Marten.Events.StreamAction stream, Marten.Events.IEvent e)
{
return new Marten.Generated.EventStore.AppendEventOperationQuickWithVersion(stream, e);
}


public override Marten.Internal.Operations.IStorageOperation QuickAppendEvents(Marten.Events.StreamAction stream)
{
return new Marten.Generated.EventStore.QuickAppendEventsOperation(stream);
}

}

// END: GeneratedEventDocumentStorage
Expand All @@ -127,37 +139,106 @@ public AppendEventOperation(Marten.Events.StreamAction stream, Marten.Events.IEv
}


public const string SQL = "insert into public.mt_events (data, type, mt_dotnet_type, seq_id, id, stream_id, version, timestamp, tenant_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";


public override void ConfigureCommand(Weasel.Postgresql.ICommandBuilder builder, Marten.Internal.IMartenSession session)
{
var parameters = builder.AppendWithParameters(SQL);
parameters[0].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Jsonb;
parameters[0].Value = session.Serializer.ToJson(Event.Data);
parameters[1].Value = Event.EventTypeName != null ? (object)Event.EventTypeName : System.DBNull.Value;
parameters[1].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
parameters[2].Value = Event.DotNetTypeName != null ? (object)Event.DotNetTypeName : System.DBNull.Value;
parameters[2].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
parameters[3].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
parameters[3].Value = Event.Sequence;
parameters[4].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
parameters[4].Value = Event.Id;
parameters[5].Value = Stream.Id;
parameters[5].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
parameters[6].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
parameters[6].Value = Event.Version;
parameters[7].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.TimestampTz;
parameters[7].Value = Event.Timestamp;
parameters[8].Value = Stream.TenantId != null ? (object)Stream.TenantId : System.DBNull.Value;
parameters[8].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
builder.Append("insert into public.mt_events (data, type, mt_dotnet_type, id, stream_id, version, timestamp, tenant_id, seq_id) values (");
var parameterBuilder = builder.CreateGroupedParameterBuilder(',');
var parameter0 = parameterBuilder.AppendParameter(session.Serializer.ToJson(Event.Data));
parameter0.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Jsonb;
var parameter1 = Event.EventTypeName != null ? parameterBuilder.AppendParameter(Event.EventTypeName) : parameterBuilder.AppendParameter<object>(System.DBNull.Value);
parameter1.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
var parameter2 = Event.DotNetTypeName != null ? parameterBuilder.AppendParameter(Event.DotNetTypeName) : parameterBuilder.AppendParameter<object>(System.DBNull.Value);
parameter2.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
var parameter3 = parameterBuilder.AppendParameter(Event.Id);
parameter3.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
var parameter4 = parameterBuilder.AppendParameter(Stream.Id);
parameter4.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
var parameter5 = parameterBuilder.AppendParameter(Event.Version);
parameter5.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
var parameter6 = parameterBuilder.AppendParameter(Event.Timestamp);
parameter6.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.TimestampTz;
var parameter7 = Stream.TenantId != null ? parameterBuilder.AppendParameter(Stream.TenantId) : parameterBuilder.AppendParameter<object>(System.DBNull.Value);
parameter7.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
var parameter8 = parameterBuilder.AppendParameter(Event.Sequence);
parameter8.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
builder.Append(')');
}

}

// END: AppendEventOperation


// START: AppendEventOperationQuickWithVersion
public class AppendEventOperationQuickWithVersion : Marten.Events.Operations.AppendEventOperationBase
{
private readonly Marten.Events.StreamAction _stream;
private readonly Marten.Events.IEvent _e;

public AppendEventOperationQuickWithVersion(Marten.Events.StreamAction stream, Marten.Events.IEvent e) : base(stream, e)
{
_stream = stream;
_e = e;
}



public override void ConfigureCommand(Weasel.Postgresql.ICommandBuilder builder, Marten.Internal.IMartenSession session)
{
builder.Append("insert into public.mt_events (data, type, mt_dotnet_type, id, stream_id, version, timestamp, tenant_id, seq_id) values (");
var parameterBuilder = builder.CreateGroupedParameterBuilder(',');
var parameter0 = parameterBuilder.AppendParameter(session.Serializer.ToJson(Event.Data));
parameter0.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Jsonb;
var parameter1 = Event.EventTypeName != null ? parameterBuilder.AppendParameter(Event.EventTypeName) : parameterBuilder.AppendParameter<object>(System.DBNull.Value);
parameter1.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
var parameter2 = Event.DotNetTypeName != null ? parameterBuilder.AppendParameter(Event.DotNetTypeName) : parameterBuilder.AppendParameter<object>(System.DBNull.Value);
parameter2.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
var parameter3 = parameterBuilder.AppendParameter(Event.Id);
parameter3.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
var parameter4 = parameterBuilder.AppendParameter(Stream.Id);
parameter4.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
var parameter5 = parameterBuilder.AppendParameter(Event.Version);
parameter5.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
var parameter6 = parameterBuilder.AppendParameter(Event.Timestamp);
parameter6.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.TimestampTz;
var parameter7 = Stream.TenantId != null ? parameterBuilder.AppendParameter(Stream.TenantId) : parameterBuilder.AppendParameter<object>(System.DBNull.Value);
parameter7.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
builder.Append(",nextval('public.mt_events_sequence')");
builder.Append(')');
}

}

// END: AppendEventOperationQuickWithVersion


// START: QuickAppendEventsOperation
public class QuickAppendEventsOperation : Marten.Events.Operations.QuickAppendEventsOperationBase
{
private readonly Marten.Events.StreamAction _stream;

public QuickAppendEventsOperation(Marten.Events.StreamAction stream) : base(stream)
{
_stream = stream;
}



public override void ConfigureCommand(Weasel.Postgresql.ICommandBuilder builder, Marten.Internal.IMartenSession session)
{
builder.Append("select public.mt_quick_append_events(");
var parameterBuilder = builder.CreateGroupedParameterBuilder(',');
writeId(parameterBuilder);
writeBasicParameters(parameterBuilder, session);
builder.Append(')');
}

}

// END: QuickAppendEventsOperation


// START: GeneratedInsertStream
public class GeneratedInsertStream : Marten.Events.Operations.InsertStreamBase
{
Expand All @@ -169,20 +250,20 @@ public GeneratedInsertStream(Marten.Events.StreamAction stream) : base(stream)
}


public const string SQL = "insert into public.mt_streams (id, type, version, tenant_id) values (?, ?, ?, ?)";


public override void ConfigureCommand(Weasel.Postgresql.ICommandBuilder builder, Marten.Internal.IMartenSession session)
{
var parameters = builder.AppendWithParameters(SQL);
parameters[0].Value = Stream.Id;
parameters[0].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
parameters[1].Value = Stream.AggregateTypeName != null ? (object)Stream.AggregateTypeName : System.DBNull.Value;
parameters[1].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
parameters[2].Value = Stream.Version;
parameters[2].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
parameters[3].Value = Stream.TenantId != null ? (object)Stream.TenantId : System.DBNull.Value;
parameters[3].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
builder.Append("insert into public.mt_streams (id, type, version, tenant_id) values (");
var parameterBuilder = builder.CreateGroupedParameterBuilder(',');
var parameter0 = parameterBuilder.AppendParameter(Stream.Id);
parameter0.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
var parameter1 = Stream.AggregateTypeName != null ? parameterBuilder.AppendParameter(Stream.AggregateTypeName) : parameterBuilder.AppendParameter<object>(System.DBNull.Value);
parameter1.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
var parameter2 = parameterBuilder.AppendParameter(Stream.Version);
parameter2.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
var parameter3 = Stream.TenantId != null ? parameterBuilder.AppendParameter(Stream.TenantId) : parameterBuilder.AppendParameter<object>(System.DBNull.Value);
parameter3.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
builder.Append(')');
}

}
Expand All @@ -201,14 +282,12 @@ public GeneratedStreamStateQueryHandler(System.Guid streamId)
}


public const string SQL = "select id, version, type, timestamp, created as timestamp, is_archived from public.mt_streams where id = ?";


public override void ConfigureCommand(Weasel.Postgresql.ICommandBuilder builder, Marten.Internal.IMartenSession session)
{
var npgsqlParameterArray = builder.AppendWithParameters(SQL);
npgsqlParameterArray[0].Value = _streamId;
npgsqlParameterArray[0].DbType = System.Data.DbType.Guid;
builder.Append("select id, version, type, timestamp, created as timestamp, is_archived from public.mt_streams where id = ");
var parameter1 = builder.AppendParameter(_streamId);
parameter1.DbType = System.Data.DbType.Guid;
}


Expand Down Expand Up @@ -263,18 +342,20 @@ public GeneratedStreamVersionOperation(Marten.Events.StreamAction stream) : base
}


public const string SQL = "update public.mt_streams set version = ? where id = ? and version = ?";


public override void ConfigureCommand(Weasel.Postgresql.ICommandBuilder builder, Marten.Internal.IMartenSession session)
{
var parameters = builder.AppendWithParameters(SQL);
parameters[0].Value = Stream.Version;
parameters[0].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
parameters[1].Value = Stream.Id;
parameters[1].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
parameters[2].Value = Stream.ExpectedVersionOnServer;
parameters[2].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
builder.Append("update public.mt_streams ");
var parameterBuilder = builder.CreateGroupedParameterBuilder();
builder.Append("set version = ");
var parameter0 = parameterBuilder.AppendParameter(Stream.Version);
parameter0.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
builder.Append("where id = ");
var parameter1 = parameterBuilder.AppendParameter(Stream.Id);
parameter1.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Uuid;
builder.Append("and version = ");
var parameter2 = parameterBuilder.AppendParameter(Stream.ExpectedVersionOnServer);
parameter2.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Bigint;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public override void ApplyEvent(Marten.IDocumentOperations operations, Marten.Ev
{
switch (e)
{
case Marten.Events.IEvent<EventSourcingTests.Bugs.Bug_2438_generated_code_throwing_nre.MyEvent> event_MyEvent11272:
Project1.Invoke(event_MyEvent11272.Data, operations);
case Marten.Events.IEvent<EventSourcingTests.Bugs.Bug_2438_generated_code_throwing_nre.MyEvent> event_MyEvent11301:
Project1.Invoke(event_MyEvent11301.Data, operations);
break;
}

Expand Down
13 changes: 13 additions & 0 deletions src/Marten/Events/CodeGeneration/CodeGenerationExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<ICommandBuilder>());
}

public static void AppendSql(this FramesCollection collection, char sql)
{
collection.Code($"{{0}}.{nameof(CommandBuilder.Append)}('{sql}');", Use.Type<ICommandBuilder>());
}
}
Loading

0 comments on commit eef1383

Please sign in to comment.