SqlFileParser { public List<String> parse() { String line = null; int lineCounter = 0; StringBuilder statement = new StringBuilder(); List<String> statements = new LinkedList<String>(); Pattern commentPattern = Pattern.compile("( try { while ((line = reader.readLine()) != null) { lineCounter++; Matcher m = commentPattern.matcher(line); if (m.find()) { line = line.substring(0, m.start()); } statement.append(" "); line = statement.append(line).toString(); line = line.replaceAll("\\s+", " "); line = line.trim(); String[] tokens = line.split(";"); for (int i = 0; i < tokens.length; i++) { tokens[i] = tokens[i].trim(); } boolean containsSemicolon = line.contains(";"); boolean endsWithSemicolon = line.endsWith(";"); if (!containsSemicolon) { continue; } if (tokens.length == 1 && endsWithSemicolon) { statements.add(tokens[0]); statement = new StringBuilder(); continue; } for (int i = 0; i < tokens.length - 1; i++) { statements.add(tokens[0]); statement = new StringBuilder(); } if (endsWithSemicolon) { statements.add(tokens[0]); statement = new StringBuilder(); } else { statement = new StringBuilder(); statement.append(tokens[tokens.length - 1]); } } if (statement != null && statement.toString().trim().length() > 0) throw new UnclosedStatementException("Statement is not closed until the end of the file."); } catch (IOException e) { logger.warn(e); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } return statements; } SqlFileParser(BufferedReader reader);  SqlFileParser(Reader reader);  SqlFileParser(File sqlScript);  SqlFileParser(String filename);  SqlFileParser(InputStream stream); List<String> parse();  }
@Test public void testSimpleSql() { InputStream in = getClass().getResourceAsStream(TEST_FILE_PREFIX + "sqlFileParserTestSimple.sql"); Reader reader = new InputStreamReader(in); SqlFileParser parser = new SqlFileParser(reader); List<String> statements = parser.parse(); assertEquals(statements.size(), 5); int cnt = 0; for (String stmt : statements) { cnt++; assertEquals(" while processing statement " + cnt, "select * from dual", stmt); } }
@Test public void testMultiLineSql() { InputStream in = getClass().getResourceAsStream(TEST_FILE_PREFIX + "sqlFileParserTestMultiline.sql"); Reader reader = new InputStreamReader(in); SqlFileParser parser = new SqlFileParser(reader); List<String> statements = parser.parse(); assertEquals(statements.size(), 5); int cnt = 0; for (String stmt : statements) { cnt++; assertEquals(" while processing statement " + cnt, "select * from dual", stmt); } }
@Test public void testMultipleStatementsPerLine() { InputStream in = getClass() .getResourceAsStream(TEST_FILE_PREFIX + "sqlFileParserTestMultipleStatementsPerLine.sql"); Reader reader = new InputStreamReader(in); SqlFileParser parser = new SqlFileParser(reader); List<String> statements = parser.parse(); assertEquals(statements.size(), 10); int cnt = 0; for (String stmt : statements) { cnt++; assertEquals(" while processing statement " + cnt, "select * from dual", stmt); } }
@Test public void testUnclosedStatement() { InputStream in = getClass().getResourceAsStream(TEST_FILE_PREFIX + "sqlFileParserTestUnclosedStatement.sql"); Reader reader = new InputStreamReader(in); SqlFileParser parser = new SqlFileParser(reader); try { parser.parse(); fail("expected an UnclosedStatementException"); } catch (UnclosedStatementException e) { } }
@Test public void testTrailingWhitespace() { InputStream in = getClass().getResourceAsStream(TEST_FILE_PREFIX + "sqlFileParserTestTrailingWhitespace.sql"); Reader reader = new InputStreamReader(in); SqlFileParser parser = new SqlFileParser(reader); try { parser.parse(); } catch (UnclosedStatementException e) { fail("parser must not throw an UnclosedStatementException here"); } }
@Test public void testMultipleCommentCharacters() { InputStream in = getClass().getResourceAsStream(TEST_FILE_PREFIX + "sqlFileParserTestMultipleCommentChars.sql"); Reader reader = new InputStreamReader(in); SqlFileParser parser = new SqlFileParser(reader); try { List<String> statements = parser.parse(); assertEquals(5, statements.size()); int cnt = 0; for (String stmt : statements) { cnt++; assertEquals(" while processing statement " + cnt, "select * from dual", stmt); } } catch (UnclosedStatementException e) { fail("parser must not throw an UnclosedStatementException here"); } }