Skip to content

Commit

Permalink
WIP: refs #570
Browse files Browse the repository at this point in the history
  • Loading branch information
rnorth committed Feb 3, 2018
1 parent 198fa8c commit ca506a2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public static void splitSqlScript(String resource, String script, String separat
StringBuilder sb = new StringBuilder();
boolean inLiteral = false;
boolean inEscape = false;
int blockDepth = 0;
char[] content = script.toCharArray();
for (int i = 0; i < script.length(); i++) {
char c = content[i];
Expand All @@ -133,7 +134,13 @@ public static void splitSqlScript(String resource, String script, String separat
if (c == '\'') {
inLiteral = !inLiteral;
}
if (!inLiteral) {
if (contentMatches(content, i, "BEGIN")) {
blockDepth++;
}
if (contentMatches(content, i, "END")) {
blockDepth--;
}
if (!inLiteral && blockDepth == 0) {
if (script.startsWith(separator, i)) {
// we've reached the end of the current statement
if (sb.length() > 0) {
Expand Down Expand Up @@ -185,6 +192,21 @@ else if (c == ' ' || c == '\n' || c == '\t') {
}
}

private static boolean contentMatches(char[] content, int offset, String needle) {
final char[] needleChars = needle.toCharArray();
final int end = offset + needleChars.length;
if (content.length < end) {
return false;
}

for (int i = 0; i < needleChars.length; i++) {
if (Character.toLowerCase(content[offset + i]) != Character.toLowerCase(needleChars[i])) {
return false;
}
}
return true;
}

private static void checkArgument(boolean expression, String errorMessage) {
if (!expression) {
throw new IllegalArgumentException(errorMessage);
Expand Down
18 changes: 17 additions & 1 deletion modules/jdbc-test/src/test/resources/somepath/init_mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,20 @@ CREATE TABLE bar (
foo VARCHAR(255)
);

INSERT INTO bar (foo) VALUES ('hello world');
INSERT INTO bar (foo) VALUES ('hello world');

DROP PROCEDURE IF EXISTS count_foo;

CREATE PROCEDURE count_foo()
BEGIN

-- we can do comments

/* including block
comments
*/

select * from bar;
select 1 from dual;

END;

0 comments on commit ca506a2

Please sign in to comment.