Permalink
Browse files

Display IP addresses nicely in the client on vertical output

mysql> CREATE TABLE t1 (
    ->   id bigint unsigned auto_increment primary key,
    ->   ip varbinary(16)
    -> );
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO t1(ip) VALUES
    -> (inet6_aton('::1')),
    -> (inet6_aton('127.0.0.1')),
    -> (inet6_aton('8.8.8.8')),
    -> (inet6_aton('8.8.4.4'));
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select id,hex(ip),ip from t1\G
*************************** 1. row ***************************
     id: 1
hex(ip): 00000000000000000000000000000001
     ip: INET6_ATON('::1')
*************************** 2. row ***************************
     id: 2
hex(ip): 7F000001
     ip: INET6_ATON('127.0.0.1')
*************************** 3. row ***************************
     id: 3
hex(ip): 08080808
     ip: INET6_ATON('8.8.8.8')
*************************** 4. row ***************************
     id: 4
hex(ip): 08080404
     ip: INET6_ATON('8.8.4.4')
4 rows in set (0.00 sec)
  • Loading branch information...
dveeden committed Dec 24, 2016
1 parent 2303280 commit 0ddb45419c3338ff16c470d2da642b313660a061
Showing with 23 additions and 1 deletion.
  1. +23 −1 client/mysql.cc
View
@@ -42,6 +42,7 @@
#include <signal.h>
#include <violite.h>
#include "prealloced_array.h"
+#include <arpa/inet.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
@@ -4178,6 +4179,8 @@ print_table_data_vertically(MYSQL_RES *result)
MYSQL_ROW cur;
uint max_length=0;
MYSQL_FIELD *field;
+ char ip_dst[INET6_ADDRSTRLEN];
+ char ip_tmp[16];
while ((field = mysql_fetch_field(result)))
{
@@ -4203,7 +4206,26 @@ 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 (strcmp(field->name, "ip") == 0)
+ {
+ if (lengths[off] == 4)
+ {
+ memcpy(ip_tmp, cur[off], lengths[off]);
+ inet_ntop(AF_INET, ip_tmp, ip_dst, INET_ADDRSTRLEN);
+ tee_fprintf(PAGER, "INET6_ATON('%s')\n", ip_dst);
+ }
+ else if (lengths[off] == 16)
+ {
+ memcpy(ip_tmp, cur[off], lengths[off]);
+ inet_ntop(AF_INET6, ip_tmp, ip_dst, INET6_ADDRSTRLEN);
+ tee_fprintf(PAGER, "INET6_ATON('%s')\n", ip_dst);
+ }
+ else {
+ tee_write(PAGER, cur[off], lengths[off], MY_PRINT_SPS_0 | MY_PRINT_MB);
+ tee_putc('\n', PAGER);
+ }
+ }
+ else if (cur[off])
{
tee_write(PAGER, cur[off], lengths[off], MY_PRINT_SPS_0 | MY_PRINT_MB);
tee_putc('\n', PAGER);

0 comments on commit 0ddb454

Please sign in to comment.