~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.c

Moved base64.h to mysys.

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>
 
41
#include <my_global.h>
 
42
#include <my_sys.h>
 
43
#include <m_string.h>
 
44
#include <m_ctype.h>
 
45
#include <hash.h>
 
46
#include <stdarg.h>
 
47
 
31
48
#include "client_priv.h"
32
 
 
33
 
#include <mysys/my_sys.h>
34
 
#include <mystrings/m_string.h>
35
 
#include <mystrings/m_ctype.h>
36
 
#include <mysys/hash.h>
37
 
#include <stdarg.h>
38
 
 
39
 
#include <drizzled/error.h>
40
 
 
41
 
using namespace std;
 
49
#include "drizzle_version.h"
 
50
#include "mysqld_error.h"
 
51
 
42
52
/* Exit codes */
43
53
 
44
54
#define EX_USAGE 1
64
74
#define IGNORE_DATA 0x01 /* don't dump data for this table */
65
75
#define IGNORE_INSERT_DELAYED 0x02 /* table doesn't support INSERT DELAYED */
66
76
 
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,
 
77
static void add_load_option(DYNAMIC_STRING *str, const char *option,
 
78
                             const char *option_value);
 
79
static ulong find_set(TYPELIB *lib, const char *x, uint length,
70
80
                      char **err_pos, uint *err_len);
71
81
 
72
 
static void field_escape(string &in, const char *from);
 
82
static void field_escape(DYNAMIC_STRING* in, const char *from);
73
83
static bool  verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
74
84
                quick= 1, extended_insert= 1,
75
85
                lock_tables=1,ignore_errors=0,flush_logs=0,flush_privileges=0,
88
98
                opt_include_master_host_port= 0,
89
99
                opt_events= 0,
90
100
                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;
 
101
static bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0;
 
102
static ulong opt_max_allowed_packet, opt_net_buffer_length;
93
103
static DRIZZLE drizzle_connection,*drizzle=0;
94
 
static string insert_pat;
 
104
static DYNAMIC_STRING insert_pat;
95
105
static char  *opt_password=0,*current_user=0,
96
106
             *current_host=0,*path=0,*fields_terminated=0,
97
107
             *lines_terminated=0, *enclosed=0, *opt_enclosed=0, *escaped=0,
103
113
static char compatible_mode_normal_str[255];
104
114
/* Server supports character_set_results session variable? */
105
115
static bool server_supports_switching_charsets= true;
106
 
static uint32_t opt_compatible_mode= 0;
 
116
static ulong opt_compatible_mode= 0;
107
117
#define DRIZZLE_OPT_MASTER_DATA_EFFECTIVE_SQL 1
108
118
#define DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL 2
109
119
#define DRIZZLE_OPT_SLAVE_DATA_EFFECTIVE_SQL 1
112
122
static uint opt_slave_data;
113
123
static uint my_end_arg;
114
124
static int   first_error=0;
115
 
static string extended_row;
 
125
static DYNAMIC_STRING extended_row;
116
126
FILE *md_result_file= 0;
117
127
FILE *stderror_file=0;
118
128
 
 
129
static uint opt_protocol= DRIZZLE_PROTOCOL_TCP;
 
130
 
 
131
/*
 
132
Dynamic_string wrapper functions. In this file use these
 
133
wrappers, they will terminate the process if there is
 
134
an allocation failure.
 
135
*/
 
136
static void init_dynamic_string_checked(DYNAMIC_STRING *str, const char *init_str,
 
137
                            uint init_alloc, uint alloc_increment);
 
138
static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src);
 
139
static void dynstr_set_checked(DYNAMIC_STRING *str, const char *init_str);
 
140
static void dynstr_append_mem_checked(DYNAMIC_STRING *str, const char *append,
 
141
                          uint length);
 
142
static void dynstr_realloc_checked(DYNAMIC_STRING *str, ulong additional_size);
119
143
/*
120
144
  Constant for detection of default value of default_charset.
121
145
  If default_charset is equal to drizzle_universal_client_charset, then
122
146
  it is the default value which assigned at the very beginning of main().
123
147
*/
124
148
static const char *drizzle_universal_client_charset=
125
 
  DRIZZLE_UNIVERSAL_CLIENT_CHARSET;
 
149
  MYSQL_UNIVERSAL_CLIENT_CHARSET;
126
150
static char *default_charset;
127
 
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
 
151
static CHARSET_INFO *charset_info= &my_charset_latin1;
128
152
const char *default_dbug_option="d:t:o,/tmp/drizzledump.trace";
129
153
/* have we seen any VIEWs during table scanning? */
130
154
bool seen_views= 0;
133
157
  "MYSQL323", "MYSQL40", "POSTGRESQL", "ORACLE", "MSSQL", "DB2",
134
158
  "MAXDB", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS",
135
159
  "ANSI",
136
 
  NULL
 
160
  NullS
137
161
};
138
162
#define MASK_ANSI_QUOTES \
139
163
(\
492
516
static void print_version(void)
493
517
{
494
518
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,DUMP_VERSION,
495
 
         drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
519
         MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
496
520
} /* print_version */
497
521
 
498
522
 
545
569
    {
546
570
      fprintf(sql_file,
547
571
              "-- DRIZZLE dump %s  Distrib %s, for %s (%s)\n--\n",
548
 
              DUMP_VERSION, drizzle_get_client_info(),
549
 
              SYSTEM_TYPE, MACHINE_TYPE);
 
572
              DUMP_VERSION, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
550
573
      fprintf(sql_file, "-- Host: %s    Database: %s\n",
551
574
              current_host ? current_host : "localhost", db_name ? db_name :
552
575
              "");
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
  }
767
794
static int get_options(int *argc, char ***argv)
768
795
{
769
796
  int ho_error;
770
 
  const DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
 
797
  DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
771
798
 
772
799
  opt_max_allowed_packet= *drizzle_params->p_max_allowed_packet;
773
800
  opt_net_buffer_length= *drizzle_params->p_net_buffer_length;
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
 
1484
1524
    {
1485
1525
      /* Make an sql-file, if path was given iow. option -T was given */
1486
1526
      char buff[20+FN_REFLEN];
1487
 
      const DRIZZLE_FIELD *field;
 
1527
      DRIZZLE_FIELD *field;
1488
1528
 
1489
1529
      snprintf(buff, sizeof(buff), "show create table %s", result_table);
1490
1530
 
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,
2240
2292
           we'll dump in hex only BLOB columns.
2241
2293
        */
2242
2294
        is_blob= (opt_hex_blob && field->charsetnr == 63 &&
2243
 
                  (field->type == DRIZZLE_TYPE_VARCHAR ||
 
2295
                  (field->type == DRIZZLE_TYPE_STRING ||
 
2296
                   field->type == DRIZZLE_TYPE_VARCHAR ||
2244
2297
                   field->type == DRIZZLE_TYPE_BLOB)) ? 1 : 0;
2245
2298
        if (extended_insert && !opt_xml)
2246
2299
        {
2247
2300
          if (i == 0)
2248
 
            extended_row= "(";
 
2301
            dynstr_set_checked(&extended_row,"(");
2249
2302
          else
2250
 
            extended_row.append(",");
 
2303
            dynstr_append_checked(&extended_row,",");
2251
2304
 
2252
2305
          if (row[i])
2253
2306
          {
2254
2307
            if (length)
2255
2308
            {
2256
 
              if (!(field->type & NUM_FLAG))
 
2309
              if (!IS_NUM_FIELD(field))
2257
2310
              {
2258
2311
                /*
2259
2312
                  "length * 2 + 2" is OK for both HEX and non-HEX modes:
2263
2316
                  plus 2 bytes for leading and trailing '\'' characters.
2264
2317
                  Also we need to reserve 1 byte for terminating '\0'.
2265
2318
                */
2266
 
                char * tmp_str= (char *)malloc(length * 2 + 2 + 1);
2267
 
                memset(tmp_str, '\0', length * 2 + 2 + 1);
 
2319
                dynstr_realloc_checked(&extended_row,length * 2 + 2 + 1);
2268
2320
                if (opt_hex_blob && is_blob)
2269
2321
                {
2270
 
                  extended_row.append("0x");
2271
 
                  drizzle_hex_string(tmp_str, row[i], length);
2272
 
                  extended_row.append(tmp_str);
 
2322
                  dynstr_append_checked(&extended_row, "0x");
 
2323
                  extended_row.length+= drizzle_hex_string(extended_row.str +
 
2324
                                                         extended_row.length,
 
2325
                                                         row[i], length);
 
2326
                  assert(extended_row.length+1 <= extended_row.max_length);
 
2327
                  /* drizzle_hex_string() already terminated string by '\0' */
 
2328
                  assert(extended_row.str[extended_row.length] == '\0');
2273
2329
                }
2274
2330
                else
2275
2331
                {
2276
 
                  extended_row.append("'");
2277
 
                  drizzle_escape_string(tmp_str,
2278
 
                                        row[i],length);
2279
 
                  extended_row.append(tmp_str);
2280
 
                  extended_row.append("'");
 
2332
                  dynstr_append_checked(&extended_row,"'");
 
2333
                  extended_row.length +=
 
2334
                  drizzle_real_escape_string(&drizzle_connection,
 
2335
                                           &extended_row.str[extended_row.length],
 
2336
                                           row[i],length);
 
2337
                  extended_row.str[extended_row.length]='\0';
 
2338
                  dynstr_append_checked(&extended_row,"'");
2281
2339
                }
2282
 
                free(tmp_str);
2283
2340
              }
2284
2341
              else
2285
2342
              {
2287
2344
                char *ptr= row[i];
2288
2345
                if (my_isalpha(charset_info, *ptr) || (*ptr == '-' &&
2289
2346
                    my_isalpha(charset_info, ptr[1])))
2290
 
                  extended_row.append( "NULL");
 
2347
                  dynstr_append_checked(&extended_row, "NULL");
2291
2348
                else
2292
2349
                {
2293
 
                  extended_row.append( ptr);
 
2350
                  dynstr_append_checked(&extended_row, ptr);
2294
2351
                }
2295
2352
              }
2296
2353
            }
2297
2354
            else
2298
 
              extended_row.append("''");
 
2355
              dynstr_append_checked(&extended_row,"''");
2299
2356
          }
2300
2357
          else
2301
 
            extended_row.append("NULL");
 
2358
            dynstr_append_checked(&extended_row,"NULL");
2302
2359
        }
2303
2360
        else
2304
2361
        {
2309
2366
          }
2310
2367
          if (row[i])
2311
2368
          {
2312
 
            if (!(field->type & NUM_FLAG))
 
2369
            if (!IS_NUM_FIELD(field))
2313
2370
            {
2314
2371
              if (opt_xml)
2315
2372
              {
2317
2374
                {
2318
2375
                  /* Define xsi:type="xs:hexBinary" for hex encoded data */
2319
2376
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2320
 
                                field->name, "xsi:type=", "xs:hexBinary", NULL);
 
2377
                                field->name, "xsi:type=", "xs:hexBinary", NullS);
2321
2378
                  print_blob_as_hex(md_result_file, row[i], length);
2322
2379
                }
2323
2380
                else
2324
2381
                {
2325
2382
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=", 
2326
 
                                field->name, NULL);
 
2383
                                field->name, NullS);
2327
2384
                  print_quoted_xml(md_result_file, row[i], length);
2328
2385
                }
2329
2386
                fputs("</field>\n", md_result_file);
2343
2400
              if (opt_xml)
2344
2401
              {
2345
2402
                print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2346
 
                        field->name, NULL);
 
2403
                        field->name, NullS);
2347
2404
                fputs(!my_isalpha(charset_info, *ptr) ? ptr: "NULL",
2348
2405
                      md_result_file);
2349
2406
                fputs("</field>\n", md_result_file);
2376
2433
 
2377
2434
      if (extended_insert)
2378
2435
      {
2379
 
        uint32_t row_length;
2380
 
        extended_row.append(")");
2381
 
        row_length= 2 + extended_row.length();
 
2436
        ulong row_length;
 
2437
        dynstr_append_checked(&extended_row,")");
 
2438
        row_length= 2 + extended_row.length;
2382
2439
        if (total_length + row_length < opt_net_buffer_length)
2383
2440
        {
2384
2441
          total_length+= row_length;
2385
2442
          fputc(',',md_result_file);            /* Always row break */
2386
 
          fputs(extended_row.c_str(),md_result_file);
 
2443
          fputs(extended_row.str,md_result_file);
2387
2444
        }
2388
2445
        else
2389
2446
        {
2391
2448
            fputs(";\n", md_result_file);
2392
2449
          row_break=1;                          /* This is first row */
2393
2450
 
2394
 
          fputs(insert_pat.c_str(),md_result_file);
2395
 
          fputs(extended_row.c_str(),md_result_file);
 
2451
          fputs(insert_pat.str,md_result_file);
 
2452
          fputs(extended_row.str,md_result_file);
2396
2453
          total_length= row_length+init_length;
2397
2454
        }
2398
2455
        check_io(md_result_file);
2414
2471
    if (drizzle_errno(drizzle))
2415
2472
    {
2416
2473
      snprintf(buf, sizeof(buf),
2417
 
               "%s: Error %d: %s when dumping table %s at row: %d\n",
 
2474
               "%s: Error %d: %s when dumping table %s at row: %ld\n",
2418
2475
               my_progname,
2419
2476
               drizzle_errno(drizzle),
2420
2477
               drizzle_error(drizzle),
2444
2501
    }
2445
2502
    drizzle_free_result(res);
2446
2503
  }
 
2504
  dynstr_free(&query_string);
2447
2505
  return;
2448
2506
 
2449
2507
err:
 
2508
  dynstr_free(&query_string);
2450
2509
  maybe_exit(error);
2451
2510
  return;
2452
2511
} /* dump_table */
2459
2518
 
2460
2519
  if (!res)
2461
2520
  {
2462
 
    if (!(res= drizzle_list_tables(drizzle,NULL)))
 
2521
    if (!(res= drizzle_list_tables(drizzle,NullS)))
2463
2522
      return(NULL);
2464
2523
  }
2465
2524
  if ((row= drizzle_fetch_row(res)))
2567
2626
static int init_dumping(char *database, int init_func(char*))
2568
2627
{
2569
2628
  if (drizzle_get_server_version(drizzle) >= 50003 &&
2570
 
      !my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
 
2629
      !my_strcasecmp(&my_charset_latin1, database, "information_schema"))
2571
2630
    return 1;
2572
2631
 
2573
2632
  if (drizzle_select_db(drizzle, database))
2598
2657
    }
2599
2658
  }
2600
2659
  if (extended_insert)
2601
 
    extended_row.clear();
 
2660
    init_dynamic_string_checked(&extended_row, "", 1024, 1024);
2602
2661
  return 0;
2603
2662
} /* init_dumping */
2604
2663
 
2605
2664
 
2606
2665
/* Return 1 if we should copy the table */
2607
2666
 
2608
 
static bool include_table(const unsigned char *hash_key, size_t len)
 
2667
static bool include_table(const uchar *hash_key, size_t len)
2609
2668
{
2610
2669
  return !hash_search(&ignore_table, hash_key, len);
2611
2670
}
2618
2677
  char table_buff[NAME_LEN*2+3];
2619
2678
  char hash_key[2*NAME_LEN+2];  /* "db.tablename" */
2620
2679
  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);
 
2680
  int using_mysql_db= my_strcasecmp(&my_charset_latin1, database, "mysql");
 
2681
 
 
2682
 
 
2683
  afterdot= strmov(hash_key, database);
2625
2684
  *afterdot++= '.';
2626
2685
 
2627
2686
  if (init_dumping(database, init_dumping_tables))
2628
2687
    return(1);
2629
2688
  if (opt_xml)
2630
 
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NULL);
 
2689
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NullS);
2631
2690
  if (lock_tables)
2632
2691
  {
2633
 
    string query;
2634
 
    query= "LOCK TABLES ";
 
2692
    DYNAMIC_STRING query;
 
2693
    init_dynamic_string_checked(&query, "LOCK TABLES ", 256, 1024);
2635
2694
    for (numrows= 0 ; (table= getTableName(1)) ; )
2636
2695
    {
2637
 
      char *end= my_stpcpy(afterdot, table);
2638
 
      if (include_table((unsigned char*) hash_key,end - hash_key))
 
2696
      char *end= strmov(afterdot, table);
 
2697
      if (include_table((uchar*) hash_key,end - hash_key))
2639
2698
      {
2640
2699
        numrows++;
2641
 
        query.append( quote_name(table, table_buff, 1));
2642
 
        query.append( " READ /*!32311 LOCAL */,");
 
2700
        dynstr_append_checked(&query, quote_name(table, table_buff, 1));
 
2701
        dynstr_append_checked(&query, " READ /*!32311 LOCAL */,");
2643
2702
      }
2644
2703
    }
2645
 
    if (numrows && drizzle_real_query(drizzle, query.c_str(), query.length()-1))
 
2704
    if (numrows && drizzle_real_query(drizzle, query.str, query.length-1))
2646
2705
      DB_error(drizzle, "when using LOCK TABLES");
2647
2706
            /* We shall continue here, if --force was given */
2648
 
    query.clear();
 
2707
    dynstr_free(&query);
2649
2708
  }
2650
2709
  if (flush_logs)
2651
2710
  {
2655
2714
  }
2656
2715
  while ((table= getTableName(0)))
2657
2716
  {
2658
 
    char *end= my_stpcpy(afterdot, table);
2659
 
    if (include_table((unsigned char*) hash_key, end - hash_key))
 
2717
    char *end= strmov(afterdot, table);
 
2718
    if (include_table((uchar*) hash_key, end - hash_key))
2660
2719
    {
2661
2720
      dump_table(table,database);
2662
 
      free(order_by);
 
2721
      my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2663
2722
      order_by= 0;
2664
2723
    }
2665
2724
  }
2669
2728
    check_io(md_result_file);
2670
2729
  }
2671
2730
  if (lock_tables)
2672
 
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
 
2731
    VOID(drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES"));
2673
2732
  if (flush_privileges && using_mysql_db == 0)
2674
2733
  {
2675
2734
    fprintf(md_result_file,"\n--\n-- Flush Grant Tables \n--\n");
2705
2764
           quote_for_like(old_table_name, show_name_buff));
2706
2765
 
2707
2766
  if (drizzle_query_with_error_report(drizzle, 0, query))
2708
 
    return NULL;
 
2767
    return NullS;
2709
2768
 
2710
2769
  if ((table_res= drizzle_store_result(drizzle)))
2711
2770
  {
2730
2789
static int dump_selected_tables(char *db, char **table_names, int tables)
2731
2790
{
2732
2791
  char table_buff[NAME_LEN*2+3];
2733
 
  string lock_tables_query;
 
2792
  DYNAMIC_STRING lock_tables_query;
2734
2793
  MEM_ROOT root;
2735
2794
  char **dump_tables, **pos, **end;
2736
2795
 
2742
2801
  if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *))))
2743
2802
     die(EX_EOM, "alloc_root failure.");
2744
2803
 
2745
 
  lock_tables_query= "LOCK TABLES ";
 
2804
  init_dynamic_string_checked(&lock_tables_query, "LOCK TABLES ", 256, 1024);
2746
2805
  for (; tables > 0 ; tables-- , table_names++)
2747
2806
  {
2748
2807
    /* the table name passed on commandline may be wrong case */
2751
2810
      /* Add found table name to lock_tables_query */
2752
2811
      if (lock_tables)
2753
2812
      {
2754
 
        lock_tables_query.append( quote_name(*pos, table_buff, 1));
2755
 
        lock_tables_query.append( " READ /*!32311 LOCAL */,");
 
2813
        dynstr_append_checked(&lock_tables_query, quote_name(*pos, table_buff, 1));
 
2814
        dynstr_append_checked(&lock_tables_query, " READ /*!32311 LOCAL */,");
2756
2815
      }
2757
2816
      pos++;
2758
2817
    }
2760
2819
    {
2761
2820
      if (!ignore_errors)
2762
2821
      {
 
2822
        dynstr_free(&lock_tables_query);
2763
2823
        free_root(&root, MYF(0));
2764
2824
      }
2765
2825
      maybe_die(EX_ILLEGAL_TABLE, "Couldn't find table: \"%s\"", *table_names);
2770
2830
 
2771
2831
  if (lock_tables)
2772
2832
  {
2773
 
    if (drizzle_real_query(drizzle, lock_tables_query.c_str(),
2774
 
                         lock_tables_query.length()-1))
 
2833
    if (drizzle_real_query(drizzle, lock_tables_query.str,
 
2834
                         lock_tables_query.length-1))
2775
2835
    {
2776
2836
      if (!ignore_errors)
2777
2837
      {
 
2838
        dynstr_free(&lock_tables_query);
2778
2839
        free_root(&root, MYF(0));
2779
2840
      }
2780
2841
      DB_error(drizzle, "when doing LOCK TABLES");
2781
2842
       /* We shall countinue here, if --force was given */
2782
2843
    }
2783
2844
  }
 
2845
  dynstr_free(&lock_tables_query);
2784
2846
  if (flush_logs)
2785
2847
  {
2786
2848
    if (drizzle_refresh(drizzle, REFRESH_LOG))
2792
2854
     /* We shall countinue here, if --force was given */
2793
2855
  }
2794
2856
  if (opt_xml)
2795
 
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NULL);
 
2857
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NullS);
2796
2858
 
2797
2859
  /* Dump each selected table */
2798
2860
  for (pos= dump_tables; pos < end; pos++)
2799
2861
    dump_table(*pos, db);
2800
2862
 
2801
2863
  free_root(&root, MYF(0));
2802
 
  free(order_by);
 
2864
  my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2803
2865
  order_by= 0;
2804
2866
  if (opt_xml)
2805
2867
  {
2807
2869
    check_io(md_result_file);
2808
2870
  }
2809
2871
  if (lock_tables)
2810
 
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
 
2872
    VOID(drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES"));
2811
2873
  return(0);
2812
2874
} /* dump_selected_tables */
2813
2875
 
3024
3086
 
3025
3087
static int purge_bin_logs_to(DRIZZLE *drizzle_con, char* log_name)
3026
3088
{
 
3089
  DYNAMIC_STRING str;
3027
3090
  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());
 
3091
  init_dynamic_string_checked(&str, "PURGE BINARY LOGS TO '", 1024, 1024);
 
3092
  dynstr_append_checked(&str, log_name);
 
3093
  dynstr_append_checked(&str, "'");
 
3094
  err = drizzle_query_with_error_report(drizzle_con, 0, str.str);
 
3095
  dynstr_free(&str);
3032
3096
  return err;
3033
3097
}
3034
3098
 
3065
3129
}
3066
3130
 
3067
3131
 
3068
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint length,
 
3132
static ulong find_set(TYPELIB *lib, const char *x, uint length,
3069
3133
                      char **err_pos, uint *err_len)
3070
3134
{
3071
3135
  const char *end= x + length;
3072
 
  uint32_t found= 0;
 
3136
  ulong found= 0;
3073
3137
  uint find;
3074
3138
  char buff[255];
3075
3139
 
3084
3148
    for (;;)
3085
3149
    {
3086
3150
      const char *pos= start;
3087
 
      uint32_t var_len;
 
3151
      uint var_len;
3088
3152
 
3089
3153
      for (; pos != end && *pos != ','; pos++) ;
3090
 
      var_len= (uint32_t) (pos - start);
3091
 
      strmake(buff, start, min((uint32_t)sizeof(buff), var_len));
 
3154
      var_len= (uint) (pos - start);
 
3155
      strmake(buff, start, min(sizeof(buff), var_len));
3092
3156
      find= find_type(buff, lib, var_len);
3093
3157
      if (!find)
3094
3158
      {
3213
3277
      If these two types, we do want to skip dumping the table
3214
3278
    */
3215
3279
    if (!opt_no_data &&
3216
 
        (!my_strcasecmp(&my_charset_utf8_general_ci, table_type, "MRG_MyISAM") ||
 
3280
        (!my_strcasecmp(&my_charset_latin1, table_type, "MRG_MyISAM") ||
3217
3281
         !strcmp(table_type,"MRG_ISAM")))
3218
3282
      result= IGNORE_DATA;
3219
3283
  }
3284
3348
  {
3285
3349
    char *end;
3286
3350
    /* result (terminating \0 is already in result_length) */
3287
 
    result= (char *)my_malloc(result_length + 10, MYF(MY_WME));
 
3351
    result= my_malloc(result_length + 10, MYF(MY_WME));
3288
3352
    if (!result)
3289
3353
    {
3290
3354
      fprintf(stderr, "Error: Not enough memory to store ORDER BY clause\n");
3293
3357
    drizzle_data_seek(res, 0);
3294
3358
    row= drizzle_fetch_row(res);
3295
3359
    quoted_field= quote_name(row[4], buff, 0);
3296
 
    end= my_stpcpy(result, quoted_field);
 
3360
    end= strmov(result, quoted_field);
3297
3361
    while ((row= drizzle_fetch_row(res)) && atoi(row[3]) > 1)
3298
3362
    {
3299
3363
      quoted_field= quote_name(row[4], buff, 0);
3300
 
      end= strxmov(end, ",", quoted_field, NULL);
 
3364
      end= strxmov(end, ",", quoted_field, NullS);
3301
3365
    }
3302
3366
  }
3303
3367
 
3308
3372
  return result;
3309
3373
}
3310
3374
 
 
3375
/*
 
3376
  The following functions are wrappers for the dynamic string functions
 
3377
  and if they fail, the wrappers will terminate the current process.
 
3378
*/
 
3379
 
 
3380
#define DYNAMIC_STR_ERROR_MSG "Couldn't perform DYNAMIC_STRING operation"
 
3381
 
 
3382
static void init_dynamic_string_checked(DYNAMIC_STRING *str, const char *init_str,
 
3383
                            uint init_alloc, uint alloc_increment)
 
3384
{
 
3385
  if (init_dynamic_string(str, init_str, init_alloc, alloc_increment))
 
3386
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3387
}
 
3388
 
 
3389
static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src)
 
3390
{
 
3391
  if (dynstr_append(dest, src))
 
3392
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3393
}
 
3394
 
 
3395
static void dynstr_set_checked(DYNAMIC_STRING *str, const char *init_str)
 
3396
{
 
3397
  if (dynstr_set(str, init_str))
 
3398
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3399
}
 
3400
 
 
3401
static void dynstr_append_mem_checked(DYNAMIC_STRING *str, const char *append,
 
3402
                          uint length)
 
3403
{
 
3404
  if (dynstr_append_mem(str, append, length))
 
3405
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3406
}
 
3407
 
 
3408
static void dynstr_realloc_checked(DYNAMIC_STRING *str, ulong additional_size)
 
3409
{
 
3410
  if (dynstr_realloc(str, additional_size))
 
3411
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3412
}
 
3413
 
3311
3414
 
3312
3415
int main(int argc, char **argv)
3313
3416
{
3317
3420
 
3318
3421
  compatible_mode_normal_str[0]= 0;
3319
3422
  default_charset= (char *)drizzle_universal_client_charset;
3320
 
  memset(&ignore_table, 0, sizeof(ignore_table));
 
3423
  bzero((char*) &ignore_table, sizeof(ignore_table));
3321
3424
 
3322
3425
  exit_code= get_options(&argc, &argv);
3323
3426
  if (exit_code)