Skip to content

Commit

Permalink
Mark AdvancedSqlQuery* methods as obsolete
Browse files Browse the repository at this point in the history
Moved to IAdvancedSql interface
  • Loading branch information
e-tobi authored and jeremydmiller committed May 28, 2024
1 parent 4ea682d commit 8b79c2a
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 97 deletions.
18 changes: 9 additions & 9 deletions src/DocumentDbTests/Reading/advanced_sql_query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async void can_query_scalar()
await session.SaveChangesAsync();
#region sample_advanced_sql_query_single_scalar
var schema = session.DocumentStore.Options.Schema;
var name = (await session.AdvancedSqlQueryAsync<string>(
var name = (await session.AdvancedSql.QueryAsync<string>(
$"select data ->> 'Name' from {schema.For<DocWithMeta>()} limit 1",
CancellationToken.None)).First();
#endregion
Expand All @@ -36,7 +36,7 @@ public async void can_query_multiple_scalars()
{
await using var session = theStore.LightweightSession();
#region sample_advanced_sql_query_multiple_scalars
var (number,text, boolean) = (await session.AdvancedSqlQueryAsync<int, string, bool>(
var (number,text, boolean) = (await session.AdvancedSql.QueryAsync<int, string, bool>(
"select row(5), row('foo'), row(true) from (values(1)) as dummy",
CancellationToken.None)).First();
#endregion
Expand All @@ -50,7 +50,7 @@ public async void can_query_non_document_classes_from_json()
{
await using var session = theStore.LightweightSession();
#region sample_advanced_sql_query_json_object
var result = (await session.AdvancedSqlQueryAsync<Foo, Bar>(
var result = (await session.AdvancedSql.QueryAsync<Foo, Bar>(
"select row(json_build_object('Name', 'foo')), row(json_build_object('Name', 'bar')) from (values(1)) as dummy",
CancellationToken.None)).First();
#endregion
Expand All @@ -67,7 +67,7 @@ public async void can_query_documents()
await session.SaveChangesAsync();
#region sample_advanced_sql_query_documents
var schema = session.DocumentStore.Options.Schema;
var docs = await session.AdvancedSqlQueryAsync<DocWithoutMeta>(
var docs = await session.AdvancedSql.QueryAsync<DocWithoutMeta>(
$"select id, data from {schema.For<DocWithoutMeta>()} order by data ->> 'Name'",
CancellationToken.None);
#endregion
Expand All @@ -84,7 +84,7 @@ public async void can_query_documents_and_will_set_metadata_on_result_documents(
await session.SaveChangesAsync();
#region sample_advanced_sql_query_documents_with_metadata
var schema = session.DocumentStore.Options.Schema;
var doc = (await session.AdvancedSqlQueryAsync<DocWithMeta>(
var doc = (await session.AdvancedSql.QueryAsync<DocWithMeta>(
$"select id, data, mt_version from {schema.For<DocWithMeta>()} where data ->> 'Name' = 'Max'",
CancellationToken.None)).First();
#endregion
Expand All @@ -110,7 +110,7 @@ public async void can_query_multiple_documents_and_scalar()

var schema = session.DocumentStore.Options.Schema;
IReadOnlyList<(DocWithMeta doc, DocDetailsWithMeta detail, long totalResults)> results =
await session.AdvancedSqlQueryAsync<DocWithMeta, DocDetailsWithMeta, long>(
await session.AdvancedSql.QueryAsync<DocWithMeta, DocDetailsWithMeta, long>(
$"""
select
row(a.id, a.data, a.mt_version),
Expand Down Expand Up @@ -194,10 +194,10 @@ public void can_query_synchrounously()
{
using var session = theStore.LightweightSession();

var singleResult = session.AdvancedSqlQuery<int>("select 5 from (values(1)) as dummy").First();
var tuple2Result = session.AdvancedSqlQuery<int, string>(
var singleResult = session.AdvancedSql.Query<int>("select 5 from (values(1)) as dummy").First();
var tuple2Result = session.AdvancedSql.Query<int, string>(
"select row(5), row('foo')from (values(1)) as dummy").First();
var tuple3Result = session.AdvancedSqlQuery<int, string, bool>(
var tuple3Result = session.AdvancedSql.Query<int, string, bool>(
"select row(5), row('foo'), row(true) from (values(1)) as dummy").First();

singleResult.ShouldBe(5);
Expand Down
89 changes: 89 additions & 0 deletions src/Marten/IAdvancedSql.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,100 @@
#nullable enable
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Marten;

public interface IAdvancedSql
{
/// <summary>
/// Asynchronously queries the document storage with the supplied SQL.
/// The type parameter can be a document class, a scalar or any JSON-serializable class.
/// If the result is a document, the SQL must contain a select with 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
/// selected.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
Task<IReadOnlyList<T>> QueryAsync<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></returns>
Task<IReadOnlyList<(T1, T2)>> QueryAsync<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></returns>
Task<IReadOnlyList<(T1, T2, T3)>> QueryAsync<T1, T2, T3>(string sql, CancellationToken token, params object[] parameters);

/// <summary>
/// Asynchronously queries the document storage with the supplied SQL.
/// The type parameter can be a document class, a scalar or any JSON-serializable class.
/// If the result is a document, the SQL must contain a select with 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
/// selected.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
IReadOnlyList<T> Query<T>(string sql, 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></returns>
IReadOnlyList<(T1, T2)> Query<T1, T2>(string sql, 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></returns>
IReadOnlyList<(T1, T2, T3)> Query<T1, T2, T3>(string sql, 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.
Expand Down
7 changes: 6 additions & 1 deletion src/Marten/IQuerySession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ public interface IQuerySession: IDisposable, IAsyncDisposable
/// <returns></returns>
Task<IReadOnlyList<T>> QueryAsync<T>(string sql, params object[] parameters);


/// <summary>
/// Asynchronously queries the document storage with the supplied SQL.
/// The type parameter can be a document class, a scalar or any JSON-serializable class.
Expand All @@ -215,6 +214,7 @@ public interface IQuerySession: IDisposable, IAsyncDisposable
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
[Obsolete("Will be removed in 8.0. Use AdvancedSql.QueryAsync<T>(...) instead.")]
Task<IReadOnlyList<T>> AdvancedSqlQueryAsync<T>(string sql, CancellationToken token, params object[] parameters);

/// <summary>
Expand All @@ -230,6 +230,7 @@ public interface IQuerySession: IDisposable, IAsyncDisposable
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
[Obsolete("Will be removed in 8.0. Use AdvancedSql.QueryAsync<T1, T2>(...) instead.")]
Task<IReadOnlyList<(T1, T2)>> AdvancedSqlQueryAsync<T1, T2>(string sql, CancellationToken token, params object[] parameters);

/// <summary>
Expand All @@ -246,6 +247,7 @@ public interface IQuerySession: IDisposable, IAsyncDisposable
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
[Obsolete("Will be removed in 8.0. Use AdvancedSql.QueryAsync<T1, T2, T3>(...) instead.")]
Task<IReadOnlyList<(T1, T2, T3)>> AdvancedSqlQueryAsync<T1, T2,T3>(string sql, CancellationToken token, params object[] parameters);

/// <summary>
Expand All @@ -259,6 +261,7 @@ public interface IQuerySession: IDisposable, IAsyncDisposable
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
[Obsolete("Will be removed in 8.0. Use AdvancedSql.QueryAsync<T>(...) instead.")]
IReadOnlyList<T> AdvancedSqlQuery<T>(string sql, params object[] parameters);

/// <summary>
Expand All @@ -274,6 +277,7 @@ public interface IQuerySession: IDisposable, IAsyncDisposable
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
[Obsolete("Will be removed in 8.0. Use AdvancedSql.QueryAsync<T1, T2>(...) instead.")]
IReadOnlyList<(T1, T2)> AdvancedSqlQuery<T1, T2>(string sql, params object[] parameters);

/// <summary>
Expand All @@ -290,6 +294,7 @@ public interface IQuerySession: IDisposable, IAsyncDisposable
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
[Obsolete("Will be removed in 8.0. Use AdvancedSql.QueryAsync<T1, T2, T3>(...) instead.")]
IReadOnlyList<(T1, T2, T3)> AdvancedSqlQuery<T1, T2, T3>(string sql, params object[] parameters);

/// <summary>
Expand Down
98 changes: 95 additions & 3 deletions src/Marten/Internal/Sessions/QuerySession.AdvancedSql.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,106 @@
using Marten.Linq;
using Marten.Linq.QueryHandlers;
using Marten.Util;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace Marten.Internal.Sessions;

public partial class QuerySession: IAdvancedSql
{
public async IAsyncEnumerable<T> StreamAsync<T>(string sql, [EnumeratorCancellation] CancellationToken token,
async Task<IReadOnlyList<T>> IAdvancedSql.QueryAsync<T>(string sql, 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 provider = new MartenLinqQueryProvider(this, typeof(T));
return await provider.ExecuteHandlerAsync(handler, token).ConfigureAwait(false);
}

async Task<IReadOnlyList<(T1, T2)>> IAdvancedSql.QueryAsync<T1, T2>(string sql, 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 provider = new MartenLinqQueryProvider(this, typeof((T1, T2)));
return await provider.ExecuteHandlerAsync(handler, token).ConfigureAwait(false);
}

async Task<IReadOnlyList<(T1, T2, T3)>> IAdvancedSql.QueryAsync<T1, T2, T3>(string sql, 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 provider = new MartenLinqQueryProvider(this, typeof((T1, T2, T3)));
return await provider.ExecuteHandlerAsync(handler, token).ConfigureAwait(false);
}

IReadOnlyList<T> IAdvancedSql.Query<T>(string sql, params object[] parameters)
{
assertNotDisposed();

var handler = new AdvancedSqlQueryHandler<T>(this, sql, parameters);

foreach (var documentType in handler.DocumentTypes)
{
Database.EnsureStorageExists(documentType);
}

var provider = new MartenLinqQueryProvider(this, typeof(T));
return provider.ExecuteHandler(handler);
}

IReadOnlyList<(T1, T2)> IAdvancedSql.Query<T1, T2>(string sql, params object[] parameters)
{
assertNotDisposed();

var handler = new AdvancedSqlQueryHandler<T1, T2>(this, sql, parameters);

foreach (var documentType in handler.DocumentTypes)
{
Database.EnsureStorageExists(documentType);
}

var provider = new MartenLinqQueryProvider(this, typeof((T1, T2)));
return provider.ExecuteHandler(handler);
}

IReadOnlyList<(T1, T2, T3)> IAdvancedSql.Query<T1, T2, T3>(string sql, params object[] parameters)
{
assertNotDisposed();

var handler = new AdvancedSqlQueryHandler<T1, T2, T3>(this, sql, parameters);

foreach (var documentType in handler.DocumentTypes)
{
Database.EnsureStorageExists(documentType);
}

var provider = new MartenLinqQueryProvider(this, typeof((T1, T2, T3)));
return provider.ExecuteHandler(handler);
}

async IAsyncEnumerable<T> IAdvancedSql.StreamAsync<T>(string sql, [EnumeratorCancellation] CancellationToken token,
params object[] parameters)
{
assertNotDisposed();
Expand All @@ -29,7 +121,7 @@ public async IAsyncEnumerable<T> StreamAsync<T>(string sql, [EnumeratorCancellat
}
}

public async IAsyncEnumerable<(T1, T2)> StreamAsync<T1, T2>(string sql, [EnumeratorCancellation] CancellationToken token,
async IAsyncEnumerable<(T1, T2)> IAdvancedSql.StreamAsync<T1, T2>(string sql, [EnumeratorCancellation] CancellationToken token,
params object[] parameters)
{
assertNotDisposed();
Expand All @@ -50,7 +142,7 @@ public async IAsyncEnumerable<T> StreamAsync<T>(string sql, [EnumeratorCancellat
}
}

public async IAsyncEnumerable<(T1, T2, T3)> StreamAsync<T1, T2, T3>(string sql, [EnumeratorCancellation] CancellationToken token,
async IAsyncEnumerable<(T1, T2, T3)> IAdvancedSql.StreamAsync<T1, T2, T3>(string sql, [EnumeratorCancellation] CancellationToken token,
params object[] parameters)
{
assertNotDisposed();
Expand Down
Loading

0 comments on commit 8b79c2a

Please sign in to comment.