~mdcallag/+junk/5.1-map

« back to all changes in this revision

Viewing changes to client/mysql.cc

  • Committer: sasha at sashanet
  • Date: 2001-04-12 01:09:00 UTC
  • mfrom: (669.1.1)
  • Revision ID: sp1r-sasha@mysql.sashanet.com-20010412010900-14282
Ugly merge of 3.23 changes into 4.0 - fix up needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 *
20
20
 * Written by:
21
21
 *   Michael 'Monty' Widenius
22
 
 *   Andi Gutmans  <andi@zend.com>
 
22
 *   Andi Gutmans <andi@zend.com>
23
23
 *   Zeev Suraski <zeev@zend.com>
24
24
 *   Jani Tolonen <jani@mysql.com>
 
25
 *   Matt Wagner  <mwagner@mysql.com>
 
26
 *   Jeremy Cole  <jcole@mysql.com>
25
27
 *
26
28
 **/
27
29
 
120
122
            no_rehash=0,skip_updates=0,safe_updates=0,one_database=0,
121
123
            opt_compress=0,
122
124
            vertical=0,skip_line_numbers=0,skip_column_names=0,opt_html=0,
123
 
            opt_nopager=1, opt_outfile=0, no_named_cmds=1;
 
125
            opt_xml=0,opt_nopager=1, opt_outfile=0, no_named_cmds=1;
124
126
static uint verbose=0,opt_silent=0,opt_mysql_port=0;
125
127
static my_string opt_mysql_unix_port=0;
126
128
static int connect_flag=CLIENT_INTERACTIVE;
131
133
static int wait_time = 5;
132
134
static STATUS status;
133
135
static ulong select_limit,max_join_size,opt_connect_timeout=0;
 
136
static char *xmlmeta[] = {
 
137
  "&", "&amp;",
 
138
  "<", "&lt;",
 
139
  0, 0
 
140
};
134
141
static char default_pager[FN_REFLEN];
135
142
char pager[FN_REFLEN], outfile[FN_REFLEN];
136
143
FILE *PAGER, *OUTFILE;
166
173
                       uint silent);
167
174
static int put_info(const char *str,INFO_TYPE info,uint error=0);
168
175
static void safe_put_field(const char *pos,ulong length);
 
176
static char *array_value(char **array, char *key);
 
177
static char *xmlencode(char *dest, char *src);
 
178
static void my_chomp(char *end);
169
179
static void init_pager();
170
180
static void end_pager();
171
181
static void init_tee();
250
260
static void remove_cntrl(String &buffer);
251
261
static void print_table_data(MYSQL_RES *result);
252
262
static void print_table_data_html(MYSQL_RES *result);
 
263
static void print_table_data_xml(MYSQL_RES *result);
253
264
static void print_tab_data(MYSQL_RES *result);
254
265
static void print_table_data_vertically(MYSQL_RES *result);
255
266
static ulong start_timer(void);
313
324
 
314
325
#ifdef HAVE_READLINE
315
326
  initialize_readline(my_progname);
316
 
  if (!status.batch && !quick && !opt_html)
 
327
  if (!status.batch && !quick && !opt_html && !opt_xml)
317
328
  {
318
329
    /*read-history from file, default ~/.mysql_history*/
319
330
    if (getenv("MYSQL_HISTFILE"))
351
362
  if (connected)
352
363
    mysql_close(&mysql);
353
364
#ifdef HAVE_READLINE
354
 
  if (!status.batch && !quick && ! opt_html)
 
365
  if (!status.batch && !quick && !opt_html && !opt_xml)
355
366
  {
356
367
    /* write-history */
357
368
    if (verbose)
396
407
  {"force",         no_argument,           0, 'f'},
397
408
  {"help",          no_argument,           0, '?'},
398
409
  {"html",          no_argument,           0, 'H'},
 
410
  {"xml",           no_argument,           0, 'X'},
399
411
  {"host",          required_argument,     0, 'h'},
400
412
  {"ignore-spaces", no_argument,           0, 'i'},
401
413
  {"no-auto-rehash",no_argument,           0, 'A'},
490
502
  -i, --ignore-space    Ignore space after function names.\n\
491
503
  -h, --host=...        Connect to host.\n\
492
504
  -H, --html            Produce HTML output.\n\
 
505
  -X, --xml             Produce XML output.\n\
493
506
  -L, --skip-line-numbers\n\
494
507
                        Don't write line number for errors.\n");
495
508
#ifndef __WIN__
564
577
 
565
578
  set_all_changeable_vars(changeable_vars);
566
579
  while ((c=getopt_long(argc,argv,
567
 
                        "?ABCD:LfgGHinNoqrstTU::vVw::WEe:h:O:P:S:u:#::p::",
 
580
                        "?ABCD:LfgGHXinNoqrstTU::vVw::WEe:h:O:P:S:u:#::p::",
568
581
                        long_options, &option_index)) != EOF)
569
582
  {
570
583
    switch(c) {
685
698
    case 'G': no_named_cmds=0; break;
686
699
    case 'g': no_named_cmds=1; break;
687
700
    case 'H': opt_html=1; break;
 
701
    case 'X': opt_xml=1; break;
688
702
    case 'i': connect_flag|= CLIENT_IGNORE_SPACE; break;
689
703
    case 'B':
690
704
      if (!status.batch)
1442
1456
      init_pager();
1443
1457
      if (opt_html)
1444
1458
        print_table_data_html(result);
 
1459
      else if (opt_xml)
 
1460
        print_table_data_xml(result);
1445
1461
      else if (vertical)
1446
1462
        print_table_data_vertically(result);
1447
1463
      else if (opt_silent && verbose <= 2 && !output_tables)
1644
1660
}
1645
1661
 
1646
1662
 
 
1663
static void
 
1664
print_table_data_xml(MYSQL_RES *result)
 
1665
{
 
1666
  MYSQL_ROW   cur;
 
1667
  MYSQL_FIELD *fields;
 
1668
 
 
1669
  mysql_field_seek(result,0);
 
1670
 
 
1671
  char *statement;
 
1672
  statement=(char*) my_malloc(strlen(glob_buffer.ptr())*5+1, MYF(MY_WME));
 
1673
  xmlencode(statement, (char*) glob_buffer.ptr());
 
1674
 
 
1675
  (void) my_chomp(strend(statement));
 
1676
 
 
1677
  tee_fprintf(PAGER,"<?xml version=\"1.0\"?>\n\n<resultset statement=\"%s\">", statement);
 
1678
 
 
1679
  my_free(statement,MYF(MY_ALLOW_ZERO_PTR));
 
1680
 
 
1681
  fields = mysql_fetch_fields(result);
 
1682
 
 
1683
  while ((cur = mysql_fetch_row(result)))
 
1684
  {
 
1685
    (void) tee_fputs("\n  <row>\n", PAGER);
 
1686
    for (uint i=0; i < mysql_num_fields(result); i++)
 
1687
    {
 
1688
      char *data;
 
1689
      ulong *lengths=mysql_fetch_lengths(result);
 
1690
      data=(char*) my_malloc(lengths[i]*5+1, MYF(MY_WME));
 
1691
      tee_fprintf(PAGER, "\t<%s>", (fields[i].name ?
 
1692
                                  (fields[i].name[0] ? fields[i].name :
 
1693
                                   " &nbsp; ") : "NULL"));
 
1694
      xmlencode(data, cur[i]);
 
1695
      safe_put_field(data, strlen(data));
 
1696
      tee_fprintf(PAGER, "</%s>\n", (fields[i].name ?
 
1697
                                     (fields[i].name[0] ? fields[i].name :
 
1698
                                      " &nbsp; ") : "NULL"));
 
1699
      my_free(data,MYF(MY_ALLOW_ZERO_PTR));
 
1700
    }
 
1701
    (void) tee_fputs("  </row>\n", PAGER);
 
1702
  }
 
1703
  (void) tee_fputs("</resultset>\n", PAGER);
 
1704
}
 
1705
 
1647
1706
 
1648
1707
static void
1649
1708
print_table_data_vertically(MYSQL_RES *result)
1675
1734
  }
1676
1735
}
1677
1736
 
 
1737
static char
 
1738
*array_value(char **array, char *key) {
 
1739
  int x;
 
1740
  for(x=0; array[x]; x+=2)
 
1741
    if(!strcmp(array[x], key))
 
1742
      return array[x+1];
 
1743
  return 0;
 
1744
}
 
1745
 
 
1746
static char
 
1747
*xmlencode(char *dest, char *src) {
 
1748
  char *p = src;
 
1749
  char *t;
 
1750
  char s[2] = { 0, 0 };
 
1751
  *dest = 0;
 
1752
  
 
1753
  do {
 
1754
    s[0] = *p;
 
1755
    if(!(t=array_value(xmlmeta, s))) t = s;
 
1756
    strcat(dest, t);
 
1757
  } while(*p++);
 
1758
  return dest;
 
1759
}
 
1760
 
 
1761
static void
 
1762
my_chomp(char *end) {
 
1763
  char *mend;
 
1764
  mend = end;
 
1765
 
 
1766
  do {
 
1767
    if (isspace(*mend)) {
 
1768
      *mend = '\0';
 
1769
    } else
 
1770
      mend--;
 
1771
  } while (mend && *mend);
 
1772
}
 
1773
    
1678
1774
 
1679
1775
static void
1680
1776
safe_put_field(const char *pos,ulong length)
1705
1801
        tee_fputs("\\n", PAGER); // This too
1706
1802
      else if (*pos == '\\')
1707
1803
        tee_fputs("\\\\", PAGER);
1708
 
      else
 
1804
        else
1709
1805
        tee_putc(*pos, PAGER);
1710
1806
    }
1711
1807
  }