use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_41 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "select * from db_00.t_00_00 where id > 1";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement statement = stmtList.get(0);
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
List<Object> parameters = new ArrayList<Object>();
visitor.setParameterized(true);
visitor.setParameterizedMergeInList(true);
visitor.setParameters(parameters);
/*visitor.setPrettyFormat(false);*/
statement.accept(visitor);
/* JSONArray array = new JSONArray();
for(String table : visitor.getTables()){
array.add(table.replaceAll("`",""));
}*/
String psql = out.toString();
assertEquals("SELECT *\n" + "FROM db.t\n" + "WHERE id > ?", psql);
String params_json = JSONArray.toJSONString(parameters, SerializerFeature.WriteClassName);
System.out.println(params_json);
JSONArray jsonArray = JSON.parseArray(params_json);
String json = JSONArray.toJSONString(jsonArray, SerializerFeature.WriteClassName);
assertEquals("[1]", json);
String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
assertEquals("SELECT *\n" + "FROM db.t\n" + "WHERE id > 1", rsql);
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class ParseUtil method restore.
public static String restore(String sql, String table, String params) /*JSONArray paramsArray, JSONArray destArray*/
{
JSONArray destArray = null;
if (table != null) {
destArray = JSON.parseArray(table.replaceAll("''", "'"));
}
JSONArray paramsArray = JSON.parseArray(params.replaceAll("''", "'"));
DbType dbType = JdbcConstants.MYSQL;
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
SQLStatement stmt = stmtList.get(0);
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
List<Object> paramsList = new ArrayList(paramsArray);
visitor.setParameters(paramsList);
JSONArray srcArray = getSrcArray(sql);
if (destArray != null) {
for (int i = 0; i < srcArray.size(); i++) {
visitor.addTableMapping(srcArray.getString(i), destArray.getString(i));
}
}
stmt.accept(visitor);
return out.toString();
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class ParseUtil method getSqlHash.
public static String getSqlHash(SQLStatement statement) {
try {
StringBuilder out = new StringBuilder();
List<Object> parameters = new ArrayList<Object>();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
visitor.setParameterized(true);
visitor.setParameterizedMergeInList(true);
visitor.setParameters(parameters);
// visitor.setExportTables(true);
visitor.setPrettyFormat(false);
statement.accept(visitor);
String sqlTemplate = out.toString();
return DigestUtils.md5Hex(sqlTemplate);
} catch (Exception ex) {
logger.error("parseSql error ", ex);
}
return null;
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class ParseUtil method getSrcArray.
private static JSONArray getSrcArray(String sql) {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
SQLStatement stmt = stmtList.get(0);
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
List<Object> parameters = new ArrayList<Object>();
visitor.setParameterized(true);
visitor.setParameterizedMergeInList(true);
visitor.setParameters(parameters);
visitor.setExportTables(true);
stmt.accept(visitor);
String srcStr = JSONArray.toJSONString(visitor.getTables());
return JSONArray.parseArray(srcStr);
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_45 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "(SELECT `t_like_count`.`id`, `t_like_count`.`target_id`, `t_like_count`.`target_type`, `t_like_count`.`like_type`, `t_like_count`.`like_count` , `t_like_count`.`like_optimalize_count`, `t_like_count`.`create_time`, `t_like_count`.`update_time` FROM `t_like_count0062` `t_like_count` WHERE `t_like_count`.`target_type` = ? AND `t_like_count`.`target_id` IN (?)) UNION ALL (SELECT `t_like_count`.`id`, `t_like_count`.`target_id`, `t_like_count`.`target_type`, `t_like_count`.`like_type`, `t_like_count`.`like_count` , `t_like_count`.`like_optimalize_count`, `t_like_count`.`create_time`, `t_like_count`.`update_time` FROM `t_like_count0057` `t_like_count` WHERE `t_like_count`.`target_type` = ? AND `t_like_count`.`target_id` IN (?)) UNION ALL (SELECT `t_like_count`.`id`, `t_like_count`.`target_id`, `t_like_count`.`target_type`, `t_like_count`.`like_type`, `t_like_count`.`like_count` , `t_like_count`.`like_optimalize_count`, `t_like_count`.`create_time`, `t_like_count`.`update_time` FROM `t_like_count0050` `t_like_count` WHERE `t_like_count`.`target_type` = ? AND `t_like_count`.`target_id` IN (?)) UNION ALL (SELECT `t_like_count`.`id`, `t_like_count`.`target_id`, `t_like_count`.`target_type`, `t_like_count`.`like_type`, `t_like_count`.`like_count` , `t_like_count`.`like_optimalize_count`, `t_like_count`.`create_time`, `t_like_count`.`update_time` FROM `t_like_count0048` `t_like_count` WHERE `t_like_count`.`target_type` = ? AND `t_like_count`.`target_id` IN (?))";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement statement = stmtList.get(0);
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
List<Object> parameters = new ArrayList<Object>();
visitor.setParameterized(true);
visitor.setParameterizedMergeInList(true);
visitor.setParameters(parameters);
/*visitor.setPrettyFormat(false);*/
statement.accept(visitor);
/* JSONArray array = new JSONArray();
for(String table : visitor.getTables()){
array.add(table.replaceAll("`",""));
}*/
String psql = out.toString();
System.out.println(psql);
assertEquals("(SELECT `t_like_count`.`id`, `t_like_count`.`target_id`, `t_like_count`.`target_type`, `t_like_count`.`like_type`, `t_like_count`.`like_count`\n" + "\t, `t_like_count`.`like_optimalize_count`, `t_like_count`.`create_time`, `t_like_count`.`update_time`\n" + "FROM t_like_count `t_like_count`\n" + "WHERE `t_like_count`.`target_type` = ?\n" + "\tAND `t_like_count`.`target_id` IN (?))\n" + "UNION ALL\n" + "(SELECT `t_like_count`.`id`, `t_like_count`.`target_id`, `t_like_count`.`target_type`, `t_like_count`.`like_type`, `t_like_count`.`like_count`\n" + "\t, `t_like_count`.`like_optimalize_count`, `t_like_count`.`create_time`, `t_like_count`.`update_time`\n" + "FROM t_like_count `t_like_count`\n" + "WHERE `t_like_count`.`target_type` = ?\n" + "\tAND `t_like_count`.`target_id` IN (?))\n" + "UNION ALL\n" + "(SELECT `t_like_count`.`id`, `t_like_count`.`target_id`, `t_like_count`.`target_type`, `t_like_count`.`like_type`, `t_like_count`.`like_count`\n" + "\t, `t_like_count`.`like_optimalize_count`, `t_like_count`.`create_time`, `t_like_count`.`update_time`\n" + "FROM t_like_count `t_like_count`\n" + "WHERE `t_like_count`.`target_type` = ?\n" + "\tAND `t_like_count`.`target_id` IN (?))\n" + "UNION ALL\n" + "(SELECT `t_like_count`.`id`, `t_like_count`.`target_id`, `t_like_count`.`target_type`, `t_like_count`.`like_type`, `t_like_count`.`like_count`\n" + "\t, `t_like_count`.`like_optimalize_count`, `t_like_count`.`create_time`, `t_like_count`.`update_time`\n" + "FROM t_like_count `t_like_count`\n" + "WHERE `t_like_count`.`target_type` = ?\n" + "\tAND `t_like_count`.`target_id` IN (?))", psql);
}
Aggregations