private static String processConvertToken(String functionToken,boolean serverSupportsConvertFn,MySQLConnection conn) throws SQLException {
  int firstIndexOfParen=functionToken.indexOf("(");
  if (firstIndexOfParen == -1) {
    throw SQLError.createSQLException("Syntax error while processing {fn convert (... , ...)} token, missing opening parenthesis in token '" + functionToken + "'.",SQLError.SQL_STATE_SYNTAX_ERROR,conn.getExceptionInterceptor());
  }
  int indexOfComma=functionToken.lastIndexOf(",");
  if (indexOfComma == -1) {
    throw SQLError.createSQLException("Syntax error while processing {fn convert (... , ...)} token, missing comma in token '" + functionToken + "'.",SQLError.SQL_STATE_SYNTAX_ERROR,conn.getExceptionInterceptor());
  }
  int indexOfCloseParen=functionToken.indexOf(')',indexOfComma);
  if (indexOfCloseParen == -1) {
    throw SQLError.createSQLException("Syntax error while processing {fn convert (... , ...)} token, missing closing parenthesis in token '" + functionToken + "'.",SQLError.SQL_STATE_SYNTAX_ERROR,conn.getExceptionInterceptor());
  }
  String expression=functionToken.substring(firstIndexOfParen + 1,indexOfComma);
  String type=functionToken.substring(indexOfComma + 1,indexOfCloseParen);
  String newType=null;
  String trimmedType=type.trim();
  if (StringUtils.startsWithIgnoreCase(trimmedType,"SQL_")) {
    trimmedType=trimmedType.substring(4,trimmedType.length());
  }
  if (serverSupportsConvertFn) {
    newType=JDBC_CONVERT_TO_MYSQL_TYPE_MAP.get(trimmedType.toUpperCase(Locale.ENGLISH));
  }
 else {
    newType=JDBC_NO_CONVERT_TO_MYSQL_EXPRESSION_MAP.get(trimmedType.toUpperCase(Locale.ENGLISH));
    if (newType == null) {
      throw SQLError.createSQLException("Can't find conversion re-write for type '" + type + "' that is applicable for this server version while processing escape tokens.",SQLError.SQL_STATE_GENERAL_ERROR,conn.getExceptionInterceptor());
    }
  }
  if (newType == null) {
    throw SQLError.createSQLException("Unsupported conversion type '" + type.trim() + "' found while processing escape token.",SQLError.SQL_STATE_GENERAL_ERROR,conn.getExceptionInterceptor());
  }
  int replaceIndex=newType.indexOf("?");
  if (replaceIndex != -1) {
    StringBuilder convertRewrite=new StringBuilder(newType.substring(0,replaceIndex));
    convertRewrite.append(expression);
    convertRewrite.append(newType.substring(replaceIndex + 1,newType.length()));
    return convertRewrite.toString();
  }
  StringBuilder castRewrite=new StringBuilder("CAST(");
  castRewrite.append(expression);
  castRewrite.append(" AS ");
  castRewrite.append(newType);
  castRewrite.append(")");
  return castRewrite.toString();
}

private static String processConvertToken ( String functionToken , boolean serverSupportsConvertFn , MySQLConnection conn ) throws SQLException { int firstIndexOfParen = functionToken . indexOf ( "(" ) ; if ( firstIndexOfParen == - 1 ) { throw SQLError . createSQLException ( "Syntax error while processing {fn convert (... , ...)} token, missing opening parenthesis in token '" + functionToken + "'." , SQLError . SQL_STATE_SYNTAX_ERROR , conn . getExceptionInterceptor ( ) ) ; } int indexOfComma = functionToken . lastIndexOf ( "," ) ; if ( indexOfComma == - 1 ) { throw SQLError . createSQLException ( "Syntax error while processing {fn convert (... , ...)} token, missing comma in token '" + functionToken + "'." , SQLError . SQL_STATE_SYNTAX_ERROR , conn . getExceptionInterceptor ( ) ) ; } int indexOfCloseParen = functionToken . indexOf ( ')' , indexOfComma ) ; if ( indexOfCloseParen == - 1 ) { throw SQLError . createSQLException ( "Syntax error while processing {fn convert (... , ...)} token, missing closing parenthesis in token '" + functionToken + "'." , SQLError . SQL_STATE_SYNTAX_ERROR , conn . getExceptionInterceptor ( ) ) ; } String expression = functionToken . substring ( firstIndexOfParen + 1 , indexOfComma ) ; String type = functionToken . substring ( indexOfComma + 1 , indexOfCloseParen ) ; String newType = null ; String trimmedType = type . trim ( ) ; if ( StringUtils . startsWithIgnoreCase ( trimmedType , "SQL_" ) ) { trimmedType = trimmedType . substring ( 4 , trimmedType . length ( ) ) ; } if ( serverSupportsConvertFn ) { newType = JDBC_CONVERT_TO_MYSQL_TYPE_MAP . get ( trimmedType . toUpperCase ( Locale . ENGLISH ) ) ; } else { newType = JDBC_NO_CONVERT_TO_MYSQL_EXPRESSION_MAP . get ( trimmedType . toUpperCase ( Locale . ENGLISH ) ) ; if ( newType == null ) { throw SQLError . createSQLException ( "Can't find conversion re-write for type '" + type + "' that is applicable for this server version while processing escape tokens." , SQLError . SQL_STATE_GENERAL_ERROR , conn . getExceptionInterceptor ( ) ) ; } } if ( newType == null ) { throw SQLError . createSQLException ( "Unsupported conversion type '" + type . trim ( ) + "' found while processing escape token." , SQLError . SQL_STATE_GENERAL_ERROR , conn . getExceptionInterceptor ( ) ) ; } int replaceIndex = newType . indexOf ( "?" ) ; if ( replaceIndex != - 1 ) { StringBuilder convertRewrite = new StringBuilder ( newType . substring ( 0 , replaceIndex ) ) ; convertRewrite . append ( expression ) ; convertRewrite . append ( newType . substring ( replaceIndex + 1 , newType . length ( ) ) ) ; return convertRewrite . toString ( ) ; } StringBuilder castRewrite = new StringBuilder ( "CAST(" ) ; castRewrite . append ( expression ) ; castRewrite . append ( " AS " ) ; castRewrite . append ( newType ) ; castRewrite . append ( ")" ) ; return castRewrite . toString ( ) ; }
re - writes { fn convert ( expr , type ) } as cast ( expr as type )
