Skip to content

Commit

Permalink
fix: fixing batch query options and unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith committed Oct 20, 2024
1 parent 7dad33e commit 43c255c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/Codibre.GrpcSqlProxy.Client/ISqlProxyBatchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface ISqlProxyBatchQuery
IResultHook<T> QueryFirstHook<T>(FormattableString builtScript, object token) where T : class, new();
IResultHook<T?> QueryFirstOrDefaultHook<T>(FormattableString builtScript) where T : class, new();
IResultHook<T?> QueryFirstOrDefaultHook<T>(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<T>(object token);
Expand All @@ -56,13 +56,13 @@ public interface ISqlProxyBatchQuery
IAsyncEnumerable<KeyValuePair<TInput, TOutput>> PrepareEnumerable<TInput, TOutput>(
IEnumerable<TInput> enumerable,
Func<TInput, ISqlProxyBatchQuery, ValueTask<TOutput>> PreRunQuery,
SqlProxyQueryOptions? options = null,
SqlProxyBatchQueryOptions? options = null,
int paramMargin = 100
);
IAsyncEnumerable<KeyValuePair<TInput, TOutput>> PrepareEnumerable<TInput, TOutput>(
IEnumerable<TInput> enumerable,
Func<TInput, ISqlProxyBatchQuery, TOutput> PreRunQuery,
SqlProxyQueryOptions? options = null,
SqlProxyBatchQueryOptions? options = null,
int paramMargin = 100
);

Expand Down
14 changes: 0 additions & 14 deletions src/Codibre.GrpcSqlProxy.Client/Impl/ResultHook.cs

This file was deleted.

14 changes: 7 additions & 7 deletions src/Codibre.GrpcSqlProxy.Client/Impl/SqlProxyBatchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type> _types = [];
private readonly Dictionary<object, object?> _results = [];
private readonly List<Func<Reader, Task>> _hooks = [];
Expand Down Expand Up @@ -74,7 +74,7 @@ public IResultHook<IEnumerable<T>> QueryHook<T>(FormattableString builtScript, o
async (reader) => SetResult(token, await reader.ReadAsync<T>().ToArrayAsync())
);

public async Task RunQueries(SqlProxyQueryOptions? options)
public async Task RunQueries(SqlProxyBatchQueryOptions? options)
{
if (_builder.QueryCount <= 0) return;
var reader = _tunnel.QueryMultipleAsync(
Expand Down Expand Up @@ -138,7 +138,7 @@ private void ClearPendingRun()
private async IAsyncEnumerable<IList<KeyValuePair<TInput, TOutput>>> InternalPrepareEnumerable<TInput, TOutput>(
IEnumerable<TInput> enumerable,
Func<TInput, ISqlProxyBatchQuery, ValueTask<TOutput>> PreRunQuery,
SqlProxyQueryOptions? options,
SqlProxyBatchQueryOptions? options,
int paramMargin = 100
)
{
Expand Down Expand Up @@ -169,14 +169,14 @@ private async IAsyncEnumerable<IList<KeyValuePair<TInput, TOutput>>> InternalPre
public IAsyncEnumerable<KeyValuePair<TInput, TOutput>> PrepareEnumerable<TInput, TOutput>(
IEnumerable<TInput> enumerable,
Func<TInput, ISqlProxyBatchQuery, ValueTask<TOutput>> PreRunQuery,
SqlProxyQueryOptions? options = null,
SqlProxyBatchQueryOptions? options = null,
int paramMargin = 100
) => InternalPrepareEnumerable(enumerable, PreRunQuery, options, paramMargin)
.SelectMany(x => x.ToAsyncEnumerable());
public IAsyncEnumerable<KeyValuePair<TInput, TOutput>> PrepareEnumerable<TInput, TOutput>(
IEnumerable<TInput> enumerable,
Func<TInput, ISqlProxyBatchQuery, TOutput> PreRunQuery,
SqlProxyQueryOptions? options = null,
SqlProxyBatchQueryOptions? options = null,
int paramMargin = 100
) => PrepareEnumerable(
enumerable,
Expand Down Expand Up @@ -329,14 +329,14 @@ public static IAsyncEnumerable<KeyValuePair<TInput, TOutput>> PrepareQueryBatch<
this IEnumerable<TInput> enumerable,
ISqlProxyBatchQuery batchQuery,
Func<TInput, ISqlProxyBatchQuery, ValueTask<TOutput>> PreRunQuery,
SqlProxyQueryOptions? options = null,
SqlProxyBatchQueryOptions? options = null,
int paramMargin = 100
) => batchQuery.PrepareEnumerable(enumerable, PreRunQuery, options, paramMargin);
public static IAsyncEnumerable<KeyValuePair<TInput, TOutput>> PrepareQueryBatch<TInput, TOutput>(
this IEnumerable<TInput> enumerable,
ISqlProxyBatchQuery batchQuery,
Func<TInput, ISqlProxyBatchQuery, TOutput> PreRunQuery,
SqlProxyQueryOptions? options = null,
SqlProxyBatchQueryOptions? options = null,
int paramMargin = 100
) => batchQuery.PrepareEnumerable(
enumerable,
Expand Down
31 changes: 31 additions & 0 deletions test/Codibre.GrpcSqlProxy.Test/GrpcSqlProxyClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,6 +42,35 @@ public async Task Should_Keep_Transaction_Opened()
result2.Should().BeEquivalentTo(Array.Empty<TB_PEDIDO>());
}

[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<ISqlProxyClient>();

// 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<TB_PEDIDO>("SELECT * FROM TB_PEDIDO");
await channel.Rollback();
var result2 = await channel.Query<TB_PEDIDO>("SELECT * FROM TB_PEDIDO").ToArrayAsync();

// Assert
result1.Should().BeOfType<TB_PEDIDO>();
result2.Should().BeOfType<TB_PEDIDO[]>();
result1.Should().BeEquivalentTo(new TB_PEDIDO { CD_PEDIDO = 1 });
result2.Should().BeEquivalentTo(Array.Empty<TB_PEDIDO>());
}

[Fact]
public async Task Should_Use_Compression()
{
Expand Down

0 comments on commit 43c255c

Please sign in to comment.