Path: redb/redb.Tests.Integration/Tests/Base/RecordingRedbContext.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/RecordingRedbContext.cs ---- using System.Collections.Concurrent; using System.Data; using Microsoft.Extensions.DependencyInjection; using redb.Core.Data; namespace redb.Tests.Integration.Tests.Base; /// /// One command seen by : its SQL text, the thread it was issued on and the context /// instance (one scope, one connection) it ran on. /// public sealed record RecordedCommand(string Sql, int ThreadId, IRedbContext Context); /// /// Commands that went through a between and , /// and how many contexts - scopes with a connection of their own - were created meanwhile. /// public sealed class SqlRecorder { private readonly ConcurrentQueue _commands = new(); private volatile bool _armed; private int _contextsCreated; /// Contexts created since . public int ContextsCreated => Volatile.Read(ref _contextsCreated); public void Start() { _commands.Clear(); Interlocked.Exchange(ref _contextsCreated, 0); _armed = true; } public IReadOnlyList Stop() { _armed = false; return _commands.ToArray(); } /// The commands recorded so far; recording goes on. public IReadOnlyList Peek() => _commands.ToArray(); /// /// Runs before a QueryAsync command is forwarded, with its SQL text, armed or not: a test does something to the /// database between two statements of one operation (a delete between the read of the objects and of their values). /// public Func? BeforeQuery { get; set; } /// Runs after a QueryAsync command returned, with its SQL text, armed or not (see ). public Func? AfterQuery { get; set; } internal void Note(IRedbContext context, string sql) { if (_armed) _commands.Enqueue(new RecordedCommand(sql, Environment.CurrentManagedThreadId, context)); } internal void NoteCreated() { if (_armed) Interlocked.Increment(ref _contextsCreated); } } /// /// Test decorator over the provider's : records the SQL text of every command that goes /// through the context, then forwards it. It tells WHICH road a load took - the Free in-database JSON builder or the Pro /// C# materializer - and ON WHICH context, without depending on the database having that function: on SQLite the Free /// native extension is process-wide static state that another suite may or may not have loaded. /// /// Every SQL-bearing member is implemented here, the synchronous ones included: their interface defaults forward to /// and would bypass the record. A new SQL member of must be /// added here too. /// /// public sealed class RecordingRedbContext : IRedbContext { private readonly IRedbContext _inner; private readonly SqlRecorder _recorder; public RecordingRedbContext(IRedbContext inner, SqlRecorder recorder) { _inner = inner; _recorder = recorder; } /// Wraps the last registration - the one every service resolves. public static void Decorate(IServiceCollection services, SqlRecorder recorder) { var descriptor = services.LastOrDefault(d => d.ServiceType == typeof(IRedbContext)) ?? throw new InvalidOperationException("No IRedbContext registration to decorate"); var factory = descriptor.ImplementationFactory ?? throw new InvalidOperationException($"IRedbContext is expected to be registered by a factory: {descriptor}"); services.Remove(descriptor); services.Add(new ServiceDescriptor(typeof(IRedbContext), sp => { recorder.NoteCreated(); return new RecordingRedbContext((IRedbContext)factory(sp), recorder); }, descriptor.Lifetime)); } private string Note(string sql) { _recorder.Note(this, sql); return sql; } public bool IsDisposed => _inner.IsDisposed; public IRedbConnection Db => _inner.Db; public IKeyGenerator Keys => _inner.Keys; public IBulkOperations Bulk => _inner.Bulk; public async Task> QueryAsync(string sql, params object[] parameters) where T : new() { if (_recorder.BeforeQuery is { } before) await before(sql); var rows = await _inner.QueryAsync(Note(sql), parameters); if (_recorder.AfterQuery is { } after) await after(sql); return rows; } public async Task> QueryAsync(string sql, object[] parameters, CancellationToken cancellationToken) where T : new() { if (_recorder.BeforeQuery is { } before) await before(sql); var rows = await _inner.QueryAsync(Note(sql), parameters, cancellationToken); if (_recorder.AfterQuery is { } after) await after(sql); return rows; } public Task QueryFirstOrDefaultAsync(string sql, params object[] parameters) where T : class, new() => _inner.QueryFirstOrDefaultAsync(Note(sql), parameters); public Task QueryFirstOrDefaultAsync(string sql, object[] parameters, CancellationToken cancellationToken) where T : class, new() => _inner.QueryFirstOrDefaultAsync(Note(sql), parameters, cancellationToken); public Task ExecuteScalarAsync(string sql, params object[] parameters) => _inner.ExecuteScalarAsync(Note(sql), parameters); public Task ExecuteScalarAsync(string sql, object[] parameters, CancellationToken cancellationToken) => _inner.ExecuteScalarAsync(Note(sql), parameters, cancellationToken); public T? QueryFirstOrDefault(string sql, params object[] parameters) where T : class, new() => _inner.QueryFirstOrDefault(Note(sql), parameters); public T? ExecuteScalar(string sql, params object[] parameters) => _inner.ExecuteScalar(Note(sql), parameters); public string? ExecuteJson(string sql, params object[] parameters) => _inner.ExecuteJson(Note(sql), parameters); public List Query(string sql, params object[] parameters) where T : new() => _inner.Query(Note(sql), parameters); public int Execute(string sql, params object[] parameters) => _inner.Execute(Note(sql), parameters); public Task> QueryScalarListAsync(string sql, params object[] parameters) => _inner.QueryScalarListAsync(Note(sql), parameters); public Task> QueryScalarListAsync(string sql, object[] parameters, CancellationToken cancellationToken) => _inner.QueryScalarListAsync(Note(sql), parameters, cancellationToken); public Task ExecuteAsync(string sql, params object[] parameters) => _inner.ExecuteAsync(Note(sql), parameters); public Task ExecuteAsync(string sql, object[] parameters, CancellationToken cancellationToken) => _inner.ExecuteAsync(Note(sql), parameters, cancellationToken); public Task ExecuteJsonAsync(string sql, params object[] parameters) => _inner.ExecuteJsonAsync(Note(sql), parameters); public Task ExecuteJsonAsync(string sql, object[] parameters, CancellationToken cancellationToken) => _inner.ExecuteJsonAsync(Note(sql), parameters, cancellationToken); public Task> ExecuteJsonListAsync(string sql, params object[] parameters) => _inner.ExecuteJsonListAsync(Note(sql), parameters); public Task> ExecuteJsonListAsync(string sql, object[] parameters, CancellationToken cancellationToken) => _inner.ExecuteJsonListAsync(Note(sql), parameters, cancellationToken); public Task QueryRowsAsJsonAsync(string sql, object[] parameters, CancellationToken cancellationToken = default) => _inner.QueryRowsAsJsonAsync(Note(sql), parameters, cancellationToken); public IRedbTransaction? CurrentTransaction => _inner.CurrentTransaction; public bool IsInTransaction => _inner.IsInTransaction; public Task BeginTransactionAsync(IsolationLevel? isolationLevel = null, CancellationToken cancellationToken = default) => _inner.BeginTransactionAsync(isolationLevel, cancellationToken); public Task ExecuteAtomicAsync(Func operations, CancellationToken cancellationToken = default) => _inner.ExecuteAtomicAsync(operations, cancellationToken); public Task ExecuteAtomicAsync(IsolationLevel isolationLevel, Func operations, CancellationToken cancellationToken = default) => _inner.ExecuteAtomicAsync(isolationLevel, operations, cancellationToken); public Task ExecuteAtomicAsync(Func> operations, CancellationToken cancellationToken = default) => _inner.ExecuteAtomicAsync(operations, cancellationToken); public Task ExecuteAtomicAsync(IsolationLevel isolationLevel, Func> operations, CancellationToken cancellationToken = default) => _inner.ExecuteAtomicAsync(isolationLevel, operations, cancellationToken); public Task NextObjectIdAsync(CancellationToken cancellationToken = default) => _inner.NextObjectIdAsync(cancellationToken); public Task NextValueIdAsync(CancellationToken cancellationToken = default) => _inner.NextValueIdAsync(cancellationToken); public Task NextObjectIdBatchAsync(int count, CancellationToken cancellationToken = default) => _inner.NextObjectIdBatchAsync(count, cancellationToken); public Task NextValueIdBatchAsync(int count, CancellationToken cancellationToken = default) => _inner.NextValueIdBatchAsync(count, cancellationToken); public ValueTask DisposeAsync() => _inner.DisposeAsync(); public void Dispose() => _inner.Dispose(); }