imporved sql parser.

This commit is contained in:
wenshao
2017-10-19 11:17:10 +08:00
parent 377d9346b8
commit d1b21e20c1
8 changed files with 253 additions and 12 deletions
@@ -16,16 +16,16 @@ import java.util.List;
*/
public class MySqlParameterizedOutputVisitorTest_restore_0 extends TestCase {
public void test_for_parameterize() throws Exception {
String sqlTemplate = "SELECT id, name, x, y, city_code FROM `geo_poi_raw_data` `geo_poi_raw_data` WHERE 1 = 1 AND `id` > ? ORDER BY `id`";
String sqlTemplate = "SELECT id, name, x, y, city_code FROM `gpo_abi_raw_data` `gpo_abi_raw_data` WHERE 1 = 1 AND `id` > ? ORDER BY `id`";
String params = "[[1200000,1250000]]";
params = params.replaceAll("''", "'");
sqlTemplate = SQLUtils.formatMySql(sqlTemplate);
String table = "[\"`geo_poi_raw_data`\"]";
String table = "[\"`gpo_abi_raw_data`\"]";
String formattedSql = ParseUtil.restore(sqlTemplate, table, params);
assertEquals("SELECT id, name, x, y, city_code\n" +
"FROM `geo_poi_raw_data` `geo_poi_raw_data`\n" +
"FROM `gpo_abi_raw_data` `gpo_abi_raw_data`\n" +
"WHERE 1 = 1\n" +
"\tAND (`id`>1200000 OR `id`>1250000)\n" +
"\tAND (`id` > 1200000 OR `id` > 1250000)\n" +
"ORDER BY `id`", formattedSql);
}
}
@@ -0,0 +1,44 @@
package com.alibaba.druid.bvt.sql.mysql.param;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
/**
* Created by wenshao on 16/8/23.
*/
public class MySqlParameterizedOutputVisitorTest_restore_1 extends TestCase {
public void test_for_parameterize() throws Exception {
String sqlTemplate = "SELECT `buyer_resource`.`RESOURCE_ID`, `buyer_resource`.`RESOURCE_PROVIDER`, `buyer_resource`.`BUYER_ID`, `buyer_resource`.`RESOURCE_TYPE`, `buyer_resource`.`SUB_RESOURCE_TYPE`\n" +
"\t, `buyer_resource`.`STATUS`, `buyer_resource`.`START_TIME`, `buyer_resource`.`END_TIME`, `buyer_resource`.`FEATURE`, `buyer_resource`.`GMT_CREATED`\n" +
"\t, `buyer_resource`.`GMT_MODIFIED`, `buyer_resource`.`source`, `buyer_resource`.`seller_id`, `buyer_resource`.`original_Resource_Id`, `buyer_resource`.`business_unit`\n" +
"\t, `buyer_resource`.`resource_code`, `buyer_resource`.`OPTIONS`, `buyer_resource`.`AVAILABLE_COUNT`, `buyer_resource`.`TOTAL_COUNT`, `buyer_resource`.`OUT_INSTANCE_ID`\n" +
"\t, `buyer_resource`.`CONSUME_ID`, `buyer_resource`.`GROUP_ID`, `buyer_resource`.`BUSINESS_ID`, `buyer_resource`.`rule`, `buyer_resource`.`market_place`\n" +
"\t, `buyer_resource`.`VERSION`\n" +
"FROM buyer_resource `buyer_resource`\n" +
"WHERE `buyer_resource`.`BUYER_ID` = ?\n" +
"\tAND `buyer_resource`.`STATUS` = ?\n" +
"\tAND `buyer_resource`.`START_TIME` <= ?\n" +
"\tAND `buyer_resource`.`END_TIME` >= ?\n" +
"\tAND `buyer_resource`.`seller_id` = ?\n" +
"\tAND (`buyer_resource`.`AVAILABLE_COUNT` IS ?)\n" +
"LIMIT ?, ?";
String params = "[1957025290,1,\"2017-10-16 23:34:28.519\",\"2017-10-16 23:34:28.519\",2933220011L,[0,-1],0,20]";
params = params.replaceAll("''", "'");
sqlTemplate = SQLUtils.formatMySql(sqlTemplate);
String formattedSql = ParseUtil.restore(sqlTemplate, null, params);
assertEquals("SELECT `buyer_resource`.`RESOURCE_ID`, `buyer_resource`.`RESOURCE_PROVIDER`, `buyer_resource`.`BUYER_ID`, `buyer_resource`.`RESOURCE_TYPE`, `buyer_resource`.`SUB_RESOURCE_TYPE`\n" +
"\t, `buyer_resource`.`STATUS`, `buyer_resource`.`START_TIME`, `buyer_resource`.`END_TIME`, `buyer_resource`.`FEATURE`, `buyer_resource`.`GMT_CREATED`\n" +
"\t, `buyer_resource`.`GMT_MODIFIED`, `buyer_resource`.`source`, `buyer_resource`.`seller_id`, `buyer_resource`.`original_Resource_Id`, `buyer_resource`.`business_unit`\n" +
"\t, `buyer_resource`.`resource_code`, `buyer_resource`.`OPTIONS`, `buyer_resource`.`AVAILABLE_COUNT`, `buyer_resource`.`TOTAL_COUNT`, `buyer_resource`.`OUT_INSTANCE_ID`\n" +
"\t, `buyer_resource`.`CONSUME_ID`, `buyer_resource`.`GROUP_ID`, `buyer_resource`.`BUSINESS_ID`, `buyer_resource`.`rule`, `buyer_resource`.`market_place`\n" +
"\t, `buyer_resource`.`VERSION`\n" +
"FROM buyer_resource `buyer_resource`\n" +
"WHERE `buyer_resource`.`BUYER_ID` = 1957025290\n" +
"\tAND `buyer_resource`.`STATUS` = 1\n" +
"\tAND `buyer_resource`.`START_TIME` <= '2017-10-16 23:34:28.519'\n" +
"\tAND `buyer_resource`.`END_TIME` >= '2017-10-16 23:34:28.519'\n" +
"\tAND `buyer_resource`.`seller_id` = 2933220011\n" +
"\tAND (`buyer_resource`.`AVAILABLE_COUNT` = 0 OR `buyer_resource`.`AVAILABLE_COUNT` = -1)\n" +
"LIMIT 0, 20", formattedSql);
}
}
@@ -120,7 +120,12 @@ public class ParseUtil {
}
public static String restore(String sql, String table, String params/*JSONArray paramsArray, JSONArray destArray*/) {
JSONArray destArray = JSON.parseArray(table.replaceAll("''", "'"));
JSONArray destArray = null;
if (table != null) {
destArray = JSON.parseArray(table.replaceAll("''", "'"));
}
JSONArray paramsArray = JSON.parseArray(params.replaceAll("''", "'"));
String dbType = JdbcConstants.MYSQL;
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
@@ -141,8 +146,10 @@ public class ParseUtil {
srcArray.add(entry.getKey().getName());
}*/
for (int i = 0; i < srcArray.size(); i++) {
visitor.addTableMapping(srcArray.getString(i), destArray.getString(i));
if (destArray != null) {
for (int i = 0; i < srcArray.size(); i++) {
visitor.addTableMapping(srcArray.getString(i), destArray.getString(i));
}
}
stmt.accept(visitor);