SPR-6688: Add tests and explicit detection of \ in sql script extractor
This commit is contained in:
parent
e0ad6500be
commit
645631ad2e
|
|
@ -251,9 +251,21 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
|
|||
private void splitSqlScript(String script, char delim, List<String> statements) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean inLiteral = false;
|
||||
boolean inEscape = false;
|
||||
char[] content = script.toCharArray();
|
||||
for (int i = 0; i < script.length(); i++) {
|
||||
char c = content[i];
|
||||
if (inEscape) {
|
||||
inEscape = false;
|
||||
sb.append(c);
|
||||
continue;
|
||||
}
|
||||
// MySQL style escapes
|
||||
if (c == '\\') {
|
||||
inEscape = true;
|
||||
sb.append(c);
|
||||
continue;
|
||||
}
|
||||
if (c == '\'') {
|
||||
inLiteral = !inLiteral;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ import java.sql.Connection;
|
|||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassRelativeResourceLoader;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
|
@ -43,7 +42,11 @@ public class DatabasePopulatorTests {
|
|||
private final JdbcTemplate jdbcTemplate = new JdbcTemplate(db);
|
||||
|
||||
private void assertTestDatabaseCreated() {
|
||||
assertEquals("Keith", jdbcTemplate.queryForObject("select NAME from T_TEST", String.class));
|
||||
assertTestDatabaseCreated("Keith");
|
||||
}
|
||||
|
||||
private void assertTestDatabaseCreated(String name) {
|
||||
assertEquals(name, jdbcTemplate.queryForObject("select NAME from T_TEST", String.class));
|
||||
}
|
||||
|
||||
private void assertUsersDatabaseCreated(DataSource db) {
|
||||
|
|
@ -71,6 +74,34 @@ public class DatabasePopulatorTests {
|
|||
assertTestDatabaseCreated();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildWithNormalEscapedLiteral() throws Exception {
|
||||
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
|
||||
databasePopulator.addScript(resourceLoader.getResource("db-test-data-escaped-literal.sql"));
|
||||
Connection connection = db.getConnection();
|
||||
try {
|
||||
databasePopulator.populate(connection);
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
assertTestDatabaseCreated("'Keith'");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildWithMySQLEscapedLiteral() throws Exception {
|
||||
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
|
||||
databasePopulator.addScript(resourceLoader.getResource("db-test-data-mysql-escaped-literal.sql"));
|
||||
Connection connection = db.getConnection();
|
||||
try {
|
||||
databasePopulator.populate(connection);
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
assertTestDatabaseCreated("\\$Keith\\$");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scriptWithEolBetweenTokens() throws Exception {
|
||||
databasePopulator.addScript(resourceLoader.getResource("users-schema.sql"));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
drop table T_TEST if exists;
|
||||
|
||||
create table T_TEST (NAME varchar(50) not null);
|
||||
|
|
@ -0,0 +1 @@
|
|||
insert into T_TEST (NAME) values ('''Keith''');
|
||||
|
|
@ -0,0 +1 @@
|
|||
insert into T_TEST (NAME) values ('\$Keith\$');
|
||||
Loading…
Reference in New Issue