use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_37 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "insert into fc_sms_0011_201704 (c1, c2, c3) values (1, 'a', 'b')";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement statement = stmtList.get(0);
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);
assertEquals("INSERT INTO fc_sms (c1, c2, c3) VALUES (?, ?, ?)", out.toString());
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_43 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "UPDATE `feel_07`.feed_item_receive SET `attributes` = ?, `gmt_modified` = ?, `lock_version` = ? WHERE `feed_id` = ?";
String params = "[\"enableTime:1498682416713,src:top,importFrom:0\",\"2017-06-29 04:40:20\",1,313825887478L]";
String table = "[\"`feel_07`.`feed_item_receive_0502`\"]";
String restoredSql = restore(sql, table, params);
assertEquals("UPDATE `feel_07`.`feed_item_receive_0502`\n" + "SET `attributes` = 'enableTime:1498682416713,src:top,importFrom:0', `gmt_modified` = '2017-06-29 04:40:20', `lock_version` = 1\n" + "WHERE `feed_id` = 313825887478", restoredSql);
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_43 method restore.
public static String restore(String sql, String table, String params) /*JSONArray paramsArray, JSONArray destArray*/
{
JSONArray destArray = JSON.parseArray(table.replaceAll("''", "'"));
params = StringUtils.replace(params.replaceAll("''", "'"), "\\\"", "\"");
JSONArray paramsArray = JSON.parseArray(params);
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<Object>(paramsArray);
visitor.setParameters(paramsList);
SchemaStatVisitor schemaStatVisitor = new MySqlSchemaStatVisitor();
stmt.accept(schemaStatVisitor);
JSONArray srcArray = new JSONArray();
for (Map.Entry<TableStat.Name, TableStat> entry : schemaStatVisitor.getTables().entrySet()) {
System.out.println(entry.getKey().getName());
srcArray.add(entry.getKey().getName());
}
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.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_14 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "insert into\n" + "\t\tt_temp (processId,resultId,gmt_create,gmt_modified,result_content,result_number)\n" + "\t\tvalues\n" + "\t\t \n" + "\t\t\t('4254cc14-1c83-4eaf-95ae-59438dd0cc17', '5fd20fa9-7659-4f8b-a4c2-2021a48317d8', now(),now(),null,null)";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
String s = "INSERT INTO t_temp (processId, resultId, gmt_create, gmt_modified, result_content\n" + "\t, result_number)\n" + "VALUES (?, ?, now(), now(), ?\n" + "\t, ?)";
assertEquals(s, psql);
paramaterizeAST(sql, "INSERT INTO t_temp (processId, resultId, gmt_create, gmt_modified, result_content\n" + "\t, result_number)\n" + "VALUES (?, ?, now(), now(), NULL\n" + "\t, NULL)");
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_16 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "/* 0bfacfa414829200086238910e/0.3// */" + "insert into `t1` (" + " `f0`, `f1`, `f2`, `f3`, `f4`, " + "`f5`, `f6`, `f7`, `f8`, `f9`, " + "`destination`, `start_standard`, `start_fee`, `add_standard`, `add_fee`, " + "`region_fee_standard`, `region_fee_add`, `cell_fee`, `way_day`, `version`)" + " values ( 1, 2, 2, 3, 0, -4, 1, null, '2016-12-28 18:13:28.825', '2016-12-28 18:13:28.825', 1, 1, 0, 1, 0, null, null, null, null, 0)\n";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
assertEquals("INSERT INTO `t1` (`f0`, `f1`, `f2`, `f3`, `f4`\n" + "\t, `f5`, `f6`, `f7`, `f8`, `f9`\n" + "\t, `destination`, `start_standard`, `start_fee`, `add_standard`, `add_fee`\n" + "\t, `region_fee_standard`, `region_fee_add`, `cell_fee`, `way_day`, `version`)\n" + "VALUES (?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?)", psql);
StringBuilder out = new StringBuilder();
ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
for (SQLStatement stmt : stmtList) {
stmt.accept(visitor);
}
assertEquals(20, visitor.getParameters().size());
}
Aggregations