Ejemplo n.º 1
0
        /// <summary>
        /// Great of huge result sets, process one row at the time
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="processor">Result processor</param>
        /// <param name="mapper"></param>
        /// <param name="firstRowOnly"></param>
        /// <param name="disposeConnection">True to dispose the connection</param>
        public static void QueryAndProcess <T>(this DbCommand cmd, Func <T, bool> processor, Func <DbDataReader, T> mapper = null,
                                               bool firstRowOnly = false, bool disposeConnection = false)
        {
            var             fuCommand = cmd.CastAs <SqlFuCommand>();
            var             strat     = fuCommand.GetErrorsStrategy();
            CommandBehavior behavior  = firstRowOnly ? CommandBehavior.SingleRow : CommandBehavior.Default;

            SqlFuCommand.HandleTransients(cmd, () =>
            {
                using (var reader = cmd.ExecuteReader(behavior))
                {
                    while (reader.Read())
                    {
                        if (!processor(cmd.GetMapper(mapper, cmd.CommandText)(reader)))
                        {
                            break;
                        }
                    }
                }
            }, strat, fuCommand.Provider, cmd.SqlConfig());
            if (disposeConnection)
            {
                cmd.Connection.Dispose();
                cmd.Dispose();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes an async query then processes each result
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="cancellation"></param>
        /// <param name="processor">Delegate to process a result item. Return false from delegate to end the query</param>
        /// <param name="mapper"></param>
        /// <param name="firstRowOnly"></param>
        /// <returns></returns>
        public static async Task QueryAndProcessAsync <T>(this DbCommand cmd, CancellationToken cancellation, Func <T, bool> processor, Func <DbDataReader, T> mapper = null,
                                                          bool firstRowOnly = false, bool disposeConnection = false)
        {
            var             fuCommand = cmd.CastAs <SqlFuCommand>();
            var             strat     = fuCommand.GetErrorsStrategy();
            CommandBehavior behavior  = firstRowOnly ? CommandBehavior.SingleRow : CommandBehavior.Default;
            await SqlFuCommand.HandleTransientsAsync(cmd, async (c) =>
            {
                using (var reader = await cmd.ExecuteReaderAsync(behavior, c).ConfigureFalse())
                {
                    while (await reader.ReadAsync(c).ConfigureFalse())
                    {
                        if (!processor(cmd.GetMapper(mapper, cmd.CommandText)(reader)))
                        {
                            break;
                        }
                    }
                }
            }, strat, fuCommand.Provider, cancellation, cmd.SqlConfig()).ConfigureFalse();

            if (disposeConnection)
            {
                cmd.Connection.Dispose();
                cmd.Dispose();
            }
        }