Search in sources :

Example 71 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class OracleLexer method scanVariable.

public void scanVariable() {
    if (ch == '@') {
        scanChar();
        token = Token.MONKEYS_AT;
        return;
    }
    if (ch != ':' && ch != '#' && ch != '$') {
        throw new ParserException("illegal variable");
    }
    mark = pos;
    bufPos = 1;
    char ch;
    boolean quoteFlag = false;
    boolean mybatisFlag = false;
    if (charAt(pos + 1) == '"') {
        pos++;
        bufPos++;
        quoteFlag = true;
    } else if (charAt(pos + 1) == '{') {
        pos++;
        bufPos++;
        mybatisFlag = true;
    }
    for (; ; ) {
        ch = charAt(++pos);
        if (!isIdentifierChar(ch)) {
            break;
        }
        bufPos++;
        continue;
    }
    if (quoteFlag) {
        if (ch != '"') {
            throw new ParserException("syntax error");
        }
        ++pos;
        bufPos++;
    } else if (mybatisFlag) {
        if (ch != '}') {
            throw new ParserException("syntax error");
        }
        ++pos;
        bufPos++;
    }
    this.ch = charAt(pos);
    stringVal = addSymbol();
    Token tok = keywods.getKeyword(stringVal);
    if (tok != null) {
        token = tok;
    } else {
        token = Token.VARIANT;
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) Token(com.alibaba.druid.sql.parser.Token)

Example 72 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class OdpsStatementParser method parseDescribe.

public OdpsDescStmt parseDescribe() {
    if (lexer.token() == Token.DESC || identifierEquals("DESCRIBE")) {
        lexer.nextToken();
    } else {
        throw new ParserException("expect DESC, actual " + lexer.token());
    }
    OdpsDescStmt stmt = new OdpsDescStmt();
    if (identifierEquals("ROLE")) {
        lexer.nextToken();
        stmt.setObjectType(SQLObjectType.ROLE);
    } else if (identifierEquals("PACKAGE")) {
        lexer.nextToken();
        stmt.setObjectType(SQLObjectType.PACKAGE);
    } else if (identifierEquals("INSTANCE")) {
        lexer.nextToken();
        stmt.setObjectType(SQLObjectType.INSTANCE);
    }
    stmt.setObject(this.exprParser.name());
    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        this.accept(Token.LPAREN);
        for (; ; ) {
            stmt.getPartition().add(this.exprParser.expr());
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            if (lexer.token() == Token.RPAREN) {
                lexer.nextToken();
                break;
            }
        }
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OdpsDescStmt(com.alibaba.druid.sql.dialect.odps.ast.OdpsDescStmt)

Example 73 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlStatementParser method parseDescribe.

public MySqlDescribeStatement parseDescribe() {
    if (lexer.token() == Token.DESC || identifierEquals(DESCRIBE)) {
        lexer.nextToken();
    } else {
        throw new ParserException("expect DESC, actual " + lexer.token());
    }
    MySqlDescribeStatement stmt = new MySqlDescribeStatement();
    stmt.setObject(this.exprParser.name());
    if (lexer.token() == Token.IDENTIFIER) {
        stmt.setColName(this.exprParser.name());
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlDescribeStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement)

Example 74 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlStatementParser method parseLoadXml.

protected MySqlLoadXmlStatement parseLoadXml() {
    acceptIdentifier("XML");
    MySqlLoadXmlStatement stmt = new MySqlLoadXmlStatement();
    if (identifierEquals(LOW_PRIORITY)) {
        stmt.setLowPriority(true);
        lexer.nextToken();
    }
    if (identifierEquals("CONCURRENT")) {
        stmt.setConcurrent(true);
        lexer.nextToken();
    }
    if (identifierEquals(LOCAL)) {
        stmt.setLocal(true);
        lexer.nextToken();
    }
    acceptIdentifier("INFILE");
    SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr();
    stmt.setFileName(fileName);
    if (lexer.token() == Token.REPLACE) {
        stmt.setReplicate(true);
        lexer.nextToken();
    }
    if (identifierEquals(IGNORE)) {
        stmt.setIgnore(true);
        lexer.nextToken();
    }
    accept(Token.INTO);
    accept(Token.TABLE);
    SQLName tableName = exprParser.name();
    stmt.setTableName(tableName);
    if (identifierEquals(CHARACTER)) {
        lexer.nextToken();
        accept(Token.SET);
        if (lexer.token() != Token.LITERAL_CHARS) {
            throw new ParserException("syntax error, illegal charset");
        }
        String charset = lexer.stringVal();
        lexer.nextToken();
        stmt.setCharset(charset);
    }
    if (identifierEquals("ROWS")) {
        lexer.nextToken();
        accept(Token.IDENTIFIED);
        accept(Token.BY);
        SQLExpr rowsIdentifiedBy = exprParser.expr();
        stmt.setRowsIdentifiedBy(rowsIdentifiedBy);
    }
    if (identifierEquals(IGNORE)) {
        throw new ParserException("TODO");
    }
    if (lexer.token() == Token.SET) {
        throw new ParserException("TODO");
    }
    return stmt;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) MySqlLoadXmlStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 75 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project canal by alibaba.

the class DruidDdlParser method parse.

public static List<DdlResult> parse(String queryString, String schmeaName) {
    List<SQLStatement> stmtList = null;
    try {
        stmtList = SQLUtils.parseStatements(queryString, JdbcConstants.MYSQL, false);
    } catch (ParserException e) {
        // 可能存在一些SQL是不支持的,比如存储过程
        DdlResult ddlResult = new DdlResult();
        ddlResult.setType(EventType.QUERY);
        return Arrays.asList(ddlResult);
    }
    List<DdlResult> ddlResults = new ArrayList<>();
    for (SQLStatement statement : stmtList) {
        if (statement instanceof SQLCreateTableStatement) {
            DdlResult ddlResult = new DdlResult();
            SQLCreateTableStatement createTable = (SQLCreateTableStatement) statement;
            processName(ddlResult, schmeaName, createTable.getName(), false);
            ddlResult.setType(EventType.CREATE);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLAlterTableStatement) {
            SQLAlterTableStatement alterTable = (SQLAlterTableStatement) statement;
            if (alterTable.getTableOptions().size() > 0) {
                DdlResult ddlResult = new DdlResult();
                processName(ddlResult, schmeaName, alterTable.getName(), false);
                ddlResult.setType(EventType.ALTER);
                ddlResults.add(ddlResult);
            }
            for (SQLAlterTableItem item : alterTable.getItems()) {
                if (item instanceof SQLAlterTableRename) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), true);
                    processName(ddlResult, schmeaName, ((SQLAlterTableRename) item).getToName(), false);
                    ddlResult.setType(EventType.RENAME);
                    ddlResults.add(ddlResult);
                } else if (item instanceof SQLAlterTableAddIndex) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    ddlResult.setType(EventType.CINDEX);
                    ddlResults.add(ddlResult);
                } else if (item instanceof SQLAlterTableDropIndex || item instanceof SQLAlterTableDropKey) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    ddlResult.setType(EventType.DINDEX);
                    ddlResults.add(ddlResult);
                } else if (item instanceof SQLAlterTableAddConstraint) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    SQLConstraint constraint = ((SQLAlterTableAddConstraint) item).getConstraint();
                    if (constraint instanceof SQLUnique) {
                        ddlResult.setType(EventType.CINDEX);
                        ddlResults.add(ddlResult);
                    }
                } else if (item instanceof SQLAlterTableDropConstraint) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    ddlResult.setType(EventType.DINDEX);
                    ddlResults.add(ddlResult);
                } else {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    ddlResult.setType(EventType.ALTER);
                    ddlResults.add(ddlResult);
                }
            }
        } else if (statement instanceof SQLDropTableStatement) {
            SQLDropTableStatement dropTable = (SQLDropTableStatement) statement;
            for (SQLExprTableSource tableSource : dropTable.getTableSources()) {
                DdlResult ddlResult = new DdlResult();
                processName(ddlResult, schmeaName, tableSource.getExpr(), false);
                ddlResult.setType(EventType.ERASE);
                ddlResults.add(ddlResult);
            }
        } else if (statement instanceof SQLCreateIndexStatement) {
            SQLCreateIndexStatement createIndex = (SQLCreateIndexStatement) statement;
            SQLTableSource tableSource = createIndex.getTable();
            DdlResult ddlResult = new DdlResult();
            processName(ddlResult, schmeaName, ((SQLExprTableSource) tableSource).getExpr(), false);
            ddlResult.setType(EventType.CINDEX);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLDropIndexStatement) {
            SQLDropIndexStatement dropIndex = (SQLDropIndexStatement) statement;
            SQLExprTableSource tableSource = dropIndex.getTableName();
            DdlResult ddlResult = new DdlResult();
            processName(ddlResult, schmeaName, tableSource.getExpr(), false);
            ddlResult.setType(EventType.DINDEX);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLTruncateStatement) {
            SQLTruncateStatement truncate = (SQLTruncateStatement) statement;
            for (SQLExprTableSource tableSource : truncate.getTableSources()) {
                DdlResult ddlResult = new DdlResult();
                processName(ddlResult, schmeaName, tableSource.getExpr(), false);
                ddlResult.setType(EventType.TRUNCATE);
                ddlResults.add(ddlResult);
            }
        } else if (statement instanceof MySqlRenameTableStatement) {
            MySqlRenameTableStatement rename = (MySqlRenameTableStatement) statement;
            for (Item item : rename.getItems()) {
                DdlResult ddlResult = new DdlResult();
                processName(ddlResult, schmeaName, item.getName(), true);
                processName(ddlResult, schmeaName, item.getTo(), false);
                ddlResult.setType(EventType.RENAME);
                ddlResults.add(ddlResult);
            }
        } else if (statement instanceof SQLInsertStatement) {
            DdlResult ddlResult = new DdlResult();
            SQLInsertStatement insert = (SQLInsertStatement) statement;
            processName(ddlResult, schmeaName, insert.getTableName(), false);
            ddlResult.setType(EventType.INSERT);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLUpdateStatement) {
            DdlResult ddlResult = new DdlResult();
            SQLUpdateStatement update = (SQLUpdateStatement) statement;
            // 拿到的表名可能为null,比如update a,b set a.id=x
            processName(ddlResult, schmeaName, update.getTableName(), false);
            ddlResult.setType(EventType.UPDATE);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLDeleteStatement) {
            DdlResult ddlResult = new DdlResult();
            SQLDeleteStatement delete = (SQLDeleteStatement) statement;
            // 拿到的表名可能为null,比如delete a,b from a where a.id = b.id
            processName(ddlResult, schmeaName, delete.getTableName(), false);
            ddlResult.setType(EventType.DELETE);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLCreateDatabaseStatement) {
            SQLCreateDatabaseStatement create = (SQLCreateDatabaseStatement) statement;
            DdlResult ddlResult = new DdlResult();
            ddlResult.setType(EventType.QUERY);
            processName(ddlResult, create.getDatabaseName(), null, false);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLDropDatabaseStatement) {
            SQLDropDatabaseStatement drop = (SQLDropDatabaseStatement) statement;
            DdlResult ddlResult = new DdlResult();
            ddlResult.setType(EventType.QUERY);
            processName(ddlResult, drop.getDatabaseName(), null, false);
            ddlResults.add(ddlResult);
        }
    }
    return ddlResults;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLAlterTableAddConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableAddConstraint) ArrayList(java.util.ArrayList) SQLAlterTableDropIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex) SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) SQLDropDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLDropDatabaseStatement) SQLUnique(com.alibaba.druid.sql.ast.statement.SQLUnique) SQLAlterTableDropKey(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropKey) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLAlterTableItem(com.alibaba.druid.sql.ast.statement.SQLAlterTableItem) SQLDropTableStatement(com.alibaba.druid.sql.ast.statement.SQLDropTableStatement) Item(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement.Item) SQLAlterTableItem(com.alibaba.druid.sql.ast.statement.SQLAlterTableItem) SQLCreateDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement) SQLAlterTableRename(com.alibaba.druid.sql.ast.statement.SQLAlterTableRename) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) MySqlRenameTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement) SQLDropIndexStatement(com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLConstraint(com.alibaba.druid.sql.ast.statement.SQLConstraint) SQLAlterTableDropConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLAlterTableAddIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableAddIndex) SQLTruncateStatement(com.alibaba.druid.sql.ast.statement.SQLTruncateStatement) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLCreateIndexStatement(com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement)

Aggregations

ParserException (com.alibaba.druid.sql.parser.ParserException)98 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)25 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLName (com.alibaba.druid.sql.ast.SQLName)16 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)9 Token (com.alibaba.druid.sql.parser.Token)9 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)5 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)5 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)4 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)4 OracleConstraint (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint)4 DbType (com.alibaba.druid.DbType)3 SQLDeclareItem (com.alibaba.druid.sql.ast.SQLDeclareItem)3 SQLPartitionByHash (com.alibaba.druid.sql.ast.SQLPartitionByHash)3