in src/main/java/org/mariadb/jdbc/internal/util/Utils.java [477:584]
public static String nativeSql(String sql, Protocol protocol) throws SQLException {
if (!sql.contains("{")) {
return sql;
}
StringBuilder escapeSequenceBuf = new StringBuilder();
StringBuilder sqlBuffer = new StringBuilder();
char[] charArray = sql.toCharArray();
char lastChar = 0;
boolean inQuote = false;
char quoteChar = 0;
boolean inComment = false;
boolean isSlashSlashComment = false;
int inEscapeSeq = 0;
for (int i = 0; i < charArray.length; i++) {
char car = charArray[i];
if (lastChar == '\\' && !protocol.noBackslashEscapes()) {
sqlBuffer.append(car);
// avoid considering escaped backslash as a futur escape character
lastChar = ' ';
continue;
}
switch (car) {
case '\'':
case '"':
case '`':
if (!inComment) {
if (inQuote) {
if (quoteChar == car) {
inQuote = false;
}
} else {
inQuote = true;
quoteChar = car;
}
}
break;
case '*':
if (!inQuote && !inComment && lastChar == '/') {
inComment = true;
isSlashSlashComment = false;
}
break;
case '/':
case '-':
if (!inQuote) {
if (inComment) {
if (lastChar == '*' && !isSlashSlashComment) {
inComment = false;
} else if (lastChar == car && isSlashSlashComment) {
inComment = false;
}
} else {
if (lastChar == car) {
inComment = true;
isSlashSlashComment = true;
} else if (lastChar == '*') {
inComment = true;
isSlashSlashComment = false;
}
}
}
break;
case '\n':
if (inComment && isSlashSlashComment) {
// slash-slash and dash-dash comments ends with the end of line
inComment = false;
}
break;
case '{':
if (!inQuote && !inComment) {
inEscapeSeq++;
}
break;
case '}':
if (!inQuote && !inComment) {
inEscapeSeq--;
if (inEscapeSeq == 0) {
escapeSequenceBuf.append(car);
sqlBuffer.append(resolveEscapes(escapeSequenceBuf.toString(), protocol));
escapeSequenceBuf.setLength(0);
lastChar = car;
continue;
}
}
break;
default:
break;
}
lastChar = car;
if (inEscapeSeq > 0) {
escapeSequenceBuf.append(car);
} else {
sqlBuffer.append(car);
}
}
if (inEscapeSeq > 0) {
throw new SQLException(
"Invalid escape sequence , missing closing '}' character in '" + sqlBuffer);
}
return sqlBuffer.toString();
}