예제 #1
0
        protected override ICommandResult ExecuteUpdateCommand(SqlModelCommand <T> dataCommand, IDbCommand command)
        {
            var result = new ModelCommandResult <T>();
            var item   = dataCommand.Model as IModelBase;

            //TODO beforeExectute
            if (item != null)
            {
                if (item.Error == null)
                {
                    if (item.HasErrors())
                    {
                        result.AddError(LogType.Error, "Model has validation error");
                        return(result);
                    }
                }
            }
            if (TableMapFromDatabase)
            {
                dataCommand.GetTableMap();
            }


            if (dataCommand.TableMap.TableType == "VIEW")
            {
                if (item.Configuration == null)
                {
                    result.AddMessage(string.Format("The command is type of View and has no merge configuration"));
                    return(result);
                }
            }
            else
            {
                if (dataCommand.Track == false)
                {
                    dataCommand.AddChanges();
                }
                dataCommand.BuildKeys();
                foreach (var change in dataCommand.Changes)
                {
                    if (dataCommand.TableMap != null)
                    {
                        var column = dataCommand.TableMap.ColumnMaps.FirstOrDefault(m => m.PropertyName == change);

                        if (column == null)
                        {
                            column = dataCommand.TableMap.ColumnMaps.FirstOrDefault(m => m.ColumnName == change);
                        }

                        if (column?.IsIdentity == false)
                        {
                            dataCommand.Value(column.ColumnName, dataCommand.GetValue(change));
                        }
                    }
                    else
                    {
                        dataCommand.Value(change, dataCommand.GetValue(change));
                    }
                }

                OnBeforeCommandExecute(this, new ModelCommandBeforeExecuteEventArgs(dataCommand, command, DataCommandType.Update));

                if (dataCommand.Columns.Count > 0 && dataCommand.Changes.Count > 0)
                {
                    dataCommand.BuildSqlParameters(command);
                    dataCommand.BuildSqlCommand();
                    command.CommandText = dataCommand.SqlCommandText;



                    if (command.CommandText.Contains("WHERE"))
                    {
                        int resultIndex = command.ExecuteNonQuery();
                        result.RecordsAffected = resultIndex;
                        if (resultIndex == 0)
                        {
                            result.AddError(LogType.Information, "No rows affected");
                        }
                        else
                        {
                            dataCommand.ResetCommand();
                        }
                    }
                    else
                    {
                        result.AddError(LogType.Information, "No where in update");
                    }
                    result.AddMessage(string.Format("{0} executed with {1} rows affected", command.CommandText,
                                                    result.RecordsAffected));
                }
                result.DataCommand = dataCommand;
            }
            return(result);
        }