private void testRewriteMultiPacket2param(boolean rewritableMulti) throws SQLException {
  Statement statement=sharedConnection.createStatement();
  statement.execute("TRUNCATE PreparedStatementTest1");
  ResultSet rs=statement.executeQuery("select @@max_allowed_packet");
  rs.next();
  int maxAllowedPacket=rs.getInt(1);
  if (maxAllowedPacket < 21000000) {
    String query="INSERT INTO PreparedStatementTest1 VALUES (null, ?)" + (rewritableMulti ? "" : " ON DUPLICATE KEY UPDATE id=?");
    char[] arr=new char[(maxAllowedPacket - (query.length() + 18)) / 2];
    for (int i=0; i < arr.length; i++) {
      arr[i]=(char)('a' + (i % 10));
    }
    try (Connection connection=setConnection("&rewriteBatchedStatements=true&profileSql=true")){
      PreparedStatement pstmt=connection.prepareStatement(query);
      for (int i=0; i < 4; i++) {
        pstmt.setString(1,new String(arr));
        if (!rewritableMulti)         pstmt.setInt(2,1);
        pstmt.addBatch();
      }
      int[] results=pstmt.executeBatch();
      assertEquals(4,results.length);
      for (      int result : results)       assertEquals(1,result);
    }
     rs=statement.executeQuery("select * from PreparedStatementTest1");
    int counter=0;
    while (rs.next()) {
      counter++;
      byte[] newBytes=rs.getBytes(2);
      assertEquals(arr.length,newBytes.length);
      for (int i=0; i < arr.length; i++) {
        assertEquals(arr[i],newBytes[i]);
      }
    }
    assertEquals(4,counter);
  }
}

private void testRewriteMultiPacket2param ( boolean rewritableMulti ) throws SQLException { Statement statement = sharedConnection . createStatement ( ) ; statement . execute ( "TRUNCATE PreparedStatementTest1" ) ; ResultSet rs = statement . executeQuery ( "select @@max_allowed_packet" ) ; rs . next ( ) ; int maxAllowedPacket = rs . getInt ( 1 ) ; if ( maxAllowedPacket < 21000000 ) { String query = "INSERT INTO PreparedStatementTest1 VALUES (null, ?)" + ( rewritableMulti ? "" : " ON DUPLICATE KEY UPDATE id=?" ) ; char [ ] arr = new char [ ( maxAllowedPacket - ( query . length ( ) + 18 ) ) / 2 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = ( char ) ( 'a' + ( i % 10 ) ) ; } try ( Connection connection = setConnection ( "&rewriteBatchedStatements=true&profileSql=true" ) ) { PreparedStatement pstmt = connection . prepareStatement ( query ) ; for ( int i = 0 ; i < 4 ; i ++ ) { pstmt . setString ( 1 , new String ( arr ) ) ; if ( ! rewritableMulti ) pstmt . setInt ( 2 , 1 ) ; pstmt . addBatch ( ) ; } int [ ] results = pstmt . executeBatch ( ) ; assertEquals ( 4 , results . length ) ; for ( int result : results ) assertEquals ( 1 , result ) ; } rs = statement . executeQuery ( "select * from PreparedStatementTest1" ) ; int counter = 0 ; while ( rs . next ( ) ) { counter ++ ; byte [ ] newBytes = rs . getBytes ( 2 ) ; assertEquals ( arr . length , newBytes . length ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { assertEquals ( arr [ i ] , newBytes [ i ] ) ; } } assertEquals ( 4 , counter ) ; } }
goal is send rewritten query with 2 parameters with size exacting max_allowed_packet .
