SqlScriptReader extends LineNumberReader implements ISqlStatementSource { public String readSqlStatement() { try { String line = readLine(); StringBuilder sql = null; int checkStatementEndsIndex = 0; CheckEndStatus endStatus = new CheckEndStatus(); if (line != null) { do { if (sql == null) { sql = new StringBuilder(); } sql.append("\n"); sql.append(line); if (checkStatementEnds(endStatus, checkStatementEndsIndex, sql.toString())) { String toExecute = sql.substring(0, sql.lastIndexOf(delimiter)); toExecute = prepareForExecute(toExecute); if (StringUtils.isNotBlank(toExecute)) { return toExecute; } else { sql.setLength(0); } } else { checkStatementEndsIndex = sql.length(); } line = readLine(); } while (line != null); String toExecute = sql.toString(); if (StringUtils.isNotBlank(toExecute)) { return prepareForExecute(toExecute); } else { return null; } } else { return null; } } catch (IOException ex) { throw new IoException(ex); } } SqlScriptReader(Reader in); void setUsePrefixSuffixForReplacementTokens(boolean usePrefixSuffixForReplacementTokens); void setStripOutBlockComments(boolean stripOutBlockComments); boolean isStripOutBlockComments(); boolean isUsePrefixSuffixForReplacementTokens(); void setDelimiter(String delimiter); String getDelimiter(); void setReplacementTokens(Map<String, String> replacementTokens); Map<String, String> getReplacementTokens(); String readSqlStatement(); final static String QUERY_ENDS; }
@Test public void testReadScript2() throws Exception { SqlScriptReader reader = new SqlScriptReader(new InputStreamReader(getClass().getResourceAsStream("/test-script-2.sql"))); int count = 0; while (reader.readSqlStatement() != null) { count++; } assertEquals(36, count); }