Example #1
0
        /// <summary>
        ///     条件更新实体中已记录更新部分
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="lambda">条件</param>
        /// <returns>更新行数</returns>
        public async Task <int> SetValueAsync(TData entity, Expression <Func <TData, bool> > lambda)
        {
            if (UpdateByMidified && !entity.__status.IsModified)
            {
                return(-1);
            }
            var setValueSql = GetModifiedSqlCode(entity);

            if (setValueSql == null)
            {
                return(-1);
            }
            var convert = Compile(lambda);

            await using var connectionScope = new ConnectionScope(DataBase);
            await using var cmd             = DataBase.CreateCommand(connectionScope);
            SetUpdateCommand(entity, cmd);
            cmd.CommandText = CreateUpdateSql(setValueSql, convert.ConditionSql);
            MySqlDataBase.TraceSql(cmd);
            return(await cmd.ExecuteNonQueryAsync());
        }
Example #2
0
        /// <summary>
        ///     插入数据
        /// </summary>
        /// <param name="entity">插入数据的实体</param>
        private async Task <bool> UpdateInnerAsync(TData entity)
        {
            string sql;

            if (UpdateByMidified)
            {
                if (UpdateByMidified && !entity.__status.IsModified)
                {
                    return(false);
                }
                PrepareSave(entity, DataOperatorType.Update);
                sql = GetModifiedSqlCode(entity);
            }
            else
            {
                PrepareSave(entity, DataOperatorType.Update);
                sql = GetSqlCode(entity);
            }
            if (sql == null)
            {
                return(false);
            }

            await using var connectionScope = new ConnectionScope(DataBase);
            await using var cmd             = DataBase.CreateCommand(connectionScope);
            SetUpdateCommand(entity, cmd);
            cmd.CommandText = CreateUpdateSql(sql, PrimaryKeyConditionSQL);

            MySqlDataBase.TraceSql(cmd);

            if (await cmd.ExecuteNonQueryAsync() <= 0)
            {
                return(false);
            }

            await EndSaved(entity, DataOperatorType.Update);

            return(true);
        }
Example #3
0
        /// <summary>
        ///     插入数据
        /// </summary>
        /// <param name="entity">插入数据的实体</param>
        private bool UpdateInner(TData entity)
        {
            string sql;

            if (UpdateByMidified)
            {
                if (UpdateByMidified && !entity.__status.IsModified)
                {
                    return(false);
                }
                PrepareSave(entity, DataOperatorType.Update);
                sql = GetModifiedSqlCode(entity);
            }
            else
            {
                PrepareSave(entity, DataOperatorType.Update);
                sql = GetSqlCode(entity);
            }
            if (sql == null)
            {
                return(false);
            }

            using var scope = new ConnectionScope(DataBase);
            using var cmd   = DataBase.CreateCommand(scope);
            SetUpdateCommand(entity, cmd);
            cmd.CommandText = CreateUpdateSql(sql, PrimaryKeyConditionSQL);

            MySqlDataBase.TraceSql(cmd);

            var result = cmd.ExecuteNonQuery();

            if (result <= 0)
            {
                return(false);
            }
            EndSaved(entity, DataOperatorType.Update);
            return(true);
        }
Example #4
0
        /// <summary>
        ///     更新数据
        /// </summary>
        /// <param name="entity">更新数据的实体</param>
        protected async Task <bool> InsertInnerAsync(TData entity)
        {
            PrepareSave(entity, DataOperatorType.Insert);
            await using var connectionScope = new ConnectionScope(DataBase);
            await using var cmd             = DataBase.CreateCommand(connectionScope);
            var isIdentitySql = SetInsertCommand(entity, cmd);

            MySqlDataBase.TraceSql(cmd);
            if (isIdentitySql)
            {
                var key = await cmd.ExecuteScalarAsync();

                if (key == DBNull.Value || key == null)
                {
                    return(false);
                }
                entity.SetValue(KeyField, key);
            }
            else
            {
                if (await cmd.ExecuteNonQueryAsync() == 0)
                {
                    return(false);
                }
            }

            var sql = AfterUpdateSql(PrimaryKeyConditionSQL);

            if (!string.IsNullOrEmpty(sql))
            {
                await DataBase.ExecuteAsync(sql, CreatePimaryKeyParameter(entity.GetValue(KeyField)));
            }
            await EndSaved(entity, DataOperatorType.Insert);

            return(true);
        }
Example #5
0
        /// <summary>
        ///     更新数据
        /// </summary>
        /// <param name="entity">更新数据的实体</param>
        protected bool InsertInner(TData entity)
        {
            PrepareSave(entity, DataOperatorType.Insert);
            //using (TransactionScope.CreateScope(DataBase))
            {
                using var scope = new ConnectionScope(DataBase);
                using var cmd   = DataBase.CreateCommand(scope);
                var isIdentitySql = SetInsertCommand(entity, cmd);
                MySqlDataBase.TraceSql(cmd);
                if (isIdentitySql)
                {
                    var key = cmd.ExecuteScalar();;
                    if (key == DBNull.Value || key == null)
                    {
                        return(false);
                    }
                    entity.SetValue(KeyField, key);
                }
                else
                {
                    if (cmd.ExecuteNonQuery() == 0)
                    {
                        return(false);
                    }
                }

                var sql = AfterUpdateSql(PrimaryKeyConditionSQL);
                if (!string.IsNullOrEmpty(sql))
                {
                    DataBase.Execute(sql, CreatePimaryKeyParameter(entity.GetValue(KeyField)));
                }
            }

            EndSaved(entity, DataOperatorType.Insert);
            return(true);
        }