@@ -97,10 +97,16 @@ public override IEnumerable<IDictionary<string, object>> RunQuery(SimpleQuery qu
9797 {
9898 if ( query . Clauses . OfType < WithCountClause > ( ) . Any ( ) ) return RunQueryWithCount ( query , out unhandledClauses ) ;
9999
100+ var commandBuilders = this . GetQueryCommandBuilders ( query , out unhandledClauses ) ;
100101 var connection = _connectionProvider . CreateConnection ( ) ;
101- return new QueryBuilder ( this ) . Build ( query , out unhandledClauses )
102- . GetCommand ( connection )
103- . ToEnumerable ( connection ) ;
102+ if ( ProviderSupportsCompoundStatements || commandBuilders . Length == 1 )
103+ {
104+ return CommandBuilder . CreateCommand ( _providerHelper . GetCustomProvider < IDbParameterFactory > ( _schema . SchemaProvider ) , commandBuilders , connection ) . ToEnumerable ( connection ) ;
105+ }
106+ else
107+ {
108+ return commandBuilders . SelectMany ( cb => cb . GetCommand ( connection ) . ToEnumerable ( connection ) ) ;
109+ }
104110 }
105111
106112 private IEnumerable < IDictionary < string , object > > RunQueryWithCount ( SimpleQuery query , out IEnumerable < SimpleQueryClauseBase > unhandledClauses )
@@ -141,19 +147,83 @@ private IEnumerable<IDictionary<string, object>> RunQueryWithCount(SimpleQuery q
141147 }
142148 }
143149
144- public override IEnumerable < IEnumerable < IDictionary < string , object > > > RunQueries ( SimpleQuery [ ] queries , List < IEnumerable < SimpleQueryClauseBase > > unhandledClauses )
150+ private ICommandBuilder [ ] GetPagedQueryCommandBuilders ( SimpleQuery query , out IEnumerable < SimpleQueryClauseBase > unhandledClauses )
151+ {
152+ return this . GetPagedQueryCommandBuilders ( query , - 1 , out unhandledClauses ) ;
153+ }
154+
155+ private ICommandBuilder [ ] GetPagedQueryCommandBuilders ( SimpleQuery query , Int32 bulkIndex , out IEnumerable < SimpleQueryClauseBase > unhandledClauses )
156+ {
157+ var commandBuilders = new List < ICommandBuilder > ( ) ;
158+ var unhandledClausesList = new List < SimpleQueryClauseBase > ( ) ;
159+ unhandledClauses = unhandledClausesList ;
160+
161+ IEnumerable < SimpleQueryClauseBase > unhandledClausesForPagedQuery ;
162+ var mainCommandBuilder = new QueryBuilder ( this , bulkIndex ) . Build ( query , out unhandledClausesForPagedQuery ) ;
163+ unhandledClausesList . AddRange ( unhandledClausesForPagedQuery ) ;
164+
165+ const int maxInt = 2147483646 ;
166+
167+ var skipClause = query . Clauses . OfType < SkipClause > ( ) . FirstOrDefault ( ) ?? new SkipClause ( 0 ) ;
168+ var takeClause = query . Clauses . OfType < TakeClause > ( ) . FirstOrDefault ( ) ?? new TakeClause ( maxInt ) ;
169+
170+ if ( skipClause . Count != 0 || takeClause . Count != maxInt )
171+ {
172+ var queryPager = this . ProviderHelper . GetCustomProvider < IQueryPager > ( this . ConnectionProvider ) ;
173+ if ( queryPager == null )
174+ {
175+ unhandledClausesList . AddRange ( query . OfType < SkipClause > ( ) ) ;
176+ unhandledClausesList . AddRange ( query . OfType < TakeClause > ( ) ) ;
177+ }
178+
179+ var commandTexts = queryPager . ApplyPaging ( mainCommandBuilder . Text , skipClause . Count , takeClause . Count ) ;
180+
181+ foreach ( var commandText in commandTexts )
182+ {
183+ var commandBuilder = new CommandBuilder ( commandText , this . _schema , mainCommandBuilder . Parameters ) ;
184+ commandBuilders . Add ( commandBuilder ) ;
185+ }
186+ }
187+ return commandBuilders . ToArray ( ) ;
188+ }
189+
190+ private ICommandBuilder [ ] GetQueryCommandBuilders ( SimpleQuery query , out IEnumerable < SimpleQueryClauseBase > unhandledClauses )
191+ {
192+ if ( query . Clauses . OfType < TakeClause > ( ) . Any ( ) || query . Clauses . OfType < SkipClause > ( ) . Any ( ) )
193+ {
194+ return this . GetPagedQueryCommandBuilders ( query , out unhandledClauses ) ;
195+ }
196+ else
197+ {
198+ return new [ ] { new QueryBuilder ( this ) . Build ( query , out unhandledClauses ) } ;
199+ }
200+ }
201+
202+ private ICommandBuilder [ ] GetQueryCommandBuilders ( SimpleQuery query , Int32 bulkIndex , out IEnumerable < SimpleQueryClauseBase > unhandledClauses )
203+ {
204+ if ( query . Clauses . OfType < TakeClause > ( ) . Any ( ) || query . Clauses . OfType < SkipClause > ( ) . Any ( ) )
205+ {
206+ return this . GetPagedQueryCommandBuilders ( query , bulkIndex , out unhandledClauses ) ;
207+ }
208+ else
209+ {
210+ return new [ ] { new QueryBuilder ( this , bulkIndex ) . Build ( query , out unhandledClauses ) } ;
211+ }
212+ }
213+
214+ public override IEnumerable < IEnumerable < IDictionary < string , object > > > RunQueries ( SimpleQuery [ ] queries , List < IEnumerable < SimpleQueryClauseBase > > unhandledClauses )
145215 {
146216 if ( ProviderSupportsCompoundStatements && queries . Length > 1 )
147217 {
148- var commandBuilders = new ICommandBuilder [ queries . Length ] ;
218+ var commandBuilders = new List < ICommandBuilder > ( ) ;
149219 for ( int i = 0 ; i < queries . Length ; i ++ )
150220 {
151221 IEnumerable < SimpleQueryClauseBase > unhandledClausesForThisQuery ;
152- commandBuilders [ i ] = new QueryBuilder ( this , i ) . Build ( queries [ i ] , out unhandledClausesForThisQuery ) ;
222+ commandBuilders . AddRange ( GetQueryCommandBuilders ( queries [ i ] , i , out unhandledClausesForThisQuery ) ) ;
153223 unhandledClauses . Add ( unhandledClausesForThisQuery ) ;
154224 }
155225 var connection = _connectionProvider . CreateConnection ( ) ;
156- var command = CommandBuilder . CreateCommand ( _providerHelper . GetCustomProvider < IDbParameterFactory > ( _schema . SchemaProvider ) , commandBuilders , connection ) ;
226+ var command = CommandBuilder . CreateCommand ( _providerHelper . GetCustomProvider < IDbParameterFactory > ( _schema . SchemaProvider ) , commandBuilders . ToArray ( ) , connection ) ;
157227 foreach ( var item in command . ToEnumerables ( connection ) )
158228 {
159229 yield return item . ToList ( ) ;
0 commit comments