Example #1
0
 public RetrieveDocumentIdsVisitor(QueryMetadata metadata, ByteStringContext allocator) : base(metadata.Query.QueryText)
 {
     _query     = metadata.Query;
     _metadata  = metadata;
     _allocator = allocator;
 }
Example #2
0
        public Query Parse(QueryType queryType = QueryType.Select)
        {
            var q = new Query
            {
                QueryText = Scanner.Input
            };

            while (Scanner.TryScan("DECLARE"))
            {
                var(name, func) = DeclaredFunction();

                if (q.TryAddFunction(name, func) == false)
                {
                    ThrowParseException(name + " function was declared multiple times");
                }
            }

            q.From = FromClause();

            if (Scanner.TryScan("GROUP BY"))
            {
                q.GroupBy = GroupBy();
            }

            if (Scanner.TryScan("WHERE") && Expression(out q.Where) == false)
            {
                ThrowParseException("Unable to parse WHERE clause");
            }

            if (Scanner.TryScan("ORDER BY"))
            {
                q.OrderBy = OrderBy();
            }

            if (Scanner.TryScan("LOAD"))
            {
                q.Load = SelectClauseExpressions("LOAD", false);
            }

            switch (queryType)
            {
            case QueryType.Select:
                if (Scanner.TryScan("SELECT"))
                {
                    q.Select = SelectClause("SELECT", q);
                }
                if (Scanner.TryScan("INCLUDE"))
                {
                    q.Include = IncludeClause();
                }
                break;

            case QueryType.Update:
                if (Scanner.TryScan("UPDATE") == false)
                {
                    ThrowParseException("Update operations must end with UPDATE clause");
                }

                var functionStart = Scanner.Position;
                if (Scanner.FunctionBody() == false)
                {
                    ThrowParseException("Update clause must have a single function body");
                }

                q.UpdateBody = Scanner.Input.Substring(functionStart, Scanner.Position - functionStart);
                try
                {
                    // validate the js code
                    ValidateScript("function test()" + q.UpdateBody);
                }
                catch (Exception e)
                {
                    throw new InvalidQueryException("Update clause contains invalid script", Scanner.Input, null, e);
                }
                break;

            default:
                ThrowUnknownQueryType(queryType);
                break;
            }

            if (Scanner.AtEndOfInput() == false)
            {
                ThrowParseException("Expected end of query");
            }

            return(q);
        }