use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class ExportParameterShardingTest method restore.
public String restore(String sql, List<Object> parameters) {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
assertEquals(1, stmtList.size());
SQLStatement stmt = stmtList.get(0);
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
visitor.setInputParameters(parameters);
visitor.addTableMapping("t_user", "t_user_0000");
stmt.accept(visitor);
return out.toString();
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class ExportParameterInTest method restore.
public void restore(String sql, List<Object> parameters) {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
assertEquals(1, stmtList.size());
SQLStatement stmt = stmtList.get(0);
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
visitor.setParameters(parameters);
stmt.accept(visitor);
System.out.println(out);
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_19 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "SELECT COUNT(*), xx_id FROM `a_lb`.lb_release_source_aio GROUP BY xx_id";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
assertEquals("SELECT COUNT(*), xx_id FROM `a_lb`.lb_release_source_aio GROUP BY xx_id", psql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
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);
/*visitor.setPrettyFormat(false);*/
SQLStatement stmt = stmtList.get(0);
stmt.accept(visitor);
// System.out.println(parameters);
assertEquals(0, parameters.size());
StringBuilder buf = new StringBuilder();
SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
visitor1.addTableMapping("lb_release_source_aio", "lb_release_source_aio_0008");
visitor1.setParameters(visitor.getParameters());
stmt.accept(visitor1);
assertEquals("SELECT COUNT(*), xx_id\n" + "FROM `a_lb`.lb_release_source_aio_0008\n" + "GROUP BY xx_id", buf.toString());
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_32 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "/* cds internal mark */select count(*) as count from (" + "( select env_type from `tmall_miaoscm`.`miao_sale_ledger_0060` where `id` > 1 limit 136 )" + " union all ( select env_type from `tmall_miaoscm`.`miao_sale_ledger_0060` where `id` > 2331 limit 136 )" + ") as miao_sale_ledger_0060 where `miao_sale_ledger_0060`.`env_type` = 3";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
assertEquals("SELECT count(*) AS count\n" + "FROM (\n" + "\t(SELECT env_type\n" + "\tFROM `tmall_miaoscm`.miao_sale_ledger\n" + "\tWHERE `id` > ?\n" + "\tLIMIT ?)\n" + "\tUNION ALL\n" + "\t(SELECT env_type\n" + "\tFROM `tmall_miaoscm`.miao_sale_ledger\n" + "\tWHERE `id` > ?\n" + "\tLIMIT ?)\n" + ") miao_sale_ledger_0060\n" + "WHERE `miao_sale_ledger_0060`.`env_type` = ?", psql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
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);
/*visitor.setPrettyFormat(false);*/
SQLStatement stmt = stmtList.get(0);
stmt.accept(visitor);
// System.out.println(parameters);
assertEquals(5, parameters.size());
// SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, dbType);
// List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement pstmt = SQLUtils.parseStatements(psql, dbType).get(0);
StringBuilder buf = new StringBuilder();
SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
visitor1.addTableMapping("udata", "udata_0888");
visitor1.setInputParameters(visitor.getParameters());
pstmt.accept(visitor1);
assertEquals("SELECT count(*) AS count\n" + "FROM (\n" + "\t(SELECT env_type\n" + "\tFROM `tmall_miaoscm`.miao_sale_ledger\n" + "\tWHERE `id` > 1\n" + "\tLIMIT 136)\n" + "\tUNION ALL\n" + "\t(SELECT env_type\n" + "\tFROM `tmall_miaoscm`.miao_sale_ledger\n" + "\tWHERE `id` > 2331\n" + "\tLIMIT 136)\n" + ") miao_sale_ledger_0060\n" + "WHERE `miao_sale_ledger_0060`.`env_type` = 3", buf.toString());
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_34 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "select * from t where id = 1 or id = 2 or id = 3";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = ?", psql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
List<Object> parameters = new ArrayList<Object>();
visitor.setParameterized(true);
visitor.setParameterizedMergeInList(true);
visitor.setOutputParameters(parameters);
visitor.setExportTables(true);
/*visitor.setPrettyFormat(false);*/
SQLStatement stmt = stmtList.get(0);
stmt.accept(visitor);
// System.out.println(parameters);
assertEquals(1, parameters.size());
// SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, dbType);
// List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement pstmt = SQLUtils.parseStatements(psql, dbType).get(0);
StringBuilder buf = new StringBuilder();
SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
visitor1.addTableMapping("udata", "udata_0888");
visitor1.setInputParameters(visitor.getParameters());
pstmt.accept(visitor1);
assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id IN (1, 2, 3)", buf.toString());
}
Aggregations