using System.Data; using System.Data.Common; using System.Runtime.CompilerServices; using Dapper; using FruityFoundation.DataAccess.Abstractions; namespace FruityFoundation.DataAccess.Core; // ReSharper disable once ClassWithVirtualMembersNeverInherited.Global public class NonTransactionalDbConnection : INonTransactionalDbConnection where TConnectionType : ConnectionType { private readonly DbConnection _connection; /// /// C'tor /// public NonTransactionalDbConnection(DbConnection connection) { _connection = connection; } /// public async Task> Query( string sql, object? param = null, CancellationToken cancellationToken = default ) => await _connection.QueryAsync(new CommandDefinition(sql, param, cancellationToken: cancellationToken)); /// public async IAsyncEnumerable QueryUnbuffered( string sql, object? param = null, [EnumeratorCancellation] CancellationToken cancellationToken = default ) { var query = _connection.QueryUnbufferedAsync(sql, param, transaction: null) .WithCancellation(cancellationToken); await foreach (var item in query) yield return item; } /// public async Task QuerySingle( string sql, object? param = null, CancellationToken cancellationToken = default ) => await _connection.QuerySingleAsync(new CommandDefinition(sql, param, cancellationToken: cancellationToken)); /// public async Task Execute( string sql, object? param = null, CancellationToken cancellationToken = default ) => await _connection.ExecuteAsync(new CommandDefinition(sql, param, cancellationToken: cancellationToken)); /// public async Task ExecuteScalar( string sql, object? param = null, CancellationToken cancellationToken = default ) => await _connection.ExecuteScalarAsync(new CommandDefinition(sql, param, cancellationToken: cancellationToken)); /// public async Task ExecuteReader( string sql, object? param = null, CancellationToken cancellationToken = default ) => await _connection.ExecuteReaderAsync(new CommandDefinition(sql, param, cancellationToken: cancellationToken)); /// public async Task> CreateTransaction(CancellationToken cancellationToken) { if (!_connection.State.HasFlag(ConnectionState.Open)) await _connection.OpenAsync(cancellationToken); var tx = await _connection.BeginTransactionAsync(cancellationToken); return new DbTransaction(tx); } /// public async Task> CreateTransaction(IsolationLevel isolationLevel, CancellationToken cancellationToken) { if (!_connection.State.HasFlag(ConnectionState.Open)) await _connection.OpenAsync(cancellationToken); var tx = await _connection.BeginTransactionAsync(isolationLevel, cancellationToken); return new DbTransaction(tx); } protected virtual void Dispose(bool disposing) { if (disposing) { #pragma warning disable IDISP007 _connection.Dispose(); #pragma warning restore IDISP007 } } /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual async ValueTask DisposeAsyncCore() { #pragma warning disable IDISP007 await _connection.DisposeAsync(); #pragma warning restore IDISP007 } /// public async ValueTask DisposeAsync() { await DisposeAsyncCore(); GC.SuppressFinalize(this); } }