From 152db93ba10830f274c89f04d879549568c693cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Mon, 26 Dec 2016 11:34:55 +0100 Subject: [PATCH 1/3] Print blob and varchar as hex literal in vertical output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Example: mysql> create table t1(id binary(16) primary key, name varchar(100)); Query OK, 0 rows affected (0.05 sec) mysql> insert into t1 values(uuid_to_bin(uuid()), 'test 1'); Query OK, 1 row affected (0.01 sec) mysql> insert into t1 values(uuid_to_bin(uuid()), 'test 2'); Query OK, 1 row affected (0.01 sec) mysql> insert into t1 values(uuid_to_bin(uuid()), 'test 3'); Query OK, 1 row affected (0.01 sec) mysql> select * from t1; +------------------+--------+ | id | name | +------------------+--------+ | � �`��w���}4 | test 1 | | "3l��`��w���}4 | test 2 | | $����`��w���}4 | test 3 | +------------------+--------+ 3 rows in set (0.00 sec) mysql> select * from t1\G *************************** 1. row *************************** id: 0x20D7170ACB6011E6BD77AC87A3027D34 name: test 1 *************************** 2. row *************************** id: 0x22336CBDCB6011E6BD77AC87A3027D34 name: test 2 *************************** 3. row *************************** id: 0x24A2AD93CB6011E6BD77AC87A3027D34 name: test 3 3 rows in set (0.00 sec) mysql> select * from t1 where id=0x22336CBDCB6011E6BD77AC87A3027D34; +------------------+--------+ | id | name | +------------------+--------+ | "3l��`��w���}4 | test 2 | +------------------+--------+ 1 row in set (0.00 sec) --- client/mysql.cc | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/client/mysql.cc b/client/mysql.cc index 810db2e..700057c 100644 --- a/client/mysql.cc +++ b/client/mysql.cc @@ -4170,6 +4170,14 @@ print_table_data_xml(MYSQL_RES *result) } +static void print_blob_as_hex(FILE *output_file, const char *str, ulong len) +{ + const char *ptr= str, *end= ptr + len; + for (; ptr < end ; ptr++) + fprintf(output_file, "%02X", *((uchar *)ptr)); +} + + static void print_table_data_vertically(MYSQL_RES *result) { @@ -4203,8 +4211,19 @@ print_table_data_vertically(MYSQL_RES *result) tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name); if (cur[off]) { - tee_write(PAGER, cur[off], lengths[off], MY_PRINT_SPS_0 | MY_PRINT_MB); - tee_putc('\n', PAGER); + if ((field->type == MYSQL_TYPE_BLOB) + || ((field->type == MYSQL_TYPE_VAR_STRING) && (field->charsetnr == 63)) + || ((field->type == MYSQL_TYPE_STRING) && (field->charsetnr == 63))) + { + tee_fprintf(PAGER, "0x"); + print_blob_as_hex(PAGER, cur[off], lengths[off]); + tee_putc('\n', PAGER); + } + else + { + tee_write(PAGER, cur[off], lengths[off], MY_PRINT_SPS_0 | MY_PRINT_MB); + tee_putc('\n', PAGER); + } } else tee_fprintf(PAGER, "NULL\n"); From 606604dd9ac0f0e55a0cadae177eb072108cb9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Sun, 1 Jan 2017 16:17:30 +0100 Subject: [PATCH 2/3] Also print horizontal output as hex --- client/mysql.cc | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/client/mysql.cc b/client/mysql.cc index 700057c..7febd64 100644 --- a/client/mysql.cc +++ b/client/mysql.cc @@ -3895,6 +3895,34 @@ print_field_types(MYSQL_RES *result) } +/* Used to determine if we should invoke print_as_hex for this field */ +my_bool +is_binary_field(MYSQL_FIELD *field) +{ + if ((field->type == MYSQL_TYPE_BLOB) + || ((field->type == MYSQL_TYPE_VAR_STRING) && (field->charsetnr == 63)) + || ((field->type == MYSQL_TYPE_STRING) && (field->charsetnr == 63)) + || ((field->type == MYSQL_TYPE_GEOMETRY) && (field->charsetnr == 63))) + return 1; + return 0; +} + + +/* Print binary value as hex literal (0x...) */ +static void +print_as_hex(FILE *output_file, const char *str, ulong len, ulong total_bytes_to_send) +{ + const char *ptr= str, *end= ptr + len; + ulong i; + + fprintf(output_file, "0x"); + for (; ptr < end ; ptr++) + fprintf(output_file, "%02X", *((uchar *)ptr)); + for (i= 2+len*2; i < total_bytes_to_send; i++) + tee_putc((int)' ', output_file); +} + + static void print_table_data(MYSQL_RES *result) { @@ -3923,6 +3951,8 @@ print_table_data(MYSQL_RES *result) length= max(length, field->max_length); if (length < 4 && !IS_NOT_NULL(field->flags)) length=4; // Room for "NULL" + if (is_binary_field(field)) + length=2+length*2; field->max_length=length; separator.fill(separator.length()+length+2,'-'); separator.append('+'); @@ -3992,7 +4022,11 @@ print_table_data(MYSQL_RES *result) visible_length= charset_info->cset->numcells(charset_info, buffer, buffer + data_length); extra_padding= data_length - visible_length; - if (field_max_length > MAX_COLUMN_LENGTH) + if (is_binary_field(field)) + { + print_as_hex(PAGER, cur[off], lengths[off], field_max_length); + } + else if (field_max_length > MAX_COLUMN_LENGTH) tee_print_sized_data(buffer, data_length, MAX_COLUMN_LENGTH+extra_padding, FALSE); else { @@ -4170,14 +4204,6 @@ print_table_data_xml(MYSQL_RES *result) } -static void print_blob_as_hex(FILE *output_file, const char *str, ulong len) -{ - const char *ptr= str, *end= ptr + len; - for (; ptr < end ; ptr++) - fprintf(output_file, "%02X", *((uchar *)ptr)); -} - - static void print_table_data_vertically(MYSQL_RES *result) { @@ -4211,12 +4237,9 @@ print_table_data_vertically(MYSQL_RES *result) tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name); if (cur[off]) { - if ((field->type == MYSQL_TYPE_BLOB) - || ((field->type == MYSQL_TYPE_VAR_STRING) && (field->charsetnr == 63)) - || ((field->type == MYSQL_TYPE_STRING) && (field->charsetnr == 63))) + if (is_binary_field(field)) { - tee_fprintf(PAGER, "0x"); - print_blob_as_hex(PAGER, cur[off], lengths[off]); + print_as_hex(PAGER, cur[off], lengths[off], lengths[off]); tee_putc('\n', PAGER); } else From 6fd9d146329159d31ef74f13fb8c0684e615e1a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Sun, 1 Jan 2017 16:31:27 +0100 Subject: [PATCH 3/3] Add option to disable the print-binary-as-hex feature --- client/mysql.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/client/mysql.cc b/client/mysql.cc index 7febd64..456d0b0 100644 --- a/client/mysql.cc +++ b/client/mysql.cc @@ -134,7 +134,8 @@ static my_bool ignore_errors=0,wait_flag=0,quick=0, default_pager_set= 0, opt_sigint_ignore= 0, auto_vertical_output= 0, show_warnings= 0, executing_query= 0, interrupted_query= 0, - ignore_spaces= 0, sigint_received= 0, opt_syslog= 0; + ignore_spaces= 0, sigint_received= 0, opt_syslog= 0, + opt_binhex= 0; static my_bool debug_info_flag, debug_check_flag; static my_bool column_types_flag; static my_bool preserve_comments= 0; @@ -1658,6 +1659,8 @@ static struct my_option my_long_options[] = {"bind-address", 0, "IP address to bind to.", (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, + {"binary-as-hex", 'b', "Print binary data as hex", &opt_binhex, &opt_binhex, + 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0}, {"character-sets-dir", OPT_CHARSETS_DIR, "Directory for character set files.", &charsets_dir, &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, @@ -3951,7 +3954,7 @@ print_table_data(MYSQL_RES *result) length= max(length, field->max_length); if (length < 4 && !IS_NOT_NULL(field->flags)) length=4; // Room for "NULL" - if (is_binary_field(field)) + if (opt_binhex && is_binary_field(field)) length=2+length*2; field->max_length=length; separator.fill(separator.length()+length+2,'-'); @@ -4022,7 +4025,7 @@ print_table_data(MYSQL_RES *result) visible_length= charset_info->cset->numcells(charset_info, buffer, buffer + data_length); extra_padding= data_length - visible_length; - if (is_binary_field(field)) + if (opt_binhex && is_binary_field(field)) { print_as_hex(PAGER, cur[off], lengths[off], field_max_length); } @@ -4237,7 +4240,7 @@ print_table_data_vertically(MYSQL_RES *result) tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name); if (cur[off]) { - if (is_binary_field(field)) + if (opt_binhex && is_binary_field(field)) { print_as_hex(PAGER, cur[off], lengths[off], lengths[off]); tee_putc('\n', PAGER);