use of com.alibaba.druid.sql.visitor.ExportParameterVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_9 method test_parameterize.
public void test_parameterize() throws Exception {
final String dbType = JdbcConstants.MYSQL;
String sql = "select * from t limit 3, 4";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
Assert.assertEquals("SELECT *\n" + "FROM t\n" + "LIMIT ?, ?", psql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
StringBuilder out = new StringBuilder();
ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
for (SQLStatement stmt : stmtList) {
stmt.accept(visitor);
}
Assert.assertEquals(2, visitor.getParameters().size());
Assert.assertEquals(3, visitor.getParameters().get(0));
Assert.assertEquals(4, visitor.getParameters().get(1));
}
use of com.alibaba.druid.sql.visitor.ExportParameterVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_10 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
/*String instance = "100.81.152.9"+"_"+3314;
int urlNum = Math.abs(instance.hashCode()) % 2;
System.out.println(urlNum);*/
/* String formattedSql = SQLUtils.format("select * from ? where id = ?", JdbcConstants.MYSQL,
Arrays.<Object> asList("abc,a"));
System.out.println(formattedSql);*/
final String dbType = JdbcConstants.MYSQL;
String sql = "SELECT `SURVEY_ANSWER`.`TIME_UPDATED`, `SURVEY_ANSWER`.`ANSWER_VALUE` FROM `S_ANSWER_P0115` `SURVEY_ANSWER` WHERE `SURVEY_ANSWER`.`SURVEY_ID` = 11 AND `SURVEY_ANSWER`.`QUESTION_CODE` = 'qq' ORDER BY `SURVEY_ANSWER`.`TIME_UPDATED` DESC LIMIT 1, 2";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
/* Assert.assertEquals("SELECT *\n" +
"FROM t\n" +
"LIMIT ?, ?", psql);*/
System.out.println(psql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
StringBuilder out = new StringBuilder();
ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
for (SQLStatement stmt : stmtList) {
stmt.accept(visitor);
}
System.out.println(visitor.getParameters());
stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
visitor = new MySqlExportParameterVisitor();
for (SQLStatement stmt : stmtList) {
stmt.accept(visitor);
}
System.out.println(visitor.getParameters());
/* Assert.assertEquals(2, visitor.getParameters().size());
Assert.assertEquals(3, visitor.getParameters().get(0));
Assert.assertEquals(4, visitor.getParameters().get(1));*/
}
use of com.alibaba.druid.sql.visitor.ExportParameterVisitor in project druid by alibaba.
the class ExportParameters method test_export_parameters.
public void test_export_parameters() throws Exception {
String sql = "select * from t where id = 3 and name = 'abc'";
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
StringBuilder out = new StringBuilder();
ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
for (SQLStatement stmt : stmtList) {
stmt.accept(visitor);
}
String paramteredSql = out.toString();
System.out.println(paramteredSql);
// [3, "abc"]
List<Object> paramters = visitor.getParameters();
for (Object param : paramters) {
System.out.println(param);
}
}
use of com.alibaba.druid.sql.visitor.ExportParameterVisitor in project druid by alibaba.
the class ExportAndParameterizedVisitorTestCase method testParameterizedVisitor.
public void testParameterizedVisitor() {
// final String sql =
// "insert into tab01(a,b,c) values('a1','bXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1',5)";
Object[][] sqlAndExpectedCases = { { "insert into tab01(a,b,c) values('a1','b1',5)", 3, "a1" }, { "select * from tab01 where a=1 and b='b1'", 2, 1 }, { "update tab01 set d='d1' where a=1 and b='b1'", 3, "d1" }, { "delete from tab01 where a=1 and b='b1'", 2, 1.0 } };
String[] dbTypes = { "mysql", "oracle", "db2", JdbcConstants.POSTGRESQL, JdbcUtils.JTDS, "not-found" };
// String[] dbTypes = { JdbcUtils.JTDS};
for (String dbType : dbTypes) {
System.out.println("dbType:" + dbType);
for (Object[] arr : sqlAndExpectedCases) {
final String sql = (String) arr[0];
StringBuilder out = new StringBuilder();
final SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
final ParameterizedVisitor pVisitor = (ParameterizedVisitor) ExportParameterVisitorUtils.createExportParameterVisitor(out, dbType);
final SQLStatement parseStatement = parser.parseStatement();
parseStatement.accept(pVisitor);
// final ExportParameterVisitor vistor2 = new MySqlExportParameterVisitor();
// parseStatement.accept(vistor2);
final ExportParameterVisitor vistor2 = (ExportParameterVisitor) pVisitor;
System.out.println("before:" + sql);
System.out.println("after:" + out);
System.out.println("size:" + vistor2.getParameters());
final int expectedSize = (Integer) arr[1];
Assert.assertEquals(expectedSize, vistor2.getParameters().size());
}
}
}
Aggregations