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 IEnumerable MapFromReader(IDataReader reader, SelectClause selectClause)
 {
     var successRead = reader.Read();
     LayerInfo firstLayer = null;
     LayerInfo lastLayer = null;
     foreach (var selectPart in selectClause.Parts)
     {
         var typePart = selectPart as TypePart;
         if (typePart != null)
         {
             var layer = new LayerInfo
             {
                 TypePart = typePart,
                 CreatingDelegate = GetTypeInstanceCreateDelegate(typePart.Type),
                 FillingDelegates = typePart.Tables.Select(z => GetFiller(typePart.Type, z)).ToList(),
             };
             if (lastLayer != null)
                 lastLayer.AddNextLayer(layer);
             if (firstLayer == null)
                 firstLayer = layer;
             lastLayer = layer;
         }
         else
         {
             var expansion = selectPart as ExpansionPart;
             if (expansion != null)
             {
                 if(lastLayer == null)
                     throw new InvalidOperationException("No last layer. Primitives can't be first");
                 if(lastLayer.TypePart.Type != expansion.CollectingType)
                     throw new InvalidOperationException("Implementation accepts only collections for previous type's properties");
                 lastLayer.Expansions.Add(expansion);
             }
         }
     }
     while (successRead)
     {
         yield return LoadObjectTree(firstLayer, reader, out successRead);
     }
 }
        public QueryPlan GetQuery(string type, string[] paths, string[] includes = null)
        {
            var typeMapping = _metaInfoProvider.GetTypeMapping(type);
            var tables = typeMapping.Tables.OfType<EntityTable>().ToList();
            var mainTable = tables.First();
            var mainTableContext = GetTableContext(mainTable, "M");
            var selectParts = new List<SelectPart>();
            var selectBuilder = new SelectSqlBuilder(mainTableContext).SelectAll(mainTableContext);
            var mainPart = new TypePart
            {
                Type =  typeMapping,
                Tables = new List<Table>(tables)
            };
            selectParts.Add(mainPart);
            if (includes != null)
            {
                foreach (var include in includes)
                {
                    selectParts.AddRange(Include(selectBuilder, mainTableContext, typeMapping, include));
                }
            }
            foreach (var table in tables.Skip(1))
            {
                var secondContext = GetTableContext(table, "T");
                selectBuilder
                    .SelectAll(secondContext)
                    .InnerJoin(mainTableContext, secondContext, table.IdentityColumn.ColumnName, table.IdentityColumn.ColumnName);
            }
            foreach (var s in paths)
            {
                BuildSelectPredicate(selectBuilder, mainTableContext, typeMapping, s);
            }

            var selectClause = new SelectClause{ Parts = selectParts.ToArray() };
            return new QueryPlan
            {
                SqlString = selectBuilder.GetSql(),
                SelectClause = selectClause
            };
        }
        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());
        }
 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);
 }