/// <summary> /// Iterates a reader on a command with a handler function. /// </summary> /// <param name="command">The IExecuteReader to iterate.</param> /// <param name="handler">The handler function for each IDataRecord.</param> /// <param name="behavior">The behavior to use with the data reader.</param> public static ValueTask IterateReaderAsync(this IExecuteReaderAsync command, Action <IDataRecord> handler, CommandBehavior behavior = CommandBehavior.Default) { if (command is null) { throw new ArgumentNullException(nameof(command)); } if (handler is null) { throw new ArgumentNullException(nameof(handler)); } Contract.EndContractBlock(); return(command.ExecuteReaderAsync( reader => { if (reader is DbDataReader r) { return r.ForEachAsync(handler, command.UseAsyncRead, command.CancellationToken); } reader.ForEach(handler, true, command.CancellationToken); return new ValueTask(); }, behavior | CommandBehavior.SingleResult)); }
/// <summary> /// Iterates a reader on a command while the handler function returns true. /// </summary> /// <param name="command">The IExecuteReader to iterate.</param> /// <param name="handler">The handler function for each IDataRecord.</param> /// <param name="behavior">The behavior to use with the data reader.</param> public static ValueTask IterateReaderWhileAsync(this IExecuteReaderAsync command, Func <IDataRecord, ValueTask <bool> > handler, CommandBehavior behavior = CommandBehavior.Default) { if (command is null) { throw new ArgumentNullException(nameof(command)); } if (handler is null) { throw new ArgumentNullException(nameof(handler)); } Contract.EndContractBlock(); return(command.ExecuteReaderAsync( reader => reader.IterateWhileAsync(handler, command.CancellationToken), behavior | CommandBehavior.SingleResult)); }