Esempio n. 1
0
        private void Parse2(
            string statementText
            )
        {
//			Debug.WriteLine(statementText);

            PARSERSTATE parserState = PARSERSTATE.NOTHINGYET;

            Token[] namePart    = new Token[4];
            int     currentPart = 0;

            Token alias = Token.Null;

            TokenType lastTokenType = TokenType.Null;

            int parenLevel = 0;

//			bool			distinctFound;

            _columns = null;
            _tables  = null;

            Match match = SqlTokenParser.Match(statementText);
            Token token = TokenFromMatch(match);

            while (true)
            {
//				Debug.WriteLine(String.Format("{0,-15} {1,-17} {2} {3}", parserState, token.Type.ToString(), parenLevel, token.Value));

                switch (parserState)
                {
                case PARSERSTATE.DONE:
                    return;

                case PARSERSTATE.NOTHINGYET:
                    switch (token.Type)
                    {
                    case TokenType.Keyword_SELECT:
                        parserState = PARSERSTATE.SELECT;
                        break;

                    default:
                        Debug.Assert(false, "no state defined!!!!we should never be here!!!");
                        throw ADP.InvalidOperation(Res.GetString(Res.ADP_SQLParserInternalError));
                    }
                    break;

                case PARSERSTATE.SELECT:
                    switch (token.Type)
                    {
                    case TokenType.Identifier:
                    case TokenType.QuotedIdentifier:
                        parserState = PARSERSTATE.COLUMN;
                        currentPart = 0;
                        namePart[0] = token;
                        break;

                    case TokenType.Keyword_FROM:
                        parserState = PARSERSTATE.FROM;
                        break;

                    case TokenType.Other_Star:
                        parserState = PARSERSTATE.COLUMNALIAS;
                        currentPart = 0;
                        namePart[0] = token;
                        break;

                    case TokenType.Keyword_DISTINCT:
//								distinctFound = true;
                        break;

                    case TokenType.Keyword_ALL:
                        break;

                    case TokenType.Other_LeftParen:
                        parserState = PARSERSTATE.EXPRESSION;
                        parenLevel++;
                        break;

                    case TokenType.Other_RightParen:
                        throw ADP.SyntaxErrorMissingParenthesis();

                    default:
                        parserState = PARSERSTATE.EXPRESSION;
                        break;
                    }
                    break;

                case PARSERSTATE.COLUMN:
                    switch (token.Type)
                    {
                    case TokenType.Identifier:
                    case TokenType.QuotedIdentifier:
                        if (TokenType.Other_Period != lastTokenType)
                        {
                            parserState = PARSERSTATE.COLUMNALIAS;
                            alias       = token;
                        }
                        else
                        {
                            namePart[++currentPart] = token;
                        }
                        break;

                    case TokenType.Other_Period:
                        if (currentPart > 3)
                        {
                            throw ADP.SyntaxErrorTooManyNameParts();
                        }

                        break;

                    case TokenType.Other_Star:
                        parserState             = PARSERSTATE.COLUMNALIAS;
                        namePart[++currentPart] = token;
                        break;

                    case TokenType.Keyword_AS:
                        break;

                    case TokenType.Keyword_FROM:
                    case TokenType.Other_Comma:
                        parserState = (token.Type == TokenType.Keyword_FROM) ? PARSERSTATE.FROM : PARSERSTATE.SELECT;
                        AddColumn(currentPart, namePart, alias);
                        currentPart = -1;
                        alias       = Token.Null;
                        break;

                    case TokenType.Other_LeftParen:
                        parserState = PARSERSTATE.EXPRESSION;
                        parenLevel++;
                        currentPart = -1;
                        break;

                    case TokenType.Other_RightParen:
                        throw ADP.SyntaxErrorMissingParenthesis();

                    default:
                        parserState = PARSERSTATE.EXPRESSION;
                        currentPart = -1;
                        break;
                    }
                    break;

                case PARSERSTATE.COLUMNALIAS:
                    switch (token.Type)
                    {
                    case TokenType.Keyword_FROM:
                    case TokenType.Other_Comma:
                        parserState = (token.Type == TokenType.Keyword_FROM) ? PARSERSTATE.FROM : PARSERSTATE.SELECT;
                        AddColumn(currentPart, namePart, alias);
                        currentPart = -1;
                        alias       = Token.Null;
                        break;

                    default:
                        throw ADP.SyntaxErrorExpectedCommaAfterColumn();
                    }
                    break;

                case PARSERSTATE.EXPRESSION:
                    switch (token.Type)
                    {
                    case TokenType.Identifier:
                    case TokenType.QuotedIdentifier:
                        if (0 == parenLevel)
                        {
                            alias = token;
                        }
                        break;

                    case TokenType.Keyword_FROM:
                    case TokenType.Other_Comma:
                        if (0 == parenLevel)
                        {
                            parserState = (token.Type == TokenType.Keyword_FROM) ? PARSERSTATE.FROM : PARSERSTATE.SELECT;
                            AddColumn(currentPart, namePart, alias);
                            currentPart = -1;
                            alias       = Token.Null;
                        }
                        else
                        {
                            if (token.Type == TokenType.Keyword_FROM)
                            {
                                throw ADP.SyntaxErrorUnexpectedFrom();
                            }
                        }
                        break;

                    case TokenType.Other_LeftParen:
                        parenLevel++;
                        break;

                    case TokenType.Other_RightParen:
                        parenLevel--;
                        break;
                    }
                    break;

                case PARSERSTATE.FROM:
                    switch (token.Type)
                    {
                    case TokenType.Identifier:
                    case TokenType.QuotedIdentifier:
                        parserState = PARSERSTATE.TABLE;
                        currentPart = 0;
                        namePart[0] = token;
                        break;

                    default:
                        throw ADP.SyntaxErrorExpectedIdentifier();
                    }
                    break;

                case PARSERSTATE.TABLE:
                    switch (token.Type)
                    {
                    case TokenType.Identifier:
                    case TokenType.QuotedIdentifier:
                        if (TokenType.Other_Period != lastTokenType)
                        {
                            parserState = PARSERSTATE.TABLEALIAS;
                            alias       = token;
                        }
                        else
                        {
                            namePart[++currentPart] = token;
                        }
                        break;

                    case TokenType.Other_Period:
                        if (currentPart > 2)
                        {
                            throw ADP.SyntaxErrorTooManyNameParts();
                        }

                        break;

                    case TokenType.Keyword_AS:
                        break;

                    case TokenType.Keyword_COMPUTE:
                    case TokenType.Keyword_FOR:
                    case TokenType.Keyword_GROUP:
                    case TokenType.Keyword_HAVING:
                    case TokenType.Keyword_INTERSECT:
                    case TokenType.Keyword_MINUS:
                    case TokenType.Keyword_ORDER:
                    case TokenType.Keyword_UNION:
                    case TokenType.Keyword_WHERE:
                    case TokenType.Null:

                    case TokenType.Other_Comma:
                        parserState = (TokenType.Other_Comma == token.Type) ? PARSERSTATE.FROM : PARSERSTATE.DONE;
                        AddTable(currentPart, namePart, alias);
                        currentPart = -1;
                        alias       = Token.Null;
                        break;

                    default:
                        throw ADP.SyntaxErrorExpectedNextPart();
                    }
                    break;

                case PARSERSTATE.TABLEALIAS:
                    switch (token.Type)
                    {
                    case TokenType.Keyword_COMPUTE:
                    case TokenType.Keyword_FOR:
                    case TokenType.Keyword_GROUP:
                    case TokenType.Keyword_HAVING:
                    case TokenType.Keyword_INTERSECT:
                    case TokenType.Keyword_MINUS:
                    case TokenType.Keyword_ORDER:
                    case TokenType.Keyword_UNION:
                    case TokenType.Keyword_WHERE:
                    case TokenType.Null:

                    case TokenType.Other_Comma:
                        parserState = (TokenType.Other_Comma == token.Type) ? PARSERSTATE.FROM : PARSERSTATE.DONE;
                        AddTable(currentPart, namePart, alias);
                        currentPart = -1;
                        alias       = Token.Null;
                        break;

                    default:
                        throw ADP.SyntaxErrorExpectedCommaAfterTable();
                    }
                    break;


                default:
                    Debug.Assert(false, "no state defined!!!!we should never be here!!!");
                    throw ADP.InvalidOperation(Res.GetString(Res.ADP_SQLParserInternalError));
                }

                lastTokenType = token.Type;

                match = match.NextMatch();
                token = TokenFromMatch(match);
            }
        }
        private void Parse2(string statementText)
        {
            bool        flag;
            PARSERSTATE nOTHINGYET = PARSERSTATE.NOTHINGYET;

            Token[]   namePart = new Token[4];
            int       maxPart  = 0;
            Token     @null    = Token.Null;
            TokenType type7    = TokenType.Null;
            int       num2     = 0;

            this._columns = null;
            this._tables  = null;
            Match match  = SqlTokenParser.Match(statementText);
            Token token2 = TokenFromMatch(match);

Label_003B:
            flag = false;
            switch (nOTHINGYET)
            {
            case PARSERSTATE.NOTHINGYET:
                if (token2.Type != TokenType.Keyword_SELECT)
                {
                    throw System.Data.Common.ADP.InvalidOperation(Res.GetString("ADP_SQLParserInternalError"));
                }
                nOTHINGYET = PARSERSTATE.SELECT;
                goto Label_061A;

            case PARSERSTATE.SELECT:
            {
                TokenType type2 = token2.Type;
                if (type2 > TokenType.Other_Star)
                {
                    switch (type2)
                    {
                    case TokenType.Keyword_DISTINCT:
                    case TokenType.Keyword_ALL:
                        goto Label_061A;

                    case TokenType.Keyword_FROM:
                        nOTHINGYET = PARSERSTATE.FROM;
                        goto Label_061A;
                    }
                    break;
                }
                switch (type2)
                {
                case TokenType.Identifier:
                case TokenType.QuotedIdentifier:
                    nOTHINGYET  = PARSERSTATE.COLUMN;
                    maxPart     = 0;
                    namePart[0] = token2;
                    goto Label_061A;

                case TokenType.Other_LeftParen:
                    nOTHINGYET = PARSERSTATE.EXPRESSION;
                    num2++;
                    goto Label_061A;

                case TokenType.Other_RightParen:
                    throw System.Data.Common.ADP.SyntaxErrorMissingParenthesis();

                case TokenType.Other_Star:
                    nOTHINGYET  = PARSERSTATE.COLUMNALIAS;
                    maxPart     = 0;
                    namePart[0] = token2;
                    goto Label_061A;
                }
                break;
            }

            case PARSERSTATE.COLUMN:
            {
                TokenType type = token2.Type;
                if (type > TokenType.Other_Star)
                {
                    switch (type)
                    {
                    case TokenType.Keyword_AS:
                        goto Label_061A;

                    case TokenType.Keyword_FROM:
                        goto Label_01FF;
                    }
                }
                else
                {
                    switch (type)
                    {
                    case TokenType.Identifier:
                    case TokenType.QuotedIdentifier:
                        if (TokenType.Other_Period != type7)
                        {
                            nOTHINGYET = PARSERSTATE.COLUMNALIAS;
                            @null      = token2;
                        }
                        else
                        {
                            namePart[++maxPart] = token2;
                        }
                        goto Label_061A;

                    case TokenType.Other_Comma:
                        goto Label_01FF;

                    case TokenType.Other_Period:
                        if (maxPart > 3)
                        {
                            throw System.Data.Common.ADP.SyntaxErrorTooManyNameParts();
                        }
                        goto Label_061A;

                    case TokenType.Other_LeftParen:
                        nOTHINGYET = PARSERSTATE.EXPRESSION;
                        num2++;
                        maxPart = -1;
                        goto Label_061A;

                    case TokenType.Other_RightParen:
                        throw System.Data.Common.ADP.SyntaxErrorMissingParenthesis();

                    case TokenType.Other_Star:
                        nOTHINGYET          = PARSERSTATE.COLUMNALIAS;
                        namePart[++maxPart] = token2;
                        goto Label_061A;
                    }
                }
                nOTHINGYET = PARSERSTATE.EXPRESSION;
                maxPart    = -1;
                goto Label_061A;
            }

            case PARSERSTATE.COLUMNALIAS:
            {
                TokenType type9 = token2.Type;
                if ((type9 != TokenType.Other_Comma) && (type9 != TokenType.Keyword_FROM))
                {
                    throw System.Data.Common.ADP.SyntaxErrorExpectedCommaAfterColumn();
                }
                nOTHINGYET = (token2.Type == TokenType.Keyword_FROM) ? PARSERSTATE.FROM : PARSERSTATE.SELECT;
                this.AddColumn(maxPart, namePart, @null);
                maxPart = -1;
                @null   = Token.Null;
                goto Label_061A;
            }

            case PARSERSTATE.TABLE:
                switch (token2.Type)
                {
                case TokenType.Null:
                case TokenType.Keyword_COMPUTE:
                case TokenType.Keyword_FOR:
                case TokenType.Keyword_GROUP:
                case TokenType.Keyword_HAVING:
                case TokenType.Keyword_INTERSECT:
                case TokenType.Keyword_MINUS:
                case TokenType.Keyword_ORDER:
                case TokenType.Keyword_UNION:
                case TokenType.Keyword_WHERE:
                    nOTHINGYET = PARSERSTATE.DONE;
                    flag       = true;
                    goto Label_061A;

                case TokenType.Identifier:
                case TokenType.QuotedIdentifier:
                    if (TokenType.Other_Period != type7)
                    {
                        nOTHINGYET = PARSERSTATE.TABLEALIAS;
                        @null      = token2;
                    }
                    else
                    {
                        namePart[++maxPart] = token2;
                    }
                    goto Label_061A;

                case TokenType.Other_Comma:
                case TokenType.Keyword_JOIN:
                    nOTHINGYET = PARSERSTATE.FROM;
                    flag       = true;
                    goto Label_061A;

                case TokenType.Other_Period:
                    if (maxPart > 2)
                    {
                        throw System.Data.Common.ADP.SyntaxErrorTooManyNameParts();
                    }
                    goto Label_061A;

                case TokenType.Keyword_AS:
                    goto Label_061A;

                case TokenType.Keyword_CROSS:
                case TokenType.Keyword_LEFT:
                case TokenType.Keyword_NATURAL:
                case TokenType.Keyword_RIGHT:
                    nOTHINGYET = PARSERSTATE.JOIN;
                    flag       = true;
                    goto Label_061A;

                case TokenType.Keyword_ON:
                case TokenType.Keyword_USING:
                    nOTHINGYET = PARSERSTATE.JOINCONDITION;
                    flag       = true;
                    goto Label_061A;
                }
                throw System.Data.Common.ADP.SyntaxErrorExpectedNextPart();

            case PARSERSTATE.TABLEALIAS:
                flag = true;
                switch (token2.Type)
                {
                case TokenType.Keyword_COMPUTE:
                case TokenType.Keyword_FOR:
                case TokenType.Keyword_GROUP:
                case TokenType.Keyword_HAVING:
                case TokenType.Keyword_INTERSECT:
                case TokenType.Keyword_MINUS:
                case TokenType.Keyword_ORDER:
                case TokenType.Keyword_UNION:
                case TokenType.Keyword_WHERE:
                case TokenType.Null:
                    nOTHINGYET = PARSERSTATE.DONE;
                    goto Label_061A;

                case TokenType.Keyword_CROSS:
                case TokenType.Keyword_LEFT:
                case TokenType.Keyword_NATURAL:
                case TokenType.Keyword_RIGHT:
                    nOTHINGYET = PARSERSTATE.JOIN;
                    goto Label_061A;

                case TokenType.Keyword_JOIN:
                case TokenType.Other_Comma:
                    nOTHINGYET = PARSERSTATE.FROM;
                    goto Label_061A;

                case TokenType.Keyword_ON:
                case TokenType.Keyword_USING:
                    nOTHINGYET = PARSERSTATE.JOINCONDITION;
                    goto Label_061A;
                }
                throw System.Data.Common.ADP.SyntaxErrorExpectedCommaAfterTable();

            case PARSERSTATE.FROM:
                switch (token2.Type)
                {
                case TokenType.Identifier:
                case TokenType.QuotedIdentifier:
                    nOTHINGYET  = PARSERSTATE.TABLE;
                    maxPart     = 0;
                    namePart[0] = token2;
                    goto Label_061A;
                }
                throw System.Data.Common.ADP.SyntaxErrorExpectedIdentifier();

            case PARSERSTATE.EXPRESSION:
                switch (token2.Type)
                {
                case TokenType.Identifier:
                case TokenType.QuotedIdentifier:
                    if (num2 == 0)
                    {
                        @null = token2;
                    }
                    break;

                case TokenType.Other_Comma:
                case TokenType.Keyword_FROM:
                    if (num2 == 0)
                    {
                        nOTHINGYET = (token2.Type == TokenType.Keyword_FROM) ? PARSERSTATE.FROM : PARSERSTATE.SELECT;
                        this.AddColumn(maxPart, namePart, @null);
                        maxPart = -1;
                        @null   = Token.Null;
                    }
                    break;

                case TokenType.Other_LeftParen:
                    num2++;
                    break;

                case TokenType.Other_RightParen:
                    num2--;
                    break;
                }
                goto Label_061A;

            case PARSERSTATE.JOIN:
                switch (token2.Type)
                {
                case TokenType.Keyword_INNER:
                case TokenType.Keyword_OUTER:
                    goto Label_061A;

                case TokenType.Keyword_JOIN:
                    nOTHINGYET = PARSERSTATE.FROM;
                    goto Label_061A;
                }
                throw System.Data.Common.ADP.SyntaxErrorExpectedNextPart();

            case PARSERSTATE.JOINCONDITION:
                switch (token2.Type)
                {
                case TokenType.Other_LeftParen:
                    num2++;
                    goto Label_061A;

                case TokenType.Other_RightParen:
                    num2--;
                    goto Label_061A;
                }
                if (num2 == 0)
                {
                    switch (token2.Type)
                    {
                    case TokenType.Keyword_COMPUTE:
                    case TokenType.Keyword_FOR:
                    case TokenType.Keyword_GROUP:
                    case TokenType.Keyword_HAVING:
                    case TokenType.Keyword_INTERSECT:
                    case TokenType.Keyword_MINUS:
                    case TokenType.Keyword_ORDER:
                    case TokenType.Keyword_UNION:
                    case TokenType.Keyword_WHERE:
                    case TokenType.Null:
                        nOTHINGYET = PARSERSTATE.DONE;
                        break;

                    case TokenType.Keyword_CROSS:
                    case TokenType.Keyword_LEFT:
                    case TokenType.Keyword_NATURAL:
                    case TokenType.Keyword_RIGHT:
                        nOTHINGYET = PARSERSTATE.JOIN;
                        break;

                    case TokenType.Keyword_JOIN:
                        nOTHINGYET = PARSERSTATE.FROM;
                        break;
                    }
                }
                goto Label_061A;

            case PARSERSTATE.DONE:
                return;

            default:
                throw System.Data.Common.ADP.InvalidOperation(Res.GetString("ADP_SQLParserInternalError"));
            }
            nOTHINGYET = PARSERSTATE.EXPRESSION;
            goto Label_061A;
Label_01FF:
            nOTHINGYET = (token2.Type == TokenType.Keyword_FROM) ? PARSERSTATE.FROM : PARSERSTATE.SELECT;
            this.AddColumn(maxPart, namePart, @null);
            maxPart = -1;
            @null   = Token.Null;
Label_061A:
            if (flag)
            {
                this.AddTable(maxPart, namePart, @null);
                maxPart = -1;
                @null   = Token.Null;
                flag    = false;
            }
            type7  = token2.Type;
            match  = match.NextMatch();
            token2 = TokenFromMatch(match);
            goto Label_003B;
        }