Search in sources :

Example 1 with MySQLDDLParser

use of com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDDLParser in project cobar by alibaba.

the class SQLParserDelegate method parse.

public static SQLStatement parse(String sql, MySQLLexer lexer, String charset) throws SQLSyntaxErrorException {
    try {
        SQLStatement stmt = null;
        boolean isEOF = true;
        MySQLExprParser exprParser = new MySQLExprParser(lexer, charset);
        stmtSwitch: switch(lexer.token()) {
            case KW_DESC:
            case KW_DESCRIBE:
                stmt = new MySQLDALParser(lexer, exprParser).desc();
                break stmtSwitch;
            case KW_SELECT:
            case PUNC_LEFT_PAREN:
                stmt = new MySQLDMLSelectParser(lexer, exprParser).selectUnion();
                break stmtSwitch;
            case KW_DELETE:
                stmt = new MySQLDMLDeleteParser(lexer, exprParser).delete();
                break stmtSwitch;
            case KW_INSERT:
                stmt = new MySQLDMLInsertParser(lexer, exprParser).insert();
                break stmtSwitch;
            case KW_REPLACE:
                stmt = new MySQLDMLReplaceParser(lexer, exprParser).replace();
                break stmtSwitch;
            case KW_UPDATE:
                stmt = new MySQLDMLUpdateParser(lexer, exprParser).update();
                break stmtSwitch;
            case KW_CALL:
                stmt = new MySQLDMLCallParser(lexer, exprParser).call();
                break stmtSwitch;
            case KW_SET:
                stmt = new MySQLDALParser(lexer, exprParser).set();
                break stmtSwitch;
            case KW_SHOW:
                stmt = new MySQLDALParser(lexer, exprParser).show();
                break stmtSwitch;
            case KW_ALTER:
            case KW_CREATE:
            case KW_DROP:
            case KW_RENAME:
                stmt = new MySQLDDLParser(lexer, exprParser).ddlStmt();
                isEOF = isEOFedDDL(stmt);
                break stmtSwitch;
            case KW_RELEASE:
                stmt = new MySQLMTSParser(lexer).release();
                break stmtSwitch;
            case IDENTIFIER:
                SpecialIdentifier si = null;
                if ((si = specialIdentifiers.get(lexer.stringValueUppercase())) != null) {
                    switch(si) {
                        case TRUNCATE:
                            stmt = new MySQLDDLParser(lexer, exprParser).truncate();
                            break stmtSwitch;
                        case SAVEPOINT:
                            stmt = new MySQLMTSParser(lexer).savepoint();
                            break stmtSwitch;
                        case ROLLBACK:
                            stmt = new MySQLMTSParser(lexer).rollback();
                            break stmtSwitch;
                    }
                }
            default:
                throw new SQLSyntaxErrorException("sql is not a supported statement");
        }
        if (isEOF) {
            while (lexer.token() == MySQLToken.PUNC_SEMICOLON) {
                lexer.nextToken();
            }
            if (lexer.token() != MySQLToken.EOF) {
                throw new SQLSyntaxErrorException("SQL syntax error!");
            }
        }
        return stmt;
    } catch (Exception e) {
        throw new SQLSyntaxErrorException(buildErrorMsg(e, lexer, sql), e);
    }
}
Also used : MySQLDMLUpdateParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLUpdateParser) MySQLDMLSelectParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLSelectParser) MySQLDDLParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDDLParser) MySQLDALParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDALParser) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySQLDMLCallParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLCallParser) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) MySQLDMLDeleteParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLDeleteParser) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySQLExprParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser) MySQLDMLReplaceParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLReplaceParser) MySQLMTSParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLMTSParser) MySQLDMLInsertParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLInsertParser)

Aggregations

SQLStatement (com.alibaba.cobar.parser.ast.stmt.SQLStatement)1 MySQLDALParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDALParser)1 MySQLDDLParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDDLParser)1 MySQLDMLCallParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLCallParser)1 MySQLDMLDeleteParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLDeleteParser)1 MySQLDMLInsertParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLInsertParser)1 MySQLDMLReplaceParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLReplaceParser)1 MySQLDMLSelectParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLSelectParser)1 MySQLDMLUpdateParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLUpdateParser)1 MySQLExprParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser)1 MySQLMTSParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLMTSParser)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1