@date 2019年4月30日上午09:58:02 @desc sql解析器
| 32 | * @desc sql解析器 |
| 33 | */ |
| 34 | public class SqlParser { |
| 35 | private Statement statement; |
| 36 | |
| 37 | private String type; |
| 38 | |
| 39 | public SqlParser(String sql) { |
| 40 | String normalizedSql = sql.toUpperCase().trim(); |
| 41 | if (normalizedSql.indexOf("SELECT") == 0) { |
| 42 | statement = com.sql.parse.statement.Select.parse(sql); |
| 43 | type = "SELECT"; |
| 44 | } else if (normalizedSql.indexOf("INSERT") == 0) { |
| 45 | statement = com.sql.parse.statement.Insert.parse(sql); |
| 46 | type = "INSERT"; |
| 47 | } else if (normalizedSql.indexOf("UPDATE") == 0) { |
| 48 | statement = com.sql.parse.statement.Update.parse(sql); |
| 49 | type = "UPDATE"; |
| 50 | } else if (normalizedSql.indexOf("DELETE") == 0) { |
| 51 | statement = com.sql.parse.statement.Delete.parse(sql); |
| 52 | type = "DELETE"; |
| 53 | } else { |
| 54 | throw BaseException.errorCode(SqlParseConstant.CODE_010); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public String getType() { |
| 59 | return type; |
| 60 | } |
| 61 | |
| 62 | public Statement getStatement() { |
| 63 | return statement; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @title: getTableNames |
| 68 | * @desc 获取表名 |
| 69 | */ |
| 70 | public List<String> getTableNames() { |
| 71 | switch (SqlParseConfig.getHandle()) { |
| 72 | case "com.github.jsqlparser":{ |
| 73 | TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); |
| 74 | if ("SELECT".equals(type)) { |
| 75 | Select select = (Select) statement; |
| 76 | return tablesNamesFinder.getTableList(select.getSelect()); |
| 77 | } else if ("INSERT".equals(type)) { |
| 78 | Insert insert = (Insert) statement; |
| 79 | return tablesNamesFinder.getTableList(insert.getInsert()); |
| 80 | } else if ("UPDATE".equals(type)) { |
| 81 | Update update = (Update) statement; |
| 82 | return tablesNamesFinder.getTableList(update.getUpdate()); |
| 83 | } else if ("DELETE".equals(type)) { |
| 84 | Delete delete = (Delete) statement; |
| 85 | return tablesNamesFinder.getTableList(delete.getDelete()); |
| 86 | } else { |
| 87 | throw BaseException.errorCode(SqlParseConstant.CODE_011); |
| 88 | } |
| 89 | } |
| 90 | case "com.alibaba.druid":{ |
| 91 | List<String> list = new ArrayList<>(); |
nothing calls this directly
no outgoing calls
no test coverage detected