📄 queryanalyser.java
字号:
}); } private void setLeftStatusText(final String text) { GUIUtils.invokeAndWait(new Runnable() { public void run() { panel.setLeftStatusText(text); } }); } private void setOutputMessage(final int type, final String text) { GUIUtils.invokeAndWait(new Runnable() { public void run() { panel.setOutputMessage(type, text); if (text != null) { logOutput(text); } } }); } private void setResultSet(final ResultSet rs, final String query) { GUIUtils.invokeAndWait(new Runnable() { public void run() { try { panel.setResultSet(rs, query); } catch (SQLException e) { processException(e); } } }); } /** matcher to remove new lines from log messages */ private Matcher newLineMatcher; /** * Logs the specified text to the logger. * * @param text - the text to log */ private void logOutput(String text) { if (logging) { newLineMatcher.reset(text); OutputLogger.append(LOGGER_NAME, newLineMatcher.replaceAll(" ")); } } /** * Formats and prints to the output pane the specified warning. * * @param warning - the warning to be printed */ private void outputWarnings(SQLWarning warning) { if (warning == null) { return; } String dash = " - "; // print the first warning setOutputMessage(QueryEditorConstants.WARNING_MESSAGE, warning.getErrorCode() + dash + warning.getMessage()); // retrieve subsequent warnings SQLWarning _warning = null; int errorCode = -1000; int _errorCode = warning.getErrorCode(); while ((_warning = warning.getNextWarning()) != null) { errorCode = _warning.getErrorCode(); if (errorCode == _errorCode) { return; } _errorCode = errorCode; setOutputMessage(QueryEditorConstants.WARNING_MESSAGE, _errorCode + dash + _warning.getMessage()); warning = _warning; } } /** Original single queries sent list */ private List<String> originalQueries; /** Stored tokens */ private List<Token> tokens; /** * Splits (tokenizes) the specified query block into multiple single * queries using the semi-colon (;) character as the effective delimiter. * <p>This will store the queries in their original form in addition to * returning a list of individual queries formatted for execution by * removing any multi-line and single-line comments from each query. * * @param query - the query block to be executed * @return a list of individual queries derived from the specified block */ public List<String> tokenizeQuery(String query) { // init or clear the token cache if (tokens == null) { tokens = new ArrayList<Token>(); } else { tokens.clear(); } // make sure the matchers are initialised initMatchers(); // store the multi-line comments position multiLineCommentMatcher.reset(query); while (multiLineCommentMatcher.find()) { tokens.add(new Token(TokenTypes.COMMENT, multiLineCommentMatcher.start(), multiLineCommentMatcher.end())); } // store the single-line comments position singleLineCommentMatcher.reset(query); while (singleLineCommentMatcher.find()) { tokens.add(new Token(TokenTypes.SINGLE_LINE_COMMENT, singleLineCommentMatcher.start(), singleLineCommentMatcher.end())); } // store the quoted string positions quoteMatcher.reset(query); while (quoteMatcher.find()) { tokens.add(new Token(TokenTypes.STRING, quoteMatcher.start(), quoteMatcher.end())); } int index = 0; // store the delim (;) indexes List<Integer> delims = new ArrayList<Integer>(); while ((index = query.indexOf(delim, index + 1)) != -1) { delims.add(new Integer(index)); } int delimCount = delims.size(); int tokenCount = tokens.size(); if (tokenCount > 0 && delimCount > 0) { // loop through and remove the delim indexes that are // either a comment (single or multi -line) or a quoted string for (int i = 0; i < delims.size(); i++) { // add 1 to this index as the token endIndex // includes the last char in the pattern test index = delims.get(i).intValue() + 1; for (int j = 0; j < tokenCount; j++) { Token token = tokens.get(j); if (token.contains(index)) { delims.remove(i); i--; break; } } } } // ------------------------------------------------- // first, store the queries in their original form // init the original queries list if (originalQueries == null) { originalQueries = new ArrayList<String>(); } else { originalQueries.clear(); } index = 0; delimCount = delims.size(); if (delimCount > 0) { for (int i = 0; i < delimCount; i++) { int delimIndex = delims.get(i).intValue(); originalQueries.add(query.substring(index, delimIndex)); index = delimIndex + 1; } } else { originalQueries.add(query); } // --------------------------------------------- // next, store the queries in execution form // remove all multi-line comments multiLineCommentMatcher.reset(); String _query = multiLineCommentMatcher.replaceAll(Constants.EMPTY); // remove all the single-line comments singleLineCommentMatcher.reset(_query); _query = singleLineCommentMatcher.replaceAll(Constants.EMPTY); // rescan for quoted text tokens.clear(); quoteMatcher.reset(_query); while (quoteMatcher.find()) { tokens.add(new Token(quoteMatcher.start(), quoteMatcher.end())); } // rescan for delims in absence of commented strings index = 0; delims.clear(); while ((index = _query.indexOf(delim, index + 1)) != -1) { delims.add(new Integer(index)); } delimCount = delims.size(); tokenCount = tokens.size(); if (tokenCount > 0 && delimCount > 0) { // loop through and remove the delim indexes that are // either a comment (single or multi -line) or a quoted string for (int i = 0; i < delims.size(); i++) { // add 1 to this index as the token endIndex // includes the last char in the pattern test index = delims.get(i).intValue() + 1; for (int j = 0; j < tokenCount; j++) { Token token = tokens.get(j); if (token.contains(index)) { delims.remove(i); i--; break; } } } } index = 0; delimCount = delims.size(); List<String> queries = new ArrayList<String>(delimCount); if (delimCount > 0) { for (int i = 0; i < delimCount; i++) { int delimIndex = delims.get(i).intValue(); trimNewLineMatcher.reset(_query.substring(index, delimIndex)); queries.add(trimNewLineMatcher.replaceAll( Constants.NEW_LINE_STRING).trim()); index = delimIndex + 1; } } else { trimNewLineMatcher.reset(_query); queries.add(trimNewLineMatcher.replaceAll( Constants.NEW_LINE_STRING).trim()); } return queries; }/*insert into dummy_table_2 (column1, column2) --jhdsfhs ; kjdshfvalues ('ABC12', 'f_issueQC=true;f_articleQC=true;f_pdfQC=true;f_hasPDFs=true');insert into dummy_table_2 (column1, column2) --jhdsfhs ; kjdshfvalues (/* kkjdhgjhdgj jshdkjfs; h * /'ABC12', 'f_issueQC=true;f_articleQC=true;f_pdfQC=true;f_hasPDFs=true');*/ /** * Closes the current connection. */ public void destroyConnection() { if (qs != null) { try { qs.destroyConnection(); } catch (SQLException e) {} } } /** * Dtermines whether the specified query is attempting * to create a SQL PROCEDURE or FUNCTION. * * @param query - the query to be executed * @return true | false */ private boolean isCreateProcedureOrFunction(String query) { return isCreateProcedure(query) || isCreateFunction(query); } /** * Dtermines whether the specified query is attempting * to create a SQL PROCEDURE. * * @param query - the query to be executed * @return true | false */ private boolean isCreateProcedure(String query) { int createIndex = query.indexOf("CREATE"); int tableIndex = query.indexOf("TABLE"); int procedureIndex = query.indexOf("PROCEDURE"); int packageIndex = query.indexOf("PACKAGE"); return createIndex != -1 && tableIndex == -1 && (procedureIndex > createIndex || packageIndex > createIndex); } /** * Dtermines whether the specified query is attempting * to create a SQL FUNCTION. * * @param query - the query to be executed * @return true | false */ private boolean isCreateFunction(String query) { int createIndex = query.indexOf("CREATE"); int tableIndex = query.indexOf("TABLE"); int functionIndex = query.indexOf("FUNCTION"); return createIndex != -1 && tableIndex == -1 && functionIndex > createIndex; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -