Support comments in statements in JdbcTestUtils

Prior to this commit, executing an SQL script with JdbcTestUtils would
fail if a statement in the script contained a line comment within the
statement.

This commit ensures that standard SQL comments (i.e., any text beginning
with two hyphens and extending to the end of the line) are properly
omitted from the statement before executing it.

In addition, multiple adjacent whitespace characters within a statement
but outside a literal are now collapsed into a single space.

Issue: SPR-9982
This commit is contained in:
Sam Brannen
2012-12-05 16:58:26 +01:00
parent d1a6ceecc2
commit d0f687f028
3 changed files with 65 additions and 33 deletions

View File

@@ -86,20 +86,22 @@ public class JdbcTestUtilsTests {
LineNumberReader lineNumberReader = new LineNumberReader(resource.getReader());
String script = JdbcTestUtils.readScript(lineNumberReader);
assertFalse("script should not contain --", script.contains("--"));
char delim = ';';
List<String> statements = new ArrayList<String>();
JdbcTestUtils.splitSqlScript(script, delim, statements);
String statement1 = "insert into customer (id, name) values (1, 'Rod; Johnson'), (2, 'Adrian Collier')";
String statement2 = " insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
String statement3 = " insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
String statement3 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
// Statement 4 addresses the error described in SPR-9982.
String statement4 = "INSERT INTO persons( person_id , name) VALUES( 1 , 'Name' )";
assertEquals("wrong number of statements", 3, statements.size());
assertEquals("wrong number of statements", 4, statements.size());
assertEquals("statement 1 not split correctly", statement1, statements.get(0));
assertEquals("statement 2 not split correctly", statement2, statements.get(1));
assertEquals("statement 3 not split correctly", statement3, statements.get(2));
assertEquals("statement 4 not split correctly", statement4, statements.get(3));
}
}