Exemplo n.º 1
0
 private static Delegate GenerateSingleMapper() {
     var config = new CustomConfig();
     var selectQuery = new SelectQuery<Post>(new Mock<ISelectQueryExecutor>().Object).Fetch(p => p.Blog) as SelectQuery<Post>;
     var writer = new SelectWriter(new SqlServer2012Dialect(), config);
     var result = writer.GenerateSql(selectQuery);
     var mapper = new NonCollectionMapperGenerator(config);
     var func = mapper.GenerateNonCollectionMapper<Post>(result.FetchTree);
     return func.Item1;
 }
Exemplo n.º 2
0
        public void ExcludeRemovesColumnFromRoot()
        {
            var query  = this.GetSelectQuery <Post>();
            var config = new MutableConfiguration(new System.Configuration.ConnectionStringSettings("Default", "", "System.Data.SqlClient"));

            config.AddNamespaceOf <Post>();
            config.Setup <Post>().Property(p => p.Content).ExcludeByDefault();
            var writer = new SelectWriter(new SqlServerDialect(), config);
            var sql    = writer.GenerateSql(query);

            this.output.WriteLine(sql.Sql);
            Assert.Equal("select [PostId], [Title], [Rating], [AuthorId], [BlogId], [DoNotMap] from [Posts]", sql.Sql);
        }
Exemplo n.º 3
0
        public void IncludeAllReaddsColumn()
        {
            var query  = (SelectQuery <Post>) this.GetSelectQuery <Post>().Fetch(p => p.Blog).IncludeAll();
            var config = new MutableConfiguration(new System.Configuration.ConnectionStringSettings("Default", "", "System.Data.SqlClient"));

            config.AddNamespaceOf <Post>();
            config.Setup <Blog>().Property(b => b.Description).ExcludeByDefault();
            var writer = new SelectWriter(new SqlServerDialect(), config);
            var sql    = writer.GenerateSql(query);

            this.output.WriteLine(sql.Sql);
            Assert.Equal("select t.[PostId], t.[Title], t.[Content], t.[Rating], t.[AuthorId], t.[DoNotMap], t_1.[BlogId], t_1.[Title], t_1.[CreateDate], t_1.[Description], t_1.[OwnerId] from [Posts] as t left join [Blogs] as t_1 on t.BlogId = t_1.BlogId", sql.Sql);
        }
Exemplo n.º 4
0
        public void should_render_any_sql()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Any();
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Scalar);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT CAST(CASE WHEN EXISTS (SELECT * FROM [{0}] {1}) THEN 1 ELSE 0 END AS bit)", TableName1, select.From.Alias));
        }
Exemplo n.º 5
0
        public void Select_Order_By_Test()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.OrderBy(y => y.Name).OrderByDescending(y => y.Values["CompanyName"]);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM [{0}] {1} ORDER BY [name] ASC, [companyname] DESC", TableName1, select.From.Alias));
        }
Exemplo n.º 6
0
        private static Delegate GenerateSingleMapperWithFetch()
        {
            var config      = new CustomConfig();
            var selectQuery =
                new SelectQuery <Post>(new Mock <IProjectedSelectQueryExecutor>().Object).Fetch(p => p.Comments).Fetch(p => p.Blog) as SelectQuery <Post>;
            var writer = new SelectWriter(new SqlServer2012Dialect(), config);
            var result = writer.GenerateSql(selectQuery, new AutoNamingDynamicParameters());

            var mapper = new SingleCollectionMapperGenerator(config);
            var func   = mapper.GenerateCollectionMapper <Post>(result.FetchTree);

            return(func.Item1);
        }
Exemplo n.º 7
0
        private static Delegate GenerateThenFetchMapper()
        {
            var config      = new CustomConfig();
            var selectQuery =
                new SelectQuery <Post>(new Mock <ISelectQueryExecutor>().Object).FetchMany(p => p.Comments).ThenFetch(c => c.User) as SelectQuery <Post>;
            var writer = new SelectWriter(new SqlServer2012Dialect(), config);
            var result = writer.GenerateSql(selectQuery);

            var mapper = new SingleCollectionMapperGenerator(config);
            var func   = mapper.GenerateCollectionMapper <Post>(result.FetchTree);

            return(func.Item1);
        }
Exemplo n.º 8
0
        public void Select_Count_Test()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Count();
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Scalar);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT COUNT(*) FROM [{0}] {1}", TableName1, select.From.Alias));
        }
Exemplo n.º 9
0
        public void Select_Randomize_Test()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Randomize();
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM [{0}] {1} ORDER BY NEWID()", TableName1, select.From.Alias));
        }
Exemplo n.º 10
0
        public void ExplicitExcludeRemovesColumnFromRoot()
        {
            var query = (SelectQuery <Post>) this.GetSelectQuery <Post>()
                        .Exclude(p => p.Content);
            var config = new MutableConfiguration();

            config.AddNamespaceOf <Post>();
            var writer = new SelectWriter(new SqlServerDialect(), config);
            var sql    = writer.GenerateSql(query);

            this.output.WriteLine(sql.Sql);
            Assert.Equal("select [PostId], [Title], [Rating], [AuthorId], [BlogId], [DoNotMap] from [Posts]", sql.Sql);
        }
Exemplo n.º 11
0
        public void ExcludeRemovesColumnFromGet()
        {
            var config = new MutableConfiguration();

            config.AddNamespaceOf <Post>();
            config.Setup <Post>()
            .Property(p => p.Content)
            .ExcludeByDefault();
            var writer = new SelectWriter(new SqlServerDialect(), config);
            var sql    = writer.GenerateGetSql <Post, int>(1);

            this.output.WriteLine(sql.Sql);
            Assert.Equal("select [PostId], [Title], [Rating], [AuthorId], [BlogId], [DoNotMap] from [Posts] where PostId = @Id", sql.Sql);
        }
Exemplo n.º 12
0
        private static Delegate GenerateSingleAwkwardMapper()
        {
            var config      = new CustomConfig();
            var selectQuery =
                new SelectQuery <PostWithoutCollectionInitializerInConstructor>(new Mock <ISelectQueryExecutor>().Object).Fetch(p => p.Comments) as
                SelectQuery <PostWithoutCollectionInitializerInConstructor>;
            var writer = new SelectWriter(new SqlServer2012Dialect(), config);
            var result = writer.GenerateSql(selectQuery);

            var mapper = new SingleCollectionMapperGenerator(config);
            var func   = mapper.GenerateCollectionMapper <PostWithoutCollectionInitializerInConstructor>(result.FetchTree);

            return(func.Item1);
        }
Exemplo n.º 13
0
        public void Select_Where_Test()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Where(y => y.Name == "hello");
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(1);
            statement.Parameters.First().Value.ShouldEqual("hello");
            statement.Text.ShouldEqual(string.Format("SELECT * FROM [{0}] {1} WHERE ([{1}].[name] = @{2})", TableName1, select.From.Alias, statement.Parameters.First().Key));
        }
Exemplo n.º 14
0
        public void Select_Skip_And_Take_Test()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Skip(10).Take(10);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY [id]) AS [__RowNumber__] FROM [{0}] {1}) AS {1} WHERE [__RowNumber__] BETWEEN 11 AND 20",
                                                     TableName1, select.From.Alias));
        }
Exemplo n.º 15
0
        public void ExplicitExcludeRemovesFetchedParentColumn()
        {
            var query = (SelectQuery <Post>) this.GetSelectQuery <Post>()
                        .Fetch(p => p.Blog)
                        .Exclude(p => p.Blog.Description);
            var config = new MutableConfiguration();

            config.AddNamespaceOf <Post>();
            var writer = new SelectWriter(new SqlServerDialect(), config);
            var sql    = writer.GenerateSql(query, new AutoNamingDynamicParameters());

            this.output.WriteLine(sql.Sql);
            Assert.Equal("select t.[PostId], t.[Title], t.[Content], t.[Rating], t.[AuthorId], t.[DoNotMap], t_1.[BlogId], t_1.[Title], t_1.[CreateDate], t_1.[OwnerId] from [Posts] as t left join [Blogs] as t_1 on t.BlogId = t_1.BlogId", sql.Sql);
        }
Exemplo n.º 16
0
        public void Select_Paged_Order_By_Test()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.OrderBy(y => y.Name).OrderByDescending(y => y.Values["CompanyName"]).Skip(10).Take(10);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY [name] ASC, [companyname] DESC) AS [__RowNumber__] FROM [{0}] {1}) AS {1} WHERE [__RowNumber__] BETWEEN 11 AND 20",
                                                     TableName1, select.From.Alias));
        }
Exemplo n.º 17
0
        public void should_render_duplicates_with_double_order_descending_sql()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Duplicates(x => x.Name, x => x.Age, Order.Descending, x => x.Created, Order.Ascending);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY [name] ORDER BY [age] DESC, [created] ASC) AS [__Partition__] FROM [{0}] {1}) AS {1} WHERE [__Partition__] > 1",
                                                     TableName1, select.From.Alias));
        }
Exemplo n.º 18
0
        public void should_render_multi_distinct_sql()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Distinct(x => x.Name.ToUpper()).Distinct(x => x.Age);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY UPPER([name]), [age] ORDER BY UPPER([name]) ASC, [age] ASC) AS [__Partition__] FROM [{0}] {1}) AS {1} WHERE [__Partition__] = 1",
                                                     TableName1, select.From.Alias));
        }
Exemplo n.º 19
0
        public void should_render_sql_with_nolock_hint_in_nested_query()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Skip(10);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map, noLock : true);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY [id]) AS [__RowNumber__] FROM [{0}] {1} WITH (NOLOCK)) AS {1} WHERE [__RowNumber__] >= 11",
                                                     TableName1, select.From.Alias));
        }
Exemplo n.º 20
0
        public void should_render_duplicates_with_precedence_sql()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Duplicates(x => x.Name, x => x.Age > 50, Order.Ascending);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(1);
            statement.Parameters.First().Value.ShouldEqual(50);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY [name] ORDER BY CASE WHEN ([{1}].[age] > @{2}) THEN 1 ELSE 0 END ASC) AS [__Partition__] FROM [{0}] {1}) AS {1} WHERE [__Partition__] > 1",
                                                     TableName1, select.From.Alias, statement.Parameters.First().Key));
        }
Exemplo n.º 21
0
        public void Chained_Union_Top_Test()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Union(MockQueryable <Entity> .Create(TableName2).Take(1).Union(MockQueryable <Entity> .Create(TableName3).Take(2)));
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Parameters.Count().ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT TOP (2) * FROM [XLIST_3] {0} UNION SELECT TOP (1) * FROM [XLIST_2] {1} UNION SELECT * FROM [XLIST_1] {2}",
                                                     select.From.Queries[0].From.Queries[0].From.Alias,
                                                     select.From.Queries[0].From.Queries[1].From.Alias,
                                                     select.From.Queries[1].From.Alias));
        }
Exemplo n.º 22
0
        public void should_render_any_and_where_sql()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Any(x => x.Age == 44);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Scalar);
            statement.Parameters.Count.ShouldEqual(1);
            statement.Parameters.First().Value.ShouldEqual(44);
            statement.Text.ShouldEqual(string.Format("SELECT CAST(CASE WHEN EXISTS (SELECT * FROM [{0}] {1} WHERE ([{1}].[age] = @{2})) THEN 1 ELSE 0 END AS bit)",
                                                     TableName1, select.From.Alias, statement.Parameters.First().Key));
        }
Exemplo n.º 23
0
        public void Select_Paged_Where_Test()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Where(y => y.Name == "hello").Skip(10).Take(10);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(1);
            statement.Parameters.First().Value.ShouldEqual("hello");
            statement.Text.ShouldEqual(string.Format("SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY [id]) AS [__RowNumber__] FROM [{0}] {1} WHERE ([{1}].[name] = @{2})) AS {1} WHERE [__RowNumber__] BETWEEN 11 AND 20",
                                                     TableName1, select.From.Alias, statement.Parameters.First().Key));
        }
Exemplo n.º 24
0
        public void ExcludeRemovesColumnFromRoot()
        {
            var query  = this.GetSelectQuery <Post>();
            var config = new MutableConfiguration();

            config.AddNamespaceOf <Post>();
            config.Setup <Post>()
            .Property(p => p.Content)
            .ExcludeByDefault();
            var writer = new SelectWriter(new SqlServerDialect(), config);
            var sql    = writer.GenerateSql(query, new AutoNamingDynamicParameters());

            this.output.WriteLine(sql.Sql);
            Assert.Equal("select [PostId], [Title], [Rating], [AuthorId], [BlogId], [DoNotMap] from [Posts]", sql.Sql);
        }
Exemplo n.º 25
0
        public void ExcludeRemovesFetchedCollectionColumn()
        {
            var query = (SelectQuery <Post>) this.GetSelectQuery <Post>()
                        .Fetch(p => p.Comments);
            var config = new MutableConfiguration();

            config.AddNamespaceOf <Post>();
            config.Setup <Comment>()
            .Property(c => c.Content)
            .ExcludeByDefault();
            var writer = new SelectWriter(new SqlServerDialect(), config);
            var sql    = writer.GenerateSql(query, new AutoNamingDynamicParameters());

            this.output.WriteLine(sql.Sql);
            Assert.Equal("select t.[PostId], t.[Title], t.[Content], t.[Rating], t.[AuthorId], t.[BlogId], t.[DoNotMap], t_1.[CommentId], t_1.[PostId], t_1.[UserId], t_1.[CommentDate] from [Posts] as t left join [Comments] as t_1 on t.PostId = t_1.PostId order by t.[PostId]", sql.Sql);
        }
Exemplo n.º 26
0
        private TResult ExecuteQuery <TResult>(Select select)
        {
            IEnumerable <string> columns = null;

            if (select.From.HasQueries)
            {
                var columnsStatement = SchemaWriter.CreateUnionColumnsStatement(select);
                columns = Command.Create(columnsStatement, _profiler)
                          .ExecuteEnumerable <string>(_connectionManager);
            }
            var selectStatement = SelectWriter <TEntity> .CreateStatement(
                select, _map, columns, _noLock);

            return((TResult)(new Loader <TEntity>(Command.Create(selectStatement,
                                                                 _profiler), _map).Execute(_connectionManager)));
        }
Exemplo n.º 27
0
        public void Select_Except_Test()
        {
            var query1 = MockQueryable <Entity> .Create(TableName1);

            var query2 = MockQueryable <Entity> .Create(TableName2);

            query1.Except(query2, x => x.Name, x => x.Age);
            var select = QueryVisitor <Entity> .CreateModel(query1.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM [{0}] {1} WHERE NOT EXISTS (SELECT TOP (1) [name], [age] FROM [{2}] {3} WHERE (([name] = [{1}].[name]) AND ([age] = [{1}].[age])))",
                                                     TableName1,
                                                     select.From.Alias,
                                                     TableName2,
                                                     select.SetOperatons[0].Select.From.Alias));
        }
Exemplo n.º 28
0
        public void Select_Except_Where_Test()
        {
            var query1 = MockQueryable <Entity> .Create(TableName1);

            var query2 = MockQueryable <Entity> .Create(TableName2);

            query1.Where(x => x.Active).Except(query2, x => x.Name.ToUpper(), x => x.Age);
            var select = QueryVisitor <Entity> .CreateModel(query1.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(1);
            statement.Parameters.First().Value.ShouldEqual(true);
            statement.Text.ShouldEqual(string.Format("SELECT * FROM [{0}] {1} WHERE ([{1}].[active] = @{3}) AND NOT EXISTS (SELECT TOP (1) [UPPER([name])], [age] FROM [{2}] {4} WHERE ((UPPER([name]) = UPPER([{1}].[name])) AND ([age] = [{1}].[age])))",
                                                     TableName1,
                                                     select.From.Alias,
                                                     TableName2,
                                                     statement.Parameters.First().Key,
                                                     select.SetOperatons[0].Select.From.Alias));
        }
Exemplo n.º 29
0
        public void Select_Skip_Projection_Test()
        {
            var query = MockQueryable <Entity> .Create(TableName1);

            query.Skip(10);
            var select = QueryVisitor <Entity> .CreateModel(query.Expression, x => ((MockQueryable <Entity>)x).Name).Select;

            select.Projection = new List <SelectProjection>
            {
                new SelectProjection {
                    Projection = Projection.Create.Field("Name")
                },
                new SelectProjection {
                    Projection = Projection.Create.Field("Age")
                }
            };
            var statement = SelectWriter <Entity> .CreateStatement(select, Map);

            statement.Result.ShouldEqual(Statement.ResultType.Multiple);
            statement.Parameters.Count.ShouldEqual(0);
            statement.Text.ShouldEqual(string.Format("SELECT [name], [age] FROM (SELECT [name], [age], ROW_NUMBER() OVER (ORDER BY [name], [age]) AS [__RowNumber__] FROM [{0}] {1}) AS {1} WHERE [__RowNumber__] >= 11",
                                                     TableName1, select.From.Alias));
        }
Exemplo n.º 30
0
        public void FetchManyNonRootWorks() {
            var config = new CustomConfig();
            var selectQuery =
                new SelectQuery<PostTag>(new Mock<ISelectQueryExecutor>().Object).FetchMany(p => p.Post.Comments).ThenFetch(c => c.User) as
                SelectQuery<PostTag>;
            var writer = new SelectWriter(new SqlServer2012Dialect(), config);
            var result = writer.GenerateSql(selectQuery);
            var mapper = new SingleCollectionMapperGenerator(config);
            var funcFac = mapper.GenerateCollectionMapper<PostTag>(result.FetchTree).Item1;

            // setup the scenario
            var tag1 = new PostTag { PostTagId = 1 };
            var tag2 = new PostTag { PostTagId = 2 };
            var tag3 = new PostTag { PostTagId = 3 };
            var post1 = new Post { PostId = 1, Title = "Foo" };
            var anotherPost1 = new Post { PostId = 1, Title = "Foo" };
            var post2 = new Post { PostId = 2, Title = "Foo" };
            var post3 = new Post { PostId = 3, Title = "Foo" };
            var post4 = new Post { PostId = 4, Title = "Foo" };
            var comment1 = new Comment { CommentId = 1 };
            var comment2 = new Comment { CommentId = 2 };
            var comment3 = new Comment { CommentId = 3 };
            var comment4 = new Comment { CommentId = 4 };
            var comment5 = new Comment { CommentId = 5 };
            var comment6 = new Comment { CommentId = 6 };
            var user1 = new User { UserId = 1 };
            var user2 = new User { UserId = 2 };
            var user3 = new User { UserId = 3 };
            var user4 = new User { UserId = 4 };
            var user5 = new User { UserId = 5 };

            PostTag currentRoot = null;
            IList<PostTag> results = new List<PostTag>();
            var func = (Func<object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results);
            func(new object[] { tag1, post1, comment1, user1 });
            func(new object[] { tag1, post1, comment2, user1 });
            func(new object[] { tag1, post1, comment3, user2 });
            func(new object[] { tag2, anotherPost1, comment1, user1 });
            func(new object[] { tag2, anotherPost1, comment2, user1 });
            func(new object[] { tag2, anotherPost1, comment3, user2 });
            func(new object[] { tag3, post2, comment4, user3 });
            func(new object[] { tag3, post2, comment5, user4 });
            func(new object[] { tag3, post2, comment6, user5 });

            Assert.Equal(3, results.Count);
            Assert.Equal(3, results.First().Post.Comments.Count);
            Assert.Equal(1, results.First().Post.PostId);
            Assert.Equal(1, results.First().Post.Comments.First().User.UserId);
            Assert.Equal(1, results.First().Post.Comments.ElementAt(1).User.UserId);
            Assert.Equal(2, results.First().Post.Comments.ElementAt(2).User.UserId);
            Assert.Equal(3, results.ElementAt(1).Post.Comments.Count);
            Assert.Equal(1, results.ElementAt(1).Post.PostId);
            Assert.Equal(1, results.ElementAt(1).Post.Comments.First().User.UserId);
            Assert.Equal(1, results.ElementAt(1).Post.Comments.ElementAt(1).User.UserId);
            Assert.Equal(2, results.ElementAt(1).Post.Comments.ElementAt(2).User.UserId);
            Assert.Equal(3, results.ElementAt(2).Post.Comments.Count);
            Assert.Equal(2, results.ElementAt(2).Post.PostId);
            Assert.Equal(4, results.ElementAt(2).Post.Comments.First().CommentId);
            Assert.Equal(5, results.ElementAt(2).Post.Comments.ElementAt(1).CommentId);
            Assert.Equal(6, results.ElementAt(2).Post.Comments.ElementAt(2).CommentId);
        }
Exemplo n.º 31
0
        public void MultipleManyToManyFetchingWorks() {
            // setup the factory
            var config = new CustomConfig();
            var selectQuery =
                new SelectQuery<Post>(new Mock<ISelectQueryExecutor>().Object).FetchMany(p => p.Tags)
                                                                              .ThenFetch(p => p.ElTag)
                                                                              .FetchMany(p => p.DeletedTags)
                                                                              .ThenFetch(t => t.ElTag) as SelectQuery<Post>;
            var writer = new SelectWriter(new SqlServer2012Dialect(), config);
            var result = writer.GenerateSql(selectQuery);
            var mapper = new MultiCollectionMapperGenerator(config);
            var funcFac = mapper.GenerateMultiCollectionMapper<Post>(result.FetchTree).Item1;

            // setup the scenario
            var post1 = new Post { PostId = 1 };
            var tag1 = new Tag { TagId = 1 };
            var tag2 = new Tag { TagId = 2 };
            var tag3 = new Tag { TagId = 3 };
            var postTag1 = new PostTag { PostTagId = 1 };
            var postTag2 = new PostTag { PostTagId = 2 };
            var postTag3 = new PostTag { PostTagId = 3 };

            // act
            Post currentRoot = null;
            IList<Post> results = new List<Post>();
            var dict0 = new Dictionary<int, PostTag>();
            var hashsetPair0 = new HashSet<Tuple<int, int>>();
            var dict1 = new Dictionary<int, PostTag>();
            var hashsetPair1 = new HashSet<Tuple<int, int>>();

            var func = (Func<object[], Post>)funcFac.DynamicInvoke(currentRoot, results, dict0, hashsetPair0, dict1, hashsetPair1);
            func(new object[] { post1, postTag2, tag2, postTag1, tag1 });
            func(new object[] { post1, postTag3, tag3, postTag1, tag1 });

            Assert.Equal(1, results.Count);
            Assert.Equal(1, results[0].Tags.Count);
            Assert.Equal(1, results[0].Tags[0].PostTagId);
            Assert.Equal(2, results[0].DeletedTags.Count);
            Assert.Equal(2, results[0].DeletedTags[0].PostTagId);
            Assert.Equal(3, results[0].DeletedTags[1].PostTagId);
        }
Exemplo n.º 32
0
        public void NestedMultipleOneToManyFetchingWorksTrackingEnabledLast() {
            // setup the factory
            var config = new CustomConfig();
            var selectQuery =
                new SelectQuery<Blog>(new Mock<ISelectQueryExecutor>().Object).FetchMany(b => b.Posts)
                                                                              .ThenFetchMany(p => p.Tags)
                                                                              .ThenFetch(t => t.ElTag)
                                                                              .FetchMany(b => b.Posts)
                                                                              .ThenFetchMany(p => p.DeletedTags)
                                                                              .ThenFetch(t => t.ElTag)
                                                                              .FetchMany(p => p.Posts)
                                                                              .ThenFetch(p => p.Author) as SelectQuery<Blog>;
            var writer = new SelectWriter(new SqlServer2012Dialect(), config);
            var result = writer.GenerateSql(selectQuery);
            var mapper = new MultiCollectionMapperGenerator(config);
            var funcFac = mapper.GenerateMultiCollectionMapper<Blog>(result.FetchTree).Item1;

            // setup the scenario
            var blog1 = new Blog { BlogId = 1 };
            var blog2 = new Blog { BlogId = 2 };
            var post1 = new Post { PostId = 1 };
            var post2 = new Post { PostId = 2 };
            var post3 = new Post { PostId = 3 };
            var posttag1 = new PostTag { PostTagId = 1 };
            var posttag2 = new PostTag { PostTagId = 2 };
            var posttag3 = new PostTag { PostTagId = 3 };
            var tag1 = new Tag { TagId = 1 };
            var tag2 = new Tag { TagId = 2 };
            var tag3 = new Tag { TagId = 3 };
            var tag4 = new Tag { TagId = 4 };
            var delPostTag1 = new PostTag { PostTagId = 3 };
            var delPostTag2 = new PostTag { PostTagId = 4 };
            var delPostTag3 = new PostTag { PostTagId = 5 };
            var author1 = new User { UserId = 1 };
            var author2 = new User { UserId = 2 };

            // act
            Blog currentRoot = null;
            IList<Blog> results = new List<Blog>();
            var dict0 = new Dictionary<int, Post>();
            var hashsetPair0 = new HashSet<Tuple<int, int>>();
            var dict1 = new Dictionary<int, PostTag>();
            var hashsetPair1 = new HashSet<Tuple<int, int>>();
            var dict2 = new Dictionary<int, PostTag>();
            var hashsetPair2 = new HashSet<Tuple<int, int>>();

            var func =
                (Func<object[], Blog>)funcFac.DynamicInvoke(currentRoot, results, dict0, hashsetPair0, dict1, hashsetPair1, dict2, hashsetPair2);
            func(new object[] { blog1, post1, author1, null, null, posttag1, tag1 });
            func(new object[] { blog1, post1, author1, null, null, posttag2, tag2 });
            func(new object[] { blog1, post2, author2, delPostTag1, tag3, null, null });
            func(new object[] { blog1, post2, author2, delPostTag2, tag4, null, null });
            func(new object[] { blog2, post3, author1, delPostTag1, tag3, posttag1, tag1 });
            func(new object[] { blog2, post3, author1, delPostTag2, tag4, posttag1, tag1 });
            func(new object[] { blog2, post3, author1, delPostTag3, tag4, posttag1, tag1 });
            func(new object[] { blog2, post3, author1, delPostTag1, tag3, posttag2, tag2 });
            func(new object[] { blog2, post3, author1, delPostTag2, tag4, posttag2, tag2 });
            func(new object[] { blog2, post3, author1, delPostTag3, tag4, posttag2, tag2 });
            func(new object[] { blog2, post3, author1, delPostTag1, tag3, posttag3, tag3 });
            func(new object[] { blog2, post3, author1, delPostTag2, tag4, posttag3, tag3 });
            func(new object[] { blog2, post3, author1, delPostTag3, tag4, posttag3, tag3 });

            Assert.False(((ITrackedEntity)results[0]).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Posts[0]).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Posts[0].Author).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Posts[0].Tags[0]).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Posts[0].Tags[0].ElTag).GetDirtyProperties().Any());
        }
Exemplo n.º 33
0
        private static Delegate GenerateSingleAwkwardMapper() {
            var config = new CustomConfig();
            var selectQuery =
                new SelectQuery<PostWithoutCollectionInitializerInConstructor>(new Mock<ISelectQueryExecutor>().Object).Fetch(p => p.Comments) as
                SelectQuery<PostWithoutCollectionInitializerInConstructor>;
            var writer = new SelectWriter(new SqlServer2012Dialect(), config);
            var result = writer.GenerateSql(selectQuery);

            var mapper = new SingleCollectionMapperGenerator(config);
            var func = mapper.GenerateCollectionMapper<PostWithoutCollectionInitializerInConstructor>(result.FetchTree);
            return func.Item1;
        }
Exemplo n.º 34
0
        public void NestedMultipleOneToManyFetchingWorksTrackingEnabled()
        {
            // setup the factory
            var config      = new CustomConfig();
            var selectQuery =
                new SelectQuery <Blog>(new Mock <ISelectQueryExecutor>().Object).FetchMany(b => b.Posts)
                .ThenFetchMany(p => p.Tags)
                .ThenFetch(t => t.ElTag)
                .FetchMany(b => b.Posts)
                .ThenFetchMany(p => p.DeletedTags)
                .ThenFetch(t => t.ElTag)
                .FetchMany(p => p.Posts)
                .ThenFetch(p => p.Author) as SelectQuery <Blog>;
            var writer  = new SelectWriter(new SqlServer2012Dialect(), config);
            var result  = writer.GenerateSql(selectQuery);
            var mapper  = new MultiCollectionMapperGenerator(config);
            var funcFac = mapper.GenerateMultiCollectionMapper <Blog>(result.FetchTree).Item1;

            // setup the scenario
            var blog1 = new Blog {
                BlogId = 1
            };
            var blog2 = new Blog {
                BlogId = 2
            };
            var post1 = new Post {
                PostId = 1
            };
            var post2 = new Post {
                PostId = 2
            };
            var post3 = new Post {
                PostId = 3
            };
            var posttag1 = new PostTag {
                PostTagId = 1
            };
            var posttag2 = new PostTag {
                PostTagId = 2
            };
            var posttag3 = new PostTag {
                PostTagId = 3
            };
            var tag1 = new Tag {
                TagId = 1
            };
            var tag2 = new Tag {
                TagId = 2
            };
            var tag3 = new Tag {
                TagId = 3
            };
            var tag4 = new Tag {
                TagId = 4
            };
            var delPostTag1 = new PostTag {
                PostTagId = 3
            };
            var delPostTag2 = new PostTag {
                PostTagId = 4
            };
            var delPostTag3 = new PostTag {
                PostTagId = 5
            };
            var author1 = new User {
                UserId = 1
            };
            var author2 = new User {
                UserId = 2
            };

            // act
            Blog         currentRoot  = null;
            IList <Blog> results      = new List <Blog>();
            var          dict0        = new Dictionary <int, Post>();
            var          hashsetPair0 = new HashSet <Tuple <int, int> >();
            var          dict1        = new Dictionary <int, PostTag>();
            var          hashsetPair1 = new HashSet <Tuple <int, int> >();
            var          dict2        = new Dictionary <int, PostTag>();
            var          hashsetPair2 = new HashSet <Tuple <int, int> >();

            var func =
                (Func <object[], Blog>)funcFac.DynamicInvoke(currentRoot, results, dict0, hashsetPair0, dict1, hashsetPair1, dict2, hashsetPair2);

            func(new object[] { blog1, post1, author1, null, null, posttag1, tag1 });
            func(new object[] { blog1, post1, author1, null, null, posttag2, tag2 });
            func(new object[] { blog1, post2, author2, delPostTag1, tag3, null, null });
            func(new object[] { blog1, post2, author2, delPostTag2, tag4, null, null });
            func(new object[] { blog2, post3, author1, delPostTag1, tag3, posttag1, tag1 });
            func(new object[] { blog2, post3, author1, delPostTag2, tag4, posttag1, tag1 });
            func(new object[] { blog2, post3, author1, delPostTag3, tag4, posttag1, tag1 });
            func(new object[] { blog2, post3, author1, delPostTag1, tag3, posttag2, tag2 });
            func(new object[] { blog2, post3, author1, delPostTag2, tag4, posttag2, tag2 });
            func(new object[] { blog2, post3, author1, delPostTag3, tag4, posttag2, tag2 });
            func(new object[] { blog2, post3, author1, delPostTag1, tag3, posttag3, tag3 });
            func(new object[] { blog2, post3, author1, delPostTag2, tag4, posttag3, tag3 });
            func(new object[] { blog2, post3, author1, delPostTag3, tag4, posttag3, tag3 });

            Assert.True(((ITrackedEntity)results[0]).IsTrackingEnabled());
            Assert.True(((ITrackedEntity)results[0].Posts[0]).IsTrackingEnabled());
            Assert.True(((ITrackedEntity)results[0].Posts[0].Author).IsTrackingEnabled());
            Assert.True(((ITrackedEntity)results[0].Posts[0].Tags[0]).IsTrackingEnabled());
            Assert.True(((ITrackedEntity)results[0].Posts[0].Tags[0].ElTag).IsTrackingEnabled());
        }
Exemplo n.º 35
0
        public void FetchManyNonRootTrackingEnabledLast()
        {
            var config      = new CustomConfig();
            var selectQuery =
                new SelectQuery <PostTag>(new Mock <ISelectQueryExecutor>().Object).FetchMany(p => p.Post.Comments).ThenFetch(c => c.User) as
                SelectQuery <PostTag>;
            var writer  = new SelectWriter(new SqlServer2012Dialect(), config);
            var result  = writer.GenerateSql(selectQuery);
            var mapper  = new SingleCollectionMapperGenerator(config);
            var funcFac = mapper.GenerateCollectionMapper <PostTag>(result.FetchTree).Item1;

            // setup the scenario
            var tag1 = new PostTag {
                PostTagId = 1
            };
            var tag2 = new PostTag {
                PostTagId = 2
            };
            var tag3 = new PostTag {
                PostTagId = 3
            };
            var post1 = new Post {
                PostId = 1, Title = "Foo"
            };
            var anotherPost1 = new Post {
                PostId = 1, Title = "Foo"
            };
            var post2 = new Post {
                PostId = 2, Title = "Foo"
            };
            var post3 = new Post {
                PostId = 3, Title = "Foo"
            };
            var post4 = new Post {
                PostId = 4, Title = "Foo"
            };
            var comment1 = new Comment {
                CommentId = 1
            };
            var comment2 = new Comment {
                CommentId = 2
            };
            var comment3 = new Comment {
                CommentId = 3
            };
            var comment4 = new Comment {
                CommentId = 4
            };
            var comment5 = new Comment {
                CommentId = 5
            };
            var comment6 = new Comment {
                CommentId = 6
            };
            var user1 = new User {
                UserId = 1
            };
            var user2 = new User {
                UserId = 2
            };
            var user3 = new User {
                UserId = 3
            };
            var user4 = new User {
                UserId = 4
            };
            var user5 = new User {
                UserId = 5
            };

            PostTag         currentRoot = null;
            IList <PostTag> results     = new List <PostTag>();
            var             func        = (Func <object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results);

            func(new object[] { tag1, post1, comment1, user1 });
            func(new object[] { tag1, post1, comment2, user1 });
            func(new object[] { tag1, post1, comment3, user2 });
            func(new object[] { tag2, anotherPost1, comment1, user1 });
            func(new object[] { tag2, anotherPost1, comment2, user1 });
            func(new object[] { tag2, anotherPost1, comment3, user2 });
            func(new object[] { tag3, post2, comment4, user3 });
            func(new object[] { tag3, post2, comment5, user4 });
            func(new object[] { tag3, post2, comment6, user5 });

            Assert.False(((ITrackedEntity)results[0]).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Post).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Post.Comments[0]).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Post.Comments[0].User).GetDirtyProperties().Any());
        }
Exemplo n.º 36
0
        public void FetchManyNonRootTrackingEnabledLast() {
            var config = new CustomConfig();
            var selectQuery =
                new SelectQuery<PostTag>(new Mock<ISelectQueryExecutor>().Object).FetchMany(p => p.Post.Comments).ThenFetch(c => c.User) as
                SelectQuery<PostTag>;
            var writer = new SelectWriter(new SqlServer2012Dialect(), config);
            var result = writer.GenerateSql(selectQuery);
            var mapper = new SingleCollectionMapperGenerator(config);
            var funcFac = mapper.GenerateCollectionMapper<PostTag>(result.FetchTree).Item1;

            // setup the scenario
            var tag1 = new PostTag { PostTagId = 1 };
            var tag2 = new PostTag { PostTagId = 2 };
            var tag3 = new PostTag { PostTagId = 3 };
            var post1 = new Post { PostId = 1, Title = "Foo" };
            var anotherPost1 = new Post { PostId = 1, Title = "Foo" };
            var post2 = new Post { PostId = 2, Title = "Foo" };
            var post3 = new Post { PostId = 3, Title = "Foo" };
            var post4 = new Post { PostId = 4, Title = "Foo" };
            var comment1 = new Comment { CommentId = 1 };
            var comment2 = new Comment { CommentId = 2 };
            var comment3 = new Comment { CommentId = 3 };
            var comment4 = new Comment { CommentId = 4 };
            var comment5 = new Comment { CommentId = 5 };
            var comment6 = new Comment { CommentId = 6 };
            var user1 = new User { UserId = 1 };
            var user2 = new User { UserId = 2 };
            var user3 = new User { UserId = 3 };
            var user4 = new User { UserId = 4 };
            var user5 = new User { UserId = 5 };

            PostTag currentRoot = null;
            IList<PostTag> results = new List<PostTag>();
            var func = (Func<object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results);
            func(new object[] { tag1, post1, comment1, user1 });
            func(new object[] { tag1, post1, comment2, user1 });
            func(new object[] { tag1, post1, comment3, user2 });
            func(new object[] { tag2, anotherPost1, comment1, user1 });
            func(new object[] { tag2, anotherPost1, comment2, user1 });
            func(new object[] { tag2, anotherPost1, comment3, user2 });
            func(new object[] { tag3, post2, comment4, user3 });
            func(new object[] { tag3, post2, comment5, user4 });
            func(new object[] { tag3, post2, comment6, user5 });

            Assert.False(((ITrackedEntity)results[0]).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Post).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Post.Comments[0]).GetDirtyProperties().Any());
            Assert.False(((ITrackedEntity)results[0].Post.Comments[0].User).GetDirtyProperties().Any());
        }