예제 #1
0
        private void ReadTables(DataSchema dataSchema, IEnumIVMEModelElements elementEnumerator)
        {
            //  Iterate over all the entities in the model, searching
            //  for tables.
            //  Each entity is a table, we gather column and index information from here
            for (IVMEModelElement element = elementEnumerator.Next(); element != null; element = elementEnumerator.Next())
            {
                if (backgroundWorker.CancellationPending)
                    return;

                if (element.Type == VMEModelElementKind.eVMEKindEREntity)
                {
                    IVMEEntity entity = element as IVMEEntity;

                    Table newTable = new Table();
                    newTable.Name = entity.PhysicalName;
                    dataSchema.Tables.Add(newTable);

                    //
                    //  Here we search for the columns of the table
                    //
                    IEnumIVMEAttributes columnEnumerator = entity.Attributes;
                    for (IVMEAttribute columnDefinition = columnEnumerator.Next(); columnDefinition != null; columnDefinition = columnEnumerator.Next())
                    {
                        if (backgroundWorker.CancellationPending)
                            return;
                        Column newColumn = new Column();
                        newColumn.Name = columnDefinition.PhysicalName;
                        string columnTypeName = GetColumnTypeName(columnDefinition.DataType);
                        newColumn.ColumnType = FindColumnType(dataSchema, columnTypeName);
                        if (newColumn.ColumnType == null)
                        {
                            throw CreateException("Column type " + columnTypeName + " used in table " + newTable.Name + " was not found in the type definition document");
                        }

                        newColumn.CanBeNull = columnDefinition.AllowNulls;
                        newTable.Columns.Add(newColumn);
                    }

                    //
                    //  Now we search for indexes in the structure
                    //
                    IEnumIVMEEntityAnnotations indexesEnumerator = entity.EntityAnnotations;
                    for (IVMEEntityAnnotation indexDefinition = indexesEnumerator.Next(); indexDefinition != null; indexDefinition = indexesEnumerator.Next())
                    {
                        if (backgroundWorker.CancellationPending)
                            return;
                        bool isPrimaryKey = indexDefinition.kind == VMEEREntityAnnotationKind.eVMEEREntityAnnotationPrimary;
                        if (!isPrimaryKey)
                        {
                            // Primary keys in the Knightrunner DataSchema are not index objects
                            Index newIndex = new Index(newTable);
                            newIndex.Name = indexDefinition.PhysicalName;
                            newIndex.IsUnique = indexDefinition.kind == VMEEREntityAnnotationKind.eVMEEREntityAnnotationAlternate;

                            IEnumIVMEAttributes indexColumnEnumerator = indexDefinition.Attributes;
                            for (IVMEAttribute indexColumn = indexColumnEnumerator.Next(); indexColumn != null; indexColumn = indexColumnEnumerator.Next())
                            {
                                newIndex.Columns.Add(newTable.Columns[indexColumn.PhysicalName]);
                            }

                            newTable.Indices.Add(newIndex);
                        }
                        else
                        {
                            IEnumIVMEAttributes indexColumnEnumerator = indexDefinition.Attributes;
                            for (IVMEAttribute indexColumn = indexColumnEnumerator.Next(); indexColumn != null; indexColumn = indexColumnEnumerator.Next())
                            {
                                newTable.Columns[indexColumn.PhysicalName].InPrimaryKey = true;
                            }
                        }
                    }

                }
            }
        }
예제 #2
0
        private void ReadRelations(DataSchema dataSchema, IEnumIVMEModelElements elementEnumerator)
        {
            //
            //  Iterates over all the entities in the model, searching
            //  for relationships.
            //
            //  for each relationship, we need to find the related tables
            //  and all the interesting properties of the relationship
            //
            for (IVMEModelElement element = elementEnumerator.Next(); element != null; element = elementEnumerator.Next())
            {
                if (element.Type == VMEModelElementKind.eVMEKindERRelationship)
                {
                    IVMEBinaryRelationship relationship = element as IVMEBinaryRelationship;

                    if (relationship.FirstEntity != null && relationship.SecondEntity != null)
                    {
                        string relationName = relationship.PhysicalName;
                        Table primaryTable = dataSchema.Tables[relationship.FirstEntity.PhysicalName];
                        Table referencedTable = dataSchema.Tables[relationship.SecondEntity.PhysicalName];

                        //
                        //  Now we need to add the columns that are used to mantain the
                        //  relationship. We need two cursors, one for the primary
                        //  table and the other for the referenced one
                        //
                        IEnumIVMEAttributes primaryColumnEnumerator = relationship.FirstAttributes;
                        IVMEAttribute primaryColumn = primaryColumnEnumerator.Next();

                        IEnumIVMEAttributes referencedColumnEnumerator = relationship.SecondAttributes;
                        IVMEAttribute referencedColumn = referencedColumnEnumerator.Next();

                        while (primaryColumn != null)
                        {
                            Column primaryDataColumn = primaryTable.Columns[primaryColumn.PhysicalName];
                            Column referencedDataColumn = referencedTable.Columns[referencedColumn.PhysicalName];

                            ForeignKey foreignKey = new ForeignKey();
                            foreignKey.FromTable = primaryTable;
                            foreignKey.ToTable = referencedTable;
                            foreignKey.Columns.Add(new ForeignKey.ColumnPair { FromColumn = primaryDataColumn, ToColumn = referencedDataColumn });

                            primaryColumn = primaryColumnEnumerator.Next();
                            referencedColumn = referencedColumnEnumerator.Next();
                        }
                    }
                }
            }
        }