diff --git a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/DBUtilities.java b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/DBUtilities.java index dc58b02a009ff5097c08ca830f9410474f6b1342..caee74971b4934a4ba3d935475fad6e5b798dab3 100644 --- a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/DBUtilities.java +++ b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/DBUtilities.java @@ -84,17 +84,29 @@ public class DBUtilities { line = line.substring(0, commentStart); } - statement.append(line.trim()); + int startIdx = 0; + for (int endIdx = line.indexOf(';') + 1; endIdx > 0; startIdx = endIdx, endIdx = + line.indexOf(';', startIdx) + 1) + { + statement.append(line.substring(startIdx, endIdx).trim()); + addToStatements(statements, statement); + } + statement.append(line.substring(startIdx).trim()); statement.append(' '); if (statement.length() > 1 && statement.charAt(statement.length() - 2) == ';') { - final String statementStr = - statement.toString().trim().replaceAll("\\s+", " ").replaceAll("\\)(\\w)", - ") $1").replaceAll("\\s+;", ";"); - statements.add(statementStr); - statement.setLength(0); + addToStatements(statements, statement); } } return statements; } + + private static void addToStatements(final List<String> statements, StringBuilder statement) + { + final String statementStr = + statement.toString().trim().replaceAll("\\s+", " ").replaceAll("\\)(\\w)", ") $1").replaceAll("\\s+;", + ";"); + statements.add(statementStr); + statement.setLength(0); + } } diff --git a/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/DBUtilitiesTest.java b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/DBUtilitiesTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bdc5057696b845f96e45a54580165793aae167ed --- /dev/null +++ b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/DBUtilitiesTest.java @@ -0,0 +1,75 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.dbmigration; + +import java.util.Arrays; + +import org.testng.annotations.Test; + +import static org.testng.AssertJUnit.*; + +/** + * Test cases for {@link DBUtilities}. + * + * @author Bernd Rinn + */ +public class DBUtilitiesTest +{ + + @Test + public void testSplitSqlStatementsSpaceLeftRight() + { + assertEquals(Arrays.asList("statement1;", "statement2;"), DBUtilities + .splitSqlStatements("statement1 ; statement2;")); + } + + @Test + public void testSplitSqlStatementsSpaceRight() + { + assertEquals(Arrays.asList("statement1;", "statement2;"), DBUtilities + .splitSqlStatements("statement1; statement2;")); + } + + @Test + public void testSplitSqlStatementsNoSpaces() + { + assertEquals(Arrays.asList("statement1;", "statement2;"), DBUtilities + .splitSqlStatements("statement1;statement2;")); + } + + @Test + public void testSplitSqlStatementsNewLine() + { + assertEquals(Arrays.asList("statement1;", "statement2;"), DBUtilities + .splitSqlStatements("statement1 ;\n statement2 ;\n")); + } + + @Test + public void testSplitSqlStatementsOneStatementOnMultipleLines() + { + assertEquals(Arrays.asList("statement part1 statement part2;", "statement2.1 statement2.2;"), DBUtilities + .splitSqlStatements("statement part1 \n statement part2 ;\n statement2.1\nstatement2.2 ; ")); + } + + @Test + public void testSplitSqlStatementsComments() + { + assertEquals(Arrays.asList("statement1 statement2;"), DBUtilities + .splitSqlStatements("statement1 -- comment\n -- just comments; bla\n statement2; -- another comment")); + } + +}