Beispiel #1
0
        public async Task <int> UpdateIfNotExitsAsync <T>(T entity, Expression <Func <T, bool> > where)
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            var resolver   = new EditConditionResolver <T>(entityInfo);
            var result     = resolver.Resolve(where.Body);
            var condition  = result.Condition;
            var parameters = result.Parameters;

            parameters.Add(entity);

            condition = string.IsNullOrWhiteSpace(condition) ? "1=1" : condition;

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Update(entityInfo, "");

            sql += $" AND NOT EXISTS (SELECT 1 FROM [{entityInfo.TableName}] WHERE {condition})";

            var command = new SqlCommand(sql);

            command.Parameters.AddRange(parameters.Parameters);

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                command.Connection = conn;
                return(await command.ExecuteNonQueryAsync());
            }
        }
Beispiel #2
0
        /// <summary>
        /// 如果不满足条件则创建一个实体,
        /// 如限制用户名不能重复 InsertIfNotExist(user, u => u.Name == user.Name)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">要创建的实体</param>
        /// <param name="where">条件</param>
        /// <returns>新生成记录的ID,若失败返回0</returns>
        public int InsertIfNotExists <T>(T entity, Expression <Func <T, bool> > where) where T : class, IEntity, new()
        {
            if (where == null)
            {
                return(Insert(entity));
            }

            var entityInfo = MyEntityContainer.Get(typeof(T));
            var resolver   = new EditConditionResolver <T>(entityInfo);
            var result     = resolver.Resolve(@where.Body);
            var condition  = result.Condition;
            var parameters = result.Parameters;

            parameters.Add(entity);

            condition = string.IsNullOrWhiteSpace(condition) ? "1=1" : condition;

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.InsertIfNotExists(entityInfo, condition);
            var command    = new SqlCommand(sql);

            command.Parameters.AddRange(parameters.Parameters);

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                command.Connection = conn;
                var obj = command.ExecuteScalar();
                if (obj != DBNull.Value)
                {
                    entity.Id = Convert.ToInt32(obj);
                }
                return(entity.Id);
            }
        }
Beispiel #3
0
        public async Task <int> DeleteAsync <T>(Expression <Func <T, bool> > expression, bool isForce) where T : IEntity
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            var resolver   = new EditConditionResolver <T>(entityInfo);
            var result     = resolver.Resolve(expression.Body);
            var condition  = result.Condition;
            var parameters = result.Parameters;

            condition = string.IsNullOrWhiteSpace(condition) ? "1=1" : condition;
            string sql;

            if (isForce || !entityInfo.IsSoftDelete)
            {
                sql =
                    $"DELETE [{entityInfo.TableName}] WHERE {condition}";
            }
            else
            {
                sql =
                    $"UPDATE [{entityInfo.TableName}] SET IsDel=1 WHERE {condition}";
            }

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var command = new SqlCommand(sql, conn);
                command.Parameters.AddRange(parameters.Parameters);
                return(await command.ExecuteNonQueryAsync());
            }
        }
Beispiel #4
0
        public async Task <int> UpdateAsync <T>(Expression <Func <T, bool> > expression, DbKvs kvs)
        {
            var entityInfo        = MyEntityContainer.Get(typeof(T));
            var setProperties     = kvs.Where(kv => kv.Key != "Id").Select(kv => kv.Key);
            var includeProperties = entityInfo.Properties.Where(p => setProperties.Contains(p.Name)).ToList();

            if (includeProperties.Count == 0)
            {
                return(0);
            }

            string sql;
            List <SqlParameter> parameters;

            if (expression == null)
            {
                sql =
                    $"UPDATE [{entityInfo.TableName}] SET {string.Join(",", includeProperties.Select(p => $"{p.FieldName}=@{p.Name}"))} WHERE Id=@Id";
                parameters = kvs.ToSqlParameters();
            }
            else
            {
                var resolver = new EditConditionResolver <T>(entityInfo);
                var result   = resolver.Resolve(expression.Body);
                var where = result.Condition;
                var whereParameters = result.Parameters;

                parameters = kvs.ToSqlParameters();
                parameters.AddRange(whereParameters.Parameters);

                where = string.IsNullOrWhiteSpace(where) ? "1=1" : where;

                sql =
                    $"UPDATE [{entityInfo.TableName}] SET {string.Join(",", includeProperties.Select(p => $"{p.FieldName}=@{p.Name}"))} WHERE {where}";
            }

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var command = new SqlCommand(sql, conn);
                command.Parameters.AddRange(parameters.ToArray());
                return(await command.ExecuteNonQueryAsync());
            }
        }
Beispiel #5
0
        public async Task <int> GetCountAsync <T>(Expression <Func <T, bool> > expression = null) where T : class, IEntity, new()
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            if (expression == null)
            {
                var sql = $"SELECT COUNT(0) FROM [{entityInfo.TableName}]";
                using (var conn = new SqlConnection(_connectionString))
                {
                    conn.Open();
                    var command = new SqlCommand(sql, conn);
                    return((int)command.ExecuteScalar());
                }
            }
            else
            {
                var resolver   = new EditConditionResolver <T>(entityInfo);
                var result     = resolver.Resolve(expression.Body);
                var condition  = result.Condition;
                var parameters = result.Parameters;

                condition = string.IsNullOrWhiteSpace(condition) ? "1=1" : condition;

                var sql = $"SELECT COUNT(0) FROM [{entityInfo.TableName}] WHERE [{condition}]";
                using (var conn = new SqlConnection(_connectionString))
                {
                    conn.Open();
                    var command = new SqlCommand(sql, conn);
                    command.Parameters.AddRange(parameters.Parameters);
                    var obj = await command.ExecuteScalarAsync();

                    if (obj == null)
                    {
                        return(0);
                    }

                    return((int)obj);
                }
            }
        }