diff --git a/src/main/java/io/mycat/server/parser/ServerParse.java b/src/main/java/io/mycat/server/parser/ServerParse.java index ef7826955f3fcb8050e29534d2716676b82cabb7..a0f1d8147ba52124b2764977f014259f37deb12e 100644 --- a/src/main/java/io/mycat/server/parser/ServerParse.java +++ b/src/main/java/io/mycat/server/parser/ServerParse.java @@ -75,13 +75,26 @@ public final class ServerParse { int length = stmt.length(); //FIX BUG FOR SQL SUCH AS /XXXX/SQL int rt = -1; - for (int i = 0; i < length; ++i) { - switch (stmt.charAt(i)) { - case ' ': - case '\t': - case '\r': - case '\n': - continue; + boolean isFirstChart = true; + boolean isDivision = true; + char c; + + for (int i = 0; i < length; ++i) { + c = stmt.charAt(i); + isDivision = ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n')); + + if (isDivision) { + isFirstChart = true; + continue; + } + + if (!isFirstChart) { + continue; // skip medial char + } + + isFirstChart = false; + + switch (c) { case '/': // such as /*!40101 SET character_set_client = @saved_cs_client // */; diff --git a/src/test/java/io/mycat/parser/ServerParseTest.java b/src/test/java/io/mycat/parser/ServerParseTest.java index 24581e1c06d5996f42f504a5dcc68a7d5b707277..777576f0309ab17ada2df3837fed10c9c85c8bfd 100644 --- a/src/test/java/io/mycat/parser/ServerParseTest.java +++ b/src/test/java/io/mycat/parser/ServerParseTest.java @@ -30,6 +30,9 @@ public class ServerParseTest { public static final int MYSQL_COMMENT = 19; public static final int CALL = 20; public static final int DESCRIBE = 21; + public static final int PREPARE = 24; + public static final int EXECUTE = 25; + public static final int DEALLOCATE = 26; */ @Test @@ -202,4 +205,34 @@ public class ServerParseTest { Assert.assertEquals(ServerParse.COMMIT, sqlType); } + @Test + public void testKeywordParse() { + String sql = "not-insert statement"; + int sqlType = ServerParse.parse(sql); + Assert.assertEquals(ServerParse.OTHER, sqlType); //make sure not mistaken for ServerParse.INSERT + } + + @Test + public void testPrepare() { + String sql = "PREPARE prepareId from select 1"; + int result = ServerParse.parse(sql); + int sqlType = result & 0xff; + Assert.assertEquals(ServerParse.PREPARE, sqlType); + } + + @Test + public void testExecute() { + String sql = "EXECUTE prepareId"; + int result = ServerParse.parse(sql); + int sqlType = result & 0xff; + Assert.assertEquals(ServerParse.EXECUTE, sqlType); + } + + @Test + public void testDeallocatePrepare() { + String sql = "DEALLOCATE PREPARE prepareId"; + int result = ServerParse.parse(sql); + int sqlType = result & 0xff; + Assert.assertEquals(ServerParse.DEALLOCATE, sqlType); + } }