mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor sql parser & support tableMapping. issue #1432
This commit is contained in:
@@ -659,7 +659,7 @@ public class SQLUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static String refactor(String dbType, String sql, Map<String, String> tableMapping) {
|
||||
public static String refactor(String sql, String dbType, Map<String, String> tableMapping) {
|
||||
List<SQLStatement> stmtList = parseStatements(sql, dbType);
|
||||
return SQLUtils.toSQLString(stmtList, dbType, null, null, tableMapping);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ public class MappingTest_createTable extends TestCase {
|
||||
Map<String, String> mapping = Collections.singletonMap("user", "user_01");
|
||||
|
||||
public void test_mapping_createTable() throws Exception {
|
||||
String result = SQLUtils.refactor(null, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, null, mapping);
|
||||
assertEquals("CREATE TABLE user_01 (\n" +
|
||||
"\tsource_key int,\n" +
|
||||
"\tsource_value varchar(32),\n" +
|
||||
@@ -29,7 +29,7 @@ public class MappingTest_createTable extends TestCase {
|
||||
}
|
||||
|
||||
public void test_mapping_createTable_mysql() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.MYSQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.MYSQL, mapping);
|
||||
assertEquals("CREATE TABLE user_01 (\n" +
|
||||
"\tsource_key int, \n" +
|
||||
"\tsource_value varchar(32), \n" +
|
||||
@@ -38,7 +38,7 @@ public class MappingTest_createTable extends TestCase {
|
||||
}
|
||||
|
||||
public void test_mapping_createTable_oracle() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.ORACLE, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.ORACLE, mapping);
|
||||
assertEquals("CREATE TABLE user_01 (\n" +
|
||||
"\tsource_key int,\n" +
|
||||
"\tsource_value varchar(32),\n" +
|
||||
@@ -47,7 +47,7 @@ public class MappingTest_createTable extends TestCase {
|
||||
}
|
||||
|
||||
public void test_mapping_createTable_pg() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.POSTGRESQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.POSTGRESQL, mapping);
|
||||
assertEquals("CREATE TABLE user_01 (\n" +
|
||||
"\tsource_key int,\n" +
|
||||
"\tsource_value varchar(32),\n" +
|
||||
@@ -56,7 +56,7 @@ public class MappingTest_createTable extends TestCase {
|
||||
}
|
||||
|
||||
public void test_mapping_createTable_sqlserver() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.SQL_SERVER, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.SQL_SERVER, mapping);
|
||||
assertEquals("CREATE TABLE user_01 (\n" +
|
||||
"\tsource_key int,\n" +
|
||||
"\tsource_value varchar(32),\n" +
|
||||
@@ -65,7 +65,7 @@ public class MappingTest_createTable extends TestCase {
|
||||
}
|
||||
|
||||
public void test_mapping_createTable_db2() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.DB2, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.DB2, mapping);
|
||||
assertEquals("CREATE TABLE user_01 (\n" +
|
||||
"\tsource_key int,\n" +
|
||||
"\tsource_value varchar(32),\n" +
|
||||
|
||||
@@ -15,32 +15,32 @@ public class MappingTest_delete extends TestCase {
|
||||
Map<String, String> mapping = Collections.singletonMap("user", "user_01");
|
||||
|
||||
public void test_mapping() throws Exception {
|
||||
String result = SQLUtils.refactor(null, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, null, mapping);
|
||||
assertEquals("DELETE FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_mysql() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.MYSQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.MYSQL, mapping);
|
||||
assertEquals("DELETE FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_pg() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.POSTGRESQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.POSTGRESQL, mapping);
|
||||
assertEquals("DELETE FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_oracle() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.ORACLE, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.ORACLE, mapping);
|
||||
assertEquals("DELETE FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_sqlserver() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.SQL_SERVER, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.SQL_SERVER, mapping);
|
||||
assertEquals("DELETE FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_db2() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.DB2, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.DB2, mapping);
|
||||
assertEquals("DELETE FROM user_01", result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.alibaba.druid.bvt.sql;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/9/25.
|
||||
*/
|
||||
public class MappingTest_drop extends TestCase {
|
||||
private String sql = "drop table user";
|
||||
Map<String, String> mapping = Collections.singletonMap("user", "user_01");
|
||||
|
||||
public void test_mapping() throws Exception {
|
||||
String result = SQLUtils.refactor(sql, null, mapping);
|
||||
assertEquals("DROP TABLE user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_mysql() throws Exception {
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.MYSQL, mapping);
|
||||
assertEquals("DROP TABLE user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_pg() throws Exception {
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.POSTGRESQL, mapping);
|
||||
assertEquals("DROP TABLE user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_oracle() throws Exception {
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.ORACLE, mapping);
|
||||
assertEquals("DROP TABLE user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_sqlserver() throws Exception {
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.SQL_SERVER, mapping);
|
||||
assertEquals("DROP TABLE user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_db2() throws Exception {
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.DB2, mapping);
|
||||
assertEquals("DROP TABLE user_01", result);
|
||||
}
|
||||
}
|
||||
@@ -15,37 +15,37 @@ public class MappingTest_insert extends TestCase {
|
||||
Map<String, String> mapping = Collections.singletonMap("user", "user_01");
|
||||
|
||||
public void test_mapping() throws Exception {
|
||||
String result = SQLUtils.refactor(null, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, null, mapping);
|
||||
assertEquals("INSERT INTO user_01 (id, name)\n" +
|
||||
"VALUES (123, 'abc')", result);
|
||||
}
|
||||
|
||||
public void test_mapping_mysql() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.MYSQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.MYSQL, mapping);
|
||||
assertEquals("INSERT INTO user_01 (id, name)\n" +
|
||||
"VALUES (123, 'abc')", result);
|
||||
}
|
||||
|
||||
public void test_mapping_pg() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.POSTGRESQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.POSTGRESQL, mapping);
|
||||
assertEquals("INSERT INTO user_01 (id, name)\n" +
|
||||
"VALUES (123, 'abc')", result);
|
||||
}
|
||||
|
||||
public void test_mapping_oracle() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.ORACLE, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.ORACLE, mapping);
|
||||
assertEquals("INSERT INTO user_01 (id, name)\n" +
|
||||
"VALUES (123, 'abc')", result);
|
||||
}
|
||||
|
||||
public void test_mapping_sqlserver() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.SQL_SERVER, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.SQL_SERVER, mapping);
|
||||
assertEquals("INSERT INTO user_01 (id, name)\n" +
|
||||
"VALUES (123, 'abc')", result);
|
||||
}
|
||||
|
||||
public void test_mapping_db2() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.DB2, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.DB2, mapping);
|
||||
assertEquals("INSERT INTO user_01 (id, name)\n" +
|
||||
"VALUES (123, 'abc')", result);
|
||||
}
|
||||
|
||||
@@ -15,37 +15,37 @@ public class MappingTest_select extends TestCase {
|
||||
Map<String, String> mapping = Collections.singletonMap("user", "user_01");
|
||||
|
||||
public void test_mapping() throws Exception {
|
||||
String result = SQLUtils.refactor(null, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, null, mapping);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_mysql() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.MYSQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.MYSQL, mapping);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_pg() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.POSTGRESQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.POSTGRESQL, mapping);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_oracle() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.ORACLE, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.ORACLE, mapping);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_sqlserver() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.SQL_SERVER, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.SQL_SERVER, mapping);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM user_01", result);
|
||||
}
|
||||
|
||||
public void test_mapping_odps() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.ODPS, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.ODPS, mapping);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM user_01", result);
|
||||
}
|
||||
|
||||
@@ -15,42 +15,42 @@ public class MappingTest_update extends TestCase {
|
||||
Map<String, String> mapping = Collections.singletonMap("user", "user_01");
|
||||
|
||||
public void test_mapping() throws Exception {
|
||||
String result = SQLUtils.refactor(null, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, null, mapping);
|
||||
assertEquals("UPDATE user_01\n" +
|
||||
"SET f1 = 1\n" +
|
||||
"WHERE id = 3", result);
|
||||
}
|
||||
|
||||
public void test_mapping_mysql() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.MYSQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.MYSQL, mapping);
|
||||
assertEquals("UPDATE user_01\n" +
|
||||
"SET f1 = 1\n" +
|
||||
"WHERE id = 3", result);
|
||||
}
|
||||
|
||||
public void test_mapping_pg() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.POSTGRESQL, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.POSTGRESQL, mapping);
|
||||
assertEquals("UPDATE user_01\n" +
|
||||
"SET f1 = 1\n" +
|
||||
"WHERE id = 3", result);
|
||||
}
|
||||
|
||||
public void test_mapping_oracle() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.ORACLE, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.ORACLE, mapping);
|
||||
assertEquals("UPDATE user_01\n" +
|
||||
"SET f1 = 1\n" +
|
||||
"WHERE id = 3", result);
|
||||
}
|
||||
|
||||
public void test_mapping_sqlserver() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.SQL_SERVER, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.SQL_SERVER, mapping);
|
||||
assertEquals("UPDATE user_01\n" +
|
||||
"SET f1 = 1\n" +
|
||||
"WHERE id = 3", result);
|
||||
}
|
||||
|
||||
public void test_mapping_db2() throws Exception {
|
||||
String result = SQLUtils.refactor(JdbcConstants.DB2, sql, mapping);
|
||||
String result = SQLUtils.refactor(sql, JdbcConstants.DB2, mapping);
|
||||
assertEquals("UPDATE user_01\n" +
|
||||
"SET f1 = 1\n" +
|
||||
"WHERE id = 3", result);
|
||||
|
||||
Reference in New Issue
Block a user