1- namespace Open . Database . Extensions ;
1+ using System . Diagnostics ;
2+
3+ namespace Open . Database . Extensions ;
24
35/// <summary>
46/// Extension methods for Data Readers.
@@ -140,113 +142,131 @@ public static async ValueTask ForEachAsync(
140142 public static ValueTask ForEachAsync ( this DbDataReader reader , Func < IDataRecord , ValueTask > handler , CancellationToken cancellationToken )
141143 => ForEachAsync ( reader , handler , true , cancellationToken ) ;
142144
143- /// <inheritdoc cref="AsEnumerable(IDataReader, ArrayPool{object?}, int, int[])"/>
144- public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader )
145+ static IEnumerable < object [ ] > AsEnumerableCore ( IDataReader reader )
145146 {
146- return reader is null
147- ? throw new ArgumentNullException ( nameof ( reader ) )
148- : AsEnumerableCore ( reader ) ;
147+ Debug . Assert ( reader is not null ) ;
148+
149+ if ( ! reader . Read ( ) )
150+ yield break ;
149151
150- static IEnumerable < object [ ] > AsEnumerableCore ( IDataReader reader )
152+ int fieldCount = reader . FieldCount ;
153+ do
151154 {
152- if ( ! reader . Read ( ) )
153- yield break ;
155+ object [ ] row = new object [ fieldCount ] ;
156+ reader . GetValues ( row ) ;
157+ yield return row ;
158+ } while ( reader . Read ( ) ) ;
159+ }
154160
155- int fieldCount = reader . FieldCount ;
156- do
157- {
158- object [ ] row = new object [ fieldCount ] ;
159- reader . GetValues ( row ) ;
160- yield return row ;
161- } while ( reader . Read ( ) ) ;
162- }
161+ static IEnumerable < object [ ] > AsEnumerableCore ( IDataReader reader , ArrayPool < object > arrayPool )
162+ {
163+ Debug . Assert ( reader is not null ) ;
164+ Debug . Assert ( arrayPool is not null ) ;
165+
166+ if ( ! reader . Read ( ) )
167+ yield break ;
168+
169+ int fieldCount = reader . FieldCount ;
170+ do
171+ {
172+ object [ ] row = arrayPool . Rent ( fieldCount ) ;
173+ reader . GetValues ( row ) ;
174+ yield return row ;
175+ } while ( reader . Read ( ) ) ;
163176 }
164177
165178 /// <inheritdoc cref="AsEnumerable(IDataReader, ArrayPool{object?}, int, int[])"/>
166- public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader , ArrayPool < object > arrayPool )
167- {
168- return reader is null
179+ public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader )
180+ => reader is null
181+ ? throw new ArgumentNullException ( nameof ( reader ) )
182+ : AsEnumerableCore ( reader ) ;
183+
184+ /// <inheritdoc cref="AsEnumerable(IDataReader, ArrayPool{object?}, int, int[])"/>
185+ public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader , ArrayPool < object > ? arrayPool )
186+ => reader is null
169187 ? throw new ArgumentNullException ( nameof ( reader ) )
170188 : arrayPool is null
171- ? throw new ArgumentNullException ( nameof ( arrayPool ) )
189+ ? AsEnumerableCore ( reader )
172190 : AsEnumerableCore ( reader , arrayPool ) ;
173191
174- static IEnumerable < object [ ] > AsEnumerableCore ( IDataReader reader , ArrayPool < object > arrayPool )
175- {
176- if ( ! reader . Read ( ) )
177- yield break ;
192+ static IEnumerable < object [ ] > AsEnumerableInternalCore (
193+ IDataReader reader , IEnumerable < int > ordinals , bool readStarted )
194+ {
195+ Debug . Assert ( reader is not null ) ;
196+ Debug . Assert ( ordinals is not null ) ;
178197
179- int fieldCount = reader . FieldCount ;
198+ if ( ! readStarted && ! reader . Read ( ) )
199+ yield break ;
200+
201+ IList < int > o = ordinals as IList < int > ?? ordinals . ToArray ( ) ;
202+ int fieldCount = o . Count ;
203+ if ( fieldCount == 0 )
204+ {
180205 do
181206 {
182- object [ ] row = arrayPool . Rent ( fieldCount ) ;
183- reader . GetValues ( row ) ;
184- yield return row ;
185- } while ( reader . Read ( ) ) ;
207+ yield return Array . Empty < object > ( ) ;
208+ }
209+ while ( reader . Read ( ) ) ;
210+ yield break ;
186211 }
212+
213+ do
214+ {
215+ object [ ] row = new object [ fieldCount ] ;
216+ for ( int i = 0 ; i < fieldCount ; i ++ )
217+ row [ i ] = reader . GetValue ( o [ i ] ) ;
218+ yield return row ;
219+ }
220+ while ( reader . Read ( ) ) ;
187221 }
188222
189- internal static IEnumerable < object [ ] > AsEnumerableInternal ( this IDataReader reader , IEnumerable < int > ordinals , bool readStarted )
223+ static IEnumerable < object [ ] > AsEnumerableInternalCore (
224+ IDataReader reader , IEnumerable < int > ordinals , bool readStarted , ArrayPool < object > arrayPool )
190225 {
191- return reader is null
192- ? throw new ArgumentNullException ( nameof ( reader ) )
193- : ordinals is null
194- ? throw new ArgumentNullException ( nameof ( ordinals ) )
195- : AsEnumerableInternalCore ( reader , ordinals , readStarted ) ;
226+ Debug . Assert ( reader is not null ) ;
227+ Debug . Assert ( ordinals is not null ) ;
228+ Debug . Assert ( arrayPool is not null ) ;
196229
197- static IEnumerable < object [ ] > AsEnumerableInternalCore ( IDataReader reader , IEnumerable < int > ordinals , bool readStarted )
198- {
199- if ( ! readStarted && ! reader . Read ( ) )
200- yield break ;
230+ if ( ! readStarted && ! reader . Read ( ) )
231+ yield break ;
201232
202- IList < int > o = ordinals as IList < int > ?? ordinals . ToArray ( ) ;
203- int fieldCount = o . Count ;
204- if ( fieldCount == 0 )
205- {
206- do
207- {
208- yield return Array . Empty < object > ( ) ;
209- }
210- while ( reader . Read ( ) ) ;
211- yield break ;
212- }
213-
214- do
215- {
216- object [ ] row = new object [ fieldCount ] ;
217- for ( int i = 0 ; i < fieldCount ; i ++ )
218- row [ i ] = reader . GetValue ( o [ i ] ) ;
219- yield return row ;
220- }
221- while ( reader . Read ( ) ) ;
233+ IList < int > o = ordinals as IList < int > ?? ordinals . ToArray ( ) ;
234+ int fieldCount = o . Count ;
235+ do
236+ {
237+ object [ ] row = arrayPool . Rent ( fieldCount ) ;
238+ for ( int i = 0 ; i < fieldCount ; i ++ )
239+ row [ i ] = reader . GetValue ( o [ i ] ) ;
240+ yield return row ;
222241 }
242+ while ( reader . Read ( ) ) ;
223243 }
224244
225- internal static IEnumerable < object [ ] > AsEnumerableInternal ( this IDataReader reader , IEnumerable < int > ordinals , bool readStarted , ArrayPool < object > arrayPool )
245+ internal static IEnumerable < object [ ] > AsEnumerableInternal (
246+ this IDataReader reader ,
247+ IEnumerable < int > ordinals ,
248+ bool readStarted )
226249 {
227- return reader is null
228- ? throw new ArgumentNullException ( nameof ( reader ) )
229- : ordinals is null
230- ? throw new ArgumentNullException ( nameof ( ordinals ) )
231- : arrayPool is null ? throw new ArgumentNullException ( nameof ( arrayPool ) )
232- : AsEnumerableInternalCore ( ) ;
250+ if ( reader is null ) throw new ArgumentNullException ( nameof ( reader ) ) ;
251+ if ( ordinals is null ) throw new ArgumentNullException ( nameof ( ordinals ) ) ;
252+ Contract . EndContractBlock ( ) ;
233253
234- IEnumerable < object [ ] > AsEnumerableInternalCore ( )
235- {
236- if ( ! readStarted && ! reader . Read ( ) )
237- yield break ;
254+ return AsEnumerableInternalCore ( reader , ordinals , readStarted ) ;
255+ }
238256
239- IList < int > o = ordinals as IList < int > ?? ordinals . ToArray ( ) ;
240- int fieldCount = o . Count ;
241- do
242- {
243- object [ ] row = arrayPool . Rent ( fieldCount ) ;
244- for ( int i = 0 ; i < fieldCount ; i ++ )
245- row [ i ] = reader . GetValue ( o [ i ] ) ;
246- yield return row ;
247- }
248- while ( reader . Read ( ) ) ;
249- }
257+ internal static IEnumerable < object [ ] > AsEnumerableInternal (
258+ this IDataReader reader ,
259+ IEnumerable < int > ordinals ,
260+ bool readStarted ,
261+ ArrayPool < object > ? arrayPool )
262+ {
263+ if ( reader is null ) throw new ArgumentNullException ( nameof ( reader ) ) ;
264+ if ( ordinals is null ) throw new ArgumentNullException ( nameof ( ordinals ) ) ;
265+ Contract . EndContractBlock ( ) ;
266+
267+ return arrayPool is null
268+ ? AsEnumerableInternalCore ( reader , ordinals , readStarted )
269+ : AsEnumerableInternalCore ( reader , ordinals , readStarted , arrayPool ) ;
250270 }
251271
252272 /// <inheritdoc cref="AsEnumerable(IDataReader, IEnumerable{int}, ArrayPool{object?})"/>
@@ -257,7 +277,7 @@ public static IEnumerable<object[]> AsEnumerable(this IDataReader reader, IEnume
257277 /// <param name="ordinals">The limited set of ordinals to include. If none are specified, the returned objects will be empty.</param>
258278 /// <param name="arrayPool">The array pool to acquire buffers from.</param>
259279 /// <inheritdoc cref="AsEnumerable(IDataReader, ArrayPool{object?}, int, int[])"/>
260- public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader , IEnumerable < int > ordinals , ArrayPool < object > arrayPool )
280+ public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader , IEnumerable < int > ordinals , ArrayPool < object > ? arrayPool )
261281 => AsEnumerableInternal ( reader , ordinals , false , arrayPool ) ;
262282
263283 /// <param name="reader">The reader to enumerate.</param>
@@ -276,7 +296,7 @@ public static IEnumerable<object[]> AsEnumerable(this IDataReader reader, int n,
276296 /// <param name="n">The first ordinal to include in the request to the reader for each record.</param>
277297 /// <param name="others">The remaining ordinals to request from the reader for each record.</param>
278298 /// <returns>An enumerable of the values returned from a data reader.</returns>
279- public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader , ArrayPool < object > arrayPool , int n , params int [ ] others )
299+ public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader , ArrayPool < object > ? arrayPool , int n , params int [ ] others )
280300 => AsEnumerable ( reader , CoreExtensions . Concat ( n , others ) , arrayPool ) ;
281301
282302 /// <inheritdoc cref="Select{T}(IDataReader, Func{IDataRecord, T}, CancellationToken, bool)"/>
@@ -392,12 +412,15 @@ static async IAsyncEnumerable<object[]> AsAsyncEnumerableCore(DbDataReader reade
392412 /// <param name="arrayPool">An optional array pool to acquire buffers from.</param>
393413 /// <param name="cancellationToken">Optional iteration cancellation token.</param>
394414 /// <inheritdoc cref="AsEnumerable(IDataReader, ArrayPool{object?}, int, int[])"/>
395- public static IAsyncEnumerable < object [ ] > AsAsyncEnumerable ( this DbDataReader reader , ArrayPool < object > arrayPool , CancellationToken cancellationToken = default )
415+ public static IAsyncEnumerable < object [ ] > AsAsyncEnumerable (
416+ this DbDataReader reader ,
417+ ArrayPool < object > ? arrayPool ,
418+ CancellationToken cancellationToken = default )
396419 {
397420 return reader is null
398421 ? throw new ArgumentNullException ( nameof ( reader ) )
399422 : arrayPool is null
400- ? throw new ArgumentNullException ( nameof ( arrayPool ) )
423+ ? AsAsyncEnumerable ( reader , cancellationToken )
401424 : AsAsyncEnumerableCore ( reader , arrayPool , cancellationToken ) ;
402425
403426 static async IAsyncEnumerable < object [ ] > AsAsyncEnumerableCore ( DbDataReader reader , ArrayPool < object > arrayPool , [ EnumeratorCancellation ] CancellationToken cancellationToken )
0 commit comments