gandalf-reborn/Data/Database/GrDbConnectionInterceptor.cs
2025-03-02 12:51:02 +01:00

23 lines
886 B
C#

using System.Data;
using System.Data.Common;
using Abstractions;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace W542.GandalfReborn.Data.Database;
public class GrDbConnectionInterceptor(InvokerContext invokerContext) : IDbConnectionInterceptor
{
public const string CurrentSuspectKey = "gr.current.suspect";
public async Task ConnectionOpenedAsync(DbConnection connection, ConnectionEndEventData eventData, CancellationToken cancellationToken = new())
{
if (invokerContext.Invoker is null)
return;
var command = connection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = $"""
SET SESSION "{CurrentSuspectKey}" = {invokerContext.Invoker.SubjectId};
""";
await command.ExecuteNonQueryAsync(cancellationToken);
}
}