Skip to content

Commit 771a05e

Browse files
chrunchyjesussbrannen
chrunchyjesus
authored andcommitted
Fix ScriptUtils for MS Windows line ending
Prior to this commit, ScriptUtils did not properly split SQL scripts that contained line endings for MS Windows. Closes gh-23019
1 parent 5f75413 commit 771a05e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ else if (script.startsWith(blockCommentStartDelimiter, i)) {
234234
"Missing block comment end delimiter: " + blockCommentEndDelimiter, resource);
235235
}
236236
}
237-
else if (c == ' ' || c == '\n' || c == '\t') {
237+
else if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
238238
// Avoid multiple adjacent whitespace characters
239239
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') {
240240
c = ' ';

spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ public void readAndSplitScriptContainingComments() throws Exception {
132132
assertEquals("statement 4 not split correctly", statement4, statements.get(3));
133133
}
134134

135+
@Test
136+
public void readAndSplitScriptContainingCommentsWithWindowsLineEnding() throws Exception {
137+
String script = readScript("test-data-with-comments.sql").replaceAll("\n", "\r\n");
138+
List<String> statements = new ArrayList<>();
139+
splitSqlScript(script, ';', statements);
140+
141+
String statement1 = "insert into customer (id, name) values (1, 'Rod; Johnson'), (2, 'Adrian Collier')";
142+
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
143+
String statement3 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
144+
// Statement 4 addresses the error described in SPR-9982.
145+
String statement4 = "INSERT INTO persons( person_id , name) VALUES( 1 , 'Name' )";
146+
147+
assertEquals("wrong number of statements", 4, statements.size());
148+
assertEquals("statement 1 not split correctly", statement1, statements.get(0));
149+
assertEquals("statement 2 not split correctly", statement2, statements.get(1));
150+
assertEquals("statement 3 not split correctly", statement3, statements.get(2));
151+
assertEquals("statement 4 not split correctly", statement4, statements.get(3));
152+
}
153+
135154
@Test // SPR-10330
136155
public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Exception {
137156
String script = readScript("test-data-with-comments-and-leading-tabs.sql");

0 commit comments

Comments
 (0)