Example #1
0
        public CreateTableCommand AddColumns(IEnumerable <DynamicTargetColumn> targetColumns)
        {
            if (targetColumns == null)
            {
                return(this);
            }

            foreach (var column in targetColumns)
            {
                var command = new CreateColumnCommand(Name, column.Name);

                if (column.Scope != DynamicScopeEnum.None)
                {
                    command = command.WithType(column.DataType == DataTypeEnum.String
                                                                ? DbType.String
                                                                : DbType.Int32);

                    if (column.IsRequired)
                    {
                        command = command.DbType == DbType.Int32
                                                  ? command.WithDefault(0)
                                                  : command.WithDefault("Exclude");
                    }
                }
                else
                {
                    command = command.WithType(column.DbType);
                }

                command = column.IsRequired ? command.NotNull() : command.Nullable();

                if (column.DataType == DataTypeEnum.Decimal)
                {
                    command = command.WithScale((column.Scale != -1 && column.Scale != 0) ? Convert.ToByte(column.Scale) : Convert.ToByte(5));
                    command = command.WithPrecision((column.Precision != -1 && column.Precision != 0) ? Convert.ToByte(column.Precision) : Convert.ToByte(19));
                }

                if (column.DataType == DataTypeEnum.String)
                {
                    command = command.WithLength((column.Length != -1 && column.Length != 0) ? column.Length : 255);
                }

                if (column.IsUnique)
                {
                    command = command.Unique();
                }

                TableCommands.Add(command);
            }

            return(this);
        }