Skip to content

Commit b13dcae

Browse files
updated code with a simpler and cleaner one
1 parent a89ff0f commit b13dcae

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

SqlKata.Execution/QueryFactory.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,23 @@ public Query FromQuery(Query query)
7272
return xQuery;
7373
}
7474

75-
public IEnumerable<T> GetUnbuffered<T>( Query query, IDbTransaction transaction = null, int? timeout = null )
75+
/// <summary>
76+
/// <para>Executes an unbuffered <see cref="Query"/> without reading all results to memory at the same time.
77+
/// (Does not call <see cref="Enumerable.ToList{TSource}(IEnumerable{TSource})"/> or similar on the result.)</para>
78+
/// <para>The execution of a <see cref="Query"/> with <see cref="Query.Includes"/> is NOT supported this way.</para>
79+
/// <para>IMPORTANT: The returned <see cref="IEnumerable{T}"/> *MUST* be fully iterated over by the caller to free underlying allotted resources.
80+
/// If this does not occur, subsequent queries will fail (especially if the underlying DB does not support multiple result sets).</para>
81+
/// </summary>
82+
/// <exception cref="InvalidOperationException">if <paramref name="query"/> has <see cref="Query.Includes"/></exception>
83+
public IEnumerable<T> GetUnbuffered<T>(Query query, IDbTransaction transaction = null, int? timeout = null)
7684
{
77-
var compiled = CompileAndLog( query );
85+
var compiled = CompileAndLog(query);
7886

87+
if(query.Includes.Count != 0)
88+
{
89+
throw new InvalidOperationException($"{nameof( GetUnbuffered )} does not support {nameof( Query )} execution with {nameof( query.Includes )}.");
90+
}
91+
7992
var result = this.Connection.Query<T>(
8093
compiled.Sql,
8194
compiled.NamedBindings,
@@ -84,8 +97,6 @@ public IEnumerable<T> GetUnbuffered<T>( Query query, IDbTransaction transaction
8497
commandTimeout: timeout ?? this.QueryTimeout
8598
);
8699

87-
result = handleIncludes<T>( query, result );
88-
89100
return result;
90101
}
91102

@@ -688,7 +699,8 @@ private static IEnumerable<T> handleIncludes<T>(Query query, IEnumerable<T> resu
688699

689700
var dynamicResult = result
690701
.Cast<IDictionary<string, object>>()
691-
.Select(x => new Dictionary<string, object>(x, StringComparer.OrdinalIgnoreCase));
702+
.Select(x => new Dictionary<string, object>(x, StringComparer.OrdinalIgnoreCase))
703+
.ToList();
692704

693705
foreach (var include in query.Includes)
694706
{
@@ -713,9 +725,11 @@ private static IEnumerable<T> handleIncludes<T>(Query query, IEnumerable<T> resu
713725
include.ForeignKey = table.Singularize(false) + "Id";
714726
}
715727

716-
var localIds = dynamicResult.Where( x => x[include.LocalKey] != null ).Select( x => x[include.LocalKey].ToString() );
728+
var localIds = dynamicResult.Where(x => x[include.LocalKey] != null)
729+
.Select(x => x[include.LocalKey].ToString())
730+
.ToList();
717731

718-
if (!localIds.Any())
732+
if(!localIds.Any())
719733
{
720734
continue;
721735
}

0 commit comments

Comments
 (0)