~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.c

  • Committer: Monty Taylor
  • Date: 2008-08-09 02:24:34 UTC
  • mto: (266.1.8 codestyle)
  • mto: This revision was merged to the branch mainline in revision 279.
  • Revision ID: monty@inaugust.com-20080809022434-97na8pnugghskxip
Removed mystringslt conv lib and replaced it with a noinst lib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
/* drizzledump.cc  - Dump a tables contents and format to an ASCII file
 
16
/* drizzledump.c  - Dump a tables contents and format to an ASCII file
17
17
**
18
18
** The author's original notes follow :-
19
19
**
22
22
** WARRANTY: None, expressed, impressed, implied
23
23
**          or other
24
24
** STATUS: Public domain
 
25
** Adapted and optimized for DRIZZLE by
 
26
** Michael Widenius, Sinisa Milivojevic, Jani Tolonen
 
27
** -w --where added 9/10/98 by Jim Faucette
 
28
** slave code by David Saez Padros <david@ols.es>
 
29
** master/autocommit code by Brian Aker <brian@tangent.org>
 
30
** SSL by
 
31
** Andrei Errapart <andreie@no.spam.ee>
 
32
** Tõnu Samuel  <tonu@please.do.not.remove.this.spam.ee>
 
33
** XML by Gary Huntress <ghuntress@mediaone.net> 10/10/01, cleaned up
 
34
** and adapted to drizzledump 05/11/01 by Jani Tolonen
 
35
** Added --single-transaction option 06/06/2002 by Peter Zaitsev
 
36
** 10 Jun 2003: SET NAMES and --no-set-names by Alexander Barkov
25
37
*/
26
38
 
27
39
#define DUMP_VERSION "10.13"
28
40
 
29
 
#include "config.h"
30
 
#include <string>
31
41
#include "client_priv.h"
32
42
 
33
43
#include <mysys/my_sys.h>
38
48
 
39
49
#include <drizzled/error.h>
40
50
 
41
 
using namespace std;
42
51
/* Exit codes */
43
52
 
44
53
#define EX_USAGE 1
64
73
#define IGNORE_DATA 0x01 /* don't dump data for this table */
65
74
#define IGNORE_INSERT_DELAYED 0x02 /* table doesn't support INSERT DELAYED */
66
75
 
67
 
static void add_load_option(string &str, const char *option,
68
 
                            const char *option_value);
69
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint length,
 
76
static void add_load_option(DYNAMIC_STRING *str, const char *option,
 
77
                             const char *option_value);
 
78
static ulong find_set(TYPELIB *lib, const char *x, uint length,
70
79
                      char **err_pos, uint *err_len);
71
80
 
72
 
static void field_escape(string &in, const char *from);
 
81
static void field_escape(DYNAMIC_STRING* in, const char *from);
73
82
static bool  verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
74
83
                quick= 1, extended_insert= 1,
75
84
                lock_tables=1,ignore_errors=0,flush_logs=0,flush_privileges=0,
88
97
                opt_include_master_host_port= 0,
89
98
                opt_events= 0,
90
99
                opt_alltspcs=0, opt_notspcs= 0;
91
 
static bool debug_info_flag= 0, debug_check_flag= 0;
92
 
static uint32_t opt_max_allowed_packet, opt_net_buffer_length;
 
100
static bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0;
 
101
static ulong opt_max_allowed_packet, opt_net_buffer_length;
93
102
static DRIZZLE drizzle_connection,*drizzle=0;
94
 
static string insert_pat;
 
103
static DYNAMIC_STRING insert_pat;
95
104
static char  *opt_password=0,*current_user=0,
96
105
             *current_host=0,*path=0,*fields_terminated=0,
97
106
             *lines_terminated=0, *enclosed=0, *opt_enclosed=0, *escaped=0,
103
112
static char compatible_mode_normal_str[255];
104
113
/* Server supports character_set_results session variable? */
105
114
static bool server_supports_switching_charsets= true;
106
 
static uint32_t opt_compatible_mode= 0;
 
115
static ulong opt_compatible_mode= 0;
107
116
#define DRIZZLE_OPT_MASTER_DATA_EFFECTIVE_SQL 1
108
117
#define DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL 2
109
118
#define DRIZZLE_OPT_SLAVE_DATA_EFFECTIVE_SQL 1
112
121
static uint opt_slave_data;
113
122
static uint my_end_arg;
114
123
static int   first_error=0;
115
 
static string extended_row;
 
124
static DYNAMIC_STRING extended_row;
116
125
FILE *md_result_file= 0;
117
126
FILE *stderror_file=0;
118
127
 
 
128
static uint opt_protocol= DRIZZLE_PROTOCOL_TCP;
 
129
 
 
130
/*
 
131
Dynamic_string wrapper functions. In this file use these
 
132
wrappers, they will terminate the process if there is
 
133
an allocation failure.
 
134
*/
 
135
static void init_dynamic_string_checked(DYNAMIC_STRING *str, const char *init_str,
 
136
                            uint init_alloc, uint alloc_increment);
 
137
static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src);
 
138
static void dynstr_set_checked(DYNAMIC_STRING *str, const char *init_str);
 
139
static void dynstr_append_mem_checked(DYNAMIC_STRING *str, const char *append,
 
140
                          uint length);
 
141
static void dynstr_realloc_checked(DYNAMIC_STRING *str, ulong additional_size);
119
142
/*
120
143
  Constant for detection of default value of default_charset.
121
144
  If default_charset is equal to drizzle_universal_client_charset, then
122
145
  it is the default value which assigned at the very beginning of main().
123
146
*/
124
147
static const char *drizzle_universal_client_charset=
125
 
  DRIZZLE_UNIVERSAL_CLIENT_CHARSET;
 
148
  MYSQL_UNIVERSAL_CLIENT_CHARSET;
126
149
static char *default_charset;
127
 
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
 
150
static const CHARSET_INFO *charset_info= &my_charset_latin1;
128
151
const char *default_dbug_option="d:t:o,/tmp/drizzledump.trace";
129
152
/* have we seen any VIEWs during table scanning? */
130
153
bool seen_views= 0;
133
156
  "MYSQL323", "MYSQL40", "POSTGRESQL", "ORACLE", "MSSQL", "DB2",
134
157
  "MAXDB", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS",
135
158
  "ANSI",
136
 
  NULL
 
159
  NullS
137
160
};
138
161
#define MASK_ANSI_QUOTES \
139
162
(\
557
580
    }
558
581
    if (opt_set_charset)
559
582
      fprintf(sql_file,
 
583
"\n/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;"
 
584
"\n/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;"
560
585
"\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;"
561
586
"\n/*!40101 SET NAMES %s */;\n",default_charset);
562
587
 
598
623
    }
599
624
    if (opt_set_charset)
600
625
      fprintf(sql_file,
 
626
"/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n"
 
627
"/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n"
601
628
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
602
629
    fprintf(sql_file,
603
630
            "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n");
621
648
 
622
649
static void free_table_ent(char *key)
623
650
{
624
 
  free(key);
 
651
  my_free(key, MYF(0));
625
652
}
626
653
 
627
654
 
628
 
static unsigned char* get_table_key(const char *entry, size_t *length,
 
655
static uchar* get_table_key(const char *entry, size_t *length,
629
656
                            bool not_used __attribute__((unused)))
630
657
{
631
658
  *length= strlen(entry);
632
 
  return (unsigned char*) entry;
 
659
  return (uchar*) entry;
633
660
}
634
661
 
635
662
 
642
669
    if (argument)
643
670
    {
644
671
      char *start=argument;
645
 
      free(opt_password);
 
672
      my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
646
673
      opt_password=my_strdup(argument,MYF(MY_FAE));
647
674
      while (*argument) *argument++= 'x';               /* Destroy argument */
648
675
      if (*start)
717
744
      fprintf(stderr, "Illegal use of option --ignore-table=<database>.<table>\n");
718
745
      exit(1);
719
746
    }
720
 
    if (my_hash_insert(&ignore_table, (unsigned char*)my_strdup(argument, MYF(0))))
 
747
    if (my_hash_insert(&ignore_table, (uchar*)my_strdup(argument, MYF(0))))
721
748
      exit(EX_EOM);
722
749
    break;
723
750
  }
725
752
    {
726
753
      char buff[255];
727
754
      char *end= compatible_mode_normal_str;
728
 
      uint32_t i;
729
 
      uint32_t mode;
730
 
      uint32_t error_len;
 
755
      int i;
 
756
      ulong mode;
 
757
      uint err_len;
731
758
 
732
759
      opt_quoted= 1;
733
760
      opt_set_charset= 0;
734
761
      opt_compatible_mode_str= argument;
735
762
      opt_compatible_mode= find_set(&compatible_mode_typelib,
736
763
                                    argument, strlen(argument),
737
 
                                    &err_ptr, &error_len);
738
 
      if (error_len)
 
764
                                    &err_ptr, &err_len);
 
765
      if (err_len)
739
766
      {
740
 
        strmake(buff, err_ptr, min((uint32_t)sizeof(buff), error_len));
 
767
        strmake(buff, err_ptr, min(sizeof(buff), err_len));
741
768
        fprintf(stderr, "Invalid mode to --compatible: %s\n", buff);
742
769
        exit(1);
743
770
      }
746
773
      {
747
774
        if (mode & 1)
748
775
        {
749
 
          end= my_stpcpy(end, compatible_mode_names[i]);
750
 
          end= my_stpcpy(end, ",");
 
776
          end= strmov(end, compatible_mode_names[i]);
 
777
          end= strmov(end, ",");
751
778
        }
752
779
      }
753
780
      if (end!=compatible_mode_normal_str)
757
784
        been reset yet by --default-character-set=xxx.
758
785
      */
759
786
      if (default_charset == drizzle_universal_client_charset)
760
 
        default_charset= (char*) DRIZZLE_DEFAULT_CHARSET_NAME;
 
787
        default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
761
788
      break;
762
789
    }
763
790
  }
782
809
    return(EX_EOM);
783
810
  /* Don't copy internal log tables */
784
811
  if (my_hash_insert(&ignore_table,
785
 
                     (unsigned char*) my_strdup("mysql.apply_status", MYF(MY_WME))) ||
786
 
      my_hash_insert(&ignore_table,
787
 
                     (unsigned char*) my_strdup("mysql.schema", MYF(MY_WME))) ||
788
 
      my_hash_insert(&ignore_table,
789
 
                     (unsigned char*) my_strdup("mysql.general_log", MYF(MY_WME))) ||
790
 
      my_hash_insert(&ignore_table,
791
 
                     (unsigned char*) my_strdup("mysql.slow_log", MYF(MY_WME))) ||
792
 
      my_hash_insert(&ignore_table,
793
 
                     (unsigned char*) my_strdup("mysql.online_backup", MYF(MY_WME))) ||
794
 
      my_hash_insert(&ignore_table,
795
 
                     (unsigned char*) my_strdup("mysql.online_backup_progress", MYF(MY_WME))))
 
812
                     (uchar*) my_strdup("mysql.apply_status", MYF(MY_WME))) ||
 
813
      my_hash_insert(&ignore_table,
 
814
                     (uchar*) my_strdup("mysql.schema", MYF(MY_WME))) ||
 
815
      my_hash_insert(&ignore_table,
 
816
                     (uchar*) my_strdup("mysql.general_log", MYF(MY_WME))) ||
 
817
      my_hash_insert(&ignore_table,
 
818
                     (uchar*) my_strdup("mysql.slow_log", MYF(MY_WME))) ||
 
819
      my_hash_insert(&ignore_table,
 
820
                     (uchar*) my_strdup("mysql.online_backup", MYF(MY_WME))) ||
 
821
      my_hash_insert(&ignore_table,
 
822
                     (uchar*) my_strdup("mysql.online_backup_progress", MYF(MY_WME))))
796
823
    return(EX_EOM);
797
824
 
798
825
  if ((ho_error= handle_options(argc, argv, my_long_options, get_one_option)))
861
888
    return EX_USAGE;
862
889
  }
863
890
  if (tty_password)
864
 
    opt_password=get_tty_password(NULL);
 
891
    opt_password=get_tty_password(NullS);
865
892
  return(0);
866
893
} /* get_options */
867
894
 
1016
1043
{
1017
1044
  FILE* res;
1018
1045
  char filename[FN_REFLEN], tmp_path[FN_REFLEN];
1019
 
  convert_dirname(tmp_path,path,NULL);
 
1046
  convert_dirname(tmp_path,path,NullS);
1020
1047
  res= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4),
1021
1048
                O_WRONLY, MYF(MY_WME));
1022
1049
  return res;
1027
1054
{
1028
1055
  if (md_result_file && md_result_file != stdout)
1029
1056
    my_fclose(md_result_file, MYF(0));
1030
 
  free(opt_password);
 
1057
  my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
1031
1058
  if (hash_inited(&ignore_table))
1032
1059
    hash_free(&ignore_table);
 
1060
  if (extended_insert)
 
1061
    dynstr_free(&extended_row);
 
1062
  if (insert_pat_inited)
 
1063
    dynstr_free(&insert_pat);
1033
1064
  if (defaults_argv)
1034
1065
    free_defaults(defaults_argv);
1035
1066
  my_end(my_end_arg);
1061
1092
  verbose_msg("-- Connecting to %s...\n", host ? host : "localhost");
1062
1093
  drizzle_create(&drizzle_connection);
1063
1094
  if (opt_compress)
1064
 
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_COMPRESS,NULL);
 
1095
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_COMPRESS,NullS);
 
1096
  if (opt_protocol)
 
1097
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
 
1098
  drizzle_options(&drizzle_connection, DRIZZLE_SET_CHARSET_NAME, default_charset);
1065
1099
  if (!(drizzle= drizzle_connect(&drizzle_connection,host,user,passwd,
1066
1100
                                  NULL,opt_drizzle_port, NULL,
1067
1101
                                  0)))
1108
1142
  if (!(tmp=(char*) my_malloc(length*2+1, MYF(MY_WME))))
1109
1143
    die(EX_DRIZZLEERR, "Couldn't allocate memory");
1110
1144
 
1111
 
  drizzle_escape_string(tmp, pos, length);
 
1145
  drizzle_real_escape_string(&drizzle_connection, tmp, pos, length);
1112
1146
  fputc('\'', file);
1113
1147
  fputs(tmp, file);
1114
1148
  fputc('\'', file);
1115
1149
  check_io(file);
1116
 
  free(tmp);
 
1150
  my_free(tmp, MYF(MY_WME));
1117
1151
  return;
1118
1152
} /* unescape */
1119
1153
 
1221
1255
    Quote '<' '>' '&' '\"' chars and print a string to the xml_file.
1222
1256
*/
1223
1257
 
1224
 
static void print_quoted_xml(FILE *xml_file, const char *str, uint32_t len)
 
1258
static void print_quoted_xml(FILE *xml_file, const char *str, ulong len)
1225
1259
{
1226
1260
  const char *end;
1227
1261
 
1254
1288
 
1255
1289
  SYNOPSIS
1256
1290
    print_xml_tag(xml_file, sbeg, send, tag_name, first_attribute_name, 
1257
 
                    ..., attribute_name_n, attribute_value_n, NULL)
 
1291
                    ..., attribute_name_n, attribute_value_n, NullS)
1258
1292
    xml_file              - output file
1259
1293
    sbeg                  - line beginning
1260
1294
    line_end              - line ending
1291
1325
 
1292
1326
  va_start(arg_list, first_attribute_name);
1293
1327
  attribute_name= first_attribute_name;
1294
 
  while (attribute_name != NULL)
 
1328
  while (attribute_name != NullS)
1295
1329
  {
1296
1330
    attribute_value= va_arg(arg_list, char *);
1297
 
    assert(attribute_value != NULL);
 
1331
    assert(attribute_value != NullS);
1298
1332
 
1299
1333
    fputc(' ', xml_file);
1300
1334
    fputs(attribute_name, xml_file);    
1404
1438
    Print hex value for blob data.
1405
1439
*/
1406
1440
 
1407
 
static void print_blob_as_hex(FILE *output_file, const char *str, uint32_t len)
 
1441
static void print_blob_as_hex(FILE *output_file, const char *str, ulong len)
1408
1442
{
1409
1443
    /* sakaik got the idea to to provide blob's in hex notation. */
1410
1444
    const char *ptr= str, *end= ptr + len;
1411
1445
    for (; ptr < end ; ptr++)
1412
 
      fprintf(output_file, "%02X", *((unsigned char *)ptr));
 
1446
      fprintf(output_file, "%02X", *((uchar *)ptr));
1413
1447
    check_io(output_file);
1414
1448
}
1415
1449
 
1456
1490
  if ((write_data= !(*ignore_flag & IGNORE_DATA)))
1457
1491
  {
1458
1492
    complete_insert= opt_complete_insert;
1459
 
    insert_pat= "";
 
1493
    if (!insert_pat_inited)
 
1494
    {
 
1495
      insert_pat_inited= 1;
 
1496
      init_dynamic_string_checked(&insert_pat, "", 1024, 1024);
 
1497
    }
 
1498
    else
 
1499
      dynstr_set_checked(&insert_pat, "");
1460
1500
  }
1461
1501
 
1462
1502
  insert_option= ((delayed && opt_ignore) ? " DELAYED IGNORE " :
1473
1513
 
1474
1514
  if (opt_order_by_primary)
1475
1515
  {
1476
 
    free(order_by);
 
1516
    my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
1477
1517
    order_by= primary_key_fields(result_table);
1478
1518
  }
1479
1519
 
1560
1600
          if (drizzle_errno(drizzle) == ER_VIEW_INVALID)
1561
1601
            fprintf(sql_file, "\n-- failed on view %s: %s\n\n", result_table, scv_buff ? scv_buff : "");
1562
1602
 
1563
 
          free(scv_buff);
 
1603
          my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
1564
1604
 
1565
1605
          return(0);
1566
1606
        }
1567
1607
        else
1568
 
          free(scv_buff);
 
1608
          my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
1569
1609
 
1570
1610
        if (drizzle_num_rows(result))
1571
1611
        {
1582
1622
          }
1583
1623
 
1584
1624
          fprintf(sql_file,
 
1625
                  "SET @saved_cs_client     = @@character_set_client;\n"
 
1626
                  "SET character_set_client = utf8;\n"
1585
1627
                  "/*!50001 CREATE TABLE %s (\n",
1586
1628
                  result_table);
1587
1629
 
1602
1644
            fprintf(sql_file, ",\n  %s %s",
1603
1645
                    quote_name(row[0], name_buff, 0), row[1]);
1604
1646
          }
1605
 
          fprintf(sql_file, "\n) */;\n"); 
 
1647
          fprintf(sql_file,
 
1648
                  "\n) */;\n"
 
1649
                  "SET character_set_client = @saved_cs_client;\n");
 
1650
 
1606
1651
          check_io(sql_file);
1607
1652
        }
1608
1653
 
1617
1662
 
1618
1663
      row= drizzle_fetch_row(result);
1619
1664
 
1620
 
      fprintf(sql_file, "%s;\n", row[1]);
 
1665
      fprintf(sql_file,
 
1666
              "SET @saved_cs_client     = @@character_set_client;\n"
 
1667
              "SET character_set_client = utf8;\n"
 
1668
              "%s;\n"
 
1669
              "SET character_set_client = @saved_cs_client;\n",
 
1670
              row[1]);
1621
1671
 
1622
1672
      check_io(sql_file);
1623
1673
      drizzle_free_result(result);
1640
1690
    if (write_data)
1641
1691
    {
1642
1692
      if (opt_replace_into)
1643
 
        insert_pat.append("REPLACE ");
 
1693
        dynstr_append_checked(&insert_pat, "REPLACE ");
1644
1694
      else
1645
 
        insert_pat.append("INSERT ");
1646
 
      insert_pat.append(insert_option);
1647
 
      insert_pat.append("INTO ");
1648
 
      insert_pat.append(opt_quoted_table);
 
1695
        dynstr_append_checked(&insert_pat, "INSERT ");
 
1696
      dynstr_append_checked(&insert_pat, insert_option);
 
1697
      dynstr_append_checked(&insert_pat, "INTO ");
 
1698
      dynstr_append_checked(&insert_pat, opt_quoted_table);
1649
1699
      if (complete_insert)
1650
1700
      {
1651
 
        insert_pat.append(" (");
 
1701
        dynstr_append_checked(&insert_pat, " (");
1652
1702
      }
1653
1703
      else
1654
1704
      {
1655
 
        insert_pat.append(" VALUES ");
 
1705
        dynstr_append_checked(&insert_pat, " VALUES ");
1656
1706
        if (!extended_insert)
1657
 
          insert_pat.append("(");
 
1707
          dynstr_append_checked(&insert_pat, "(");
1658
1708
      }
1659
1709
    }
1660
1710
 
1664
1714
      {
1665
1715
        if (init)
1666
1716
        {
1667
 
          insert_pat.append(", ");
 
1717
          dynstr_append_checked(&insert_pat, ", ");
1668
1718
        }
1669
1719
        init=1;
1670
 
        insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
 
1720
        dynstr_append_checked(&insert_pat,
 
1721
                      quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1671
1722
      }
1672
1723
    }
1673
1724
    num_fields= drizzle_num_rows(result);
1701
1752
        fprintf(sql_file, "CREATE TABLE %s (\n", result_table);
1702
1753
      else
1703
1754
        print_xml_tag(sql_file, "\t", "\n", "table_structure", "name=", table, 
1704
 
                NULL);
 
1755
                NullS);
1705
1756
      check_io(sql_file);
1706
1757
    }
1707
1758
 
1708
1759
    if (write_data)
1709
1760
    {
1710
1761
      if (opt_replace_into)
1711
 
        insert_pat.append("REPLACE ");
 
1762
        dynstr_append_checked(&insert_pat, "REPLACE ");
1712
1763
      else
1713
 
        insert_pat.append("INSERT ");
1714
 
      insert_pat.append(insert_option);
1715
 
      insert_pat.append("INTO ");
1716
 
      insert_pat.append(result_table);
 
1764
        dynstr_append_checked(&insert_pat, "INSERT ");
 
1765
      dynstr_append_checked(&insert_pat, insert_option);
 
1766
      dynstr_append_checked(&insert_pat, "INTO ");
 
1767
      dynstr_append_checked(&insert_pat, result_table);
1717
1768
      if (complete_insert)
1718
 
        insert_pat.append(" (");
 
1769
        dynstr_append_checked(&insert_pat, " (");
1719
1770
      else
1720
1771
      {
1721
 
        insert_pat.append(" VALUES ");
 
1772
        dynstr_append_checked(&insert_pat, " VALUES ");
1722
1773
        if (!extended_insert)
1723
 
          insert_pat.append("(");
 
1774
          dynstr_append_checked(&insert_pat, "(");
1724
1775
      }
1725
1776
    }
1726
1777
 
1735
1786
          check_io(sql_file);
1736
1787
        }
1737
1788
        if (complete_insert)
1738
 
          insert_pat.append(", ");
 
1789
          dynstr_append_checked(&insert_pat, ", ");
1739
1790
      }
1740
1791
      init=1;
1741
1792
      if (complete_insert)
1742
 
        insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
 
1793
        dynstr_append_checked(&insert_pat,
 
1794
                      quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1743
1795
      if (!opt_no_create_info)
1744
1796
      {
1745
1797
        if (opt_xml)
1898
1950
  }
1899
1951
  if (complete_insert)
1900
1952
  {
1901
 
    insert_pat.append(") VALUES ");
 
1953
    dynstr_append_checked(&insert_pat, ") VALUES ");
1902
1954
    if (!extended_insert)
1903
 
      insert_pat.append("(");
 
1955
      dynstr_append_checked(&insert_pat, "(");
1904
1956
  }
1905
1957
  if (sql_file != md_result_file)
1906
1958
  {
1911
1963
  return((uint) num_fields);
1912
1964
} /* get_table_structure */
1913
1965
 
1914
 
static void add_load_option(string &str, const char *option,
1915
 
                            const char *option_value)
 
1966
static void add_load_option(DYNAMIC_STRING *str, const char *option,
 
1967
                             const char *option_value)
1916
1968
{
1917
1969
  if (!option_value)
1918
1970
  {
1920
1972
    return;
1921
1973
  }
1922
1974
 
1923
 
  str.append(option);
 
1975
  dynstr_append_checked(str, option);
1924
1976
  
1925
1977
  if (strncmp(option_value, "0x", sizeof("0x")-1) == 0)
1926
1978
  {
1927
1979
    /* It's a hex constant, don't escape */
1928
 
    str.append(option_value);
 
1980
    dynstr_append_checked(str, option_value);
1929
1981
  }
1930
1982
  else
1931
1983
  {
1942
1994
  syntax errors from the SQL parser.
1943
1995
*/
1944
1996
 
1945
 
static void field_escape(string &in, const char *from)
 
1997
static void field_escape(DYNAMIC_STRING* in, const char *from)
1946
1998
{
1947
1999
  uint end_backslashes= 0; 
1948
2000
 
1949
 
  in.append("'");
 
2001
  dynstr_append_checked(in, "'");
1950
2002
 
1951
2003
  while (*from)
1952
2004
  {
1953
 
    in.append(from, 1);
 
2005
    dynstr_append_mem_checked(in, from, 1);
1954
2006
 
1955
2007
    if (*from == '\\')
1956
2008
      end_backslashes^=1;    /* find odd number of backslashes */
1959
2011
      if (*from == '\'' && !end_backslashes)
1960
2012
      {
1961
2013
        /* We want a duplicate of "'" for DRIZZLE */
1962
 
        in.append("\'");
 
2014
        dynstr_append_checked(in, "\'");
1963
2015
      }
1964
2016
      end_backslashes=0;
1965
2017
    }
1967
2019
  }
1968
2020
  /* Add missing backslashes if user has specified odd number of backs.*/
1969
2021
  if (end_backslashes)
1970
 
    in.append("\\");
1971
 
 
1972
 
  in.append("'");
 
2022
    dynstr_append_checked(in, "\\");
 
2023
  
 
2024
  dynstr_append_checked(in, "'");
1973
2025
}
1974
2026
 
1975
2027
 
1994
2046
{
1995
2047
  char ignore_flag;
1996
2048
  char buf[200], table_buff[NAME_LEN+3];
1997
 
  string query_string;
 
2049
  DYNAMIC_STRING query_string;
1998
2050
  char table_type[NAME_LEN];
1999
2051
  char *result_table, table_buff2[NAME_LEN*2+3], *opt_quoted_table;
2000
2052
  int error= 0;
2001
 
  uint32_t         rownr, row_break, total_length, init_length;
 
2053
  ulong         rownr, row_break, total_length, init_length;
2002
2054
  uint num_fields;
2003
2055
  DRIZZLE_RES     *res;
2004
2056
  DRIZZLE_FIELD   *field;
2048
2100
     discarding SHOW CREATE EVENT statements generation. The myslq.event
2049
2101
     table data should be skipped too.
2050
2102
  */
2051
 
  if (!opt_events && !my_strcasecmp(&my_charset_utf8_general_ci, db, "mysql") &&
2052
 
      !my_strcasecmp(&my_charset_utf8_general_ci, table, "event"))
 
2103
  if (!opt_events && !my_strcasecmp(&my_charset_latin1, db, "mysql") &&
 
2104
      !my_strcasecmp(&my_charset_latin1, table, "event"))
2053
2105
  {
2054
2106
    verbose_msg("-- Skipping data table mysql.event, --skip-events was used\n");
2055
2107
    return;
2060
2112
 
2061
2113
  verbose_msg("-- Sending SELECT query...\n");
2062
2114
 
2063
 
  query_string.clear();
2064
 
  query_string.reserve(1024);
 
2115
  init_dynamic_string_checked(&query_string, "", 1024, 1024);
2065
2116
 
2066
2117
  if (path)
2067
2118
  {
2071
2122
      Convert the path to native os format
2072
2123
      and resolve to the full filepath.
2073
2124
    */
2074
 
    convert_dirname(tmp_path,path,NULL);    
 
2125
    convert_dirname(tmp_path,path,NullS);    
2075
2126
    my_load_path(tmp_path, tmp_path, NULL);
2076
2127
    fn_format(filename, table, tmp_path, ".txt", MYF(MY_UNPACK_FILENAME));
2077
2128
 
2083
2134
 
2084
2135
    /* now build the query string */
2085
2136
 
2086
 
    query_string.append( "SELECT * INTO OUTFILE '");
2087
 
    query_string.append( filename);
2088
 
    query_string.append( "'");
 
2137
    dynstr_append_checked(&query_string, "SELECT * INTO OUTFILE '");
 
2138
    dynstr_append_checked(&query_string, filename);
 
2139
    dynstr_append_checked(&query_string, "'");
2089
2140
 
2090
2141
    if (fields_terminated || enclosed || opt_enclosed || escaped)
2091
 
      query_string.append( " FIELDS");
 
2142
      dynstr_append_checked(&query_string, " FIELDS");
2092
2143
    
2093
 
    add_load_option(query_string, " TERMINATED BY ", fields_terminated);
2094
 
    add_load_option(query_string, " ENCLOSED BY ", enclosed);
2095
 
    add_load_option(query_string, " OPTIONALLY ENCLOSED BY ", opt_enclosed);
2096
 
    add_load_option(query_string, " ESCAPED BY ", escaped);
2097
 
    add_load_option(query_string, " LINES TERMINATED BY ", lines_terminated);
 
2144
    add_load_option(&query_string, " TERMINATED BY ", fields_terminated);
 
2145
    add_load_option(&query_string, " ENCLOSED BY ", enclosed);
 
2146
    add_load_option(&query_string, " OPTIONALLY ENCLOSED BY ", opt_enclosed);
 
2147
    add_load_option(&query_string, " ESCAPED BY ", escaped);
 
2148
    add_load_option(&query_string, " LINES TERMINATED BY ", lines_terminated);
2098
2149
 
2099
 
    query_string.append( " FROM ");
2100
 
    query_string.append( result_table);
 
2150
    dynstr_append_checked(&query_string, " FROM ");
 
2151
    dynstr_append_checked(&query_string, result_table);
2101
2152
 
2102
2153
    if (where)
2103
2154
    {
2104
 
      query_string.append( " WHERE ");
2105
 
      query_string.append( where);
 
2155
      dynstr_append_checked(&query_string, " WHERE ");
 
2156
      dynstr_append_checked(&query_string, where);
2106
2157
    }
2107
2158
 
2108
2159
    if (order_by)
2109
2160
    {
2110
 
      query_string.append( " ORDER BY ");
2111
 
      query_string.append( order_by);
 
2161
      dynstr_append_checked(&query_string, " ORDER BY ");
 
2162
      dynstr_append_checked(&query_string, order_by);
2112
2163
    }
2113
2164
 
2114
 
    if (drizzle_real_query(drizzle, query_string.c_str(), query_string.length()))
 
2165
    if (drizzle_real_query(drizzle, query_string.str, query_string.length))
2115
2166
    {
2116
2167
      DB_error(drizzle, "when executing 'SELECT INTO OUTFILE'");
 
2168
      dynstr_free(&query_string);
2117
2169
      return;
2118
2170
    }
2119
2171
  }
2126
2178
      check_io(md_result_file);
2127
2179
    }
2128
2180
    
2129
 
    query_string.append( "SELECT * FROM ");
2130
 
    query_string.append( result_table);
 
2181
    dynstr_append_checked(&query_string, "SELECT * FROM ");
 
2182
    dynstr_append_checked(&query_string, result_table);
2131
2183
 
2132
2184
    if (where)
2133
2185
    {
2137
2189
        check_io(md_result_file);
2138
2190
      }
2139
2191
      
2140
 
      query_string.append( " WHERE ");
2141
 
      query_string.append( where);
 
2192
      dynstr_append_checked(&query_string, " WHERE ");
 
2193
      dynstr_append_checked(&query_string, where);
2142
2194
    }
2143
2195
    if (order_by)
2144
2196
    {
2147
2199
        fprintf(md_result_file, "-- ORDER BY:  %s\n", order_by);
2148
2200
        check_io(md_result_file);
2149
2201
      }
2150
 
      query_string.append( " ORDER BY ");
2151
 
      query_string.append( order_by);
 
2202
      dynstr_append_checked(&query_string, " ORDER BY ");
 
2203
      dynstr_append_checked(&query_string, order_by);
2152
2204
    }
2153
2205
 
2154
2206
    if (!opt_xml && !opt_compact)
2156
2208
      fputs("\n", md_result_file);
2157
2209
      check_io(md_result_file);
2158
2210
    }
2159
 
    if (drizzle_query_with_error_report(drizzle, 0, query_string.c_str()))
 
2211
    if (drizzle_query_with_error_report(drizzle, 0, query_string.str))
2160
2212
    {
2161
2213
      DB_error(drizzle, "when retrieving data from server");
2162
2214
      goto err;
2196
2248
    total_length= opt_net_buffer_length;                /* Force row break */
2197
2249
    row_break=0;
2198
2250
    rownr=0;
2199
 
    init_length=(uint) insert_pat.length()+4;
 
2251
    init_length=(uint) insert_pat.length+4;
2200
2252
    if (opt_xml)
2201
2253
      print_xml_tag(md_result_file, "\t", "\n", "table_data", "name=", table,
2202
 
              NULL);
 
2254
              NullS);
2203
2255
    if (opt_autocommit)
2204
2256
    {
2205
2257
      fprintf(md_result_file, "set autocommit=0;\n");
2213
2265
      rownr++;
2214
2266
      if (!extended_insert && !opt_xml)
2215
2267
      {
2216
 
        fputs(insert_pat.c_str(),md_result_file);
 
2268
        fputs(insert_pat.str,md_result_file);
2217
2269
        check_io(md_result_file);
2218
2270
      }
2219
2271
      drizzle_field_seek(res,0);
2227
2279
      for (i= 0; i < drizzle_num_fields(res); i++)
2228
2280
      {
2229
2281
        int is_blob;
2230
 
        uint32_t length= lengths[i];
 
2282
        ulong length= lengths[i];
2231
2283
 
2232
2284
        if (!(field= drizzle_fetch_field(res)))
2233
2285
          die(EX_CONSCHECK,
2245
2297
        if (extended_insert && !opt_xml)
2246
2298
        {
2247
2299
          if (i == 0)
2248
 
            extended_row= "(";
 
2300
            dynstr_set_checked(&extended_row,"(");
2249
2301
          else
2250
 
            extended_row.append(",");
 
2302
            dynstr_append_checked(&extended_row,",");
2251
2303
 
2252
2304
          if (row[i])
2253
2305
          {
2254
2306
            if (length)
2255
2307
            {
2256
 
              if (!(field->type & NUM_FLAG))
 
2308
              if (!IS_NUM_FIELD(field))
2257
2309
              {
2258
2310
                /*
2259
2311
                  "length * 2 + 2" is OK for both HEX and non-HEX modes:
2263
2315
                  plus 2 bytes for leading and trailing '\'' characters.
2264
2316
                  Also we need to reserve 1 byte for terminating '\0'.
2265
2317
                */
2266
 
                char * tmp_str= (char *)malloc(length * 2 + 2 + 1);
2267
 
                memset(tmp_str, '\0', length * 2 + 2 + 1);
 
2318
                dynstr_realloc_checked(&extended_row,length * 2 + 2 + 1);
2268
2319
                if (opt_hex_blob && is_blob)
2269
2320
                {
2270
 
                  extended_row.append("0x");
2271
 
                  drizzle_hex_string(tmp_str, row[i], length);
2272
 
                  extended_row.append(tmp_str);
 
2321
                  dynstr_append_checked(&extended_row, "0x");
 
2322
                  extended_row.length+= drizzle_hex_string(extended_row.str +
 
2323
                                                         extended_row.length,
 
2324
                                                         row[i], length);
 
2325
                  assert(extended_row.length+1 <= extended_row.max_length);
 
2326
                  /* drizzle_hex_string() already terminated string by '\0' */
 
2327
                  assert(extended_row.str[extended_row.length] == '\0');
2273
2328
                }
2274
2329
                else
2275
2330
                {
2276
 
                  extended_row.append("'");
2277
 
                  drizzle_escape_string(tmp_str,
2278
 
                                        row[i],length);
2279
 
                  extended_row.append(tmp_str);
2280
 
                  extended_row.append("'");
 
2331
                  dynstr_append_checked(&extended_row,"'");
 
2332
                  extended_row.length +=
 
2333
                  drizzle_real_escape_string(&drizzle_connection,
 
2334
                                           &extended_row.str[extended_row.length],
 
2335
                                           row[i],length);
 
2336
                  extended_row.str[extended_row.length]='\0';
 
2337
                  dynstr_append_checked(&extended_row,"'");
2281
2338
                }
2282
 
                free(tmp_str);
2283
2339
              }
2284
2340
              else
2285
2341
              {
2287
2343
                char *ptr= row[i];
2288
2344
                if (my_isalpha(charset_info, *ptr) || (*ptr == '-' &&
2289
2345
                    my_isalpha(charset_info, ptr[1])))
2290
 
                  extended_row.append( "NULL");
 
2346
                  dynstr_append_checked(&extended_row, "NULL");
2291
2347
                else
2292
2348
                {
2293
 
                  extended_row.append( ptr);
 
2349
                  dynstr_append_checked(&extended_row, ptr);
2294
2350
                }
2295
2351
              }
2296
2352
            }
2297
2353
            else
2298
 
              extended_row.append("''");
 
2354
              dynstr_append_checked(&extended_row,"''");
2299
2355
          }
2300
2356
          else
2301
 
            extended_row.append("NULL");
 
2357
            dynstr_append_checked(&extended_row,"NULL");
2302
2358
        }
2303
2359
        else
2304
2360
        {
2309
2365
          }
2310
2366
          if (row[i])
2311
2367
          {
2312
 
            if (!(field->type & NUM_FLAG))
 
2368
            if (!IS_NUM_FIELD(field))
2313
2369
            {
2314
2370
              if (opt_xml)
2315
2371
              {
2317
2373
                {
2318
2374
                  /* Define xsi:type="xs:hexBinary" for hex encoded data */
2319
2375
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2320
 
                                field->name, "xsi:type=", "xs:hexBinary", NULL);
 
2376
                                field->name, "xsi:type=", "xs:hexBinary", NullS);
2321
2377
                  print_blob_as_hex(md_result_file, row[i], length);
2322
2378
                }
2323
2379
                else
2324
2380
                {
2325
2381
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=", 
2326
 
                                field->name, NULL);
 
2382
                                field->name, NullS);
2327
2383
                  print_quoted_xml(md_result_file, row[i], length);
2328
2384
                }
2329
2385
                fputs("</field>\n", md_result_file);
2343
2399
              if (opt_xml)
2344
2400
              {
2345
2401
                print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2346
 
                        field->name, NULL);
 
2402
                        field->name, NullS);
2347
2403
                fputs(!my_isalpha(charset_info, *ptr) ? ptr: "NULL",
2348
2404
                      md_result_file);
2349
2405
                fputs("</field>\n", md_result_file);
2376
2432
 
2377
2433
      if (extended_insert)
2378
2434
      {
2379
 
        uint32_t row_length;
2380
 
        extended_row.append(")");
2381
 
        row_length= 2 + extended_row.length();
 
2435
        ulong row_length;
 
2436
        dynstr_append_checked(&extended_row,")");
 
2437
        row_length= 2 + extended_row.length;
2382
2438
        if (total_length + row_length < opt_net_buffer_length)
2383
2439
        {
2384
2440
          total_length+= row_length;
2385
2441
          fputc(',',md_result_file);            /* Always row break */
2386
 
          fputs(extended_row.c_str(),md_result_file);
 
2442
          fputs(extended_row.str,md_result_file);
2387
2443
        }
2388
2444
        else
2389
2445
        {
2391
2447
            fputs(";\n", md_result_file);
2392
2448
          row_break=1;                          /* This is first row */
2393
2449
 
2394
 
          fputs(insert_pat.c_str(),md_result_file);
2395
 
          fputs(extended_row.c_str(),md_result_file);
 
2450
          fputs(insert_pat.str,md_result_file);
 
2451
          fputs(extended_row.str,md_result_file);
2396
2452
          total_length= row_length+init_length;
2397
2453
        }
2398
2454
        check_io(md_result_file);
2414
2470
    if (drizzle_errno(drizzle))
2415
2471
    {
2416
2472
      snprintf(buf, sizeof(buf),
2417
 
               "%s: Error %d: %s when dumping table %s at row: %d\n",
 
2473
               "%s: Error %d: %s when dumping table %s at row: %ld\n",
2418
2474
               my_progname,
2419
2475
               drizzle_errno(drizzle),
2420
2476
               drizzle_error(drizzle),
2444
2500
    }
2445
2501
    drizzle_free_result(res);
2446
2502
  }
 
2503
  dynstr_free(&query_string);
2447
2504
  return;
2448
2505
 
2449
2506
err:
 
2507
  dynstr_free(&query_string);
2450
2508
  maybe_exit(error);
2451
2509
  return;
2452
2510
} /* dump_table */
2459
2517
 
2460
2518
  if (!res)
2461
2519
  {
2462
 
    if (!(res= drizzle_list_tables(drizzle,NULL)))
 
2520
    if (!(res= drizzle_list_tables(drizzle,NullS)))
2463
2521
      return(NULL);
2464
2522
  }
2465
2523
  if ((row= drizzle_fetch_row(res)))
2567
2625
static int init_dumping(char *database, int init_func(char*))
2568
2626
{
2569
2627
  if (drizzle_get_server_version(drizzle) >= 50003 &&
2570
 
      !my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
 
2628
      !my_strcasecmp(&my_charset_latin1, database, "information_schema"))
2571
2629
    return 1;
2572
2630
 
2573
2631
  if (drizzle_select_db(drizzle, database))
2598
2656
    }
2599
2657
  }
2600
2658
  if (extended_insert)
2601
 
    extended_row.clear();
 
2659
    init_dynamic_string_checked(&extended_row, "", 1024, 1024);
2602
2660
  return 0;
2603
2661
} /* init_dumping */
2604
2662
 
2605
2663
 
2606
2664
/* Return 1 if we should copy the table */
2607
2665
 
2608
 
static bool include_table(const unsigned char *hash_key, size_t len)
 
2666
static bool include_table(const uchar *hash_key, size_t len)
2609
2667
{
2610
2668
  return !hash_search(&ignore_table, hash_key, len);
2611
2669
}
2618
2676
  char table_buff[NAME_LEN*2+3];
2619
2677
  char hash_key[2*NAME_LEN+2];  /* "db.tablename" */
2620
2678
  char *afterdot;
2621
 
  int using_mysql_db= my_strcasecmp(&my_charset_utf8_general_ci, database, "mysql");
2622
 
 
2623
 
 
2624
 
  afterdot= my_stpcpy(hash_key, database);
 
2679
  int using_mysql_db= my_strcasecmp(&my_charset_latin1, database, "mysql");
 
2680
 
 
2681
 
 
2682
  afterdot= strmov(hash_key, database);
2625
2683
  *afterdot++= '.';
2626
2684
 
2627
2685
  if (init_dumping(database, init_dumping_tables))
2628
2686
    return(1);
2629
2687
  if (opt_xml)
2630
 
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NULL);
 
2688
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NullS);
2631
2689
  if (lock_tables)
2632
2690
  {
2633
 
    string query;
2634
 
    query= "LOCK TABLES ";
 
2691
    DYNAMIC_STRING query;
 
2692
    init_dynamic_string_checked(&query, "LOCK TABLES ", 256, 1024);
2635
2693
    for (numrows= 0 ; (table= getTableName(1)) ; )
2636
2694
    {
2637
 
      char *end= my_stpcpy(afterdot, table);
2638
 
      if (include_table((unsigned char*) hash_key,end - hash_key))
 
2695
      char *end= strmov(afterdot, table);
 
2696
      if (include_table((uchar*) hash_key,end - hash_key))
2639
2697
      {
2640
2698
        numrows++;
2641
 
        query.append( quote_name(table, table_buff, 1));
2642
 
        query.append( " READ /*!32311 LOCAL */,");
 
2699
        dynstr_append_checked(&query, quote_name(table, table_buff, 1));
 
2700
        dynstr_append_checked(&query, " READ /*!32311 LOCAL */,");
2643
2701
      }
2644
2702
    }
2645
 
    if (numrows && drizzle_real_query(drizzle, query.c_str(), query.length()-1))
 
2703
    if (numrows && drizzle_real_query(drizzle, query.str, query.length-1))
2646
2704
      DB_error(drizzle, "when using LOCK TABLES");
2647
2705
            /* We shall continue here, if --force was given */
2648
 
    query.clear();
 
2706
    dynstr_free(&query);
2649
2707
  }
2650
2708
  if (flush_logs)
2651
2709
  {
2655
2713
  }
2656
2714
  while ((table= getTableName(0)))
2657
2715
  {
2658
 
    char *end= my_stpcpy(afterdot, table);
2659
 
    if (include_table((unsigned char*) hash_key, end - hash_key))
 
2716
    char *end= strmov(afterdot, table);
 
2717
    if (include_table((uchar*) hash_key, end - hash_key))
2660
2718
    {
2661
2719
      dump_table(table,database);
2662
 
      free(order_by);
 
2720
      my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2663
2721
      order_by= 0;
2664
2722
    }
2665
2723
  }
2669
2727
    check_io(md_result_file);
2670
2728
  }
2671
2729
  if (lock_tables)
2672
 
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
 
2730
    VOID(drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES"));
2673
2731
  if (flush_privileges && using_mysql_db == 0)
2674
2732
  {
2675
2733
    fprintf(md_result_file,"\n--\n-- Flush Grant Tables \n--\n");
2705
2763
           quote_for_like(old_table_name, show_name_buff));
2706
2764
 
2707
2765
  if (drizzle_query_with_error_report(drizzle, 0, query))
2708
 
    return NULL;
 
2766
    return NullS;
2709
2767
 
2710
2768
  if ((table_res= drizzle_store_result(drizzle)))
2711
2769
  {
2730
2788
static int dump_selected_tables(char *db, char **table_names, int tables)
2731
2789
{
2732
2790
  char table_buff[NAME_LEN*2+3];
2733
 
  string lock_tables_query;
 
2791
  DYNAMIC_STRING lock_tables_query;
2734
2792
  MEM_ROOT root;
2735
2793
  char **dump_tables, **pos, **end;
2736
2794
 
2742
2800
  if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *))))
2743
2801
     die(EX_EOM, "alloc_root failure.");
2744
2802
 
2745
 
  lock_tables_query= "LOCK TABLES ";
 
2803
  init_dynamic_string_checked(&lock_tables_query, "LOCK TABLES ", 256, 1024);
2746
2804
  for (; tables > 0 ; tables-- , table_names++)
2747
2805
  {
2748
2806
    /* the table name passed on commandline may be wrong case */
2751
2809
      /* Add found table name to lock_tables_query */
2752
2810
      if (lock_tables)
2753
2811
      {
2754
 
        lock_tables_query.append( quote_name(*pos, table_buff, 1));
2755
 
        lock_tables_query.append( " READ /*!32311 LOCAL */,");
 
2812
        dynstr_append_checked(&lock_tables_query, quote_name(*pos, table_buff, 1));
 
2813
        dynstr_append_checked(&lock_tables_query, " READ /*!32311 LOCAL */,");
2756
2814
      }
2757
2815
      pos++;
2758
2816
    }
2760
2818
    {
2761
2819
      if (!ignore_errors)
2762
2820
      {
 
2821
        dynstr_free(&lock_tables_query);
2763
2822
        free_root(&root, MYF(0));
2764
2823
      }
2765
2824
      maybe_die(EX_ILLEGAL_TABLE, "Couldn't find table: \"%s\"", *table_names);
2770
2829
 
2771
2830
  if (lock_tables)
2772
2831
  {
2773
 
    if (drizzle_real_query(drizzle, lock_tables_query.c_str(),
2774
 
                         lock_tables_query.length()-1))
 
2832
    if (drizzle_real_query(drizzle, lock_tables_query.str,
 
2833
                         lock_tables_query.length-1))
2775
2834
    {
2776
2835
      if (!ignore_errors)
2777
2836
      {
 
2837
        dynstr_free(&lock_tables_query);
2778
2838
        free_root(&root, MYF(0));
2779
2839
      }
2780
2840
      DB_error(drizzle, "when doing LOCK TABLES");
2781
2841
       /* We shall countinue here, if --force was given */
2782
2842
    }
2783
2843
  }
 
2844
  dynstr_free(&lock_tables_query);
2784
2845
  if (flush_logs)
2785
2846
  {
2786
2847
    if (drizzle_refresh(drizzle, REFRESH_LOG))
2792
2853
     /* We shall countinue here, if --force was given */
2793
2854
  }
2794
2855
  if (opt_xml)
2795
 
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NULL);
 
2856
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NullS);
2796
2857
 
2797
2858
  /* Dump each selected table */
2798
2859
  for (pos= dump_tables; pos < end; pos++)
2799
2860
    dump_table(*pos, db);
2800
2861
 
2801
2862
  free_root(&root, MYF(0));
2802
 
  free(order_by);
 
2863
  my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2803
2864
  order_by= 0;
2804
2865
  if (opt_xml)
2805
2866
  {
2807
2868
    check_io(md_result_file);
2808
2869
  }
2809
2870
  if (lock_tables)
2810
 
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
 
2871
    VOID(drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES"));
2811
2872
  return(0);
2812
2873
} /* dump_selected_tables */
2813
2874
 
3024
3085
 
3025
3086
static int purge_bin_logs_to(DRIZZLE *drizzle_con, char* log_name)
3026
3087
{
 
3088
  DYNAMIC_STRING str;
3027
3089
  int err;
3028
 
  string str= "PURGE BINARY LOGS TO '";
3029
 
  str.append(log_name);
3030
 
  str.append("'");
3031
 
  err = drizzle_query_with_error_report(drizzle_con, 0, str.c_str());
 
3090
  init_dynamic_string_checked(&str, "PURGE BINARY LOGS TO '", 1024, 1024);
 
3091
  dynstr_append_checked(&str, log_name);
 
3092
  dynstr_append_checked(&str, "'");
 
3093
  err = drizzle_query_with_error_report(drizzle_con, 0, str.str);
 
3094
  dynstr_free(&str);
3032
3095
  return err;
3033
3096
}
3034
3097
 
3065
3128
}
3066
3129
 
3067
3130
 
3068
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint length,
 
3131
static ulong find_set(TYPELIB *lib, const char *x, uint length,
3069
3132
                      char **err_pos, uint *err_len)
3070
3133
{
3071
3134
  const char *end= x + length;
3072
 
  uint32_t found= 0;
 
3135
  ulong found= 0;
3073
3136
  uint find;
3074
3137
  char buff[255];
3075
3138
 
3084
3147
    for (;;)
3085
3148
    {
3086
3149
      const char *pos= start;
3087
 
      uint32_t var_len;
 
3150
      uint var_len;
3088
3151
 
3089
3152
      for (; pos != end && *pos != ','; pos++) ;
3090
 
      var_len= (uint32_t) (pos - start);
3091
 
      strmake(buff, start, min((uint32_t)sizeof(buff), var_len));
 
3153
      var_len= (uint) (pos - start);
 
3154
      strmake(buff, start, min(sizeof(buff), var_len));
3092
3155
      find= find_type(buff, lib, var_len);
3093
3156
      if (!find)
3094
3157
      {
3213
3276
      If these two types, we do want to skip dumping the table
3214
3277
    */
3215
3278
    if (!opt_no_data &&
3216
 
        (!my_strcasecmp(&my_charset_utf8_general_ci, table_type, "MRG_MyISAM") ||
 
3279
        (!my_strcasecmp(&my_charset_latin1, table_type, "MRG_MyISAM") ||
3217
3280
         !strcmp(table_type,"MRG_ISAM")))
3218
3281
      result= IGNORE_DATA;
3219
3282
  }
3284
3347
  {
3285
3348
    char *end;
3286
3349
    /* result (terminating \0 is already in result_length) */
3287
 
    result= (char *)my_malloc(result_length + 10, MYF(MY_WME));
 
3350
    result= my_malloc(result_length + 10, MYF(MY_WME));
3288
3351
    if (!result)
3289
3352
    {
3290
3353
      fprintf(stderr, "Error: Not enough memory to store ORDER BY clause\n");
3293
3356
    drizzle_data_seek(res, 0);
3294
3357
    row= drizzle_fetch_row(res);
3295
3358
    quoted_field= quote_name(row[4], buff, 0);
3296
 
    end= my_stpcpy(result, quoted_field);
 
3359
    end= strmov(result, quoted_field);
3297
3360
    while ((row= drizzle_fetch_row(res)) && atoi(row[3]) > 1)
3298
3361
    {
3299
3362
      quoted_field= quote_name(row[4], buff, 0);
3300
 
      end= strxmov(end, ",", quoted_field, NULL);
 
3363
      end= strxmov(end, ",", quoted_field, NullS);
3301
3364
    }
3302
3365
  }
3303
3366
 
3308
3371
  return result;
3309
3372
}
3310
3373
 
 
3374
/*
 
3375
  The following functions are wrappers for the dynamic string functions
 
3376
  and if they fail, the wrappers will terminate the current process.
 
3377
*/
 
3378
 
 
3379
#define DYNAMIC_STR_ERROR_MSG "Couldn't perform DYNAMIC_STRING operation"
 
3380
 
 
3381
static void init_dynamic_string_checked(DYNAMIC_STRING *str, const char *init_str,
 
3382
                            uint init_alloc, uint alloc_increment)
 
3383
{
 
3384
  if (init_dynamic_string(str, init_str, init_alloc, alloc_increment))
 
3385
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3386
}
 
3387
 
 
3388
static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src)
 
3389
{
 
3390
  if (dynstr_append(dest, src))
 
3391
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3392
}
 
3393
 
 
3394
static void dynstr_set_checked(DYNAMIC_STRING *str, const char *init_str)
 
3395
{
 
3396
  if (dynstr_set(str, init_str))
 
3397
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3398
}
 
3399
 
 
3400
static void dynstr_append_mem_checked(DYNAMIC_STRING *str, const char *append,
 
3401
                          uint length)
 
3402
{
 
3403
  if (dynstr_append_mem(str, append, length))
 
3404
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3405
}
 
3406
 
 
3407
static void dynstr_realloc_checked(DYNAMIC_STRING *str, ulong additional_size)
 
3408
{
 
3409
  if (dynstr_realloc(str, additional_size))
 
3410
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3411
}
 
3412
 
3311
3413
 
3312
3414
int main(int argc, char **argv)
3313
3415
{