Пример #1
0
 public void HavingClause_SanityCheck()
 {
     using (StringReader reader = new StringReader(
                @"bogus"))
         using (ITSQLTokenizer tokenizer = new TSQLTokenizer(reader))
         {
             Assert.IsTrue(tokenizer.MoveNext());
             Exception ex = Assert.Throws <InvalidOperationException>(
                 delegate
             {
                 TSQLHavingClause having = new TSQLHavingClauseParser().Parse(tokenizer);
             });
         }
 }
        public TSQLSelectStatement Parse(ITSQLTokenizer tokenizer)
        {
            TSQLSelectStatement select = new TSQLSelectStatement();

            TSQLSelectClause selectClause = new TSQLSelectClauseParser().Parse(tokenizer);

            select.Select = selectClause;

            select.Tokens.AddRange(selectClause.Tokens);

            if (tokenizer.Current.IsKeyword(TSQLKeywords.INTO))
            {
                TSQLIntoClause intoClause = new TSQLIntoClauseParser().Parse(tokenizer);

                select.Into = intoClause;

                select.Tokens.AddRange(intoClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.FROM))
            {
                TSQLFromClause fromClause = new TSQLFromClauseParser().Parse(tokenizer);

                select.From = fromClause;

                select.Tokens.AddRange(fromClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.WHERE))
            {
                TSQLWhereClause whereClause = new TSQLWhereClauseParser().Parse(tokenizer);

                select.Where = whereClause;

                select.Tokens.AddRange(whereClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.GROUP))
            {
                TSQLGroupByClause groupByClause = new TSQLGroupByClauseParser().Parse(tokenizer);

                select.GroupBy = groupByClause;

                select.Tokens.AddRange(groupByClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.HAVING))
            {
                TSQLHavingClause havingClause = new TSQLHavingClauseParser().Parse(tokenizer);

                select.Having = havingClause;

                select.Tokens.AddRange(havingClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.ORDER))
            {
                TSQLOrderByClause orderByClause = new TSQLOrderByClauseParser().Parse(tokenizer);

                select.OrderBy = orderByClause;

                select.Tokens.AddRange(orderByClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.OPTION))
            {
                TSQLOptionClause optionClause = new TSQLOptionClauseParser().Parse(tokenizer);

                select.Option = optionClause;

                select.Tokens.AddRange(optionClause.Tokens);
            }

            if (
                tokenizer.Current != null &&
                tokenizer.Current.Type == TSQLTokenType.Keyword)
            {
                tokenizer.Putback();
            }

            return(select);
        }
        public TSQLSelectStatement Parse(TSQLTokenizer tokenizer)
        {
            TSQLSelectStatement select = new TSQLSelectStatement();

            // should whitespace be excluded from statement parsing logic?

            // should I differentiate keywords that start commands?

            // correlated subqueries
            // scalar function calls

            // SELECT clause

            TSQLSelectClause selectClause = new TSQLSelectClauseParser().Parse(tokenizer);

            select.Select = selectClause;

            select.Tokens.AddRange(selectClause.Tokens);

            if (tokenizer.Current.IsKeyword(TSQLKeywords.INTO))
            {
                TSQLIntoClause intoClause = new TSQLIntoClauseParser().Parse(tokenizer);

                select.Into = intoClause;

                select.Tokens.AddRange(intoClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.FROM))
            {
                TSQLFromClause fromClause = new TSQLFromClauseParser().Parse(tokenizer);

                select.From = fromClause;

                select.Tokens.AddRange(fromClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.WHERE))
            {
                TSQLWhereClause whereClause = new TSQLWhereClauseParser().Parse(tokenizer);

                select.Where = whereClause;

                select.Tokens.AddRange(whereClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.GROUP))
            {
                TSQLGroupByClause groupByClause = new TSQLGroupByClauseParser().Parse(tokenizer);

                select.GroupBy = groupByClause;

                select.Tokens.AddRange(groupByClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.HAVING))
            {
                TSQLHavingClause havingClause = new TSQLHavingClauseParser().Parse(tokenizer);

                select.Having = havingClause;

                select.Tokens.AddRange(havingClause.Tokens);
            }

            if (tokenizer.Current.IsKeyword(TSQLKeywords.ORDER))
            {
                TSQLOrderByClause orderByClause = new TSQLOrderByClauseParser().Parse(tokenizer);

                select.OrderBy = orderByClause;

                select.Tokens.AddRange(orderByClause.Tokens);
            }

            if (tokenizer.Current.IsCharacter(TSQLCharacters.Semicolon))
            {
                select.Tokens.Add(tokenizer.Current);
            }

            return(select);
        }
Пример #4
0
        public TSQLSelectStatement Parse()
        {
            TSQLSelectClause selectClause = new TSQLSelectClauseParser().Parse(Tokenizer);

            Statement.Select = selectClause;

            Statement.Tokens.AddRange(selectClause.Tokens);

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.INTO))
            {
                TSQLIntoClause intoClause = new TSQLIntoClauseParser().Parse(Tokenizer);

                Statement.Into = intoClause;

                Statement.Tokens.AddRange(intoClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.FROM))
            {
                TSQLFromClause fromClause = new TSQLFromClauseParser().Parse(Tokenizer);

                Statement.From = fromClause;

                Statement.Tokens.AddRange(fromClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.WHERE))
            {
                TSQLWhereClause whereClause = new TSQLWhereClauseParser().Parse(Tokenizer);

                Statement.Where = whereClause;

                Statement.Tokens.AddRange(whereClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.GROUP))
            {
                TSQLGroupByClause groupByClause = new TSQLGroupByClauseParser().Parse(Tokenizer);

                Statement.GroupBy = groupByClause;

                Statement.Tokens.AddRange(groupByClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.HAVING))
            {
                TSQLHavingClause havingClause = new TSQLHavingClauseParser().Parse(Tokenizer);

                Statement.Having = havingClause;

                Statement.Tokens.AddRange(havingClause.Tokens);
            }

            // ORDER not allowed

            // FOR not allowed

            // OPTION not allowed

            if (
                Tokenizer.Current?.AsKeyword != null &&
                Tokenizer.Current.AsKeyword.Keyword.IsStatementStart())
            {
                Tokenizer.Putback();
            }

            return Statement;
        }
Пример #5
0
        public TSQLSelectStatement Parse()
        {
            TSQLSelectClause selectClause = new TSQLSelectClauseParser().Parse(Tokenizer);

            Statement.Select = selectClause;

            Statement.Tokens.AddRange(selectClause.Tokens);

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.INTO))
            {
                TSQLIntoClause intoClause = new TSQLIntoClauseParser().Parse(Tokenizer);

                Statement.Into = intoClause;

                Statement.Tokens.AddRange(intoClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.FROM))
            {
                TSQLFromClause fromClause = new TSQLFromClauseParser().Parse(Tokenizer);

                Statement.From = fromClause;

                Statement.Tokens.AddRange(fromClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.WHERE))
            {
                TSQLWhereClause whereClause = new TSQLWhereClauseParser().Parse(Tokenizer);

                Statement.Where = whereClause;

                Statement.Tokens.AddRange(whereClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.GROUP))
            {
                TSQLGroupByClause groupByClause = new TSQLGroupByClauseParser().Parse(Tokenizer);

                Statement.GroupBy = groupByClause;

                Statement.Tokens.AddRange(groupByClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.HAVING))
            {
                TSQLHavingClause havingClause = new TSQLHavingClauseParser().Parse(Tokenizer);

                Statement.Having = havingClause;

                Statement.Tokens.AddRange(havingClause.Tokens);
            }

            // ORDER not allowed

            // FOR not allowed

            // OPTION not allowed

            TSQLTokenParserHelper.ReadCommentsAndWhitespace(
                Tokenizer,
                Statement);

            return(Statement);
        }
Пример #6
0
        public TSQLSelectStatement Parse()
        {
            // (SELECT 1)
            int level = 0;

            while (Tokenizer.Current.IsCharacter(TSQLCharacters.OpenParentheses))
            {
                Statement.Tokens.Add(Tokenizer.Current);

                level++;

                Tokenizer.MoveNext();

                TSQLTokenParserHelper.ReadCommentsAndWhitespace(
                    Tokenizer,
                    Statement);
            }

            TSQLSelectClause selectClause = new TSQLSelectClauseParser().Parse(Tokenizer);

            Statement.Select = selectClause;

            Statement.Tokens.AddRange(selectClause.Tokens);

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.INTO))
            {
                TSQLIntoClause intoClause = new TSQLIntoClauseParser().Parse(Tokenizer);

                Statement.Into = intoClause;

                Statement.Tokens.AddRange(intoClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.FROM))
            {
                TSQLFromClause fromClause = new TSQLFromClauseParser().Parse(Tokenizer);

                Statement.From = fromClause;

                Statement.Tokens.AddRange(fromClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.WHERE))
            {
                TSQLWhereClause whereClause = new TSQLWhereClauseParser().Parse(Tokenizer);

                Statement.Where = whereClause;

                Statement.Tokens.AddRange(whereClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.GROUP))
            {
                TSQLGroupByClause groupByClause = new TSQLGroupByClauseParser().Parse(Tokenizer);

                Statement.GroupBy = groupByClause;

                Statement.Tokens.AddRange(groupByClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.HAVING))
            {
                TSQLHavingClause havingClause = new TSQLHavingClauseParser().Parse(Tokenizer);

                Statement.Having = havingClause;

                Statement.Tokens.AddRange(havingClause.Tokens);
            }

            while (level > 0 && Tokenizer.Current.IsCharacter(TSQLCharacters.CloseParentheses))
            {
                Statement.Tokens.Add(Tokenizer.Current);

                level--;

                Tokenizer.MoveNext();
            }

            while (Tokenizer.Current?.AsKeyword != null &&
                   Tokenizer.Current.AsKeyword.Keyword.In(
                       TSQLKeywords.UNION,
                       TSQLKeywords.EXCEPT,
                       TSQLKeywords.INTERSECT))
            {
                TSQLSetOperatorClause set = new TSQLSetOperatorClauseParser().Parse(Tokenizer);

                Statement.SetOperators.Add(set);

                Statement.Tokens.AddRange(set.Tokens);
            }

            while (level > 0 && Tokenizer.Current.IsCharacter(TSQLCharacters.CloseParentheses))
            {
                Statement.Tokens.Add(Tokenizer.Current);

                level--;

                Tokenizer.MoveNext();
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.ORDER))
            {
                TSQLOrderByClause orderByClause = new TSQLOrderByClauseParser().Parse(Tokenizer);

                Statement.OrderBy = orderByClause;

                Statement.Tokens.AddRange(orderByClause.Tokens);
            }

            // order for OPTION and FOR doesn't seem to matter
            while (
                Tokenizer.Current.IsKeyword(TSQLKeywords.FOR) ||
                Tokenizer.Current.IsKeyword(TSQLKeywords.OPTION))
            {
                if (Tokenizer.Current.IsKeyword(TSQLKeywords.FOR))
                {
                    TSQLForClause forClause = new TSQLForClauseParser().Parse(Tokenizer);

                    Statement.For = forClause;

                    Statement.Tokens.AddRange(forClause.Tokens);
                }

                if (Tokenizer.Current.IsKeyword(TSQLKeywords.OPTION))
                {
                    TSQLOptionClause optionClause = new TSQLOptionClauseParser().Parse(Tokenizer);

                    Statement.Option = optionClause;

                    Statement.Tokens.AddRange(optionClause.Tokens);
                }
            }

            return(Statement);
        }
Пример #7
0
        public TSQLSelectStatement Parse()
        {
            TSQLSelectClause selectClause = new TSQLSelectClauseParser().Parse(Tokenizer);

            Statement.Select = selectClause;

            Statement.Tokens.AddRange(selectClause.Tokens);

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.INTO))
            {
                TSQLIntoClause intoClause = new TSQLIntoClauseParser().Parse(Tokenizer);

                Statement.Into = intoClause;

                Statement.Tokens.AddRange(intoClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.FROM))
            {
                TSQLFromClause fromClause = new TSQLFromClauseParser().Parse(Tokenizer);

                Statement.From = fromClause;

                Statement.Tokens.AddRange(fromClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.WHERE))
            {
                TSQLWhereClause whereClause = new TSQLWhereClauseParser().Parse(Tokenizer);

                Statement.Where = whereClause;

                Statement.Tokens.AddRange(whereClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.GROUP))
            {
                TSQLGroupByClause groupByClause = new TSQLGroupByClauseParser().Parse(Tokenizer);

                Statement.GroupBy = groupByClause;

                Statement.Tokens.AddRange(groupByClause.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.HAVING))
            {
                TSQLHavingClause havingClause = new TSQLHavingClauseParser().Parse(Tokenizer);

                Statement.Having = havingClause;

                Statement.Tokens.AddRange(havingClause.Tokens);
            }

            if (Tokenizer.Current?.AsKeyword != null &&
                Tokenizer.Current.AsKeyword.Keyword.In(
                    TSQLKeywords.UNION,
                    TSQLKeywords.EXCEPT,
                    TSQLKeywords.INTERSECT))
            {
                TSQLSetOperatorClause set = new TSQLSetOperatorClauseParser().Parse(Tokenizer);

                Statement.SetOperator = set;

                Statement.Tokens.AddRange(set.Tokens);
            }

            if (Tokenizer.Current.IsKeyword(TSQLKeywords.ORDER))
            {
                TSQLOrderByClause orderByClause = new TSQLOrderByClauseParser().Parse(Tokenizer);

                Statement.OrderBy = orderByClause;

                Statement.Tokens.AddRange(orderByClause.Tokens);
            }

            // order for OPTION and FOR doesn't seem to matter
            while (
                Tokenizer.Current.IsKeyword(TSQLKeywords.FOR) ||
                Tokenizer.Current.IsKeyword(TSQLKeywords.OPTION))
            {
                if (Tokenizer.Current.IsKeyword(TSQLKeywords.FOR))
                {
                    TSQLForClause forClause = new TSQLForClauseParser().Parse(Tokenizer);

                    Statement.For = forClause;

                    Statement.Tokens.AddRange(forClause.Tokens);
                }

                if (Tokenizer.Current.IsKeyword(TSQLKeywords.OPTION))
                {
                    TSQLOptionClause optionClause = new TSQLOptionClauseParser().Parse(Tokenizer);

                    Statement.Option = optionClause;

                    Statement.Tokens.AddRange(optionClause.Tokens);
                }
            }

            if (
                Tokenizer.Current?.AsKeyword != null &&
                Tokenizer.Current.AsKeyword.Keyword.IsStatementStart())
            {
                Tokenizer.Putback();
            }

            return(Statement);
        }