public static ClientPrepareResult parameterParts(String queryString, boolean noBackslashEscapes) {
    try {
      boolean reWritablePrepare = false;
      boolean multipleQueriesPrepare = true;
      List<byte[]> partList = new ArrayList<>();
      LexState state = LexState.Normal;
      char lastChar = '\0';
      boolean endingSemicolon = false;

      boolean singleQuotes = false;
      int lastParameterPosition = 0;

      char[] query = queryString.toCharArray();
      int queryLength = query.length;
      for (int i = 0; i < queryLength; i++) {

        char car = query[i];
        if (state == LexState.Escape && !((car == '\'' && singleQuotes) || (car == '"'
            && !singleQuotes))) {
          state = LexState.String;
          lastChar = car;
          continue;
        }
        switch (car) {
          case '*':
            if (state == LexState.Normal && lastChar == '/') {
              state = LexState.SlashStarComment;
            }
            break;

          case '/':
            if (state == LexState.SlashStarComment && lastChar == '*') {
              state = LexState.Normal;
            } else if (state == LexState.Normal && lastChar == '/') {
              state = LexState.EOLComment;
            }
            break;

          case '#':
            if (state == LexState.Normal) {
              state = LexState.EOLComment;
            }
            break;

          case '-':
            if (state == LexState.Normal && lastChar == '-') {
              state = LexState.EOLComment;
              multipleQueriesPrepare = false;
            }
            break;

          case '\n':
            if (state == LexState.EOLComment) {
              multipleQueriesPrepare = true;
              state = LexState.Normal;
            }
            break;

          case '"':
            if (state == LexState.Normal) {
              state = LexState.String;
              singleQuotes = false;
            } else if (state == LexState.String && !singleQuotes) {
              state = LexState.Normal;
            } else if (state == LexState.Escape && !singleQuotes) {
              state = LexState.String;
            }
            break;

          case '\'':
            if (state == LexState.Normal) {
              state = LexState.String;
              singleQuotes = true;
            } else if (state == LexState.String && singleQuotes) {
              state = LexState.Normal;
            } else if (state == LexState.Escape && singleQuotes) {
              state = LexState.String;
            }
            break;

          case '\\':
            if (noBackslashEscapes) {
              break;
            }
            if (state == LexState.String) {
              state = LexState.Escape;
            }
            break;
          case ';':
            if (state == LexState.Normal) {
              endingSemicolon = true;
              multipleQueriesPrepare = false;
            }
            break;
          case '?':
            if (state == LexState.Normal) {
              partList.add(queryString.substring(lastParameterPosition, i).getBytes("UTF-8"));
              lastParameterPosition = i + 1;
            }
            break;
          case '`':
            if (state == LexState.Backtick) {
              state = LexState.Normal;
            } else if (state == LexState.Normal) {
              state = LexState.Backtick;
            }
            break;
          default:
            //multiple queries
            if (state == LexState.Normal && endingSemicolon && ((byte) car >= 40)) {
              endingSemicolon = false;
              multipleQueriesPrepare = true;
            }
            break;
        }
        lastChar = car;
      }
      if (lastParameterPosition == 0) {
        partList.add(queryString.getBytes("UTF-8"));
      } else {
        partList.add(queryString.substring(lastParameterPosition, queryLength).getBytes("UTF-8"));
      }

      return new ClientPrepareResult(queryString, partList, reWritablePrepare,
          multipleQueriesPrepare, false);
    } catch (UnsupportedEncodingException u) {
      //cannot happen
      return null;
    }

  }
public static ClientPrepareResult parameterParts ( String queryString , boolean noBackslashEscapes ) { try { boolean reWritablePrepare = false ; boolean multipleQueriesPrepare = true ; List < byte [ ] > partList = new ArrayList < > ( ) ; LexState state = LexState . Normal ; char lastChar = '\0' ; boolean endingSemicolon = false ; boolean singleQuotes = false ; int lastParameterPosition = 0 ; char [ ] query = queryString . toCharArray ( ) ; int queryLength = query . length ; for ( int i = 0 ; i < queryLength ; i ++ ) { char car = query [ i ] ; if ( state == LexState . Escape && ! ( ( car == '\'' && singleQuotes ) || ( car == '"' && ! singleQuotes ) ) ) { state = LexState . String ; lastChar = car ; continue ; } switch ( car ) { case '*' : if ( state == LexState . Normal && lastChar == '/' ) { state = LexState . SlashStarComment ; } break ; case '/' : if ( state == LexState . SlashStarComment && lastChar == '*' ) { state = LexState . Normal ; } else if ( state == LexState . Normal && lastChar == '/' ) { state = LexState . EOLComment ; } break ; case '#' : if ( state == LexState . Normal ) { state = LexState . EOLComment ; } break ; case '-' : if ( state == LexState . Normal && lastChar == '-' ) { state = LexState . EOLComment ; multipleQueriesPrepare = false ; } break ; case '\n' : if ( state == LexState . EOLComment ) { multipleQueriesPrepare = true ; state = LexState . Normal ; } break ; case '"' : if ( state == LexState . Normal ) { state = LexState . String ; singleQuotes = false ; } else if ( state == LexState . String && ! singleQuotes ) { state = LexState . Normal ; } else if ( state == LexState . Escape && ! singleQuotes ) { state = LexState . String ; } break ; case '\'' : if ( state == LexState . Normal ) { state = LexState . String ; singleQuotes = true ; } else if ( state == LexState . String && singleQuotes ) { state = LexState . Normal ; } else if ( state == LexState . Escape && singleQuotes ) { state = LexState . String ; } break ; case '\\' : if ( noBackslashEscapes ) { break ; } if ( state == LexState . String ) { state = LexState . Escape ; } break ; case ';' : if ( state == LexState . Normal ) { endingSemicolon = true ; multipleQueriesPrepare = false ; } break ; case '?' : if ( state == LexState . Normal ) { partList . add ( queryString . substring ( lastParameterPosition , i ) . getBytes ( "UTF-8" ) ) ; lastParameterPosition = i + 1 ; } break ; case '`' : if ( state == LexState . Backtick ) { state = LexState . Normal ; } else if ( state == LexState . Normal ) { state = LexState . Backtick ; } break ; default : if ( state == LexState . Normal && endingSemicolon && ( ( byte ) car >= 40 ) ) { endingSemicolon = false ; multipleQueriesPrepare = true ; } break ; } lastChar = car ; } if ( lastParameterPosition == 0 ) { partList . add ( queryString . getBytes ( "UTF-8" ) ) ; } else { partList . add ( queryString . substring ( lastParameterPosition , queryLength ) . getBytes ( "UTF-8" ) ) ; } return new ClientPrepareResult ( queryString , partList , reWritablePrepare , multipleQueriesPrepare , false ) ; } catch ( UnsupportedEncodingException u ) { return null ; } }
Separate query in a String list and set flag isQueryMultipleRewritable. The resulting string
list is separed by ? that are not in comments. isQueryMultipleRewritable flag is set if query
can be rewrite in one query (all case but if using "-- comment"). example for query : "INSERT
INTO tableName(id, name) VALUES (?, ?)" result list will be : {"INSERT INTO tableName(id, name)
VALUES (", ", ", ")"}

@param queryString        query
@param noBackslashEscapes escape mode
@return ClientPrepareResult