private void buttonOk_Click(object sender, EventArgs e) { for (int i = this.Controls.Count - 1; i >= 0; i--) { Control control = this.Controls[i]; control.Focus(); if (!Validate()) { DialogResult = DialogResult.None; return; } } if (_mapColumn == null) { Relationship[] relationshipPath = new Relationship[1] { (Relationship)comboBoxRelationship.SelectedItem }; Column column = (Column)comboBoxMapColumn.SelectedItem; _mapColumn = new MapColumn(textBoxName.Text, true, relationshipPath, column, _parent.Columns.Length, column.IsNullable, column.DataType, column.CharacterMaximumLength); _mapColumn.Alias = textBoxAlias.Text; _mapColumn.AliasDisplay = textBoxAliasDisplay.Text; } else { _mapColumn.ForeignColumn = (Column)comboBoxMapColumn.SelectedItem; _mapColumn.Name = textBoxName.Text; _mapColumn.Alias = textBoxAlias.Text; _mapColumn.AliasDisplay = textBoxAliasDisplay.Text; } }
public MapColumn(string name, bool isUserDefined, Relationship[] relationshipPath, Column foreignColumn, int ordinalPosition, bool isNullable, string dataType, int characterMaximumLength, int precision, int scale) : base(name, isUserDefined, relationshipPath == null ? null : relationshipPath[0].Parent, ordinalPosition, isNullable, dataType, characterMaximumLength, false, false, null, true, false, precision, scale) { _IsMapColumn = true; _currentParent = relationshipPath == null ? null : relationshipPath[0].Parent; _foreignColumn = foreignColumn; _relationshipPath = relationshipPath; ResetDefaults(); }
public FormRelationship(Relationship relationship, ScriptObject[] scriptObjects) { InitializeComponent(); ucHeading1.Text = ""; _parent = relationship.Parent; _primaryRelationship = relationship; _scriptObjects = scriptObjects; Controller.ShadeMainForm(); }
public FormRelationship(Relationship relationship, ScriptObject[] scriptObjects) { InitializeComponent(); ucHeading1.Text = ""; TypeOfRelationship = relationship.GetType(); _parent = relationship.Parent; _primaryRelationship = relationship; _scriptObjects = scriptObjects; ArchAngel.Interfaces.Events.ShadeMainForm(); }
public static int GetAliasCount(Relationship[] relationships, string alias) { int count = 0; foreach (Relationship relationship in relationships) { if (relationship.Alias == alias) { count++; } } return count; }
public static Relationship GetRelationship(Relationship[] relationships, string name, bool throwException) { foreach (Relationship relationship in relationships) { if (relationship.Name == name) { return relationship; } } if (throwException) { throw new Exception("Cannot find relationship " + name); } return null; }
public override string AliasPluralDefault(Relationship relationship) { return AliasPluralDefault((OneToManyRelationship)relationship); }
public static Relationship GetRelationshipByRelationshipAlias(Relationship[] relationships, string alias) { foreach (Relationship relationship in relationships) { if (relationship.Alias == alias) { return relationship; } } return null; }
public static Relationship GetRelationship(Relationship[] relationships, string name) { foreach (Relationship relationship in relationships) { if (relationship.Name == name) { return relationship; } } return null; }
public bool NameValidate(Relationship relationship, out string failReason) { failReason = ""; /*Don't check items that are not enabled*/ if (!relationship.Enabled) { return true; } if (string.IsNullOrEmpty(relationship.Name)) { failReason = "Name cannot be zero-length."; return false; } if (relationship.Name.IndexOf(" ") >= 0) { failReason = "Name cannot have spaces."; return false; } return true; }
public static Relationship[] GetUserDefinedRelationships(Relationship[] relationships) { List<Relationship> userDefinedRelationships = new List<Relationship>(); foreach (Relationship relationship in relationships) { if (relationship.IsUserDefined) { userDefinedRelationships.Add(relationship); } } return userDefinedRelationships.ToArray(); }
public void RemoveRelationship(Relationship relationship) { Interfaces.Events.RaiseDataChangedEvent(GetType(), (MethodInfo)MethodBase.GetCurrentMethod(), relationship, null); switch (relationship.GetType().Name) { case "OneToOneRelationship": _oneToOneRelationships.Remove((OneToOneRelationship)relationship); break; case "OneToManyRelationship": _oneToManyRelationships.Remove((OneToManyRelationship)relationship); break; case "ManyToOneRelationship": _manyToOneRelationships.Remove((ManyToOneRelationship)relationship); break; case "ManyToManyRelationship": _manyToManyRelationships.Remove((ManyToManyRelationship)relationship); break; default: throw new NotImplementedException("This relationship type has not been coded yet."); } }
/// <summary> /// Updates objects that have just been read from the database with any changes the user made, such as renaming. /// </summary> /// <param name="newDatabase"></param> /// <param name="oldDatabase"></param> /// <param name="processViews"></param> /// <param name="processStoredProcedures"></param> /// <param name="processTables"></param> /// <returns>Returns a description of any errors that occurred or alerts that the user needs to be aware of.</returns> private string UpdateScriptObjects( Model.Database newDatabase, Model.Database oldDatabase, bool processTables, bool processViews, bool processStoredProcedures) { oldDatabase.SnapshotMode = true; StringBuilder sb = new StringBuilder(1000); #region Script Objects // Find old user defined script objects and add to new database ScriptObject[] userDefinedScriptObjects = GetUserDefinedScriptObjects(oldDatabase.AllScriptObjects); foreach (ScriptObject scriptObject in userDefinedScriptObjects) { newDatabase.AddScriptObject(scriptObject); Interfaces.Events.RaiseObjectBeingProcessedEvent(scriptObject.Name, "Script Object"); //RaiseObjectBeingProcessedEvent(scriptObject.Name, "Script Object"); } #endregion #region Force update of the cached ScriptObjects oldDatabase.ResetAllScriptObjects(); ScriptObject[] ScriptObjects = oldDatabase.AllScriptObjects; newDatabase.SnapshotMode = true; newDatabase.ResetAllScriptObjects(); #endregion #region Columns Interfaces.Events.RaiseObjectBeingProcessedEvent("", "Updating Columns"); // Update new script objects with old columns foreach (ScriptObject newScriptObject in newDatabase.AllScriptObjects) { if ((newScriptObject.IsTable && !processTables) || (newScriptObject.IsStoredProcedure && !processStoredProcedures) || (newScriptObject.IsView && !processViews)) { continue; } // Find old script object if it exists ScriptObject oldScriptObject = Search.GetScriptObject(oldDatabase.AllScriptObjects, newScriptObject, newScriptObject.GetType(), false); if (oldScriptObject == null || oldScriptObject.IsUserDefined) { continue; } // Find old user defined columns and add to script object Column[] userDefinedColumns = GetUserDefinedColumns(oldScriptObject.Columns); foreach (Column oldColumn in userDefinedColumns) { if (oldColumn.GetType() == typeof(Column)) { Interfaces.Events.RaiseObjectBeingProcessedEvent(oldColumn.Name, "Column"); //RaiseObjectBeingProcessedEvent(oldColumn.Name, "Column"); Column newColumn = new Column(oldColumn.Name, oldColumn.IsUserDefined, newScriptObject, newScriptObject.Columns.Length, oldColumn.IsNullable, oldColumn.DataType, oldColumn.Size, oldColumn.InPrimaryKey, oldColumn.IsIdentity, oldColumn.Default, oldColumn.ReadOnly, oldColumn.IsCalculated, oldColumn.Precision, oldColumn.Scale); newColumn.Alias = oldColumn.Alias; newColumn.AliasDisplay = oldColumn.AliasDisplay; newScriptObject.AddColumn(newColumn); } } if (newScriptObject.GetType() == typeof(Model.StoredProcedure) && oldScriptObject.Enabled) { // Execute Stored Procedure if enabled and retrieve columns FillStoredProcedureColumns((Model.StoredProcedure)newScriptObject); } foreach (string error in newScriptObject.Errors) { sb.AppendLine(error); } } #endregion #region Map Columns Interfaces.Events.RaiseObjectBeingProcessedEvent("", "Updating Map Columns"); // Update new script objects with old properties foreach (ScriptObject newScriptObject in newDatabase.AllScriptObjects) { if ((newScriptObject.IsTable && !processTables) || (newScriptObject.IsStoredProcedure && !processStoredProcedures) || (newScriptObject.IsView && !processViews)) { continue; } // Find old script object if it exists ScriptObject oldScriptObject = Search.GetScriptObject(oldDatabase.AllScriptObjects, newScriptObject, newScriptObject.GetType(), false); if (oldScriptObject == null) { continue; } // Find old user defined columns and add to script object Column[] userDefinedColumns = GetUserDefinedColumns(oldScriptObject.Columns); foreach (Column oldColumn in userDefinedColumns) { if (ModelTypes.MapColumn.IsInstanceOfType(oldColumn)) { Interfaces.Events.RaiseObjectBeingProcessedEvent(oldColumn.Name, "Map Column"); //RaiseObjectBeingProcessedEvent(oldColumn.Name, "Map Column"); MapColumn oldMapColumn = (MapColumn)oldColumn; // We insert a null RelationshipPath here. Don't worry, because we wire up the correct ones below in the region called 'Update Map Column Relationship Path and Foreign Column' MapColumn newMapColumn = new MapColumn(oldMapColumn.Name, oldMapColumn.IsUserDefined, null, null, newScriptObject.Columns.Length, oldMapColumn.IsNullable, oldMapColumn.DataType, oldMapColumn.Size, oldMapColumn.Precision, oldMapColumn.Scale); newMapColumn.Alias = oldMapColumn.Alias; newMapColumn.AliasDisplay = oldMapColumn.AliasDisplay; if (oldMapColumn.RelationshipPath != null) { //foreach (Relationship rel in oldMapColumn.RelationshipPath) //{ // Relationship newRel = Search.GetRelationship(newScriptObject.Relationships, rel.Name, false); //} } //newMapColumn.Parent = newScriptObject; newScriptObject.AddColumn(newMapColumn); } } } #endregion #region Key and Index ArchAngel.Interfaces.Events.RaiseObjectBeingProcessedEvent("", "Updating Keys and Indexes"); if (processTables) { // Update new script objects with old properties foreach (Model.Table newTable in newDatabase.Tables) { // Find old script object if it exists Model.Table oldTable = Search.GetTable(oldDatabase.Tables, newTable, false); if (oldTable == null) { continue; } // Find old user defined keys Key[] userDefinedKeys = GetUserDefinedKeys(oldTable.Keys); foreach (Key oldKey in userDefinedKeys) { Interfaces.Events.RaiseObjectBeingProcessedEvent(oldKey.Name, "User-Defined Key"); // TODO: Add new key //Key newKey = new Key(oldKey.Name, oldKey.Alias, oldKey.IsUserDefined, oldKey.AliasDisplay, newScriptObject, newScriptObject.Keys.Length, oldKey.IsNullable, oldKey.DataType, oldKey.Size, oldKey.InPrimaryKey, oldKey.IsMap, oldKey.IsIdentity); //newTable.AddKey(newKey); } // Find old user defined indexes Index[] userDefinedIndexes = GetUserDefinedIndexes(oldTable.Indexes); foreach (Index oldIndex in userDefinedIndexes) { Interfaces.Events.RaiseObjectBeingProcessedEvent(oldIndex.Name, "User-Defined Index"); // TODO: Add new index //Index newIndex = new Index(oldIndex.Name, oldIndex.Alias, oldIndex.IsUserDefined, oldIndex.AliasDisplay, newScriptObject, newScriptObject.Indexs.Length, oldIndex.IsNullable, oldIndex.DataType, oldIndex.Size, oldIndex.InPrimaryIndex, oldIndex.IsMap, oldIndex.IsIdentity); //newTable.AddIndex(newIndex); } } } #endregion #region Filter Interfaces.Events.RaiseObjectBeingProcessedEvent("", "Updating Filters"); // Update new script objects with old properties foreach (ScriptObject newScriptObject in newDatabase.AllScriptObjects) { if ((newScriptObject.IsTable && !processTables) || (newScriptObject.IsStoredProcedure && !processStoredProcedures) || (newScriptObject.IsView && !processViews)) { continue; } // Find old script object if it exists ScriptObject oldScriptObject = Search.GetScriptObject(oldDatabase.AllScriptObjects, newScriptObject, newScriptObject.GetType(), false); if (oldScriptObject == null || oldScriptObject.IsUserDefined) { continue; } // Find old user defined filters Filter[] userDefinedFilters = GetUserDefinedFilters(oldScriptObject.Filters); foreach (Filter oldFilter in userDefinedFilters) { Interfaces.Events.RaiseObjectBeingProcessedEvent(oldFilter.Name, "Filter"); Filter newFilter = new Filter(oldFilter.Name, oldFilter.IsUserDefined, newScriptObject, oldFilter.IsReturnTypeCollection, oldFilter.CreateStoredProcedure, oldFilter.UseCustomWhere, oldFilter.CustomWhere, oldFilter.Key); newFilter.Alias = oldFilter.Alias; newFilter.Name = oldFilter.Name; newFilter.CustomWhere = oldFilter.CustomWhere; newFilter.CreateStoredProcedure = oldFilter.CreateStoredProcedure; newFilter.Enabled = oldFilter.Enabled; newFilter.IsReturnTypeCollection = oldFilter.IsReturnTypeCollection; foreach (Filter.FilterColumn oldFilterColumn in oldFilter.FilterColumns) { Column newFilterColumnColumn = GetNewScriptBase(newDatabase.AllScriptObjects, oldFilter, oldFilterColumn.Column); Filter.FilterColumn newFilterColumn = new Filter.FilterColumn(newFilterColumnColumn, oldFilterColumn.LogicalOperator, oldFilterColumn.CompareOperator, oldFilterColumn.Alias); newFilter.AddFilterColumn(newFilterColumn); } foreach (Filter.OrderByColumn oldOrderByColumn in oldFilter.OrderByColumns) { Column newOrderByColumnColumn = GetNewScriptBase(newDatabase.AllScriptObjects, oldFilter, oldOrderByColumn.Column); Filter.OrderByColumn newOrderByColumn = new Filter.OrderByColumn(newOrderByColumnColumn, oldOrderByColumn.SortOperator); newFilter.AddOrderByColumn(newOrderByColumn); } newScriptObject.AddFilter(newFilter); } } #endregion #region Relationship Interfaces.Events.RaiseObjectBeingProcessedEvent("", "Updating Relationships"); // Update new script objects with old properties foreach (ScriptObject newScriptObject in newDatabase.AllScriptObjects) { if ((newScriptObject.IsTable && !processTables) || (newScriptObject.IsStoredProcedure && !processStoredProcedures) || (newScriptObject.IsView && !processViews)) { continue; } // Find old script object if it exists ScriptObject oldScriptObject = Search.GetScriptObject(oldDatabase.AllScriptObjects, newScriptObject, newScriptObject.GetType(), false); if (oldScriptObject == null)// || oldScriptObject.IsUserDefined) { continue; } // Find old user defined relationships Relationship[] userDefinedRelationships = GetUserDefinedRelationships(oldScriptObject.Relationships); foreach (Relationship oldRelationship in userDefinedRelationships) { if (oldScriptObject.IsUserDefined) { oldScriptObject.RemoveRelationship(oldRelationship); } Interfaces.Events.RaiseObjectBeingProcessedEvent(oldRelationship.Name, "Relationship"); // Find relationship script objects in new database schema ScriptObject newPrimaryScriptObject = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship.PrimaryScriptObject); ScriptObject newForeignScriptObject = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship.ForeignScriptObject); if (newPrimaryScriptObject == null || newForeignScriptObject == null) { // The corresponding ScriptObjects have been deleted, so remove the Relationship // TODO: inform the user about Relationships that have been removed, and the reason. continue; } // Find relationship columns in new databse schema Column[] newPrimaryColumns = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship, oldRelationship.PrimaryColumns); Column[] newForeignColumns = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship, oldRelationship.ForeignColumns); if (oldRelationship.GetType() == typeof(OneToOneRelationship)) { // Find relationship filter in new databse schema Filter newFilter = GetNewScriptBase(newScriptObject, oldRelationship, oldRelationship.Filter); OneToOneRelationship oldOneToOneRelationship = (OneToOneRelationship)oldRelationship; OneToOneRelationship newRelationship = new OneToOneRelationship(oldOneToOneRelationship.Name, oldOneToOneRelationship.IsUserDefined, newPrimaryScriptObject, newPrimaryColumns, newForeignScriptObject, newForeignColumns, newFilter, oldOneToOneRelationship.IsBase); Relationship foreignRelationship = null; if (oldScriptObject.IsUserDefined) { foreignRelationship = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship.ForeignRelationship); } if (oldRelationship.ForeignScriptObject.IsUserDefined) { foreignRelationship = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship.ForeignRelationship); } if (foreignRelationship != null) { newRelationship.ForeignRelationship = foreignRelationship; foreignRelationship.ForeignRelationship = newRelationship; } newRelationship.Alias = oldOneToOneRelationship.Alias; newScriptObject.AddRelationship(newRelationship); } if (oldRelationship.GetType() == typeof(OneToManyRelationship)) { // Find relationship filter in new databse schema Filter newFilter = GetNewScriptBase(newScriptObject, oldRelationship, oldRelationship.Filter); OneToManyRelationship oldOneToManyRelationship = (OneToManyRelationship)oldRelationship; OneToManyRelationship newRelationship = new OneToManyRelationship(oldOneToManyRelationship.Name, oldOneToManyRelationship.IsUserDefined, newPrimaryScriptObject, newPrimaryColumns, newForeignScriptObject, newForeignColumns, newFilter); newRelationship.Alias = oldOneToManyRelationship.Alias; newScriptObject.AddRelationship(newRelationship); } if (oldRelationship.GetType() == typeof(ManyToOneRelationship)) { // Find relationship filter in new databse schema Filter newFilter = GetNewScriptBase(newScriptObject, oldRelationship, oldRelationship.Filter); ManyToOneRelationship oldManyToOneRelationship = (ManyToOneRelationship)oldRelationship; ManyToOneRelationship newRelationship = new ManyToOneRelationship(oldManyToOneRelationship.Name, oldManyToOneRelationship.IsUserDefined, newPrimaryScriptObject, newPrimaryColumns, newForeignScriptObject, newForeignColumns, newFilter); newRelationship.Alias = oldManyToOneRelationship.Alias; newScriptObject.AddRelationship(newRelationship); } //if (oldRelationship.GetType() == typeof(ManyToManyRelationship)) //{ // ManyToManyRelationship oldManyToManyRelationship = (ManyToManyRelationship)oldRelationship; //} } } #endregion #region Update Foreign Relationship Interfaces.Events.RaiseObjectBeingProcessedEvent("", "Updating Foreign Relationships"); foreach (ScriptObject newScriptObject in newDatabase.AllScriptObjects) { if ((newScriptObject.IsTable && !processTables) || (newScriptObject.IsStoredProcedure && !processStoredProcedures) || (newScriptObject.IsView && !processViews)) { continue; } // Find old script object if it exists ScriptObject oldScriptObject = Search.GetScriptObject(oldDatabase.AllScriptObjects, newScriptObject, newScriptObject.GetType(), false); if (oldScriptObject == null)// || oldScriptObject.IsUserDefined) { continue; } // Hookup foreign relationships // Find old user defined relationships Relationship[] userDefinedRelationships = GetUserDefinedRelationships(oldScriptObject.Relationships); for (int relationshipCounter = userDefinedRelationships.Length - 1; relationshipCounter >= 0; relationshipCounter--) { Relationship oldRelationship = userDefinedRelationships[relationshipCounter]; if (oldRelationship.GetType() == typeof(ManyToManyRelationship)) { continue; } Interfaces.Events.RaiseObjectBeingProcessedEvent(oldRelationship.Name, "User-Defined Relationship"); ScriptObject newPrimaryScriptObject = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship.PrimaryScriptObject); ScriptObject newForeignScriptObject = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship.ForeignScriptObject); if (newPrimaryScriptObject == null || newForeignScriptObject == null) { // The corresponding ScriptObjects have been deleted, so remove the Relationship // TODO: inform the user about Relationships that have been removed, and the reason. continue; } Relationship newPrimaryRelationship = GetNewScriptBase(newPrimaryScriptObject, oldRelationship); Relationship newForeignRelationship = GetNewScriptBase(newForeignScriptObject, oldRelationship.ForeignRelationship); if (newPrimaryRelationship == null || newForeignRelationship == null) { // The corresponding ScriptObjects have been deleted, so remove the Relationship // TODO: inform the user about Relationships that have been removed, and the reason. continue; } newPrimaryRelationship.ForeignRelationship = newForeignRelationship; newForeignRelationship.ForeignRelationship = newPrimaryRelationship; } } #endregion #region Create Many to Many Relationship ArchAngel.Interfaces.Events.RaiseObjectBeingProcessedEvent("", "Updating Many to Many Relationships"); foreach (ScriptObject newScriptObject in newDatabase.AllScriptObjects) { if ((newScriptObject.IsTable && !processTables) || (newScriptObject.IsStoredProcedure && !processStoredProcedures) || (newScriptObject.IsView && !processViews)) { continue; } // Find old script object if it exists ScriptObject oldScriptObject = Search.GetScriptObject(oldDatabase.AllScriptObjects, newScriptObject, newScriptObject.GetType(), false); if (oldScriptObject == null) { continue; } // Find old user defined relationships Relationship[] userDefinedRelationships = GetUserDefinedRelationships(oldScriptObject.Relationships); foreach (Relationship oldRelationship in userDefinedRelationships) { // Add userdefined may to many relationships here as they are based on other relationships that must all be loaded if (oldRelationship.GetType() == typeof(ManyToManyRelationship)) { Interfaces.Events.RaiseObjectBeingProcessedEvent(oldRelationship.Name, "Many-To-Many Relationship"); //ScriptObject newPrimaryScriptObject = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship, oldRelationship.PrimaryScriptObject); //ScriptObject newForeignScriptObject = GetNewScriptBase(newDatabase.AllScriptObjects, oldRelationship, oldRelationship.ForeignScriptObject); Filter newFilter = GetNewScriptBase(newScriptObject, oldRelationship, oldRelationship.Filter); ManyToManyRelationship oldManyToManyRelationship = (ManyToManyRelationship)oldRelationship; ScriptObject newIntermediateScriptObject = GetNewScriptBase(newDatabase.AllScriptObjects, oldManyToManyRelationship.IntermediateForeignScriptObject); OneToManyRelationship newIntermediatePrimaryRelationship = (OneToManyRelationship)GetNewScriptBase(newScriptObject, oldManyToManyRelationship.IntermediatePrimaryRelationship); ManyToOneRelationship newIntermediateForeignRelationship = (ManyToOneRelationship)GetNewScriptBase(newIntermediateScriptObject, oldManyToManyRelationship.IntermediateForeignRelationship); ManyToManyRelationship newRelationship = new ManyToManyRelationship(oldManyToManyRelationship.Name, oldManyToManyRelationship.IsUserDefined, newIntermediatePrimaryRelationship, newIntermediateForeignRelationship, newFilter); newRelationship.Alias = oldManyToManyRelationship.Alias; newScriptObject.AddRelationship(newRelationship); } } } #endregion #region Update Remaining Properties // Update new script objects with old properties foreach (ScriptObject newScriptObject in newDatabase.AllScriptObjects) { if ((newScriptObject.IsTable && !processTables) || (newScriptObject.IsStoredProcedure && !processStoredProcedures) || (newScriptObject.IsView && !processViews)) { continue; } // Find old script object if it exists ScriptObject oldScriptObject = Search.GetScriptObject(oldDatabase.AllScriptObjects, newScriptObject, newScriptObject.GetType(), false); if (oldScriptObject == null) { continue; } Interfaces.Events.RaiseObjectBeingProcessedEvent(oldScriptObject.Name, "Update Remaining Properties"); // Update new script object newScriptObject.Enabled = oldScriptObject.Enabled; newScriptObject.Alias = oldScriptObject.Alias; newScriptObject.AliasPlural = oldScriptObject.AliasPlural; newScriptObject.Ex = oldScriptObject.Ex; // Update new columns with old properties foreach (Column newColumn in newScriptObject.Columns) { // Find old column if it exists Column oldColumn; if (ModelTypes.MapColumn.IsInstanceOfType(newColumn)) { // Lookup MapColumns by using Alias as well, otherwise we have a problem when multiple MapColumns link to the // same foreign column. oldColumn = Search.GetColumn(oldScriptObject.Columns, newColumn.Name, newColumn.Alias, false); } else { oldColumn = Search.GetColumn(oldScriptObject.Columns, newColumn.Name, false); } if (oldColumn == null) { continue; } // Update new column newColumn.Enabled = oldColumn.Enabled; newColumn.IsNullable = oldColumn.IsNullable; newColumn.Alias = oldColumn.Alias; newColumn.AliasDisplay = oldColumn.AliasDisplay; newColumn.Ex = oldColumn.Ex; newColumn.Parent = Search.GetScriptObject(newDatabase.AllScriptObjects, oldColumn.Parent, oldColumn.Parent.GetType(), false); } // TODO: the code below should be refactored to use the Search class instead of traversing the individual elements. Eg: Search.GetFilterColumns // This will reduce the amount of duplicate code. #region Update new filters with old properties foreach (Filter newFilter in newScriptObject.Filters) { #region Find old filter if it exists Filter oldFilter = Search.GetFilter(oldScriptObject.Filters, newFilter.Name, false); if (oldFilter == null) { oldFilter = Search.GetFilter(oldScriptObject.Filters, newFilter); } if (oldFilter == null) { continue; } #endregion SyncFilterProperties(sb, newFilter, oldFilter); } #endregion #region Update new relationships with old properties foreach (Relationship newRelationship in newScriptObject.Relationships) { Relationship oldRelationship = Search.GetRelationship(oldScriptObject.Relationships, newRelationship.Name, false); if (oldRelationship == null) { continue; } // Update new relationship newRelationship.Enabled = oldRelationship.Enabled; newRelationship.Alias = oldRelationship.Alias; newRelationship.Ex = oldRelationship.Ex; } #endregion if (newScriptObject.GetType() == typeof(Model.Table)) { Model.Table oldTable = (Model.Table)oldScriptObject; Model.Table newTable = (Model.Table)newScriptObject; // Update new keys with old properties foreach (Key newKey in newTable.Keys) { Key oldKey = Search.GetKey(oldTable.Keys, newKey.Name, false); if (oldKey == null) { continue; } // Update new key newKey.Enabled = oldKey.Enabled; newKey.Alias = oldKey.Alias; newKey.Ex = oldKey.Ex; } // Update new indexes with old properties foreach (Index newIndex in newTable.Indexes) { Index oldIndex = Search.GetIndex(oldTable.Indexes, newIndex.Name, false); if (oldIndex == null) { continue; } // Update new index newIndex.Enabled = oldIndex.Enabled; newIndex.Alias = oldIndex.Alias; newIndex.Ex = oldIndex.Ex; } } } #endregion #region Update Map Column Relationship Path and Foreign Column Interfaces.Events.RaiseObjectBeingProcessedEvent("", "Updating Map Columns Relationships"); // Update new script objects with old properties foreach (ScriptObject newScriptObject in newDatabase.AllScriptObjects) { if ((newScriptObject.IsTable && !processTables) || (newScriptObject.IsStoredProcedure && !processStoredProcedures) || (newScriptObject.IsView && !processViews)) { continue; } // Find old script object if it exists ScriptObject oldScriptObject = Search.GetScriptObject(oldDatabase.AllScriptObjects, newScriptObject, newScriptObject.GetType(), false); if (oldScriptObject == null) { continue; } // Find old user defined columns and add to script object Column[] userDefinedColumns = GetUserDefinedColumns(oldScriptObject.Columns); //foreach (Column oldColumn in userDefinedColumns) for (int columnCounter = userDefinedColumns.Length - 1; columnCounter >= 0; columnCounter--) { Column oldColumn = userDefinedColumns[columnCounter]; if (ModelTypes.MapColumn.IsInstanceOfType(oldColumn)) { //ArchAngel.Interfaces.ProjectHelper.RaiseObjectBeingProcessedEvent(oldColumn.Name, "User-Defined Column"); MapColumn oldMapColumn = (MapColumn)oldColumn; // Find foreign column's parent in new database schema ScriptObject newForeignScriptObject = null; if (oldMapColumn.ForeignColumn != null) { newForeignScriptObject = GetNewScriptBase(newDatabase.AllScriptObjects, oldMapColumn.ForeignColumn.Parent); } else { oldScriptObject.RemoveColumn(oldColumn); continue; } // Find foreign column in new database schema Column newForeignColumn = GetNewScriptBase(newDatabase.AllScriptObjects, oldMapColumn, oldMapColumn.ForeignColumn); Relationship[] newRelationshipPath = new Relationship[oldMapColumn.RelationshipPath.Length]; for (int i = 0; i < oldMapColumn.RelationshipPath.Length; i++) { Relationship newRelationship = GetNewScriptBase(newScriptObject, oldMapColumn.RelationshipPath[i]); newRelationshipPath[i] = newRelationship; } MapColumn newMapColumn = (MapColumn)Search.GetColumn(newScriptObject.Columns, oldMapColumn.Name, oldMapColumn.Alias); newMapColumn.RelationshipPath = newRelationshipPath; newMapColumn.ForeignColumn = newForeignColumn; } } } #endregion newDatabase.SnapshotMode = false; oldDatabase.SnapshotMode = false; return sb.ToString(); }
private static Relationship GetNewScriptBase(ScriptObject[] newScriptObjects, Relationship oldRelationship) { ScriptObject newScriptObject = GetNewScriptBase(newScriptObjects, oldRelationship.Parent); Relationship newRelationship = Search.GetRelationship(newScriptObject.Relationships, oldRelationship.Name, false); //if (newRelationship == null) //{ // string errorMessage = oldImmediateParent.GetType().Name + " " + oldImmediateParent.Alias + " references deleted relationship " + oldRelationship.Alias + " of script object " + oldRelationship.Parent.Alias + // ".\n" + oldRelationship.GetType().Name + " " + oldRelationship.Alias + " does not exist anymore." + // "\nPlease delete " + oldRelationship.GetType().Name + " " + oldRelationship.Alias + " from this " + oldImmediateParent.GetType().Name; // throw new Exception(errorMessage); //} return newRelationship; }
private static Relationship GetNewScriptBase(ScriptObject newScriptObject, Relationship oldRelationship) { Relationship newRelationship = Search.GetRelationship(newScriptObject.Relationships, oldRelationship.Name, false); //if (newRelationship == null) //{ // string errorMessage = string.Format("{0} {1} references deleted relationship {2} of script object {3}.\n {4} does not exist anymore.\nPlease delete {5} {6} from this {7}.", // oldImmediateParent.GetType().Name, // oldImmediateParent.Alias, // oldRelationship.Alias, // oldRelationship.Parent.Alias, // oldRelationship.GetType().Name, // oldRelationship.Alias, // oldRelationship.GetType().Name, // oldRelationship.Alias, // oldImmediateParent.GetType().Name); // throw new Exception(errorMessage); //} return newRelationship; }
public bool AliasPluralValidate(Relationship relationship, out string failReason) { failReason = ""; return true; }
public override string AliasPluralDefault(Relationship relationship) { return(AliasPluralDefault((ManyToOneRelationship)relationship)); }
internal static bool IsValidFilter(Relationship relationship, out string failReason) { failReason = ""; if (!relationship.Enabled) { return true; } if (ManyToManyType.IsInstanceOfType(relationship)) { return IsValidManyToManyFilter((ManyToManyRelationship)relationship, out failReason); } if (relationship.Filter == null) { failReason = string.Format("Relationship [{0}] has no filter.", relationship.Alias); return false; } for (int columnCounter = 0; columnCounter < relationship.PrimaryColumns.Length; columnCounter++) { bool found = false; for (int i = 0; i < relationship.Filter.FilterColumns.Length; i++) { if (relationship.Filter.FilterColumns[i].Column == relationship.PrimaryColumns[columnCounter]) { found = true; break; } } if (!found) { failReason = string.Format("{0}: Filter doesn't contain all necessary Relationship columns.\n", relationship.Filter.Alias); return false; } } return true; }
public static Relationship GetRelationship(Relationship[] relationships, string name) { return GetRelationship(relationships, name, true); }
public static Relationship[] GetEnabledRelationships(Relationship[] relationships) { List<Relationship> enabledRelationships = new List<Relationship>(); foreach (Relationship relationship in relationships) { if (relationship.Enabled) { enabledRelationships.Add(relationship); } } return enabledRelationships.ToArray(); }
public virtual string AliasPluralDefault(Relationship relationship) { return ArchAngel.Providers.Database.Helper.Script.GetPlural(relationship.Alias); }
private void buttonOk_Click(object sender, EventArgs e) { for (int i = this.Controls.Count - 1; i >= 0; i--) { Control control = this.Controls[i]; control.Focus(); // First tab tabStripRelationship.SelectedIndex = 0; if (!ValidateControl(control)) { DialogResult = DialogResult.None; return; } // Second tab if (radioButtonOneToOne.Checked || radioButtonOneToMany.Checked || radioButtonManyToOne.Checked) { tabStripRelationship.SelectedIndex = 1; if (!ValidateControl(control)) { DialogResult = DialogResult.None; return; } } } Column[] primaryColumns = new Column[listViewPrimaryColumn.Items.Count]; for (int i = 0; i < listViewPrimaryColumn.Items.Count; i++) { ListViewItem listViewItem = listViewPrimaryColumn.Items[i]; primaryColumns[i] = (Column)listViewItem.Tag; } Column[] foreignColumns = new Column[listViewForeignColumn.Items.Count]; for (int i = 0; i < listViewForeignColumn.Items.Count; i++) { ListViewItem listViewItem = listViewForeignColumn.Items[i]; foreignColumns[i] = (Column)listViewItem.Tag; } if (_primaryRelationship == null) { ScriptObject primaryScriptObject = (ScriptObject)comboBoxPrimaryScriptObject.SelectedItem; ScriptObject foreignScriptObject = (ScriptObject)comboBoxForeignScriptObject.SelectedItem; Filter primaryFilter = (Filter)comboBoxPrimaryFilter.SelectedItem; Filter foreignFilter = (Filter)comboBoxForeignFilter.SelectedItem; if (radioButtonOneToOne.Checked) { _primaryRelationship = new OneToOneRelationship(textBoxName.Text, true, _parent, primaryColumns, foreignScriptObject, foreignColumns, primaryFilter, checkBoxIsBase.Checked); _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _foreignRelationship = new OneToOneRelationship(primaryScriptObject.Alias, true, foreignScriptObject, foreignColumns, _parent, primaryColumns, foreignFilter, !checkBoxIsBase.Checked); _foreignRelationship.Alias = primaryScriptObject.Alias; } if (radioButtonOneToMany.Checked) { _primaryRelationship = new OneToManyRelationship(textBoxName.Text, true, _parent, primaryColumns, foreignScriptObject, foreignColumns, primaryFilter); _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _foreignRelationship = new ManyToOneRelationship(primaryScriptObject.Alias, true, foreignScriptObject, foreignColumns, _parent, primaryColumns, foreignFilter); _foreignRelationship.Alias = primaryScriptObject.Alias; } if (radioButtonManyToOne.Checked) { _primaryRelationship = new ManyToOneRelationship(textBoxName.Text, true, _parent, primaryColumns, foreignScriptObject, foreignColumns, primaryFilter); _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _foreignRelationship = new OneToManyRelationship(primaryScriptObject.AliasPlural, true, foreignScriptObject, foreignColumns, _parent, primaryColumns, foreignFilter); _foreignRelationship.Alias = primaryScriptObject.AliasPlural; } if (radioButtonManyToMany.Checked) { OneToManyRelationship oneToManyRelationship = (OneToManyRelationship)comboBoxIntermediatePrimaryRelationship.SelectedItem; ManyToOneRelationship manyToOneRelationship = (ManyToOneRelationship)comboBoxIntermediateForeignRelationship.SelectedItem; _primaryRelationship = new ManyToManyRelationship(textBoxName.Text, true, oneToManyRelationship, manyToOneRelationship, oneToManyRelationship.Filter); _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _foreignRelationship = new ManyToManyRelationship(oneToManyRelationship.Parent.AliasPlural, true, (OneToManyRelationship)manyToOneRelationship.ForeignRelationship, (ManyToOneRelationship)oneToManyRelationship.ForeignRelationship, manyToOneRelationship.ForeignRelationship.Filter); _foreignRelationship.Alias = oneToManyRelationship.Parent.AliasPlural; } _primaryRelationship.ForeignRelationship = _foreignRelationship; _foreignRelationship.ForeignRelationship = _primaryRelationship; } else { _primaryRelationship.Name = textBoxName.Text; _primaryRelationship.Alias = textBoxAlias.Text; _primaryRelationship.Description = textBoxDescription.Text; _primaryRelationship.PrimaryColumns = primaryColumns; _primaryRelationship.ForeignScriptObject = (ScriptObject)comboBoxForeignScriptObject.SelectedItem; _primaryRelationship.ForeignColumns = foreignColumns; _primaryRelationship.Filter = (Filter)comboBoxPrimaryFilter.SelectedItem; _primaryRelationship.ForeignRelationship.Filter = (Filter)comboBoxForeignFilter.SelectedItem; if (_primaryRelationship.GetType() == typeof(ManyToManyRelationship)) { ManyToManyRelationship manyToManyRelationship = (ManyToManyRelationship)_primaryRelationship; manyToManyRelationship.IntermediatePrimaryRelationship = (OneToManyRelationship)comboBoxIntermediatePrimaryRelationship.SelectedItem; manyToManyRelationship.IntermediateForeignRelationship = (ManyToOneRelationship)comboBoxIntermediateForeignRelationship.SelectedItem; } } }
public bool AliasValidate(Relationship relationship, out string failReason) { failReason = ""; if (relationship.Parent == null) { failReason = "Missing parent: " + relationship.Alias; return false; } foreach (Relationship sibling in relationship.Parent.Relationships) { if (sibling != relationship && sibling.Enabled && ArchAngel.Providers.Database.Helper.Script.StringsAreEqual(sibling.Alias, relationship.Alias, false)) { failReason = "Duplicate alias: " + relationship.Alias; return false; } } return true; }
public void UpdateRelationship(int index, Relationship relationship) { switch (relationship.GetType().Name) { case "OneToOneRelationship": Interfaces.Events.RaiseDataChangedEvent(GetType(), (MethodInfo)MethodBase.GetCurrentMethod(), _oneToOneRelationships[index], relationship); _oneToOneRelationships[index] = (OneToOneRelationship)relationship; break; case "OneToManyRelationship": Interfaces.Events.RaiseDataChangedEvent(GetType(), (MethodInfo)MethodBase.GetCurrentMethod(), _oneToManyRelationships[index], relationship); _oneToManyRelationships[index] = (OneToManyRelationship)relationship; break; case "ManyToOneRelationship": Interfaces.Events.RaiseDataChangedEvent(GetType(), (MethodInfo)MethodBase.GetCurrentMethod(), _manyToOneRelationships[index], relationship); _manyToOneRelationships[index] = (ManyToOneRelationship)relationship; break; case "ManyToManyRelationship": Interfaces.Events.RaiseDataChangedEvent(GetType(), (MethodInfo)MethodBase.GetCurrentMethod(), _manyToManyRelationships[index], relationship); _manyToManyRelationships[index] = (ManyToManyRelationship)relationship; break; default: throw new NotImplementedException("This relationship type has not been coded yet."); } }
public string AliasDefault(Relationship relationship) { return ArchAngel.Providers.Database.Helper.Script.GetSingular(ArchAngel.Providers.Database.Helper.Script.GetSingleWord(relationship.Name.Trim())); }