public void TestDerivedLoadWithFilter()
        {
            //Arrange
            var insertCommand = SqlConnection.CreateCommand();
            insertCommand.CommandText = "INSERT INTO BaseClass (ObjectId, _dscr, Prop1) VALUES(@p1, 2, 1); INSERT INTO DerivedClass (ObjectId, Prop2) VALUES(@p1, 3);";
            insertCommand.Parameters.AddWithValue("@p1", Guid.NewGuid());
            insertCommand.ExecuteNonQuery();

            var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField)
               .AddType<BaseClass>(z => z.AllProperties())
               .AddType<DerivedClass>(z => z.AllProperties())
               .GenerateTypeMappings();
            var provider = new TestProvider(types);
            //Act
            var mapper = new SqlValueMapper();
            var builder = new QueryBuilder(provider);
            var command = SqlConnection.CreateCommand();
            var plan = builder.GetQuery("DerivedClass", new [] { "Prop2" });
            BaseClass result;
            command.CommandText = plan.SqlString;
            command.Parameters.AddWithValue("@p0", 3);
            using (var reader = command.ExecuteReader())
            {
                var classes = mapper.MapFromReader(reader, plan.SelectClause);
                result = classes.OfType<BaseClass>().First();
            }
            //Assert
            Assert.IsInstanceOf<DerivedClass>(result);
            Assert.AreEqual(1, result.Prop1);
        }
        public void TestDerivedClassMapping()
        {
            var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField)
                .DefaultDiscriminatorColumnName(Discriminator)
                .AddType<BaseClass>(z => z.AllProperties())
                .AddType<DerivedClass>(z => z.AllProperties())
                .GenerateTypeMappings();
            var provider = new TestProvider(types);

            var mapper = new SqlValueMapper();
            var reader = MockRepository.GenerateMock<IDataReader>();
            reader.Stub(z => z.GetGuid(0)).Return(Guid.NewGuid());
            reader.Stub(z => z.GetInt32(1)).Return(2);
            reader.Stub(z => z.GetInt32(2)).Return(1);
            reader.Stub(z => z.GetGuid(3)).Return(Guid.NewGuid());
            reader.Stub(z => z.GetInt32(4)).Return(3);
            var numCalls = 1;
            reader.Stub(z => z.Read()).Return(true).WhenCalled(z => z.ReturnValue = --numCalls >= 0 );
                var typeMappingInfo = provider.GetTypeMapping("DerivedClass");
                var selectClause = new SelectClause
                {
                    Parts = new SelectPart[]
                    {
                        new TypePart
                        {
                            Type = typeMappingInfo,
                            Tables = typeMappingInfo.Tables.ToList()
                        }
                    }
                };
                var classes = mapper.MapFromReader(reader, selectClause);
                var result = classes.OfType<BaseClass>().First();
                Assert.AreEqual(1, result.Prop1);
        }
        public void TestMappingWithArrayOfPrimitives()
        {
            var types = FluentConfiguration.Start()
                .DefaultIdProperty(IdentityField)
                .DefaultDiscriminatorColumnName(Discriminator)
                .AddType<ClassWithIntArr>(z => z.AllProperties())
                .GenerateTypeMappings();
            var provider = new TestProvider(types);
            var mapper = new SqlValueMapper();
            var reader = MockRepository.GenerateMock<IDataReader>();
            var id = Guid.NewGuid();
            var numCalls = 2;
            reader.Stub(z => z.GetGuid(0)).Return(id);
            reader.Stub(z => z.GetValue(0)).Return(id);
            reader.Stub(z => z.GetInt32(1)).Return(1);
            reader.Stub(z => z.GetGuid(2)).Return(id);
            reader.Stub(z => z.GetValue(2)).Return(id);
            reader.Stub(z => z.GetValue(3)).Return(2);
            reader.Stub(z => z.GetInt64(4)).Return(0L).WhenCalled(z => z.ReturnValue = numCalls == 1 ? 0L : 1L);

            reader.Stub(z => z.Read()).Return(true).WhenCalled(z => z.ReturnValue = --numCalls >= 0);
            var typeMappingInfo = provider.GetTypeMapping("ClassWithIntArr");
            var selectClause = new SelectClause
            {
                Parts = new SelectPart[]
                {
                    new TypePart
                    {
                        Type = typeMappingInfo,
                        Tables = typeMappingInfo.Tables.Take(1).ToList()
                    },
                    new ExpansionPart
                    {
                        Table = typeMappingInfo.Tables.OfType<PrimitiveListTable>().First(),
                        CollectingProperty = typeMappingInfo.GetProperty("Arr"),
                        CollectingType = typeMappingInfo
                    }
                }
            };
            var classes = mapper.MapFromReader(reader, selectClause);
            var result = classes.OfType<ClassWithIntArr>().First();
            Assert.IsNotNull(result.Arr);
            Assert.AreEqual(2, result.Arr.Length);
            Assert.AreEqual(2, result.Arr.First());
        }
Esempio n. 4
0
        public void Test()
        {
            var mappings = FluentConfiguration.Start().DefaultDiscriminatorColumnName("_dscr")
                .DefaultIdProperty(IdentityField)
                .AddTypeAuto<FirstClass>()
                .AddTypeAuto<SecondClass>()
                .AddTypeAuto<ThirdClass>().GenerateTypeMappings();

            var provider = new TestProvider(mappings);
            var queryBuilder = new QueryBuilder(provider);
            var createTable = SqlConnection.CreateCommand();
            createTable.CommandText = string.Concat(
                queryBuilder.GetCreateTable(typeof(FirstClass)),
                queryBuilder.GetCreateTable(typeof(SecondClass)),
                queryBuilder.GetCreateTable(typeof(ThirdClass))
            );
            createTable.ExecuteNonQuery();
            var thirdGuids = LoadTable("ThirdClass", (row, i) => { });
            var secondGuids = LoadTable("SecondClass", (row, i) => { row["Third"] = thirdGuids[i]; });
            LoadTable("FirstClass", (row, i) => { row["Second"] = secondGuids[i]; });

            var stopWatch = new Stopwatch();
            stopWatch.Start();

            var plan = queryBuilder.GetQuery("FirstClass", new string[0], new[] { "Second.Third" });
            Debug.WriteLine(plan.SqlString);

            var mapper = new SqlValueMapper();
            var firstClasses = new List<FirstClass>(Count);
            using (var cmd = new SqlCommand(plan.SqlString, SqlConnection))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    firstClasses.AddRange(mapper.MapFromReader(reader, plan.SelectClause).OfType<FirstClass>());
                }
            }
            stopWatch.Stop();
            Assert.AreEqual(Count, firstClasses.Count(z => z.Second != null && z.Second.Third != null));
            Assert.Pass(stopWatch.Elapsed.ToString());
        }
Esempio n. 5
0
        public void TestConfig()
        {
            var stopWatch = new Stopwatch();

            var types = typeof (BaseClass_5).Assembly.GetTypes()
                    .Where(z => z.Name.StartsWith("BaseClass") || z.Name.StartsWith("Derived")).ToArray();
            var list = new List<Delegate>(types.Length * 2);
            stopWatch.Start();

            var config =
                FluentConfiguration.Start().DefaultDiscriminatorColumnName("_dscr").DefaultIdProperty(IdentityField);

            foreach (var type in types)
            {
                config.AddTypeAuto(type);
            }
            var elapsed = stopWatch.Elapsed;
            var mappings = config.GenerateTypeMappings() as IList<TypeMappingInfo>;
            Assert.IsNotNull(mappings);
            var mapper = new SqlValueMapper();
            var elapsedBuild = stopWatch.Elapsed;
            foreach (var mapping in mappings)
            {
                list.Add(mapper.GetTypeInstanceCreateDelegate(mapping));
                foreach(var table in mapping.Tables)
                    list.Add(mapper.GetFiller(mapping, table));
            }

            stopWatch.Stop();
            Debug.WriteLine("Completed full initialization in {0}", stopWatch.Elapsed);
            Debug.WriteLine("Typeload: {0}", elapsed);
            Debug.WriteLine("Build: {0}", elapsedBuild - elapsed);
            Debug.WriteLine("Mapping compile: {0}", stopWatch.Elapsed - elapsedBuild);
            Debug.WriteLine("Types: {0}", mappings.Count);
            Debug.WriteLine("NavProps: {0}", mappings.Sum(z => z.Tables.Sum(t => t.Columns.OfType<NavigationPropertyMapping>().Count())));
            Debug.WriteLine("Props: {0}", mappings.Sum(z => z.Tables.Sum(t => t.Columns.Count(p => p.GetType() == typeof(PropertyMapping)))));
            Assert.Pass();
        }
        public void TestIncludeLoad()
        {
            //Arrange
            var insertCommand = SqlConnection.CreateCommand();
            insertCommand.CommandText = "INSERT INTO BaseWithNavigationClass (ObjectId, _dscr, Nav) VALUES(@p1, 1, @p2); INSERT INTO NavigationedClass (ObjectId, _dscr, Something) VALUES(@p2, 1, 'goody')";
            insertCommand.Parameters.AddWithValue("@p1", Guid.NewGuid());
            var navGuid = Guid.NewGuid();
            insertCommand.Parameters.AddWithValue("@p2", navGuid);
            insertCommand.ExecuteNonQuery();

            var types = FluentConfiguration.Start()
                .DefaultIdProperty(IdentityField)
                .DefaultDiscriminatorColumnName("_dscr")
                .AddType<BaseWithNavigationClass>(z => z.AllProperties())
                .AddType<NavigationedClass>(z => z.AllProperties())
                .GenerateTypeMappings();
            var provider = new TestProvider(types);
            var mapper = new SqlValueMapper();
            var builder = new QueryBuilder(provider);
            var command = SqlConnection.CreateCommand();
            var plan = builder.GetQuery("BaseWithNavigationClass", new string[0], new []{ "Nav"} );
            NavigationedClass result;
            command.CommandText = plan.SqlString;
            using (var reader = command.ExecuteReader())
            {
                var classes = mapper.MapFromReader(reader, plan.SelectClause);
                result = classes.OfType<BaseWithNavigationClass>().First().Nav;
            }
            //Assert
            Assert.AreEqual("goody", result.Something);
        }
 public void TestMappingWithNavigationCollection()
 {
     var navGuid = Guid.NewGuid();
     var types = FluentConfiguration.Start()
         .DefaultIdProperty(IdentityField)
         .DefaultDiscriminatorColumnName(Discriminator)
         .AddType<ClassWithCollection>(z => z.AllProperties())
         .AddType<CollectionItem>(z => z.AllProperties())
         .GenerateTypeMappings();
     var provider = new TestProvider(types);
     var mapper = new SqlValueMapper();
     var reader = MockRepository.GenerateMock<IDataReader>();
     var id = Guid.NewGuid();
     reader.Stub(z => z.GetGuid(0)).Return(id);
     reader.Stub(z => z.GetValue(0)).Return(id);
     reader.Stub(z => z.GetInt32(1)).Return(1);
     reader.Stub(z => z.GetGuid(2)).Return(navGuid);
     reader.Stub(z => z.GetValue(2)).Return(navGuid);
     reader.Stub(z => z.GetInt32(3)).Return(1);
     reader.Stub(z => z.GetGuid(4)).Return(id);
     reader.Stub(z => z.GetString(5)).Return("someString");
     var numCalls = 1;
     reader.Stub(z => z.Read()).Return(true).WhenCalled(z => z.ReturnValue = --numCalls >= 0);
     var typeMappingInfo = provider.GetTypeMapping("ClassWithCollection");
     var itemMappingInfo = provider.GetTypeMapping("CollectionItem");
     var selectClause = new SelectClause
     {
         Parts = new SelectPart[]
         {
             new TypePart
             {
                 Type = typeMappingInfo,
                 Tables = typeMappingInfo.Tables.Take(1).ToList()
             },
             new SubTypePart
             {
                 Type = itemMappingInfo,
                 Tables = itemMappingInfo.Tables.Take(1).ToList(),
                 CollectingProperty = typeMappingInfo.GetProperty("Collection"),
                 CollectingType = typeMappingInfo
             }
         }
     };
     var classes = mapper.MapFromReader(reader, selectClause);
     var result = classes.OfType<ClassWithCollection>().First();
     Assert.IsNotNull(result.Collection);
     Assert.AreEqual(navGuid, result.Collection.First().ObjectId);
 }