Esempio n. 1
0
        private ArchAngel.Interfaces.Scripting.DatabaseChanges.IDatabase FillScriptModelCreateAll()
        {
            ArchAngel.Interfaces.Scripting.DatabaseChanges.IDatabase database = new Interfaces.Scripting.DatabaseChanges.IDatabase(DB1.Name)
            {
                DatabaseType = Interfaces.ProjectOptions.TypeMappings.Utility.DatabaseTypes.SqlServer
            };
            Dictionary<IKey, ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey> primaryKeyLookups = new Dictionary<IKey, Interfaces.Scripting.NHibernate.Model.IKey>();
            Dictionary<ITable, ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable> tableLookups = new Dictionary<ITable, Interfaces.Scripting.NHibernate.Model.ITable>();

            foreach (var table in DB1.Tables)
            {
                ITable oldTable = table;

                ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable newTable = new Interfaces.Scripting.NHibernate.Model.ITable(DB1.Name)
                {
                    Name = oldTable.Name,
                    Schema = oldTable.Schema
                };
                tableLookups.Add(oldTable, newTable);
                database.NewTables.Add(newTable);

                foreach (IColumn oldColumn in oldTable.Columns.OrderBy(c => c.OrdinalPosition))
                    newTable.Columns.Add(ConvertColumn(oldColumn));

                foreach (IIndex oldIndex in oldTable.Indexes)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IIndex newIndex = new Interfaces.Scripting.NHibernate.Model.IIndex()
                    {
                    };
                    newTable.Indexes.Add(newIndex);
                }
                if (oldTable.FirstPrimaryKey != null)
                {
                    newTable.PrimaryKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name = oldTable.FirstPrimaryKey.Name,
                        TableName = oldTable.Name,
                        TableSchema = oldTable.Schema
                    };
                    foreach (IColumn oldColumn in oldTable.FirstPrimaryKey.Columns.OrderBy(c => c.OrdinalPosition))
                        newTable.PrimaryKey.Columns.Add(ConvertColumn(oldColumn));

                    primaryKeyLookups.Add(oldTable.FirstPrimaryKey, newTable.PrimaryKey);

                }
            }
            // Foreign keys
            foreach (var table in DB1.Tables)
            {
                ITable oldTable = table;

                foreach (IKey oldKey in oldTable.ForeignKeys)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey newKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name = oldKey.Name,
                        ReferencedPrimaryKey = primaryKeyLookups[oldKey.ReferencedKey]
                    };
                    foreach (IColumn oldColumn in oldKey.Columns.OrderBy(c => c.OrdinalPosition))
                        newKey.Columns.Add(ConvertColumn(oldColumn));

                    tableLookups[oldTable].ForeignKeys.Add(newKey);
                }
            }

            return database;
        }
Esempio n. 2
0
        private ArchAngel.Interfaces.Scripting.DatabaseChanges.IDatabase FillScriptModel(string databaseName)
        {
            ArchAngel.Interfaces.Scripting.DatabaseChanges.IDatabase database = new Interfaces.Scripting.DatabaseChanges.IDatabase(databaseName)
            {
                //DatabaseType = (Interfaces.ProjectOptions.TypeMappings.Utility.DatabaseTypes)Enum.Parse(typeof(Interfaces.ProjectOptions.TypeMappings.Utility.DatabaseTypes), )
                DatabaseType = Interfaces.ProjectOptions.TypeMappings.Utility.DatabaseTypes.SqlServer
            };
            //database.

            List<ITwoStepMergeOperation> twoStepOps = new List<ITwoStepMergeOperation>();
            List<TableRemovalOperation> addedTableOps = new List<TableRemovalOperation>();
            List<TableAdditionOperation> removedTableOps = new List<TableAdditionOperation>();
            Dictionary<ITable, object> changedTableOps = new Dictionary<ITable, object>();

            List<IMergeOperation<IColumn>> changedColumnOps = new List<IMergeOperation<IColumn>>();
            List<IMergeOperation<IIndex>> changedIndexOps = new List<IMergeOperation<IIndex>>();
            List<IMergeOperation<IKey>> changedKeyOps = new List<IMergeOperation<IKey>>();
            List<IMergeOperation<Relationship>> changedRelationshipOps = new List<IMergeOperation<Relationship>>();

            foreach (Node node in nodeNewEntities.Nodes)
                if (node.Checked)
                    addedTableOps.Add((TableRemovalOperation)node.Tag);

            foreach (Node node in nodeRemovedEntities.Nodes)
                if (node.Checked)
                    removedTableOps.Add((TableAdditionOperation)node.Tag);

            foreach (Node node in nodeModifiedEntities.Nodes)
                if (node.Checked)
                {
                    foreach (Node childNode in node.Nodes)
                    {
                        if (childNode.Tag is ColumnAdditionOperation ||
                            childNode.Tag is ColumnChangeOperation ||
                            childNode.Tag is ColumnRemovalOperation)
                        {
                            changedColumnOps.Add((IMergeOperation<IColumn>)childNode.Tag);
                        }
                        else if (childNode.Tag is IndexAdditionOperation ||
                            childNode.Tag is IndexChangeOperation ||
                            childNode.Tag is IndexRemovalOperation)
                        {
                            changedIndexOps.Add((IMergeOperation<IIndex>)childNode.Tag);
                        }
                        else if (childNode.Tag is KeyAdditionOperation ||
                            childNode.Tag is KeyChangeOperation ||
                            childNode.Tag is KeyRemovalOperation)
                        {
                            changedKeyOps.Add((IMergeOperation<IKey>)childNode.Tag);
                        }
                        else if (childNode.Tag is RelationshipAdditionOperation ||
                        childNode.Tag is RelationshipChangeOperation ||
                        childNode.Tag is RelationshipRemovalOperation)
                        {
                            changedRelationshipOps.Add((IMergeOperation<Relationship>)childNode.Tag);
                        }
                        else
                            throw new NotImplementedException("Modification type not handled yet: " + childNode.Tag.GetType().Name);
                    }
                    //changedTableOps.Add((TableChangeOperation)node.Tag);
                }

            #region Added Tables

            foreach (var op in addedTableOps)
            {
                ITable oldTable = op.Object;

                ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable newTable = new Interfaces.Scripting.NHibernate.Model.ITable(databaseName)
                {
                    Name = oldTable.Name,
                    Schema = oldTable.Schema
                };
                database.NewTables.Add(newTable);

                foreach (IColumn oldColumn in oldTable.Columns)
                    newTable.Columns.Add(ConvertColumn(oldColumn));

                foreach (IIndex oldIndex in oldTable.Indexes)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IIndex newIndex = new Interfaces.Scripting.NHibernate.Model.IIndex()
                    {
                    };
                    newTable.Indexes.Add(newIndex);
                }
                foreach (IKey oldKey in oldTable.ForeignKeys)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey newKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name = oldKey.Name
                    };
                    foreach (IColumn oldColumn in oldKey.Columns)
                        newKey.Columns.Add(ConvertColumn(oldColumn));

                    newTable.ForeignKeys.Add(newKey);
                }
                if (oldTable.FirstPrimaryKey != null)
                {
                    newTable.PrimaryKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name = oldTable.FirstPrimaryKey.Name
                    };
                    foreach (IColumn oldColumn in oldTable.FirstPrimaryKey.Columns)
                        newTable.PrimaryKey.Columns.Add(ConvertColumn(oldColumn));

                }
            }
            #endregion

            #region Removed Tables
            foreach (var op in removedTableOps)
            {
                ITable oldTable = op.Object;

                ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable newTable = new Interfaces.Scripting.NHibernate.Model.ITable(databaseName)
                {
                    Name = oldTable.Name,
                    Schema = oldTable.Schema
                };
                database.RemovedTables.Add(newTable);
            }
            #endregion

            #region New, Changed, Removed columns

            foreach (var op in changedColumnOps)
            {
                IColumn oldColumn = op.Object;
                ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedTable table = GetChangedTable(database, oldColumn.Parent);

                if (op is ColumnAdditionOperation)
                    table.RemovedColumns.Add(ConvertColumn(oldColumn));
                else if (op is ColumnChangeOperation)
                {
                    ColumnChangeOperation changeOp = (ColumnChangeOperation)op;
                    ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedColumn changedColumn = new Interfaces.Scripting.DatabaseChanges.IChangedColumn();
                    //changeOp.
                    IColumn chColumn = (IColumn)changeOp.Object;
                    changedColumn.OldName = oldColumn.Name;
                    changedColumn.NewName = chColumn.Name;
                    changedColumn.NewType = chColumn.OriginalDataType;
                    changedColumn.NewLength = chColumn.Size;

                    table.ChangedColumns.Add(changedColumn);
                }
                else if (op is ColumnRemovalOperation)
                    table.NewColumns.Add(ConvertColumn(oldColumn));
            }
            #endregion

            #region New, Changed, Removed indexes

            foreach (var op in changedIndexOps)
            {
                IIndex oldIndex = op.Object;
                ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedTable table = GetChangedTable(database, oldIndex.Parent);
                ArchAngel.Interfaces.Scripting.NHibernate.Model.IIndex newIndex = new Interfaces.Scripting.NHibernate.Model.IIndex()
                {
                    Name = oldIndex.Name
                };

                if (op is IndexAdditionOperation)
                {
                    foreach (IColumn oldColumn in oldIndex.Columns)
                        newIndex.Columns.Add(ConvertColumn(oldColumn));

                    table.RemovedIndexes.Add(newIndex);
                }
                else if (op is IndexChangeOperation)
                    table.ChangedIndexes.Add(newIndex);
                else if (op is IndexRemovalOperation)
                    table.NewIndexes.Add(newIndex);
            }
            #endregion

            #region New, Changed, Removed keys

            foreach (var op in changedKeyOps)
            {
                IKey oldKey = op.Object;
                ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedTable table = GetChangedTable(database, oldKey.Parent);
                ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey newKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                {
                    Name = oldKey.Name,
                    TableName = oldKey.Parent.Name,
                    TableSchema = oldKey.Parent.Schema
                };
                newKey.KeyType = (Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes)Enum.Parse(typeof(Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes), oldKey.Keytype.ToString());

                foreach (IColumn oldColumn in oldKey.Columns)
                    newKey.Columns.Add(ConvertColumn(oldColumn));

                if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Foreign)
                {
                    ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedTable primaryTable = GetChangedTable(database, oldKey.ReferencedKey.Parent);
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey newPrimaryKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name = oldKey.ReferencedKey.Name,
                        TableSchema = oldKey.ReferencedKey.Parent.Schema,
                        TableName = oldKey.ReferencedKey.Parent.Name
                    };
                    newPrimaryKey.KeyType = (Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes)Enum.Parse(typeof(Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes), oldKey.ReferencedKey.Keytype.ToString());

                    foreach (IColumn oldColumn in oldKey.ReferencedKey.Columns)
                        newPrimaryKey.Columns.Add(ConvertColumn(oldColumn));

                    newKey.ReferencedPrimaryKey = newPrimaryKey;
                }

                if (op is KeyAdditionOperation)
                {
                    if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Foreign)
                        table.RemovedForeignKeys.Add(newKey);
                    else if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Primary)
                        table.RemovedPrimaryKeys.Add(newKey);
                    else
                        throw new NotImplementedException("KeyType not handled yet: " + newKey.KeyType.ToString());
                }
                else if (op is KeyChangeOperation)
                {
                    if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Foreign)
                        table.ChangedForeignKeys.Add(newKey);
                    else if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Primary)
                        table.ChangedPrimaryKeys.Add(newKey);
                    else
                        throw new NotImplementedException("KeyType not handled yet: " + newKey.KeyType.ToString());
                }
                else if (op is KeyRemovalOperation)
                {
                    if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Foreign)
                        table.NewForeignKeys.Add(newKey);
                    else if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Primary)
                        table.NewPrimaryKeys.Add(newKey);
                    else
                        throw new NotImplementedException("KeyType not handled yet: " + newKey.KeyType.ToString());
                }
            }
            #endregion

            // Clean-up non-changed tables
            database.ChangedTables = database.ChangedTables.Where(t => t.CountOfChanges > 0).ToList();
            return database;
        }
Esempio n. 3
0
        private ArchAngel.Interfaces.Scripting.DatabaseChanges.IDatabase FillScriptModelCreateAll()
        {
            ArchAngel.Interfaces.Scripting.DatabaseChanges.IDatabase database = new Interfaces.Scripting.DatabaseChanges.IDatabase(DB1.Name)
            {
                DatabaseType = Interfaces.ProjectOptions.TypeMappings.Utility.DatabaseTypes.SqlServer
            };
            Dictionary <IKey, ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey>     primaryKeyLookups = new Dictionary <IKey, Interfaces.Scripting.NHibernate.Model.IKey>();
            Dictionary <ITable, ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable> tableLookups      = new Dictionary <ITable, Interfaces.Scripting.NHibernate.Model.ITable>();

            foreach (var table in DB1.Tables)
            {
                ITable oldTable = table;

                ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable newTable = new Interfaces.Scripting.NHibernate.Model.ITable(DB1.Name)
                {
                    Name   = oldTable.Name,
                    Schema = oldTable.Schema
                };
                tableLookups.Add(oldTable, newTable);
                database.NewTables.Add(newTable);

                foreach (IColumn oldColumn in oldTable.Columns.OrderBy(c => c.OrdinalPosition))
                {
                    newTable.Columns.Add(ConvertColumn(oldColumn));
                }

                foreach (IIndex oldIndex in oldTable.Indexes)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IIndex newIndex = new Interfaces.Scripting.NHibernate.Model.IIndex()
                    {
                    };
                    newTable.Indexes.Add(newIndex);
                }
                if (oldTable.FirstPrimaryKey != null)
                {
                    newTable.PrimaryKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name        = oldTable.FirstPrimaryKey.Name,
                        TableName   = oldTable.Name,
                        TableSchema = oldTable.Schema
                    };
                    foreach (IColumn oldColumn in oldTable.FirstPrimaryKey.Columns.OrderBy(c => c.OrdinalPosition))
                    {
                        newTable.PrimaryKey.Columns.Add(ConvertColumn(oldColumn));
                    }

                    primaryKeyLookups.Add(oldTable.FirstPrimaryKey, newTable.PrimaryKey);
                }
            }
            // Foreign keys
            foreach (var table in DB1.Tables)
            {
                ITable oldTable = table;

                foreach (IKey oldKey in oldTable.ForeignKeys)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey newKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name = oldKey.Name,
                        ReferencedPrimaryKey = primaryKeyLookups[oldKey.ReferencedKey]
                    };
                    foreach (IColumn oldColumn in oldKey.Columns.OrderBy(c => c.OrdinalPosition))
                    {
                        newKey.Columns.Add(ConvertColumn(oldColumn));
                    }

                    tableLookups[oldTable].ForeignKeys.Add(newKey);
                }
            }

            return(database);
        }
Esempio n. 4
0
        private ArchAngel.Interfaces.Scripting.DatabaseChanges.IDatabase FillScriptModel(string databaseName)
        {
            ArchAngel.Interfaces.Scripting.DatabaseChanges.IDatabase database = new Interfaces.Scripting.DatabaseChanges.IDatabase(databaseName)
            {
                //DatabaseType = (Interfaces.ProjectOptions.TypeMappings.Utility.DatabaseTypes)Enum.Parse(typeof(Interfaces.ProjectOptions.TypeMappings.Utility.DatabaseTypes), )
                DatabaseType = Interfaces.ProjectOptions.TypeMappings.Utility.DatabaseTypes.SqlServer
            };
            //database.

            List <ITwoStepMergeOperation> twoStepOps      = new List <ITwoStepMergeOperation>();
            List <TableRemovalOperation>  addedTableOps   = new List <TableRemovalOperation>();
            List <TableAdditionOperation> removedTableOps = new List <TableAdditionOperation>();
            Dictionary <ITable, object>   changedTableOps = new Dictionary <ITable, object>();

            List <IMergeOperation <IColumn> >      changedColumnOps       = new List <IMergeOperation <IColumn> >();
            List <IMergeOperation <IIndex> >       changedIndexOps        = new List <IMergeOperation <IIndex> >();
            List <IMergeOperation <IKey> >         changedKeyOps          = new List <IMergeOperation <IKey> >();
            List <IMergeOperation <Relationship> > changedRelationshipOps = new List <IMergeOperation <Relationship> >();

            foreach (Node node in nodeNewEntities.Nodes)
            {
                if (node.Checked)
                {
                    addedTableOps.Add((TableRemovalOperation)node.Tag);
                }
            }

            foreach (Node node in nodeRemovedEntities.Nodes)
            {
                if (node.Checked)
                {
                    removedTableOps.Add((TableAdditionOperation)node.Tag);
                }
            }

            foreach (Node node in nodeModifiedEntities.Nodes)
            {
                if (node.Checked)
                {
                    foreach (Node childNode in node.Nodes)
                    {
                        if (childNode.Tag is ColumnAdditionOperation ||
                            childNode.Tag is ColumnChangeOperation ||
                            childNode.Tag is ColumnRemovalOperation)
                        {
                            changedColumnOps.Add((IMergeOperation <IColumn>)childNode.Tag);
                        }
                        else if (childNode.Tag is IndexAdditionOperation ||
                                 childNode.Tag is IndexChangeOperation ||
                                 childNode.Tag is IndexRemovalOperation)
                        {
                            changedIndexOps.Add((IMergeOperation <IIndex>)childNode.Tag);
                        }
                        else if (childNode.Tag is KeyAdditionOperation ||
                                 childNode.Tag is KeyChangeOperation ||
                                 childNode.Tag is KeyRemovalOperation)
                        {
                            changedKeyOps.Add((IMergeOperation <IKey>)childNode.Tag);
                        }
                        else if (childNode.Tag is RelationshipAdditionOperation ||
                                 childNode.Tag is RelationshipChangeOperation ||
                                 childNode.Tag is RelationshipRemovalOperation)
                        {
                            changedRelationshipOps.Add((IMergeOperation <Relationship>)childNode.Tag);
                        }
                        else
                        {
                            throw new NotImplementedException("Modification type not handled yet: " + childNode.Tag.GetType().Name);
                        }
                    }
                    //changedTableOps.Add((TableChangeOperation)node.Tag);
                }
            }

            #region Added Tables

            foreach (var op in addedTableOps)
            {
                ITable oldTable = op.Object;

                ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable newTable = new Interfaces.Scripting.NHibernate.Model.ITable(databaseName)
                {
                    Name   = oldTable.Name,
                    Schema = oldTable.Schema
                };
                database.NewTables.Add(newTable);

                foreach (IColumn oldColumn in oldTable.Columns)
                {
                    newTable.Columns.Add(ConvertColumn(oldColumn));
                }

                foreach (IIndex oldIndex in oldTable.Indexes)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IIndex newIndex = new Interfaces.Scripting.NHibernate.Model.IIndex()
                    {
                    };
                    newTable.Indexes.Add(newIndex);
                }
                foreach (IKey oldKey in oldTable.ForeignKeys)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey newKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name = oldKey.Name
                    };
                    foreach (IColumn oldColumn in oldKey.Columns)
                    {
                        newKey.Columns.Add(ConvertColumn(oldColumn));
                    }

                    newTable.ForeignKeys.Add(newKey);
                }
                if (oldTable.FirstPrimaryKey != null)
                {
                    newTable.PrimaryKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name = oldTable.FirstPrimaryKey.Name
                    };
                    foreach (IColumn oldColumn in oldTable.FirstPrimaryKey.Columns)
                    {
                        newTable.PrimaryKey.Columns.Add(ConvertColumn(oldColumn));
                    }
                }
            }
            #endregion

            #region Removed Tables
            foreach (var op in removedTableOps)
            {
                ITable oldTable = op.Object;

                ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable newTable = new Interfaces.Scripting.NHibernate.Model.ITable(databaseName)
                {
                    Name   = oldTable.Name,
                    Schema = oldTable.Schema
                };
                database.RemovedTables.Add(newTable);
            }
            #endregion

            #region New, Changed, Removed columns

            foreach (var op in changedColumnOps)
            {
                IColumn oldColumn = op.Object;
                ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedTable table = GetChangedTable(database, oldColumn.Parent);

                if (op is ColumnAdditionOperation)
                {
                    table.RemovedColumns.Add(ConvertColumn(oldColumn));
                }
                else if (op is ColumnChangeOperation)
                {
                    ColumnChangeOperation changeOp = (ColumnChangeOperation)op;
                    ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedColumn changedColumn = new Interfaces.Scripting.DatabaseChanges.IChangedColumn();
                    //changeOp.
                    IColumn chColumn = (IColumn)changeOp.Object;
                    changedColumn.OldName   = oldColumn.Name;
                    changedColumn.NewName   = chColumn.Name;
                    changedColumn.NewType   = chColumn.OriginalDataType;
                    changedColumn.NewLength = chColumn.Size;

                    table.ChangedColumns.Add(changedColumn);
                }
                else if (op is ColumnRemovalOperation)
                {
                    table.NewColumns.Add(ConvertColumn(oldColumn));
                }
            }
            #endregion

            #region New, Changed, Removed indexes

            foreach (var op in changedIndexOps)
            {
                IIndex oldIndex = op.Object;
                ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedTable table    = GetChangedTable(database, oldIndex.Parent);
                ArchAngel.Interfaces.Scripting.NHibernate.Model.IIndex       newIndex = new Interfaces.Scripting.NHibernate.Model.IIndex()
                {
                    Name = oldIndex.Name
                };

                if (op is IndexAdditionOperation)
                {
                    foreach (IColumn oldColumn in oldIndex.Columns)
                    {
                        newIndex.Columns.Add(ConvertColumn(oldColumn));
                    }

                    table.RemovedIndexes.Add(newIndex);
                }
                else if (op is IndexChangeOperation)
                {
                    table.ChangedIndexes.Add(newIndex);
                }
                else if (op is IndexRemovalOperation)
                {
                    table.NewIndexes.Add(newIndex);
                }
            }
            #endregion

            #region New, Changed, Removed keys

            foreach (var op in changedKeyOps)
            {
                IKey oldKey = op.Object;
                ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedTable table  = GetChangedTable(database, oldKey.Parent);
                ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey         newKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                {
                    Name        = oldKey.Name,
                    TableName   = oldKey.Parent.Name,
                    TableSchema = oldKey.Parent.Schema
                };
                newKey.KeyType = (Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes)Enum.Parse(typeof(Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes), oldKey.Keytype.ToString());

                foreach (IColumn oldColumn in oldKey.Columns)
                {
                    newKey.Columns.Add(ConvertColumn(oldColumn));
                }

                if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Foreign)
                {
                    ArchAngel.Interfaces.Scripting.DatabaseChanges.IChangedTable primaryTable  = GetChangedTable(database, oldKey.ReferencedKey.Parent);
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IKey         newPrimaryKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        Name        = oldKey.ReferencedKey.Name,
                        TableSchema = oldKey.ReferencedKey.Parent.Schema,
                        TableName   = oldKey.ReferencedKey.Parent.Name
                    };
                    newPrimaryKey.KeyType = (Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes)Enum.Parse(typeof(Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes), oldKey.ReferencedKey.Keytype.ToString());

                    foreach (IColumn oldColumn in oldKey.ReferencedKey.Columns)
                    {
                        newPrimaryKey.Columns.Add(ConvertColumn(oldColumn));
                    }

                    newKey.ReferencedPrimaryKey = newPrimaryKey;
                }

                if (op is KeyAdditionOperation)
                {
                    if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Foreign)
                    {
                        table.RemovedForeignKeys.Add(newKey);
                    }
                    else if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Primary)
                    {
                        table.RemovedPrimaryKeys.Add(newKey);
                    }
                    else
                    {
                        throw new NotImplementedException("KeyType not handled yet: " + newKey.KeyType.ToString());
                    }
                }
                else if (op is KeyChangeOperation)
                {
                    if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Foreign)
                    {
                        table.ChangedForeignKeys.Add(newKey);
                    }
                    else if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Primary)
                    {
                        table.ChangedPrimaryKeys.Add(newKey);
                    }
                    else
                    {
                        throw new NotImplementedException("KeyType not handled yet: " + newKey.KeyType.ToString());
                    }
                }
                else if (op is KeyRemovalOperation)
                {
                    if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Foreign)
                    {
                        table.NewForeignKeys.Add(newKey);
                    }
                    else if (newKey.KeyType == Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Primary)
                    {
                        table.NewPrimaryKeys.Add(newKey);
                    }
                    else
                    {
                        throw new NotImplementedException("KeyType not handled yet: " + newKey.KeyType.ToString());
                    }
                }
            }
            #endregion

            // Clean-up non-changed tables
            database.ChangedTables = database.ChangedTables.Where(t => t.CountOfChanges > 0).ToList();
            return(database);
        }
        public Entity CreateEntity(ITable table)
        {
            //string newName = table.Name.RemovePrefixes(table.GetMappingSet().TablePrefixes).Singularize().GetCSharpFriendlyIdentifier().GetNextName(currentlyUsedNames);

            ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable newTable = new Interfaces.Scripting.NHibernate.Model.ITable(table.Database.Name)
            {
                Name   = table.Name,
                Schema = table.Schema
            };
            ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.ExistingPropertyNames = new List <string>();
            string newName = ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.GetEntityName(newTable);

            currentlyUsedNames.Add(newName);

            Entity entity = new EntityImpl(newName)
            {
                Schema = table.Schema.GetCSharpFriendlyIdentifier()
            };

            SetUserOption(entity, "Entity_Mutable", !table.IsView);

            foreach (var column in table.Columns)
            {
                Property prop = CreateProperty(column);
                entity.AddProperty(prop);

                if (prop.IsKeyProperty)
                {
                    entity.Key.AddProperty(prop);
                }
            }
            //ProcessTable(table, entity);
            string sequenceName = "";

            if (table.Database.DatabaseType == DatabaseLayer.DatabaseTypes.PostgreSQL)
            {
                var seqColumn = table.ColumnsInPrimaryKey.FirstOrDefault(c => c.IsIdentity && c.Default.Contains("nextval"));

                if (seqColumn != null)
                {
                    string sequence = seqColumn.Default.Replace("nextval('", "");
                    sequence = sequence.Substring(0, sequence.IndexOf("'"));

                    if (sequence.Contains("."))
                    {
                        string[] parts = sequence.Split('.');

                        if (parts[0].Equals(table.Schema, StringComparison.InvariantCultureIgnoreCase))
                        {
                            sequence = parts[1];
                        }
                    }
                    sequenceName = sequence;
                }
            }
            if (!string.IsNullOrWhiteSpace(sequenceName))
            {
                entity.Generator.ClassName = "sequence";
                entity.Generator.Parameters.Add(new EntityGenerator.Parameter("sequence", sequenceName));
            }
            else if (table.ColumnsInPrimaryKey.Count() == 1 &&
                     table.ColumnsInPrimaryKey.ElementAt(0).IsIdentity)
            {
                entity.Generator.ClassName = "native";
            }
            else
            {
                entity.Generator.ClassName = "assigned";
            }

            foreach (var property in entity.Properties)
            {
                property.Name = property.Name.GetNextName(new string[1] {
                    entity.Name
                });
            }

            return(entity);
        }