Skip to content

Commit

Permalink
Merge pull request #1141 from qiankunshe/1.6
Browse files Browse the repository at this point in the history
提交处理 sql 引用的问题,并新增了几个测试方法
  • Loading branch information
magicdoom authored Oct 3, 2016
2 parents 48da839 + bec8ce0 commit 55da522
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/main/java/io/mycat/route/util/RouterUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class RouterUtil {

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


/**
* 移除执行语句中的数据库名
*
Expand All @@ -62,7 +61,6 @@ public class RouterUtil {
*
* @author mycat
*/

public static String removeSchema(String stmt, String schema) {
final String upStmt = stmt.toUpperCase();
final String upSchema = schema.toUpperCase() + ".";
Expand Down Expand Up @@ -102,9 +100,15 @@ public static String removeSchema(String stmt, String schema) {
private static int countChar(String sql,int end)
{
int count=0;
boolean skipChar = false;
for (int i = 0; i < end; i++) {
if(sql.charAt(i)=='\'') {
if(sql.charAt(i)=='\'' && !skipChar) {
count++;
skipChar = false;
}else if( sql.charAt(i)=='\\'){
skipChar = true;
}else{
skipChar = false;
}
}
return count;
Expand Down
29 changes: 28 additions & 1 deletion src/test/java/io/mycat/route/util/RouterUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* @date 2016/7/19
*/
public class RouterUtilTest {



@Test
public void testBatchInsert() {
String sql = "insert into hotnews(title,name) values('test1',\"name\"),('(test)',\"(test)\"),('\\\"',\"\\'\"),(\")\",\"\\\"\\')\");";
Expand Down Expand Up @@ -45,10 +48,34 @@ public void testRemoveSchemaSelect() {

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

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

}

@Test
public void testRemoveSchema2(){
String sql = "update testx.test set name='abcd \\' testx.aa' where id=1";
String sqltrue = "update test set name='abcd \\' testx.aa' where id=1";
String sqlnew = RouterUtil.removeSchema(sql, "testx");
Assert.assertEquals("处理错误:", sqltrue, sqlnew);
}

@Test
public void testRemoveSchema3(){
String sql = "update testx.test set testx.name='abcd testx.aa' where testx.id=1";
String sqltrue = "update test set name='abcd testx.aa' where id=1";
String sqlnew = RouterUtil.removeSchema(sql, "testx");
Assert.assertEquals("处理错误:", sqltrue, sqlnew);
}

@Test
public void testRemoveSchema4(){
String sql = "update testx.test set testx.name='abcd testx.aa' and testx.name2='abcd testx.aa' where testx.id=1";
String sqltrue = "update test set name='abcd testx.aa' and name2='abcd testx.aa' where id=1";
String sqlnew = RouterUtil.removeSchema(sql, "testx");
Assert.assertEquals("处理错误:", sqltrue, sqlnew);
}
}

0 comments on commit 55da522

Please sign in to comment.