Advertisement
Guest User

as

a guest
Jul 21st, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.95 KB | None | 0 0
  1. /*
  2. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17.  
  18. /* Show databases, tables or columns */
  19.  
  20. #define SHOW_VERSION "9.10"
  21.  
  22. #include "client_priv.h"
  23. #include "my_default.h"
  24. #include <my_sys.h>
  25. #include <m_string.h>
  26. #include <mysql.h>
  27. #include <mysqld_error.h>
  28. #include <signal.h>
  29. #include <stdarg.h>
  30. #include <sslopt-vars.h>
  31. #include <welcome_copyright_notice.h> /* ORACLE_WELCOME_COPYRIGHT_NOTICE */
  32.  
  33. static char * host=0, *opt_password=0, *user=0;
  34. static my_bool opt_show_keys= 0, opt_compress= 0, opt_count=0, opt_status= 0;
  35. static my_bool tty_password= 0, opt_table_type= 0;
  36. static my_bool debug_info_flag= 0, debug_check_flag= 0;
  37. static uint my_end_arg= 0;
  38. static uint opt_verbose=0;
  39. static char *default_charset= (char*) MYSQL_AUTODETECT_CHARSET_NAME;
  40. static char *opt_plugin_dir= 0, *opt_default_auth= 0;
  41. static my_bool opt_secure_auth= TRUE;
  42.  
  43. #ifdef HAVE_SMEM
  44. static char *shared_memory_base_name=0;
  45. #endif
  46. static uint opt_protocol=0;
  47. static char *opt_bind_addr = NULL;
  48.  
  49. static void get_options(int *argc,char ***argv);
  50. static uint opt_mysql_port=0;
  51. static int list_dbs(MYSQL *mysql,const char *wild);
  52. static int list_tables(MYSQL *mysql,const char *db,const char *table);
  53. static int list_table_status(MYSQL *mysql,const char *db,const char *table);
  54. static int list_fields(MYSQL *mysql,const char *db,const char *table,
  55. const char *field);
  56. static void print_header(const char *header,uint head_length,...);
  57. static void print_row(const char *header,uint head_length,...);
  58. static void print_trailer(uint length,...);
  59. static void print_res_header(MYSQL_RES *result);
  60. static void print_res_top(MYSQL_RES *result);
  61. static void print_res_row(MYSQL_RES *result,MYSQL_ROW cur);
  62.  
  63. static const char *load_default_groups[]= { "mysqlshow","client",0 };
  64. static char * opt_mysql_unix_port=0;
  65.  
  66. int main(int argc, char **argv)
  67. {
  68. int error;
  69. my_bool first_argument_uses_wildcards=0;
  70. char *wild;
  71. MYSQL mysql;
  72. MY_INIT(argv[0]);
  73.  
  74. my_getopt_use_args_separator= TRUE;
  75. if (load_defaults("my",load_default_groups,&argc,&argv))
  76. exit(1);
  77. my_getopt_use_args_separator= FALSE;
  78.  
  79. get_options(&argc,&argv);
  80.  
  81. wild=0;
  82. if (argc)
  83. {
  84. char *pos= argv[argc-1], *to;
  85. for (to= pos ; *pos ; pos++, to++)
  86. {
  87. switch (*pos) {
  88. case '*':
  89. *pos= '%';
  90. first_argument_uses_wildcards= 1;
  91. break;
  92. case '?':
  93. *pos= '_';
  94. first_argument_uses_wildcards= 1;
  95. break;
  96. case '%':
  97. case '_':
  98. first_argument_uses_wildcards= 1;
  99. break;
  100. case '\\':
  101. pos++;
  102. default: break;
  103. }
  104. *to= *pos;
  105. }
  106. *to= *pos; /* just to copy a '\0' if '\\' was used */
  107. }
  108. if (first_argument_uses_wildcards)
  109. wild= argv[--argc];
  110. else if (argc == 3) /* We only want one field */
  111. wild= argv[--argc];
  112.  
  113. if (argc > 2)
  114. {
  115. fprintf(stderr,"%s: Too many arguments\n",my_progname);
  116. exit(1);
  117. }
  118. mysql_init(&mysql);
  119. if (opt_compress)
  120. mysql_options(&mysql,MYSQL_OPT_COMPRESS,NullS);
  121. #ifdef HAVE_OPENSSL
  122. if (opt_use_ssl)
  123. {
  124. mysql_ssl_set(&mysql, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
  125. opt_ssl_capath, opt_ssl_cipher);
  126. mysql_options(&mysql, MYSQL_OPT_SSL_CRL, opt_ssl_crl);
  127. mysql_options(&mysql, MYSQL_OPT_SSL_CRLPATH, opt_ssl_crlpath);
  128. }
  129. mysql_options(&mysql,MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
  130. (char*)&opt_ssl_verify_server_cert);
  131. #endif
  132. if (opt_protocol)
  133. mysql_options(&mysql,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
  134. if (opt_bind_addr)
  135. mysql_options(&mysql,MYSQL_OPT_BIND,opt_bind_addr);
  136. if (!opt_secure_auth)
  137. mysql_options(&mysql, MYSQL_SECURE_AUTH,(char*)&opt_secure_auth);
  138. #ifdef HAVE_SMEM
  139. if (shared_memory_base_name)
  140. mysql_options(&mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
  141. #endif
  142. mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, default_charset);
  143.  
  144. if (opt_plugin_dir && *opt_plugin_dir)
  145. mysql_options(&mysql, MYSQL_PLUGIN_DIR, opt_plugin_dir);
  146.  
  147. if (opt_default_auth && *opt_default_auth)
  148. mysql_options(&mysql, MYSQL_DEFAULT_AUTH, opt_default_auth);
  149.  
  150. mysql_options(&mysql, MYSQL_OPT_CONNECT_ATTR_RESET, 0);
  151. mysql_options4(&mysql, MYSQL_OPT_CONNECT_ATTR_ADD,
  152. "program_name", "mysqlshow");
  153. if (!(mysql_real_connect(&mysql,host,user,opt_password,
  154. (first_argument_uses_wildcards) ? "" :
  155. argv[0],opt_mysql_port,opt_mysql_unix_port,
  156. 0)))
  157. {
  158. fprintf(stderr,"%s: %s\n",my_progname,mysql_error(&mysql));
  159. exit(1);
  160. }
  161. mysql.reconnect= 1;
  162.  
  163. switch (argc) {
  164. case 0: error=list_dbs(&mysql,wild); break;
  165. case 1:
  166. if (opt_status)
  167. error=list_table_status(&mysql,argv[0],wild);
  168. else
  169. error=list_tables(&mysql,argv[0],wild);
  170. break;
  171. default:
  172. if (opt_status && ! wild)
  173. error=list_table_status(&mysql,argv[0],argv[1]);
  174. else
  175. error=list_fields(&mysql,argv[0],argv[1],wild);
  176. break;
  177. }
  178. mysql_close(&mysql); /* Close & free connection */
  179. my_free(opt_password);
  180. #ifdef HAVE_SMEM
  181. my_free(shared_memory_base_name);
  182. #endif
  183. my_end(my_end_arg);
  184. exit(error ? 1 : 0);
  185. return 0; /* No compiler warnings */
  186. }
  187.  
  188. static struct my_option my_long_options[] =
  189. {
  190. {"bind-address", 0, "IP address to bind to.",
  191. (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
  192. REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  193. {"character-sets-dir", 'c', "Directory for character set files.",
  194. &charsets_dir, &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0,
  195. 0, 0, 0, 0, 0},
  196. {"default-character-set", OPT_DEFAULT_CHARSET,
  197. "Set the default character set.", &default_charset,
  198. &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  199. {"count", OPT_COUNT,
  200. "Show number of rows per table (may be slow for non-MyISAM tables).",
  201. &opt_count, &opt_count, 0, GET_BOOL, NO_ARG, 0, 0, 0,
  202. 0, 0, 0},
  203. {"compress", 'C', "Use compression in server/client protocol.",
  204. &opt_compress, &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
  205. 0, 0, 0},
  206. {"debug", '#', "Output debug log. Often this is 'd:t:o,filename'.",
  207. 0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
  208. {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
  209. &debug_check_flag, &debug_check_flag, 0,
  210. GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
  211. {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
  212. &debug_info_flag, &debug_info_flag,
  213. 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
  214. {"default_auth", OPT_DEFAULT_AUTH,
  215. "Default authentication client-side plugin to use.",
  216. &opt_default_auth, &opt_default_auth, 0,
  217. GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  218. {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG,
  219. 0, 0, 0, 0, 0, 0},
  220. {"host", 'h', "Connect to host.", &host, &host, 0, GET_STR,
  221. REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  222. {"status", 'i', "Shows a lot of extra information about each table.",
  223. &opt_status, &opt_status, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
  224. 0, 0},
  225. {"keys", 'k', "Show keys for table.", &opt_show_keys,
  226. &opt_show_keys, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
  227. {"password", 'p',
  228. "Password to use when connecting to server. If password is not given, it's "
  229. "solicited on the tty.",
  230. 0, 0, 0, GET_PASSWORD, OPT_ARG, 0, 0, 0, 0, 0, 0},
  231. {"plugin_dir", OPT_PLUGIN_DIR, "Directory for client-side plugins.",
  232. &opt_plugin_dir, &opt_plugin_dir, 0,
  233. GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  234. {"port", 'P', "Port number to use for connection or 0 for default to, in "
  235. "order of preference, my.cnf, $MYSQL_TCP_PORT, "
  236. #if MYSQL_PORT_DEFAULT == 0
  237. "/etc/services, "
  238. #endif
  239. "built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
  240. &opt_mysql_port,
  241. &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
  242. 0},
  243. #ifdef __WIN__
  244. {"pipe", 'W', "Use named pipes to connect to server.", 0, 0, 0, GET_NO_ARG,
  245. NO_ARG, 0, 0, 0, 0, 0, 0},
  246. #endif
  247. {"protocol", OPT_MYSQL_PROTOCOL,
  248. "The protocol to use for connection (tcp, socket, pipe, memory).",
  249. 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  250. {"secure-auth", OPT_SECURE_AUTH, "Refuse client connecting to server if it"
  251. " uses old (pre-4.1.1) protocol.", &opt_secure_auth,
  252. &opt_secure_auth, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
  253. #ifdef HAVE_SMEM
  254. {"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
  255. "Base name of shared memory.", &shared_memory_base_name,
  256. &shared_memory_base_name, 0, GET_STR_ALLOC, REQUIRED_ARG,
  257. 0, 0, 0, 0, 0, 0},
  258. #endif
  259. {"show-table-type", 't', "Show table type column.",
  260. &opt_table_type, &opt_table_type, 0, GET_BOOL,
  261. NO_ARG, 0, 0, 0, 0, 0, 0},
  262. {"socket", 'S', "The socket file to use for connection.",
  263. &opt_mysql_unix_port, &opt_mysql_unix_port, 0, GET_STR,
  264. REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  265. #include <sslopt-longopts.h>
  266. #ifndef DONT_ALLOW_USER_CHANGE
  267. {"user", 'u', "User for login if not current user.", &user,
  268. &user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  269. #endif
  270. {"verbose", 'v',
  271. "More verbose output; you can use this multiple times to get even more "
  272. "verbose output.",
  273. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  274. {"version", 'V', "Output version information and exit.", 0, 0, 0, GET_NO_ARG,
  275. NO_ARG, 0, 0, 0, 0, 0, 0},
  276. {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
  277. };
  278.  
  279.  
  280. static void print_version(void)
  281. {
  282. printf("%s Ver %s Distrib %s, for %s (%s)\n",my_progname,SHOW_VERSION,
  283. MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
  284. }
  285.  
  286.  
  287. static void usage(void)
  288. {
  289. print_version();
  290. puts(ORACLE_WELCOME_COPYRIGHT_NOTICE("2000"));
  291. puts("Shows the structure of a MySQL database (databases, tables, and columns).\n");
  292. printf("Usage: %s [OPTIONS] [database [table [column]]]\n",my_progname);
  293. puts("\n\
  294. If last argument contains a shell or SQL wildcard (*,?,% or _) then only\n\
  295. what\'s matched by the wildcard is shown.\n\
  296. If no database is given then all matching databases are shown.\n\
  297. If no table is given, then all matching tables in database are shown.\n\
  298. If no column is given, then all matching columns and column types in table\n\
  299. are shown.");
  300. print_defaults("my",load_default_groups);
  301. my_print_help(my_long_options);
  302. my_print_variables(my_long_options);
  303. }
  304.  
  305.  
  306. static my_bool
  307. get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
  308. char *argument)
  309. {
  310. switch(optid) {
  311. case 'v':
  312. opt_verbose++;
  313. break;
  314. case 'p':
  315. if (argument == disabled_my_option)
  316. argument= (char*) ""; /* Don't require password */
  317. if (argument)
  318. {
  319. char *start=argument;
  320. my_free(opt_password);
  321. opt_password=my_strdup(argument,MYF(MY_FAE));
  322. while (*argument) *argument++= 'x'; /* Destroy argument */
  323. if (*start)
  324. start[1]=0; /* Cut length of argument */
  325. tty_password= 0;
  326. }
  327. else
  328. tty_password=1;
  329. break;
  330. case 'W':
  331. #ifdef __WIN__
  332. opt_protocol = MYSQL_PROTOCOL_PIPE;
  333. #endif
  334. break;
  335. case OPT_MYSQL_PROTOCOL:
  336. opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
  337. opt->name);
  338. break;
  339. case '#':
  340. DBUG_PUSH(argument ? argument : "d:t:o");
  341. debug_check_flag= 1;
  342. break;
  343. #include <sslopt-case.h>
  344. case 'V':
  345. print_version();
  346. exit(0);
  347. break;
  348. case '?':
  349. case 'I': /* Info */
  350. usage();
  351. exit(0);
  352. }
  353. return 0;
  354. }
  355.  
  356.  
  357. static void
  358. get_options(int *argc,char ***argv)
  359. {
  360. int ho_error;
  361.  
  362. if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
  363. exit(ho_error);
  364.  
  365. if (tty_password)
  366. opt_password=get_tty_password(NullS);
  367. if (opt_count)
  368. {
  369. /*
  370. We need to set verbose to 2 as we need to change the output to include
  371. the number-of-rows column
  372. */
  373. opt_verbose= 2;
  374. }
  375. if (debug_info_flag)
  376. my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
  377. if (debug_check_flag)
  378. my_end_arg= MY_CHECK_ERROR;
  379. return;
  380. }
  381.  
  382.  
  383. static int
  384. list_dbs(MYSQL *mysql,const char *wild)
  385. {
  386. const char *header;
  387. uint length, counter = 0;
  388. ulong rowcount = 0L;
  389. char tables[NAME_LEN+1], rows[NAME_LEN+1];
  390. char query[255];
  391. MYSQL_FIELD *field;
  392. MYSQL_RES *result;
  393. MYSQL_ROW row= NULL, rrow;
  394.  
  395. if (!(result=mysql_list_dbs(mysql,wild)))
  396. {
  397. fprintf(stderr,"%s: Cannot list databases: %s\n",my_progname,
  398. mysql_error(mysql));
  399. return 1;
  400. }
  401.  
  402. /*
  403. If a wildcard was used, but there was only one row and it's name is an
  404. exact match, we'll assume they really wanted to see the contents of that
  405. database. This is because it is fairly common for database names to
  406. contain the underscore (_), like INFORMATION_SCHEMA.
  407. */
  408. if (wild && mysql_num_rows(result) == 1)
  409. {
  410. row= mysql_fetch_row(result);
  411. if (!my_strcasecmp(&my_charset_latin1, row[0], wild))
  412. {
  413. mysql_free_result(result);
  414. if (opt_status)
  415. return list_table_status(mysql, wild, NULL);
  416. else
  417. return list_tables(mysql, wild, NULL);
  418. }
  419. }
  420.  
  421. if (wild)
  422. printf("Wildcard: %s\n",wild);
  423.  
  424. header="Databases";
  425. length=(uint) strlen(header);
  426. field=mysql_fetch_field(result);
  427. if (length < field->max_length)
  428. length=field->max_length;
  429.  
  430. if (!opt_verbose)
  431. print_header(header,length,NullS);
  432. else if (opt_verbose == 1)
  433. print_header(header,length,"Tables",6,NullS);
  434. else
  435. print_header(header,length,"Tables",6,"Total Rows",12,NullS);
  436.  
  437. /* The first row may have already been read up above. */
  438. while (row || (row= mysql_fetch_row(result)))
  439. {
  440. counter++;
  441.  
  442. if (opt_verbose)
  443. {
  444. if (!(mysql_select_db(mysql,row[0])))
  445. {
  446. MYSQL_RES *tresult = mysql_list_tables(mysql,(char*)NULL);
  447. if (mysql_affected_rows(mysql) > 0)
  448. {
  449. sprintf(tables,"%6lu",(ulong) mysql_affected_rows(mysql));
  450. rowcount = 0;
  451. if (opt_verbose > 1)
  452. {
  453. /* Print the count of tables and rows for each database */
  454. MYSQL_ROW trow;
  455. while ((trow = mysql_fetch_row(tresult)))
  456. {
  457. sprintf(query,"SELECT COUNT(*) FROM `%s`",trow[0]);
  458. if (!(mysql_query(mysql,query)))
  459. {
  460. MYSQL_RES *rresult;
  461. if ((rresult = mysql_store_result(mysql)))
  462. {
  463. rrow = mysql_fetch_row(rresult);
  464. rowcount += (ulong) strtoull(rrow[0], (char**) 0, 10);
  465. mysql_free_result(rresult);
  466. }
  467. }
  468. }
  469. sprintf(rows,"%12lu",rowcount);
  470. }
  471. }
  472. else
  473. {
  474. sprintf(tables,"%6d",0);
  475. sprintf(rows,"%12d",0);
  476. }
  477. mysql_free_result(tresult);
  478. }
  479. else
  480. {
  481. strmov(tables,"N/A");
  482. strmov(rows,"N/A");
  483. }
  484. }
  485.  
  486. if (!opt_verbose)
  487. print_row(row[0],length,0);
  488. else if (opt_verbose == 1)
  489. print_row(row[0],length,tables,6,NullS);
  490. else
  491. print_row(row[0],length,tables,6,rows,12,NullS);
  492.  
  493. row= NULL;
  494. }
  495.  
  496. print_trailer(length,
  497. (opt_verbose > 0 ? 6 : 0),
  498. (opt_verbose > 1 ? 12 :0),
  499. 0);
  500.  
  501. if (counter && opt_verbose)
  502. printf("%u row%s in set.\n",counter,(counter > 1) ? "s" : "");
  503. mysql_free_result(result);
  504. return 0;
  505. }
  506.  
  507.  
  508. static int
  509. list_tables(MYSQL *mysql,const char *db,const char *table)
  510. {
  511. const char *header;
  512. uint head_length, counter = 0;
  513. char query[255], rows[NAME_LEN], fields[16];
  514. MYSQL_FIELD *field;
  515. MYSQL_RES *result;
  516. MYSQL_ROW row, rrow;
  517.  
  518. if (mysql_select_db(mysql,db))
  519. {
  520. fprintf(stderr,"%s: Cannot connect to db %s: %s\n",my_progname,db,
  521. mysql_error(mysql));
  522. return 1;
  523. }
  524. if (table)
  525. {
  526. /*
  527. We just hijack the 'rows' variable for a bit to store the escaped
  528. table name
  529. */
  530. mysql_real_escape_string(mysql, rows, table, (unsigned long)strlen(table));
  531. my_snprintf(query, sizeof(query), "show%s tables like '%s'",
  532. opt_table_type ? " full" : "", rows);
  533. }
  534. else
  535. my_snprintf(query, sizeof(query), "show%s tables",
  536. opt_table_type ? " full" : "");
  537. if (mysql_query(mysql, query) || !(result= mysql_store_result(mysql)))
  538. {
  539. fprintf(stderr,"%s: Cannot list tables in %s: %s\n",my_progname,db,
  540. mysql_error(mysql));
  541. exit(1);
  542. }
  543. printf("Database: %s",db);
  544. if (table)
  545. printf(" Wildcard: %s",table);
  546. putchar('\n');
  547.  
  548. header="Tables";
  549. head_length=(uint) strlen(header);
  550. field=mysql_fetch_field(result);
  551. if (head_length < field->max_length)
  552. head_length=field->max_length;
  553.  
  554. if (opt_table_type)
  555. {
  556. if (!opt_verbose)
  557. print_header(header,head_length,"table_type",10,NullS);
  558. else if (opt_verbose == 1)
  559. print_header(header,head_length,"table_type",10,"Columns",8,NullS);
  560. else
  561. {
  562. print_header(header,head_length,"table_type",10,"Columns",8,
  563. "Total Rows",10,NullS);
  564. }
  565. }
  566. else
  567. {
  568. if (!opt_verbose)
  569. print_header(header,head_length,NullS);
  570. else if (opt_verbose == 1)
  571. print_header(header,head_length,"Columns",8,NullS);
  572. else
  573. print_header(header,head_length,"Columns",8, "Total Rows",10,NullS);
  574. }
  575.  
  576. while ((row = mysql_fetch_row(result)))
  577. {
  578. counter++;
  579. if (opt_verbose > 0)
  580. {
  581. if (!(mysql_select_db(mysql,db)))
  582. {
  583. MYSQL_RES *rresult = mysql_list_fields(mysql,row[0],NULL);
  584. ulong rowcount=0L;
  585. if (!rresult)
  586. {
  587. strmov(fields,"N/A");
  588. strmov(rows,"N/A");
  589. }
  590. else
  591. {
  592. sprintf(fields,"%8u",(uint) mysql_num_fields(rresult));
  593. mysql_free_result(rresult);
  594.  
  595. if (opt_verbose > 1)
  596. {
  597. /* Print the count of rows for each table */
  598. sprintf(query,"SELECT COUNT(*) FROM `%s`",row[0]);
  599. if (!(mysql_query(mysql,query)))
  600. {
  601. if ((rresult = mysql_store_result(mysql)))
  602. {
  603. rrow = mysql_fetch_row(rresult);
  604. rowcount += (unsigned long) strtoull(rrow[0], (char**) 0, 10);
  605. mysql_free_result(rresult);
  606. }
  607. sprintf(rows,"%10lu",rowcount);
  608. }
  609. else
  610. sprintf(rows,"%10d",0);
  611. }
  612. }
  613. }
  614. else
  615. {
  616. strmov(fields,"N/A");
  617. strmov(rows,"N/A");
  618. }
  619. }
  620. if (opt_table_type)
  621. {
  622. if (!opt_verbose)
  623. print_row(row[0],head_length,row[1],10,NullS);
  624. else if (opt_verbose == 1)
  625. print_row(row[0],head_length,row[1],10,fields,8,NullS);
  626. else
  627. print_row(row[0],head_length,row[1],10,fields,8,rows,10,NullS);
  628. }
  629. else
  630. {
  631. if (!opt_verbose)
  632. print_row(row[0],head_length,NullS);
  633. else if (opt_verbose == 1)
  634. print_row(row[0],head_length, fields,8, NullS);
  635. else
  636. print_row(row[0],head_length, fields,8, rows,10, NullS);
  637. }
  638. }
  639.  
  640. print_trailer(head_length,
  641. (opt_table_type ? 10 : opt_verbose > 0 ? 8 : 0),
  642. (opt_table_type ? (opt_verbose > 0 ? 8 : 0)
  643. : (opt_verbose > 1 ? 10 :0)),
  644. !opt_table_type ? 0 : opt_verbose > 1 ? 10 :0,
  645. 0);
  646.  
  647. if (counter && opt_verbose)
  648. printf("%u row%s in set.\n\n",counter,(counter > 1) ? "s" : "");
  649.  
  650. mysql_free_result(result);
  651. return 0;
  652. }
  653.  
  654.  
  655. static int
  656. list_table_status(MYSQL *mysql,const char *db,const char *wild)
  657. {
  658. char query[1024],*end;
  659. MYSQL_RES *result;
  660. MYSQL_ROW row;
  661.  
  662. end=strxmov(query,"show table status from `",db,"`",NullS);
  663. if (wild && wild[0])
  664. strxmov(end," like '",wild,"'",NullS);
  665. if (mysql_query(mysql,query) || !(result=mysql_store_result(mysql)))
  666. {
  667. fprintf(stderr,"%s: Cannot get status for db: %s, table: %s: %s\n",
  668. my_progname,db,wild ? wild : "",mysql_error(mysql));
  669. if (mysql_errno(mysql) == ER_PARSE_ERROR)
  670. fprintf(stderr,"This error probably means that your MySQL server doesn't support the\n\'show table status' command.\n");
  671. return 1;
  672. }
  673.  
  674. printf("Database: %s",db);
  675. if (wild)
  676. printf(" Wildcard: %s",wild);
  677. putchar('\n');
  678.  
  679. print_res_header(result);
  680. while ((row=mysql_fetch_row(result)))
  681. print_res_row(result,row);
  682. print_res_top(result);
  683. mysql_free_result(result);
  684. return 0;
  685. }
  686.  
  687. /*
  688. list fields uses field interface as an example of how to parse
  689. a MYSQL FIELD
  690. */
  691.  
  692. static int
  693. list_fields(MYSQL *mysql,const char *db,const char *table,
  694. const char *wild)
  695. {
  696. char query[1024],*end;
  697. MYSQL_RES *result;
  698. MYSQL_ROW row;
  699. ulong UNINIT_VAR(rows);
  700.  
  701. if (mysql_select_db(mysql,db))
  702. {
  703. fprintf(stderr,"%s: Cannot connect to db: %s: %s\n",my_progname,db,
  704. mysql_error(mysql));
  705. return 1;
  706. }
  707.  
  708. if (opt_count)
  709. {
  710. sprintf(query,"select count(*) from `%s`", table);
  711. if (mysql_query(mysql,query) || !(result=mysql_store_result(mysql)))
  712. {
  713. fprintf(stderr,"%s: Cannot get record count for db: %s, table: %s: %s\n",
  714. my_progname,db,table,mysql_error(mysql));
  715. return 1;
  716. }
  717. row= mysql_fetch_row(result);
  718. rows= (ulong) strtoull(row[0], (char**) 0, 10);
  719. mysql_free_result(result);
  720. }
  721.  
  722. end=strmov(strmov(strmov(query,"show /*!32332 FULL */ columns from `"),table),"`");
  723. if (wild && wild[0])
  724. strxmov(end," like '",wild,"'",NullS);
  725. if (mysql_query(mysql,query) || !(result=mysql_store_result(mysql)))
  726. {
  727. fprintf(stderr,"%s: Cannot list columns in db: %s, table: %s: %s\n",
  728. my_progname,db,table,mysql_error(mysql));
  729. return 1;
  730. }
  731.  
  732. printf("Database: %s Table: %s", db, table);
  733. if (opt_count)
  734. printf(" Rows: %lu", rows);
  735. if (wild && wild[0])
  736. printf(" Wildcard: %s",wild);
  737. putchar('\n');
  738.  
  739. print_res_header(result);
  740. while ((row=mysql_fetch_row(result)))
  741. print_res_row(result,row);
  742. print_res_top(result);
  743. if (opt_show_keys)
  744. {
  745. end=strmov(strmov(strmov(query,"show keys from `"),table),"`");
  746. if (mysql_query(mysql,query) || !(result=mysql_store_result(mysql)))
  747. {
  748. fprintf(stderr,"%s: Cannot list keys in db: %s, table: %s: %s\n",
  749. my_progname,db,table,mysql_error(mysql));
  750. return 1;
  751. }
  752. if (mysql_num_rows(result))
  753. {
  754. print_res_header(result);
  755. while ((row=mysql_fetch_row(result)))
  756. print_res_row(result,row);
  757. print_res_top(result);
  758. }
  759. else
  760. puts("Table has no keys");
  761. }
  762. mysql_free_result(result);
  763. return 0;
  764. }
  765.  
  766.  
  767. /*****************************************************************************
  768. General functions to print a nice ascii-table from data
  769. *****************************************************************************/
  770.  
  771. static void
  772. print_header(const char *header,uint head_length,...)
  773. {
  774. va_list args;
  775. uint length,i,str_length,pre_space;
  776. const char *field;
  777.  
  778. va_start(args,head_length);
  779. putchar('+');
  780. field=header; length=head_length;
  781. for (;;)
  782. {
  783. for (i=0 ; i < length+2 ; i++)
  784. putchar('-');
  785. putchar('+');
  786. if (!(field=va_arg(args,char *)))
  787. break;
  788. length=va_arg(args,uint);
  789. }
  790. va_end(args);
  791. putchar('\n');
  792.  
  793. va_start(args,head_length);
  794. field=header; length=head_length;
  795. putchar('|');
  796. for (;;)
  797. {
  798. str_length=(uint) strlen(field);
  799. if (str_length > length)
  800. str_length=length+1;
  801. pre_space=(uint) (((int) length-(int) str_length)/2)+1;
  802. for (i=0 ; i < pre_space ; i++)
  803. putchar(' ');
  804. for (i = 0 ; i < str_length ; i++)
  805. putchar(field[i]);
  806. length=length+2-str_length-pre_space;
  807. for (i=0 ; i < length ; i++)
  808. putchar(' ');
  809. putchar('|');
  810. if (!(field=va_arg(args,char *)))
  811. break;
  812. length=va_arg(args,uint);
  813. }
  814. va_end(args);
  815. putchar('\n');
  816.  
  817. va_start(args,head_length);
  818. putchar('+');
  819. field=header; length=head_length;
  820. for (;;)
  821. {
  822. for (i=0 ; i < length+2 ; i++)
  823. putchar('-');
  824. putchar('+');
  825. if (!(field=va_arg(args,char *)))
  826. break;
  827. length=va_arg(args,uint);
  828. }
  829. va_end(args);
  830. putchar('\n');
  831. }
  832.  
  833.  
  834. static void
  835. print_row(const char *header,uint head_length,...)
  836. {
  837. va_list args;
  838. const char *field;
  839. uint i,length,field_length;
  840.  
  841. va_start(args,head_length);
  842. field=header; length=head_length;
  843. for (;;)
  844. {
  845. putchar('|');
  846. putchar(' ');
  847. fputs(field,stdout);
  848. field_length=(uint) strlen(field);
  849. for (i=field_length ; i <= length ; i++)
  850. putchar(' ');
  851. if (!(field=va_arg(args,char *)))
  852. break;
  853. length=va_arg(args,uint);
  854. }
  855. va_end(args);
  856. putchar('|');
  857. putchar('\n');
  858. }
  859.  
  860.  
  861. static void
  862. print_trailer(uint head_length,...)
  863. {
  864. va_list args;
  865. uint length,i;
  866.  
  867. va_start(args,head_length);
  868. length=head_length;
  869. putchar('+');
  870. for (;;)
  871. {
  872. for (i=0 ; i < length+2 ; i++)
  873. putchar('-');
  874. putchar('+');
  875. if (!(length=va_arg(args,uint)))
  876. break;
  877. }
  878. va_end(args);
  879. putchar('\n');
  880. }
  881.  
  882.  
  883. static void print_res_header(MYSQL_RES *result)
  884. {
  885. MYSQL_FIELD *field;
  886.  
  887. print_res_top(result);
  888. mysql_field_seek(result,0);
  889. putchar('|');
  890. while ((field = mysql_fetch_field(result)))
  891. {
  892. printf(" %-*s|",(int) field->max_length+1,field->name);
  893. }
  894. putchar('\n');
  895. print_res_top(result);
  896. }
  897.  
  898.  
  899. static void print_res_top(MYSQL_RES *result)
  900. {
  901. uint i,length;
  902. MYSQL_FIELD *field;
  903.  
  904. putchar('+');
  905. mysql_field_seek(result,0);
  906. while((field = mysql_fetch_field(result)))
  907. {
  908. if ((length=(uint) strlen(field->name)) > field->max_length)
  909. field->max_length=length;
  910. else
  911. length=field->max_length;
  912. for (i=length+2 ; i--> 0 ; )
  913. putchar('-');
  914. putchar('+');
  915. }
  916. putchar('\n');
  917. }
  918.  
  919.  
  920. static void print_res_row(MYSQL_RES *result,MYSQL_ROW cur)
  921. {
  922. uint i,length;
  923. MYSQL_FIELD *field;
  924. putchar('|');
  925. mysql_field_seek(result,0);
  926. for (i=0 ; i < mysql_num_fields(result); i++)
  927. {
  928. field = mysql_fetch_field(result);
  929. length=field->max_length;
  930. printf(" %-*s|",length+1,cur[i] ? (char*) cur[i] : "");
  931. }
  932. putchar('\n');
  933. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement