Search in sources :

Example 1 with SQLUnaryExpr

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

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

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

Example 4 with SQLUnaryExpr

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

the class WallVisitorUtils method getValue.

public static Object getValue(WallVisitor visitor, SQLExpr x) {
    if (x != null && x.getAttributes().containsKey(EVAL_VALUE)) {
        return getValueFromAttributes(visitor, x);
    }
    if (x instanceof SQLBinaryOpExpr) {
        return getValue(visitor, (SQLBinaryOpExpr) x);
    }
    if (x instanceof SQLBooleanExpr) {
        return ((SQLBooleanExpr) x).getValue();
    }
    if (x instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) x).getNumber();
    }
    if (x instanceof SQLCharExpr) {
        return ((SQLCharExpr) x).getText();
    }
    if (x instanceof SQLNCharExpr) {
        return ((SQLNCharExpr) x).getText();
    }
    if (x instanceof SQLNotExpr) {
        Object result = getValue(visitor, ((SQLNotExpr) x).getExpr());
        if (result instanceof Boolean) {
            return !((Boolean) result).booleanValue();
        }
    }
    if (x instanceof SQLQueryExpr) {
        if (isSimpleCountTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
            return Integer.valueOf(1);
        }
        if (isSimpleCaseTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
            SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) ((SQLQueryExpr) x).getSubQuery().getQuery();
            SQLCaseExpr caseExpr = (SQLCaseExpr) queryBlock.getSelectList().get(0).getExpr();
            Object result = getValue(caseExpr);
            if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
                boolean leftIsName = false;
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    SQLExpr left = ((SQLBinaryOpExpr) x.getParent()).getLeft();
                    if (left instanceof SQLName) {
                        leftIsName = true;
                    }
                }
                if (!leftIsName && result != null) {
                    addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
                }
            }
            return result;
        }
    }
    String dbType = null;
    if (visitor != null) {
        dbType = visitor.getDbType();
    }
    if (//
    x instanceof SQLMethodInvokeExpr || //
    x instanceof SQLBetweenExpr || //
    x instanceof SQLInListExpr || //
    x instanceof SQLUnaryExpr) {
        return eval(visitor, dbType, x, Collections.emptyList());
    }
    if (x instanceof SQLCaseExpr) {
        if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
            SQLCaseExpr caseExpr = (SQLCaseExpr) x;
            boolean leftIsName = false;
            if (caseExpr.getParent() instanceof SQLBinaryOpExpr) {
                SQLExpr left = ((SQLBinaryOpExpr) caseExpr.getParent()).getLeft();
                if (left instanceof SQLName) {
                    leftIsName = true;
                }
            }
            if (!leftIsName && caseExpr.getValueExpr() == null && caseExpr.getItems().size() > 0) {
                SQLCaseExpr.Item item = caseExpr.getItems().get(0);
                Object conditionVal = getValue(visitor, item.getConditionExpr());
                Object itemVal = getValue(visitor, item.getValueExpr());
                if (conditionVal instanceof Boolean && itemVal != null) {
                    addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
                }
            }
        }
        return eval(visitor, dbType, x, Collections.emptyList());
    }
    return null;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) Item(com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLNotExpr(com.alibaba.druid.sql.ast.expr.SQLNotExpr) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLCaseExpr(com.alibaba.druid.sql.ast.expr.SQLCaseExpr)

Example 5 with SQLUnaryExpr

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

the class EqualTest_unary method test_exits.

public void test_exits() throws Exception {
    String sql = "-a";
    String sql_c = "-(a+1 + +(b+1))";
    SQLUnaryExpr exprA, exprB, exprC;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprA = (SQLUnaryExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprB = (SQLUnaryExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql_c);
        exprC = (SQLUnaryExpr) parser.expr();
    }
    Assert.assertEquals(exprA, exprB);
    Assert.assertNotEquals(exprA, exprC);
    Assert.assertTrue(exprA.equals(exprA));
    Assert.assertFalse(exprA.equals(new Object()));
    Assert.assertEquals(exprA.hashCode(), exprB.hashCode());
    Assert.assertEquals(new SQLUnaryExpr(), new SQLUnaryExpr());
    Assert.assertEquals(new SQLUnaryExpr().hashCode(), new SQLUnaryExpr().hashCode());
}
Also used : OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr)

Aggregations

SQLUnaryExpr (com.alibaba.druid.sql.ast.expr.SQLUnaryExpr)7 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 Token (com.alibaba.druid.sql.parser.Token)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 SQLArrayExpr (com.alibaba.druid.sql.ast.expr.SQLArrayExpr)1 SQLBetweenExpr (com.alibaba.druid.sql.ast.expr.SQLBetweenExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLCaseExpr (com.alibaba.druid.sql.ast.expr.SQLCaseExpr)1 Item (com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)1 SQLNotExpr (com.alibaba.druid.sql.ast.expr.SQLNotExpr)1