Esempio n. 1
0
        protected override ICommandResult ExecuteInsertCommand(SqlModelCommand <T> dataCommand, IDbCommand command)
        {
            var result = new ModelCommandResult <T>();
            var item   = dataCommand.Model as IModelBase;

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


            var  select      = string.Empty;
            bool identity    = false;
            var  retunColums = new Dictionary <string, string>();

            dataCommand.AddChanges();
            foreach (var key in dataCommand.PrimaryKeys)
            {
                var column = dataCommand.TableMap.ColumnMaps.FirstOrDefault(m => m.PropertyName == key);

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

                if (column != null)
                {
                    if (column.IsIdentity)
                    {
                        select  += string.Format(" {0},", column.ColumnName);
                        identity = true;
                        retunColums.Add(column.ColumnName, column.PropertyName);
                        if (dataCommand.Changes.Contains(column.PropertyName))
                        {
                            dataCommand.Changes.Remove(column.PropertyName);
                        }
                    }
                }
            }

            var guidIdColumns =
                dataCommand.TableMap.ColumnMaps.Where(map => !string.IsNullOrEmpty(map.Default))
                .Where(
                    map =>
                    map.Default.ToUpper().Contains("NEWSEQUENTIALID") ||
                    map.Default.ToUpper().ToUpper().Contains("NEWID"))
                .ToList();

            foreach (var guidIdColumn in guidIdColumns)
            {
                select += string.Format(" {0},", guidIdColumn.ColumnName);
                retunColums.Add(guidIdColumn.ColumnName, guidIdColumn.PropertyName);
            }

            if (identity)
            {
                dataCommand.SetWhereText(string.Format(
                                             " SELECT SCOPE_IDENTITY() as [{0}]", retunColums.FirstOrDefault().Key
                                             ));
            }


            foreach (var change in dataCommand.Changes)
            {
                var column = dataCommand.TableMap.ColumnMaps.FirstOrDefault(m => m.PropertyName == change);
                if (column != null)
                {
                    dataCommand.Value(column.ColumnName, dataCommand.GetValue(change));
                }
            }

            dataCommand.BuildSqlParameters(command);
            dataCommand.BuildSqlCommand();
            command.CommandText = dataCommand.SqlCommandText;

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


            using (var dataReader = command.ExecuteReader())
            {
                result.RecordsAffected = dataReader.RecordsAffected;
                if (dataReader.RecordsAffected == 0)
                {
                    result.AddError(LogType.Information, "No rows affected");
                }
                else
                {
                    dataCommand.SetWhereText(string.Empty);
                    dataCommand.CommandType = DataCommandType.Update;
                }
                while (dataReader.Read())
                {
                    for (int i = 0; i < 1; ++i)
                    {
                        var name     = dataReader.GetName(i);
                        var property = retunColums[name];
                        dataCommand.SetValue(property, dataReader.GetValue(i).ToInt());
                    }
                }
            }
            //TODO error when transaction is full
            result.AddMessage(string.Format("{0} executed with {1} rows affected", dataCommand.SqlCommandText,
                                            result.RecordsAffected));
            dataCommand.ResetCommand();
            result.DataCommand = dataCommand;
            return(result);
        }