-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL compound statement awareness in ScriptUtils (#579)
* Fixes #570: Fix handling of SQL compound statements Prevents compound statements from being naively split by ScriptUtils * Simplify contentMatches method * Test for and fix more splitting scenarios * Update modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java Co-Authored-By: rnorth <rich.north@gmail.com>
- Loading branch information
Showing
4 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
modules/database-commons/src/test/java/org/testcontainers/ext/ScriptUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.testcontainers.ext; | ||
|
||
import com.google.common.base.Charsets; | ||
import com.google.common.io.Resources; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ScriptUtilsTest { | ||
|
||
/* | ||
* Test ScriptUtils script splitting with some ugly/hard-to-split cases | ||
*/ | ||
@Test | ||
public void testSplit() throws IOException { | ||
final String script = Resources.toString(Resources.getResource("splittable.sql"), Charsets.UTF_8); | ||
|
||
final List<String> statements = new ArrayList<>(); | ||
ScriptUtils.splitSqlScript("resourcename", script, ";", "--", "/*", "*/", statements); | ||
|
||
assertEquals(7, statements.size()); | ||
assertEquals("SELECT \"a /* string literal containing comment characters like -- here\"", statements.get(2)); | ||
assertEquals("SELECT \"a 'quoting' \\\"scenario ` involving BEGIN keyword\\\" here\"", statements.get(3)); | ||
assertEquals("SELECT * from `bar`", statements.get(4)); | ||
assertEquals("INSERT INTO bar (foo) VALUES ('hello world')", statements.get(6)); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
modules/database-commons/src/test/resources/splittable.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
CREATE TABLE bar ( | ||
foo VARCHAR(255) | ||
); | ||
|
||
DROP PROCEDURE IF EXISTS -- ; | ||
count_foo; | ||
|
||
SELECT "a /* string literal containing comment characters like -- here"; | ||
SELECT "a 'quoting' \"scenario ` involving BEGIN keyword\" here"; | ||
SELECT * from `bar`; | ||
-- What about a line comment containing imbalanced string delimiters? " | ||
CREATE PROCEDURE count_foo() | ||
BEGIN | ||
BEGIN | ||
SELECT * | ||
FROM bar; | ||
SELECT 1 | ||
FROM dual; | ||
END; | ||
BEGIN | ||
select * from bar; | ||
END; | ||
-- we can do comments | ||
/* including block | ||
comments | ||
*/ | ||
/* what if BEGIN appears inside a comment? */ | ||
select "or what if BEGIN appears inside a literal?"; | ||
END /*; */; | ||
/* or a block comment | ||
containing imbalanced string delimiters? | ||
' " | ||
*/ | ||
INSERT INTO bar (foo) /* ; */ VALUES ('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters