public void DoesRewrite() {
     Expression<Func<Course, bool>> exp = c => c.Type == new CourseType { CourseTypeId = 1 };
     Expression<Func<Course, bool>> expectedResult = c => c.Type.Equals(new CourseType { CourseTypeId = 1 });
     var rewriter = new WhereClauseOpEqualityRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(expectedResult.ToString(), rewrittenClause.ToString());
 }
        public void DoesNotRewriteLastNullEntityComparisons()
        {
            Expression <Func <Course, bool> > exp = c => c.Type.Category == null;
            var rewriter        = new WhereClauseOpEqualityRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(exp.ToString(), rewrittenClause.ToString());
        }
        public void DoesNotRewriteStringComparisons()
        {
            Expression <Func <Course, bool> > exp = c => c.Name == "Foo";
            var rewriter        = new WhereClauseOpEqualityRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(exp.ToString(), rewrittenClause.ToString());
        }
        public void DoesRewrite()
        {
            Expression <Func <Course, bool> > exp = c => c.Type == new CourseType {
                CourseTypeId = 1
            };
            Expression <Func <Course, bool> > expectedResult = c => c.Type.Equals(new CourseType {
                CourseTypeId = 1
            });
            var rewriter        = new WhereClauseOpEqualityRewriter();
            var rewrittenClause = rewriter.Rewrite(exp);

            Assert.Equal(expectedResult.ToString(), rewrittenClause.ToString());
        }
Ejemplo n.º 5
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));
            }
        }
 public void DoesNotRewriteLastNullEntityComparisons() {
     Expression<Func<Course, bool>> exp = c => c.Type.Category == null;
     var rewriter = new WhereClauseOpEqualityRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(exp.ToString(), rewrittenClause.ToString());
 }
 public void DoesNotRewriteStringComparisons() {
     Expression<Func<Course, bool>> exp = c => c.Name == "Foo";
     var rewriter = new WhereClauseOpEqualityRewriter();
     var rewrittenClause = rewriter.Rewrite(exp);
     Assert.Equal(exp.ToString(), rewrittenClause.ToString());
 }