druid 解析 MySQL 语句 "SHOW FULL TABLES FROM test-db WHERE Table_type != 'VIEW'" 时,通过 MySqlShowTablesStatement.getDatabase() 获取 database 名字不正确

qaxu7uf2  于 2021-11-27  发布在  Java
关注(0)|答案(1)|浏览(247)

代码如下:

String stmt = "SHOW FULL TABLES FROM test-ds WHERE Table_type != 'VIEW'";

MySqlStatementParser parser = new MySqlStatementParser(stmt);
MySqlShowTablesStatement statement = (MySqlShowTablesStatement) parser.parseStatement();

SQLName database = statement.getDatabase();

执行后获取的 database = 'test',不是预期的 'test-ds'

pokxtpni

pokxtpni1#

分析代码,发现是 CharTypes.identifierFlags 判断 identifier 缺少判断 ‘-’ 字符。
由于 odps 采用 ‘--’ 做 comment
因此只修改了 MySQL 的逻辑:
MysqlLexer.java

+    private final static boolean[] identifierFlags = new boolean[256];                
+    static {                                                                          
+        identifierFlags['-'] = true;                                                  
+    }                                                                                 
+                                                                                      
+    private boolean isIdentifierChar0(char c) {                                       
+        if (c <= identifierFlags.length) {                                            
+            return identifierFlags[c];                                                
+        }                                                                             
+                                                                                      
+        return c != ' ' && c != ',';                                                  
+    }                                                                                 

     private boolean isIdentifierChar(char c) {                                        
-        return c != '#' && CharTypes.isIdentifierChar(c);                             
+        return c != '#' && (CharTypes.isIdentifierChar(c) || isIdentifierChar0(c));   
     }

相关问题