public void ParentOfParentNotReturnedIfNotFetched()
        {
            var cloner = new FetchCloner(new TestConfiguration());
            var query  = new SelectQuery <Post>(new NonExecutingSelectQueryExecutor()).Fetch(p => p.Blog) as SelectQuery <Post>;
            var post   = new Post {
                PostId = 1,
                Title  = "Bar",
                Blog   = new Blog {
                    BlogId = 2, Description = "Foo", Owner = new User {
                        UserId = 4, Username = "******"
                    }
                }
            };
            var clone = cloner.Clone(query, post);

            Assert.False(ReferenceEquals(post, clone));
            Assert.False(ReferenceEquals(post.Blog, clone.Blog));
            Assert.False(ReferenceEquals(post.Title, clone.Title));
            Assert.False(ReferenceEquals(post.Blog.Description, clone.Blog.Description));
            Assert.False(ReferenceEquals(post.Blog.Owner, clone.Blog.Owner));
            Assert.Equal(1, clone.PostId);
            Assert.Equal("Bar", clone.Title);
            Assert.Equal(2, clone.Blog.BlogId);
            Assert.Equal("Foo", clone.Blog.Description);
            Assert.Equal(4, clone.Blog.Owner.UserId);
            Assert.Null(clone.Blog.Owner.Username);
        }
        public void FetchedCollectionNoThenFetchWorks()
        {
            var cloner = new FetchCloner(new TestConfiguration());
            var query  = new SelectQuery <Blog>(new NonExecutingSelectQueryExecutor()).Fetch(p => p.Posts) as SelectQuery <Blog>;
            var blog   = new Blog {
                BlogId = 1,
                Posts  =
                    new List <Post> {
                    new Post {
                        PostId = 2, Title = "Foo", Author = new User {
                            UserId = 5, Username = "******"
                        }
                    },
                    new Post {
                        PostId = 3, Title = "Boo", Author = new User {
                            UserId = 7, Username = "******"
                        }
                    }
                }
            };
            var clone = cloner.Clone(query, blog);

            Assert.False(ReferenceEquals(blog, clone));
            Assert.False(ReferenceEquals(blog.Posts, clone.Posts));
            Assert.False(ReferenceEquals(blog.Posts[0], clone.Posts[0]));
            Assert.False(ReferenceEquals(blog.Posts[1], clone.Posts[1]));
            Assert.False(ReferenceEquals(blog.Posts[0].Author, clone.Posts[0].Author));
            Assert.False(ReferenceEquals(blog.Posts[1].Author, clone.Posts[1].Author));
            Assert.Equal("Foo", clone.Posts[0].Title);
            Assert.Equal("Boo", clone.Posts[1].Title);
            Assert.Equal(5, clone.Posts[0].Author.UserId);
            Assert.Equal(7, clone.Posts[1].Author.UserId);
            Assert.Null(clone.Posts[0].Author.Username);
            Assert.Null(clone.Posts[1].Author.Username);
        }
Example #3
0
        public void NoFetchReturnsEntityWithOnlyPrimaryKey() {
            var cloner = new FetchCloner(new TestConfiguration());
            var query = (new SelectQuery<Post>(new NonExecutingSelectQueryExecutor()));
            var post = new Post { PostId = 1, Title = "Bar", Blog = new Blog { BlogId = 2, Description = "Foo" } };
            var clone = cloner.Clone(query, post);

            Assert.False(ReferenceEquals(post, clone));
            Assert.False(ReferenceEquals(post.Blog, clone.Blog));
            Assert.False(ReferenceEquals(post.Title, clone.Title));
            Assert.Equal(1, clone.PostId);
            Assert.Equal("Bar", clone.Title);
            Assert.Equal(2, clone.Blog.BlogId);
            Assert.Null(clone.Blog.Description);
        }
Example #4
0
        public void FetchedEntityIsReturned() {
            var cloner = new FetchCloner(new TestConfiguration());
            var query = new SelectQuery<Post>(new NonExecutingSelectQueryExecutor()).Fetch(p => p.Blog) as SelectQuery<Post>;
            var post = new Post { PostId = 1, Title = "Bar", Blog = new Blog { BlogId = 2, Description = "Foo" } };
            var clone = cloner.Clone(query, post);

            Assert.False(ReferenceEquals(post, clone));
            Assert.False(ReferenceEquals(post.Blog, clone.Blog));
            Assert.False(ReferenceEquals(post.Title, clone.Title));
            Assert.False(ReferenceEquals(post.Blog.Description, clone.Blog.Description));
            Assert.Equal(1, clone.PostId);
            Assert.Equal("Bar", clone.Title);
            Assert.Equal(2, clone.Blog.BlogId);
            Assert.Equal("Foo", clone.Blog.Description);
        }
Example #5
0
        public void FetchedCollectionWorks() {
            var cloner = new FetchCloner(new TestConfiguration());
            var query = new SelectQuery<Blog>(new NonExecutingSelectQueryExecutor()).Fetch(p => p.Posts) as SelectQuery<Blog>;
            var blog = new Blog {
                                    BlogId = 1,
                                    Posts = new List<Post> { new Post { PostId = 2, Title = "Foo" }, new Post { PostId = 3, Title = "Boo" } }
                                };
            var clone = cloner.Clone(query, blog);

            Assert.False(ReferenceEquals(blog, clone));
            Assert.False(ReferenceEquals(blog.Posts, clone.Posts));
            Assert.False(ReferenceEquals(blog.Posts[0], clone.Posts[0]));
            Assert.False(ReferenceEquals(blog.Posts[1], clone.Posts[1]));
            Assert.Equal("Foo", clone.Posts[0].Title);
            Assert.Equal("Boo", clone.Posts[1].Title);
        }
        public void NoFetchReturnsEntityWithOnlyPrimaryKey()
        {
            var cloner = new FetchCloner(new TestConfiguration());
            var query  = (new SelectQuery <Post>(new NonExecutingSelectQueryExecutor()));
            var post   = new Post {
                PostId = 1, Title = "Bar", Blog = new Blog {
                    BlogId = 2, Description = "Foo"
                }
            };
            var clone = cloner.Clone(query, post);

            Assert.False(ReferenceEquals(post, clone));
            Assert.False(ReferenceEquals(post.Blog, clone.Blog));
            Assert.False(ReferenceEquals(post.Title, clone.Title));
            Assert.Equal(1, clone.PostId);
            Assert.Equal("Bar", clone.Title);
            Assert.Equal(2, clone.Blog.BlogId);
            Assert.Null(clone.Blog.Description);
        }
        public void FetchedEntityIsReturned()
        {
            var cloner = new FetchCloner(new TestConfiguration());
            var query  = new SelectQuery <Post>(new NonExecutingSelectQueryExecutor()).Fetch(p => p.Blog) as SelectQuery <Post>;
            var post   = new Post {
                PostId = 1, Title = "Bar", Blog = new Blog {
                    BlogId = 2, Description = "Foo"
                }
            };
            var clone = cloner.Clone(query, post);

            Assert.False(ReferenceEquals(post, clone));
            Assert.False(ReferenceEquals(post.Blog, clone.Blog));
            Assert.False(ReferenceEquals(post.Title, clone.Title));
            Assert.False(ReferenceEquals(post.Blog.Description, clone.Blog.Description));
            Assert.Equal(1, clone.PostId);
            Assert.Equal("Bar", clone.Title);
            Assert.Equal(2, clone.Blog.BlogId);
            Assert.Equal("Foo", clone.Blog.Description);
        }
Example #8
0
        public void ParentOfParentNotReturnedIfNotFetched() {
            var cloner = new FetchCloner(new TestConfiguration());
            var query = new SelectQuery<Post>(new NonExecutingSelectQueryExecutor()).Fetch(p => p.Blog) as SelectQuery<Post>;
            var post = new Post {
                                    PostId = 1,
                                    Title = "Bar",
                                    Blog = new Blog { BlogId = 2, Description = "Foo", Owner = new User { UserId = 4, Username = "******" } }
                                };
            var clone = cloner.Clone(query, post);

            Assert.False(ReferenceEquals(post, clone));
            Assert.False(ReferenceEquals(post.Blog, clone.Blog));
            Assert.False(ReferenceEquals(post.Title, clone.Title));
            Assert.False(ReferenceEquals(post.Blog.Description, clone.Blog.Description));
            Assert.False(ReferenceEquals(post.Blog.Owner, clone.Blog.Owner));
            Assert.Equal(1, clone.PostId);
            Assert.Equal("Bar", clone.Title);
            Assert.Equal(2, clone.Blog.BlogId);
            Assert.Equal("Foo", clone.Blog.Description);
            Assert.Equal(4, clone.Blog.Owner.UserId);
            Assert.Null(clone.Blog.Owner.Username);
        }
        public void FetchedCollectionWorks()
        {
            var cloner = new FetchCloner(new TestConfiguration());
            var query  = new SelectQuery <Blog>(new NonExecutingSelectQueryExecutor()).Fetch(p => p.Posts) as SelectQuery <Blog>;
            var blog   = new Blog {
                BlogId = 1,
                Posts  = new List <Post> {
                    new Post {
                        PostId = 2, Title = "Foo"
                    }, new Post {
                        PostId = 3, Title = "Boo"
                    }
                }
            };
            var clone = cloner.Clone(query, blog);

            Assert.False(ReferenceEquals(blog, clone));
            Assert.False(ReferenceEquals(blog.Posts, clone.Posts));
            Assert.False(ReferenceEquals(blog.Posts[0], clone.Posts[0]));
            Assert.False(ReferenceEquals(blog.Posts[1], clone.Posts[1]));
            Assert.Equal("Foo", clone.Posts[0].Title);
            Assert.Equal("Boo", clone.Posts[1].Title);
        }
Example #10
0
        public void FetchedCollectionThenFetchWorks() {
            var cloner = new FetchCloner(new TestConfiguration());
            var query =
                new SelectQuery<Blog>(new NonExecutingSelectQueryExecutor()).FetchMany(p => p.Posts).ThenFetch(p => p.Author) as SelectQuery<Blog>;
            var blog = new Blog {
                                    BlogId = 1,
                                    Posts =
                                        new List<Post> {
                                                           new Post { PostId = 2, Title = "Foo", Author = new User { UserId = 5, Username = "******" } },
                                                           new Post { PostId = 3, Title = "Boo", Author = new User { UserId = 7, Username = "******" } }
                                                       }
                                };
            var clone = cloner.Clone(query, blog);

            Assert.False(ReferenceEquals(blog, clone));
            Assert.False(ReferenceEquals(blog.Posts, clone.Posts));
            Assert.False(ReferenceEquals(blog.Posts[0], clone.Posts[0]));
            Assert.False(ReferenceEquals(blog.Posts[1], clone.Posts[1]));
            Assert.False(ReferenceEquals(blog.Posts[0].Author, clone.Posts[0].Author));
            Assert.False(ReferenceEquals(blog.Posts[1].Author, clone.Posts[1].Author));
            Assert.Equal("Foo", clone.Posts[0].Title);
            Assert.Equal("Boo", clone.Posts[1].Title);
            Assert.Equal("james", clone.Posts[0].Author.Username);
            Assert.Equal("mark", clone.Posts[1].Author.Username);
        }
Example #11
0
        public IEnumerable <T> Query <T>(IDbConnection connection, IDbTransaction transaction, SelectQuery <T> query) where T : class, new()
        {
            this.AssertConfigured();
            var table = this.tables[typeof(T)];
            var whereClauseNullCheckRewriter  = new WhereClauseNullCheckRewriter();
            var whereClauseOpEqualityRewriter = new WhereClauseOpEqualityRewriter();
            var fetchCloner = new FetchCloner(this.Configuration);
            var enumerable  =
                typeof(InMemoryTable <,>).MakeGenericType(typeof(T), this.Configuration.GetMap(typeof(T)).PrimaryKey.Type)
                .GetMethod("Query")
                .Invoke(table, new object[0]) as IEnumerable <T>;

            var fetchParser = new FetchTreeParser(this.Configuration);
            int numberCollectionFetches;
            int aliasCounter;
            var fetchTree = fetchParser.GetFetchTree(query, out aliasCounter, out numberCollectionFetches);

            // we may have to query across non-fetched stuff
            var baseWriter = new BaseWriter(new SqlServer2012Dialect(), this.configuration);

            baseWriter.AddWhereClause(query.WhereClauses, new StringBuilder(), ref fetchTree);

            // note that this fetches all the things in the tree as the whereclause may reference things not fetched
            if (fetchTree != null)
            {
                enumerable = enumerable.Fetch(fetchTree, this.tables);
            }

            foreach (var whereClause in query.WhereClauses)
            {
                var rewrittenWhereClause = whereClauseNullCheckRewriter.Rewrite(whereClause);
                rewrittenWhereClause = whereClauseOpEqualityRewriter.Rewrite(rewrittenWhereClause);
                enumerable           = enumerable.Where(rewrittenWhereClause.Compile());
            }

            var firstOrderClause = true;

            foreach (var orderClause in query.OrderClauses)
            {
                var expr     = ((LambdaExpression)orderClause.Expression).Compile();
                var funcName = firstOrderClause
                                   ? (orderClause.Direction == ListSortDirection.Ascending ? "OrderBy" : "OrderByDescending")
                                   : (orderClause.Direction == ListSortDirection.Ascending ? "ThenBy" : "ThenByDescending");
                var orderBy =
                    typeof(Enumerable).GetMethods()
                    .Single(
                        m =>
                        m.GetParameters().Count() == 2 &&
                        m.Name == funcName).MakeGenericMethod(typeof(T), ((LambdaExpression)orderClause.Expression).ReturnType);
                enumerable       = (IEnumerable <T>)orderBy.Invoke(null, new object[] { enumerable, expr });
                firstOrderClause = false;
            }

            if (query.SkipN > 0)
            {
                enumerable = enumerable.Skip(query.SkipN);
            }

            if (query.TakeN > 0)
            {
                enumerable = enumerable.Take(query.TakeN);
            }

            foreach (var entity in enumerable)
            {
                yield return(fetchCloner.Clone(query, entity));
            }
        }