public static bool IsGeneratorExist(this DBSchema schema, string gname) { if (schema == null) { throw new ArgumentNullException("schema"); } if (gname == null) { throw new ArgumentNullException("gname"); } return(schema.Generators.Any(gen => gname == gen.Name)); }
public static TableSchema GetTable(this DBSchema schema, string tableName) { if (schema == null) { throw new ArgumentNullException("schema"); } if (tableName == null) { throw new ArgumentNullException("tableName"); } return(schema.Tables.FirstOrDefault(table => tableName == table.Name)); }
public static bool IsTableExist(this DBSchema schema, string tableName) { if (schema == null) { throw new ArgumentNullException("schema"); } if (tableName == null) { throw new ArgumentNullException("tableName"); } return(schema.Tables.Any(table => tableName == table.Name)); }
/// <summary> /// Сравнение схемы с эталонной и выдача во внутреннюю хеш таблицу DDL комманд, /// выполнение общих для всех движков действий. /// </summary> public void CompareDbsc(DBSchema mbDbsc, string targetConnStr) { // TODO : эту кашу надо откомментировать и отрефакторить по человечески. var existingSchema = LoadExistingSchema(targetConnStr); var targetSchema = DbscCopyPrepare(mbDbsc); existingSchema.Normalize(); targetSchema.Normalize(); _ddlCmdTable.Rows.Clear(); #region Tables var i = 1; foreach (var eTable in existingSchema.Tables) if (!targetSchema.IsTableExist(eTable.Name)) { HelperTableDrop(eTable, existingSchema.Tables, i); i++; } i = 1; foreach (var mTable in targetSchema.Tables) { var eTable = existingSchema.GetTable(mTable.Name); if (eTable == null) HelperTableCreate(mTable, i); else if (mTable.GetDiffColumnsCount(eTable) == eTable.Columns.Length) { HelperTableDrop(eTable, existingSchema.Tables, i); HelperTableCreate(mTable, i); } else { #region Column scan foreach (var eColumn in eTable.Columns) if (!mTable.IsColumnExist(eColumn.Name)) AddDdlCommand(i, eTable.Name + @"." + eColumn.Name, "Column", MakeDdlColumnDrop(eColumn, eTable)); foreach (var mColumn in mTable.Columns) { var eColumn = eTable.GetColumn(mColumn.Name); if (eColumn == null) AddDdlCommand(i, mTable.Name + @"." + mColumn.Name, "Column", MakeDdlColumnCreate(mColumn, mTable)); // to post processing in all workers AlterColumn } #endregion #region Indexes scan foreach (var eIndex in eTable.Indexes) if (!mTable.IsIndexExist(eIndex.Name)) { AddDdlCommand(i, eTable.Name + @"." + eIndex.Name, "Index", MakeDdlIndexDrop(eIndex, eTable)); if (CheckIndexTypeForRecreate(eIndex)) foreach (var eTable1 in existingSchema.Tables) { var eKey1 = eTable1.GetKey(ConstraintType.KeyForeign, eTable.Name, ParseColumnListIndexClear(eIndex.Columns)); if (eKey1 != null) { AddDdlCommand(i, eTable.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyDrop(eKey1, eTable1)); AddDdlCommand(i, eTable.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyCreateByAlter(eKey1, eTable1)); } } } foreach (var mIndex in mTable.Indexes) if (eTable.IsIndexExist(mIndex.Name)) { var eIndex = eTable.GetIndex(mIndex.Name); if (!eIndex.Equals(mIndex)) { AddDdlCommand(i, eTable.Name + @"." + eIndex.Name, "Index", MakeDdlIndexDrop(eIndex, eTable)); AddDdlCommand(i, eTable.Name + @"." + mIndex, "Index", MakeDdlIndexCreate(mIndex, mTable)); if (CheckIndexTypeForRecreate(eIndex)) foreach (var eTable1 in existingSchema.Tables) { var eKey1 = eTable1.GetKey(ConstraintType.KeyForeign, eTable.Name, ParseColumnListIndexClear(eIndex.Columns)); if (eKey1 != null) { AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyDrop(eKey1, eTable1)); AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyCreateByAlter(eKey1, eTable1)); } } } } else AddDdlCommand(i, mTable.Name + @"." + mIndex.Name, "Index", MakeDdlIndexCreate(mIndex, mTable)); #endregion #region Keys scan foreach (var eKey in eTable.Keys) { if (eKey.KeyType == ConstraintType.Default) continue; if (!mTable.IsKeyExist(eKey.Name)) { AddDdlCommand(i, eTable.Name + @"." + eKey.Name, eKey.KeyType.ToString(), MakeDdlKeyDrop(eKey, eTable)); if (CheckKeyTypeForRecreate(eKey)) foreach (var eTable1 in existingSchema.Tables) { var eKey1 = eTable1.GetKey(ConstraintType.KeyForeign, eTable.Name, eKey.Columns); if (eKey1 != null) { AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyDrop(eKey1, eTable1)); AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyCreateByAlter(eKey1, eTable1)); } } } } foreach (var mKey in mTable.Keys) { if (mKey.KeyType == ConstraintType.Default) continue; if (eTable.IsKeyExist(mKey.Name)) { var eKey = eTable.GetKey(mKey.Name); if (!eKey.Equals(mKey)) { AddDdlCommand(i, eTable.Name + @"." + mKey.Name, mKey.KeyType.ToString(), MakeDdlKeyDrop(eKey, eTable)); AddDdlCommand(i, eTable.Name + @"." + mKey.Name, mKey.KeyType.ToString(), MakeDdlKeyCreateByAlter(mKey, eTable)); if (CheckKeyTypeForRecreate(eKey)) foreach (var eTable1 in existingSchema.Tables) { var eKey1 = eTable1.GetKey(ConstraintType.KeyForeign, eTable.Name, eKey.Columns); if (eKey1 != null) { AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyDrop(eKey1, eTable1)); AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyCreateByAlter(eKey1, eTable1)); } } } } else AddDdlCommand(i, eTable.Name + @"." + mKey.Name, mKey.KeyType.ToString(), MakeDdlKeyCreateByAlter(mKey, eTable)); } #endregion } i++; } #endregion AfterSchemaComparision(existingSchema, targetSchema); _ddlCommands = Reparse(); _ddlCmdTable.Rows.Clear(); }
/// <summary> /// Препроцесинг схем, детали для разных движков /// </summary> /// <param name="existingSchema">Существующая схема</param> /// <param name="targetSchema">Эталоная схема</param> protected virtual void AfterSchemaComparision(DBSchema existingSchema, DBSchema targetSchema) {}
/// <summary> /// Так как схема - референс тип, создание его глубокой копии. /// Которая при приведение к искомому движку бедет правиться. /// И приведение её к исходному движку /// </summary> /// <param name="schema">Эталоная схема</param> /// <returns></returns> protected abstract DBSchema DbscCopyPrepare(DBSchema schema);
/// <summary> /// Сравнение схемы с эталонной и выдача во внутреннюю хеш таблицу DDL комманд, /// выполнение общих для всех движков действий. /// </summary> public void CompareDbsc(DBSchema mbDbsc, string targetConnStr) { // TODO : эту кашу надо откомментировать и отрефакторить по человечески. var existingSchema = LoadExistingSchema(targetConnStr); var targetSchema = DbscCopyPrepare(mbDbsc); existingSchema.Normalize(); targetSchema.Normalize(); _ddlCmdTable.Rows.Clear(); #region Tables var i = 1; foreach (var eTable in existingSchema.Tables) { if (!targetSchema.IsTableExist(eTable.Name)) { HelperTableDrop(eTable, existingSchema.Tables, i); i++; } } i = 1; foreach (var mTable in targetSchema.Tables) { var eTable = existingSchema.GetTable(mTable.Name); if (eTable == null) { HelperTableCreate(mTable, i); } else if (mTable.GetDiffColumnsCount(eTable) == eTable.Columns.Length) { HelperTableDrop(eTable, existingSchema.Tables, i); HelperTableCreate(mTable, i); } else { #region Column scan foreach (var eColumn in eTable.Columns) { if (!mTable.IsColumnExist(eColumn.Name)) { AddDdlCommand(i, eTable.Name + @"." + eColumn.Name, "Column", MakeDdlColumnDrop(eColumn, eTable)); } } foreach (var mColumn in mTable.Columns) { var eColumn = eTable.GetColumn(mColumn.Name); if (eColumn == null) { AddDdlCommand(i, mTable.Name + @"." + mColumn.Name, "Column", MakeDdlColumnCreate(mColumn, mTable)); } // to post processing in all workers AlterColumn } #endregion #region Indexes scan foreach (var eIndex in eTable.Indexes) { if (!mTable.IsIndexExist(eIndex.Name)) { AddDdlCommand(i, eTable.Name + @"." + eIndex.Name, "Index", MakeDdlIndexDrop(eIndex, eTable)); if (CheckIndexTypeForRecreate(eIndex)) { foreach (var eTable1 in existingSchema.Tables) { var eKey1 = eTable1.GetKey(ConstraintType.KeyForeign, eTable.Name, ParseColumnListIndexClear(eIndex.Columns)); if (eKey1 != null) { AddDdlCommand(i, eTable.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyDrop(eKey1, eTable1)); AddDdlCommand(i, eTable.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyCreateByAlter(eKey1, eTable1)); } } } } } foreach (var mIndex in mTable.Indexes) { if (eTable.IsIndexExist(mIndex.Name)) { var eIndex = eTable.GetIndex(mIndex.Name); if (!eIndex.Equals(mIndex)) { AddDdlCommand(i, eTable.Name + @"." + eIndex.Name, "Index", MakeDdlIndexDrop(eIndex, eTable)); AddDdlCommand(i, eTable.Name + @"." + mIndex, "Index", MakeDdlIndexCreate(mIndex, mTable)); if (CheckIndexTypeForRecreate(eIndex)) { foreach (var eTable1 in existingSchema.Tables) { var eKey1 = eTable1.GetKey(ConstraintType.KeyForeign, eTable.Name, ParseColumnListIndexClear(eIndex.Columns)); if (eKey1 != null) { AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyDrop(eKey1, eTable1)); AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyCreateByAlter(eKey1, eTable1)); } } } } } else { AddDdlCommand(i, mTable.Name + @"." + mIndex.Name, "Index", MakeDdlIndexCreate(mIndex, mTable)); } } #endregion #region Keys scan foreach (var eKey in eTable.Keys) { if (eKey.KeyType == ConstraintType.Default) { continue; } if (!mTable.IsKeyExist(eKey.Name)) { AddDdlCommand(i, eTable.Name + @"." + eKey.Name, eKey.KeyType.ToString(), MakeDdlKeyDrop(eKey, eTable)); if (CheckKeyTypeForRecreate(eKey)) { foreach (var eTable1 in existingSchema.Tables) { var eKey1 = eTable1.GetKey(ConstraintType.KeyForeign, eTable.Name, eKey.Columns); if (eKey1 != null) { AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyDrop(eKey1, eTable1)); AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyCreateByAlter(eKey1, eTable1)); } } } } } foreach (var mKey in mTable.Keys) { if (mKey.KeyType == ConstraintType.Default) { continue; } if (eTable.IsKeyExist(mKey.Name)) { var eKey = eTable.GetKey(mKey.Name); if (!eKey.Equals(mKey)) { AddDdlCommand(i, eTable.Name + @"." + mKey.Name, mKey.KeyType.ToString(), MakeDdlKeyDrop(eKey, eTable)); AddDdlCommand(i, eTable.Name + @"." + mKey.Name, mKey.KeyType.ToString(), MakeDdlKeyCreateByAlter(mKey, eTable)); if (CheckKeyTypeForRecreate(eKey)) { foreach (var eTable1 in existingSchema.Tables) { var eKey1 = eTable1.GetKey(ConstraintType.KeyForeign, eTable.Name, eKey.Columns); if (eKey1 != null) { AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyDrop(eKey1, eTable1)); AddDdlCommand(i, eTable1.Name + @"." + eKey1.Name, eKey1.KeyType.ToString(), MakeDdlKeyCreateByAlter(eKey1, eTable1)); } } } } } else { AddDdlCommand(i, eTable.Name + @"." + mKey.Name, mKey.KeyType.ToString(), MakeDdlKeyCreateByAlter(mKey, eTable)); } } #endregion } i++; } #endregion AfterSchemaComparision(existingSchema, targetSchema); _ddlCommands = Reparse(); _ddlCmdTable.Rows.Clear(); }
/// <summary> /// Препроцесинг схем, детали для разных движков /// </summary> /// <param name="existingSchema">Существующая схема</param> /// <param name="targetSchema">Эталоная схема</param> protected virtual void AfterSchemaComparision(DBSchema existingSchema, DBSchema targetSchema) { }