Search in sources :

Example 76 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class ExportTables method evaluate.

public String evaluate(String sql, String dbTypeName, boolean throwError) throws Throwable {
    DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
    Throwable error = null;
    try {
        List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        for (SQLStatement stmt : statementList) {
            stmt.accept(visitor);
        }
        StringBuffer buf = new StringBuffer();
        for (Map.Entry<TableStat.Name, TableStat> entry : visitor.getTables().entrySet()) {
            TableStat.Name name = entry.getKey();
            if (buf.length() != 0) {
                buf.append(',');
            }
            buf.append(name.toString());
        }
        return buf.toString();
    } catch (Exception ignored) {
    // skip
    } catch (StackOverflowError ignored) {
    // skip
    }
    if (throwError && error != null) {
        throw error;
    }
    return null;
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) Map(java.util.Map) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 77 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SqlCodeStat method evaluate.

public String evaluate(String sql, String dbTypeName, boolean throwError) {
    DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
    try {
        List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, DbType.odps, SQLParserFeature.EnableMultiUnion, SQLParserFeature.EnableSQLBinaryOpExprGroup);
        CodeStatVisitor v = new CodeStatVisitor();
        for (SQLStatement stmt : stmtList) {
            stmt.accept(v);
        }
        return v.toString();
    } catch (Exception ex) {
        if (throwError) {
            throw new IllegalArgumentException("error sql : \n" + sql, ex);
        }
        return null;
    }
}
Also used : DbType(com.alibaba.druid.DbType)

Example 78 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SqlParamsFill method evaluate.

public String evaluate(String sql, String params, String dbTypeName, boolean throwError) {
    try {
        DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
        List<Object> inputParams = (List<Object>) JSONUtils.parse(params);
        return ParameterizedOutputVisitorUtils.restore(sql, dbType, inputParams);
    } catch (ParserException ex) {
        if (throwError) {
            throw new IllegalArgumentException("error sql : \n" + sql, ex);
        }
        return null;
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) List(java.util.List) DbType(com.alibaba.druid.DbType)

Example 79 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class WallVisitorUtils method getValue.

public static Object getValue(WallVisitor visitor, SQLBinaryOpExpr x) {
    if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
        List<SQLExpr> groupList = SQLBinaryOpExpr.split(x);
        return getValue_or(visitor, groupList);
    }
    if (x.getOperator() == SQLBinaryOperator.BooleanAnd) {
        List<SQLExpr> groupList = SQLBinaryOpExpr.split(x);
        return getValue_and(visitor, groupList);
    }
    boolean checkCondition = visitor != null && (!visitor.getConfig().isConstArithmeticAllow() || !visitor.getConfig().isConditionOpBitwseAllow() || !visitor.getConfig().isConditionOpXorAllow());
    if (x.getLeft() instanceof SQLName) {
        if (x.getRight() instanceof SQLName) {
            if (x.getLeft().toString().equalsIgnoreCase(x.getRight().toString())) {
                switch(x.getOperator()) {
                    case Equality:
                    case Like:
                        return Boolean.TRUE;
                    case NotEqual:
                    case GreaterThan:
                    case GreaterThanOrEqual:
                    case LessThan:
                    case LessThanOrEqual:
                    case LessThanOrGreater:
                    case NotLike:
                        return Boolean.FALSE;
                    default:
                        break;
                }
            }
        } else if (!checkCondition) {
            switch(x.getOperator()) {
                case Equality:
                case NotEqual:
                case GreaterThan:
                case GreaterThanOrEqual:
                case LessThan:
                case LessThanOrEqual:
                case LessThanOrGreater:
                    return null;
                default:
                    break;
            }
        }
    }
    if (x.getLeft() instanceof SQLValuableExpr && x.getRight() instanceof SQLValuableExpr) {
        Object leftValue = ((SQLValuableExpr) x.getLeft()).getValue();
        Object rightValue = ((SQLValuableExpr) x.getRight()).getValue();
        if (x.getOperator() == SQLBinaryOperator.Equality) {
            boolean evalValue = SQLEvalVisitorUtils.eq(leftValue, rightValue);
            x.putAttribute(EVAL_VALUE, evalValue);
            return evalValue;
        } else if (x.getOperator() == SQLBinaryOperator.NotEqual) {
            boolean evalValue = SQLEvalVisitorUtils.eq(leftValue, rightValue);
            x.putAttribute(EVAL_VALUE, !evalValue);
            return !evalValue;
        }
    }
    Object leftResult = getValue(visitor, x.getLeft());
    Object rightResult = getValue(visitor, x.getRight());
    if (x.getOperator() == SQLBinaryOperator.Like && leftResult instanceof String && leftResult.equals(rightResult)) {
        addViolation(visitor, ErrorCode.SAME_CONST_LIKE, "same const like", x);
    }
    if (x.getOperator() == SQLBinaryOperator.Like || x.getOperator() == SQLBinaryOperator.NotLike) {
        WallContext context = WallContext.current();
        if (context != null) {
            if (rightResult instanceof Number || leftResult instanceof Number) {
                context.incrementLikeNumberWarnings();
            }
        }
    }
    DbType dbType = null;
    WallContext wallContext = WallContext.current();
    if (wallContext != null) {
        dbType = wallContext.getDbType();
    }
    return eval(visitor, dbType, x, Collections.emptyList());
}
Also used : DbType(com.alibaba.druid.DbType)

Example 80 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class Issue1865 method test_for_select_group.

public void test_for_select_group() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select * from t where id = 2 and name = 'wenshao'";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, SQLParserFeature.EnableSQLBinaryOpExprGroup);
    SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatement();
    SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
    assertTrue(queryBlock.removeCondition("name = 'wenshao'"));
    assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = 2", stmt.toString());
    assertTrue(queryBlock.removeCondition("id = 2"));
    assertEquals("SELECT *\n" + "FROM t", stmt.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) DbType(com.alibaba.druid.DbType)

Aggregations

DbType (com.alibaba.druid.DbType)114 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)76 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)70 ArrayList (java.util.ArrayList)50 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)45 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)21 TableStat (com.alibaba.druid.stat.TableStat)10 JSONArray (com.alibaba.fastjson.JSONArray)10 MySqlExportParameterVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor)5 ExportParameterVisitor (com.alibaba.druid.sql.visitor.ExportParameterVisitor)5 ParserException (com.alibaba.druid.sql.parser.ParserException)4 Map (java.util.Map)4 JdbcParameter (com.alibaba.druid.proxy.jdbc.JdbcParameter)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)2 OracleSQLObject (com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject)2