public void Contains() {
     Expression<Func<Course, bool>> exp = c => new[] { 1 }.Contains(c.Type.CourseTypeId);
     Expression<Func<Course, bool>> expectedResult = c => c.Type != null && new[] { 1 }.Contains(c.Type.CourseTypeId);
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void SimpleEntityEquals() {
     Expression<Func<Course, bool>> exp = c => c.Type == new CourseType { CourseTypeId = 1 };
     Expression<Func<Course, bool>> expectedResult = c => c.Type != null && c.Type == new CourseType { CourseTypeId = 1 };
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void OrElseAlready() {
     Expression<Func<Course, bool>> exp = c => c.Type == new CourseType { CourseTypeId = 1 } || c.Price > 1000;
     Expression<Func<Course, bool>> expectedResult = c => (c.Type != null && c.Type == new CourseType { CourseTypeId = 1 }) || c.Price > 1000;
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void Bool() {
     Expression<Func<Booking, bool>> exp = b => b.Student.IsOraAmbassador;
     Expression<Func<Booking, bool>> expectedResult = b => b.Student != null && b.Student.IsOraAmbassador;
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void DoesNotRewriteLastNullCheck() {
     Expression<Func<Booking, bool>> exp = b => b.Student.Course == null;
     Expression<Func<Booking, bool>> expectedResult = b => b.Student != null && b.Student.Course == null;
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
示例#6
0
        public void DoesNotRewriteSimple()
        {
            Expression <Func <Course, bool> > exp = c => c.CourseId == 1;
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(exp.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#7
0
        public void Bool()
        {
            Expression <Func <Booking, bool> > exp            = b => b.Student.IsOraAmbassador;
            Expression <Func <Booking, bool> > expectedResult = b => b.Student != null && b.Student.IsOraAmbassador;
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#8
0
        public void Contains()
        {
            Expression <Func <Course, bool> > exp = c => new[] { 1 }.Contains(c.Type.CourseTypeId);
            Expression <Func <Course, bool> > expectedResult = c => c.Type != null && new[] { 1 }.Contains(c.Type.CourseTypeId);
            var rewriter = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#9
0
        public void NullableEqualsNullOnParentWithOrWorks()
        {
            Expression <Func <Comment, bool> > exp            = c => c.Post.DeletedDate == null || c.User.IsEnabled;
            Expression <Func <Comment, bool> > expectedResult = c => (c.Post != null && c.Post.DeletedDate == null) || (c.User != null && c.User.IsEnabled);
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#10
0
        public void NullableEqualsNotNullWorks()
        {
            Expression <Func <Comment, bool> > exp            = c => c.DeletedDate != null;
            Expression <Func <Comment, bool> > expectedResult = c => c.DeletedDate != null;
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#11
0
        public void HasValueOnParentOk()
        {
            Expression <Func <Comment, bool> > exp            = c => c.Post.DeletedDate.HasValue;
            Expression <Func <Comment, bool> > expectedResult = c => c.Post != null && c.Post.DeletedDate.HasValue;
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#12
0
        public void DoesNotRewriteNullCheck()
        {
            Expression <Func <Booking, bool> > exp            = b => b.Student == null;
            Expression <Func <Booking, bool> > expectedResult = b => b.Student == null;
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#13
0
        public void HasValueOnParentWithOr()
        {
            var userIds = new List <int>();
            Expression <Func <Comment, bool> > exp            = c => c.Post.DeletedDate.HasValue || userIds.Contains(c.User.UserId);
            Expression <Func <Comment, bool> > expectedResult = c => (c.Post != null && c.Post.DeletedDate.HasValue) || (c.User != null && userIds.Contains(c.User.UserId));
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#14
0
        public void OrElseScopesNullCheckCorrectly()
        {
            var author  = new User();
            var blogIds = new List <int>();
            Expression <Func <Post, bool> > exp            = p => p.Author == author || blogIds.Contains(p.Blog.BlogId);
            Expression <Func <Post, bool> > expectedResult = p => (p.Author != null && p.Author == author) || (p.Blog != null && blogIds.Contains(p.Blog.BlogId));
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#15
0
        public void SimpleEntityEquals()
        {
            Expression <Func <Course, bool> > exp = c => c.Type == new CourseType {
                CourseTypeId = 1
            };
            Expression <Func <Course, bool> > expectedResult = c => c.Type != null && c.Type == new CourseType {
                CourseTypeId = 1
            };
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
示例#16
0
        public void OrElseAlready()
        {
            Expression <Func <Course, bool> > exp = c => c.Type == new CourseType {
                CourseTypeId = 1
            } || c.Price > 1000;
            Expression <Func <Course, bool> > expectedResult = c => (c.Type != null && c.Type == new CourseType {
                CourseTypeId = 1
            }) || c.Price > 1000;
            var rewriter        = new WhereClauseNullCheckRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
        }
 public void OrElseScopesNullCheckCorrectly() {
     var author = new User();
     var blogIds = new List<int>();
     Expression<Func<Post, bool>> exp = p => p.Author == author || blogIds.Contains(p.Blog.BlogId);
     Expression<Func<Post, bool>> expectedResult = p => (p.Author != null && p.Author == author) || (p.Blog != null && blogIds.Contains(p.Blog.BlogId));
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void NullableEqualsNullOnParentWithOrWorks() {
     Expression<Func<Comment, bool>> exp = c => c.Post.DeletedDate == null || c.User.IsEnabled;
     Expression<Func<Comment, bool>> expectedResult = c => (c.Post != null && c.Post.DeletedDate == null) || (c.User != null && c.User.IsEnabled);
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void NullableEqualsNotNullWorks() {
     Expression<Func<Comment, bool>> exp = c => c.DeletedDate != null;
     Expression<Func<Comment, bool>> expectedResult = c => c.DeletedDate != null;
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void HasValueOnParentWithOr() {
     var userIds = new List<int>();
     Expression<Func<Comment, bool>> exp = c => c.Post.DeletedDate.HasValue || userIds.Contains(c.User.UserId);
     Expression<Func<Comment, bool>> expectedResult = c => (c.Post != null && c.Post.DeletedDate.HasValue) || (c.User != null && userIds.Contains(c.User.UserId));
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void DoesNotRewriteSimple() {
     Expression<Func<Course, bool>> exp = c => c.CourseId == 1;
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(exp.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void HasValueOnParentOk() {
     Expression<Func<Comment, bool>> exp = c => c.Post.DeletedDate.HasValue;
     Expression<Func<Comment, bool>> expectedResult = c => c.Post != null && c.Post.DeletedDate.HasValue;
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void DoesNotRewriteNullCheck2() {
     Expression<Func<Booking, bool>> exp = b => null == b.Student;
     Expression<Func<Booking, bool>> expectedResult = b => null == b.Student;
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
 public void DoesNotRewriteNullCheck3() {
     Expression<Func<Student, bool>> exp = s => s.Name == null;
     Expression<Func<Student, bool>> expectedResult = s => s.Name == null;
     var rewriter = new WhereClauseNullCheckRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToDebugString(), rewrittenClause.ToDebugString());
 }
示例#25
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));
            }
        }