Skip to content

Commit

Permalink
port "Print binary data as hex in the mysql client" by @dveeden
Browse files Browse the repository at this point in the history
original code: mysql/mysql-server#118

MariaDB [test]> create table t1(id binary(16) primary key, name varchar(100));
Query OK, 0 rows affected (0.03 sec)

MariaDB [test]> insert into t1 values(unhex(replace(uuid(),'-','')), 'test 1');
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> insert into t1 values(unhex(replace(uuid(),'-','')), 'test 2');
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> insert into t1 values(unhex(replace(uuid(),'-','')), 'test 3');
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from t1;
+------------------+--------+
| id               | name   |
+------------------+--------+
| ^_?m?T?dl?^T?|H | test 1 |
| !?T?dl?^T?|H | test 2 |
| #t1?T??dl?^T??|H | test 3 |
+------------------+--------+
3 rows in set (0.00 sec)

With the addition of this patch, the following is possible:

MariaDB [test]> select * from t1;
+------------------------------------+--------+
| id                                 | name   |
+------------------------------------+--------+
| 0x1FE6166DFF5411E6AB646C8814987C48 | test 1 |
| 0x21EFDCB2FF5411E6AB646C8814987C48 | test 2 |
| 0x237431D4FF5411E6AB646C8814987C48 | test 3 |
+------------------------------------+--------+
3 rows in set (0.00 sec)

MariaDB [test]> select * from t1 where id = 0x21EFDCB2FF5411E6AB646C8814987C48;
+------------------------------------+--------+
| id                                 | name   |
+------------------------------------+--------+
| 0x21EFDCB2FF5411E6AB646C8814987C48 | test 2 |
+------------------------------------+--------+
1 row in set (0.00 sec)

MariaDB [test]> select * from t1\G
*************************** 1. row ***************************
  id: 0x1FE6166DFF5411E6AB646C8814987C48
name: test 1
*************************** 2. row ***************************
  id: 0x21EFDCB2FF5411E6AB646C8814987C48
name: test 2
*************************** 3. row ***************************
  id: 0x237431D4FF5411E6AB646C8814987C48
name: test 3
3 rows in set (0.01 sec)

This patch also introduces the new option --binary-as-hex=0 in
order to disable this new behavior.
  • Loading branch information
ericherman committed Mar 3, 2017
1 parent 7b9029b commit e5ef7c1
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions client/mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static my_bool ignore_errors=0,wait_flag=0,quick=0,
opt_secure_auth= 0,
default_pager_set= 0, opt_sigint_ignore= 0,
auto_vertical_output= 0,
show_warnings= 0, executing_query= 0,
show_warnings= 0, executing_query= 0, opt_binhex= 0,
ignore_spaces= 0, opt_progress_reports;
static my_bool debug_info_flag, debug_check_flag, batch_abort_on_error;
static my_bool column_types_flag;
Expand Down Expand Up @@ -1494,6 +1494,8 @@ static struct my_option my_long_options[] =
{"batch", 'B',
"Don't use history file. Disable interactive behavior. (Enables --silent.)",
0, 0, 0, GET_NO_ARG, NO_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},
Expand Down Expand Up @@ -3533,6 +3535,30 @@ print_field_types(MYSQL_RES *result)
}


static my_bool
is_binary_field(MYSQL_FIELD *field)
{
return ((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)));
}


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;

tee_fprintf(output_file, "0x");
for (; ptr < end ; ptr++)
tee_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)
{
Expand All @@ -3559,6 +3585,8 @@ print_table_data(MYSQL_RES *result)
length= MY_MAX(length,field->max_length);
if (length < 4 && !IS_NOT_NULL(field->flags))
length=4; // Room for "NULL"
if (opt_binhex && is_binary_field(field))
length= 2+length*2;
field->max_length=length;
num_flag[mysql_field_tell(result) - 1]= IS_NUM(field->type);
separator.fill(separator.length()+length+2,'-');
Expand Down Expand Up @@ -3628,7 +3656,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 (opt_binhex && is_binary_field(field))
{
print_as_hex(PAGER, buffer, data_length, 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
{
Expand Down Expand Up @@ -3845,7 +3877,12 @@ print_table_data_vertically(MYSQL_RES *result)
field= mysql_fetch_field(result);
if (column_names)
tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
if (cur[off])
if (opt_binhex && is_binary_field(field))
{
print_as_hex(PAGER, cur[off], lengths[off], lengths[off]);
tee_putc('\n', PAGER);
}
else if (cur[off])
{
unsigned int i;
const char *p;
Expand Down

0 comments on commit e5ef7c1

Please sign in to comment.