docs: @SuppressWarnings("ConstantConditions")
    public static String nativeSql(String sql, boolean noBackslashEscapes) 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 == '\\' && !noBackslashEscapes) {
          sqlBuffer.append(car);
          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 'S':
            // skip SQL_xxx and SQL_TSI_xxx in functions
            // This would convert e.g SQL_INTEGER => INTEGER, SQL_TSI_HOUR=>HOUR
  
            if (!inQuote && !inComment && inEscapeSeq > 0
                && i + 4 < charArray.length && charArray[i + 1] == 'Q'
                && charArray[i + 2] == 'L' && charArray[i + 3] == 'L'
                && charArray[i + 4] == '_') {
  
              if (i + 8 < charArray.length
                  && charArray[i + 5] == 'T'
                  && charArray[i + 6] == 'S'
                  && charArray[i + 7] == 'I'
                  && charArray[i + 8] == '_') {
                i += 8;
                continue;
              }
              i += 4;
              continue;
            }
            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(), noBackslashEscapes));
                escapeSequenceBuf.setLength(0);
                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();
    }