Support comments in statements in RDbPopulator

Prior to this commit, executing an SQL script via
ResourceDatabasePopulator 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-10075
This commit is contained in:
Sam Brannen
2012-12-05 17:41:24 +01:00
parent d0f687f028
commit aa16caa2ac
3 changed files with 75 additions and 37 deletions

View File

@@ -59,9 +59,11 @@ public class DatabasePopulatorTests {
assertEquals(name, jdbcTemplate.queryForObject("select NAME from T_TEST", String.class));
}
private void assertUsersDatabaseCreated() {
assertEquals("Sam", jdbcTemplate.queryForObject("select first_name from users where last_name = 'Brannen'",
String.class));
private void assertUsersDatabaseCreated(String... lastNames) {
for (String lastName : lastNames) {
assertEquals("Did not find user with last name [" + lastName + "].", 1,
jdbcTemplate.queryForInt("select count(0) from users where last_name = ?", lastName));
}
}
@After
@@ -73,7 +75,6 @@ public class DatabasePopulatorTests {
db.shutdown();
}
@Test
public void testBuildWithCommentsAndFailedDrop() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema-failed-drop-comments.sql"));
@@ -215,7 +216,22 @@ public class DatabasePopulatorTests {
connection.close();
}
assertUsersDatabaseCreated();
assertUsersDatabaseCreated("Brannen");
}
@Test
public void scriptWithCommentsWithinStatements() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("users-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("users-data-with-comments.sql"));
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
}
finally {
connection.close();
}
assertUsersDatabaseCreated("Brannen", "Hoeller");
}
@Test