private static RelationalModel CreateModel()
    {
        var model     = new RelationalModel("dbo");
        var dbo       = model["dbo"];
        var secondary = model.AddSchema("secondary");

        var people   = dbo.AddTable("people");
        var peopleId = people.AddColumn("id", true);

        people.AddColumn("name", false);
        var peopleFavoritePetId = people.AddColumn("favoritePetId", false);

        var pet   = secondary.AddTable("pet");
        var petId = pet.AddColumn("id", true);

        pet.AddColumn("name", false);
        var petOwnerId = pet.AddColumn("ownerPeopleId", false);

        people.AddForeignKeyTo(pet)
        .AddColumnPair(peopleFavoritePetId, petId);

        pet.AddForeignKeyTo(people)
        .AddColumnPair(petOwnerId, peopleId);

        return(model);
    }
        public void ColumnAdditionalData()
        {
            var model = new RelationalModel();
            var dbo   = model.AddSchema("dbo");

            var people   = dbo.AddTable("people");
            var peopleId = people.AddColumn("id", true);

            peopleId.SetAdditionalData("a", 18);
            peopleId.SetAdditionalData("a", 21);

            Assert.AreEqual(21, (int)people["ID"].GetAdditionalData("A"));
        }
        public void Keys()
        {
            var model     = new RelationalModel("dbo");
            var secondary = model.AddSchema("secondary");

            var people   = model.DefaultSchema.AddTable("people");
            var peopleId = people.AddColumn("id", true);

            people.AddColumn("name", false);
            var peopleFavoritePetId = people.AddColumn("favoritePetId", false);

            var pet   = secondary.AddTable("pet");
            var petId = pet.AddColumn("id", true);

            pet.AddColumn("name", false);
            var petOwnerId = pet.AddColumn("ownerPeopleId", false);

            people.AddForeignKeyTo(pet)
            .AddColumnPair(peopleFavoritePetId, petId);

            pet.AddForeignKeyTo(people)
            .AddColumnPair(petOwnerId, peopleId);

            Assert.AreEqual(2, model.Schemas.Count);
            Assert.AreEqual(1, model["dbo"].Tables.Count);
            Assert.AreEqual(1, secondary.Tables.Count);
            Assert.AreEqual(3, model["dbo"]["PEOPLE"].Columns.Count);
            Assert.AreEqual(3, secondary["PET"].Columns.Count);
            Assert.AreEqual(1, model["dbo"]["People"].ForeignKeys.Count);
            Assert.AreEqual(1, secondary["Pet"].ForeignKeys.Count);
            Assert.IsTrue(pet.ForeignKeys[0].TargetTable == people);
            Assert.IsTrue(people.ForeignKeys[0].ColumnPairs[0].SourceColumn == peopleFavoritePetId);
            Assert.IsTrue(people.ForeignKeys[0].ColumnPairs[0].TargetColumn == petId);
            Assert.IsTrue(pet.ForeignKeys[0].ColumnPairs[0].SourceColumn == petOwnerId);
            Assert.IsTrue(pet.ForeignKeys[0].ColumnPairs[0].TargetColumn == peopleId);
            Assert.AreEqual(1, model["dbo"]["PEOPLE"].PrimaryKeyColumns.Count);
            Assert.AreEqual(1, secondary["peT"].PrimaryKeyColumns.Count);
        }
Example #4
0
        public static RelationalModel Convert(DatabaseDefinition sourceDefinition, string defaultSourceSchemaName, CaseInsensitiveStringKeyDictionary <string> schemaNameMap = null, Func <SqlTable, bool> filterDelegate = null)
        {
            var newDefaultSchemaName = schemaNameMap?[defaultSourceSchemaName] ?? defaultSourceSchemaName;
            var newModel             = new RelationalModel(newDefaultSchemaName);
            var sourceTablesOrdered  = sourceDefinition.GetTables()
                                       .Where(x => filterDelegate?.Invoke(x) != false)
                                       .OrderBy(x => x.SchemaAndTableName.SchemaAndName)
                                       .ToList();

            foreach (var sourceTable in sourceTablesOrdered)
            {
                var newSchemaName = schemaNameMap?[sourceTable.SchemaAndTableName.Schema] ?? sourceTable.SchemaAndTableName.Schema ?? newDefaultSchemaName;

                var newSchema = newModel[newSchemaName]
                                ?? newModel.AddSchema(newSchemaName);

                var primaryKey = sourceTable.Properties.OfType <PrimaryKey>().FirstOrDefault();
                var newTable   = newSchema.AddTable(sourceTable.SchemaAndTableName.TableName);

                foreach (var property in sourceTable.Properties.OfType <DwhTableFlagProperty>())
                {
                    newTable.SetFlag(property.Name, true);
                }

                foreach (var property in sourceTable.Properties.OfType <DwhTableDataProperty>())
                {
                    newTable.SetAdditionalData(property.Name, property.Value);
                }

                if (sourceTable.HasProperty <EtlRunInfoDisabledProperty>())
                {
                    newTable.SetEtlRunInfoDisabled();
                }

                if (sourceTable.HasProperty <HasHistoryTableProperty>())
                {
                    newTable.SetHasHistoryTable();
                }

                var sourceTableNameOverrideProperty = sourceTable.Properties.OfType <SourceTableNameOverrideProperty>().FirstOrDefault();
                if (sourceTableNameOverrideProperty != null)
                {
                    newTable.SetSourceTableNameOverride(sourceTableNameOverrideProperty.SourceTableName);
                }

                foreach (var sourceColumn in sourceTable.Columns)
                {
                    var partOfPrimaryKey = primaryKey?.SqlColumns.Any(x => x.SqlColumn == sourceColumn) == true;
                    var newColumn        = newTable.AddColumn(sourceColumn.Name, partOfPrimaryKey);

                    foreach (var property in sourceColumn.Properties.OfType <DwhColumnFlagProperty>())
                    {
                        newColumn.SetFlag(property.Name, true);
                    }

                    foreach (var property in sourceColumn.Properties.OfType <DwhColumnDataProperty>())
                    {
                        newColumn.SetAdditionalData(property.Name, property.Value);
                    }

                    if (sourceColumn.HasProperty <Identity>())
                    {
                        newColumn.SetIdentity();
                    }

                    if (sourceColumn.HasProperty <HistoryDisabledProperty>())
                    {
                        newColumn.SetHistoryDisabled();
                    }

                    if (sourceColumn.HasProperty <RecordTimestampIndicatorProperty>())
                    {
                        newColumn.SetRecordTimestampIndicator();
                    }
                }
            }

            foreach (var table in sourceTablesOrdered)
            {
                var newSourceSchemaName = schemaNameMap?[table.SchemaAndTableName.Schema] ?? table.SchemaAndTableName.Schema ?? newDefaultSchemaName;

                var newSourceSchema = newModel[newSourceSchemaName];
                var newSourceTable  = newSourceSchema[table.SchemaAndTableName.TableName];

                foreach (var fk in table.Properties.OfType <ForeignKey>())
                {
                    var newTargetSchemaName = schemaNameMap?[fk.ReferredTable.SchemaAndTableName.Schema] ?? fk.ReferredTable.SchemaAndTableName.Schema ?? newDefaultSchemaName;

                    var newTargetSchema = newModel[newTargetSchemaName];
                    var newTargetTable  = newTargetSchema[fk.ReferredTable.SchemaAndTableName.TableName];

                    if (newTargetTable == null) // target table is filtered out
                    {
                        continue;
                    }

                    var newFk = newSourceTable.AddForeignKeyTo(newTargetTable);
                    foreach (var map in fk.ForeignKeyColumns)
                    {
                        var sourceColumn = newSourceTable[map.ForeignKeyColumn.Name];
                        var targetColumn = newTargetTable[map.ReferredColumn.Name];
                        newFk.AddColumnPair(sourceColumn, targetColumn);
                    }
                }
            }

            return(newModel);
        }