Search in sources :

Example 6 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project druid by alibaba.

the class MySqlStatementParser method parseSet.

public SQLStatement parseSet() {
    accept(Token.SET);
    if (identifierEquals("PASSWORD")) {
        lexer.nextToken();
        MySqlSetPasswordStatement stmt = new MySqlSetPasswordStatement();
        if (lexer.token() == Token.FOR) {
            lexer.nextToken();
            stmt.setUser(this.exprParser.name());
        }
        accept(Token.EQ);
        stmt.setPassword(this.exprParser.expr());
        return stmt;
    }
    Boolean global = null;
    if (identifierEquals(GLOBAL)) {
        global = Boolean.TRUE;
        lexer.nextToken();
    } else if (identifierEquals(SESSION)) {
        global = Boolean.FALSE;
        lexer.nextToken();
    }
    if (identifierEquals("TRANSACTION")) {
        MySqlSetTransactionStatement stmt = new MySqlSetTransactionStatement();
        stmt.setGlobal(global);
        lexer.nextToken();
        if (identifierEquals("ISOLATION")) {
            lexer.nextToken();
            acceptIdentifier("LEVEL");
            if (identifierEquals(READ)) {
                lexer.nextToken();
                if (identifierEquals("UNCOMMITTED")) {
                    stmt.setIsolationLevel("READ UNCOMMITTED");
                    lexer.nextToken();
                } else if (identifierEquals(WRITE)) {
                    stmt.setIsolationLevel("READ WRITE");
                    lexer.nextToken();
                } else if (identifierEquals("ONLY")) {
                    stmt.setIsolationLevel("READ ONLY");
                    lexer.nextToken();
                } else if (identifierEquals("COMMITTED")) {
                    stmt.setIsolationLevel("READ COMMITTED");
                    lexer.nextToken();
                } else {
                    throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
                }
            } else if (identifierEquals("SERIALIZABLE")) {
                stmt.setIsolationLevel("SERIALIZABLE");
                lexer.nextToken();
            } else if (identifierEquals("REPEATABLE")) {
                lexer.nextToken();
                if (identifierEquals(READ)) {
                    stmt.setIsolationLevel("REPEATABLE READ");
                    lexer.nextToken();
                } else {
                    throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
                }
            } else {
                throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
            }
        } else if (identifierEquals(READ)) {
            lexer.nextToken();
            if (identifierEquals("ONLY")) {
                stmt.setAccessModel("ONLY");
                lexer.nextToken();
            } else if (identifierEquals("WRITE")) {
                stmt.setAccessModel("WRITE");
                lexer.nextToken();
            } else {
                throw new ParserException("UNKOWN ACCESS MODEL : " + lexer.stringVal());
            }
        }
        return stmt;
    } else if (identifierEquals("NAMES")) {
        lexer.nextToken();
        MySqlSetNamesStatement stmt = new MySqlSetNamesStatement();
        if (lexer.token() == Token.DEFAULT) {
            lexer.nextToken();
            stmt.setDefault(true);
        } else {
            String charSet = lexer.stringVal();
            stmt.setCharSet(charSet);
            lexer.nextToken();
            if (identifierEquals(COLLATE2)) {
                lexer.nextToken();
                String collate = lexer.stringVal();
                stmt.setCollate(collate);
                lexer.nextToken();
            }
        }
        return stmt;
    } else if (identifierEquals(CHARACTER)) {
        lexer.nextToken();
        accept(Token.SET);
        MySqlSetCharSetStatement stmt = new MySqlSetCharSetStatement();
        if (lexer.token() == Token.DEFAULT) {
            lexer.nextToken();
            stmt.setDefault(true);
        } else {
            String charSet = lexer.stringVal();
            stmt.setCharSet(charSet);
            lexer.nextToken();
            if (identifierEquals(COLLATE2)) {
                lexer.nextToken();
                String collate = lexer.stringVal();
                stmt.setCollate(collate);
                lexer.nextToken();
            }
        }
        return stmt;
    } else {
        SQLSetStatement stmt = new SQLSetStatement(getDbType());
        parseAssignItems(stmt.getItems(), stmt);
        if (global != null && global.booleanValue()) {
            SQLVariantRefExpr varRef = (SQLVariantRefExpr) stmt.getItems().get(0).getTarget();
            varRef.setGlobal(true);
        }
        if (lexer.token() == Token.HINT) {
            stmt.setHints(this.exprParser.parseHints());
        }
        return stmt;
    }
}
Also used : MySqlSetNamesStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement) MySqlSetTransactionStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionStatement) ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlSetCharSetStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) MySqlSetPasswordStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetPasswordStatement)

Example 7 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project druid by alibaba.

the class MySqlExprParser method parseAssignItem.

public SQLAssignItem parseAssignItem() {
    SQLAssignItem item = new SQLAssignItem();
    SQLExpr var = primary();
    String ident = null;
    if (var instanceof SQLIdentifierExpr) {
        ident = ((SQLIdentifierExpr) var).getName();
        if ("GLOBAL".equalsIgnoreCase(ident)) {
            ident = lexer.stringVal();
            lexer.nextToken();
            var = new SQLVariantRefExpr(ident, true);
        } else if ("SESSION".equalsIgnoreCase(ident)) {
            ident = lexer.stringVal();
            lexer.nextToken();
            var = new SQLVariantRefExpr(ident, false);
        } else {
            var = new SQLVariantRefExpr(ident);
        }
    }
    if ("NAMES".equalsIgnoreCase(ident)) {
    // skip
    } else if ("CHARACTER".equalsIgnoreCase(ident)) {
        var = new SQLIdentifierExpr("CHARACTER SET");
        accept(Token.SET);
        if (lexer.token() == Token.EQ) {
            lexer.nextToken();
        }
    } else {
        if (lexer.token() == Token.COLONEQ) {
            lexer.nextToken();
        } else {
            accept(Token.EQ);
        }
    }
    if (lexer.token() == Token.ON) {
        lexer.nextToken();
        item.setValue(new SQLIdentifierExpr("ON"));
    } else {
        item.setValue(this.expr());
    }
    item.setTarget(var);
    return item;
}
Also used : SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 8 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project druid by alibaba.

the class MySqlExprParser method primary.

public SQLExpr primary() {
    final Token tok = lexer.token();
    if (identifierEquals("outfile")) {
        lexer.nextToken();
        SQLExpr file = primary();
        SQLExpr expr = new MySqlOutFileExpr(file);
        return primaryRest(expr);
    }
    switch(tok) {
        case LITERAL_ALIAS:
            String aliasValue = lexer.stringVal();
            lexer.nextToken();
            return primaryRest(new SQLCharExpr(aliasValue));
        case VARIANT:
            SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(lexer.stringVal());
            lexer.nextToken();
            if (varRefExpr.getName().equalsIgnoreCase("@@global")) {
                accept(Token.DOT);
                varRefExpr = new SQLVariantRefExpr(lexer.stringVal(), true);
                lexer.nextToken();
            } else if (varRefExpr.getName().equals("@") && lexer.token() == Token.LITERAL_CHARS) {
                varRefExpr.setName("@'" + lexer.stringVal() + "'");
                lexer.nextToken();
            } else if (varRefExpr.getName().equals("@@") && lexer.token() == Token.LITERAL_CHARS) {
                varRefExpr.setName("@@'" + lexer.stringVal() + "'");
                lexer.nextToken();
            }
            return primaryRest(varRefExpr);
        case VALUES:
            lexer.nextToken();
            if (lexer.token() != Token.LPAREN) {
                throw new ParserException("syntax error, illegal values clause");
            }
            return this.methodRest(new SQLIdentifierExpr("VALUES"), true);
        case BINARY:
            lexer.nextToken();
            if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI || lexer.token() == Token.EOF) {
                return new SQLIdentifierExpr("BINARY");
            } else {
                SQLUnaryExpr binaryExpr = new SQLUnaryExpr(SQLUnaryOperator.BINARY, expr());
                return primaryRest(binaryExpr);
            }
        case CACHE:
        case GROUP:
            lexer.nextToken();
            return primaryRest(new SQLIdentifierExpr(lexer.stringVal()));
        default:
            return super.primary();
    }
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) Token(com.alibaba.druid.sql.parser.Token) MySqlOutFileExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 9 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr 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 10 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project druid by alibaba.

the class PGExprParser method primary.

public SQLExpr primary() {
    if (lexer.token() == Token.ARRAY) {
        SQLArrayExpr array = new SQLArrayExpr();
        array.setExpr(new SQLIdentifierExpr(lexer.stringVal()));
        lexer.nextToken();
        accept(Token.LBRACKET);
        this.exprList(array.getValues(), array);
        accept(Token.RBRACKET);
        return primaryRest(array);
    } else if (lexer.token() == Token.POUND) {
        lexer.nextToken();
        if (lexer.token() == Token.LBRACE) {
            lexer.nextToken();
            String varName = lexer.stringVal();
            lexer.nextToken();
            accept(Token.RBRACE);
            SQLVariantRefExpr expr = new SQLVariantRefExpr("#{" + varName + "}");
            return primaryRest(expr);
        } else {
            SQLExpr value = this.primary();
            SQLUnaryExpr expr = new SQLUnaryExpr(SQLUnaryOperator.Pound, value);
            return primaryRest(expr);
        }
    }
    return super.primary();
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLArrayExpr(com.alibaba.druid.sql.ast.expr.SQLArrayExpr)

Aggregations

SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)22 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)11 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)9 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 Test (org.junit.Test)5 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 ArrayList (java.util.ArrayList)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLUnaryExpr (com.alibaba.druid.sql.ast.expr.SQLUnaryExpr)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)2 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)2 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)2 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)2 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)2