예제 #1
0
        public void RebuildAllTypesIfOnlySomeTimesFound()
        {
            var variableName = $"tbl12_{guid1}";
            var table        = new Mock <Table>()
            {
                CallBase = true
            };

            table.Setup(c => c.VariableName).Returns(variableName);
            table.Setup(c => c.GetHashCode()).Returns(1234566);

            var table2 = new Mock <Table>()
            {
                CallBase = true
            };

            table2.Setup(c => c.VariableName).Returns($"tbl2_{guid2}");
            table2.Setup(c => c.GetHashCode()).Returns(4561233);

            schema.Setup(c => c.Tables).Returns(new List <Table> {
                table.Object, table2.Object
            });

            //Create so already found
            string asmName = $"dynamicassembly.tbl12_{guid1}1234566.reqTbl987654";

            CreateAsm(asmName, $"tbl12_{guid1}");

            Assert.AreEqual(1, AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().Name.ToUpperInvariant() == asmName.ToUpperInvariant()).Count());
            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            Assert.AreEqual(2, classBuilder.ExistingAssemblies.Count());
            //all upper means generated
            Assert.AreEqual($"TBL12_{guid1.ToString().ToUpper()}", classBuilder.ExistingAssemblies[asmName.ToUpperInvariant()].Name);
            Assert.AreEqual(2, AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().Name.ToUpperInvariant() == asmName.ToUpperInvariant()).Count());
        }
예제 #2
0
        public void Add1ToManyRelationship()
        {
            var table = new Mock <Table>()
            {
                CallBase = true
            };

            table.Object.Database = "db";
            table.Object.Schema   = "sch";
            table.Object.Name     = $"tbl1_{guid1}";
            table.Setup(c => c.Columns).Returns(new List <Column>
            {
                new Column {
                    ColumnName = "pkCol", PKName = "pk1", TableName = $"tbl1_{guid1}", ColumnType = "int", DataType = new DataType {
                        SystemType = typeof(int)
                    }
                },
            });
            table.Setup(c => c.GetHashCode()).Returns(123456);

            var table2 = new Mock <Table>()
            {
                CallBase = true
            };

            table2.Object.Database = "db";
            table2.Object.Schema   = "sch";
            table2.Object.Name     = $"tbl2_{guid2}";
            table2.Setup(c => c.Columns).Returns(new List <Column>
            {
                new Column {
                    ColumnName = "pkCol2", FKName = "fk1", TableName = $"tbl2_{guid2}", ReferencedColumn = "pkCol", ReferencedTable = $"tbl1_{guid1}", ReferencedSchema = "sch", ReferencedDatabase = "db", ColumnType = "int", DataType = new DataType {
                        SystemType = typeof(int)
                    }
                },
            });
            table2.Setup(c => c.GetHashCode()).Returns(456123);

            schema.Setup(c => c.Tables).Returns(new List <Table> {
                table.Object, table2.Object
            });

            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            var property    = classBuilder.ExistingAssemblies[$"DYNAMICASSEMBLY.DB_SCH_TBL1_{guid1.ToString().ToUpper()}123456.REQTBL987654"].GetProperty($"Tbl2_{guid2}ListFromPkCol2");
            var refProperty = classBuilder.ExistingAssemblies[$"DYNAMICASSEMBLY.DB_SCH_TBL2_{guid2.ToString().ToUpper()}456123.REQTBL987654"].GetProperty($"Tbl1_{guid1}ObjectFromPkCol2");


            Assert.AreEqual($"Tbl1_{guid1}ObjectFromPkCol2", property.GetCustomAttribute <InversePropertyAttribute>().Property);
            Assert.AreEqual($"Tbl2 {guid2} List From Pk Col2".ToUpper(), property.GetCustomAttribute <DisplayNameAttribute>().DisplayName.ToUpper());
            Assert.IsNotNull(property.GetCustomAttribute <DataMemberAttribute>());
            Assert.IsTrue(property.GetCustomAttribute <BrowsableAttribute>().Browsable);
            Assert.AreEqual("db", property.GetCustomAttribute <ReferencedDbObjectAttribute>().DbName);
            Assert.AreEqual("sch", property.GetCustomAttribute <ReferencedDbObjectAttribute>().SchemaName);
            Assert.AreEqual($"tbl2_{guid2}", property.GetCustomAttribute <ReferencedDbObjectAttribute>().TableName);

            Assert.AreEqual($"Tbl1 {guid1} Object From Pk Col2".ToUpper(), refProperty.GetCustomAttribute <DisplayNameAttribute>().DisplayName.ToUpper());
            Assert.IsNotNull(refProperty.GetCustomAttribute <DataMemberAttribute>());
            Assert.IsTrue(refProperty.GetCustomAttribute <BrowsableAttribute>().Browsable);
            Assert.AreEqual("db", refProperty.GetCustomAttribute <ReferencedDbObjectAttribute>().DbName);
            Assert.AreEqual("sch", refProperty.GetCustomAttribute <ReferencedDbObjectAttribute>().SchemaName);
            Assert.AreEqual($"tbl1_{guid1}", refProperty.GetCustomAttribute <ReferencedDbObjectAttribute>().TableName);
        }
예제 #3
0
        private int GetPrivateTypeBuilderCount(DynamicClassBuilder classBuilder)
        {
            FieldInfo strProperty = classBuilder.GetType().GetField("_typeBuilders", BindingFlags.NonPublic | BindingFlags.Instance);

            return(((Dictionary <string, TypeBuilder>)strProperty.GetValue(classBuilder)).Count());
        }
예제 #4
0
        public void AddForigenKeyUpdatesForcesRegen()
        {
            Table tbl1 = new Table
            {
                Name     = "tbl1" + tableSuffix,
                Schema   = "sch1",
                Database = "db1",
            };

            tbl1.Columns.AddRange(
                new List <Column>
            {
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl1" + tableSuffix,
                    ColumnName  = "Id1",
                    ColumnType  = "int",
                    PKName      = "pk",
                    DataType    = new DataType
                    {
                        SystemType = typeof(int)
                    }
                },
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl1" + tableSuffix,
                    ColumnName  = "fkCol",
                    ColumnType  = "int",
                    DataType    = new DataType
                    {
                        SystemType = typeof(int)
                    }
                }
            });

            Table tbl2 = new Table
            {
                Name     = "tbl2" + tableSuffix,
                Schema   = "sch1",
                Database = "db1"
            };

            tbl2.Columns.AddRange(
                new List <Column>
            {
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl2" + tableSuffix,
                    ColumnName  = "Id2",
                    ColumnType  = "int",
                    PKName      = "pk",
                    DataType    = new DataType
                    {
                        SystemType = typeof(int)
                    }
                },
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl2" + tableSuffix,
                    ColumnName  = "name",
                    ColumnType  = "varchar",
                    DataType    = new DataType
                    {
                        SystemType = typeof(string)
                    }
                }
            });

            var initList = new List <Table> {
                tbl1, tbl2
            };

            var dbSchema = new Mock <IDbSchema>();

            dbSchema.SetupGet(c => c.Tables).Returns(initList);
            dbSchema.SetupGet(c => c.Views).Returns(new List <View>());

            DynamicClassBuilder classBuilder = new DynamicClassBuilder(dbSchema.Object);

            classBuilder.CreateModelTypes("tbl1" + tableSuffix);

            //Created types and total types are the same
            Assert.AreEqual(2, GetPrivateTypeBuilderCount(classBuilder));
            Assert.AreEqual(2, classBuilder.ExistingAssemblies.Count);

            //Properties are just the columns
            Assert.AreEqual(2, classBuilder.ExistingAssemblies.Values.First().GetProperties().Length);
            Assert.AreEqual(2, classBuilder.ExistingAssemblies.Values.Last().GetProperties().Length);

            //Keep same columns but link to table 1
            tbl2.Columns.Clear();
            tbl2.Columns.AddRange(new List <Column>
            {
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl2" + tableSuffix,
                    ColumnName  = "Id2",
                    ColumnType  = "int",
                    PKName      = "pk",
                    DataType    = new DataType
                    {
                        SystemType = typeof(int)
                    }
                },
                new Column
                {
                    TableSchema        = "sch1",
                    TableName          = "tbl2" + tableSuffix,
                    ColumnName         = "name",
                    ColumnType         = "varchar",
                    FKName             = "fk",
                    ReferencedDatabase = "db1",
                    ReferencedSchema   = "sch1",
                    ReferencedTable    = "tbl1" + tableSuffix,
                    ReferencedColumn   = "Id2",
                    DataType           = new DataType
                    {
                        SystemType = typeof(string)
                    }
                }
            });



            var dbSchema2 = new Mock <IDbSchema>();

            dbSchema2.SetupGet(c => c.Tables).Returns(initList);
            dbSchema2.SetupGet(c => c.Views).Returns(new List <View>());

            DynamicClassBuilder classBuilder2 = new DynamicClassBuilder(dbSchema2.Object);

            classBuilder2.CreateModelTypes("tbl1" + tableSuffix);

            //Created types and total types are the same
            Assert.AreEqual(2, GetPrivateTypeBuilderCount(classBuilder2), "Not generating asms.");
            Assert.AreEqual(2, classBuilder2.ExistingAssemblies.Count);

            //Properties are include the columns and navproperties
            Assert.AreEqual(3, classBuilder2.ExistingAssemblies.Values.First().GetProperties().Length);
            Assert.AreEqual(3, classBuilder2.ExistingAssemblies.Values.Last().GetProperties().Length);
        }
예제 #5
0
        protected virtual IEnumerable <NavigationPropertyDefinition> ListNavigationProperties()
        {
            Table targetTable = DbSchema.GetTable(DatabaseName, SchemaName, DbObjectName);
            var   primaryKeys = targetTable.Columns.Where(c => c.IsPK).Select(c => c.ColumnName)
                                .ToImmutableHashSet();

            //Many to One
            foreach (var columnGroup in targetTable.Columns.Where(c => c.IsFK).GroupBy(c => c.FKName))
            {
                Table primaryTable = DbSchema.GetTable(columnGroup.First().ReferencedDatabase, columnGroup.First().ReferencedSchema, columnGroup.First().ReferencedTable);
                NavigationPropertyDefinition navigationProperty = new NavigationPropertyDefinition()
                {
                    Name             = DynamicClassBuilder.DependentToPrimaryObjectName(columnGroup.First(), targetTable),
                    ReferencedSchema = primaryTable.Schema,
                    ReferencedTable  = primaryTable.Name,
                    FromProperty     = string.Join(",", columnGroup),
                    ToProperty       = string.Join(",", primaryTable.Columns.Where(c => c.IsPK)),
                    Relationship     = "Many to 1"
                };

                yield return(navigationProperty);
            }


            foreach (var table in DbSchema.Tables)
            {
                var tablePks = table.Columns.Where(c => c.IsPK).Select(c => c.ColumnName)
                               .ToImmutableHashSet();
                //1 to Many or One to Many
                foreach (var columnGroup in table.Columns.Where(c => c.IsFK && c.ReferencedVariableName == targetTable.VariableName)
                         .GroupBy(c => c.FKName))
                {
                    var groupColumns   = columnGroup.Select(c => c.ColumnName);
                    var groupRefColumn = columnGroup.Select(c => c.ReferencedColumn);



                    bool isOneToOne = tablePks.SetEquals(groupColumns) && primaryKeys.SetEquals(groupRefColumn);

                    NavigationPropertyDefinition navigationProperty = new NavigationPropertyDefinition()
                    {
                        Name = isOneToOne? DynamicClassBuilder.PrimaryToDepdenty1To1ObjectName(columnGroup.First()):
                               DynamicClassBuilder.PrimaryToDependenty1ToManyListName(columnGroup.First(), table),
                        ReferencedSchema = table.Schema,
                        ReferencedTable  = table.Name,
                        FromProperty     = string.Join(",", targetTable.Columns.Where(c => c.IsPK)),
                        ToProperty       = string.Join(",", columnGroup),
                        Relationship     = isOneToOne ? "1 to 1" : "1 to Many"
                    };

                    yield return(navigationProperty);
                }
            }

            var userJoins = Config.UserDefinedJoins;

            foreach (var userJoin in userJoins)
            {
                yield return(new NavigationPropertyDefinition()
                {
                    Name = UserJoinListName(userJoin),
                    ReferencedSchema = userJoin.DependentSchema,
                    ReferencedTable = userJoin.DependentTable,
                    FromProperty = userJoin.PrincipalColumns,
                    ToProperty = userJoin.DependentColumns,
                    IsUserDefinied = true,
                    Relationship = "Many to Many"
                });
            }
        }