From 43c255ca5bbf12af40d2c5d899b7c2f2c08e2015 Mon Sep 17 00:00:00 2001 From: Thiago Oliveira Santos Date: Sun, 20 Oct 2024 11:42:04 -0300 Subject: [PATCH] fix: fixing batch query options and unused code --- .../ISqlProxyBatchQuery.cs | 6 ++-- .../Impl/ResultHook.cs | 14 --------- .../Impl/SqlProxyBatchQuery.cs | 14 ++++----- .../GrpcSqlProxyClientTest.cs | 31 +++++++++++++++++++ 4 files changed, 41 insertions(+), 24 deletions(-) delete mode 100644 src/Codibre.GrpcSqlProxy.Client/Impl/ResultHook.cs diff --git a/src/Codibre.GrpcSqlProxy.Client/ISqlProxyBatchQuery.cs b/src/Codibre.GrpcSqlProxy.Client/ISqlProxyBatchQuery.cs index 04a0b55..bb99c57 100644 --- a/src/Codibre.GrpcSqlProxy.Client/ISqlProxyBatchQuery.cs +++ b/src/Codibre.GrpcSqlProxy.Client/ISqlProxyBatchQuery.cs @@ -42,7 +42,7 @@ public interface ISqlProxyBatchQuery IResultHook QueryFirstHook(FormattableString builtScript, object token) where T : class, new(); IResultHook QueryFirstOrDefaultHook(FormattableString builtScript) where T : class, new(); IResultHook QueryFirstOrDefaultHook(FormattableString builtScript, object token) where T : class, new(); - Task RunQueries(SqlProxyQueryOptions? options = null); + Task RunQueries(SqlProxyBatchQueryOptions? options = null); Task Execute(TimeSpan? customTimeout = null); T Get(object token); @@ -56,13 +56,13 @@ public interface ISqlProxyBatchQuery IAsyncEnumerable> PrepareEnumerable( IEnumerable enumerable, Func> PreRunQuery, - SqlProxyQueryOptions? options = null, + SqlProxyBatchQueryOptions? options = null, int paramMargin = 100 ); IAsyncEnumerable> PrepareEnumerable( IEnumerable enumerable, Func PreRunQuery, - SqlProxyQueryOptions? options = null, + SqlProxyBatchQueryOptions? options = null, int paramMargin = 100 ); diff --git a/src/Codibre.GrpcSqlProxy.Client/Impl/ResultHook.cs b/src/Codibre.GrpcSqlProxy.Client/Impl/ResultHook.cs deleted file mode 100644 index 7654c95..0000000 --- a/src/Codibre.GrpcSqlProxy.Client/Impl/ResultHook.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Codibre.GrpcSqlProxy.Client.Impl; - -internal class ResultHook : IResultHook -{ - private readonly Func _result; - public T Result => _result(); - - internal ResultHook(Func result) => _result = result; -} \ No newline at end of file diff --git a/src/Codibre.GrpcSqlProxy.Client/Impl/SqlProxyBatchQuery.cs b/src/Codibre.GrpcSqlProxy.Client/Impl/SqlProxyBatchQuery.cs index 3e9ad59..0d23f88 100644 --- a/src/Codibre.GrpcSqlProxy.Client/Impl/SqlProxyBatchQuery.cs +++ b/src/Codibre.GrpcSqlProxy.Client/Impl/SqlProxyBatchQuery.cs @@ -12,7 +12,7 @@ internal sealed class SqlProxyBatchQuery(ISqlProxyClientTunnel _tunnel) : ISqlPr private static readonly FormattableString _beginTran = $"BEGIN TRAN;"; private static readonly object _waiting = new(); private readonly ScriptBuilder _builder = new(); - private readonly SqlProxyQueryOptions? _options = null; + private readonly SqlProxyBatchQueryOptions? _options = null; private readonly List _types = []; private readonly Dictionary _results = []; private readonly List> _hooks = []; @@ -74,7 +74,7 @@ public IResultHook> QueryHook(FormattableString builtScript, o async (reader) => SetResult(token, await reader.ReadAsync().ToArrayAsync()) ); - public async Task RunQueries(SqlProxyQueryOptions? options) + public async Task RunQueries(SqlProxyBatchQueryOptions? options) { if (_builder.QueryCount <= 0) return; var reader = _tunnel.QueryMultipleAsync( @@ -138,7 +138,7 @@ private void ClearPendingRun() private async IAsyncEnumerable>> InternalPrepareEnumerable( IEnumerable enumerable, Func> PreRunQuery, - SqlProxyQueryOptions? options, + SqlProxyBatchQueryOptions? options, int paramMargin = 100 ) { @@ -169,14 +169,14 @@ private async IAsyncEnumerable>> InternalPre public IAsyncEnumerable> PrepareEnumerable( IEnumerable enumerable, Func> PreRunQuery, - SqlProxyQueryOptions? options = null, + SqlProxyBatchQueryOptions? options = null, int paramMargin = 100 ) => InternalPrepareEnumerable(enumerable, PreRunQuery, options, paramMargin) .SelectMany(x => x.ToAsyncEnumerable()); public IAsyncEnumerable> PrepareEnumerable( IEnumerable enumerable, Func PreRunQuery, - SqlProxyQueryOptions? options = null, + SqlProxyBatchQueryOptions? options = null, int paramMargin = 100 ) => PrepareEnumerable( enumerable, @@ -329,14 +329,14 @@ public static IAsyncEnumerable> PrepareQueryBatch< this IEnumerable enumerable, ISqlProxyBatchQuery batchQuery, Func> PreRunQuery, - SqlProxyQueryOptions? options = null, + SqlProxyBatchQueryOptions? options = null, int paramMargin = 100 ) => batchQuery.PrepareEnumerable(enumerable, PreRunQuery, options, paramMargin); public static IAsyncEnumerable> PrepareQueryBatch( this IEnumerable enumerable, ISqlProxyBatchQuery batchQuery, Func PreRunQuery, - SqlProxyQueryOptions? options = null, + SqlProxyBatchQueryOptions? options = null, int paramMargin = 100 ) => batchQuery.PrepareEnumerable( enumerable, diff --git a/test/Codibre.GrpcSqlProxy.Test/GrpcSqlProxyClientTest.cs b/test/Codibre.GrpcSqlProxy.Test/GrpcSqlProxyClientTest.cs index f2b8eef..76fef82 100644 --- a/test/Codibre.GrpcSqlProxy.Test/GrpcSqlProxyClientTest.cs +++ b/test/Codibre.GrpcSqlProxy.Test/GrpcSqlProxyClientTest.cs @@ -3,6 +3,8 @@ using Codibre.GrpcSqlProxy.Client.Impl; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace Codibre.GrpcSqlProxy.Test; @@ -40,6 +42,35 @@ public async Task Should_Keep_Transaction_Opened() result2.Should().BeEquivalentTo(Array.Empty()); } + [Fact] + public async Task Should_Inject_SqlProxy_Properly() + { + // Arrange + var server = await TestServer.Get(); + var builder = Host.CreateApplicationBuilder([]); + builder.Configuration.GetSection("GrpcSqlProxy").GetSection("Url").Value = server.Url; + builder.Configuration.GetSection("GrpcSqlProxy").GetSection("Compress").Value = "False"; + builder.Configuration.GetSection("GrpcSqlProxy").GetSection("PacketSize").Value = "2000"; + builder.Services.AddGrpcSqlProxy(); + var app = builder.Build(); + var client = app.Services.GetRequiredService(); + + // Act + using var channel = client.CreateChannel(); + await channel.Execute("DELETE FROM TB_PEDIDO"); + await channel.BeginTransaction(); + await channel.Execute("INSERT INTO TB_PEDIDO (CD_PEDIDO) VALUES (1)"); + var result1 = await channel.QueryFirstOrDefault("SELECT * FROM TB_PEDIDO"); + await channel.Rollback(); + var result2 = await channel.Query("SELECT * FROM TB_PEDIDO").ToArrayAsync(); + + // Assert + result1.Should().BeOfType(); + result2.Should().BeOfType(); + result1.Should().BeEquivalentTo(new TB_PEDIDO { CD_PEDIDO = 1 }); + result2.Should().BeEquivalentTo(Array.Empty()); + } + [Fact] public async Task Should_Use_Compression() {