public TSQLOrderByClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLOrderByClause orderBy = new TSQLOrderByClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.ORDER))
            {
                throw new InvalidOperationException("ORDER expected.");
            }

            orderBy.Tokens.Add(tokenizer.Current);

            TSQLTokenParserHelper.ReadUntilStop(
                tokenizer,
                orderBy,
                new List <TSQLFutureKeywords>()
            {
                TSQLFutureKeywords.OFFSET
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.FOR,
                TSQLKeywords.OPTION
            },
                lookForStatementStarts: true);

            // have to handle OFFSET parsing specially because it can contain FETCH, which would otherwise
            // signal the start of a new statement instead of still being contained within OFFSET
            if (tokenizer.Current.IsFutureKeyword(TSQLFutureKeywords.OFFSET))
            {
                TSQLOffsetClause offsetClause = new TSQLOffsetClauseParser().Parse(tokenizer);
                orderBy.Tokens.AddRange(offsetClause.Tokens);
            }

            return(orderBy);
        }
        public TSQLOrderByClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLOrderByClause orderBy = new TSQLOrderByClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.ORDER))
            {
                throw new InvalidOperationException("ORDER expected.");
            }

            orderBy.Tokens.Add(tokenizer.Current);

            // subqueries

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                orderBy,
                new List <TSQLFutureKeywords>()
            {
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.FOR,
                TSQLKeywords.OPTION
            },
                lookForStatementStarts: true);

            return(orderBy);
        }
Ejemplo n.º 3
0
        public TSQLOrderByClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLOrderByClause orderBy = new TSQLOrderByClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.ORDER))
            {
                throw new InvalidOperationException("ORDER expected.");
            }

            orderBy.Tokens.Add(tokenizer.Current);

            // subqueries
            int nestedLevel = 0;

            while (
                tokenizer.MoveNext() &&
                !tokenizer.Current.IsCharacter(TSQLCharacters.Semicolon) &&
                !(
                    nestedLevel == 0 &&
                    tokenizer.Current.IsCharacter(TSQLCharacters.CloseParentheses)
                    ) &&
                (
                    nestedLevel > 0 ||
                    tokenizer.Current.Type != TSQLTokenType.Keyword ||
                    (
                        tokenizer.Current.Type == TSQLTokenType.Keyword &&
                        !tokenizer.Current.AsKeyword.Keyword.In
                        (
                            TSQLKeywords.FOR,
                            TSQLKeywords.OPTION
                        ) &&
                        !tokenizer.Current.AsKeyword.Keyword.IsStatementStart()
                    )
                ))
            {
                TSQLSubqueryHelper.RecurseParens(
                    tokenizer,
                    orderBy,
                    ref nestedLevel);
            }

            return(orderBy);
        }
Ejemplo n.º 4
0
        public TSQLOrderByClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLOrderByClause orderBy = new TSQLOrderByClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.ORDER))
            {
                throw new InvalidOperationException("ORDER expected.");
            }

            orderBy.Tokens.Add(tokenizer.Current);

            // subqueries
            int nestedLevel = 0;

            while (
                tokenizer.MoveNext() &&
                !tokenizer.Current.IsCharacter(TSQLCharacters.Semicolon) &&
                !(
                    nestedLevel == 0 &&
                    tokenizer.Current.IsCharacter(TSQLCharacters.CloseParentheses)
                    ) &&
                (
                    nestedLevel > 0 ||
                    tokenizer.Current.Type != TSQLTokenType.Keyword ||
                    (
                        tokenizer.Current.Type == TSQLTokenType.Keyword &&
                        tokenizer.Current.AsKeyword.Keyword.In
                        (
                            TSQLKeywords.BY,
                            TSQLKeywords.NULL,
                            TSQLKeywords.CASE,
                            TSQLKeywords.WHEN,
                            TSQLKeywords.THEN,
                            TSQLKeywords.ELSE,
                            TSQLKeywords.AND,
                            TSQLKeywords.OR,
                            TSQLKeywords.BETWEEN,
                            TSQLKeywords.EXISTS,
                            TSQLKeywords.END,
                            TSQLKeywords.IN,
                            TSQLKeywords.IS,
                            TSQLKeywords.NOT,
                            TSQLKeywords.OVER,
                            TSQLKeywords.LIKE,
                            TSQLKeywords.ASC,
                            TSQLKeywords.DESC,
                            TSQLKeywords.FETCH,
                            TSQLKeywords.COLLATE
                        )
                    )
                ))
            {
                orderBy.Tokens.Add(tokenizer.Current);

                if (tokenizer.Current.Type == TSQLTokenType.Character)
                {
                    TSQLCharacters character = tokenizer.Current.AsCharacter.Character;

                    if (character == TSQLCharacters.OpenParentheses)
                    {
                        // should we recurse for subqueries?
                        nestedLevel++;

                        if (tokenizer.MoveNext())
                        {
                            if (tokenizer.Current.IsKeyword(TSQLKeywords.SELECT))
                            {
                                TSQLSelectStatement selectStatement = new TSQLSelectStatementParser().Parse(tokenizer);

                                orderBy.Tokens.AddRange(selectStatement.Tokens);

                                if (tokenizer.Current.IsCharacter(TSQLCharacters.CloseParentheses))
                                {
                                    nestedLevel--;
                                    orderBy.Tokens.Add(tokenizer.Current);
                                }
                            }
                            else if (tokenizer.Current.IsCharacter(
                                         TSQLCharacters.CloseParentheses))
                            {
                                nestedLevel--;
                                orderBy.Tokens.Add(tokenizer.Current);
                            }
                            else
                            {
                                orderBy.Tokens.Add(tokenizer.Current);
                            }
                        }
                    }
                    else if (character == TSQLCharacters.CloseParentheses)
                    {
                        nestedLevel--;
                    }
                }
            }

            return(orderBy);
        }
        public TSQLOrderByClause Parse(TSQLTokenizer tokenizer)
        {
            TSQLOrderByClause orderBy = new TSQLOrderByClause();

            TSQLKeyword keyword = tokenizer.Current.AsKeyword;

            if (keyword == null ||
                keyword.Keyword != TSQLKeywords.ORDER)
            {
                throw new ApplicationException("ORDER expected.");
            }

            orderBy.Tokens.Add(keyword);

            // subqueries
            int nestedLevel = 0;

            while (
                tokenizer.Read() &&
                !(
                    tokenizer.Current.Type == TSQLTokenType.Character &&
                    tokenizer.Current.AsCharacter.Character == TSQLCharacters.Semicolon
                ) &&
                !(
                    nestedLevel == 0 &&
                    tokenizer.Current.Type == TSQLTokenType.Character &&
                    tokenizer.Current.AsCharacter.Character == TSQLCharacters.CloseParentheses
                ) &&
                (
                    nestedLevel > 0 ||
                    tokenizer.Current.Type != TSQLTokenType.Keyword ||
                    (
                        tokenizer.Current.Type == TSQLTokenType.Keyword &&
                        tokenizer.Current.AsKeyword.Keyword.In
                        (
                            TSQLKeywords.BY,
                            TSQLKeywords.NULL,
                            TSQLKeywords.CASE,
                            TSQLKeywords.WHEN,
                            TSQLKeywords.THEN,
                            TSQLKeywords.ELSE,
                            TSQLKeywords.AND,
                            TSQLKeywords.OR,
                            TSQLKeywords.BETWEEN,
                            TSQLKeywords.EXISTS,
                            TSQLKeywords.END,
                            TSQLKeywords.IN,
                            TSQLKeywords.IS,
                            TSQLKeywords.NOT,
                            TSQLKeywords.OVER,
                            TSQLKeywords.LIKE,
                            TSQLKeywords.ASC,
                            TSQLKeywords.DESC,
                            TSQLKeywords.FETCH,
                            TSQLKeywords.COLLATE
                        )
                    )
                ))
            {
                orderBy.Tokens.Add(tokenizer.Current);

                if (tokenizer.Current.Type == TSQLTokenType.Character)
                {
                    TSQLCharacters character = tokenizer.Current.AsCharacter.Character;

                    if (character == TSQLCharacters.OpenParentheses)
                    {
                        // should we recurse for subqueries?
                        nestedLevel++;

                        if (tokenizer.Read())
                        {
                            if (
                                tokenizer.Current.Type == TSQLTokenType.Keyword &&
                                tokenizer.Current.AsKeyword.Keyword == TSQLKeywords.SELECT)
                            {
                                TSQLSelectStatement selectStatement = new TSQLSelectStatementParser().Parse(tokenizer);

                                orderBy.Tokens.AddRange(selectStatement.Tokens);

                                if (
                                    tokenizer.Current != null &&
                                    tokenizer.Current.Type == TSQLTokenType.Character &&
                                    tokenizer.Current.AsCharacter.Character == TSQLCharacters.CloseParentheses)
                                {
                                    nestedLevel--;
                                    orderBy.Tokens.Add(tokenizer.Current);
                                }
                            }
                            else
                            {
                                orderBy.Tokens.Add(tokenizer.Current);
                            }
                        }
                    }
                    else if (character == TSQLCharacters.CloseParentheses)
                    {
                        nestedLevel--;
                    }
                }
            }

            return orderBy;
        }