/// <inheritdoc />
        public bool Add(IEnumerable <TEntity> instances, IDbTransaction transaction = null)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                var entities = instances.ToList();
                foreach (var instance in entities)
                {
                    instance.SetCreateAudit(instance.CreateUserId > 0 ? instance.CreateUserId : UserId);
                }

                var listGroup = new List <List <TEntity> >();
                var j = 2000;//每 2k 条执行一次
                for (var i = 0; i < entities.Count; i += 2000)
                {
                    var cList = entities.Take(j).Skip(i).ToList();
                    j += 2000;
                    listGroup.Add(cList);
                }

                var result = 0;
                foreach (var groupList in listGroup)
                {
                    var queryResult = SqlGenerator.GetBulkInsert(groupList);
                    result += connection.Execute(queryResult.GetSql(), queryResult.Param, transaction);
                }

                return result == entities.Count;
            }));
        }
예제 #2
0
        public PagedList <T> Page <T>(string sql, Parameters param = null, int pageIndex = 1, int pageSize = 20)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                var dic = new Dictionary <string, object>();
                if (param != null)
                {
                    foreach (var paramParameterName in param.ParameterNames)
                    {
                        dic.Add(paramParameterName, param.Get <object>(paramParameterName));
                    }
                }

                var result = PageHelper.Page(sql, dic, pageIndex, pageSize);

                var total = connection.QueryFirstOrDefault <int>(result.Item1.GetSql(), result.Item1.Param);
                var data = connection.Query <T>(result.Item2.GetSql(), result.Item2.Param);

                return new PagedList <T>()
                {
                    PageIndex = result.Item3,
                    PageSize = result.Item4,
                    TotalCount = total,
                    Items = data
                };
            }));
        }
예제 #3
0
        public async Task <PagedList <T> > PageAsync <T>(string sql, Parameters param = null, int pageIndex = 1, int pageSize = 10)
        {
            return(await ExecuteHelper.Execute(async connection =>
            {
                var dic = new Dictionary <string, object>();
                if (param != null)
                {
                    foreach (var paramParameterName in param.ParameterNames)
                    {
                        dic.Add(paramParameterName, param.Get <object>(paramParameterName));
                    }
                }

                var result = PageHelper.Page(sql, dic, pageIndex, pageSize);

                var total = await connection.QueryFirstOrDefaultAsync <int>(result.Item1.GetSql(), result.Item1.Param).ConfigureAwait(false);
                var data = await connection.QueryAsync <T>(result.Item2.GetSql(), result.Item2.Param).ConfigureAwait(false);

                return new PagedList <T>()
                {
                    PageIndex = result.Item3,
                    PageSize = result.Item4,
                    TotalCount = total,
                    Items = data
                };
            }));
        }
 /// <inheritdoc />
 public TEntity Get(object id, IDbTransaction transaction = null)
 {
     return(ExecuteHelper.Execute(connection =>
     {
         var queryResult = SqlGenerator.GetSelectById(id);
         return connection.QuerySingleOrDefault <TEntity>(queryResult.GetSql(), queryResult.Param, transaction);
     }));
 }
예제 #5
0
 /// <inheritdoc />
 public async Task <IEnumerable <TEntity> > GetListAsync(Expression <Func <TEntity, bool> > predicate, Expression <Func <TEntity, object> > filterColumns = null, IDbTransaction transaction = null)
 {
     return(await ExecuteHelper.Execute(async connection =>
     {
         var queryResult = SqlGenerator.GetSelect(predicate, filterColumns, false);
         return await connection.QueryAsync <TEntity>(queryResult.GetSql(), queryResult.Param, transaction).ConfigureAwait(false);
     }));
 }
예제 #6
0
 /// <inheritdoc />
 public int Count(Expression <Func <TEntity, bool> > predicate, IDbTransaction transaction = null)
 {
     return(ExecuteHelper.Execute(connection =>
     {
         var queryResult = SqlGenerator.GetCount(predicate);
         return connection.QueryFirstOrDefault <int>(queryResult.GetSql(), queryResult.Param, transaction);
     }));
 }
예제 #7
0
 /// <inheritdoc />
 public virtual async Task <TResult?> AvgAsync <TResult>(Expression <Func <TEntity, TResult> > predicate, Expression <Func <TEntity, bool> > condition, IDbTransaction transaction = null) where TResult : struct
 {
     return(await ExecuteHelper.Execute(async connection =>
     {
         var queryResult = SqlGenerator.GetAvg(predicate, condition);
         return await connection.QueryFirstOrDefaultAsync <TResult?>(queryResult.GetSql(), queryResult.Param, transaction).ConfigureAwait(false);
     }));
 }
예제 #8
0
 /// <inheritdoc />
 public async Task <int> CountAsync(Expression <Func <TEntity, bool> > predicate, IDbTransaction transaction = null)
 {
     return(await ExecuteHelper.Execute(async connection =>
     {
         var queryResult = SqlGenerator.GetCount(predicate);
         return await connection.QueryFirstOrDefaultAsync <int>(queryResult.GetSql(), queryResult.Param, transaction).ConfigureAwait(false);
     }));
 }
예제 #9
0
 /// <inheritdoc />
 public TEntity Get(Expression <Func <TEntity, bool> > predicate, Expression <Func <TEntity, object> > filterColumns = null, IDbTransaction transaction = null)
 {
     return(ExecuteHelper.Execute(connection =>
     {
         var queryResult = SqlGenerator.GetSelect(predicate, filterColumns, true);
         return connection.QueryFirstOrDefault <TEntity>(queryResult.GetSql(), queryResult.Param, transaction);
     }));
 }
예제 #10
0
 /// <inheritdoc />
 public bool Exist(Expression <Func <TEntity, bool> > predicate)
 {
     return(ExecuteHelper.Execute(connection =>
     {
         var queryResult = SqlGenerator.GetCount(predicate);
         return connection.QueryFirstOrDefault <int>(queryResult.GetSql(), queryResult.Param) > 0;
     }));
 }
예제 #11
0
 /// <inheritdoc />
 public IEnumerable <TEntity> GetList(Expression <Func <TEntity, bool> > predicate, Expression <Func <TEntity, object> > filterColumns = null, IDbTransaction transaction = null)
 {
     return(ExecuteHelper.Execute(connection =>
     {
         var queryResult = SqlGenerator.GetSelect(predicate, filterColumns, false);
         return connection.Query <TEntity>(queryResult.GetSql(), queryResult.Param, transaction);
     }));
 }
예제 #12
0
 /// <inheritdoc />
 public async Task <TEntity> GetAsync(object id, Expression <Func <TEntity, object> > filterColumns = null, IDbTransaction transaction = null)
 {
     return(await ExecuteHelper.Execute(async connection =>
     {
         var queryResult = SqlGenerator.GetSelectById(id, filterColumns);
         return await connection.QuerySingleOrDefaultAsync <TEntity>(queryResult.GetSql(), queryResult.Param, transaction).ConfigureAwait(false);
     }));
 }
예제 #13
0
 /// <inheritdoc />
 public TEntity Get(object id, Expression <Func <TEntity, object> > filterColumns = null, IDbTransaction transaction = null)
 {
     return(ExecuteHelper.Execute(connection =>
     {
         var queryResult = SqlGenerator.GetSelectById(id, filterColumns);
         return connection.QuerySingleOrDefault <TEntity>(queryResult.GetSql(), queryResult.Param, transaction);
     }));
 }
예제 #14
0
 /// <inheritdoc />
 public virtual TResult?Avg <TResult>(Expression <Func <TEntity, TResult> > predicate, Expression <Func <TEntity, bool> > condition, IDbTransaction transaction = null) where TResult : struct
 {
     return(ExecuteHelper.Execute(connection =>
     {
         var queryResult = SqlGenerator.GetAvg(predicate, condition);
         return connection.QueryFirstOrDefault <TResult?>(queryResult.GetSql(), queryResult.Param, transaction);
     }));
 }
예제 #15
0
        /// <inheritdoc />
        public bool Update(TEntity instance, IDbTransaction transaction = null)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                instance.SetUpdateAudit(instance.UpdateUserId > 0 ? instance.UpdateUserId : UserId);

                var sqlQuery = SqlGenerator.GetUpdate(instance);
                return connection.Execute(sqlQuery.GetSql(), instance, transaction) > 0;
            }));
        }
예제 #16
0
        /// <inheritdoc />
        public bool Delete(Expression <Func <TEntity, bool> > predicate, bool isLogicDelete = true, object updateUserId = null, IDbTransaction transaction = null)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                var userId = updateUserId ?? UserId;

                var queryResult = SqlGenerator.GetDelete(predicate, userId as object, isLogicDelete);
                return connection.Execute(queryResult.GetSql(), queryResult.Param, transaction) > 0;
            }));
        }
예제 #17
0
        /// <inheritdoc />
        public async Task <bool> UpdateAsync(TEntity instance, IDbTransaction transaction = null)
        {
            return(await ExecuteHelper.Execute(async connection =>
            {
                instance.SetUpdateAudit(instance.UpdateUserId > 0 ? instance.UpdateUserId : UserId);

                var sqlQuery = SqlGenerator.GetUpdate(instance);
                return await connection.ExecuteAsync(sqlQuery.GetSql(), instance, transaction).ConfigureAwait(false) > 0;
            }));
        }
예제 #18
0
        /// <inheritdoc />
        public bool Update(Expression <Func <TEntity, bool> > predicate, Expression <Func <TEntity, TEntity> > updateValues, object updateUserId = null, IDbTransaction transaction = null)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                var userId = updateUserId ?? UserId;

                var queryResult = SqlGenerator.GetUpdate(updateValues, predicate, userId as object);
                return connection.Execute(queryResult.GetSql(), queryResult.Param, transaction) > 0;
            }));
        }
예제 #19
0
        /// <inheritdoc />
        public async Task <bool> UpdateAsync(Expression <Func <TEntity, bool> > predicate, Expression <Func <TEntity, TEntity> > updateValues, object updateUserId = null, IDbTransaction transaction = null)
        {
            return(await ExecuteHelper.Execute(async connection =>
            {
                var userId = updateUserId ?? UserId;

                var queryResult = SqlGenerator.GetUpdate(updateValues, predicate, userId as object);
                return await connection.ExecuteAsync(queryResult.GetSql(), queryResult.Param, transaction).ConfigureAwait(false) > 0;
            }));
        }
예제 #20
0
        /// <inheritdoc />
        public async Task <bool> UpdateAsync(TEntity instance, Expression <Func <TEntity, bool> > predicate, IDbTransaction transaction = null)
        {
            return(await ExecuteHelper.Execute(async connection =>
            {
                var userId = instance.UpdateUserId > 0 ? instance.UpdateUserId : UserId;

                var sqlQuery = SqlGenerator.GetUpdate(predicate, instance, userId as object);
                return await connection.ExecuteAsync(sqlQuery.GetSql(), sqlQuery.Param, transaction).ConfigureAwait(false) > 0;
            }));
        }
예제 #21
0
        /// <inheritdoc />
        public bool Update(TEntity instance, Expression <Func <TEntity, bool> > predicate, IDbTransaction transaction = null)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                var userId = instance.UpdateUserId > 0 ? instance.UpdateUserId : UserId;

                var sqlQuery = SqlGenerator.GetUpdate(predicate, instance, userId as object);
                return connection.Execute(sqlQuery.GetSql(), sqlQuery.Param, transaction) > 0;
            }));
        }
예제 #22
0
        /// <inheritdoc />
        public bool Delete(TEntity instance, bool isLogicDelete = true, IDbTransaction transaction = null)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                if (isLogicDelete)
                {
                    instance.SetUpdateAudit(instance.UpdateUserId > 0 ? instance.UpdateUserId : UserId);
                }

                var queryResult = SqlGenerator.GetDelete(instance, isLogicDelete);
                return connection.Execute(queryResult.GetSql(), queryResult.Param, transaction) > 0;
            }));
        }
예제 #23
0
        /// <inheritdoc />
        public async Task <bool> DeleteAsync(TEntity instance, bool isLogicDelete = true, IDbTransaction transaction = null)
        {
            return(await ExecuteHelper.Execute(async connection =>
            {
                if (isLogicDelete)
                {
                    instance.SetUpdateAudit(instance.UpdateUserId > 0 ? instance.UpdateUserId : UserId);
                }

                var queryResult = SqlGenerator.GetDelete(instance, isLogicDelete);
                return await connection.ExecuteAsync(queryResult.GetSql(), queryResult.Param, transaction).ConfigureAwait(false) > 0;
            }));
        }
예제 #24
0
        /// <inheritdoc />
        public async Task <IEnumerable <TEntity> > InAsync(Expression <Func <TEntity, object> > field, IEnumerable <dynamic> keys)
        {
            return(await ExecuteHelper.Execute(async connection =>
            {
                var objects = keys.ToList();
                if (!objects.Any())
                {
                    return null;
                }

                var sqlQuery = SqlGenerator.GetIn(objects, field);
                return await connection.QueryAsync <TEntity>(sqlQuery.GetSql(), sqlQuery.Param).ConfigureAwait(false);
            }));
        }
예제 #25
0
        /// <inheritdoc />
        public async Task <bool> UpdateAsync(IEnumerable <TEntity> instances, IDbTransaction transaction = null)
        {
            return(await ExecuteHelper.Execute(async connection =>
            {
                var entities = instances.ToList();
                foreach (var instance in entities)
                {
                    instance.SetUpdateAudit(instance.UpdateUserId > 0 ? instance.UpdateUserId : UserId);
                }

                var queryResult = SqlGenerator.GetBulkUpdate(entities);
                return await connection.ExecuteAsync(queryResult.GetSql(), queryResult.Param, transaction).ConfigureAwait(false) > 0;
            }));
        }
예제 #26
0
        /// <inheritdoc />
        public bool Update(IEnumerable <TEntity> instances, IDbTransaction transaction = null)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                var entities = instances.ToList();
                foreach (var instance in entities)
                {
                    instance.SetUpdateAudit(instance.UpdateUserId > 0 ? instance.UpdateUserId : UserId);
                }

                var queryResult = SqlGenerator.GetBulkUpdate(entities);
                return connection.Execute(queryResult.GetSql(), queryResult.Param, transaction) > 0;
            }));
        }
        /// <inheritdoc />
        public IEnumerable <TEntity> In(Expression <Func <TEntity, object> > field, IEnumerable <dynamic> keys)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                var objects = keys.ToList();
                if (!objects.Any())
                {
                    return null;
                }

                var sqlQuery = SqlGenerator.GetIn(objects, field);
                return connection.Query <TEntity>(sqlQuery.GetSql(), sqlQuery.Param);
            }));
        }
        /// <inheritdoc />
        public bool Add(TEntity instance, IDbTransaction transaction = null)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                instance.SetCreateAudit(instance.CreateUserId > 0 ? instance.CreateUserId : UserId);

                var queryResult = SqlGenerator.GetInsert(instance);
                if (SqlGenerator.IsIdentity)
                {
                    var newId = connection.Query <long>(queryResult.GetSql(), queryResult.Param, transaction)
                                .FirstOrDefault();
                    return SetValue(newId, instance);
                }

                return connection.Execute(queryResult.GetSql(), instance, transaction) > 0;
            }));
        }
예제 #29
0
        /// <inheritdoc />
        public async Task <bool> AddAsync(TEntity instance, IDbTransaction transaction = null)
        {
            return(await ExecuteHelper.Execute(async connection =>
            {
                instance.SetCreateAudit(instance.CreateUserId > 0 ? instance.CreateUserId : UserId);

                var queryResult = SqlGenerator.GetInsert(instance);
                if (SqlGenerator.IsIdentity)
                {
                    var newId = (await connection.QueryAsync <long>(queryResult.GetSql(), queryResult.Param, transaction).ConfigureAwait(false))
                                .FirstOrDefault();
                    return SetValue(newId, instance);
                }

                return await connection.ExecuteAsync(queryResult.GetSql(), instance, transaction) > 0;
            }));
        }
예제 #30
0
        /// <inheritdoc />
        public PagedList <TEntity> GetPageList(Expression <Func <TEntity, bool> > predicate, int pageIndex, int pageSize, string orderBy = null, Expression <Func <TEntity, object> > filterColumns = null)
        {
            return(ExecuteHelper.Execute(connection =>
            {
                var sqlQuery = SqlGenerator.GetSelect(predicate, filterColumns, false);
                if (!string.IsNullOrEmpty(orderBy))
                {
                    sqlQuery.SqlBuilder.Append(" order by " + orderBy);
                }

                var result = PageHelper.Page(sqlQuery.GetSql(), sqlQuery.Param, pageIndex, pageSize);

                var total = connection.QueryFirstOrDefault <int>(result.Item1.GetSql(), result.Item1.Param);
                var data = connection.Query <TEntity>(result.Item2.GetSql(), result.Item2.Param);

                return new PagedList <TEntity>()
                {
                    PageIndex = result.Item3,
                    PageSize = result.Item4,
                    TotalCount = total,
                    Items = data
                };
            }));
        }