private static void ParseGroupBy(string apply, QuerySpec spec, TableSpec tabSpec)
        {
            if (string.IsNullOrEmpty(apply))
            {
                return;
            }
            var lexer = new ODataTranslatorLexer(new AntlrInputStream(apply), tabSpec, spec);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            // Pass the tokens to the parser
            var parser = new ODataTranslatorParser(tokens, tabSpec, spec);

            // Run root rule ("apply" in this grammar)
            parser.apply();
            if (parser.Aggregates.Count == 0)
            {
                _log.ErrorFormat("Cannot extract agregates from $apply= {apply} value. ", apply);
                throw new ArgumentException("Cannot parse $apply operator.", "$apply");
            }

            spec.select = parser.GroupBy;
            foreach (var a in parser.Aggregates)
            {
                spec.select += ((spec.select == null) ?"":",") + a.AggregateColumnAlias + "=" + a.AggregateMethod + "(" + a.AggregateColumn + ")";
            }
            spec.groupBy = parser.GroupBy;
        }
 private static void ParseSearch(string filter, QuerySpec spec, TableSpec tabSpec)
 {
     if (!string.IsNullOrWhiteSpace(filter))
     {
         var lexer     = new ODataTranslatorLexer(new AntlrInputStream(filter), tabSpec, spec);
         var predicate = new StringBuilder();
         while (!lexer._hitEOF)
         {
             var token = lexer.NextToken();
             predicate.Append(token.Text);
         }
         spec.predicate = predicate.ToString();
     }
 }
        private static void ParseExpand(StringValues expand, QuerySpec spec, TableSpec tabSpec)
        {
            if (string.IsNullOrEmpty(expand))
            {
                return;
            }

            var lexer = new ODataTranslatorLexer(new AntlrInputStream(expand), tabSpec, spec);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            // Pass the tokens to the parser
            var parser = new ODataTranslatorParser(tokens, tabSpec, spec);

            spec.expand = new System.Collections.Generic.Dictionary <string, QuerySpec>();
            // Run  rule "expandItems" in this grammar

            parser.expandItems();
            spec.expand = parser.Relations;
        }
        private static void ParseOrderBy(TableSpec tabSpec, string orderby, QuerySpec spec)
        {
            if (!string.IsNullOrWhiteSpace(orderby))
            {
                spec.order = new Hashtable();
                foreach (var colDir in orderby.Split(','))
                {
                    string dir = "asc", column = colDir;
                    if (colDir.EndsWith(" desc"))
                    {
                        dir    = "desc";
                        column = colDir.Substring(0, colDir.Length - 5).Trim();
                    }
                    else if (colDir.EndsWith(" asc"))
                    {
                        column = colDir.Substring(0, colDir.Length - 4).Trim();
                    }

                    if (EnableODataExtensions)
                    {
                        var lexer = new ODataTranslatorLexer(new AntlrInputStream(column), tabSpec, spec);
                        CommonTokenStream tokens = new CommonTokenStream(lexer);
                        // Pass the tokens to the parser
                        var parser = new ODataTranslatorParser(tokens, tabSpec, spec);
                        parser.ErrorHandler = FastFail;
                        var orderBy = parser.orderBy();
                        column = orderBy.Expression + " " + orderBy.Direction;
                        if (string.IsNullOrWhiteSpace(column))
                        {
                            _log.ErrorFormat("Cannot extract order by clause from $orderby= {orderby} value. ", orderby);
                            throw new ArgumentException("Cannot parse $orderby parameter.", "$orderby");
                        }
                    }
                    else
                    {
                        tabSpec.HasColumn(column);
                    }
                    spec.order.Add(column, dir);
                }
                spec.IsOrderClauseValidated = true;
            }
        }