Search in sources :

Example 56 with SQLExpr

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

the class OracleStatementParser method parserAlter.

public SQLStatement parserAlter() {
    accept(Token.ALTER);
    if (lexer.token() == Token.SESSION) {
        lexer.nextToken();
        OracleAlterSessionStatement stmt = new OracleAlterSessionStatement();
        if (lexer.token() == Token.SET) {
            lexer.nextToken();
            parseAssignItems(stmt.getItems(), stmt);
        } else {
            throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
        }
        return stmt;
    } else if (lexer.token() == Token.PROCEDURE) {
        lexer.nextToken();
        OracleAlterProcedureStatement stmt = new OracleAlterProcedureStatement();
        stmt.setName(this.exprParser.name());
        if (identifierEquals("COMPILE")) {
            lexer.nextToken();
            stmt.setCompile(true);
        }
        if (identifierEquals("REUSE")) {
            lexer.nextToken();
            acceptIdentifier("SETTINGS");
            stmt.setReuseSettings(true);
        }
        return stmt;
    } else if (lexer.token() == Token.TABLE) {
        return parseAlterTable();
    } else if (lexer.token() == Token.INDEX) {
        lexer.nextToken();
        OracleAlterIndexStatement stmt = new OracleAlterIndexStatement();
        stmt.setName(this.exprParser.name());
        if (identifierEquals("RENAME")) {
            lexer.nextToken();
            accept(Token.TO);
            stmt.setRenameTo(this.exprParser.name());
        }
        for (; ; ) {
            if (identifierEquals("rebuild")) {
                lexer.nextToken();
                OracleAlterIndexStatement.Rebuild rebuild = new OracleAlterIndexStatement.Rebuild();
                stmt.setRebuild(rebuild);
                continue;
            } else if (identifierEquals("MONITORING")) {
                lexer.nextToken();
                acceptIdentifier("USAGE");
                stmt.setMonitoringUsage(Boolean.TRUE);
                continue;
            } else if (identifierEquals("PARALLEL")) {
                lexer.nextToken();
                stmt.setParallel(this.exprParser.expr());
            }
            break;
        }
        return stmt;
    } else if (lexer.token() == Token.TRIGGER) {
        lexer.nextToken();
        OracleAlterTriggerStatement stmt = new OracleAlterTriggerStatement();
        stmt.setName(this.exprParser.name());
        for (; ; ) {
            if (lexer.token() == Token.ENABLE) {
                lexer.nextToken();
                stmt.setEnable(Boolean.TRUE);
                continue;
            } else if (lexer.token() == Token.DISABLE) {
                lexer.nextToken();
                stmt.setEnable(Boolean.FALSE);
                continue;
            } else if (identifierEquals("COMPILE")) {
                lexer.nextToken();
                stmt.setCompile(true);
                continue;
            }
            break;
        }
        return stmt;
    } else if (identifierEquals("SYNONYM")) {
        lexer.nextToken();
        OracleAlterSynonymStatement stmt = new OracleAlterSynonymStatement();
        stmt.setName(this.exprParser.name());
        for (; ; ) {
            if (lexer.token() == Token.ENABLE) {
                lexer.nextToken();
                stmt.setEnable(Boolean.TRUE);
                continue;
            } else if (lexer.token() == Token.DISABLE) {
                lexer.nextToken();
                stmt.setEnable(Boolean.FALSE);
                continue;
            } else if (identifierEquals("COMPILE")) {
                lexer.nextToken();
                stmt.setCompile(true);
                continue;
            }
            break;
        }
        return stmt;
    } else if (lexer.token() == Token.VIEW) {
        lexer.nextToken();
        OracleAlterViewStatement stmt = new OracleAlterViewStatement();
        stmt.setName(this.exprParser.name());
        for (; ; ) {
            if (lexer.token() == Token.ENABLE) {
                lexer.nextToken();
                stmt.setEnable(Boolean.TRUE);
                continue;
            } else if (lexer.token() == Token.DISABLE) {
                lexer.nextToken();
                stmt.setEnable(Boolean.FALSE);
                continue;
            } else if (identifierEquals("COMPILE")) {
                lexer.nextToken();
                stmt.setCompile(true);
                continue;
            }
            break;
        }
        return stmt;
    } else if (lexer.token() == Token.TABLESPACE) {
        lexer.nextToken();
        OracleAlterTablespaceStatement stmt = new OracleAlterTablespaceStatement();
        stmt.setName(this.exprParser.name());
        if (identifierEquals("ADD")) {
            lexer.nextToken();
            if (identifierEquals("DATAFILE")) {
                lexer.nextToken();
                OracleAlterTablespaceAddDataFile item = new OracleAlterTablespaceAddDataFile();
                for (; ; ) {
                    OracleFileSpecification file = new OracleFileSpecification();
                    for (; ; ) {
                        SQLExpr fileName = this.exprParser.expr();
                        file.getFileNames().add(fileName);
                        if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                        break;
                    }
                    if (identifierEquals("SIZE")) {
                        lexer.nextToken();
                        file.setSize(this.exprParser.expr());
                    }
                    if (identifierEquals("AUTOEXTEND")) {
                        lexer.nextToken();
                        if (identifierEquals("OFF")) {
                            lexer.nextToken();
                            file.setAutoExtendOff(true);
                        } else if (identifierEquals("ON")) {
                            lexer.nextToken();
                            file.setAutoExtendOn(this.exprParser.expr());
                        } else {
                            throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
                        }
                    }
                    item.getFiles().add(file);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    }
                    break;
                }
                stmt.setItem(item);
            } else {
                throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
            }
        } else {
            throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
        }
        return stmt;
    }
    throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OracleAlterProcedureStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterProcedureStatement) OracleAlterTablespaceAddDataFile(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterTablespaceAddDataFile) OracleAlterSessionStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterSessionStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) OracleAlterSynonymStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterSynonymStatement) OracleAlterIndexStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterIndexStatement) OracleAlterTriggerStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterTriggerStatement) OracleAlterTablespaceStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterTablespaceStatement) OracleAlterViewStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterViewStatement) OracleFileSpecification(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleFileSpecification)

Example 57 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr 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)

Example 58 with SQLExpr

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

the class OracleExprParser method exprRest.

public SQLExpr exprRest(SQLExpr expr) {
    expr = super.exprRest(expr);
    if (lexer.token() == Token.COLONEQ) {
        lexer.nextToken();
        SQLExpr right = expr();
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Assignment, right, getDbType());
    }
    return expr;
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 59 with SQLExpr

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

the class OracleExprParser method primary.

public SQLExpr primary() {
    final Token tok = lexer.token();
    SQLExpr sqlExpr = null;
    switch(tok) {
        case SYSDATE:
            lexer.nextToken();
            OracleSysdateExpr sysdate = new OracleSysdateExpr();
            if (lexer.token() == Token.MONKEYS_AT) {
                lexer.nextToken();
                accept(Token.BANG);
                sysdate.setOption("!");
            }
            sqlExpr = sysdate;
            return primaryRest(sqlExpr);
        case PRIOR:
            lexer.nextToken();
            sqlExpr = expr();
            sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Prior, sqlExpr);
            return primaryRest(sqlExpr);
        case COLON:
            lexer.nextToken();
            if (lexer.token() == Token.LITERAL_INT) {
                String name = ":" + lexer.numberString();
                lexer.nextToken();
                return new SQLVariantRefExpr(name);
            } else if (lexer.token() == Token.IDENTIFIER) {
                String name = lexer.stringVal();
                if (name.charAt(0) == 'B' || name.charAt(0) == 'b') {
                    lexer.nextToken();
                    return new SQLVariantRefExpr(":" + name);
                }
                throw new ParserException("syntax error : " + lexer.token() + " " + lexer.stringVal());
            } else {
                throw new ParserException("syntax error : " + lexer.token());
            }
        case LITERAL_ALIAS:
            String alias = '"' + lexer.stringVal() + '"';
            lexer.nextToken();
            return primaryRest(new SQLIdentifierExpr(alias));
        case BINARY_FLOAT:
            OracleBinaryFloatExpr floatExpr = new OracleBinaryFloatExpr();
            floatExpr.setValue(Float.parseFloat(lexer.numberString()));
            lexer.nextToken();
            return primaryRest(floatExpr);
        case BINARY_DOUBLE:
            OracleBinaryDoubleExpr doubleExpr = new OracleBinaryDoubleExpr();
            doubleExpr.setValue(Double.parseDouble(lexer.numberString()));
            lexer.nextToken();
            return primaryRest(doubleExpr);
        case TABLE:
            lexer.nextToken();
            return primaryRest(new SQLIdentifierExpr("TABLE"));
        case PLUS:
            lexer.nextToken();
            switch(lexer.token()) {
                case LITERAL_INT:
                    sqlExpr = new SQLIntegerExpr(lexer.integerValue());
                    lexer.nextToken();
                    break;
                case LITERAL_FLOAT:
                    sqlExpr = new SQLNumberExpr(lexer.decimalValue());
                    lexer.nextToken();
                    break;
                case BINARY_FLOAT:
                    sqlExpr = new OracleBinaryFloatExpr(Float.parseFloat(lexer.numberString()));
                    lexer.nextToken();
                    break;
                case BINARY_DOUBLE:
                    sqlExpr = new OracleBinaryDoubleExpr(Double.parseDouble(lexer.numberString()));
                    lexer.nextToken();
                    break;
                case LPAREN:
                    lexer.nextToken();
                    sqlExpr = expr();
                    accept(Token.RPAREN);
                    sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Plus, sqlExpr);
                    break;
                default:
                    throw new ParserException("TODO");
            }
            return primaryRest(sqlExpr);
        case SUB:
            lexer.nextToken();
            switch(lexer.token()) {
                case LITERAL_INT:
                    Number integerValue = lexer.integerValue();
                    if (integerValue instanceof Integer) {
                        int intVal = ((Integer) integerValue).intValue();
                        if (intVal == Integer.MIN_VALUE) {
                            integerValue = Long.valueOf(((long) intVal) * -1);
                        } else {
                            integerValue = Integer.valueOf(intVal * -1);
                        }
                    } else if (integerValue instanceof Long) {
                        long longVal = ((Long) integerValue).longValue();
                        if (longVal == 2147483648L) {
                            integerValue = Integer.valueOf((int) (((long) longVal) * -1));
                        } else {
                            integerValue = Long.valueOf(longVal * -1);
                        }
                    } else {
                        integerValue = ((BigInteger) integerValue).negate();
                    }
                    sqlExpr = new SQLIntegerExpr(integerValue);
                    lexer.nextToken();
                    break;
                case LITERAL_FLOAT:
                    sqlExpr = new SQLNumberExpr(lexer.decimalValue().negate());
                    lexer.nextToken();
                    break;
                case BINARY_FLOAT:
                    sqlExpr = new OracleBinaryFloatExpr(Float.parseFloat(lexer.numberString()) * -1);
                    lexer.nextToken();
                    break;
                case BINARY_DOUBLE:
                    sqlExpr = new OracleBinaryDoubleExpr(Double.parseDouble(lexer.numberString()) * -1);
                    lexer.nextToken();
                    break;
                case VARIANT:
                case IDENTIFIER:
                    sqlExpr = expr();
                    sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
                    break;
                case LPAREN:
                    lexer.nextToken();
                    sqlExpr = expr();
                    accept(Token.RPAREN);
                    sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
                    break;
                default:
                    throw new ParserException("TODO " + lexer.token());
            }
            return primaryRest(sqlExpr);
        case CURSOR:
            lexer.nextToken();
            accept(Token.LPAREN);
            OracleSelect select = createSelectParser().select();
            OracleCursorExpr cursorExpr = new OracleCursorExpr(select);
            accept(Token.RPAREN);
            sqlExpr = cursorExpr;
            return primaryRest(sqlExpr);
        case MODEL:
        case PCTFREE:
        case INITRANS:
        case MAXTRANS:
        case SEGMENT:
        case CREATION:
        case IMMEDIATE:
        case DEFERRED:
        case STORAGE:
        case NEXT:
        case MINEXTENTS:
        case MAXEXTENTS:
        case MAXSIZE:
        case PCTINCREASE:
        case FLASH_CACHE:
        case CELL_FLASH_CACHE:
        case KEEP:
        case NONE:
        case LOB:
        case STORE:
        case ROW:
        case CHUNK:
        case CACHE:
        case NOCACHE:
        case LOGGING:
        case NOCOMPRESS:
        case KEEP_DUPLICATES:
        case EXCEPTIONS:
        case PURGE:
            sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
            lexer.nextToken();
            return primaryRest(sqlExpr);
        default:
            return super.primary();
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OracleSysdateExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSysdateExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) OracleBinaryFloatExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleBinaryFloatExpr) Token(com.alibaba.druid.sql.parser.Token) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLForeignKeyConstraint(com.alibaba.druid.sql.ast.statement.SQLForeignKeyConstraint) OracleConstraint(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint) BigInteger(java.math.BigInteger) OracleCursorExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleCursorExpr) OracleSelect(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) BigInteger(java.math.BigInteger) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) OracleBinaryDoubleExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleBinaryDoubleExpr)

Example 60 with SQLExpr

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

the class OracleExprParser method relationalRest.

public SQLExpr relationalRest(SQLExpr expr) {
    if (lexer.token() == Token.IS) {
        lexer.nextToken();
        if (lexer.token() == Token.NOT) {
            lexer.nextToken();
            SQLExpr rightExpr = primary();
            expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.IsNot, rightExpr, getDbType());
        } else if (identifierEquals("A")) {
            lexer.nextToken();
            accept(Token.SET);
            expr = new OracleIsSetExpr(expr);
        } else {
            SQLExpr rightExpr = primary();
            expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Is, rightExpr, getDbType());
        }
        return expr;
    }
    return super.relationalRest(expr);
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) OracleIsSetExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIsSetExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)225 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)42 SQLName (com.alibaba.druid.sql.ast.SQLName)33 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)30 ParserException (com.alibaba.druid.sql.parser.ParserException)23 SQLObject (com.alibaba.druid.sql.ast.SQLObject)22 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)17 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)13 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)13 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)12 ArrayList (java.util.ArrayList)12 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)10 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)10 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)8 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)8 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)7 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)7