diff --git a/SHiNE-server/shine-server-db/src/main/java/shine/db/DatabaseInitializer.java b/SHiNE-server/shine-server-db/src/main/java/shine/db/DatabaseInitializer.java index ca921ac0..fd4c7603 100644 --- a/SHiNE-server/shine-server-db/src/main/java/shine/db/DatabaseInitializer.java +++ b/SHiNE-server/shine-server-db/src/main/java/shine/db/DatabaseInitializer.java @@ -787,33 +787,88 @@ public final class DatabaseInitializer { List statements = new ArrayList<>(); StringBuilder current = new StringBuilder(); boolean inSingleQuote = false; + boolean inLineComment = false; + boolean inBlockComment = false; String dollarQuoteTag = null; for (int i = 0; i < sqlScript.length(); i++) { char ch = sqlScript.charAt(i); - if (dollarQuoteTag == null && ch == '\'' && !isEscapedSingleQuote(sqlScript, i)) { - inSingleQuote = !inSingleQuote; + if (inLineComment) { + current.append(ch); + if (ch == '\n') { + inLineComment = false; + } + continue; + } + + if (inBlockComment) { + current.append(ch); + if (ch == '*' && i + 1 < sqlScript.length() && sqlScript.charAt(i + 1) == '/') { + current.append('/'); + i += 1; + inBlockComment = false; + } + continue; + } + + if (dollarQuoteTag != null) { + if (startsWithAt(sqlScript, i, dollarQuoteTag)) { + current.append(dollarQuoteTag); + i += dollarQuoteTag.length() - 1; + dollarQuoteTag = null; + } else { + current.append(ch); + } + continue; + } + + if (inSingleQuote) { + current.append(ch); + if (ch == '\'') { + if (i + 1 < sqlScript.length() && sqlScript.charAt(i + 1) == '\'') { + current.append('\''); + i += 1; + } else { + inSingleQuote = false; + } + } + continue; + } + + if (ch == '-' && i + 1 < sqlScript.length() && sqlScript.charAt(i + 1) == '-') { + current.append("--"); + i += 1; + inLineComment = true; + continue; + } + + if (ch == '/' && i + 1 < sqlScript.length() && sqlScript.charAt(i + 1) == '*') { + current.append("/*"); + i += 1; + inBlockComment = true; + continue; + } + + if (ch == '\'') { + inSingleQuote = true; current.append(ch); continue; } - if (!inSingleQuote) { - String tag = readDollarQuoteTag(sqlScript, i); - if (tag != null) { - current.append(tag); - i += tag.length() - 1; - if (dollarQuoteTag == null) { - dollarQuoteTag = tag; - } else if (dollarQuoteTag.equals(tag)) { - dollarQuoteTag = null; - } - continue; - } + String tag = readDollarQuoteTag(sqlScript, i); + if (tag != null) { + current.append(tag); + i += tag.length() - 1; + dollarQuoteTag = tag; + continue; } - if (ch == ';' && !inSingleQuote && dollarQuoteTag == null) { - statements.add(current.toString()); + if (ch == ';') { + String statement = current.toString().trim(); + if (!statement.isEmpty()) { + statements.add(current.toString()); + } current.setLength(0); continue; } @@ -822,13 +877,16 @@ public final class DatabaseInitializer { } if (!current.isEmpty()) { - statements.add(current.toString()); + String statement = current.toString().trim(); + if (!statement.isEmpty()) { + statements.add(current.toString()); + } } return statements; } - private static boolean isEscapedSingleQuote(String sqlScript, int index) { - return index + 1 < sqlScript.length() && sqlScript.charAt(index + 1) == '\''; + private static boolean startsWithAt(String sqlScript, int index, String token) { + return sqlScript.regionMatches(index, token, 0, token.length()); } private static String readDollarQuoteTag(String sqlScript, int index) { diff --git a/VERSION.properties b/VERSION.properties index 36ef3dfe..043db45b 100644 --- a/VERSION.properties +++ b/VERSION.properties @@ -1,2 +1,2 @@ client.version=1.2.354 -server.version=1.2.320 +server.version=1.2.321