-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding options to create an AsyncDbSession with retry support for high latency connection usage
- Loading branch information
1 parent
f01fc4b
commit a0dce6d
Showing
6 changed files
with
137 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.Data.SqlClient; | ||
|
||
namespace Codibre.MSSqlSession.Impl; | ||
|
||
public class AsyncSqlOptions | ||
{ | ||
public string ConnectionString { get; set; } | ||
Check warning on line 7 in src/Codibre.MSSqlSession/Impl/AsyncSqlOptions.cs GitHub Actions / build
Check warning on line 7 in src/Codibre.MSSqlSession/Impl/AsyncSqlOptions.cs GitHub Actions / build
Check warning on line 7 in src/Codibre.MSSqlSession/Impl/AsyncSqlOptions.cs GitHub Actions / build
Check warning on line 7 in src/Codibre.MSSqlSession/Impl/AsyncSqlOptions.cs GitHub Actions / build
Check warning on line 7 in src/Codibre.MSSqlSession/Impl/AsyncSqlOptions.cs GitHub Actions / test
|
||
public bool CustomPool { get; set; } | ||
public SqlRetryLogicBaseProvider? RetryLogicBaseProvider { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Codibre.MSSqlSession.Extensions; | ||
using Codibre.MSSqlSession.Impl.Utils; | ||
|
||
namespace Codibre.MSSqlSession.Impl; | ||
|
||
internal partial class BatchQuery : IBatchQuery | ||
{ | ||
private readonly IAsyncDbSession _session; | ||
private readonly IScriptBuilder _builder; | ||
private readonly HookStorage _hookStorage; | ||
internal BatchQuery(IAsyncDbSession session) | ||
{ | ||
_session = session; | ||
_builder = new ScriptBuilder(session.Connection); | ||
_hookStorage = new( | ||
new(), | ||
_builder | ||
); | ||
} | ||
|
||
public string Sql => _builder.Sql; | ||
public int QueryCount => _builder.QueryCount; | ||
public int ParamCount => _builder.ParamCount; | ||
|
||
public void AddNoResultScript(FormattableString builtScript) => _builder.Add(builtScript); | ||
|
||
public IResultHook<T> QueryFirstHook<T>(FormattableString builtScript) | ||
{ | ||
var token = new object(); | ||
return _hookStorage.AddRequiredHook<T>(builtScript, token, | ||
async (reader, setResult) => setResult(token, await reader.ReadFirstAsync<T>()) | ||
); | ||
} | ||
public IResultHook<T?> QueryFirstOrDefaultHook<T>(FormattableString builtScript) | ||
{ | ||
var token = new object(); | ||
return _hookStorage.AddNullableHook<T?>(builtScript, token, | ||
async (reader, setResult) => setResult(token, await reader.ReadFirstOrDefaultAsync<T>()) | ||
); | ||
} | ||
|
||
public IResultHook<IEnumerable<T>> QueryHook<T>(FormattableString builtScript) | ||
{ | ||
var token = new object(); | ||
return _hookStorage.AddRequiredHook<IEnumerable<T>>(builtScript, token, | ||
async (reader, setResult) => setResult(token, await reader.ReadAsync<T>()) | ||
); | ||
} | ||
|
||
public Task RunQueries(TimeSpan? customTimeout = null) | ||
=> _hookStorage.RunQueries(customTimeout); | ||
|
||
public Task Execute(TimeSpan? customTimeout = null) | ||
=> _hookStorage.Execute(customTimeout); | ||
|
||
public void Clear() => _hookStorage.Clear(); | ||
|
||
private IAsyncEnumerable<IList<KeyValuePair<TInput, TOutput>>> InternalPrepareEnumerable<TInput, TOutput>( | ||
IEnumerable<TInput> enumerable, | ||
Func<TInput, IBatchQuery, ValueTask<TOutput>> PreRunQuery | ||
) => _hookStorage | ||
.PrepareEnumerable(enumerable, (current) => PreRunQuery(current, this)) | ||
.OnStartAsync(async () => _session.ConnectionAcquired ? null : await _session.StartSession()); | ||
|
||
public IAsyncEnumerable<KeyValuePair<TInput, TOutput>> PrepareEnumerable<TInput, TOutput>( | ||
IEnumerable<TInput> enumerable, | ||
Func<TInput, IBatchQuery, ValueTask<TOutput>> PreRunQuery | ||
) => InternalPrepareEnumerable(enumerable, PreRunQuery) | ||
.SelectMany(x => x.ToAsyncEnumerable()); | ||
} |
68 changes: 2 additions & 66 deletions
68
src/Codibre.MSSqlSession/Impl/BatchQuery.cs → ...SqlSession/Impl/BatchQuery.Transaction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.