use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_47_hints method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "/* ///d45b1886/ *//*+TDDL({'extra':{'SOCKET_TIMEOUT':'0'}})*/ DELETE FROM page_scores WHERE createdat <= '2017-06-16 00:39:09.1';";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
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.setPrettyFormat(false);*/
stmt.accept(visitor);
/* JSONArray array = new JSONArray();
for(String table : visitor.getTables()){
array.add(table.replaceAll("`",""));
}*/
String psql = out.toString();
System.out.println(psql);
assertEquals("/*+TDDL({'extra':{'SOCKET_TIMEOUT':'0'}})*/\n" + "DELETE FROM page_scores\n" + "WHERE createdat <= ?;", psql);
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_49_createTable method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = //
"CREATE TABLE projects (" + "long_name int(3) NOT NULL default 1 + 2" + ") ";
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("CREATE TABLE projects (\n" + "\tlong_name int(3) NOT NULL DEFAULT 1 + 2\n" + ")", psql);
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_50_orderby method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "select * from t order by 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();
System.out.println(psql);
assertEquals("SELECT *\n" + "FROM t\n" + "ORDER BY 1", psql);
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_28 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "SELECT `xxx_reverse_od`.dept, count(*) from xxx_reverse_od_0446 `xxx_reverse_od` join abc_xx_abc `aa` on `xxx_reverse_od`.id = `aa`.id";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
assertEquals("SELECT `xxx_reverse_od`.dept, count(*)\n" + "FROM xxx_reverse_od `xxx_reverse_od`\n" + "\tJOIN abc_xx_abc `aa` ON `xxx_reverse_od`.id = `aa`.id", psql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, 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("xxx_reverse_od", "xxx_reverse_od_0446");
visitor1.setParameters(visitor.getParameters());
stmt.accept(visitor1);
assertEquals("SELECT `xxx_reverse_od`.dept, count(*)\n" + "FROM xxx_reverse_od_0446 `xxx_reverse_od`\n" + "\tJOIN abc_xx_abc `aa` ON `xxx_reverse_od`.id = `aa`.id", buf.toString());
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_33 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";
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.setParameters(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)", buf.toString());
}
Aggregations