public void HandlePrimaryKey(SqlTable table, SqlColumn column, StringBuilder columnBuilder)
 {
     if (column.IsPrimaryKey)
     {
         // TODO: this will break, when column is part of a composite primairy key.
         columnBuilder.AppendFormat(" constraint PK_{0}_{1}_{2} primary key", table.Schema, table.Name, column.Name);
     }
 }
 public void HandleForeignKey(SqlTable table, SqlColumn column, StringBuilder columnBuilder)
 {
     if (column.IsForeignKey)
     {
         foreach (SqlForeignKey fk in table.ForeignKeys)
         {
             if (fk.ColumnName.Equals(column.Name))
             {
                 // TODO: this will break, when column is part of multiple foreingkeys.
                 columnBuilder.AppendFormat(" constraint FK_{0}_{1}_{2} foreign key references {3}.{4}({5})", table.Schema, table.Name, column.Name, fk.ReferencedSchemaName, fk.ReferencedTableName, fk.ReferencedColumnName);
                 break;
             }
         }
     }
 }
 public void HandleDefault(SqlTable table, SqlColumn column, StringBuilder columnBuilder)
 {
     if (column.IsDefault)
     {
         // TODO: this will break, when column is foreignkey and default.
         columnBuilder.AppendFormat(" constraint DF_{0}_{1}_{2} default {3}", table.Schema, table.Name, column.Name, column.DefaultText);
     }
 }
        public void MapSmoTablesToSqlScriptDtos(IEnumerable<Table> tables, SqlScriptingInput input)
        {
            foreach (Table table in tables)
            {
                var sqlTable = new SqlTable
                {
                    Name = table.Name,
                    Schema = table.Schema
                };

                foreach (Column column in table.Columns)
                {
                    DataType dataType =  column.DataType;
                    var sqlColumn = new SqlColumn
                    {
                        DataTypeMaximumLength = dataType.MaximumLength,
                        DataTypeName = dataType.Name,
                        DataTypeNumericPrecision = dataType.NumericPrecision,
                        DataTypeNumericScale = dataType.NumericScale,
                        IdentityIncrement = column.IdentityIncrement,
                        IdentitySeed = column.IdentitySeed,
                        IsDefault = (column.DefaultConstraint != null),
                        IsForeignKey = (table.ForeignKeys != null && table.ForeignKeys.Count > 0),
                        IsIdentity = column.Identity,
                        IsNullable = column.Nullable,
                        IsPrimaryKey = (column.InPrimaryKey),
                        Name = column.Name
                    };

                    sqlTable.Columns.Add(sqlColumn);
                }

                foreach (ForeignKey fk in table.ForeignKeys)
                {
                    ForeignKeyColumn fkc = fk.Columns[0];
                    
                    // TODO: this is not a correct mapping, must be fixed!
                    var sqlForeignKey = new SqlForeignKey
                    {
                        ReferencedSchemaName = fk.ReferencedTableSchema,
                        ReferencedTableName = fk.ReferencedTable,
                        ColumnName = fkc.Name,
                        ReferencedColumnName = fkc.ReferencedColumn
                    };
                    
                    sqlTable.ForeignKeys.Add(sqlForeignKey);
                }

                input.Tables.Add(sqlTable);
            }
        }