Exemplo n.º 1
0
 public ManyToManyRelationship(string name, bool isUserDefined, OneToManyRelationship primaryRelationship, ManyToOneRelationship foreignRelationship, Filter filter)
     : base(name, isUserDefined, primaryRelationship.Parent, primaryRelationship.Parent, primaryRelationship.PrimaryColumns, foreignRelationship.ForeignRelationship.Parent, foreignRelationship.ForeignRelationship.PrimaryColumns, filter)
 {
     _intermediatePrimaryRelationship = primaryRelationship;
     _intermediateForeignRelationship = foreignRelationship;
     ResetDefaults();
 }
 public ManyToManyRelationship(string name, bool isUserDefined, OneToManyRelationship primaryRelationship, ManyToOneRelationship foreignRelationship, Filter filter)
     : base(name, isUserDefined, primaryRelationship.Parent, primaryRelationship.Parent, primaryRelationship.PrimaryColumns, foreignRelationship.ForeignRelationship.Parent, foreignRelationship.ForeignRelationship.PrimaryColumns, filter)
 {
     _intermediatePrimaryRelationship = primaryRelationship;
     _intermediateForeignRelationship = foreignRelationship;
     ResetDefaults();
 }
Exemplo n.º 3
0
        public bool AliasValidate(OneToManyRelationship oneToManyRelationship, out string failReason)
        {
            failReason = "";
            /*Don't check items that are not enabled*/

            if (!oneToManyRelationship.Enabled)
            {
                return(true);
            }

            foreach (Relationship sibling in oneToManyRelationship.Parent.Relationships)
            {
                if (sibling != oneToManyRelationship && sibling.Enabled && ArchAngel.Providers.Database.Helper.Script.StringsAreEqual(sibling.Alias, oneToManyRelationship.Alias, false))
                {
                    failReason = "Duplicate alias: " + oneToManyRelationship.Alias;
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 4
0
        public bool NameValidate(OneToManyRelationship oneToManyRelationship, out string failReason)
        {
            failReason = "";
            /*Don't check items that are not enabled*/

            if (!oneToManyRelationship.Enabled)
            {
                return(true);
            }
            if (string.IsNullOrEmpty(oneToManyRelationship.Name))
            {
                failReason = "Name cannot be zero-length.";
                return(false);
            }
            if (oneToManyRelationship.Name.IndexOf(" ") >= 0)
            {
                failReason = "Name cannot have spaces.";
                return(false);
            }
            return(true);
        }
Exemplo n.º 5
0
 public bool AliasPluralValidate(OneToManyRelationship oneToManyRelationship, out string failReason)
 {
     failReason = "";
     return(true);
 }
Exemplo n.º 6
0
 public virtual string AliasPluralDefault(OneToManyRelationship oneToManyRelationship)
 {
     return(ArchAngel.Providers.Database.Helper.Script.GetPlural(oneToManyRelationship.Alias));
 }
Exemplo n.º 7
0
 public string AliasDefault(OneToManyRelationship oneToManyRelationship)
 {
     return(oneToManyRelationship.ForeignScriptObject.AliasPlural);
 }
        public bool NameValidate(OneToManyRelationship oneToManyRelationship, out string failReason)
        {
            failReason = "";
            /*Don't check items that are not enabled*/

            if (!oneToManyRelationship.Enabled)
            {
                return true;
            }
            if (string.IsNullOrEmpty(oneToManyRelationship.Name))
            {
                failReason = "Name cannot be zero-length.";
                return false;
            }
            if (oneToManyRelationship.Name.IndexOf(" ") >= 0)
            {
                failReason = "Name cannot have spaces.";
                return false;
            }
            return true;
        }
        public bool AliasValidate(OneToManyRelationship oneToManyRelationship, out string failReason)
        {
            failReason = "";
            /*Don't check items that are not enabled*/

            if (!oneToManyRelationship.Enabled)
            {
                return true;
            }

            foreach (Relationship sibling in oneToManyRelationship.Parent.Relationships)
            {
                if (sibling != oneToManyRelationship && sibling.Enabled && ArchAngel.Providers.Database.Helper.Script.StringsAreEqual(sibling.Alias, oneToManyRelationship.Alias, false))
                {
                    failReason = "Duplicate alias: " + oneToManyRelationship.Alias;
                    return false;
                }
            }
            return true;
        }
Exemplo n.º 10
0
 public bool AliasPluralValidate(OneToManyRelationship oneToManyRelationship, out string failReason)
 {
     failReason = "";
     return true;
 }
Exemplo n.º 11
0
 public virtual string AliasPluralDefault(OneToManyRelationship oneToManyRelationship)
 {
     return ArchAngel.Providers.Database.Helper.Script.GetPlural(oneToManyRelationship.Alias);
 }
Exemplo n.º 12
0
 public string AliasDefault(OneToManyRelationship oneToManyRelationship)
 {
     return oneToManyRelationship.ForeignScriptObject.AliasPlural;
 }
Exemplo n.º 13
0
        /// <exclude/>
        public ManyToManyRelationship(SerializationInfo serializationInfo, StreamingContext streamingContext)
        {
            if (SerializerHelper.UseFastSerialization)
            {
                _foreignColumns = null;

                using (SerializationReader reader = new SerializationReader((byte[])serializationInfo.GetValue("d", typeof(byte[]))))
                {
                    UniqueId = reader.ReadString();
                    Lookups.Add(UniqueId, this);
                    _alias                             = reader.ReadString();
                    _enabled                           = reader.ReadBoolean();
                    _filterId                          = reader.ReadString();
                    _ForeignColumnIds                  = reader.ReadStringArray();
                    _ForeignRelationshipId             = reader.ReadString();
                    _ForeignScriptObjectId             = reader.ReadString();
                    _intermediateForeignRelationshipId = reader.ReadString();
                    _intermediatePrimaryRelationshipId = reader.ReadString();
                    _isUserDefined                     = reader.ReadBoolean();
                    _name = reader.ReadString();
                    // TODO: Parent
                    string[] primaryColumnIds = reader.ReadStringArray();

                    foreach (string primaryColumnId in primaryColumnIds)
                    {
                        _primaryColumns.Add((Column)Lookups[primaryColumnId]);
                    }
                    // TODO: PrimaryScriptObject
                    _userOptions = (List <IUserOption>)reader.ReadObject();

                    for (int i = 0; i < _userOptions.Count; i++)
                    {
                        _userOptions[i].Owner = this;
                    }
                }
            }
            else
            {
                int version = 0;

                if (SerializationVersionExists)
                {
                    try
                    {
                        version = serializationInfo.GetInt32("SerializationVersion");
                    }
                    catch (SerializationException)
                    {
                        // ignore
                        SerializationVersionExists = false;
                    }
                }
                _alias   = serializationInfo.GetString("Alias");
                _enabled = serializationInfo.GetBoolean("Enabled");
                //_exposedUserOptions = serializationInfo.GetValue("ExposedUserOptions", ModelTypes.Object);
                _filter                          = (Filter)serializationInfo.GetValue("Filter", ModelTypes.Filter);
                _foreignColumns                  = (List <Column>)serializationInfo.GetValue("ForeignColumns", ModelTypes.ColumnList);
                _foreignRelationship             = (Relationship)serializationInfo.GetValue("ForeignRelationship", ModelTypes.Relationship);
                _foreignScriptObject             = (ScriptObject)serializationInfo.GetValue("ForeignScriptObject", ModelTypes.ScriptObject);
                _intermediateForeignRelationship = (ManyToOneRelationship)serializationInfo.GetValue("IntermediateForeignRelationship", ModelTypes.ManyToOneRelationship);
                _intermediatePrimaryRelationship = (OneToManyRelationship)serializationInfo.GetValue("IntermediatePrimaryRelationship", ModelTypes.OneToManyRelationship);
                _isUserDefined                   = serializationInfo.GetBoolean("IsUserDefined");
                _name                = serializationInfo.GetString("Name");
                _parent              = (ScriptObject)serializationInfo.GetValue("Parent", ModelTypes.ScriptObject);
                _primaryColumns      = (List <Column>)serializationInfo.GetValue("PrimaryColumns", ModelTypes.ColumnList);
                _primaryScriptObject = (ScriptObject)serializationInfo.GetValue("PrimaryScriptObject", ModelTypes.ScriptObject);
                _userOptions         = (List <IUserOption>)serializationInfo.GetValue("UserOptions", ModelTypes.UserOptionList);

                if (version >= 8)
                {
                    _description = serializationInfo.GetString("Description");
                }
            }
        }
Exemplo n.º 14
0
        private void InitialUpdateRelationships(IList<Model.Table> tables)
        {
            foreach (Model.Table table in tables)
            {
                foreach (Key key in table.Keys)
                {
                    // Look for one to one relationship
                    if (key.Type != DatabaseConstant.KeyType.Foreign)
                    {
                        continue;
                    }
                    if (IsOneToOneRelationship(key))
                    {
                        Filter filter = Search.GetFilter(table.Filters, new Helper(DalAssemblyName).GetPrimaryKey((Model.Table)key.Parent).Name);
                        OneToOneRelationship oneToOneRelationship = new OneToOneRelationship("One_" + key.Name, false, table, key.Columns, key.ReferencedTable, key.ReferencedKey.Columns, filter, false);
                        table.AddRelationship(oneToOneRelationship);

                        // Back other way
                        Filter filter2 = Search.GetFilter(key.ReferencedKey.Parent.Filters, new Helper(DalAssemblyName).GetPrimaryKey((Model.Table)key.ReferencedKey.Parent).Name);
                        OneToOneRelationship oneToOneRelationship2 = new OneToOneRelationship("One_" + key.Name, false, key.ReferencedTable, key.ReferencedKey.Columns, table, key.Columns, filter2, true);
                        key.ReferencedTable.AddRelationship(oneToOneRelationship2);

                        oneToOneRelationship.ForeignRelationship = oneToOneRelationship2;
                        oneToOneRelationship2.ForeignRelationship = oneToOneRelationship;
                    }
                    else
                    {
                        Filter filter = Search.GetFilter(table.Filters, key.Name);
                        ManyToOneRelationship manyToOneRelationship = new ManyToOneRelationship("One_" + key.Name, false, table, key.Columns, key.ReferencedTable, key.ReferencedKey.Columns, filter);
                        table.AddRelationship(manyToOneRelationship);

                        // Back other way
                        Filter filter2 = Search.GetFilter(key.ReferencedTable.Filters, key.ReferencedKey.Name);
                        OneToManyRelationship oneToManyRelationship = new OneToManyRelationship("Many_" + key.Name, false, key.ReferencedTable, key.ReferencedKey.Columns, table, key.Columns, filter2);
                        key.ReferencedTable.AddRelationship(oneToManyRelationship);

                        manyToOneRelationship.ForeignRelationship = oneToManyRelationship;
                        oneToManyRelationship.ForeignRelationship = manyToOneRelationship;
                    }
                }
            }
            // Temp list to search for foreign relationship
            List<Relationship> manyToManyRelationships = new List<Relationship>();
            foreach (Model.Table table in tables)
            {
                foreach (ManyToOneRelationship manyToOneRelationship in table.ManyToOneRelationships)
                {
                    string path = manyToOneRelationship.ForeignScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship.ForeignColumns) + " -> " + manyToOneRelationship.PrimaryScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship.PrimaryColumns);

                    foreach (ManyToOneRelationship manyToOneRelationship2 in table.ManyToOneRelationships)
                    {
                        if (manyToOneRelationship2.Name == manyToOneRelationship.Name)
                        {
                            continue;
                        }
                        path += " -> " + manyToOneRelationship2.PrimaryScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship2.PrimaryColumns) + " -> " + manyToOneRelationship2.ForeignScriptObject.Name + " -> " + GetColumnNameList(manyToOneRelationship2.ForeignColumns);
                        ManyToManyRelationship manyToManyRelationship = new ManyToManyRelationship("Many_" + manyToOneRelationship.Name + "_" + manyToOneRelationship2.Name,
                            false, (OneToManyRelationship)manyToOneRelationship.ForeignRelationship, manyToOneRelationship2, manyToOneRelationship2.ForeignRelationship.Filter);

                        manyToOneRelationship.ForeignScriptObject.AddRelationship(manyToManyRelationship);
                        manyToManyRelationships.Add(manyToManyRelationship);
                    }
                }
            }
            #region Fill foreign relationships
            foreach (Model.Table table in tables)
            {
                foreach (ManyToManyRelationship manyToManyRelationship in table.ManyToManyRelationships)
                {
                    string foreignRelationshipName = "Many_" + manyToManyRelationship.IntermediatePrimaryRelationship.ForeignRelationship.Name + "_" + manyToManyRelationship.IntermediateForeignRelationship.Name;

                    ManyToManyRelationship foreignRelationship = (ManyToManyRelationship)Search.GetRelationship(manyToManyRelationships.ToArray(), foreignRelationshipName);
                    manyToManyRelationship.ForeignRelationship = foreignRelationship;
                }
            }
            #endregion
        }
Exemplo n.º 15
0
        /// <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();
        }
Exemplo n.º 16
0
        /// <exclude/>
        public ManyToManyRelationship(SerializationInfo serializationInfo, StreamingContext streamingContext)
        {
            if (SerializerHelper.UseFastSerialization)
            {
                _foreignColumns = null;

                using (SerializationReader reader = new SerializationReader((byte[])serializationInfo.GetValue("d", typeof(byte[]))))
                {
                    UniqueId = reader.ReadString();
                    Lookups.Add(UniqueId, this);
                    _alias = reader.ReadString();
                    _enabled = reader.ReadBoolean();
                    _filterId = reader.ReadString();
                    _ForeignColumnIds = reader.ReadStringArray();
                    _ForeignRelationshipId = reader.ReadString();
                    _ForeignScriptObjectId = reader.ReadString();
                    _intermediateForeignRelationshipId = reader.ReadString();
                    _intermediatePrimaryRelationshipId = reader.ReadString();
                    _isUserDefined = reader.ReadBoolean();
                    _name = reader.ReadString();
                    // TODO: Parent
                    string[] primaryColumnIds = reader.ReadStringArray();

                    foreach (string primaryColumnId in primaryColumnIds)
                    {
                        _primaryColumns.Add((Column)Lookups[primaryColumnId]);
                    }
                    // TODO: PrimaryScriptObject
                    _userOptions = (List<IUserOption>)reader.ReadObject();

                    for (int i = 0; i < _userOptions.Count; i++)
                    {
                        _userOptions[i].Owner = this;
                    }
                }
            }
            else
            {
                int version = 0;

                if (SerializationVersionExists)
                {
                    try
                    {
                        version = serializationInfo.GetInt32("SerializationVersion");
                    }
                    catch (SerializationException)
                    {
                        // ignore
                        SerializationVersionExists = false;
                    }
                }
                _alias = serializationInfo.GetString("Alias");
                _enabled = serializationInfo.GetBoolean("Enabled");
                //_exposedUserOptions = serializationInfo.GetValue("ExposedUserOptions", ModelTypes.Object);
                _filter = (Filter)serializationInfo.GetValue("Filter", ModelTypes.Filter);
                _foreignColumns = (List<Column>)serializationInfo.GetValue("ForeignColumns", ModelTypes.ColumnList);
                _foreignRelationship = (Relationship)serializationInfo.GetValue("ForeignRelationship", ModelTypes.Relationship);
                _foreignScriptObject = (ScriptObject)serializationInfo.GetValue("ForeignScriptObject", ModelTypes.ScriptObject);
                _intermediateForeignRelationship = (ManyToOneRelationship)serializationInfo.GetValue("IntermediateForeignRelationship", ModelTypes.ManyToOneRelationship);
                _intermediatePrimaryRelationship = (OneToManyRelationship)serializationInfo.GetValue("IntermediatePrimaryRelationship", ModelTypes.OneToManyRelationship);
                _isUserDefined = serializationInfo.GetBoolean("IsUserDefined");
                _name = serializationInfo.GetString("Name");
                _parent = (ScriptObject)serializationInfo.GetValue("Parent", ModelTypes.ScriptObject);
                _primaryColumns = (List<Column>)serializationInfo.GetValue("PrimaryColumns", ModelTypes.ColumnList);
                _primaryScriptObject = (ScriptObject)serializationInfo.GetValue("PrimaryScriptObject", ModelTypes.ScriptObject);
                _userOptions = (List<IUserOption>)serializationInfo.GetValue("UserOptions", ModelTypes.UserOptionList);

                if (version >= 8)
                {
                    _description = serializationInfo.GetString("Description");
                }
            }
        }