Exemple #1
0
        private static LucQuery BuildLucQuery(System.Linq.Expressions.Expression expression, Type sourceCollectionItemType, string contextPath, ChildrenDefinition childrenDef)
        {
            Query q0 = null;

            CQVisitor v = null;

            // #1 compiling linq expression
            if (expression != null)
            {
                var v1    = new SetExecVisitor();
                var expr1 = v1.Visit(expression);
                var expr2 = expr1;
                if (v1.HasParameter)
                {
                    var v2 = new ExecutorVisitor(v1.GetExpressions());
                    expr2 = v2.Visit(expr1);
                }
                v = new CQVisitor();
                v.Visit(expr2);
                q0 = v.GetQuery(sourceCollectionItemType, childrenDef);
            }

            // #2 combining with additional query clause
            LucQuery lq = null;

            if (!string.IsNullOrEmpty(childrenDef.ContentQuery))
            {
                lq = LucQuery.Parse(childrenDef.ContentQuery);
                if (q0 == null)
                {
                    q0 = lq.Query;
                }
                else
                {
                    q0 = CombineQueries(q0, lq.Query);
                }
            }

            // #3 combining with context path
            if (q0 == null)
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = GetPathQuery(contextPath, childrenDef.PathUsage == PathUsageMode.InTreeAnd || childrenDef.PathUsage == PathUsageMode.InTreeOr);
                }
            }
            else
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = CombinePathQuery(q0, contextPath, childrenDef.PathUsage);
                }
            }

            // #4 empty query is invalid in this place
            if (q0 == null)
            {
                throw new NotSupportedException("Cannot execute empty query. Expression: " + expression);
            }

            var q1 = OptimizeBooleans(q0);

            // #5 configuring query by linq expression (the smallest priority)
            var query = LucQuery.Create(q1);

            if (v != null)
            {
                query.Skip          = v.Skip;
                query.Top           = v.Top;
                query.CountOnly     = v.CountOnly;
                query.SortFields    = v.SortFields.ToArray();
                query.ThrowIfEmpty  = v.ThrowIfEmpty;
                query.ExistenceOnly = v.ExistenceOnly;
            }
            // #6 configuring query by children definition
            if (childrenDef != null)
            {
                if (childrenDef.Skip > 0)
                {
                    query.Skip = childrenDef.Skip;
                }
                if (childrenDef.Top > 0)
                {
                    query.Top = childrenDef.Top;
                }
                if (childrenDef.Sort != null)
                {
                    query.SetSort(childrenDef.Sort);
                }
                if (childrenDef.CountAllPages != null)
                {
                    query.CountAllPages = childrenDef.CountAllPages.Value;
                }
                if (childrenDef.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = childrenDef.EnableAutofilters;
                }
                if (childrenDef.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = childrenDef.EnableLifespanFilter;
                }
                if (childrenDef.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = childrenDef.QueryExecutionMode;
                }
            }

            // #7 configuring query by additional query clause (the biggest priority)
            if (lq != null)
            {
                if (lq.Skip > 0)
                {
                    query.Skip = lq.Skip;
                }
                if (lq.Top > 0)
                {
                    query.Top = lq.Top;
                }
                if (lq.SortFields != null && lq.SortFields.Length > 0)
                {
                    query.SortFields = lq.SortFields;
                }
                if (lq.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = lq.EnableAutofilters;
                }
                if (lq.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = lq.EnableLifespanFilter;
                }
                if (lq.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = lq.QueryExecutionMode;
                }
            }

            return(query);
        }
Exemple #2
0
        private static SnQuery BuildSnQuery(Expression expression, Type sourceCollectionItemType, string contextPath, ChildrenDefinition childrenDef, out string elementSelection)
        {
            SnQueryPredicate q0 = null;

            elementSelection = null;

            SnLinqVisitor v = null;

            // #1 compiling linq expression
            if (expression != null)
            {
                var v1    = new SetExecVisitor();
                var expr1 = v1.Visit(expression);
                var expr2 = expr1;
                if (v1.HasParameter)
                {
                    var v2 = new ExecutorVisitor(v1.GetExpressions());
                    expr2 = v2.Visit(expr1);
                }
                v = new SnLinqVisitor();
                v.Visit(expr2);
                q0 = v.GetPredicate(sourceCollectionItemType, childrenDef);
                elementSelection = v.ElementSelection;
            }

            // #2 combining with additional query clause
            SnQuery lq = null;

            if (!string.IsNullOrEmpty(childrenDef?.ContentQuery))
            {
                var queryText = TemplateManager.Replace(typeof(ContentQueryTemplateReplacer), childrenDef.ContentQuery);

                lq = SnQuery.Parse(queryText, new SnQueryContext(QuerySettings.Default, User.Current.Id));
                q0 = q0 == null
                    ? lq.QueryTree
                    : CombineQueries(q0, lq.QueryTree);
            }

            // #3 combining with context path
            if (q0 == null)
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = GetPathPredicate(contextPath, childrenDef.PathUsage == PathUsageMode.InTreeAnd || childrenDef.PathUsage == PathUsageMode.InTreeOr);
                }
            }
            else
            {
                if (childrenDef != null && childrenDef.PathUsage != PathUsageMode.NotUsed && contextPath != null)
                {
                    q0 = CombinePathPredicate(q0, contextPath, childrenDef.PathUsage);
                }
            }

            // #4 empty query substitution
            if (q0 == null)
            {
                q0 = new RangePredicate(IndexFieldName.NodeId, new IndexValue(0), null, true, false);
            }

            var q1 = OptimizeBooleans(q0);

            // #5 configuring query by linq expression (the smallest priority)
            var query = SnQuery.Create(q1);

            if (v != null)
            {
                query.Skip          = v.Skip;
                query.Top           = v.Top;
                query.CountOnly     = v.CountOnly;
                query.Sort          = v.Sort.ToArray();
                query.ThrowIfEmpty  = v.ThrowIfEmpty;
                query.ExistenceOnly = v.ExistenceOnly;
            }
            // #6 configuring query by children definition
            if (childrenDef != null)
            {
                if (childrenDef.Skip > 0)
                {
                    query.Skip = childrenDef.Skip;
                }
                if (childrenDef.Top > 0)
                {
                    query.Top = childrenDef.Top;
                }
                if (childrenDef.Sort != null)
                {
                    query.Sort = childrenDef.Sort.ToArray();
                }
                if (childrenDef.CountAllPages != null)
                {
                    query.CountAllPages = childrenDef.CountAllPages.Value;
                }
                if (childrenDef.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = childrenDef.EnableAutofilters;
                }
                if (childrenDef.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = childrenDef.EnableLifespanFilter;
                }
                if (childrenDef.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = childrenDef.QueryExecutionMode;
                }
            }

            // #7 configuring query by additional query clause (the biggest priority)
            if (lq != null)
            {
                if (lq.Skip > 0)
                {
                    query.Skip = lq.Skip;
                }
                if (lq.Top > 0 && lq.Top != int.MaxValue)
                {
                    query.Top = lq.Top;
                }
                if (lq.Sort != null && lq.Sort.Length > 0)
                {
                    query.Sort = lq.Sort;
                }
                if (lq.EnableAutofilters != FilterStatus.Default)
                {
                    query.EnableAutofilters = lq.EnableAutofilters;
                }
                if (lq.EnableLifespanFilter != FilterStatus.Default)
                {
                    query.EnableLifespanFilter = lq.EnableLifespanFilter;
                }
                if (lq.QueryExecutionMode != QueryExecutionMode.Default)
                {
                    query.QueryExecutionMode = lq.QueryExecutionMode;
                }
                if (lq.AllVersions)
                {
                    query.AllVersions = true;
                }
            }

            return(query);
        }