diff --git a/SqlKata.Execution/QueryFactory.cs b/SqlKata.Execution/QueryFactory.cs index 5ff399aa..0073d8a7 100644 --- a/SqlKata.Execution/QueryFactory.cs +++ b/SqlKata.Execution/QueryFactory.cs @@ -72,6 +72,33 @@ public Query FromQuery(Query query) return xQuery; } + /// + /// Executes an unbuffered without reading all results to memory at the same time. + /// The execution of a with is *NOT* supported this way. + /// IMPORTANT: The returned *MUST* be fully iterated over by the caller to free underlying allotted resources. + /// If this does not occur, subsequent queries will fail (especially if the underlying DB does not support multiple result sets). + /// + /// if has + public IEnumerable GetUnbuffered(Query query, IDbTransaction transaction = null, int? timeout = null) + { + var compiled = CompileAndLog(query); + + if(query.Includes.Count != 0) + { + throw new InvalidOperationException($"{nameof( GetUnbuffered )} does not support {nameof( Query )} execution with {nameof( query.Includes )}."); + } + + var result = this.Connection.Query( + compiled.Sql, + compiled.NamedBindings, + transaction: transaction, + buffered: false, + commandTimeout: timeout ?? this.QueryTimeout + ); + + return result; + } + public IEnumerable Get(Query query, IDbTransaction transaction = null, int? timeout = null) { var compiled = CompileAndLog(query);