-
-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add session.AdvancedSql.StreamAsync<>()
- Loading branch information
1 parent
24db609
commit 4ea682d
Showing
6 changed files
with
214 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#nullable enable | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
namespace Marten; | ||
|
||
public interface IAdvancedSql | ||
{ | ||
/// <summary> | ||
/// Asynchronously queries the document storage with the supplied SQL. | ||
/// The type parameters can be any document class, scalar or JSON-serializable class. | ||
/// For each result type parameter, the SQL SELECT statement must contain a ROW. | ||
/// For document types, the row must contain the required fields in the correct order, | ||
/// depending on the session type and the metadata the document might use, at least id and data must be | ||
/// provided. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="sql"></param> | ||
/// <param name="parameters"></param> | ||
/// <returns>An async enumerable iterating over the results</returns> | ||
IAsyncEnumerable<T> StreamAsync<T>(string sql, CancellationToken token, params object[] parameters); | ||
|
||
/// <summary> | ||
/// Asynchronously queries the document storage with the supplied SQL. | ||
/// The type parameters can be any document class, scalar or JSON-serializable class. | ||
/// For each result type parameter, the SQL SELECT statement must contain a ROW. | ||
/// For document types, the row must contain the required fields in the correct order, | ||
/// depending on the session type and the metadata the document might use, at least id and data must be | ||
/// provided. | ||
/// </summary> | ||
/// <typeparam name="T1"></typeparam> | ||
/// <typeparam name="T2"></typeparam> | ||
/// <param name="sql"></param> | ||
/// <param name="parameters"></param> | ||
/// <returns>An async enumerable iterating over the list of result tuples</returns> | ||
IAsyncEnumerable<(T1, T2)> StreamAsync<T1, T2>(string sql, CancellationToken token, params object[] parameters); | ||
|
||
/// <summary> | ||
/// Asynchronously queries the document storage with the supplied SQL. | ||
/// The type parameters can be any document class, scalar or JSON-serializable class. | ||
/// For each result type parameter, the SQL SELECT statement must contain a ROW. | ||
/// For document types, the row must contain the required fields in the correct order, | ||
/// depending on the session type and the metadata the document might use, at least id and data must be | ||
/// provided. | ||
/// </summary> | ||
/// <typeparam name="T1"></typeparam> | ||
/// <typeparam name="T2"></typeparam> | ||
/// <typeparam name="T3"></typeparam> | ||
/// <param name="sql"></param> | ||
/// <param name="parameters"></param> | ||
/// <returns>An async enumerable iterating over the list of result tuples</returns> | ||
IAsyncEnumerable<(T1, T2, T3)> StreamAsync<T1, T2, T3>(string sql, CancellationToken token, params object[] parameters); | ||
} |
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,73 @@ | ||
using Marten.Linq.QueryHandlers; | ||
using Marten.Util; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
|
||
namespace Marten.Internal.Sessions; | ||
|
||
public partial class QuerySession: IAdvancedSql | ||
{ | ||
public async IAsyncEnumerable<T> StreamAsync<T>(string sql, [EnumeratorCancellation] CancellationToken token, | ||
params object[] parameters) | ||
{ | ||
assertNotDisposed(); | ||
|
||
var handler = new AdvancedSqlQueryHandler<T>(this, sql, parameters); | ||
|
||
foreach (var documentType in handler.DocumentTypes) | ||
{ | ||
await Database.EnsureStorageExistsAsync(documentType, token).ConfigureAwait(false); | ||
} | ||
|
||
var batch = this.BuildCommand(handler); | ||
await using var reader = await ExecuteReaderAsync(batch, token).ConfigureAwait(false); | ||
|
||
await foreach (var result in handler.EnumerateResults(reader, token)) | ||
{ | ||
yield return result; | ||
} | ||
} | ||
|
||
public async IAsyncEnumerable<(T1, T2)> StreamAsync<T1, T2>(string sql, [EnumeratorCancellation] CancellationToken token, | ||
params object[] parameters) | ||
{ | ||
assertNotDisposed(); | ||
|
||
var handler = new AdvancedSqlQueryHandler<T1, T2>(this, sql, parameters); | ||
|
||
foreach (var documentType in handler.DocumentTypes) | ||
{ | ||
await Database.EnsureStorageExistsAsync(documentType, token).ConfigureAwait(false); | ||
} | ||
|
||
var batch = this.BuildCommand(handler); | ||
await using var reader = await ExecuteReaderAsync(batch, token).ConfigureAwait(false); | ||
|
||
await foreach (var result in handler.EnumerateResults(reader, token)) | ||
{ | ||
yield return result; | ||
} | ||
} | ||
|
||
public async IAsyncEnumerable<(T1, T2, T3)> StreamAsync<T1, T2, T3>(string sql, [EnumeratorCancellation] CancellationToken token, | ||
params object[] parameters) | ||
{ | ||
assertNotDisposed(); | ||
|
||
var handler = new AdvancedSqlQueryHandler<T1, T2, T3>(this, sql, parameters); | ||
|
||
foreach (var documentType in handler.DocumentTypes) | ||
{ | ||
await Database.EnsureStorageExistsAsync(documentType, token).ConfigureAwait(false); | ||
} | ||
|
||
var batch = this.BuildCommand(handler); | ||
await using var reader = await ExecuteReaderAsync(batch, token).ConfigureAwait(false); | ||
|
||
await foreach (var result in handler.EnumerateResults(reader, token)) | ||
{ | ||
yield return result; | ||
} | ||
} | ||
} |
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