Search in sources :

Example 16 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project druid by alibaba.

the class MySqlStatementParser method parseAnalyze.

public MySqlAnalyzeStatement parseAnalyze() {
    accept(Token.ANALYZE);
    accept(Token.TABLE);
    MySqlAnalyzeStatement stmt = new MySqlAnalyzeStatement();
    List<SQLName> names = new ArrayList<SQLName>();
    this.exprParser.names(names, stmt);
    for (SQLName name : names) {
        stmt.addTableSource(new SQLExprTableSource(name));
    }
    return stmt;
}
Also used : MySqlAnalyzeStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAnalyzeStatement) ArrayList(java.util.ArrayList) SQLName(com.alibaba.druid.sql.ast.SQLName)

Example 17 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project druid by alibaba.

the class MySqlStatementParser method parseInsert.

public SQLInsertStatement parseInsert() {
    MySqlInsertStatement insertStatement = new MySqlInsertStatement();
    if (lexer.token() == Token.INSERT) {
        lexer.nextToken();
        for (; ; ) {
            if (identifierEquals(LOW_PRIORITY)) {
                insertStatement.setLowPriority(true);
                lexer.nextToken();
                continue;
            }
            if (identifierEquals(DELAYED)) {
                insertStatement.setDelayed(true);
                lexer.nextToken();
                continue;
            }
            if (identifierEquals("HIGH_PRIORITY")) {
                insertStatement.setHighPriority(true);
                lexer.nextToken();
                continue;
            }
            if (identifierEquals(IGNORE)) {
                insertStatement.setIgnore(true);
                lexer.nextToken();
                continue;
            }
            if (identifierEquals("ROLLBACK_ON_FAIL")) {
                insertStatement.setRollbackOnFail(true);
                lexer.nextToken();
                continue;
            }
            break;
        }
        if (lexer.token() == Token.INTO) {
            lexer.nextToken();
        }
        SQLName tableName = this.exprParser.name();
        insertStatement.setTableName(tableName);
        if (lexer.token() == Token.IDENTIFIER && !identifierEquals("VALUE")) {
            insertStatement.setAlias(lexer.stringVal());
            lexer.nextToken();
        }
    }
    int columnSize = 0;
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        if (lexer.token() == (Token.SELECT)) {
            SQLSelect select = this.exprParser.createSelectParser().select();
            select.setParent(insertStatement);
            insertStatement.setQuery(select);
        } else {
            this.exprParser.exprList(insertStatement.getColumns(), insertStatement);
            columnSize = insertStatement.getColumns().size();
        }
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.VALUES || identifierEquals("VALUE")) {
        lexer.nextTokenLParen();
        parseValueClause(insertStatement.getValuesList(), columnSize);
    } else if (lexer.token() == Token.SET) {
        lexer.nextToken();
        SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause();
        insertStatement.getValuesList().add(values);
        for (; ; ) {
            SQLName name = this.exprParser.name();
            insertStatement.addColumn(name);
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            } else {
                accept(Token.COLONEQ);
            }
            values.addValue(this.exprParser.expr());
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
    } else if (lexer.token() == (Token.SELECT)) {
        SQLSelect select = this.exprParser.createSelectParser().select();
        select.setParent(insertStatement);
        insertStatement.setQuery(select);
    } else if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        SQLSelect select = this.exprParser.createSelectParser().select();
        select.setParent(insertStatement);
        insertStatement.setQuery(select);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.ON) {
        lexer.nextToken();
        acceptIdentifier("DUPLICATE");
        accept(Token.KEY);
        accept(Token.UPDATE);
        exprParser.exprList(insertStatement.getDuplicateKeyUpdate(), insertStatement);
    }
    return insertStatement;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Example 18 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project druid by alibaba.

the class OracleStatementParser method parseDeleteStatement.

public OracleDeleteStatement parseDeleteStatement() {
    OracleDeleteStatement deleteStatement = new OracleDeleteStatement();
    if (lexer.token() == Token.DELETE) {
        lexer.nextToken();
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        parseHints(deleteStatement.getHints());
        if (lexer.token() == (Token.FROM)) {
            lexer.nextToken();
        }
        if (identifierEquals("ONLY")) {
            lexer.nextToken();
            accept(Token.LPAREN);
            SQLName tableName = exprParser.name();
            deleteStatement.setTableName(tableName);
            accept(Token.RPAREN);
        } else if (lexer.token() == Token.LPAREN) {
            SQLTableSource tableSource = this.createSQLSelectParser().parseTableSource();
            deleteStatement.setTableSource(tableSource);
        } else {
            SQLName tableName = exprParser.name();
            deleteStatement.setTableName(tableName);
        }
        deleteStatement.setAlias(as());
    }
    if (lexer.token() == (Token.WHERE)) {
        lexer.nextToken();
        deleteStatement.setWhere(this.exprParser.expr());
    }
    if (lexer.token() == Token.RETURNING) {
        OracleReturningClause clause = this.parseReturningClause();
        deleteStatement.setReturning(clause);
    }
    if (identifierEquals("RETURN") || identifierEquals("RETURNING")) {
        throw new ParserException("TODO");
    }
    if (identifierEquals("LOG")) {
        throw new ParserException("TODO");
    }
    return deleteStatement;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OracleDeleteStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) OracleReturningClause(com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleReturningClause)

Example 19 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project druid by alibaba.

the class OracleCreateTableParser method parsePartitionByRest.

protected void parsePartitionByRest(SQLPartitionBy clause) {
    if (lexer.token() == Token.STORE) {
        lexer.nextToken();
        accept(Token.IN);
        accept(Token.LPAREN);
        for (; ; ) {
            SQLName tablespace = this.exprParser.name();
            clause.getStoreIn().add(tablespace);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
        accept(Token.RPAREN);
    }
    if (identifierEquals("SUBPARTITION")) {
        SQLSubPartitionBy subPartitionBy = subPartitionBy();
        clause.setSubPartitionBy(subPartitionBy);
    }
    accept(Token.LPAREN);
    for (; ; ) {
        SQLPartition partition = parsePartition();
        clause.addPartition(partition);
        if (lexer.token() == Token.COMMA) {
            lexer.nextToken();
            continue;
        }
        break;
    }
    accept(Token.RPAREN);
}
Also used : SQLPartition(com.alibaba.druid.sql.ast.SQLPartition) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLSubPartitionBy(com.alibaba.druid.sql.ast.SQLSubPartitionBy)

Example 20 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project druid by alibaba.

the class OracleCreateTableParser method subPartitionBy.

protected SQLSubPartitionBy subPartitionBy() {
    lexer.nextToken();
    accept(Token.BY);
    if (identifierEquals("HASH")) {
        lexer.nextToken();
        accept(Token.LPAREN);
        SQLSubPartitionByHash byHash = new SQLSubPartitionByHash();
        SQLExpr expr = this.exprParser.expr();
        byHash.setExpr(expr);
        accept(Token.RPAREN);
        return byHash;
    } else if (identifierEquals("LIST")) {
        lexer.nextToken();
        accept(Token.LPAREN);
        SQLSubPartitionByList byList = new SQLSubPartitionByList();
        SQLName column = this.exprParser.name();
        byList.setColumn(column);
        accept(Token.RPAREN);
        if (identifierEquals("SUBPARTITION")) {
            lexer.nextToken();
            acceptIdentifier("TEMPLATE");
            accept(Token.LPAREN);
            for (; ; ) {
                SQLSubPartition subPartition = parseSubPartition();
                subPartition.setParent(byList);
                byList.getSubPartitionTemplate().add(subPartition);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            accept(Token.RPAREN);
        }
        return byList;
    }
    throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
}
Also used : SQLSubPartitionByList(com.alibaba.druid.sql.ast.SQLSubPartitionByList) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLSubPartition(com.alibaba.druid.sql.ast.SQLSubPartition) SQLSubPartitionByHash(com.alibaba.druid.sql.ast.SQLSubPartitionByHash) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLName (com.alibaba.druid.sql.ast.SQLName)102 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)33 TableStat (com.alibaba.druid.stat.TableStat)20 ParserException (com.alibaba.druid.sql.parser.ParserException)17 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)8 SQLObject (com.alibaba.druid.sql.ast.SQLObject)6 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)6 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)6 WallContext (com.alibaba.druid.wall.WallContext)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)5 WallSqlTableStat (com.alibaba.druid.wall.WallSqlTableStat)5 SQLPartition (com.alibaba.druid.sql.ast.SQLPartition)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)4 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)4 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)4 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)4 SQLSubPartition (com.alibaba.druid.sql.ast.SQLSubPartition)3 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3