--- sql/handler.cc +++ sql/handler.cc @@ -1885,6 +1885,10 @@ { return HA_ADMIN_NEEDS_ALTER; } + if ((*field)->type() == FIELD_TYPE_VAR_STRING) + { + return HA_ADMIN_NEEDS_ALTER; + } } } return 0; --- client/mysqlcheck.c +++ client/mysqlcheck.c @@ -515,6 +515,87 @@ return 0; } /* use_db */ +/* + * remove trailing spaces / zeroes from pre-5.0 varbinary fields + * actually, this doesn't belog here but into the server (REPAIR TABLE), + */ +static int repair_varbinary(char *table, uint length) +{ + MYSQL_RES *res; + MYSQL_ROW row; + char *query, message[100], *ptr; + uint query_length= 0, num_cols; + + if (opt_all_in_1) + { + fputs("repair_varbinary doesn't work with --all-in-1\n", stderr); + return 1; + } + /* SHOW COLUMNS FROM `db`.`table` */ + query_length= sizeof("SHOW COLUMNS FROM ````")-1 + length; + if (!(query =(char *) my_malloc(query_length+1, MYF(MY_WME)))) + return 1; + ptr= strmov(query, "SHOW COLUMNS FROM "); + ptr= fix_table_name(ptr, table); + if (mysql_real_query(sock, query, query_length)) + { + sprintf(message, "when executing '%s'", query); + DBerror(sock, message); + return 1; + } + my_free(query, MYF(0)); + + /* UPDATE `db`.`table` SET `col1`=TRIM(TRAILING '\0' FROM `col1`), + * `col1`=RTIM(`col1`), + * `col2`= ... + */ + res= mysql_store_result(sock); + + /* first compute query_length */ + query_length= sizeof("UPDATE ```` SET ")-1 + length; + num_cols = 0; + while ((row= mysql_fetch_row(res))) + { + if (!strncmp(row[1], "varbinary(", sizeof("varbinary(")-1)) + { + query_length += sizeof("``=TRIM(TRAILING '\\0' FROM ``), ``=RTRIM(``), ") + - 1 + 4 * strlen(row[0]); + num_cols++; + } + } + if (!num_cols) { + /* nothing to do */ + return 0; + } + query_length -= 2; /* no comma-space after last RTRIM */ + + /* then rewind and construct the query */ + if (!(query= (char *) my_malloc(query_length+1, MYF(MY_WME)))) + return 1; + mysql_data_seek(res, 0); + ptr= strmov(query, "UPDATE "); + ptr= fix_table_name(ptr, table); + ptr= strmov(ptr, " SET "); + while ((row= mysql_fetch_row(res))) + { + if (!strncmp(row[1], "varbinary(", sizeof("varbinary(")-1)) + { + ptr= strxmov(ptr, "`", row[0], "`=TRIM(TRAILING '\\0' FROM `", + row[0], "`), `", row[0], "`=RTRIM(`", row[0], "`)", NullS); + if (--num_cols) + ptr= strmov(ptr, ", "); + } + } + if (mysql_real_query(sock, query, query_length)) + { + sprintf(message, "when executing '%s'", query); + DBerror(sock, message); + my_free(query, MYF(0)); + return 1; + } + my_free(query, MYF(0)); + return 0; +} static int handle_request_for_tables(char *tables, uint length) { @@ -573,6 +654,8 @@ } print_result(); my_free(query, MYF(0)); + if (what_to_do == DO_REPAIR && repair_varbinary(tables, length) != 0) + return 1; return 0; }