Example #1
0
        public static async Task <List <T> > FetchAsync <T>(this DbCommand cmd, CancellationToken cancellation, Func <IDataReader, T> mapper = null,
                                                            bool firstRowOnly = false)
        {
            List <T> rez = new List <T>();

            try
            {
                CommandBehavior behavior = firstRowOnly ? CommandBehavior.SingleRow : CommandBehavior.Default;
                using (var reader = await cmd.ExecuteReaderAsync(behavior, cancellation))
                {
                    SqlFuDao.OnCommand(cmd);
                    while (await reader.ReadAsync(cancellation))
                    {
                        if (mapper == null)
                        {
                            mapper = PocoFactory.GetPocoMapper <T>(reader, cmd.CommandText);
                        }
                        rez.Add(mapper(reader));
                    }
                }

                return(rez);
            }
            catch (DbException ex)
            {
                SqlFuDao.OnException(cmd, ex);
                throw;
            }
        }
Example #2
0
 public static async Task<T> GetValueAsync<T>(this DbCommand cmd,Func<object, T> converter,CancellationToken token)
 {
     object rez;
     try
     {
         rez = await cmd.ExecuteScalarAsync(token);
         SqlFuDao.OnCommand(cmd);
         return converter(rez);
     }
     catch (Exception ex)
     {
         SqlFuDao.OnException(cmd, ex);
         throw;
     }
 }
Example #3
0
 public static async Task<int> ExecuteAsync(this DbCommand cmd,CancellationToken token)
 {
     int rez;
     try
     {
         rez = await cmd.ExecuteNonQueryAsync(token);
         SqlFuDao.OnCommand(cmd);
         return rez;
     }
     catch (Exception ex)
     {
         SqlFuDao.OnException(cmd, ex);
         throw;
     }
 }
Example #4
0
        public static async Task <PagedResult <T> > PagedQueryAsync <T>(this DbConnection cnx, CancellationToken token, long skip, int take, string sql,
                                                                        params object[] args)
        {
            var rez = new PagedResult <T>();

            using (var cmd = cnx.CreateCommand())
            {
                var statement = new PreparePagedStatement(cnx.GetProvider(), skip, take, sql, args);
                statement.SetupCount(cmd);
                try
                {
                    var cnt = await cmd.ExecuteScalarAsync(token);

                    cnt.MustNotBeNull();
                    if (cnt.GetType() == typeof(Int32))
                    {
                        rez.Count = (int)cnt;
                    }
                    else
                    {
                        rez.LongCount = (long)cnt;
                    }
                    SqlFuDao.OnCommand(cmd);
                }
                catch (Exception ex)
                {
                    SqlFuDao.OnException(cmd, ex);
                    throw;
                }

                if (rez.Count > 0)
                {
                    statement.Setup(cmd);
                    rez.Items = await cmd.FetchAsync <T>(token);
                }

                return(rez);
            }
        }