protected override Expression VisitField(FieldExpression field)
        {
            var fields = new FieldGatherer().Gather(field.Expression);
            if (fields.Count == 0)
                _fields.Add(field);
            else
                _fields.AddRange(fields);

            return base.VisitField(field);
        }
Esempio n. 2
0
        protected override Expression VisitField(FieldExpression field)
        {
            var fields = new FieldGatherer().Gather(field.Expression);

            if (fields.Count == 0)
            {
                _fields.Add(field);
            }
            else
            {
                _fields.AddRange(fields);
            }

            return(base.VisitField(field));
        }
        protected override Expression VisitSelect(SelectExpression select)
        {
            select = PreProcessSelect(select);

            if (select.From != null)
            {
                VisitSource(select.From);
            }
            if (select.Where != null)
            {
                try
                {
                    //try this first, and if it fails, resort to javascript generation, which is slower on the server side.
                    _queryObject.SetQueryDocument(new DocumentFormatter().FormatDocument(select.Where));
                }
                catch
                {
                    _queryObject.SetWhereClause(new JavascriptFormatter().FormatJavascript(select.Where));
                }
            }

            if (_queryAttributes.IsMapReduce)
            {
                _queryObject.IsMapReduce       = true;
                _queryObject.MapFunction       = new MapReduceMapFunctionBuilder().Build(select.Fields, select.GroupBy);
                _queryObject.ReduceFunction    = new MapReduceReduceFunctionBuilder().Build(select.Fields);
                _queryObject.FinalizerFunction = new MapReduceFinalizerFunctionBuilder().Build(select.Fields);
            }
            else if (!_queryAttributes.IsCount && !select.Fields.HasSelectAllField())
            {
                var fieldGatherer = new FieldGatherer();
                foreach (var field in select.Fields)
                {
                    var expandedFields = fieldGatherer.Gather(field.Expression);
                    foreach (var expandedField in expandedFields)
                    {
                        _queryObject.Fields[expandedField.Name] = 1;
                    }
                }
            }

            if (select.OrderBy != null)
            {
                foreach (var order in select.OrderBy)
                {
                    var field = Visit(order.Expression) as FieldExpression;
                    if (field == null)
                    {
                        throw new InvalidQueryException("Complex order by clauses are not supported.");
                    }
                    _queryObject.AddSort(field.Name, order.OrderType == OrderType.Ascending ? 1 : -1);
                }
            }

            if (select.Take != null)
            {
                _queryObject.NumberToLimit = EvaluateConstant <int>(select.Take);
            }

            if (select.Skip != null)
            {
                _queryObject.NumberToSkip = EvaluateConstant <int>(select.Skip);
            }

            return(select);
        }