use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_44 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "select 1 from a where c1 in (date_format(date_add(curdate(), INTERVAL -7 DAY), '%Y%m%d'))";
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 a\n" + "WHERE c1 IN (date_format(date_add(curdate(), INTERVAL ? DAY), '%Y%m%d'))", 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,-7]", json);
String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
assertEquals("SELECT 1\n" + "FROM a\n" + "WHERE c1 IN (date_format(date_add(curdate(), INTERVAL -7 DAY), '%Y%m%d'))", rsql);
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_48_aggregate method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "select count(1) from t";
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 count(1)\n" + "FROM t", psql);
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_51_schema method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "UPDATE `galicdb_0102`.auction_auctions_0201 SET `starts` = ?, `pict_url` = ?, `category` = ?, `minimum_bid` = ?, `reserve_price` = ?, `city` = ?, `prov` = ?, `ends` = ?, `current_bid` = NULL, `quantity` = ?, `zoo` = ?, `secure_trade_ordinary_post_fee` = ?, `secure_trade_fast_post_fee` = ?, `old_quantity` = ?, `options` = ?, `secure_trade_ems_post_fee` = ?, `property` = ?, `last_modified` = ?, `desc_path` = ?, `postage_id` = ?, `shop_categories_id_lists` = ?, `spu_id` = ?, `sync_version` = ?, `auction_status` = ?, `features` = ?, `feature_cc` = ?, `main_color` = ?, `outer_id` = ?, `auction_sub_status` = ?, `commodity_id` = ? WHERE `auction_id` = ?";
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("UPDATE galicdb.auction_auctions\n" + "SET `starts` = ?, `pict_url` = ?, `category` = ?, `minimum_bid` = ?, `reserve_price` = ?, `city` = ?, `prov` = ?, `ends` = ?, `current_bid` = NULL, `quantity` = ?, `zoo` = ?, `secure_trade_ordinary_post_fee` = ?, `secure_trade_fast_post_fee` = ?, `old_quantity` = ?, `options` = ?, `secure_trade_ems_post_fee` = ?, `property` = ?, `last_modified` = ?, `desc_path` = ?, `postage_id` = ?, `shop_categories_id_lists` = ?, `spu_id` = ?, `sync_version` = ?, `auction_status` = ?, `features` = ?, `feature_cc` = ?, `main_color` = ?, `outer_id` = ?, `auction_sub_status` = ?, `commodity_id` = ?\n" + "WHERE `auction_id` = ?", psql);
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor 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.sql.visitor.SQLASTOutputVisitor 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);
}
Aggregations