diff --git a/sql12/core/src/net/sourceforge/squirrel_sql/fw/sql/querytokenizer/QueryTokenizer.java b/sql12/core/src/net/sourceforge/squirrel_sql/fw/sql/querytokenizer/QueryTokenizer.java index 5c4700340..c76176881 100755 --- a/sql12/core/src/net/sourceforge/squirrel_sql/fw/sql/querytokenizer/QueryTokenizer.java +++ b/sql12/core/src/net/sourceforge/squirrel_sql/fw/sql/querytokenizer/QueryTokenizer.java @@ -164,6 +164,13 @@ public void setScriptToTokenize(String script) for (int i = 0; i < script.length(); ++i) { final NextPositionAction nextPositionAction = commentAndLiteralHandler.nextPosition(i); + if (!commentAndLiteralHandler.isInLiteral() && !commentAndLiteralHandler.isInMultiLineComment()) { + String newQuerySep = findSetTerminatorInstruction(script, i); + if (newQuerySep != null) { + s_log.info("changing statement separator to '" + newQuerySep + "'"); + setQuerySep(newQuerySep); + } + } curOriginalQuery.append(script.charAt(i)); @@ -206,6 +213,37 @@ public void setScriptToTokenize(String script) _queryIterator = _queries.iterator(); } + /** + * Check for a line that contains '--#SET TERMINATOR x' to change the current new statement separator + * The line may start with spaces or tabs. Other characters are not allowed. + */ + private String findSetTerminatorInstruction(String script, final int i) { + if (script.startsWith(_lineCommentBegin + "#SET TERMINATOR ", i)) { + // Check if the comment is only preceded with space or tabs + int j = i; + while (j-- > 0) { + char c = script.charAt(j); + if (c == '\n') { + // Found the start of the line, break the loop + break; + } else if (c != ' ' && c != '\t') { + // Found non-whitespace character + return null; + } + } + + // Only when the comment starts on a new line + int newLinePos = script.indexOf('\n', i); + if (newLinePos > i + 16 + _lineCommentBegin.length()) { + String terminator = script.substring(i + 16 + _lineCommentBegin.length(), newLinePos).trim(); + if (!terminator.isEmpty()) { + return terminator; + } + } + } + return null; + } + /** * Returns the number of queries that the tokenizer found in the script * given in the last call to setScriptToTokenize, or 0 if