Exemplo n.º 1
0
        public IEnumerable <T> Query <T>(ISessionState sessionState, SelectQuery <T> query) where T : class, new()
        {
            this.AssertConfigured();
            var table = this.tables[typeof(T)];
            var whereClauseNullCheckRewriter = new WhereClauseNullCheckRewriter();
            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)
            {
                enumerable = enumerable.Where(whereClauseNullCheckRewriter.Rewrite(whereClause).Compile());
            }

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

            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));
            }
        }
Exemplo n.º 2
0
 public FetchCloner(IConfiguration configuration)
 {
     this.configuration   = configuration;
     this.fetchTreeParser = new FetchTreeParser(configuration);
 }
Exemplo n.º 3
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);
            var fetchTree   = fetchParser.GetFetchTree(query, out _, out _);

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

            baseWriter.AddWhereClause(query.WhereClauses, new StringBuilder(), new AutoNamingDynamicParameters(), 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));
            }
        }