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;
}
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;
}
}
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;
}
}
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());
}
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());
}
Aggregations