Search in sources :

Example 56 with SQLName

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

the class OracleStatementParser method parseAlterTableSplit.

private void parseAlterTableSplit(SQLAlterTableStatement stmt) {
    lexer.nextToken();
    if (identifierEquals("PARTITION")) {
        lexer.nextToken();
        OracleAlterTableSplitPartition item = new OracleAlterTableSplitPartition();
        item.setName(this.exprParser.name());
        if (identifierEquals("AT")) {
            lexer.nextToken();
            accept(Token.LPAREN);
            this.exprParser.exprList(item.getAt(), item);
            accept(Token.RPAREN);
        } else {
            throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
        }
        if (lexer.token() == Token.INTO) {
            lexer.nextToken();
            accept(Token.LPAREN);
            for (; ; ) {
                NestedTablePartitionSpec spec = new NestedTablePartitionSpec();
                acceptIdentifier("PARTITION");
                spec.setPartition(this.exprParser.name());
                for (; ; ) {
                    if (lexer.token() == Token.TABLESPACE) {
                        lexer.nextToken();
                        SQLName tablespace = this.exprParser.name();
                        spec.getSegmentAttributeItems().add(new TableSpaceItem(tablespace));
                        continue;
                    } else if (identifierEquals("PCTREE")) {
                        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
                    } else if (identifierEquals("PCTUSED")) {
                        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
                    } else if (identifierEquals("INITRANS")) {
                        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
                    } else if (identifierEquals("STORAGE")) {
                        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
                    } else if (identifierEquals("LOGGING")) {
                        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
                    } else if (identifierEquals("NOLOGGING")) {
                        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
                    } else if (identifierEquals("FILESYSTEM_LIKE_LOGGING")) {
                        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
                    }
                    break;
                }
                item.getInto().add(spec);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            accept(Token.RPAREN);
        }
        if (lexer.token() == Token.UPDATE) {
            lexer.nextToken();
            acceptIdentifier("INDEXES");
            UpdateIndexesClause updateIndexes = new UpdateIndexesClause();
            item.setUpdateIndexes(updateIndexes);
        }
        stmt.addItem(item);
    } else {
        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) UpdateIndexesClause(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterTableSplitPartition.UpdateIndexesClause) NestedTablePartitionSpec(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterTableSplitPartition.NestedTablePartitionSpec) OracleAlterTableSplitPartition(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterTableSplitPartition) TableSpaceItem(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterTableSplitPartition.TableSpaceItem)

Example 57 with SQLName

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

the class PGSQLStatementParser method parseInsert.

public PGInsertStatement parseInsert() {
    PGInsertStatement stmt = new PGInsertStatement();
    if (lexer.token() == Token.INSERT) {
        lexer.nextToken();
        accept(Token.INTO);
        SQLName tableName = this.exprParser.name();
        stmt.setTableName(tableName);
        if (lexer.token() == Token.IDENTIFIER) {
            stmt.setAlias(lexer.stringVal());
            lexer.nextToken();
        }
    }
    if (lexer.token() == Token.DEFAULT) {
        lexer.nextToken();
        accept(Token.VALUES);
        stmt.setDefaultValues(true);
    }
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getColumns(), stmt);
        accept(Token.RPAREN);
    }
    if (lexer.token() == (Token.VALUES)) {
        lexer.nextToken();
        for (; ; ) {
            accept(Token.LPAREN);
            SQLInsertStatement.ValuesClause valuesCaluse = new SQLInsertStatement.ValuesClause();
            this.exprParser.exprList(valuesCaluse.getValues(), valuesCaluse);
            stmt.addValueCause(valuesCaluse);
            accept(Token.RPAREN);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
    } else if (lexer.token() == (Token.SELECT)) {
        SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr();
        stmt.setQuery(queryExpr.getSubQuery());
    }
    if (lexer.token() == Token.RETURNING) {
        lexer.nextToken();
        SQLExpr returning = this.exprParser.expr();
        stmt.setReturning(returning);
    }
    return stmt;
}
Also used : SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) PGInsertStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 58 with SQLName

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

the class PGSQLStatementParser method parseDeleteStatement.

public PGDeleteStatement parseDeleteStatement() {
    lexer.nextToken();
    PGDeleteStatement deleteStatement = new PGDeleteStatement();
    if (lexer.token() == (Token.FROM)) {
        lexer.nextToken();
    }
    if (lexer.token() == (Token.ONLY)) {
        lexer.nextToken();
        deleteStatement.setOnly(true);
    }
    SQLName tableName = exprParser.name();
    deleteStatement.setTableName(tableName);
    if (lexer.token() == Token.AS) {
        accept(Token.AS);
    }
    if (lexer.token() == Token.IDENTIFIER) {
        deleteStatement.setAlias(lexer.stringVal());
        lexer.nextToken();
    }
    if (lexer.token() == Token.USING) {
        lexer.nextToken();
        for (; ; ) {
            SQLName name = this.exprParser.name();
            deleteStatement.getUsing().add(name);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
    }
    if (lexer.token() == (Token.WHERE)) {
        lexer.nextToken();
        if (lexer.token() == Token.CURRENT) {
            lexer.nextToken();
            accept(Token.OF);
            SQLName cursorName = this.exprParser.name();
            SQLExpr where = new SQLCurrentOfCursorExpr(cursorName);
            deleteStatement.setWhere(where);
        } else {
            SQLExpr where = this.exprParser.expr();
            deleteStatement.setWhere(where);
        }
    }
    if (lexer.token() == Token.RETURNING) {
        lexer.nextToken();
        accept(Token.STAR);
        deleteStatement.setReturning(true);
    }
    return deleteStatement;
}
Also used : SQLCurrentOfCursorExpr(com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) PGDeleteStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 59 with SQLName

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

the class OracleStatementParser method parseAlterDrop.

public void parseAlterDrop(SQLAlterTableStatement stmt) {
    lexer.nextToken();
    if (lexer.token() == Token.CONSTRAINT) {
        lexer.nextToken();
        SQLAlterTableDropConstraint item = new SQLAlterTableDropConstraint();
        item.setConstraintName(this.exprParser.name());
        stmt.addItem(item);
    } else if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLAlterTableDropColumnItem item = new SQLAlterTableDropColumnItem();
        this.exprParser.names(item.getColumns());
        stmt.addItem(item);
        accept(Token.RPAREN);
    } else if (lexer.token() == Token.COLUMN) {
        lexer.nextToken();
        SQLAlterTableDropColumnItem item = new SQLAlterTableDropColumnItem();
        this.exprParser.names(item.getColumns());
        stmt.addItem(item);
    } else if (identifierEquals("PARTITION")) {
        lexer.nextToken();
        OracleAlterTableDropPartition item = new OracleAlterTableDropPartition();
        item.setName(this.exprParser.name());
        stmt.addItem(item);
    } else if (lexer.token() == Token.INDEX) {
        lexer.nextToken();
        SQLName indexName = this.exprParser.name();
        SQLAlterTableDropIndex item = new SQLAlterTableDropIndex();
        item.setIndexName(indexName);
        stmt.addItem(item);
    } else {
        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) OracleAlterTableDropPartition(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterTableDropPartition)

Example 60 with SQLName

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

the class OracleStatementParser method parseStatementList.

public void parseStatementList(List<SQLStatement> statementList, int max) {
    for (; ; ) {
        if (max != -1) {
            if (statementList.size() >= max) {
                return;
            }
        }
        if (lexer.token() == Token.EOF) {
            return;
        }
        if (lexer.token() == Token.END) {
            return;
        }
        if (lexer.token() == Token.ELSE) {
            return;
        }
        if (lexer.token() == (Token.SEMI)) {
            lexer.nextToken();
            continue;
        }
        if (lexer.token() == (Token.SELECT)) {
            SQLSelectStatement stmt = new SQLSelectStatement(new OracleSelectParser(this.exprParser).select(), JdbcConstants.ORACLE);
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == (Token.UPDATE)) {
            statementList.add(parseUpdateStatement());
            continue;
        }
        if (lexer.token() == (Token.CREATE)) {
            statementList.add(parseCreate());
            continue;
        }
        if (lexer.token() == Token.INSERT) {
            statementList.add(parseInsert());
            continue;
        }
        if (lexer.token() == (Token.DELETE)) {
            statementList.add(parseDeleteStatement());
            continue;
        }
        if (lexer.token() == (Token.SLASH)) {
            lexer.nextToken();
            statementList.add(new OraclePLSQLCommitStatement());
            continue;
        }
        if (lexer.token() == Token.ALTER) {
            statementList.add(parserAlter());
            continue;
        }
        if (lexer.token() == Token.WITH) {
            statementList.add(new SQLSelectStatement(new OracleSelectParser(this.exprParser).select()));
            continue;
        }
        if (lexer.token() == Token.LBRACE || identifierEquals("CALL")) {
            statementList.add(this.parseCall());
            continue;
        }
        if (lexer.token() == Token.MERGE) {
            statementList.add(this.parseMerge());
            continue;
        }
        if (lexer.token() == Token.BEGIN) {
            statementList.add(this.parseBlock());
            continue;
        }
        if (lexer.token() == Token.DECLARE) {
            statementList.add(this.parseBlock());
            continue;
        }
        if (lexer.token() == Token.LOCK) {
            statementList.add(this.parseLock());
            continue;
        }
        if (lexer.token() == Token.TRUNCATE) {
            statementList.add(this.parseTruncate());
            continue;
        }
        if (lexer.token() == Token.VARIANT) {
            SQLExpr variant = this.exprParser.primary();
            if (variant instanceof SQLBinaryOpExpr) {
                SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) variant;
                if (binaryOpExpr.getOperator() == SQLBinaryOperator.Assignment) {
                    SQLSetStatement stmt = new SQLSetStatement(binaryOpExpr.getLeft(), binaryOpExpr.getRight(), getDbType());
                    statementList.add(stmt);
                    continue;
                }
            }
            accept(Token.COLONEQ);
            SQLExpr value = this.exprParser.expr();
            SQLSetStatement stmt = new SQLSetStatement(variant, value, getDbType());
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.EXCEPTION) {
            statementList.add(this.parseException());
            continue;
        }
        if (identifierEquals("EXIT")) {
            lexer.nextToken();
            OracleExitStatement stmt = new OracleExitStatement();
            if (lexer.token() == Token.WHEN) {
                lexer.nextToken();
                stmt.setWhen(this.exprParser.expr());
            }
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.FETCH || identifierEquals("FETCH")) {
            SQLStatement stmt = parseFetch();
            statementList.add(stmt);
            continue;
        }
        if (identifierEquals("ROLLBACK")) {
            SQLRollbackStatement stmt = parseRollback();
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.EXPLAIN) {
            statementList.add(this.parseExplain());
            continue;
        }
        if (lexer.token() == Token.IDENTIFIER) {
            SQLExpr expr = exprParser.expr();
            OracleExprStatement stmt = new OracleExprStatement(expr);
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.LPAREN) {
            char ch = lexer.current();
            int bp = lexer.bp();
            lexer.nextToken();
            if (lexer.token() == Token.SELECT) {
                lexer.reset(bp, ch, Token.LPAREN);
                statementList.add(this.parseSelect());
                continue;
            } else {
                throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
            }
        }
        if (lexer.token() == Token.SET) {
            statementList.add(this.parseSet());
            continue;
        }
        if (lexer.token() == Token.GRANT) {
            statementList.add(this.parseGrant());
            continue;
        }
        if (lexer.token() == Token.REVOKE) {
            statementList.add(this.parseRevoke());
            continue;
        }
        if (lexer.token() == Token.COMMENT) {
            statementList.add(this.parseComment());
            continue;
        }
        if (lexer.token() == Token.FOR) {
            statementList.add(this.parseFor());
            continue;
        }
        if (lexer.token() == Token.LOOP) {
            statementList.add(this.parseLoop());
            continue;
        }
        if (lexer.token() == Token.IF) {
            statementList.add(this.parseIf());
            continue;
        }
        if (lexer.token() == Token.GOTO) {
            lexer.nextToken();
            SQLName label = this.exprParser.name();
            OracleGotoStatement stmt = new OracleGotoStatement(label);
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.COMMIT) {
            lexer.nextToken();
            if (identifierEquals("WORK")) {
                lexer.nextToken();
            }
            OracleCommitStatement stmt = new OracleCommitStatement();
            if (identifierEquals("WRITE")) {
                stmt.setWrite(true);
                lexer.nextToken();
                for (; ; ) {
                    if (lexer.token() == Token.WAIT) {
                        lexer.nextToken();
                        stmt.setWait(Boolean.TRUE);
                        continue;
                    } else if (lexer.token() == Token.NOWAIT) {
                        lexer.nextToken();
                        stmt.setWait(Boolean.FALSE);
                        continue;
                    } else if (lexer.token() == Token.IMMEDIATE) {
                        lexer.nextToken();
                        stmt.setImmediate(Boolean.TRUE);
                        continue;
                    } else if (identifierEquals("BATCH")) {
                        lexer.nextToken();
                        stmt.setImmediate(Boolean.FALSE);
                        continue;
                    }
                    break;
                }
            }
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.SAVEPOINT) {
            lexer.nextToken();
            OracleSavePointStatement stmt = new OracleSavePointStatement();
            if (lexer.token() == Token.TO) {
                lexer.nextToken();
                stmt.setTo(this.exprParser.name());
            }
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.LTLT) {
            lexer.nextToken();
            SQLName label = this.exprParser.name();
            OracleLabelStatement stmt = new OracleLabelStatement(label);
            accept(Token.GTGT);
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.DROP) {
            lexer.nextToken();
            if (lexer.token() == Token.TABLE) {
                SQLDropTableStatement stmt = parseDropTable(false);
                statementList.add(stmt);
                continue;
            }
            boolean isPublic = false;
            if (identifierEquals("PUBLIC")) {
                lexer.nextToken();
                isPublic = true;
            }
            if (lexer.token() == Token.DATABASE) {
                lexer.nextToken();
                if (identifierEquals("LINK")) {
                    lexer.nextToken();
                    OracleDropDbLinkStatement stmt = new OracleDropDbLinkStatement();
                    if (isPublic) {
                        stmt.setPublic(isPublic);
                    }
                    stmt.setName(this.exprParser.name());
                    statementList.add(stmt);
                    continue;
                }
            }
            if (lexer.token() == Token.INDEX) {
                SQLStatement stmt = parseDropIndex();
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.VIEW) {
                SQLStatement stmt = parseDropView(false);
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.SEQUENCE) {
                SQLDropSequenceStatement stmt = parseDropSequece(false);
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.TRIGGER) {
                SQLDropTriggerStatement stmt = parseDropTrigger(false);
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.USER) {
                SQLDropUserStatement stmt = parseDropUser();
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.PROCEDURE) {
                SQLDropProcedureStatement stmt = parseDropProcedure(false);
                statementList.add(stmt);
                continue;
            }
            throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
        }
        if (lexer.token() == Token.NULL) {
            lexer.nextToken();
            OracleExprStatement stmt = new OracleExprStatement(new SQLNullExpr());
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.OPEN) {
            SQLStatement stmt = this.parseOpen();
            statementList.add(stmt);
            continue;
        }
        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
    }
}
Also used : OracleExitStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleExitStatement) OracleDropDbLinkStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDropDbLinkStatement) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleCommitStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCommitStatement) OracleSavePointStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSavePointStatement) OracleExprStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleExprStatement) OracleLabelStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleLabelStatement) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) OraclePLSQLCommitStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OraclePLSQLCommitStatement) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLHint(com.alibaba.druid.sql.ast.SQLHint) OracleConstraint(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint) OracleGotoStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleGotoStatement)

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