~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.c

  • Committer: Patrick Galbraith
  • Date: 2008-07-24 16:57:40 UTC
  • mto: (202.2.4 rename-mysql-to-drizzle)
  • mto: This revision was merged to the branch mainline in revision 212.
  • Revision ID: patg@ishvara-20080724165740-x58yw6zs6d9o17lf
Most everything working with client rename
mysqlslap test still fails... can't connect to the server

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
(\
452
476
char check_if_ignore_table(const char *table_name, char *table_type);
453
477
static char *primary_key_fields(const char *table_name);
454
478
 
 
479
#include <help_start.h>
 
480
 
455
481
/*
456
482
  Print the supplied message if in verbose mode
457
483
 
460
486
    fmt   format specifier
461
487
    ...   variable number of parameters
462
488
*/
 
489
 
463
490
static void verbose_msg(const char *fmt, ...)
464
491
{
465
492
  va_list args;
492
519
static void print_version(void)
493
520
{
494
521
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,DUMP_VERSION,
495
 
         drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
522
         MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
496
523
} /* print_version */
497
524
 
498
525
 
524
551
  printf("For more options, use %s --help\n", my_progname);
525
552
}
526
553
 
 
554
#include <help_end.h>
 
555
 
 
556
 
527
557
static void write_header(FILE *sql_file, char *db_name)
528
558
{
529
559
  if (opt_xml)
545
575
    {
546
576
      fprintf(sql_file,
547
577
              "-- DRIZZLE dump %s  Distrib %s, for %s (%s)\n--\n",
548
 
              DUMP_VERSION, drizzle_get_client_info(),
549
 
              SYSTEM_TYPE, MACHINE_TYPE);
 
578
              DUMP_VERSION, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
550
579
      fprintf(sql_file, "-- Host: %s    Database: %s\n",
551
580
              current_host ? current_host : "localhost", db_name ? db_name :
552
581
              "");
557
586
    }
558
587
    if (opt_set_charset)
559
588
      fprintf(sql_file,
 
589
"\n/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;"
 
590
"\n/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;"
560
591
"\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;"
561
592
"\n/*!40101 SET NAMES %s */;\n",default_charset);
562
593
 
598
629
    }
599
630
    if (opt_set_charset)
600
631
      fprintf(sql_file,
 
632
"/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n"
 
633
"/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n"
601
634
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
602
635
    fprintf(sql_file,
603
636
            "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n");
621
654
 
622
655
static void free_table_ent(char *key)
623
656
{
624
 
  free(key);
 
657
  my_free(key, MYF(0));
625
658
}
626
659
 
627
660
 
628
 
static unsigned char* get_table_key(const char *entry, size_t *length,
 
661
static uchar* get_table_key(const char *entry, size_t *length,
629
662
                            bool not_used __attribute__((unused)))
630
663
{
631
664
  *length= strlen(entry);
632
 
  return (unsigned char*) entry;
 
665
  return (uchar*) entry;
633
666
}
634
667
 
635
668
 
642
675
    if (argument)
643
676
    {
644
677
      char *start=argument;
645
 
      free(opt_password);
 
678
      my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
646
679
      opt_password=my_strdup(argument,MYF(MY_FAE));
647
680
      while (*argument) *argument++= 'x';               /* Destroy argument */
648
681
      if (*start)
653
686
      tty_password=1;
654
687
    break;
655
688
  case 'r':
656
 
    if (!(md_result_file= my_fopen(argument, O_WRONLY,
 
689
    if (!(md_result_file= my_fopen(argument, O_WRONLY | FILE_BINARY,
657
690
                                    MYF(MY_WME))))
658
691
      exit(1);
659
692
    break;
717
750
      fprintf(stderr, "Illegal use of option --ignore-table=<database>.<table>\n");
718
751
      exit(1);
719
752
    }
720
 
    if (my_hash_insert(&ignore_table, (unsigned char*)my_strdup(argument, MYF(0))))
 
753
    if (my_hash_insert(&ignore_table, (uchar*)my_strdup(argument, MYF(0))))
721
754
      exit(EX_EOM);
722
755
    break;
723
756
  }
725
758
    {
726
759
      char buff[255];
727
760
      char *end= compatible_mode_normal_str;
728
 
      uint32_t i;
729
 
      uint32_t mode;
730
 
      uint32_t error_len;
 
761
      int i;
 
762
      ulong mode;
 
763
      uint err_len;
731
764
 
732
765
      opt_quoted= 1;
733
766
      opt_set_charset= 0;
734
767
      opt_compatible_mode_str= argument;
735
768
      opt_compatible_mode= find_set(&compatible_mode_typelib,
736
769
                                    argument, strlen(argument),
737
 
                                    &err_ptr, &error_len);
738
 
      if (error_len)
 
770
                                    &err_ptr, &err_len);
 
771
      if (err_len)
739
772
      {
740
 
        strmake(buff, err_ptr, min((uint32_t)sizeof(buff), error_len));
 
773
        strmake(buff, err_ptr, min(sizeof(buff), err_len));
741
774
        fprintf(stderr, "Invalid mode to --compatible: %s\n", buff);
742
775
        exit(1);
743
776
      }
746
779
      {
747
780
        if (mode & 1)
748
781
        {
749
 
          end= my_stpcpy(end, compatible_mode_names[i]);
750
 
          end= my_stpcpy(end, ",");
 
782
          end= strmov(end, compatible_mode_names[i]);
 
783
          end= strmov(end, ",");
751
784
        }
752
785
      }
753
786
      if (end!=compatible_mode_normal_str)
757
790
        been reset yet by --default-character-set=xxx.
758
791
      */
759
792
      if (default_charset == drizzle_universal_client_charset)
760
 
        default_charset= (char*) DRIZZLE_DEFAULT_CHARSET_NAME;
 
793
        default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
761
794
      break;
762
795
    }
763
796
  }
767
800
static int get_options(int *argc, char ***argv)
768
801
{
769
802
  int ho_error;
770
 
  const DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
 
803
  DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
771
804
 
772
805
  opt_max_allowed_packet= *drizzle_params->p_max_allowed_packet;
773
806
  opt_net_buffer_length= *drizzle_params->p_net_buffer_length;
782
815
    return(EX_EOM);
783
816
  /* Don't copy internal log tables */
784
817
  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))))
 
818
                     (uchar*) my_strdup("mysql.apply_status", MYF(MY_WME))) ||
 
819
      my_hash_insert(&ignore_table,
 
820
                     (uchar*) my_strdup("mysql.schema", MYF(MY_WME))) ||
 
821
      my_hash_insert(&ignore_table,
 
822
                     (uchar*) my_strdup("mysql.general_log", MYF(MY_WME))) ||
 
823
      my_hash_insert(&ignore_table,
 
824
                     (uchar*) my_strdup("mysql.slow_log", MYF(MY_WME))) ||
 
825
      my_hash_insert(&ignore_table,
 
826
                     (uchar*) my_strdup("mysql.online_backup", MYF(MY_WME))) ||
 
827
      my_hash_insert(&ignore_table,
 
828
                     (uchar*) my_strdup("mysql.online_backup_progress", MYF(MY_WME))))
796
829
    return(EX_EOM);
797
830
 
798
831
  if ((ho_error= handle_options(argc, argv, my_long_options, get_one_option)))
861
894
    return EX_USAGE;
862
895
  }
863
896
  if (tty_password)
864
 
    opt_password=get_tty_password(NULL);
 
897
    opt_password=get_tty_password(NullS);
865
898
  return(0);
866
899
} /* get_options */
867
900
 
1016
1049
{
1017
1050
  FILE* res;
1018
1051
  char filename[FN_REFLEN], tmp_path[FN_REFLEN];
1019
 
  convert_dirname(tmp_path,path,NULL);
 
1052
  convert_dirname(tmp_path,path,NullS);
1020
1053
  res= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4),
1021
1054
                O_WRONLY, MYF(MY_WME));
1022
1055
  return res;
1027
1060
{
1028
1061
  if (md_result_file && md_result_file != stdout)
1029
1062
    my_fclose(md_result_file, MYF(0));
1030
 
  free(opt_password);
 
1063
  my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
1031
1064
  if (hash_inited(&ignore_table))
1032
1065
    hash_free(&ignore_table);
 
1066
  if (extended_insert)
 
1067
    dynstr_free(&extended_row);
 
1068
  if (insert_pat_inited)
 
1069
    dynstr_free(&insert_pat);
1033
1070
  if (defaults_argv)
1034
1071
    free_defaults(defaults_argv);
1035
1072
  my_end(my_end_arg);
1059
1096
 
1060
1097
 
1061
1098
  verbose_msg("-- Connecting to %s...\n", host ? host : "localhost");
1062
 
  drizzle_create(&drizzle_connection);
 
1099
  drizzle_init(&drizzle_connection);
1063
1100
  if (opt_compress)
1064
 
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_COMPRESS,NULL);
 
1101
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_COMPRESS,NullS);
 
1102
  if (opt_protocol)
 
1103
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
 
1104
  drizzle_options(&drizzle_connection, DRIZZLE_SET_CHARSET_NAME, default_charset);
1065
1105
  if (!(drizzle= drizzle_connect(&drizzle_connection,host,user,passwd,
1066
1106
                                  NULL,opt_drizzle_port, NULL,
1067
1107
                                  0)))
1108
1148
  if (!(tmp=(char*) my_malloc(length*2+1, MYF(MY_WME))))
1109
1149
    die(EX_DRIZZLEERR, "Couldn't allocate memory");
1110
1150
 
1111
 
  drizzle_escape_string(tmp, pos, length);
 
1151
  drizzle_real_escape_string(&drizzle_connection, tmp, pos, length);
1112
1152
  fputc('\'', file);
1113
1153
  fputs(tmp, file);
1114
1154
  fputc('\'', file);
1115
1155
  check_io(file);
1116
 
  free(tmp);
 
1156
  my_free(tmp, MYF(MY_WME));
1117
1157
  return;
1118
1158
} /* unescape */
1119
1159
 
1221
1261
    Quote '<' '>' '&' '\"' chars and print a string to the xml_file.
1222
1262
*/
1223
1263
 
1224
 
static void print_quoted_xml(FILE *xml_file, const char *str, uint32_t len)
 
1264
static void print_quoted_xml(FILE *xml_file, const char *str, ulong len)
1225
1265
{
1226
1266
  const char *end;
1227
1267
 
1254
1294
 
1255
1295
  SYNOPSIS
1256
1296
    print_xml_tag(xml_file, sbeg, send, tag_name, first_attribute_name, 
1257
 
                    ..., attribute_name_n, attribute_value_n, NULL)
 
1297
                    ..., attribute_name_n, attribute_value_n, NullS)
1258
1298
    xml_file              - output file
1259
1299
    sbeg                  - line beginning
1260
1300
    line_end              - line ending
1291
1331
 
1292
1332
  va_start(arg_list, first_attribute_name);
1293
1333
  attribute_name= first_attribute_name;
1294
 
  while (attribute_name != NULL)
 
1334
  while (attribute_name != NullS)
1295
1335
  {
1296
1336
    attribute_value= va_arg(arg_list, char *);
1297
 
    assert(attribute_value != NULL);
 
1337
    assert(attribute_value != NullS);
1298
1338
 
1299
1339
    fputc(' ', xml_file);
1300
1340
    fputs(attribute_name, xml_file);    
1404
1444
    Print hex value for blob data.
1405
1445
*/
1406
1446
 
1407
 
static void print_blob_as_hex(FILE *output_file, const char *str, uint32_t len)
 
1447
static void print_blob_as_hex(FILE *output_file, const char *str, ulong len)
1408
1448
{
1409
1449
    /* sakaik got the idea to to provide blob's in hex notation. */
1410
1450
    const char *ptr= str, *end= ptr + len;
1411
1451
    for (; ptr < end ; ptr++)
1412
 
      fprintf(output_file, "%02X", *((unsigned char *)ptr));
 
1452
      fprintf(output_file, "%02X", *((uchar *)ptr));
1413
1453
    check_io(output_file);
1414
1454
}
1415
1455
 
1456
1496
  if ((write_data= !(*ignore_flag & IGNORE_DATA)))
1457
1497
  {
1458
1498
    complete_insert= opt_complete_insert;
1459
 
    insert_pat= "";
 
1499
    if (!insert_pat_inited)
 
1500
    {
 
1501
      insert_pat_inited= 1;
 
1502
      init_dynamic_string_checked(&insert_pat, "", 1024, 1024);
 
1503
    }
 
1504
    else
 
1505
      dynstr_set_checked(&insert_pat, "");
1460
1506
  }
1461
1507
 
1462
1508
  insert_option= ((delayed && opt_ignore) ? " DELAYED IGNORE " :
1473
1519
 
1474
1520
  if (opt_order_by_primary)
1475
1521
  {
1476
 
    free(order_by);
 
1522
    my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
1477
1523
    order_by= primary_key_fields(result_table);
1478
1524
  }
1479
1525
 
1484
1530
    {
1485
1531
      /* Make an sql-file, if path was given iow. option -T was given */
1486
1532
      char buff[20+FN_REFLEN];
1487
 
      const DRIZZLE_FIELD *field;
 
1533
      DRIZZLE_FIELD *field;
1488
1534
 
1489
1535
      snprintf(buff, sizeof(buff), "show create table %s", result_table);
1490
1536
 
1560
1606
          if (drizzle_errno(drizzle) == ER_VIEW_INVALID)
1561
1607
            fprintf(sql_file, "\n-- failed on view %s: %s\n\n", result_table, scv_buff ? scv_buff : "");
1562
1608
 
1563
 
          free(scv_buff);
 
1609
          my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
1564
1610
 
1565
1611
          return(0);
1566
1612
        }
1567
1613
        else
1568
 
          free(scv_buff);
 
1614
          my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
1569
1615
 
1570
1616
        if (drizzle_num_rows(result))
1571
1617
        {
1582
1628
          }
1583
1629
 
1584
1630
          fprintf(sql_file,
 
1631
                  "SET @saved_cs_client     = @@character_set_client;\n"
 
1632
                  "SET character_set_client = utf8;\n"
1585
1633
                  "/*!50001 CREATE TABLE %s (\n",
1586
1634
                  result_table);
1587
1635
 
1602
1650
            fprintf(sql_file, ",\n  %s %s",
1603
1651
                    quote_name(row[0], name_buff, 0), row[1]);
1604
1652
          }
1605
 
          fprintf(sql_file, "\n) */;\n"); 
 
1653
          fprintf(sql_file,
 
1654
                  "\n) */;\n"
 
1655
                  "SET character_set_client = @saved_cs_client;\n");
 
1656
 
1606
1657
          check_io(sql_file);
1607
1658
        }
1608
1659
 
1617
1668
 
1618
1669
      row= drizzle_fetch_row(result);
1619
1670
 
1620
 
      fprintf(sql_file, "%s;\n", row[1]);
 
1671
      fprintf(sql_file,
 
1672
              "SET @saved_cs_client     = @@character_set_client;\n"
 
1673
              "SET character_set_client = utf8;\n"
 
1674
              "%s;\n"
 
1675
              "SET character_set_client = @saved_cs_client;\n",
 
1676
              row[1]);
1621
1677
 
1622
1678
      check_io(sql_file);
1623
1679
      drizzle_free_result(result);
1640
1696
    if (write_data)
1641
1697
    {
1642
1698
      if (opt_replace_into)
1643
 
        insert_pat.append("REPLACE ");
 
1699
        dynstr_append_checked(&insert_pat, "REPLACE ");
1644
1700
      else
1645
 
        insert_pat.append("INSERT ");
1646
 
      insert_pat.append(insert_option);
1647
 
      insert_pat.append("INTO ");
1648
 
      insert_pat.append(opt_quoted_table);
 
1701
        dynstr_append_checked(&insert_pat, "INSERT ");
 
1702
      dynstr_append_checked(&insert_pat, insert_option);
 
1703
      dynstr_append_checked(&insert_pat, "INTO ");
 
1704
      dynstr_append_checked(&insert_pat, opt_quoted_table);
1649
1705
      if (complete_insert)
1650
1706
      {
1651
 
        insert_pat.append(" (");
 
1707
        dynstr_append_checked(&insert_pat, " (");
1652
1708
      }
1653
1709
      else
1654
1710
      {
1655
 
        insert_pat.append(" VALUES ");
 
1711
        dynstr_append_checked(&insert_pat, " VALUES ");
1656
1712
        if (!extended_insert)
1657
 
          insert_pat.append("(");
 
1713
          dynstr_append_checked(&insert_pat, "(");
1658
1714
      }
1659
1715
    }
1660
1716
 
1664
1720
      {
1665
1721
        if (init)
1666
1722
        {
1667
 
          insert_pat.append(", ");
 
1723
          dynstr_append_checked(&insert_pat, ", ");
1668
1724
        }
1669
1725
        init=1;
1670
 
        insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
 
1726
        dynstr_append_checked(&insert_pat,
 
1727
                      quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1671
1728
      }
1672
1729
    }
1673
1730
    num_fields= drizzle_num_rows(result);
1701
1758
        fprintf(sql_file, "CREATE TABLE %s (\n", result_table);
1702
1759
      else
1703
1760
        print_xml_tag(sql_file, "\t", "\n", "table_structure", "name=", table, 
1704
 
                NULL);
 
1761
                NullS);
1705
1762
      check_io(sql_file);
1706
1763
    }
1707
1764
 
1708
1765
    if (write_data)
1709
1766
    {
1710
1767
      if (opt_replace_into)
1711
 
        insert_pat.append("REPLACE ");
 
1768
        dynstr_append_checked(&insert_pat, "REPLACE ");
1712
1769
      else
1713
 
        insert_pat.append("INSERT ");
1714
 
      insert_pat.append(insert_option);
1715
 
      insert_pat.append("INTO ");
1716
 
      insert_pat.append(result_table);
 
1770
        dynstr_append_checked(&insert_pat, "INSERT ");
 
1771
      dynstr_append_checked(&insert_pat, insert_option);
 
1772
      dynstr_append_checked(&insert_pat, "INTO ");
 
1773
      dynstr_append_checked(&insert_pat, result_table);
1717
1774
      if (complete_insert)
1718
 
        insert_pat.append(" (");
 
1775
        dynstr_append_checked(&insert_pat, " (");
1719
1776
      else
1720
1777
      {
1721
 
        insert_pat.append(" VALUES ");
 
1778
        dynstr_append_checked(&insert_pat, " VALUES ");
1722
1779
        if (!extended_insert)
1723
 
          insert_pat.append("(");
 
1780
          dynstr_append_checked(&insert_pat, "(");
1724
1781
      }
1725
1782
    }
1726
1783
 
1735
1792
          check_io(sql_file);
1736
1793
        }
1737
1794
        if (complete_insert)
1738
 
          insert_pat.append(", ");
 
1795
          dynstr_append_checked(&insert_pat, ", ");
1739
1796
      }
1740
1797
      init=1;
1741
1798
      if (complete_insert)
1742
 
        insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
 
1799
        dynstr_append_checked(&insert_pat,
 
1800
                      quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1743
1801
      if (!opt_no_create_info)
1744
1802
      {
1745
1803
        if (opt_xml)
1898
1956
  }
1899
1957
  if (complete_insert)
1900
1958
  {
1901
 
    insert_pat.append(") VALUES ");
 
1959
    dynstr_append_checked(&insert_pat, ") VALUES ");
1902
1960
    if (!extended_insert)
1903
 
      insert_pat.append("(");
 
1961
      dynstr_append_checked(&insert_pat, "(");
1904
1962
  }
1905
1963
  if (sql_file != md_result_file)
1906
1964
  {
1911
1969
  return((uint) num_fields);
1912
1970
} /* get_table_structure */
1913
1971
 
1914
 
static void add_load_option(string &str, const char *option,
1915
 
                            const char *option_value)
 
1972
static void add_load_option(DYNAMIC_STRING *str, const char *option,
 
1973
                             const char *option_value)
1916
1974
{
1917
1975
  if (!option_value)
1918
1976
  {
1920
1978
    return;
1921
1979
  }
1922
1980
 
1923
 
  str.append(option);
 
1981
  dynstr_append_checked(str, option);
1924
1982
  
1925
1983
  if (strncmp(option_value, "0x", sizeof("0x")-1) == 0)
1926
1984
  {
1927
1985
    /* It's a hex constant, don't escape */
1928
 
    str.append(option_value);
 
1986
    dynstr_append_checked(str, option_value);
1929
1987
  }
1930
1988
  else
1931
1989
  {
1942
2000
  syntax errors from the SQL parser.
1943
2001
*/
1944
2002
 
1945
 
static void field_escape(string &in, const char *from)
 
2003
static void field_escape(DYNAMIC_STRING* in, const char *from)
1946
2004
{
1947
2005
  uint end_backslashes= 0; 
1948
2006
 
1949
 
  in.append("'");
 
2007
  dynstr_append_checked(in, "'");
1950
2008
 
1951
2009
  while (*from)
1952
2010
  {
1953
 
    in.append(from, 1);
 
2011
    dynstr_append_mem_checked(in, from, 1);
1954
2012
 
1955
2013
    if (*from == '\\')
1956
2014
      end_backslashes^=1;    /* find odd number of backslashes */
1959
2017
      if (*from == '\'' && !end_backslashes)
1960
2018
      {
1961
2019
        /* We want a duplicate of "'" for DRIZZLE */
1962
 
        in.append("\'");
 
2020
        dynstr_append_checked(in, "\'");
1963
2021
      }
1964
2022
      end_backslashes=0;
1965
2023
    }
1967
2025
  }
1968
2026
  /* Add missing backslashes if user has specified odd number of backs.*/
1969
2027
  if (end_backslashes)
1970
 
    in.append("\\");
1971
 
 
1972
 
  in.append("'");
 
2028
    dynstr_append_checked(in, "\\");
 
2029
  
 
2030
  dynstr_append_checked(in, "'");
1973
2031
}
1974
2032
 
1975
2033
 
1994
2052
{
1995
2053
  char ignore_flag;
1996
2054
  char buf[200], table_buff[NAME_LEN+3];
1997
 
  string query_string;
 
2055
  DYNAMIC_STRING query_string;
1998
2056
  char table_type[NAME_LEN];
1999
2057
  char *result_table, table_buff2[NAME_LEN*2+3], *opt_quoted_table;
2000
2058
  int error= 0;
2001
 
  uint32_t         rownr, row_break, total_length, init_length;
 
2059
  ulong         rownr, row_break, total_length, init_length;
2002
2060
  uint num_fields;
2003
2061
  DRIZZLE_RES     *res;
2004
2062
  DRIZZLE_FIELD   *field;
2048
2106
     discarding SHOW CREATE EVENT statements generation. The myslq.event
2049
2107
     table data should be skipped too.
2050
2108
  */
2051
 
  if (!opt_events && !my_strcasecmp(&my_charset_utf8_general_ci, db, "mysql") &&
2052
 
      !my_strcasecmp(&my_charset_utf8_general_ci, table, "event"))
 
2109
  if (!opt_events && !my_strcasecmp(&my_charset_latin1, db, "mysql") &&
 
2110
      !my_strcasecmp(&my_charset_latin1, table, "event"))
2053
2111
  {
2054
2112
    verbose_msg("-- Skipping data table mysql.event, --skip-events was used\n");
2055
2113
    return;
2060
2118
 
2061
2119
  verbose_msg("-- Sending SELECT query...\n");
2062
2120
 
2063
 
  query_string.clear();
2064
 
  query_string.reserve(1024);
 
2121
  init_dynamic_string_checked(&query_string, "", 1024, 1024);
2065
2122
 
2066
2123
  if (path)
2067
2124
  {
2071
2128
      Convert the path to native os format
2072
2129
      and resolve to the full filepath.
2073
2130
    */
2074
 
    convert_dirname(tmp_path,path,NULL);    
 
2131
    convert_dirname(tmp_path,path,NullS);    
2075
2132
    my_load_path(tmp_path, tmp_path, NULL);
2076
2133
    fn_format(filename, table, tmp_path, ".txt", MYF(MY_UNPACK_FILENAME));
2077
2134
 
2083
2140
 
2084
2141
    /* now build the query string */
2085
2142
 
2086
 
    query_string.append( "SELECT * INTO OUTFILE '");
2087
 
    query_string.append( filename);
2088
 
    query_string.append( "'");
 
2143
    dynstr_append_checked(&query_string, "SELECT * INTO OUTFILE '");
 
2144
    dynstr_append_checked(&query_string, filename);
 
2145
    dynstr_append_checked(&query_string, "'");
2089
2146
 
2090
2147
    if (fields_terminated || enclosed || opt_enclosed || escaped)
2091
 
      query_string.append( " FIELDS");
 
2148
      dynstr_append_checked(&query_string, " FIELDS");
2092
2149
    
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);
 
2150
    add_load_option(&query_string, " TERMINATED BY ", fields_terminated);
 
2151
    add_load_option(&query_string, " ENCLOSED BY ", enclosed);
 
2152
    add_load_option(&query_string, " OPTIONALLY ENCLOSED BY ", opt_enclosed);
 
2153
    add_load_option(&query_string, " ESCAPED BY ", escaped);
 
2154
    add_load_option(&query_string, " LINES TERMINATED BY ", lines_terminated);
2098
2155
 
2099
 
    query_string.append( " FROM ");
2100
 
    query_string.append( result_table);
 
2156
    dynstr_append_checked(&query_string, " FROM ");
 
2157
    dynstr_append_checked(&query_string, result_table);
2101
2158
 
2102
2159
    if (where)
2103
2160
    {
2104
 
      query_string.append( " WHERE ");
2105
 
      query_string.append( where);
 
2161
      dynstr_append_checked(&query_string, " WHERE ");
 
2162
      dynstr_append_checked(&query_string, where);
2106
2163
    }
2107
2164
 
2108
2165
    if (order_by)
2109
2166
    {
2110
 
      query_string.append( " ORDER BY ");
2111
 
      query_string.append( order_by);
 
2167
      dynstr_append_checked(&query_string, " ORDER BY ");
 
2168
      dynstr_append_checked(&query_string, order_by);
2112
2169
    }
2113
2170
 
2114
 
    if (drizzle_real_query(drizzle, query_string.c_str(), query_string.length()))
 
2171
    if (drizzle_real_query(drizzle, query_string.str, query_string.length))
2115
2172
    {
2116
2173
      DB_error(drizzle, "when executing 'SELECT INTO OUTFILE'");
 
2174
      dynstr_free(&query_string);
2117
2175
      return;
2118
2176
    }
2119
2177
  }
2126
2184
      check_io(md_result_file);
2127
2185
    }
2128
2186
    
2129
 
    query_string.append( "SELECT * FROM ");
2130
 
    query_string.append( result_table);
 
2187
    dynstr_append_checked(&query_string, "SELECT * FROM ");
 
2188
    dynstr_append_checked(&query_string, result_table);
2131
2189
 
2132
2190
    if (where)
2133
2191
    {
2137
2195
        check_io(md_result_file);
2138
2196
      }
2139
2197
      
2140
 
      query_string.append( " WHERE ");
2141
 
      query_string.append( where);
 
2198
      dynstr_append_checked(&query_string, " WHERE ");
 
2199
      dynstr_append_checked(&query_string, where);
2142
2200
    }
2143
2201
    if (order_by)
2144
2202
    {
2147
2205
        fprintf(md_result_file, "-- ORDER BY:  %s\n", order_by);
2148
2206
        check_io(md_result_file);
2149
2207
      }
2150
 
      query_string.append( " ORDER BY ");
2151
 
      query_string.append( order_by);
 
2208
      dynstr_append_checked(&query_string, " ORDER BY ");
 
2209
      dynstr_append_checked(&query_string, order_by);
2152
2210
    }
2153
2211
 
2154
2212
    if (!opt_xml && !opt_compact)
2156
2214
      fputs("\n", md_result_file);
2157
2215
      check_io(md_result_file);
2158
2216
    }
2159
 
    if (drizzle_query_with_error_report(drizzle, 0, query_string.c_str()))
 
2217
    if (drizzle_query_with_error_report(drizzle, 0, query_string.str))
2160
2218
    {
2161
2219
      DB_error(drizzle, "when retrieving data from server");
2162
2220
      goto err;
2196
2254
    total_length= opt_net_buffer_length;                /* Force row break */
2197
2255
    row_break=0;
2198
2256
    rownr=0;
2199
 
    init_length=(uint) insert_pat.length()+4;
 
2257
    init_length=(uint) insert_pat.length+4;
2200
2258
    if (opt_xml)
2201
2259
      print_xml_tag(md_result_file, "\t", "\n", "table_data", "name=", table,
2202
 
              NULL);
 
2260
              NullS);
2203
2261
    if (opt_autocommit)
2204
2262
    {
2205
2263
      fprintf(md_result_file, "set autocommit=0;\n");
2213
2271
      rownr++;
2214
2272
      if (!extended_insert && !opt_xml)
2215
2273
      {
2216
 
        fputs(insert_pat.c_str(),md_result_file);
 
2274
        fputs(insert_pat.str,md_result_file);
2217
2275
        check_io(md_result_file);
2218
2276
      }
2219
2277
      drizzle_field_seek(res,0);
2227
2285
      for (i= 0; i < drizzle_num_fields(res); i++)
2228
2286
      {
2229
2287
        int is_blob;
2230
 
        uint32_t length= lengths[i];
 
2288
        ulong length= lengths[i];
2231
2289
 
2232
2290
        if (!(field= drizzle_fetch_field(res)))
2233
2291
          die(EX_CONSCHECK,
2240
2298
           we'll dump in hex only BLOB columns.
2241
2299
        */
2242
2300
        is_blob= (opt_hex_blob && field->charsetnr == 63 &&
2243
 
                  (field->type == DRIZZLE_TYPE_VARCHAR ||
2244
 
                   field->type == DRIZZLE_TYPE_BLOB)) ? 1 : 0;
 
2301
                  (field->type == MYSQL_TYPE_STRING ||
 
2302
                   field->type == MYSQL_TYPE_VARCHAR ||
 
2303
                   field->type == MYSQL_TYPE_BLOB)) ? 1 : 0;
2245
2304
        if (extended_insert && !opt_xml)
2246
2305
        {
2247
2306
          if (i == 0)
2248
 
            extended_row= "(";
 
2307
            dynstr_set_checked(&extended_row,"(");
2249
2308
          else
2250
 
            extended_row.append(",");
 
2309
            dynstr_append_checked(&extended_row,",");
2251
2310
 
2252
2311
          if (row[i])
2253
2312
          {
2254
2313
            if (length)
2255
2314
            {
2256
 
              if (!(field->type & NUM_FLAG))
 
2315
              if (!IS_NUM_FIELD(field))
2257
2316
              {
2258
2317
                /*
2259
2318
                  "length * 2 + 2" is OK for both HEX and non-HEX modes:
2263
2322
                  plus 2 bytes for leading and trailing '\'' characters.
2264
2323
                  Also we need to reserve 1 byte for terminating '\0'.
2265
2324
                */
2266
 
                char * tmp_str= (char *)malloc(length * 2 + 2 + 1);
2267
 
                memset(tmp_str, '\0', length * 2 + 2 + 1);
 
2325
                dynstr_realloc_checked(&extended_row,length * 2 + 2 + 1);
2268
2326
                if (opt_hex_blob && is_blob)
2269
2327
                {
2270
 
                  extended_row.append("0x");
2271
 
                  drizzle_hex_string(tmp_str, row[i], length);
2272
 
                  extended_row.append(tmp_str);
 
2328
                  dynstr_append_checked(&extended_row, "0x");
 
2329
                  extended_row.length+= drizzle_hex_string(extended_row.str +
 
2330
                                                         extended_row.length,
 
2331
                                                         row[i], length);
 
2332
                  assert(extended_row.length+1 <= extended_row.max_length);
 
2333
                  /* drizzle_hex_string() already terminated string by '\0' */
 
2334
                  assert(extended_row.str[extended_row.length] == '\0');
2273
2335
                }
2274
2336
                else
2275
2337
                {
2276
 
                  extended_row.append("'");
2277
 
                  drizzle_escape_string(tmp_str,
2278
 
                                        row[i],length);
2279
 
                  extended_row.append(tmp_str);
2280
 
                  extended_row.append("'");
 
2338
                  dynstr_append_checked(&extended_row,"'");
 
2339
                  extended_row.length +=
 
2340
                  drizzle_real_escape_string(&drizzle_connection,
 
2341
                                           &extended_row.str[extended_row.length],
 
2342
                                           row[i],length);
 
2343
                  extended_row.str[extended_row.length]='\0';
 
2344
                  dynstr_append_checked(&extended_row,"'");
2281
2345
                }
2282
 
                free(tmp_str);
2283
2346
              }
2284
2347
              else
2285
2348
              {
2287
2350
                char *ptr= row[i];
2288
2351
                if (my_isalpha(charset_info, *ptr) || (*ptr == '-' &&
2289
2352
                    my_isalpha(charset_info, ptr[1])))
2290
 
                  extended_row.append( "NULL");
 
2353
                  dynstr_append_checked(&extended_row, "NULL");
2291
2354
                else
2292
2355
                {
2293
 
                  extended_row.append( ptr);
 
2356
                  dynstr_append_checked(&extended_row, ptr);
2294
2357
                }
2295
2358
              }
2296
2359
            }
2297
2360
            else
2298
 
              extended_row.append("''");
 
2361
              dynstr_append_checked(&extended_row,"''");
2299
2362
          }
2300
2363
          else
2301
 
            extended_row.append("NULL");
 
2364
            dynstr_append_checked(&extended_row,"NULL");
2302
2365
        }
2303
2366
        else
2304
2367
        {
2309
2372
          }
2310
2373
          if (row[i])
2311
2374
          {
2312
 
            if (!(field->type & NUM_FLAG))
 
2375
            if (!IS_NUM_FIELD(field))
2313
2376
            {
2314
2377
              if (opt_xml)
2315
2378
              {
2317
2380
                {
2318
2381
                  /* Define xsi:type="xs:hexBinary" for hex encoded data */
2319
2382
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2320
 
                                field->name, "xsi:type=", "xs:hexBinary", NULL);
 
2383
                                field->name, "xsi:type=", "xs:hexBinary", NullS);
2321
2384
                  print_blob_as_hex(md_result_file, row[i], length);
2322
2385
                }
2323
2386
                else
2324
2387
                {
2325
2388
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=", 
2326
 
                                field->name, NULL);
 
2389
                                field->name, NullS);
2327
2390
                  print_quoted_xml(md_result_file, row[i], length);
2328
2391
                }
2329
2392
                fputs("</field>\n", md_result_file);
2343
2406
              if (opt_xml)
2344
2407
              {
2345
2408
                print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2346
 
                        field->name, NULL);
 
2409
                        field->name, NullS);
2347
2410
                fputs(!my_isalpha(charset_info, *ptr) ? ptr: "NULL",
2348
2411
                      md_result_file);
2349
2412
                fputs("</field>\n", md_result_file);
2376
2439
 
2377
2440
      if (extended_insert)
2378
2441
      {
2379
 
        uint32_t row_length;
2380
 
        extended_row.append(")");
2381
 
        row_length= 2 + extended_row.length();
 
2442
        ulong row_length;
 
2443
        dynstr_append_checked(&extended_row,")");
 
2444
        row_length= 2 + extended_row.length;
2382
2445
        if (total_length + row_length < opt_net_buffer_length)
2383
2446
        {
2384
2447
          total_length+= row_length;
2385
2448
          fputc(',',md_result_file);            /* Always row break */
2386
 
          fputs(extended_row.c_str(),md_result_file);
 
2449
          fputs(extended_row.str,md_result_file);
2387
2450
        }
2388
2451
        else
2389
2452
        {
2391
2454
            fputs(";\n", md_result_file);
2392
2455
          row_break=1;                          /* This is first row */
2393
2456
 
2394
 
          fputs(insert_pat.c_str(),md_result_file);
2395
 
          fputs(extended_row.c_str(),md_result_file);
 
2457
          fputs(insert_pat.str,md_result_file);
 
2458
          fputs(extended_row.str,md_result_file);
2396
2459
          total_length= row_length+init_length;
2397
2460
        }
2398
2461
        check_io(md_result_file);
2414
2477
    if (drizzle_errno(drizzle))
2415
2478
    {
2416
2479
      snprintf(buf, sizeof(buf),
2417
 
               "%s: Error %d: %s when dumping table %s at row: %d\n",
 
2480
               "%s: Error %d: %s when dumping table %s at row: %ld\n",
2418
2481
               my_progname,
2419
2482
               drizzle_errno(drizzle),
2420
2483
               drizzle_error(drizzle),
2444
2507
    }
2445
2508
    drizzle_free_result(res);
2446
2509
  }
 
2510
  dynstr_free(&query_string);
2447
2511
  return;
2448
2512
 
2449
2513
err:
 
2514
  dynstr_free(&query_string);
2450
2515
  maybe_exit(error);
2451
2516
  return;
2452
2517
} /* dump_table */
2459
2524
 
2460
2525
  if (!res)
2461
2526
  {
2462
 
    if (!(res= drizzle_list_tables(drizzle,NULL)))
 
2527
    if (!(res= drizzle_list_tables(drizzle,NullS)))
2463
2528
      return(NULL);
2464
2529
  }
2465
2530
  if ((row= drizzle_fetch_row(res)))
2567
2632
static int init_dumping(char *database, int init_func(char*))
2568
2633
{
2569
2634
  if (drizzle_get_server_version(drizzle) >= 50003 &&
2570
 
      !my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
 
2635
      !my_strcasecmp(&my_charset_latin1, database, "information_schema"))
2571
2636
    return 1;
2572
2637
 
2573
2638
  if (drizzle_select_db(drizzle, database))
2598
2663
    }
2599
2664
  }
2600
2665
  if (extended_insert)
2601
 
    extended_row.clear();
 
2666
    init_dynamic_string_checked(&extended_row, "", 1024, 1024);
2602
2667
  return 0;
2603
2668
} /* init_dumping */
2604
2669
 
2605
2670
 
2606
2671
/* Return 1 if we should copy the table */
2607
2672
 
2608
 
static bool include_table(const unsigned char *hash_key, size_t len)
 
2673
static bool include_table(const uchar *hash_key, size_t len)
2609
2674
{
2610
2675
  return !hash_search(&ignore_table, hash_key, len);
2611
2676
}
2618
2683
  char table_buff[NAME_LEN*2+3];
2619
2684
  char hash_key[2*NAME_LEN+2];  /* "db.tablename" */
2620
2685
  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);
 
2686
  int using_mysql_db= my_strcasecmp(&my_charset_latin1, database, "mysql");
 
2687
 
 
2688
 
 
2689
  afterdot= strmov(hash_key, database);
2625
2690
  *afterdot++= '.';
2626
2691
 
2627
2692
  if (init_dumping(database, init_dumping_tables))
2628
2693
    return(1);
2629
2694
  if (opt_xml)
2630
 
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NULL);
 
2695
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NullS);
2631
2696
  if (lock_tables)
2632
2697
  {
2633
 
    string query;
2634
 
    query= "LOCK TABLES ";
 
2698
    DYNAMIC_STRING query;
 
2699
    init_dynamic_string_checked(&query, "LOCK TABLES ", 256, 1024);
2635
2700
    for (numrows= 0 ; (table= getTableName(1)) ; )
2636
2701
    {
2637
 
      char *end= my_stpcpy(afterdot, table);
2638
 
      if (include_table((unsigned char*) hash_key,end - hash_key))
 
2702
      char *end= strmov(afterdot, table);
 
2703
      if (include_table((uchar*) hash_key,end - hash_key))
2639
2704
      {
2640
2705
        numrows++;
2641
 
        query.append( quote_name(table, table_buff, 1));
2642
 
        query.append( " READ /*!32311 LOCAL */,");
 
2706
        dynstr_append_checked(&query, quote_name(table, table_buff, 1));
 
2707
        dynstr_append_checked(&query, " READ /*!32311 LOCAL */,");
2643
2708
      }
2644
2709
    }
2645
 
    if (numrows && drizzle_real_query(drizzle, query.c_str(), query.length()-1))
 
2710
    if (numrows && drizzle_real_query(drizzle, query.str, query.length-1))
2646
2711
      DB_error(drizzle, "when using LOCK TABLES");
2647
2712
            /* We shall continue here, if --force was given */
2648
 
    query.clear();
 
2713
    dynstr_free(&query);
2649
2714
  }
2650
2715
  if (flush_logs)
2651
2716
  {
2655
2720
  }
2656
2721
  while ((table= getTableName(0)))
2657
2722
  {
2658
 
    char *end= my_stpcpy(afterdot, table);
2659
 
    if (include_table((unsigned char*) hash_key, end - hash_key))
 
2723
    char *end= strmov(afterdot, table);
 
2724
    if (include_table((uchar*) hash_key, end - hash_key))
2660
2725
    {
2661
2726
      dump_table(table,database);
2662
 
      free(order_by);
 
2727
      my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2663
2728
      order_by= 0;
2664
2729
    }
2665
2730
  }
2669
2734
    check_io(md_result_file);
2670
2735
  }
2671
2736
  if (lock_tables)
2672
 
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
 
2737
    VOID(drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES"));
2673
2738
  if (flush_privileges && using_mysql_db == 0)
2674
2739
  {
2675
2740
    fprintf(md_result_file,"\n--\n-- Flush Grant Tables \n--\n");
2705
2770
           quote_for_like(old_table_name, show_name_buff));
2706
2771
 
2707
2772
  if (drizzle_query_with_error_report(drizzle, 0, query))
2708
 
    return NULL;
 
2773
    return NullS;
2709
2774
 
2710
2775
  if ((table_res= drizzle_store_result(drizzle)))
2711
2776
  {
2730
2795
static int dump_selected_tables(char *db, char **table_names, int tables)
2731
2796
{
2732
2797
  char table_buff[NAME_LEN*2+3];
2733
 
  string lock_tables_query;
 
2798
  DYNAMIC_STRING lock_tables_query;
2734
2799
  MEM_ROOT root;
2735
2800
  char **dump_tables, **pos, **end;
2736
2801
 
2742
2807
  if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *))))
2743
2808
     die(EX_EOM, "alloc_root failure.");
2744
2809
 
2745
 
  lock_tables_query= "LOCK TABLES ";
 
2810
  init_dynamic_string_checked(&lock_tables_query, "LOCK TABLES ", 256, 1024);
2746
2811
  for (; tables > 0 ; tables-- , table_names++)
2747
2812
  {
2748
2813
    /* the table name passed on commandline may be wrong case */
2751
2816
      /* Add found table name to lock_tables_query */
2752
2817
      if (lock_tables)
2753
2818
      {
2754
 
        lock_tables_query.append( quote_name(*pos, table_buff, 1));
2755
 
        lock_tables_query.append( " READ /*!32311 LOCAL */,");
 
2819
        dynstr_append_checked(&lock_tables_query, quote_name(*pos, table_buff, 1));
 
2820
        dynstr_append_checked(&lock_tables_query, " READ /*!32311 LOCAL */,");
2756
2821
      }
2757
2822
      pos++;
2758
2823
    }
2760
2825
    {
2761
2826
      if (!ignore_errors)
2762
2827
      {
 
2828
        dynstr_free(&lock_tables_query);
2763
2829
        free_root(&root, MYF(0));
2764
2830
      }
2765
2831
      maybe_die(EX_ILLEGAL_TABLE, "Couldn't find table: \"%s\"", *table_names);
2770
2836
 
2771
2837
  if (lock_tables)
2772
2838
  {
2773
 
    if (drizzle_real_query(drizzle, lock_tables_query.c_str(),
2774
 
                         lock_tables_query.length()-1))
 
2839
    if (drizzle_real_query(drizzle, lock_tables_query.str,
 
2840
                         lock_tables_query.length-1))
2775
2841
    {
2776
2842
      if (!ignore_errors)
2777
2843
      {
 
2844
        dynstr_free(&lock_tables_query);
2778
2845
        free_root(&root, MYF(0));
2779
2846
      }
2780
2847
      DB_error(drizzle, "when doing LOCK TABLES");
2781
2848
       /* We shall countinue here, if --force was given */
2782
2849
    }
2783
2850
  }
 
2851
  dynstr_free(&lock_tables_query);
2784
2852
  if (flush_logs)
2785
2853
  {
2786
2854
    if (drizzle_refresh(drizzle, REFRESH_LOG))
2792
2860
     /* We shall countinue here, if --force was given */
2793
2861
  }
2794
2862
  if (opt_xml)
2795
 
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NULL);
 
2863
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NullS);
2796
2864
 
2797
2865
  /* Dump each selected table */
2798
2866
  for (pos= dump_tables; pos < end; pos++)
2799
2867
    dump_table(*pos, db);
2800
2868
 
2801
2869
  free_root(&root, MYF(0));
2802
 
  free(order_by);
 
2870
  my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2803
2871
  order_by= 0;
2804
2872
  if (opt_xml)
2805
2873
  {
2807
2875
    check_io(md_result_file);
2808
2876
  }
2809
2877
  if (lock_tables)
2810
 
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
 
2878
    VOID(drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES"));
2811
2879
  return(0);
2812
2880
} /* dump_selected_tables */
2813
2881
 
3024
3092
 
3025
3093
static int purge_bin_logs_to(DRIZZLE *drizzle_con, char* log_name)
3026
3094
{
 
3095
  DYNAMIC_STRING str;
3027
3096
  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());
 
3097
  init_dynamic_string_checked(&str, "PURGE BINARY LOGS TO '", 1024, 1024);
 
3098
  dynstr_append_checked(&str, log_name);
 
3099
  dynstr_append_checked(&str, "'");
 
3100
  err = drizzle_query_with_error_report(drizzle_con, 0, str.str);
 
3101
  dynstr_free(&str);
3032
3102
  return err;
3033
3103
}
3034
3104
 
3065
3135
}
3066
3136
 
3067
3137
 
3068
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint length,
 
3138
static ulong find_set(TYPELIB *lib, const char *x, uint length,
3069
3139
                      char **err_pos, uint *err_len)
3070
3140
{
3071
3141
  const char *end= x + length;
3072
 
  uint32_t found= 0;
 
3142
  ulong found= 0;
3073
3143
  uint find;
3074
3144
  char buff[255];
3075
3145
 
3084
3154
    for (;;)
3085
3155
    {
3086
3156
      const char *pos= start;
3087
 
      uint32_t var_len;
 
3157
      uint var_len;
3088
3158
 
3089
3159
      for (; pos != end && *pos != ','; pos++) ;
3090
 
      var_len= (uint32_t) (pos - start);
3091
 
      strmake(buff, start, min((uint32_t)sizeof(buff), var_len));
 
3160
      var_len= (uint) (pos - start);
 
3161
      strmake(buff, start, min(sizeof(buff), var_len));
3092
3162
      find= find_type(buff, lib, var_len);
3093
3163
      if (!find)
3094
3164
      {
3213
3283
      If these two types, we do want to skip dumping the table
3214
3284
    */
3215
3285
    if (!opt_no_data &&
3216
 
        (!my_strcasecmp(&my_charset_utf8_general_ci, table_type, "MRG_MyISAM") ||
 
3286
        (!my_strcasecmp(&my_charset_latin1, table_type, "MRG_MyISAM") ||
3217
3287
         !strcmp(table_type,"MRG_ISAM")))
3218
3288
      result= IGNORE_DATA;
3219
3289
  }
3284
3354
  {
3285
3355
    char *end;
3286
3356
    /* result (terminating \0 is already in result_length) */
3287
 
    result= (char *)my_malloc(result_length + 10, MYF(MY_WME));
 
3357
    result= my_malloc(result_length + 10, MYF(MY_WME));
3288
3358
    if (!result)
3289
3359
    {
3290
3360
      fprintf(stderr, "Error: Not enough memory to store ORDER BY clause\n");
3293
3363
    drizzle_data_seek(res, 0);
3294
3364
    row= drizzle_fetch_row(res);
3295
3365
    quoted_field= quote_name(row[4], buff, 0);
3296
 
    end= my_stpcpy(result, quoted_field);
 
3366
    end= strmov(result, quoted_field);
3297
3367
    while ((row= drizzle_fetch_row(res)) && atoi(row[3]) > 1)
3298
3368
    {
3299
3369
      quoted_field= quote_name(row[4], buff, 0);
3300
 
      end= strxmov(end, ",", quoted_field, NULL);
 
3370
      end= strxmov(end, ",", quoted_field, NullS);
3301
3371
    }
3302
3372
  }
3303
3373
 
3308
3378
  return result;
3309
3379
}
3310
3380
 
 
3381
/*
 
3382
  The following functions are wrappers for the dynamic string functions
 
3383
  and if they fail, the wrappers will terminate the current process.
 
3384
*/
 
3385
 
 
3386
#define DYNAMIC_STR_ERROR_MSG "Couldn't perform DYNAMIC_STRING operation"
 
3387
 
 
3388
static void init_dynamic_string_checked(DYNAMIC_STRING *str, const char *init_str,
 
3389
                            uint init_alloc, uint alloc_increment)
 
3390
{
 
3391
  if (init_dynamic_string(str, init_str, init_alloc, alloc_increment))
 
3392
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3393
}
 
3394
 
 
3395
static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src)
 
3396
{
 
3397
  if (dynstr_append(dest, src))
 
3398
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3399
}
 
3400
 
 
3401
static void dynstr_set_checked(DYNAMIC_STRING *str, const char *init_str)
 
3402
{
 
3403
  if (dynstr_set(str, init_str))
 
3404
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3405
}
 
3406
 
 
3407
static void dynstr_append_mem_checked(DYNAMIC_STRING *str, const char *append,
 
3408
                          uint length)
 
3409
{
 
3410
  if (dynstr_append_mem(str, append, length))
 
3411
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3412
}
 
3413
 
 
3414
static void dynstr_realloc_checked(DYNAMIC_STRING *str, ulong additional_size)
 
3415
{
 
3416
  if (dynstr_realloc(str, additional_size))
 
3417
    die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
 
3418
}
 
3419
 
3311
3420
 
3312
3421
int main(int argc, char **argv)
3313
3422
{
3317
3426
 
3318
3427
  compatible_mode_normal_str[0]= 0;
3319
3428
  default_charset= (char *)drizzle_universal_client_charset;
3320
 
  memset(&ignore_table, 0, sizeof(ignore_table));
 
3429
  bzero((char*) &ignore_table, sizeof(ignore_table));
3321
3430
 
3322
3431
  exit_code= get_options(&argc, &argv);
3323
3432
  if (exit_code)