Skip to content

Commit 55da522

Browse files
authored
Merge pull request #1141 from qiankunshe/1.6
提交处理 sql 引用的问题,并新增了几个测试方法
2 parents 48da839 + bec8ce0 commit 55da522

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/main/java/io/mycat/route/util/RouterUtil.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public class RouterUtil {
5252

5353
private static final Logger LOGGER = LoggerFactory.getLogger(RouterUtil.class);
5454

55-
5655
/**
5756
* 移除执行语句中的数据库名
5857
*
@@ -62,7 +61,6 @@ public class RouterUtil {
6261
*
6362
* @author mycat
6463
*/
65-
6664
public static String removeSchema(String stmt, String schema) {
6765
final String upStmt = stmt.toUpperCase();
6866
final String upSchema = schema.toUpperCase() + ".";
@@ -102,9 +100,15 @@ public static String removeSchema(String stmt, String schema) {
102100
private static int countChar(String sql,int end)
103101
{
104102
int count=0;
103+
boolean skipChar = false;
105104
for (int i = 0; i < end; i++) {
106-
if(sql.charAt(i)=='\'') {
105+
if(sql.charAt(i)=='\'' && !skipChar) {
107106
count++;
107+
skipChar = false;
108+
}else if( sql.charAt(i)=='\\'){
109+
skipChar = true;
110+
}else{
111+
skipChar = false;
108112
}
109113
}
110114
return count;

src/test/java/io/mycat/route/util/RouterUtilTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* @date 2016/7/19
1515
*/
1616
public class RouterUtilTest {
17+
18+
19+
1720
@Test
1821
public void testBatchInsert() {
1922
String sql = "insert into hotnews(title,name) values('test1',\"name\"),('(test)',\"(test)\"),('\\\"',\"\\'\"),(\")\",\"\\\"\\')\");";
@@ -45,10 +48,34 @@ public void testRemoveSchemaSelect() {
4548

4649
@Test
4750
public void testRemoveSchemaSelect2() {
48-
String sql = "select id as 'aa' from testx.test where name='abcd testx.aa' and id=1 and testx=123";
51+
String sql = "select id as 'aa' from testx.test where name='abcd testx.aa' and id=1 and testx=123";
4952

5053
String afterAql= RouterUtil.removeSchema(sql,"testx");
5154
Assert.assertNotSame(sql.indexOf("testx."),afterAql.indexOf("testx."));
5255

5356
}
57+
58+
@Test
59+
public void testRemoveSchema2(){
60+
String sql = "update testx.test set name='abcd \\' testx.aa' where id=1";
61+
String sqltrue = "update test set name='abcd \\' testx.aa' where id=1";
62+
String sqlnew = RouterUtil.removeSchema(sql, "testx");
63+
Assert.assertEquals("处理错误:", sqltrue, sqlnew);
64+
}
65+
66+
@Test
67+
public void testRemoveSchema3(){
68+
String sql = "update testx.test set testx.name='abcd testx.aa' where testx.id=1";
69+
String sqltrue = "update test set name='abcd testx.aa' where id=1";
70+
String sqlnew = RouterUtil.removeSchema(sql, "testx");
71+
Assert.assertEquals("处理错误:", sqltrue, sqlnew);
72+
}
73+
74+
@Test
75+
public void testRemoveSchema4(){
76+
String sql = "update testx.test set testx.name='abcd testx.aa' and testx.name2='abcd testx.aa' where testx.id=1";
77+
String sqltrue = "update test set name='abcd testx.aa' and name2='abcd testx.aa' where id=1";
78+
String sqlnew = RouterUtil.removeSchema(sql, "testx");
79+
Assert.assertEquals("处理错误:", sqltrue, sqlnew);
80+
}
5481
}

0 commit comments

Comments
 (0)