Search in sources :

Example 1 with SQLNotExpr

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

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

the class MySqlSelectTest_16 method test_0.

public void test_0() throws Exception {
    String sql = "select a from t where not a>1 and not b<1";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    Assert.assertNull(queryBlock.getOrderBy());
    {
        SQLExpr where = queryBlock.getWhere();
        Assert.assertTrue(where instanceof SQLBinaryOpExpr);
        SQLBinaryOpExpr binaryWhere = (SQLBinaryOpExpr) where;
        Assert.assertEquals(binaryWhere.getOperator(), SQLBinaryOperator.BooleanAnd);
        Assert.assertTrue(binaryWhere.getLeft() instanceof SQLNotExpr);
        Assert.assertTrue(binaryWhere.getRight() instanceof SQLNotExpr);
    }
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(2, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "SELECT a" + //
    "\nFROM t" + //
    "\nWHERE NOT a > 1" + //
    "\n\tAND NOT b < 1", output);
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLNotExpr(com.alibaba.druid.sql.ast.expr.SQLNotExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 3 with SQLNotExpr

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

the class EqualTest_not method test_exits.

public void test_exits() throws Exception {
    String sql = "not f1 = f2";
    String sql_c = "not f1 = f3";
    SQLNotExpr exprA, exprB, exprC;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprA = (SQLNotExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprB = (SQLNotExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql_c);
        exprC = (SQLNotExpr) 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 SQLNotExpr(), new SQLNotExpr());
    Assert.assertEquals(new SQLNotExpr().hashCode(), new SQLNotExpr().hashCode());
}
Also used : OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser) SQLNotExpr(com.alibaba.druid.sql.ast.expr.SQLNotExpr)

Example 4 with SQLNotExpr

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

the class EqualTest_not_2 method test_exits.

public void test_exits() throws Exception {
    String sql = "NOT A=1 AND NOT B=1";
    SQLNotExpr exprA, exprB;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        SQLBinaryOpExpr binaryEpr = (SQLBinaryOpExpr) parser.expr();
        exprA = (SQLNotExpr) binaryEpr.getLeft();
        exprB = (SQLNotExpr) binaryEpr.getRight();
    }
    Assert.assertNotNull(exprA);
    Assert.assertNotNull(exprB);
}
Also used : OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser) SQLNotExpr(com.alibaba.druid.sql.ast.expr.SQLNotExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Aggregations

SQLNotExpr (com.alibaba.druid.sql.ast.expr.SQLNotExpr)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 OracleExprParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLBetweenExpr (com.alibaba.druid.sql.ast.expr.SQLBetweenExpr)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 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)1 SQLUnaryExpr (com.alibaba.druid.sql.ast.expr.SQLUnaryExpr)1 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1