~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/mysqldump.c

  • Committer: Stewart Smith
  • Date: 2008-07-13 06:56:15 UTC
  • mto: (210.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 211.
  • Revision ID: stewart@flamingspork.com-20080713065615-vzok75kgnnviokl9
Move MD5() into a UDF

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright 2000-2008 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
2
 
 
3
 
  This program is free software; you can redistribute it and/or modify
4
 
  it under the terms of the GNU General Public License as published by
5
 
  the Free Software Foundation; version 2 of the License.
6
 
 
7
 
  This program is distributed in the hope that it will be useful,
8
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
  GNU General Public License for more details.
11
 
 
12
 
  You should have received a copy of the GNU General Public License
13
 
  along with this program; if not, write to the Free Software
14
 
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
16
 
/* drizzledump.cc  - Dump a tables contents and format to an ASCII file
17
 
 
18
 
 * Derived from mysqldump, which originally came from:
19
 
 **
20
 
 ** The author's original notes follow :-
21
 
 **
22
 
 ** AUTHOR: Igor Romanenko (igor@frog.kiev.ua)
23
 
 ** DATE:   December 3, 1994
24
 
 ** WARRANTY: None, expressed, impressed, implied
25
 
 **          or other
26
 
 ** STATUS: Public domain
27
 
 
28
 
 * and more work by Monty, Jani & Sinisa
29
 
 * and all the MySQL developers over the years.
 
1
/* Copyright (C) 2000 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/* mysqldump.c  - Dump a tables contents and format to an ASCII file
 
17
**
 
18
** The author's original notes follow :-
 
19
**
 
20
** AUTHOR: Igor Romanenko (igor@frog.kiev.ua)
 
21
** DATE:   December 3, 1994
 
22
** WARRANTY: None, expressed, impressed, implied
 
23
**          or other
 
24
** STATUS: Public domain
 
25
** Adapted and optimized for MySQL 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 mysqldump 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
30
37
*/
31
38
 
 
39
#define DUMP_VERSION "10.13"
 
40
 
 
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
 
32
48
#include "client_priv.h"
33
 
#include <string>
34
 
 
35
 
#include "drizzled/internal/my_sys.h"
36
 
#include "drizzled/internal/m_string.h"
37
 
#include "drizzled/charset_info.h"
38
 
#include "drizzled/hash.h"
39
 
#include <stdarg.h>
40
 
#include <algorithm>
41
 
 
42
 
#include <drizzled/gettext.h>
43
 
 
44
 
#include <drizzled/error.h>
45
 
 
46
 
using namespace std;
47
 
using namespace drizzled;
 
49
#include "drizzle_version.h"
 
50
#include "mysqld_error.h"
48
51
 
49
52
/* Exit codes */
50
53
 
51
54
#define EX_USAGE 1
52
 
#define EX_DRIZZLEERR 2
 
55
#define EX_MYSQLERR 2
53
56
#define EX_CONSCHECK 3
54
57
#define EX_EOM 4
55
58
#define EX_EOF 5 /* ferror for output file was got */
56
59
#define EX_ILLEGAL_TABLE 6
57
 
#define EX_TABLE_STATUS 7
58
60
 
59
61
/* index into 'show fields from table' */
60
62
 
66
68
 
67
69
/* Size of buffer for dump's select query */
68
70
#define QUERY_LENGTH 1536
69
 
#define DRIZZLE_MAX_LINE_LENGTH 1024*1024L-1025
70
71
 
71
72
/* ignore table flags */
72
73
#define IGNORE_NONE 0x00 /* no ignore */
73
74
#define IGNORE_DATA 0x01 /* don't dump data for this table */
74
75
#define IGNORE_INSERT_DELAYED 0x02 /* table doesn't support INSERT DELAYED */
75
76
 
76
 
static void add_load_option(string &str, const char *option,
77
 
                            const char *option_value);
78
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint32_t length,
79
 
                         char **err_pos, uint32_t *err_len);
 
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,
 
80
                      char **err_pos, uint *err_len);
80
81
 
81
 
static void field_escape(string &in, const char *from);
82
 
static bool  verbose= false;
83
 
static bool opt_no_create_info= false;
84
 
static bool opt_no_data= false;
85
 
static bool opt_mysql= false;
86
 
static bool quick= true;
87
 
static bool extended_insert= true;
88
 
static bool ignore_errors= false;
89
 
static bool flush_logs= false;
90
 
static bool opt_drop= true; 
91
 
static bool opt_keywords= false;
92
 
static bool opt_compress= false;
93
 
static bool opt_delayed= false; 
94
 
static bool create_options= true; 
95
 
static bool opt_quoted= false;
96
 
static bool opt_databases= false; 
97
 
static bool opt_alldbs= false; 
98
 
static bool opt_create_db= false;
99
 
static bool opt_lock_all_tables= false;
100
 
static bool opt_set_charset= false; 
101
 
static bool opt_dump_date= true;
102
 
static bool opt_autocommit= false; 
103
 
static bool opt_disable_keys= true;
104
 
static bool opt_xml= false;
105
 
static bool tty_password= false;
106
 
static bool opt_single_transaction= false; 
107
 
static bool opt_comments= false;
108
 
static bool opt_compact= false;
109
 
static bool opt_hex_blob= false;
110
 
static bool opt_order_by_primary=false; 
111
 
static bool opt_ignore= false;
112
 
static bool opt_complete_insert= false;
113
 
static bool opt_drop_database= false;
114
 
static bool opt_replace_into= false;
115
 
static bool opt_routines= false;
116
 
static bool opt_alltspcs= false;
117
 
static uint32_t show_progress_size= 0;
118
 
static uint64_t total_rows= 0;
119
 
static drizzle_st drizzle;
120
 
static drizzle_con_st dcon;
121
 
static string insert_pat;
122
 
static char  *opt_password= NULL;
123
 
static char *current_user= NULL;
124
 
static char  *current_host= NULL;
125
 
static char *path= NULL;
126
 
static char *fields_terminated= NULL;
127
 
static char *lines_terminated= NULL; 
128
 
static char *enclosed= NULL;
129
 
static char *opt_enclosed= NULL;
130
 
static char *escaped= NULL;
131
 
static char *where= NULL; 
132
 
static char *order_by= NULL;
133
 
static char *opt_compatible_mode_str= NULL;
134
 
static char *err_ptr= NULL;
135
 
static char **defaults_argv= NULL;
 
82
static void field_escape(DYNAMIC_STRING* in, const char *from);
 
83
static my_bool  verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
 
84
                quick= 1, extended_insert= 1,
 
85
                lock_tables=1,ignore_errors=0,flush_logs=0,flush_privileges=0,
 
86
                opt_drop=1,opt_keywords=0,opt_lock=1,opt_compress=0,
 
87
                opt_delayed=0,create_options=1,opt_quoted=0,opt_databases=0,
 
88
                opt_alldbs=0,opt_create_db=0,opt_lock_all_tables=0,
 
89
                opt_set_charset=0, opt_dump_date=1,
 
90
                opt_autocommit=0,opt_disable_keys=1,opt_xml=0,
 
91
                opt_delete_master_logs=0, tty_password=0,
 
92
                opt_single_transaction=0, opt_comments= 0, opt_compact= 0,
 
93
                opt_hex_blob=0, opt_order_by_primary=0, opt_ignore=0,
 
94
                opt_complete_insert= 0, opt_drop_database= 0,
 
95
                opt_replace_into= 0,
 
96
                opt_routines=0, opt_tz_utc=1,
 
97
                opt_slave_apply= 0, 
 
98
                opt_include_master_host_port= 0,
 
99
                opt_events= 0,
 
100
                opt_alltspcs=0, opt_notspcs= 0;
 
101
static my_bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0;
 
102
static ulong opt_max_allowed_packet, opt_net_buffer_length;
 
103
static MYSQL mysql_connection,*mysql=0;
 
104
static DYNAMIC_STRING insert_pat;
 
105
static char  *opt_password=0,*current_user=0,
 
106
             *current_host=0,*path=0,*fields_terminated=0,
 
107
             *lines_terminated=0, *enclosed=0, *opt_enclosed=0, *escaped=0,
 
108
             *where=0, *order_by=0,
 
109
             *opt_compatible_mode_str= 0,
 
110
             *err_ptr= 0,
 
111
             *log_error_file= NULL;
 
112
static char **defaults_argv= 0;
136
113
static char compatible_mode_normal_str[255];
137
 
static uint32_t opt_compatible_mode= 0;
138
 
static uint32_t opt_drizzle_port= 0;
139
 
static int first_error= 0;
140
 
static string extended_row;
 
114
/* Server supports character_set_results session variable? */
 
115
static my_bool server_supports_switching_charsets= TRUE;
 
116
static ulong opt_compatible_mode= 0;
 
117
#define MYSQL_OPT_MASTER_DATA_EFFECTIVE_SQL 1
 
118
#define MYSQL_OPT_MASTER_DATA_COMMENTED_SQL 2
 
119
#define MYSQL_OPT_SLAVE_DATA_EFFECTIVE_SQL 1
 
120
#define MYSQL_OPT_SLAVE_DATA_COMMENTED_SQL 2
 
121
static uint opt_mysql_port= 0, opt_master_data;
 
122
static uint opt_slave_data;
 
123
static uint my_end_arg;
 
124
static int   first_error=0;
 
125
static DYNAMIC_STRING extended_row;
141
126
FILE *md_result_file= 0;
142
 
FILE *stderror_file= 0;
143
 
 
144
 
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
145
 
 
146
 
static const char *compatible_mode_names[]=
 
127
FILE *stderror_file=0;
 
128
 
 
129
#ifdef HAVE_SMEM
 
130
static char *shared_memory_base_name=0;
 
131
#endif
 
132
static uint opt_protocol= MYSQL_PROTOCOL_TCP;
 
133
 
 
134
/*
 
135
Dynamic_string wrapper functions. In this file use these
 
136
wrappers, they will terminate the process if there is
 
137
an allocation failure.
 
138
*/
 
139
static void init_dynamic_string_checked(DYNAMIC_STRING *str, const char *init_str,
 
140
                            uint init_alloc, uint alloc_increment);
 
141
static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src);
 
142
static void dynstr_set_checked(DYNAMIC_STRING *str, const char *init_str);
 
143
static void dynstr_append_mem_checked(DYNAMIC_STRING *str, const char *append,
 
144
                          uint length);
 
145
static void dynstr_realloc_checked(DYNAMIC_STRING *str, ulong additional_size);
 
146
/*
 
147
  Constant for detection of default value of default_charset.
 
148
  If default_charset is equal to mysql_universal_client_charset, then
 
149
  it is the default value which assigned at the very beginning of main().
 
150
*/
 
151
static const char *mysql_universal_client_charset=
 
152
  MYSQL_UNIVERSAL_CLIENT_CHARSET;
 
153
static char *default_charset;
 
154
static CHARSET_INFO *charset_info= &my_charset_latin1;
 
155
const char *default_dbug_option="d:t:o,/tmp/mysqldump.trace";
 
156
/* have we seen any VIEWs during table scanning? */
 
157
my_bool seen_views= 0;
 
158
const char *compatible_mode_names[]=
147
159
{
148
160
  "MYSQL323", "MYSQL40", "POSTGRESQL", "ORACLE", "MSSQL", "DB2",
149
161
  "MAXDB", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS",
150
162
  "ANSI",
151
 
  NULL
 
163
  NullS
152
164
};
153
 
static TYPELIB compatible_mode_typelib= {array_elements(compatible_mode_names) - 1,
154
 
  "", compatible_mode_names, NULL};
155
 
 
156
 
drizzled::hash_set<string> ignore_table;
157
 
 
158
 
static struct option my_long_options[] =
 
165
#define MASK_ANSI_QUOTES \
 
166
(\
 
167
 (1<<2)  | /* POSTGRESQL */\
 
168
 (1<<3)  | /* ORACLE     */\
 
169
 (1<<4)  | /* MSSQL      */\
 
170
 (1<<5)  | /* DB2        */\
 
171
 (1<<6)  | /* MAXDB      */\
 
172
 (1<<10)   /* ANSI       */\
 
173
)
 
174
TYPELIB compatible_mode_typelib= {array_elements(compatible_mode_names) - 1,
 
175
                                  "", compatible_mode_names, NULL};
 
176
 
 
177
HASH ignore_table;
 
178
 
 
179
static struct my_option my_long_options[] =
159
180
{
160
181
  {"all", 'a', "Deprecated. Use --create-options instead.",
161
 
    (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
162
 
    0, 0, 0, 0, 0},
 
182
   (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
 
183
   0, 0, 0, 0, 0},
163
184
  {"all-databases", 'A',
164
 
    "Dump all the databases. This will be same as --databases with all databases selected.",
165
 
    (char**) &opt_alldbs, (char**) &opt_alldbs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
166
 
    0, 0},
 
185
   "Dump all the databases. This will be same as --databases with all databases selected.",
 
186
   (char**) &opt_alldbs, (char**) &opt_alldbs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
187
   0, 0},
167
188
  {"all-tablespaces", 'Y',
168
 
    "Dump all the tablespaces.",
169
 
    (char**) &opt_alltspcs, (char**) &opt_alltspcs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
170
 
    0, 0},
 
189
   "Dump all the tablespaces.",
 
190
   (char**) &opt_alltspcs, (char**) &opt_alltspcs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
191
   0, 0},
 
192
  {"no-tablespaces", 'y',
 
193
   "Do not dump any tablespace information.",
 
194
   (char**) &opt_notspcs, (char**) &opt_notspcs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
195
   0, 0},
171
196
  {"add-drop-database", OPT_DROP_DATABASE, "Add a 'DROP DATABASE' before each create.",
172
 
    (char**) &opt_drop_database, (char**) &opt_drop_database, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
173
 
    0},
 
197
   (char**) &opt_drop_database, (char**) &opt_drop_database, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
 
198
   0},
174
199
  {"add-drop-table", OPT_DROP, "Add a 'drop table' before each create.",
175
 
    (char**) &opt_drop, (char**) &opt_drop, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0,
176
 
    0},
 
200
   (char**) &opt_drop, (char**) &opt_drop, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0,
 
201
   0},
 
202
  {"add-locks", OPT_LOCKS, "Add locks around insert statements.",
 
203
   (char**) &opt_lock, (char**) &opt_lock, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0,
 
204
   0},
177
205
  {"allow-keywords", OPT_KEYWORDS,
178
 
    "Allow creation of column names that are keywords.", (char**) &opt_keywords,
179
 
    (char**) &opt_keywords, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
206
   "Allow creation of column names that are keywords.", (char**) &opt_keywords,
 
207
   (char**) &opt_keywords, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
208
  {"apply-slave-statements", OPT_MYSQLDUMP_SLAVE_APPLY,
 
209
   "Adds 'STOP SLAVE' prior to 'CHANGE MASTER' and 'START SLAVE' to bottom of dump.",
 
210
   (char**) &opt_slave_apply, (char**) &opt_slave_apply, 0, GET_BOOL, NO_ARG,
 
211
   0, 0, 0, 0, 0, 0},
 
212
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
213
   "Directory where character sets are.", (char**) &charsets_dir,
 
214
   (char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
180
215
  {"comments", 'i', "Write additional information.",
181
 
    (char**) &opt_comments, (char**) &opt_comments, 0, GET_BOOL, NO_ARG,
182
 
    1, 0, 0, 0, 0, 0},
 
216
   (char**) &opt_comments, (char**) &opt_comments, 0, GET_BOOL, NO_ARG,
 
217
   1, 0, 0, 0, 0, 0},
183
218
  {"compatible", OPT_COMPATIBLE,
184
 
    "Change the dump to be compatible with a given mode. By default tables are dumped in a format optimized for MySQL. Legal modes are: ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_table_options, no_field_options. One can use several modes separated by commas. Note: Requires DRIZZLE server version 4.1.0 or higher. This option is ignored with earlier server versions.",
185
 
    (char**) &opt_compatible_mode_str, (char**) &opt_compatible_mode_str, 0,
186
 
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
219
   "Change the dump to be compatible with a given mode. By default tables are dumped in a format optimized for MySQL. Legal modes are: ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_table_options, no_field_options. One can use several modes separated by commas. Note: Requires MySQL server version 4.1.0 or higher. This option is ignored with earlier server versions.",
 
220
   (char**) &opt_compatible_mode_str, (char**) &opt_compatible_mode_str, 0,
 
221
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
187
222
  {"compact", OPT_COMPACT,
188
 
    "Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs.  Enables options --skip-add-drop-table --no-set-names --skip-disable-keys --skip-add-locks",
189
 
    (char**) &opt_compact, (char**) &opt_compact, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
190
 
    0, 0},
 
223
   "Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs.  Enables options --skip-add-drop-table --no-set-names --skip-disable-keys --skip-add-locks",
 
224
   (char**) &opt_compact, (char**) &opt_compact, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
225
   0, 0},
191
226
  {"complete-insert", 'c', "Use complete insert statements.",
192
 
    (char**) &opt_complete_insert, (char**) &opt_complete_insert, 0, GET_BOOL,
193
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
227
   (char**) &opt_complete_insert, (char**) &opt_complete_insert, 0, GET_BOOL,
 
228
   NO_ARG, 0, 0, 0, 0, 0, 0},
194
229
  {"compress", 'C', "Use compression in server/client protocol.",
195
 
    (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
196
 
    0, 0, 0},
 
230
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
231
   0, 0, 0},
197
232
  {"create-options", OPT_CREATE_OPTIONS,
198
 
    "Include all DRIZZLE specific create options.",
199
 
    (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
200
 
    0, 0, 0, 0, 0},
 
233
   "Include all MySQL specific create options.",
 
234
   (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
 
235
   0, 0, 0, 0, 0},
201
236
  {"databases", 'B',
202
 
    "To dump several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames. 'USE db_name;' will be included in the output.",
203
 
    (char**) &opt_databases, (char**) &opt_databases, 0, GET_BOOL, NO_ARG, 0, 0,
204
 
    0, 0, 0, 0},
 
237
   "To dump several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames. 'USE db_name;' will be included in the output.",
 
238
   (char**) &opt_databases, (char**) &opt_databases, 0, GET_BOOL, NO_ARG, 0, 0,
 
239
   0, 0, 0, 0},
 
240
#ifdef DBUG_OFF
 
241
  {"debug", '#', "This is a non-debug version. Catch this and exit",
 
242
   0,0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
243
#else
 
244
  {"debug", '#', "Output debug log", (char**) &default_dbug_option,
 
245
   (char**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
246
#endif
 
247
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
 
248
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
 
249
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
250
  {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
 
251
   (char**) &debug_info_flag, (char**) &debug_info_flag,
 
252
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
253
  {"default-character-set", OPT_DEFAULT_CHARSET,
 
254
   "Set the default character set.", (char**) &default_charset,
 
255
   (char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
205
256
  {"delayed-insert", OPT_DELAYED, "Insert rows with INSERT DELAYED; ",
206
 
    (char**) &opt_delayed, (char**) &opt_delayed, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
207
 
    0, 0},
 
257
   (char**) &opt_delayed, (char**) &opt_delayed, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
258
   0, 0},
 
259
  {"delete-master-logs", OPT_DELETE_MASTER_LOGS,
 
260
   "Delete logs on master after backup. This automatically enables --master-data.",
 
261
   (char**) &opt_delete_master_logs, (char**) &opt_delete_master_logs, 0,
 
262
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
208
263
  {"disable-keys", 'K',
209
 
    "'ALTER TABLE tb_name DISABLE KEYS; and 'ALTER TABLE tb_name ENABLE KEYS; will be put in the output.", (char**) &opt_disable_keys,
210
 
    (char**) &opt_disable_keys, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
264
   "'/*!40000 ALTER TABLE tb_name DISABLE KEYS */; and '/*!40000 ALTER TABLE tb_name ENABLE KEYS */; will be put in the output.", (char**) &opt_disable_keys,
 
265
   (char**) &opt_disable_keys, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
266
  {"dump-slave", OPT_MYSQLDUMP_SLAVE_DATA,
 
267
   "This causes the binary log position and filename of the master to be "
 
268
   "appended to the dumped data output. Setting the value to 1, will print"
 
269
   "it as a CHANGE MASTER command in the dumped data output; if equal"
 
270
   " to 2, that command will be prefixed with a comment symbol. "
 
271
   "This option will turn --lock-all-tables on, unless "
 
272
   "--single-transaction is specified too (in which case a "
 
273
   "global read lock is only taken a short time at the beginning of the dump "
 
274
   "- don't forget to read about --single-transaction below). In all cases "
 
275
   "any action on logs will happen at the exact moment of the dump."
 
276
   "Option automatically turns --lock-tables off.",
 
277
   (char**) &opt_slave_data, (char**) &opt_slave_data, 0,
 
278
   GET_UINT, OPT_ARG, 0, 0, MYSQL_OPT_SLAVE_DATA_COMMENTED_SQL, 0, 0, 0},
 
279
  {"events", 'E', "Dump events.",
 
280
     (char**) &opt_events, (char**) &opt_events, 0, GET_BOOL,
 
281
     NO_ARG, 0, 0, 0, 0, 0, 0},
211
282
  {"extended-insert", 'e',
212
 
    "Allows utilization of the new, much faster INSERT syntax.",
213
 
    (char**) &extended_insert, (char**) &extended_insert, 0, GET_BOOL, NO_ARG,
214
 
    1, 0, 0, 0, 0, 0},
 
283
   "Allows utilization of the new, much faster INSERT syntax.",
 
284
   (char**) &extended_insert, (char**) &extended_insert, 0, GET_BOOL, NO_ARG,
 
285
   1, 0, 0, 0, 0, 0},
215
286
  {"fields-terminated-by", OPT_FTB,
216
 
    "Fields in the textfile are terminated by ...", (char**) &fields_terminated,
217
 
    (char**) &fields_terminated, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
287
   "Fields in the textfile are terminated by ...", (char**) &fields_terminated,
 
288
   (char**) &fields_terminated, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
218
289
  {"fields-enclosed-by", OPT_ENC,
219
 
    "Fields in the importfile are enclosed by ...", (char**) &enclosed,
220
 
    (char**) &enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
 
290
   "Fields in the importfile are enclosed by ...", (char**) &enclosed,
 
291
   (char**) &enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
221
292
  {"fields-optionally-enclosed-by", OPT_O_ENC,
222
 
    "Fields in the i.file are opt. enclosed by ...", (char**) &opt_enclosed,
223
 
    (char**) &opt_enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
 
293
   "Fields in the i.file are opt. enclosed by ...", (char**) &opt_enclosed,
 
294
   (char**) &opt_enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
224
295
  {"fields-escaped-by", OPT_ESC, "Fields in the i.file are escaped by ...",
225
 
    (char**) &escaped, (char**) &escaped, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
296
   (char**) &escaped, (char**) &escaped, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
297
  {"first-slave", 'x', "Deprecated, renamed to --lock-all-tables.",
 
298
   (char**) &opt_lock_all_tables, (char**) &opt_lock_all_tables, 0, GET_BOOL, NO_ARG,
 
299
   0, 0, 0, 0, 0, 0},
226
300
  {"flush-logs", 'F', "Flush logs file in server before starting dump. "
227
 
    "Note that if you dump many databases at once (using the option "
228
 
      "--databases= or --all-databases), the logs will be flushed for "
229
 
      "each database dumped. The exception is when using --lock-all-tables "
230
 
      "in this case the logs will be flushed only once, corresponding "
231
 
      "to the moment all tables are locked. So if you want your dump and "
232
 
      "the log flush to happen at the same exact moment you should use "
233
 
      "--lock-all-tables or --flush-logs",
234
 
    (char**) &flush_logs, (char**) &flush_logs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
235
 
    0, 0},
 
301
   "Note that if you dump many databases at once (using the option "
 
302
   "--databases= or --all-databases), the logs will be flushed for "
 
303
   "each database dumped. The exception is when using --lock-all-tables "
 
304
   "or --master-data: "
 
305
   "in this case the logs will be flushed only once, corresponding "
 
306
   "to the moment all tables are locked. So if you want your dump and "
 
307
   "the log flush to happen at the same exact moment you should use "
 
308
   "--lock-all-tables or --master-data with --flush-logs",
 
309
   (char**) &flush_logs, (char**) &flush_logs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
310
   0, 0},
 
311
  {"flush-privileges", OPT_ESC, "Emit a FLUSH PRIVILEGES statement "
 
312
   "after dumping the mysql database.  This option should be used any "
 
313
   "time the dump contains the mysql database and any other database "
 
314
   "that depends on the data in the mysql database for proper restore. ",
 
315
   (char**) &flush_privileges, (char**) &flush_privileges, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
316
   0, 0},
236
317
  {"force", 'f', "Continue even if we get an sql-error.",
237
 
    (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG,
238
 
    0, 0, 0, 0, 0, 0},
 
318
   (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG,
 
319
   0, 0, 0, 0, 0, 0},
239
320
  {"help", '?', "Display this help message and exit.", 0, 0, 0, GET_NO_ARG,
240
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
321
   NO_ARG, 0, 0, 0, 0, 0, 0},
241
322
  {"hex-blob", OPT_HEXBLOB, "Dump binary strings (BINARY, "
242
323
    "VARBINARY, BLOB) in hexadecimal format.",
243
 
    (char**) &opt_hex_blob, (char**) &opt_hex_blob, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
324
   (char**) &opt_hex_blob, (char**) &opt_hex_blob, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
244
325
  {"host", 'h', "Connect to host.", (char**) &current_host,
245
 
    (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
326
   (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
246
327
  {"ignore-table", OPT_IGNORE_TABLE,
247
 
    "Do not dump the specified table. To specify more than one table to ignore, "
248
 
      "use the directive multiple times, once for each table.  Each table must "
249
 
      "be specified with both database and table names, e.g. --ignore-table=database.table",
250
 
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
328
   "Do not dump the specified table. To specify more than one table to ignore, "
 
329
   "use the directive multiple times, once for each table.  Each table must "
 
330
   "be specified with both database and table names, e.g. --ignore-table=database.table",
 
331
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
332
  {"include-master-host-port", OPT_MYSQLDUMP_INCLUDE_MASTER_HOST_PORT,
 
333
   "Adds 'MASTER_HOST=<host>, MASTER_PORT=<port>' to 'CHANGE MASTER TO..' in dump produced with --dump-slave.",
 
334
   (char**) &opt_include_master_host_port, 
 
335
   (char**) &opt_include_master_host_port, 
 
336
   0, GET_BOOL, NO_ARG,
 
337
   0, 0, 0, 0, 0, 0},
251
338
  {"insert-ignore", OPT_INSERT_IGNORE, "Insert rows with INSERT IGNORE.",
252
 
    (char**) &opt_ignore, (char**) &opt_ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
253
 
    0, 0},
 
339
   (char**) &opt_ignore, (char**) &opt_ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
340
   0, 0},
254
341
  {"lines-terminated-by", OPT_LTB, "Lines in the i.file are terminated by ...",
255
 
    (char**) &lines_terminated, (char**) &lines_terminated, 0, GET_STR,
256
 
    REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
342
   (char**) &lines_terminated, (char**) &lines_terminated, 0, GET_STR,
 
343
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
257
344
  {"lock-all-tables", 'x', "Locks all tables across all databases. This "
258
 
    "is achieved by taking a global read lock for the duration of the whole "
259
 
      "dump. Automatically turns --single-transaction and --lock-tables off.",
260
 
    (char**) &opt_lock_all_tables, (char**) &opt_lock_all_tables, 0, GET_BOOL, NO_ARG,
261
 
    0, 0, 0, 0, 0, 0},
262
 
  {"mysql", 'm', N_("Use MySQL Protocol."),
263
 
    (char**) &opt_mysql, (char**) &opt_mysql, 0, GET_BOOL, NO_ARG, 1, 0, 0,
264
 
    0, 0, 0},
 
345
   "is achieved by taking a global read lock for the duration of the whole "
 
346
   "dump. Automatically turns --single-transaction and --lock-tables off.",
 
347
   (char**) &opt_lock_all_tables, (char**) &opt_lock_all_tables, 0, GET_BOOL, NO_ARG,
 
348
   0, 0, 0, 0, 0, 0},
 
349
  {"lock-tables", 'l', "Lock all tables for read.", (char**) &lock_tables,
 
350
   (char**) &lock_tables, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
351
  {"log-error", OPT_ERROR_LOG_FILE, "Append warnings and errors to given file.",
 
352
   (char**) &log_error_file, (char**) &log_error_file, 0, GET_STR,
 
353
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
354
  {"master-data", OPT_MASTER_DATA,
 
355
   "This causes the binary log position and filename to be appended to the "
 
356
   "output. If equal to 1, will print it as a CHANGE MASTER command; if equal"
 
357
   " to 2, that command will be prefixed with a comment symbol. "
 
358
   "This option will turn --lock-all-tables on, unless "
 
359
   "--single-transaction is specified too (in which case a "
 
360
   "global read lock is only taken a short time at the beginning of the dump "
 
361
   "- don't forget to read about --single-transaction below). In all cases "
 
362
   "any action on logs will happen at the exact moment of the dump."
 
363
   "Option automatically turns --lock-tables off.",
 
364
   (char**) &opt_master_data, (char**) &opt_master_data, 0,
 
365
   GET_UINT, OPT_ARG, 0, 0, MYSQL_OPT_MASTER_DATA_COMMENTED_SQL, 0, 0, 0},
 
366
  {"max_allowed_packet", OPT_MAX_ALLOWED_PACKET, "",
 
367
    (char**) &opt_max_allowed_packet, (char**) &opt_max_allowed_packet, 0,
 
368
    GET_ULONG, REQUIRED_ARG, 24*1024*1024, 4096,
 
369
   (longlong) 2L*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
 
370
  {"net_buffer_length", OPT_NET_BUFFER_LENGTH, "",
 
371
    (char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0,
 
372
    GET_ULONG, REQUIRED_ARG, 1024*1024L-1025, 4096, 16*1024L*1024L,
 
373
   MALLOC_OVERHEAD-1024, 1024, 0},
265
374
  {"no-autocommit", OPT_AUTOCOMMIT,
266
 
    "Wrap tables with autocommit/commit statements.",
267
 
    (char**) &opt_autocommit, (char**) &opt_autocommit, 0, GET_BOOL, NO_ARG,
268
 
    0, 0, 0, 0, 0, 0},
 
375
   "Wrap tables with autocommit/commit statements.",
 
376
   (char**) &opt_autocommit, (char**) &opt_autocommit, 0, GET_BOOL, NO_ARG,
 
377
   0, 0, 0, 0, 0, 0},
269
378
  {"no-create-db", 'n',
270
 
    "'CREATE DATABASE IF NOT EXISTS db_name;' will not be put in the output. The above line will be added otherwise, if --databases or --all-databases option was given.}.",
271
 
    (char**) &opt_create_db, (char**) &opt_create_db, 0, GET_BOOL, NO_ARG, 0, 0,
272
 
    0, 0, 0, 0},
 
379
   "'CREATE DATABASE /*!32312 IF NOT EXISTS*/ db_name;' will not be put in the output. The above line will be added otherwise, if --databases or --all-databases option was given.}.",
 
380
   (char**) &opt_create_db, (char**) &opt_create_db, 0, GET_BOOL, NO_ARG, 0, 0,
 
381
   0, 0, 0, 0},
273
382
  {"no-create-info", 't', "Don't write table creation info.",
274
 
    (char**) &opt_no_create_info, (char**) &opt_no_create_info, 0, GET_BOOL,
275
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
383
   (char**) &opt_no_create_info, (char**) &opt_no_create_info, 0, GET_BOOL,
 
384
   NO_ARG, 0, 0, 0, 0, 0, 0},
276
385
  {"no-data", 'd', "No row information.", (char**) &opt_no_data,
277
 
    (char**) &opt_no_data, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
386
   (char**) &opt_no_data, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
278
387
  {"no-set-names", 'N',
279
 
    "Deprecated. Use --skip-set-charset instead.",
280
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
388
   "Deprecated. Use --skip-set-charset instead.",
 
389
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
281
390
  {"opt", OPT_OPTIMIZE,
282
 
    "Same as --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. Enabled by default, disable with --skip-opt.",
283
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
391
   "Same as --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. Enabled by default, disable with --skip-opt.",
 
392
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
284
393
  {"order-by-primary", OPT_ORDER_BY_PRIMARY,
285
 
    "Sorts each table's rows by primary key, or first unique key, if such a key exists.  Useful when dumping a MyISAM table to be loaded into an InnoDB table, but will make the dump itself take considerably longer.",
286
 
    (char**) &opt_order_by_primary, (char**) &opt_order_by_primary, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
287
 
  {"password", 'P',
288
 
    "Password to use when connecting to server. If password is not given it's solicited on the tty.",
289
 
    0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
290
 
  {"port", 'p', "Port number to use for connection.", 
291
 
    0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
394
   "Sorts each table's rows by primary key, or first unique key, if such a key exists.  Useful when dumping a MyISAM table to be loaded into an InnoDB table, but will make the dump itself take considerably longer.",
 
395
   (char**) &opt_order_by_primary, (char**) &opt_order_by_primary, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
396
  {"password", 'p',
 
397
   "Password to use when connecting to server. If password is not given it's solicited on the tty.",
 
398
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
399
  {"port", 'P', "Port number to use for connection.", (char**) &opt_mysql_port,
 
400
   (char**) &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
 
401
   0},
292
402
  {"quick", 'q', "Don't buffer query, dump directly to stdout.",
293
 
    (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
403
   (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
294
404
  {"quote-names",'Q', "Quote table and column names with backticks (`).",
295
 
    (char**) &opt_quoted, (char**) &opt_quoted, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
296
 
    0, 0},
297
 
  {"replace", OPT_DRIZZLE_REPLACE_INTO, "Use REPLACE INTO instead of INSERT INTO.",
298
 
    (char**) &opt_replace_into, (char**) &opt_replace_into, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
299
 
    0, 0},
 
405
   (char**) &opt_quoted, (char**) &opt_quoted, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
 
406
   0, 0},
 
407
  {"replace", OPT_MYSQL_REPLACE_INTO, "Use REPLACE INTO instead of INSERT INTO.",
 
408
   (char**) &opt_replace_into, (char**) &opt_replace_into, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
409
   0, 0},
300
410
  {"result-file", 'r',
301
 
    "Direct output to a given file. This option should be used in MSDOS, because it prevents new line '\\n' from being converted to '\\r\\n' (carriage return + line feed).",
302
 
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
411
   "Direct output to a given file. This option should be used in MSDOS, because it prevents new line '\\n' from being converted to '\\r\\n' (carriage return + line feed).",
 
412
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
303
413
  {"routines", 'R', "Dump stored routines (functions and procedures).",
304
 
    (char**) &opt_routines, (char**) &opt_routines, 0, GET_BOOL,
305
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
414
     (char**) &opt_routines, (char**) &opt_routines, 0, GET_BOOL,
 
415
     NO_ARG, 0, 0, 0, 0, 0, 0},
 
416
  {"set-charset", OPT_SET_CHARSET,
 
417
   "Add 'SET NAMES default_character_set' to the output.",
 
418
   (char**) &opt_set_charset, (char**) &opt_set_charset, 0, GET_BOOL, NO_ARG, 1,
 
419
   0, 0, 0, 0, 0},
 
420
  {"set-variable", 'O',
 
421
   "Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value.",
 
422
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
423
#ifdef HAVE_SMEM
 
424
  {"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
 
425
   "Base name of shared memory.", (char**) &shared_memory_base_name, (char**) &shared_memory_base_name,
 
426
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
427
#endif
 
428
  /*
 
429
    Note that the combination --single-transaction --master-data
 
430
    will give bullet-proof binlog position only if server >=4.1.3. That's the
 
431
    old "FLUSH TABLES WITH READ LOCK does not block commit" fixed bug.
 
432
  */
306
433
  {"single-transaction", OPT_TRANSACTION,
307
 
    "Creates a consistent snapshot by dumping all tables in a single "
308
 
      "transaction. Works ONLY for tables stored in storage engines which "
309
 
      "support multiversioning (currently only InnoDB does); the dump is NOT "
310
 
      "guaranteed to be consistent for other storage engines. "
311
 
      "While a --single-transaction dump is in process, to ensure a valid "
312
 
      "dump file (correct table contents), no other "
313
 
      "connection should use the following statements: ALTER TABLE, DROP "
314
 
      "TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not "
315
 
      "isolated from them. Option automatically turns off --lock-tables.",
316
 
    (char**) &opt_single_transaction, (char**) &opt_single_transaction, 0,
317
 
    GET_BOOL, NO_ARG,  0, 0, 0, 0, 0, 0},
 
434
   "Creates a consistent snapshot by dumping all tables in a single "
 
435
   "transaction. Works ONLY for tables stored in storage engines which "
 
436
   "support multiversioning (currently only InnoDB does); the dump is NOT "
 
437
   "guaranteed to be consistent for other storage engines. "
 
438
   "While a --single-transaction dump is in process, to ensure a valid "
 
439
   "dump file (correct table contents and binary log position), no other "
 
440
   "connection should use the following statements: ALTER TABLE, DROP "
 
441
   "TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not "
 
442
   "isolated from them. Option automatically turns off --lock-tables.",
 
443
   (char**) &opt_single_transaction, (char**) &opt_single_transaction, 0,
 
444
   GET_BOOL, NO_ARG,  0, 0, 0, 0, 0, 0},
318
445
  {"dump-date", OPT_DUMP_DATE, "Put a dump date to the end of the output.",
319
 
    (char**) &opt_dump_date, (char**) &opt_dump_date, 0,
320
 
    GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
446
   (char**) &opt_dump_date, (char**) &opt_dump_date, 0,
 
447
   GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
321
448
  {"skip-opt", OPT_SKIP_OPTIMIZATION,
322
 
    "Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys.",
323
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
449
   "Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys.",
 
450
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
324
451
  {"tab",'T',
325
 
    "Creates tab separated textfile for each table to given path. (creates .sql and .txt files). NOTE: This only works if drizzledump is run on the same machine as the drizzled daemon.",
326
 
    (char**) &path, (char**) &path, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
452
   "Creates tab separated textfile for each table to given path. (creates .sql and .txt files). NOTE: This only works if mysqldump is run on the same machine as the mysqld daemon.",
 
453
   (char**) &path, (char**) &path, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
327
454
  {"tables", OPT_TABLES, "Overrides option --databases (-B).",
328
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
329
 
  {"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of rows before each output progress report (requires --verbose)."),
330
 
    (char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
331
 
    10000, 0, 0, 0, 0, 0},
 
455
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
456
  {"tz-utc", OPT_TZ_UTC,
 
457
    "SET TIME_ZONE='+00:00' at top of dump to allow dumping of TIMESTAMP data when a server has data in different time zones or data is being moved between servers with different time zones.",
 
458
    (char**) &opt_tz_utc, (char**) &opt_tz_utc, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
459
#ifndef DONT_ALLOW_USER_CHANGE
332
460
  {"user", 'u', "User for login if not current user.",
333
 
    (char**) &current_user, (char**) &current_user, 0, GET_STR, REQUIRED_ARG,
334
 
    0, 0, 0, 0, 0, 0},
 
461
   (char**) &current_user, (char**) &current_user, 0, GET_STR, REQUIRED_ARG,
 
462
   0, 0, 0, 0, 0, 0},
 
463
#endif
335
464
  {"verbose", 'v', "Print info about the various stages.",
336
 
    (char**) &verbose, (char**) &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
465
   (char**) &verbose, (char**) &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
337
466
  {"version",'V', "Output version information and exit.", 0, 0, 0,
338
 
    GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
467
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
339
468
  {"where", 'w', "Dump only selected records; QUOTES mandatory!",
340
 
    (char**) &where, (char**) &where, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
469
   (char**) &where, (char**) &where, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
341
470
  {"xml", 'X', "Dump a database as well formed XML.", 0, 0, 0, GET_NO_ARG,
342
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
471
   NO_ARG, 0, 0, 0, 0, 0, 0},
343
472
  {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
344
473
};
345
474
 
346
 
static const char *load_default_groups[]= { "drizzledump","client",0 };
 
475
static const char *load_default_groups[]= { "mysqldump","client",0 };
347
476
 
348
477
static void maybe_exit(int error);
349
478
static void die(int error, const char* reason, ...);
350
479
static void maybe_die(int error, const char* reason, ...);
351
480
static void write_header(FILE *sql_file, char *db_name);
352
 
static void print_value(FILE *file, drizzle_result_st *result,
353
 
                        drizzle_row_t row, const char *prefix, const char *name,
 
481
static void print_value(FILE *file, MYSQL_RES  *result, MYSQL_ROW row,
 
482
                        const char *prefix,const char *name,
354
483
                        int string_value);
355
 
static const char* fetch_named_row(drizzle_result_st *result, drizzle_row_t row,
356
 
                                   const char* name);
357
484
static int dump_selected_tables(char *db, char **table_names, int tables);
358
485
static int dump_all_tables_in_db(char *db);
359
486
static int init_dumping_tables(char *);
360
487
static int init_dumping(char *, int init_func(char*));
361
488
static int dump_databases(char **);
362
489
static int dump_all_databases(void);
363
 
static char *quote_name(const char *name, char *buff, bool force);
 
490
static char *quote_name(const char *name, char *buff, my_bool force);
364
491
char check_if_ignore_table(const char *table_name, char *table_type);
365
492
static char *primary_key_fields(const char *table_name);
366
493
 
 
494
#include <help_start.h>
 
495
 
367
496
/*
368
497
  Print the supplied message if in verbose mode
369
498
 
370
499
  SYNOPSIS
371
 
  verbose_msg()
372
 
  fmt   format specifier
373
 
  ...   variable number of parameters
 
500
    verbose_msg()
 
501
    fmt   format specifier
 
502
    ...   variable number of parameters
374
503
*/
 
504
 
375
505
static void verbose_msg(const char *fmt, ...)
376
506
{
377
507
  va_list args;
378
 
 
 
508
  DBUG_ENTER("verbose_msg");
379
509
 
380
510
  if (!verbose)
381
 
    return;
 
511
    DBUG_VOID_RETURN;
382
512
 
383
513
  va_start(args, fmt);
384
514
  vfprintf(stderr, fmt, args);
385
515
  va_end(args);
 
516
 
 
517
  DBUG_VOID_RETURN;
386
518
}
387
519
 
388
520
/*
389
521
  exit with message if ferror(file)
390
522
 
391
523
  SYNOPSIS
392
 
  check_io()
393
 
  file        - checked file
 
524
    check_io()
 
525
    file        - checked file
394
526
*/
395
527
 
396
528
static void check_io(FILE *file)
397
529
{
398
530
  if (ferror(file))
399
 
    die(EX_EOF, _("Got errno %d on write"), errno);
 
531
    die(EX_EOF, "Got errno %d on write", errno);
400
532
}
401
533
 
402
534
static void print_version(void)
403
535
{
404
 
  printf(_("%s  Drizzle %s libdrizzle %s, for %s-%s (%s)\n"), internal::my_progname,
405
 
         VERSION, drizzle_version(), HOST_VENDOR, HOST_OS, HOST_CPU);
 
536
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,DUMP_VERSION,
 
537
         MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
406
538
} /* print_version */
407
539
 
408
540
 
409
541
static void short_usage_sub(void)
410
542
{
411
 
  printf(_("Usage: %s [OPTIONS] database [tables]\n"), internal::my_progname);
412
 
  printf(_("OR     %s [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]\n"),
413
 
         internal::my_progname);
414
 
  printf(_("OR     %s [OPTIONS] --all-databases [OPTIONS]\n"), internal::my_progname);
 
543
  printf("Usage: %s [OPTIONS] database [tables]\n", my_progname);
 
544
  printf("OR     %s [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]\n",
 
545
         my_progname);
 
546
  printf("OR     %s [OPTIONS] --all-databases [OPTIONS]\n", my_progname);
415
547
}
416
548
 
417
549
 
418
550
static void usage(void)
419
551
{
420
552
  print_version();
421
 
  puts("");
422
 
  puts(_("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n"));
423
 
  puts(_("Dumps definitions and data from a Drizzle database server"));
 
553
  puts("By Igor Romanenko, Monty, Jani & Sinisa");
 
554
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n");
 
555
  puts("Dumping definition and data mysql database or table");
424
556
  short_usage_sub();
425
 
  internal::print_defaults("drizzle",load_default_groups);
 
557
  print_defaults("my",load_default_groups);
426
558
  my_print_help(my_long_options);
427
559
  my_print_variables(my_long_options);
428
560
} /* usage */
431
563
static void short_usage(void)
432
564
{
433
565
  short_usage_sub();
434
 
  printf(_("For more options, use %s --help\n"), internal::my_progname);
 
566
  printf("For more options, use %s --help\n", my_progname);
435
567
}
436
568
 
 
569
#include <help_end.h>
 
570
 
 
571
 
437
572
static void write_header(FILE *sql_file, char *db_name)
438
573
{
439
574
  if (opt_xml)
440
575
  {
441
576
    fputs("<?xml version=\"1.0\"?>\n", sql_file);
442
577
    /*
443
 
      Schema reference.  Allows use of xsi:nil for NULL values and
444
 
xsi:type to define an element's data type.
 
578
      Schema reference.  Allows use of xsi:nil for NULL values and 
 
579
      xsi:type to define an element's data type.
445
580
    */
446
 
    fputs("<drizzledump ", sql_file);
 
581
    fputs("<mysqldump ", sql_file);
447
582
    fputs("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
448
583
          sql_file);
449
584
    fputs(">\n", sql_file);
454
589
    if (opt_comments)
455
590
    {
456
591
      fprintf(sql_file,
457
 
              "-- drizzledump %s libdrizzle %s, for %s-%s (%s)\n--\n",
458
 
              VERSION, drizzle_version(), HOST_VENDOR, HOST_OS, HOST_CPU);
 
592
              "-- MySQL dump %s  Distrib %s, for %s (%s)\n--\n",
 
593
              DUMP_VERSION, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
459
594
      fprintf(sql_file, "-- Host: %s    Database: %s\n",
460
595
              current_host ? current_host : "localhost", db_name ? db_name :
461
596
              "");
462
597
      fputs("-- ------------------------------------------------------\n",
463
598
            sql_file);
464
599
      fprintf(sql_file, "-- Server version\t%s\n",
465
 
              drizzle_con_server_version(&dcon));
 
600
              mysql_get_server_info(&mysql_connection));
466
601
    }
467
602
    if (opt_set_charset)
468
603
      fprintf(sql_file,
469
 
              "\nSET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;\n");
470
 
 
471
 
    if (path == NULL)
472
 
    {
473
 
      fprintf(md_result_file,"SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;\nSET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;\n");
 
604
"\n/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;"
 
605
"\n/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;"
 
606
"\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;"
 
607
"\n/*!40101 SET NAMES %s */;\n",default_charset);
 
608
 
 
609
    if (opt_tz_utc)
 
610
    {
 
611
      fprintf(sql_file, "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n");
 
612
      fprintf(sql_file, "/*!40103 SET TIME_ZONE='+00:00' */;\n");
 
613
    }
 
614
 
 
615
    if (!path)
 
616
    {
 
617
      fprintf(md_result_file,"\
 
618
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n\
 
619
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n\
 
620
");
474
621
    }
475
622
    check_io(sql_file);
476
623
  }
481
628
{
482
629
  if (opt_xml)
483
630
  {
484
 
    fputs("</drizzledump>\n", sql_file);
 
631
    fputs("</mysqldump>\n", sql_file);
485
632
    check_io(sql_file);
486
633
  }
487
 
  else if (opt_compact == false)
 
634
  else if (!opt_compact)
488
635
  {
489
 
    if (path == NULL)
 
636
    if (opt_tz_utc)
 
637
      fprintf(sql_file,"/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n");
 
638
 
 
639
    if (!path)
490
640
    {
491
 
      fprintf(md_result_file,"SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;\nSET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;\n");
 
641
      fprintf(md_result_file,"\
 
642
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n\
 
643
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n");
492
644
    }
493
645
    if (opt_set_charset)
494
 
      fprintf(sql_file, "SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;\n");
 
646
      fprintf(sql_file,
 
647
"/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n"
 
648
"/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n"
 
649
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
 
650
    fprintf(sql_file,
 
651
            "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n");
 
652
    fputs("\n", sql_file);
495
653
    if (opt_comments)
496
654
    {
497
655
      if (opt_dump_date)
498
656
      {
499
657
        char time_str[20];
500
 
        internal::get_date(time_str, GETDATE_DATE_TIME, 0);
 
658
        get_date(time_str, GETDATE_DATE_TIME, 0);
501
659
        fprintf(sql_file, "-- Dump completed on %s\n",
502
660
                time_str);
503
661
      }
509
667
} /* write_footer */
510
668
 
511
669
 
512
 
static int get_one_option(int optid, const struct option *, char *argument)
513
 
{
514
 
  char *endchar= NULL;
515
 
  uint64_t temp_drizzle_port= 0;
516
 
 
 
670
static void free_table_ent(char *key)
 
671
{
 
672
  my_free(key, MYF(0));
 
673
}
 
674
 
 
675
 
 
676
static uchar* get_table_key(const char *entry, size_t *length,
 
677
                            my_bool not_used __attribute__((unused)))
 
678
{
 
679
  *length= strlen(entry);
 
680
  return (uchar*) entry;
 
681
}
 
682
 
 
683
 
 
684
static my_bool
 
685
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
686
               char *argument)
 
687
{
517
688
  switch (optid) {
518
689
  case 'p':
519
 
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
520
 
    /* if there is an alpha character this is not a valid port */
521
 
    if (strlen(endchar) != 0)
522
 
    {
523
 
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
524
 
      return EXIT_ARGUMENT_INVALID;
525
 
    }
526
 
    /* If the port number is > 65535 it is not a valid port
527
 
     *        This also helps with potential data loss casting unsigned long to a
528
 
     *               uint32_t. */
529
 
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
530
 
    {
531
 
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
532
 
      return EXIT_ARGUMENT_INVALID;
533
 
    }
534
 
    else
535
 
    {
536
 
      opt_drizzle_port= (uint32_t) temp_drizzle_port;
537
 
    }
538
 
    break;
539
 
  case 'P':
540
690
    if (argument)
541
691
    {
542
 
      char *start= argument;
543
 
      if (opt_password)
544
 
        free(opt_password);
545
 
      opt_password= strdup(argument);
546
 
      if (opt_password == NULL)
547
 
      {
548
 
        fprintf(stderr, _("Memory allocation error while copying password. "
549
 
                          "Aborting.\n"));
550
 
        return EXIT_OUT_OF_MEMORY;
551
 
      }
552
 
      while (*argument)
553
 
      {
554
 
        /* Overwriting password with 'x' */
555
 
        *argument++= 'x';
556
 
      }
 
692
      char *start=argument;
 
693
      my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
 
694
      opt_password=my_strdup(argument,MYF(MY_FAE));
 
695
      while (*argument) *argument++= 'x';               /* Destroy argument */
557
696
      if (*start)
558
 
      {
559
 
        /* Cut length of argument */
560
 
        start[1]= 0;
561
 
      }
 
697
        start[1]=0;                             /* Cut length of argument */
562
698
      tty_password= 0;
563
699
    }
564
700
    else
565
 
    {
566
 
      tty_password= 1;
567
 
    }
 
701
      tty_password=1;
568
702
    break;
569
703
  case 'r':
570
 
    if (!(md_result_file= fopen(argument, "w")))
 
704
    if (!(md_result_file= my_fopen(argument, O_WRONLY | FILE_BINARY,
 
705
                                    MYF(MY_WME))))
571
706
      exit(1);
572
707
    break;
573
708
  case 'N':
584
719
        a crash even if the input destination buffer is large enough
585
720
        to hold the output.
586
721
      */
587
 
      fprintf(stderr, _("Input filename too long: %s"), argument);
588
 
      return EXIT_ARGUMENT_INVALID;
 
722
      die(EX_USAGE, "Input filename too long: %s", argument);
589
723
    }
590
724
 
591
725
    break;
 
726
  case '#':
 
727
    DBUG_PUSH(argument ? argument : default_dbug_option);
 
728
    debug_check_flag= 1;
 
729
    break;
592
730
  case 'V': print_version(); exit(0);
593
731
  case 'X':
594
 
            opt_xml= 1;
595
 
            extended_insert= opt_drop=
596
 
              opt_disable_keys= opt_autocommit= opt_create_db= 0;
597
 
            break;
 
732
    opt_xml= 1;
 
733
    extended_insert= opt_drop= opt_lock=
 
734
      opt_disable_keys= opt_autocommit= opt_create_db= 0;
 
735
    break;
598
736
  case 'I':
599
737
  case '?':
600
 
            usage();
601
 
            exit(0);
 
738
    usage();
 
739
    exit(0);
 
740
  case (int) OPT_MASTER_DATA:
 
741
    if (!argument) /* work like in old versions */
 
742
      opt_master_data= MYSQL_OPT_MASTER_DATA_EFFECTIVE_SQL;
 
743
    break;
 
744
  case (int) OPT_MYSQLDUMP_SLAVE_DATA:
 
745
    if (!argument) /* work like in old versions */
 
746
      opt_slave_data= MYSQL_OPT_SLAVE_DATA_EFFECTIVE_SQL;
 
747
    break;
602
748
  case (int) OPT_OPTIMIZE:
603
 
            extended_insert= opt_drop= quick= create_options=
604
 
              opt_disable_keys= opt_set_charset= 1;
605
 
            break;
 
749
    extended_insert= opt_drop= opt_lock= quick= create_options=
 
750
      opt_disable_keys= lock_tables= opt_set_charset= 1;
 
751
    break;
606
752
  case (int) OPT_SKIP_OPTIMIZATION:
607
 
            extended_insert= opt_drop= quick= create_options=
608
 
              opt_disable_keys= opt_set_charset= 0;
609
 
            break;
 
753
    extended_insert= opt_drop= opt_lock= quick= create_options=
 
754
      opt_disable_keys= lock_tables= opt_set_charset= 0;
 
755
    break;
610
756
  case (int) OPT_COMPACT:
611
 
            if (opt_compact)
612
 
            {
613
 
              opt_comments= opt_drop= opt_disable_keys= 0;
614
 
              opt_set_charset= 0;
615
 
            }
 
757
  if (opt_compact)
 
758
  {
 
759
    opt_comments= opt_drop= opt_disable_keys= opt_lock= 0;
 
760
    opt_set_charset= 0;
 
761
  }
616
762
  case (int) OPT_TABLES:
617
 
            opt_databases=0;
618
 
            break;
 
763
    opt_databases=0;
 
764
    break;
619
765
  case (int) OPT_IGNORE_TABLE:
620
 
            {
621
 
              if (!strchr(argument, '.'))
622
 
              {
623
 
                fprintf(stderr, _("Illegal use of option --ignore-table=<database>.<table>\n"));
624
 
                return EXIT_ARGUMENT_INVALID;
625
 
              }
626
 
              string tmpptr(argument);
627
 
              ignore_table.insert(tmpptr); 
628
 
              break;
629
 
            }
 
766
  {
 
767
    if (!strchr(argument, '.'))
 
768
    {
 
769
      fprintf(stderr, "Illegal use of option --ignore-table=<database>.<table>\n");
 
770
      exit(1);
 
771
    }
 
772
    if (my_hash_insert(&ignore_table, (uchar*)my_strdup(argument, MYF(0))))
 
773
      exit(EX_EOM);
 
774
    break;
 
775
  }
630
776
  case (int) OPT_COMPATIBLE:
631
 
            {
632
 
              char buff[255];
633
 
              char *end= compatible_mode_normal_str;
634
 
              uint32_t i;
635
 
              uint32_t mode;
636
 
              uint32_t error_len;
 
777
    {
 
778
      char buff[255];
 
779
      char *end= compatible_mode_normal_str;
 
780
      int i;
 
781
      ulong mode;
 
782
      uint err_len;
637
783
 
638
 
              opt_quoted= 1;
639
 
              opt_set_charset= 0;
640
 
              opt_compatible_mode_str= argument;
641
 
              opt_compatible_mode= find_set(&compatible_mode_typelib,
642
 
                                            argument, strlen(argument),
643
 
                                            &err_ptr, &error_len);
644
 
              if (error_len)
645
 
              {
646
 
                strncpy(buff, err_ptr, min((uint32_t)sizeof(buff), error_len+1));
647
 
                fprintf(stderr, _("Invalid mode to --compatible: %s\n"), buff);
648
 
                return EXIT_ARGUMENT_INVALID;
649
 
              }
650
 
              mode= opt_compatible_mode;
651
 
              for (i= 0, mode= opt_compatible_mode; mode; mode>>= 1, i++)
652
 
              {
653
 
                if (mode & 1)
654
 
                {
655
 
                  uint32_t len = strlen(compatible_mode_names[i]);
656
 
                  end= strcpy(end, compatible_mode_names[i]) + len;
657
 
                  end= strcpy(end, ",")+1;
658
 
                }
659
 
              }
660
 
              if (end!=compatible_mode_normal_str)
661
 
                end[-1]= 0;
662
 
            }
 
784
      opt_quoted= 1;
 
785
      opt_set_charset= 0;
 
786
      opt_compatible_mode_str= argument;
 
787
      opt_compatible_mode= find_set(&compatible_mode_typelib,
 
788
                                    argument, strlen(argument),
 
789
                                    &err_ptr, &err_len);
 
790
      if (err_len)
 
791
      {
 
792
        strmake(buff, err_ptr, min(sizeof(buff), err_len));
 
793
        fprintf(stderr, "Invalid mode to --compatible: %s\n", buff);
 
794
        exit(1);
 
795
      }
 
796
#if !defined(DBUG_OFF)
 
797
      {
 
798
        uint size_for_sql_mode= 0;
 
799
        const char **ptr;
 
800
        for (ptr= compatible_mode_names; *ptr; ptr++)
 
801
          size_for_sql_mode+= strlen(*ptr);
 
802
        size_for_sql_mode+= sizeof(compatible_mode_names)-1;
 
803
        DBUG_ASSERT(sizeof(compatible_mode_normal_str)>=size_for_sql_mode);
 
804
      }
 
805
#endif
 
806
      mode= opt_compatible_mode;
 
807
      for (i= 0, mode= opt_compatible_mode; mode; mode>>= 1, i++)
 
808
      {
 
809
        if (mode & 1)
 
810
        {
 
811
          end= strmov(end, compatible_mode_names[i]);
 
812
          end= strmov(end, ",");
 
813
        }
 
814
      }
 
815
      if (end!=compatible_mode_normal_str)
 
816
        end[-1]= 0;
 
817
      /*
 
818
        Set charset to the default compiled value if it hasn't
 
819
        been reset yet by --default-character-set=xxx.
 
820
      */
 
821
      if (default_charset == mysql_universal_client_charset)
 
822
        default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
 
823
      break;
 
824
    }
663
825
  }
664
826
  return 0;
665
827
}
667
829
static int get_options(int *argc, char ***argv)
668
830
{
669
831
  int ho_error;
 
832
  MYSQL_PARAMETERS *mysql_params= mysql_get_parameters();
 
833
 
 
834
  opt_max_allowed_packet= *mysql_params->p_max_allowed_packet;
 
835
  opt_net_buffer_length= *mysql_params->p_net_buffer_length;
670
836
 
671
837
  md_result_file= stdout;
672
 
  internal::load_defaults("drizzle",load_default_groups,argc,argv);
 
838
  load_defaults("my",load_default_groups,argc,argv);
673
839
  defaults_argv= *argv;
674
840
 
 
841
  if (hash_init(&ignore_table, charset_info, 16, 0, 0,
 
842
                (hash_get_key) get_table_key,
 
843
                (hash_free_key) free_table_ent, 0))
 
844
    return(EX_EOM);
 
845
  /* Don't copy internal log tables */
 
846
  if (my_hash_insert(&ignore_table,
 
847
                     (uchar*) my_strdup("mysql.apply_status", MYF(MY_WME))) ||
 
848
      my_hash_insert(&ignore_table,
 
849
                     (uchar*) my_strdup("mysql.schema", MYF(MY_WME))) ||
 
850
      my_hash_insert(&ignore_table,
 
851
                     (uchar*) my_strdup("mysql.general_log", MYF(MY_WME))) ||
 
852
      my_hash_insert(&ignore_table,
 
853
                     (uchar*) my_strdup("mysql.slow_log", MYF(MY_WME))) ||
 
854
      my_hash_insert(&ignore_table,
 
855
                     (uchar*) my_strdup("mysql.online_backup", MYF(MY_WME))) ||
 
856
      my_hash_insert(&ignore_table,
 
857
                     (uchar*) my_strdup("mysql.online_backup_progress", MYF(MY_WME))))
 
858
    return(EX_EOM);
 
859
 
675
860
  if ((ho_error= handle_options(argc, argv, my_long_options, get_one_option)))
676
861
    return(ho_error);
677
862
 
 
863
  *mysql_params->p_max_allowed_packet= opt_max_allowed_packet;
 
864
  *mysql_params->p_net_buffer_length= opt_net_buffer_length;
 
865
  if (debug_info_flag)
 
866
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
 
867
  if (debug_check_flag)
 
868
    my_end_arg= MY_CHECK_ERROR;
 
869
 
 
870
  if (opt_delayed)
 
871
    opt_lock=0;                         /* Can't have lock with delayed */
678
872
  if (!path && (enclosed || opt_enclosed || escaped || lines_terminated ||
679
873
                fields_terminated))
680
874
  {
681
875
    fprintf(stderr,
682
 
            _("%s: You must use option --tab with --fields-...\n"), internal::my_progname);
 
876
            "%s: You must use option --tab with --fields-...\n", my_progname);
683
877
    return(EX_USAGE);
684
878
  }
685
879
 
 
880
  /* We don't delete master logs if slave data option */
 
881
  if (opt_slave_data)
 
882
  {
 
883
    opt_lock_all_tables= !opt_single_transaction;
 
884
    opt_master_data= 0;
 
885
    opt_delete_master_logs= 0;
 
886
  }
 
887
 
 
888
  /* Ensure consistency of the set of binlog & locking options */
 
889
  if (opt_delete_master_logs && !opt_master_data)
 
890
    opt_master_data= MYSQL_OPT_MASTER_DATA_COMMENTED_SQL;
686
891
  if (opt_single_transaction && opt_lock_all_tables)
687
892
  {
688
 
    fprintf(stderr, _("%s: You can't use --single-transaction and "
689
 
                      "--lock-all-tables at the same time.\n"), internal::my_progname);
 
893
    fprintf(stderr, "%s: You can't use --single-transaction and "
 
894
            "--lock-all-tables at the same time.\n", my_progname);
690
895
    return(EX_USAGE);
691
896
  }
 
897
  if (opt_master_data)
 
898
  {
 
899
    opt_lock_all_tables= !opt_single_transaction;
 
900
    opt_slave_data= 0;
 
901
  }
 
902
  if (opt_single_transaction || opt_lock_all_tables)
 
903
    lock_tables= 0;
692
904
  if (enclosed && opt_enclosed)
693
905
  {
694
 
    fprintf(stderr, _("%s: You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n"), internal::my_progname);
 
906
    fprintf(stderr, "%s: You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n", my_progname);
695
907
    return(EX_USAGE);
696
908
  }
697
909
  if ((opt_databases || opt_alldbs) && path)
698
910
  {
699
911
    fprintf(stderr,
700
 
            _("%s: --databases or --all-databases can't be used with --tab.\n"),
701
 
            internal::my_progname);
 
912
            "%s: --databases or --all-databases can't be used with --tab.\n",
 
913
            my_progname);
702
914
    return(EX_USAGE);
703
915
  }
 
916
  if (strcmp(default_charset, charset_info->csname) &&
 
917
      !(charset_info= get_charset_by_csname(default_charset,
 
918
                                            MY_CS_PRIMARY, MYF(MY_WME))))
 
919
    exit(1);
704
920
  if ((*argc < 1 && !opt_alldbs) || (*argc > 0 && opt_alldbs))
705
921
  {
706
922
    short_usage();
707
923
    return EX_USAGE;
708
924
  }
709
925
  if (tty_password)
710
 
    opt_password=client_get_tty_password(NULL);
 
926
    opt_password=get_tty_password(NullS);
711
927
  return(0);
712
928
} /* get_options */
713
929
 
714
930
 
715
931
/*
716
 
 ** DB_error -- prints DRIZZLE error message and exits the program.
 
932
** DB_error -- prints mysql error message and exits the program.
717
933
*/
718
 
static void DB_error(drizzle_result_st *res, drizzle_return_t ret,
719
 
                     const char *when)
 
934
static void DB_error(MYSQL *mysql_arg, const char *when)
720
935
{
721
 
  if (ret == DRIZZLE_RETURN_ERROR_CODE)
722
 
  {
723
 
    maybe_die(EX_DRIZZLEERR, _("Got error: %s (%d) %s"),
724
 
              drizzle_result_error(res),
725
 
              drizzle_result_error_code(res),
726
 
              when);
727
 
    drizzle_result_free(res);
728
 
  }
729
 
  else
730
 
    maybe_die(EX_DRIZZLEERR, _("Got error: %d %s"), ret, when);
731
 
 
732
 
  return;
 
936
  DBUG_ENTER("DB_error");
 
937
  maybe_die(EX_MYSQLERR, "Got error: %d: %s %s",
 
938
          mysql_errno(mysql_arg), mysql_error(mysql_arg), when);
 
939
  DBUG_VOID_RETURN;
733
940
}
734
941
 
735
942
 
738
945
  Prints out an error message and kills the process.
739
946
 
740
947
  SYNOPSIS
741
 
  die()
742
 
  error_num   - process return value
743
 
  fmt_reason  - a format string for use by vsnprintf.
744
 
  ...         - variable arguments for above fmt_reason string
745
 
 
 
948
    die()
 
949
    error_num   - process return value
 
950
    fmt_reason  - a format string for use by vsnprintf.
 
951
    ...         - variable arguments for above fmt_reason string
 
952
  
746
953
  DESCRIPTION
747
 
  This call prints out the formatted error message to stderr and then
748
 
  terminates the process.
 
954
    This call prints out the formatted error message to stderr and then
 
955
    terminates the process.
749
956
*/
750
957
static void die(int error_num, const char* fmt_reason, ...)
751
958
{
755
962
  vsnprintf(buffer, sizeof(buffer), fmt_reason, args);
756
963
  va_end(args);
757
964
 
758
 
  fprintf(stderr, "%s: %s\n", internal::my_progname, buffer);
 
965
  fprintf(stderr, "%s: %s\n", my_progname, buffer);
759
966
  fflush(stderr);
760
967
 
761
968
  ignore_errors= 0; /* force the exit */
767
974
  Prints out an error message and maybe kills the process.
768
975
 
769
976
  SYNOPSIS
770
 
  maybe_die()
771
 
  error_num   - process return value
772
 
  fmt_reason  - a format string for use by vsnprintf.
773
 
  ...         - variable arguments for above fmt_reason string
774
 
 
 
977
    maybe_die()
 
978
    error_num   - process return value
 
979
    fmt_reason  - a format string for use by vsnprintf.
 
980
    ...         - variable arguments for above fmt_reason string
 
981
  
775
982
  DESCRIPTION
776
 
  This call prints out the formatted error message to stderr and then
777
 
  terminates the process, unless the --force command line option is used.
778
 
 
779
 
  This call should be used for non-fatal errors (such as database
780
 
  errors) that the code may still be able to continue to the next unit
781
 
  of work.
782
 
 
 
983
    This call prints out the formatted error message to stderr and then
 
984
    terminates the process, unless the --force command line option is used.
 
985
    
 
986
    This call should be used for non-fatal errors (such as database
 
987
    errors) that the code may still be able to continue to the next unit
 
988
    of work.
 
989
    
783
990
*/
784
991
static void maybe_die(int error_num, const char* fmt_reason, ...)
785
992
{
789
996
  vsnprintf(buffer, sizeof(buffer), fmt_reason, args);
790
997
  va_end(args);
791
998
 
792
 
  fprintf(stderr, "%s: %s\n", internal::my_progname, buffer);
 
999
  fprintf(stderr, "%s: %s\n", my_progname, buffer);
793
1000
  fflush(stderr);
794
1001
 
795
1002
  maybe_exit(error_num);
802
1009
  some.
803
1010
 
804
1011
  SYNOPSIS
805
 
  drizzleclient_query_with_error_report()
806
 
  drizzle_con       connection to use
807
 
  res             if non zero, result will be put there with
808
 
  drizzleclient_store_result()
809
 
  query           query to send to server
 
1012
    mysql_query_with_error_report()
 
1013
    mysql_con       connection to use
 
1014
    res             if non zero, result will be put there with
 
1015
                    mysql_store_result()
 
1016
    query           query to send to server
810
1017
 
811
1018
  RETURN VALUES
812
 
  0               query sending and (if res!=0) result reading went ok
813
 
  1               error
 
1019
    0               query sending and (if res!=0) result reading went ok
 
1020
    1               error
814
1021
*/
815
1022
 
816
 
static int drizzleclient_query_with_error_report(drizzle_con_st *con,
817
 
                                                 drizzle_result_st *result,
818
 
                                                 const char *query_str,
819
 
                                                 bool no_buffer)
 
1023
static int mysql_query_with_error_report(MYSQL *mysql_con, MYSQL_RES **res,
 
1024
                                         const char *query)
820
1025
{
821
 
  drizzle_return_t ret;
822
 
 
823
 
  if (drizzle_query_str(con, result, query_str, &ret) == NULL ||
824
 
      ret != DRIZZLE_RETURN_OK)
825
 
  {
826
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
827
 
    {
828
 
      maybe_die(EX_DRIZZLEERR, _("Couldn't execute '%s': %s (%d)"),
829
 
                query_str, drizzle_result_error(result),
830
 
                drizzle_result_error_code(result));
831
 
      drizzle_result_free(result);
832
 
    }
833
 
    else
834
 
    {
835
 
      maybe_die(EX_DRIZZLEERR, _("Couldn't execute '%s': %s (%d)"),
836
 
                query_str, drizzle_con_error(con), ret);
837
 
    }
838
 
    return 1;
839
 
  }
840
 
 
841
 
  if (no_buffer)
842
 
    ret= drizzle_column_buffer(result);
843
 
  else
844
 
    ret= drizzle_result_buffer(result);
845
 
  if (ret != DRIZZLE_RETURN_OK)
846
 
  {
847
 
    drizzle_result_free(result);
848
 
    maybe_die(EX_DRIZZLEERR, _("Couldn't execute '%s': %s (%d)"),
849
 
              query_str, drizzle_con_error(con), ret);
850
 
    return 1;
851
 
  }
852
 
 
 
1026
  if (mysql_query(mysql_con, query) ||
 
1027
      (res && !((*res)= mysql_store_result(mysql_con))))
 
1028
  {
 
1029
    maybe_die(EX_MYSQLERR, "Couldn't execute '%s': %s (%d)",
 
1030
            query, mysql_error(mysql_con), mysql_errno(mysql_con));
 
1031
    return 1;
 
1032
  }
853
1033
  return 0;
854
1034
}
855
1035
 
 
1036
 
 
1037
/**
 
1038
  Switch charset for results to some specified charset.  If the server does not
 
1039
  support character_set_results variable, nothing can be done here.  As for
 
1040
  whether something should be done here, future new callers of this function
 
1041
  should be aware that the server lacking the facility of switching charsets is
 
1042
  treated as success.
 
1043
 
 
1044
  @note  If the server lacks support, then nothing is changed and no error
 
1045
         condition is returned.
 
1046
 
 
1047
  @returns  whether there was an error or not
 
1048
*/
 
1049
static int switch_character_set_results(MYSQL *mysql, const char *cs_name)
 
1050
{
 
1051
  char query_buffer[QUERY_LENGTH];
 
1052
  size_t query_length;
 
1053
 
 
1054
  /* Server lacks facility.  This is not an error, by arbitrary decision . */
 
1055
  if (!server_supports_switching_charsets)
 
1056
    return FALSE;
 
1057
 
 
1058
  query_length= snprintf(query_buffer,
 
1059
                         sizeof (query_buffer),
 
1060
                         "SET SESSION character_set_results = '%s'",
 
1061
                         (const char *) cs_name);
 
1062
 
 
1063
  return mysql_real_query(mysql, query_buffer, query_length);
 
1064
}
 
1065
 
856
1066
/*
857
1067
  Open a new .sql file to dump the table or view into
858
1068
 
859
1069
  SYNOPSIS
860
 
  open_sql_file_for_table
861
 
  name      name of the table or view
 
1070
    open_sql_file_for_table
 
1071
    name      name of the table or view
862
1072
 
863
1073
  RETURN VALUES
864
 
  0        Failed to open file
865
 
  > 0      Handle of the open file
 
1074
    0        Failed to open file
 
1075
    > 0      Handle of the open file
866
1076
*/
867
1077
static FILE* open_sql_file_for_table(const char* table)
868
1078
{
869
1079
  FILE* res;
870
1080
  char filename[FN_REFLEN], tmp_path[FN_REFLEN];
871
 
  internal::convert_dirname(tmp_path,path,NULL);
872
 
  res= fopen(internal::fn_format(filename, table, tmp_path, ".sql", 4), "w");
873
 
 
 
1081
  convert_dirname(tmp_path,path,NullS);
 
1082
  res= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4),
 
1083
                O_WRONLY, MYF(MY_WME));
874
1084
  return res;
875
1085
}
876
1086
 
878
1088
static void free_resources(void)
879
1089
{
880
1090
  if (md_result_file && md_result_file != stdout)
881
 
    fclose(md_result_file);
882
 
  free(opt_password);
 
1091
    my_fclose(md_result_file, MYF(0));
 
1092
  my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
 
1093
  if (hash_inited(&ignore_table))
 
1094
    hash_free(&ignore_table);
 
1095
  if (extended_insert)
 
1096
    dynstr_free(&extended_row);
 
1097
  if (insert_pat_inited)
 
1098
    dynstr_free(&insert_pat);
883
1099
  if (defaults_argv)
884
 
    internal::free_defaults(defaults_argv);
885
 
  internal::my_end();
 
1100
    free_defaults(defaults_argv);
 
1101
  my_end(my_end_arg);
886
1102
}
887
1103
 
888
1104
 
892
1108
    first_error= error;
893
1109
  if (ignore_errors)
894
1110
    return;
895
 
  drizzle_con_free(&dcon);
896
 
  drizzle_free(&drizzle);
 
1111
  if (mysql)
 
1112
    mysql_close(mysql);
897
1113
  free_resources();
898
1114
  exit(error);
899
1115
}
905
1121
 
906
1122
static int connect_to_db(char *host, char *user,char *passwd)
907
1123
{
908
 
  drizzle_return_t ret;
909
 
 
910
 
  verbose_msg(_("-- Connecting to %s...\n"), host ? host : "localhost");
911
 
  drizzle_create(&drizzle);
912
 
  drizzle_con_create(&drizzle, &dcon);
913
 
  drizzle_con_set_tcp(&dcon, host, opt_drizzle_port);
914
 
  drizzle_con_set_auth(&dcon, user, passwd);
915
 
  if (opt_mysql)
916
 
    drizzle_con_add_options(&dcon, DRIZZLE_CON_MYSQL);
917
 
  ret= drizzle_con_connect(&dcon);
918
 
  if (ret != DRIZZLE_RETURN_OK)
919
 
  {
920
 
    DB_error(NULL, ret, "when trying to connect");
921
 
    return(1);
922
 
  }
923
 
 
924
 
  return(0);
 
1124
  char buff[20+FN_REFLEN];
 
1125
  DBUG_ENTER("connect_to_db");
 
1126
 
 
1127
  verbose_msg("-- Connecting to %s...\n", host ? host : "localhost");
 
1128
  mysql_init(&mysql_connection);
 
1129
  if (opt_compress)
 
1130
    mysql_options(&mysql_connection,MYSQL_OPT_COMPRESS,NullS);
 
1131
  if (opt_protocol)
 
1132
    mysql_options(&mysql_connection,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
 
1133
#ifdef HAVE_SMEM
 
1134
  if (shared_memory_base_name)
 
1135
    mysql_options(&mysql_connection,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
 
1136
#endif
 
1137
  mysql_options(&mysql_connection, MYSQL_SET_CHARSET_NAME, default_charset);
 
1138
  if (!(mysql= mysql_real_connect(&mysql_connection,host,user,passwd,
 
1139
                                  NULL,opt_mysql_port, NULL,
 
1140
                                  0)))
 
1141
  {
 
1142
    DB_error(&mysql_connection, "when trying to connect");
 
1143
    DBUG_RETURN(1);
 
1144
  }
 
1145
  if (mysql_get_server_version(&mysql_connection) < 40100)
 
1146
  {
 
1147
    /* Don't dump SET NAMES with a pre-4.1 server (bug#7997).  */
 
1148
    opt_set_charset= 0;
 
1149
 
 
1150
    /* Don't switch charsets for 4.1 and earlier.  (bug#34192). */
 
1151
    server_supports_switching_charsets= FALSE;
 
1152
  } 
 
1153
  /*
 
1154
    set time_zone to UTC to allow dumping date types between servers with
 
1155
    different time zone settings
 
1156
  */
 
1157
  if (opt_tz_utc)
 
1158
  {
 
1159
    snprintf(buff, sizeof(buff), "/*!40103 SET TIME_ZONE='+00:00' */");
 
1160
    if (mysql_query_with_error_report(mysql, 0, buff))
 
1161
      DBUG_RETURN(1);
 
1162
  }
 
1163
  DBUG_RETURN(0);
925
1164
} /* connect_to_db */
926
1165
 
927
1166
 
928
1167
/*
929
 
 ** dbDisconnect -- disconnects from the host.
 
1168
** dbDisconnect -- disconnects from the host.
930
1169
*/
931
1170
static void dbDisconnect(char *host)
932
1171
{
933
 
  verbose_msg(_("-- Disconnecting from %s...\n"), host ? host : "localhost");
934
 
  drizzle_con_free(&dcon);
935
 
  drizzle_free(&drizzle);
 
1172
  verbose_msg("-- Disconnecting from %s...\n", host ? host : "localhost");
 
1173
  mysql_close(mysql);
936
1174
} /* dbDisconnect */
937
1175
 
938
1176
 
939
 
static void unescape(FILE *file,char *pos,uint32_t length)
 
1177
static void unescape(FILE *file,char *pos,uint length)
940
1178
{
941
1179
  char *tmp;
942
 
 
943
 
  if (!(tmp=(char*) malloc(length*2+1)))
944
 
    die(EX_DRIZZLEERR, _("Couldn't allocate memory"));
945
 
 
946
 
  drizzle_escape_string(tmp, pos, length);
 
1180
  DBUG_ENTER("unescape");
 
1181
  if (!(tmp=(char*) my_malloc(length*2+1, MYF(MY_WME))))
 
1182
    die(EX_MYSQLERR, "Couldn't allocate memory");
 
1183
 
 
1184
  mysql_real_escape_string(&mysql_connection, tmp, pos, length);
947
1185
  fputc('\'', file);
948
1186
  fputs(tmp, file);
949
1187
  fputc('\'', file);
950
1188
  check_io(file);
951
 
  free(tmp);
952
 
  return;
 
1189
  my_free(tmp, MYF(MY_WME));
 
1190
  DBUG_VOID_RETURN;
953
1191
} /* unescape */
954
1192
 
955
1193
 
956
 
static bool test_if_special_chars(const char *str)
 
1194
static my_bool test_if_special_chars(const char *str)
957
1195
{
 
1196
#if MYSQL_VERSION_ID >= 32300
958
1197
  for ( ; *str ; str++)
959
1198
    if (!my_isvar(charset_info,*str) && *str != '$')
960
1199
      return 1;
 
1200
#endif
961
1201
  return 0;
962
1202
} /* test_if_special_chars */
963
1203
 
979
1219
  buff                 quoted string
980
1220
 
981
1221
*/
982
 
static char *quote_name(const char *name, char *buff, bool force)
 
1222
static char *quote_name(const char *name, char *buff, my_bool force)
983
1223
{
984
1224
  char *to= buff;
985
 
  char qtype= '`';
 
1225
  char qtype= (opt_compatible_mode & MASK_ANSI_QUOTES) ? '\"' : '`';
986
1226
 
987
1227
  if (!force && !opt_quoted && !test_if_special_chars(name))
988
1228
    return (char*) name;
1003
1243
  Quote a table name so it can be used in "SHOW TABLES LIKE <tabname>"
1004
1244
 
1005
1245
  SYNOPSIS
1006
 
  quote_for_like()
1007
 
  name     name of the table
1008
 
  buff     quoted name of the table
 
1246
    quote_for_like()
 
1247
    name     name of the table
 
1248
    buff     quoted name of the table
1009
1249
 
1010
1250
  DESCRIPTION
1011
 
  Quote \, _, ' and % characters
1012
 
 
1013
 
Note: Because DRIZZLE uses the C escape syntax in strings
1014
 
(for example, '\n' to represent newline), you must double
1015
 
any '\' that you use in your LIKE  strings. For example, to
1016
 
search for '\n', specify it as '\\n'. To search for '\', specify
1017
 
it as '\\\\' (the backslashes are stripped once by the parser
1018
 
and another time when the pattern match is done, leaving a
1019
 
single backslash to be matched).
1020
 
 
1021
 
Example: "t\1" => "t\\\\1"
 
1251
    Quote \, _, ' and % characters
 
1252
 
 
1253
    Note: Because MySQL uses the C escape syntax in strings
 
1254
    (for example, '\n' to represent newline), you must double
 
1255
    any '\' that you use in your LIKE  strings. For example, to
 
1256
    search for '\n', specify it as '\\n'. To search for '\', specify
 
1257
    it as '\\\\' (the backslashes are stripped once by the parser
 
1258
    and another time when the pattern match is done, leaving a
 
1259
    single backslash to be matched).
 
1260
 
 
1261
    Example: "t\1" => "t\\\\1"
1022
1262
 
1023
1263
*/
1024
1264
static char *quote_for_like(const char *name, char *buff)
1047
1287
  Quote and print a string.
1048
1288
 
1049
1289
  SYNOPSIS
1050
 
  print_quoted_xml()
1051
 
  xml_file    - output file
1052
 
  str         - string to print
1053
 
  len         - its length
 
1290
    print_quoted_xml()
 
1291
    xml_file    - output file
 
1292
    str         - string to print
 
1293
    len         - its length
1054
1294
 
1055
1295
  DESCRIPTION
1056
 
  Quote '<' '>' '&' '\"' chars and print a string to the xml_file.
 
1296
    Quote '<' '>' '&' '\"' chars and print a string to the xml_file.
1057
1297
*/
1058
1298
 
1059
 
static void print_quoted_xml(FILE *xml_file, const char *str, uint32_t len)
 
1299
static void print_quoted_xml(FILE *xml_file, const char *str, ulong len)
1060
1300
{
1061
1301
  const char *end;
1062
1302
 
1072
1312
    case '&':
1073
1313
      fputs("&amp;", xml_file);
1074
1314
      break;
1075
 
      case '\"':
1076
 
        fputs("&quot;", xml_file);
 
1315
    case '\"':
 
1316
      fputs("&quot;", xml_file);
1077
1317
      break;
1078
1318
    default:
1079
1319
      fputc(*str, xml_file);
1088
1328
  Print xml tag. Optionally add attribute(s).
1089
1329
 
1090
1330
  SYNOPSIS
1091
 
  print_xml_tag(xml_file, sbeg, send, tag_name, first_attribute_name,
1092
 
  ..., attribute_name_n, attribute_value_n, NULL)
1093
 
  xml_file              - output file
1094
 
  sbeg                  - line beginning
1095
 
  line_end              - line ending
1096
 
  tag_name              - XML tag name.
1097
 
  first_attribute_name  - tag and first attribute
1098
 
  first_attribute_value - (Implied) value of first attribute
1099
 
  attribute_name_n      - attribute n
1100
 
  attribute_value_n     - value of attribute n
 
1331
    print_xml_tag(xml_file, sbeg, send, tag_name, first_attribute_name, 
 
1332
                    ..., attribute_name_n, attribute_value_n, NullS)
 
1333
    xml_file              - output file
 
1334
    sbeg                  - line beginning
 
1335
    line_end              - line ending
 
1336
    tag_name              - XML tag name.
 
1337
    first_attribute_name  - tag and first attribute
 
1338
    first_attribute_value - (Implied) value of first attribute
 
1339
    attribute_name_n      - attribute n
 
1340
    attribute_value_n     - value of attribute n
1101
1341
 
1102
1342
  DESCRIPTION
1103
 
  Print XML tag with any number of attribute="value" pairs to the xml_file.
 
1343
    Print XML tag with any number of attribute="value" pairs to the xml_file.
1104
1344
 
1105
 
  Format is:
1106
 
  sbeg<tag_name first_attribute_name="first_attribute_value" ...
1107
 
  attribute_name_n="attribute_value_n">send
 
1345
    Format is:
 
1346
      sbeg<tag_name first_attribute_name="first_attribute_value" ... 
 
1347
      attribute_name_n="attribute_value_n">send
1108
1348
  NOTE
1109
 
  Additional arguments must be present in attribute/value pairs.
1110
 
  The last argument should be the null character pointer.
1111
 
  All attribute_value arguments MUST be NULL terminated strings.
1112
 
  All attribute_value arguments will be quoted before output.
 
1349
    Additional arguments must be present in attribute/value pairs.
 
1350
    The last argument should be the null character pointer.
 
1351
    All attribute_value arguments MUST be NULL terminated strings.
 
1352
    All attribute_value arguments will be quoted before output.
1113
1353
*/
1114
1354
 
1115
1355
static void print_xml_tag(FILE * xml_file, const char* sbeg,
1116
 
                          const char* line_end,
1117
 
                          const char* tag_name,
 
1356
                          const char* line_end, 
 
1357
                          const char* tag_name, 
1118
1358
                          const char* first_attribute_name, ...)
1119
1359
{
1120
1360
  va_list arg_list;
1122
1362
 
1123
1363
  fputs(sbeg, xml_file);
1124
1364
  fputc('<', xml_file);
1125
 
  fputs(tag_name, xml_file);
 
1365
  fputs(tag_name, xml_file);  
1126
1366
 
1127
1367
  va_start(arg_list, first_attribute_name);
1128
1368
  attribute_name= first_attribute_name;
1129
 
  while (attribute_name != NULL)
 
1369
  while (attribute_name != NullS)
1130
1370
  {
1131
1371
    attribute_value= va_arg(arg_list, char *);
1132
 
    assert(attribute_value != NULL);
 
1372
    DBUG_ASSERT(attribute_value != NullS);
1133
1373
 
1134
1374
    fputc(' ', xml_file);
1135
 
    fputs(attribute_name, xml_file);
 
1375
    fputs(attribute_name, xml_file);    
1136
1376
    fputc('\"', xml_file);
1137
 
 
 
1377
    
1138
1378
    print_quoted_xml(xml_file, attribute_value, strlen(attribute_value));
1139
1379
    fputc('\"', xml_file);
1140
1380
 
1152
1392
  Print xml tag with for a field that is null
1153
1393
 
1154
1394
  SYNOPSIS
1155
 
  print_xml_null_tag()
1156
 
  xml_file    - output file
1157
 
  sbeg        - line beginning
1158
 
  stag_atr    - tag and attribute
1159
 
  sval        - value of attribute
1160
 
  line_end        - line ending
 
1395
    print_xml_null_tag()
 
1396
    xml_file    - output file
 
1397
    sbeg        - line beginning
 
1398
    stag_atr    - tag and attribute
 
1399
    sval        - value of attribute
 
1400
    line_end        - line ending
1161
1401
 
1162
1402
  DESCRIPTION
1163
 
  Print tag with one attribute to the xml_file. Format is:
1164
 
  <stag_atr="sval" xsi:nil="true"/>
 
1403
    Print tag with one attribute to the xml_file. Format is:
 
1404
      <stag_atr="sval" xsi:nil="true"/>
1165
1405
  NOTE
1166
 
  sval MUST be a NULL terminated string.
1167
 
  sval string will be qouted before output.
 
1406
    sval MUST be a NULL terminated string.
 
1407
    sval string will be qouted before output.
1168
1408
*/
1169
1409
 
1170
1410
static void print_xml_null_tag(FILE * xml_file, const char* sbeg,
1186
1426
  Print xml tag with many attributes.
1187
1427
 
1188
1428
  SYNOPSIS
1189
 
  print_xml_row()
1190
 
  xml_file    - output file
1191
 
  row_name    - xml tag name
1192
 
  tableRes    - query result
1193
 
  row         - result row
 
1429
    print_xml_row()
 
1430
    xml_file    - output file
 
1431
    row_name    - xml tag name
 
1432
    tableRes    - query result
 
1433
    row         - result row
1194
1434
 
1195
1435
  DESCRIPTION
1196
 
  Print tag with many attribute to the xml_file. Format is:
1197
 
  \t\t<row_name Atr1="Val1" Atr2="Val2"... />
 
1436
    Print tag with many attribute to the xml_file. Format is:
 
1437
      \t\t<row_name Atr1="Val1" Atr2="Val2"... />
1198
1438
  NOTE
1199
 
  All atributes and values will be quoted before output.
 
1439
    All atributes and values will be quoted before output.
1200
1440
*/
1201
1441
 
1202
1442
static void print_xml_row(FILE *xml_file, const char *row_name,
1203
 
                          drizzle_result_st *tableRes, drizzle_row_t *row)
 
1443
                          MYSQL_RES *tableRes, MYSQL_ROW *row)
1204
1444
{
1205
 
  uint32_t i;
1206
 
  drizzle_column_st *column;
1207
 
  size_t *lengths= drizzle_row_field_sizes(tableRes);
 
1445
  uint i;
 
1446
  MYSQL_FIELD *field;
 
1447
  ulong *lengths= mysql_fetch_lengths(tableRes);
1208
1448
 
1209
1449
  fprintf(xml_file, "\t\t<%s", row_name);
1210
1450
  check_io(xml_file);
1211
 
  drizzle_column_seek(tableRes, 0);
1212
 
  for (i= 0; (column= drizzle_column_next(tableRes)); i++)
 
1451
  mysql_field_seek(tableRes, 0);
 
1452
  for (i= 0; (field= mysql_fetch_field(tableRes)); i++)
1213
1453
  {
1214
1454
    if ((*row)[i])
1215
1455
    {
1216
1456
      fputc(' ', xml_file);
1217
 
      print_quoted_xml(xml_file, drizzle_column_name(column),
1218
 
                       strlen(drizzle_column_name(column)));
 
1457
      print_quoted_xml(xml_file, field->name, field->name_length);
1219
1458
      fputs("=\"", xml_file);
1220
1459
      print_quoted_xml(xml_file, (*row)[i], lengths[i]);
1221
1460
      fputc('"', xml_file);
1231
1470
  Print hex value for blob data.
1232
1471
 
1233
1472
  SYNOPSIS
1234
 
  print_blob_as_hex()
1235
 
  output_file         - output file
1236
 
  str                 - string to print
1237
 
  len                 - its length
 
1473
    print_blob_as_hex()
 
1474
    output_file         - output file
 
1475
    str                 - string to print
 
1476
    len                 - its length
1238
1477
 
1239
1478
  DESCRIPTION
1240
 
  Print hex value for blob data.
 
1479
    Print hex value for blob data.
1241
1480
*/
1242
1481
 
1243
 
static void print_blob_as_hex(FILE *output_file, const char *str, uint32_t len)
 
1482
static void print_blob_as_hex(FILE *output_file, const char *str, ulong len)
1244
1483
{
1245
 
  /* sakaik got the idea to to provide blob's in hex notation. */
1246
 
  const char *ptr= str, *end= ptr + len;
1247
 
  for (; ptr < end ; ptr++)
1248
 
    fprintf(output_file, "%02X", *((unsigned char *)ptr));
1249
 
  check_io(output_file);
 
1484
    /* sakaik got the idea to to provide blob's in hex notation. */
 
1485
    const char *ptr= str, *end= ptr + len;
 
1486
    for (; ptr < end ; ptr++)
 
1487
      fprintf(output_file, "%02X", *((uchar *)ptr));
 
1488
    check_io(output_file);
1250
1489
}
1251
1490
 
1252
1491
/*
1255
1494
  be dumping.
1256
1495
 
1257
1496
  ARGS
1258
 
  table       - table name
1259
 
  db          - db name
1260
 
  table_type  - table type, e.g. "MyISAM" or "InnoDB", but also "VIEW"
1261
 
  ignore_flag - what we must particularly ignore - see IGNORE_ defines above
1262
 
  num_fields  - number of fields in the table
 
1497
    table       - table name
 
1498
    db          - db name
 
1499
    table_type  - table type, e.g. "MyISAM" or "InnoDB", but also "VIEW"
 
1500
    ignore_flag - what we must particularly ignore - see IGNORE_ defines above
1263
1501
 
1264
1502
  RETURN
1265
 
  true if success, false if error
 
1503
    number of fields in table, 0 if error
1266
1504
*/
1267
1505
 
1268
 
static bool get_table_structure(char *table, char *db, char *table_type,
1269
 
                                char *ignore_flag, uint64_t *num_fields)
 
1506
static uint get_table_structure(char *table, char *db, char *table_type,
 
1507
                                char *ignore_flag)
1270
1508
{
1271
 
  bool    init=0, delayed, write_data, complete_insert;
 
1509
  my_bool    init=0, delayed, write_data, complete_insert;
 
1510
  my_ulonglong num_fields;
1272
1511
  char       *result_table, *opt_quoted_table;
1273
1512
  const char *insert_option;
1274
 
  char       name_buff[DRIZZLE_MAX_COLUMN_NAME_SIZE+3];
1275
 
  char       table_buff[DRIZZLE_MAX_COLUMN_NAME_SIZE*2+3];
1276
 
  char       table_buff2[DRIZZLE_MAX_TABLE_SIZE*2+3];
1277
 
  char       query_buff[QUERY_LENGTH];
 
1513
  char       name_buff[NAME_LEN+3],table_buff[NAME_LEN*2+3];
 
1514
  char       table_buff2[NAME_LEN*2+3], query_buff[QUERY_LENGTH];
1278
1515
  FILE       *sql_file= md_result_file;
1279
 
  drizzle_result_st result;
1280
 
  drizzle_row_t  row;
 
1516
  int        len;
 
1517
  MYSQL_RES  *result;
 
1518
  MYSQL_ROW  row;
 
1519
  DBUG_ENTER("get_table_structure");
 
1520
  DBUG_PRINT("enter", ("db: %s  table: %s", db, table));
1281
1521
 
1282
1522
  *ignore_flag= check_if_ignore_table(table, table_type);
1283
1523
 
1285
1525
  if (delayed && (*ignore_flag & IGNORE_INSERT_DELAYED))
1286
1526
  {
1287
1527
    delayed= 0;
1288
 
    verbose_msg(_("-- Warning: Unable to use delayed inserts for table '%s' "
1289
 
                  "because it's of type %s\n"), table, table_type);
 
1528
    verbose_msg("-- Warning: Unable to use delayed inserts for table '%s' "
 
1529
                "because it's of type %s\n", table, table_type);
1290
1530
  }
1291
1531
 
1292
1532
  complete_insert= 0;
1293
1533
  if ((write_data= !(*ignore_flag & IGNORE_DATA)))
1294
1534
  {
1295
1535
    complete_insert= opt_complete_insert;
1296
 
    insert_pat.clear();
 
1536
    if (!insert_pat_inited)
 
1537
    {
 
1538
      insert_pat_inited= 1;
 
1539
      init_dynamic_string_checked(&insert_pat, "", 1024, 1024);
 
1540
    }
 
1541
    else
 
1542
      dynstr_set_checked(&insert_pat, "");
1297
1543
  }
1298
1544
 
1299
1545
  insert_option= ((delayed && opt_ignore) ? " DELAYED IGNORE " :
1300
1546
                  delayed ? " DELAYED " : opt_ignore ? " IGNORE " : "");
1301
1547
 
1302
 
  verbose_msg(_("-- Retrieving table structure for table %s...\n"), table);
 
1548
  verbose_msg("-- Retrieving table structure for table %s...\n", table);
 
1549
 
 
1550
  len= snprintf(query_buff, sizeof(query_buff),
 
1551
                "SET OPTION SQL_QUOTE_SHOW_CREATE=%d",
 
1552
                (opt_quoted || opt_keywords));
1303
1553
 
1304
1554
  result_table=     quote_name(table, table_buff, 1);
1305
1555
  opt_quoted_table= quote_name(table, table_buff2, 0);
1306
1556
 
1307
1557
  if (opt_order_by_primary)
1308
1558
  {
1309
 
    free(order_by);
 
1559
    my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
1310
1560
    order_by= primary_key_fields(result_table);
1311
1561
  }
1312
1562
 
1313
 
  if (!opt_xml)
 
1563
  if (!opt_xml && !mysql_query_with_error_report(mysql, 0, query_buff))
1314
1564
  {
1315
1565
    /* using SHOW CREATE statement */
1316
1566
    if (!opt_no_create_info)
1317
1567
    {
1318
1568
      /* Make an sql-file, if path was given iow. option -T was given */
1319
1569
      char buff[20+FN_REFLEN];
1320
 
      const drizzle_column_st *column;
 
1570
      MYSQL_FIELD *field;
1321
1571
 
1322
1572
      snprintf(buff, sizeof(buff), "show create table %s", result_table);
1323
1573
 
1324
 
      if (drizzleclient_query_with_error_report(&dcon, &result, buff, false))
1325
 
        return false;
 
1574
      if (switch_character_set_results(mysql, "binary") ||
 
1575
          mysql_query_with_error_report(mysql, &result, buff) ||
 
1576
          switch_character_set_results(mysql, default_charset))
 
1577
        DBUG_RETURN(0);
1326
1578
 
1327
1579
      if (path)
1328
1580
      {
1329
1581
        if (!(sql_file= open_sql_file_for_table(table)))
1330
 
        {
1331
 
          drizzle_result_free(&result);
1332
 
          return false;
1333
 
        }
 
1582
          DBUG_RETURN(0);
1334
1583
 
1335
1584
        write_header(sql_file, db);
1336
1585
      }
1337
1586
      if (!opt_xml && opt_comments)
1338
1587
      {
 
1588
      if (strcmp (table_type, "VIEW") == 0)         /* view */
 
1589
        fprintf(sql_file, "\n--\n-- Temporary table structure for view %s\n--\n\n",
 
1590
                result_table);
 
1591
      else
1339
1592
        fprintf(sql_file, "\n--\n-- Table structure for table %s\n--\n\n",
1340
1593
                result_table);
1341
1594
        check_io(sql_file);
1342
1595
      }
1343
1596
      if (opt_drop)
1344
1597
      {
 
1598
      /*
 
1599
        Even if the "table" is a view, we do a DROP TABLE here.  The
 
1600
        view-specific code below fills in the DROP VIEW.
 
1601
       */
 
1602
        fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n",
 
1603
                opt_quoted_table);
 
1604
        check_io(sql_file);
 
1605
      }
 
1606
 
 
1607
      field= mysql_fetch_field_direct(result, 0);
 
1608
      if (strcmp(field->name, "View") == 0)
 
1609
      {
 
1610
        char *scv_buff= NULL;
 
1611
 
 
1612
        verbose_msg("-- It's a view, create dummy table for view\n");
 
1613
 
 
1614
        /* save "show create" statement for later */
 
1615
        if ((row= mysql_fetch_row(result)) && (scv_buff=row[1]))
 
1616
          scv_buff= my_strdup(scv_buff, MYF(0));
 
1617
 
 
1618
        mysql_free_result(result);
 
1619
 
1345
1620
        /*
1346
 
          Even if the "table" is a view, we do a DROP TABLE here.
 
1621
          Create a table with the same name as the view and with columns of
 
1622
          the same name in order to satisfy views that depend on this view.
 
1623
          The table will be removed when the actual view is created.
 
1624
 
 
1625
          The properties of each column, aside from the data type, are not
 
1626
          preserved in this temporary table, because they are not necessary.
 
1627
 
 
1628
          This will not be necessary once we can determine dependencies
 
1629
          between views and can simply dump them in the appropriate order.
1347
1630
        */
1348
 
        fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n", opt_quoted_table);
1349
 
        check_io(sql_file);
 
1631
        snprintf(query_buff, sizeof(query_buff),
 
1632
                 "SHOW FIELDS FROM %s", result_table);
 
1633
        if (switch_character_set_results(mysql, "binary") ||
 
1634
            mysql_query_with_error_report(mysql, &result, query_buff) ||
 
1635
            switch_character_set_results(mysql, default_charset))
 
1636
        {
 
1637
          /*
 
1638
            View references invalid or privileged table/col/fun (err 1356),
 
1639
            so we cannot create a stand-in table.  Be defensive and dump
 
1640
            a comment with the view's 'show create' statement. (Bug #17371)
 
1641
          */
 
1642
 
 
1643
          if (mysql_errno(mysql) == ER_VIEW_INVALID)
 
1644
            fprintf(sql_file, "\n-- failed on view %s: %s\n\n", result_table, scv_buff ? scv_buff : "");
 
1645
 
 
1646
          my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
 
1647
 
 
1648
          DBUG_RETURN(0);
 
1649
        }
 
1650
        else
 
1651
          my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
 
1652
 
 
1653
        if (mysql_num_rows(result))
 
1654
        {
 
1655
          if (opt_drop)
 
1656
          {
 
1657
            /*
 
1658
              We have already dropped any table of the same name above, so
 
1659
              here we just drop the view.
 
1660
            */
 
1661
 
 
1662
            fprintf(sql_file, "/*!50001 DROP VIEW IF EXISTS %s*/;\n",
 
1663
                    opt_quoted_table);
 
1664
            check_io(sql_file);
 
1665
          }
 
1666
 
 
1667
          fprintf(sql_file,
 
1668
                  "SET @saved_cs_client     = @@character_set_client;\n"
 
1669
                  "SET character_set_client = utf8;\n"
 
1670
                  "/*!50001 CREATE TABLE %s (\n",
 
1671
                  result_table);
 
1672
 
 
1673
          /*
 
1674
            Get first row, following loop will prepend comma - keeps from
 
1675
            having to know if the row being printed is last to determine if
 
1676
            there should be a _trailing_ comma.
 
1677
          */
 
1678
 
 
1679
          row= mysql_fetch_row(result);
 
1680
 
 
1681
          fprintf(sql_file, "  %s %s", quote_name(row[0], name_buff, 0),
 
1682
                  row[1]);
 
1683
 
 
1684
          while((row= mysql_fetch_row(result)))
 
1685
          {
 
1686
            /* col name, col type */
 
1687
            fprintf(sql_file, ",\n  %s %s",
 
1688
                    quote_name(row[0], name_buff, 0), row[1]);
 
1689
          }
 
1690
          fprintf(sql_file,
 
1691
                  "\n) */;\n"
 
1692
                  "SET character_set_client = @saved_cs_client;\n");
 
1693
 
 
1694
          check_io(sql_file);
 
1695
        }
 
1696
 
 
1697
        mysql_free_result(result);
 
1698
 
 
1699
        if (path)
 
1700
          my_fclose(sql_file, MYF(MY_WME));
 
1701
 
 
1702
        seen_views= 1;
 
1703
        DBUG_RETURN(0);
1350
1704
      }
1351
1705
 
1352
 
      column= drizzle_column_index(&result, 0);
1353
 
 
1354
 
      row= drizzle_row_next(&result);
1355
 
 
1356
 
      fprintf(sql_file, "%s;\n", row[1]);
 
1706
      row= mysql_fetch_row(result);
 
1707
 
 
1708
      fprintf(sql_file,
 
1709
              "SET @saved_cs_client     = @@character_set_client;\n"
 
1710
              "SET character_set_client = utf8;\n"
 
1711
              "%s;\n"
 
1712
              "SET character_set_client = @saved_cs_client;\n",
 
1713
              row[1]);
1357
1714
 
1358
1715
      check_io(sql_file);
1359
 
      drizzle_result_free(&result);
 
1716
      mysql_free_result(result);
1360
1717
    }
1361
 
 
1362
1718
    snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1363
1719
             result_table);
1364
 
 
1365
 
    if (drizzleclient_query_with_error_report(&dcon, &result, query_buff, false))
 
1720
    if (mysql_query_with_error_report(mysql, &result, query_buff))
1366
1721
    {
1367
1722
      if (path)
1368
 
        fclose(sql_file);
1369
 
      return false;
 
1723
        my_fclose(sql_file, MYF(MY_WME));
 
1724
      DBUG_RETURN(0);
1370
1725
    }
1371
1726
 
1372
1727
    /*
1378
1733
    if (write_data)
1379
1734
    {
1380
1735
      if (opt_replace_into)
1381
 
        insert_pat.append("REPLACE ");
 
1736
        dynstr_append_checked(&insert_pat, "REPLACE ");
1382
1737
      else
1383
 
        insert_pat.append("INSERT ");
1384
 
      insert_pat.append(insert_option);
1385
 
      insert_pat.append("INTO ");
1386
 
      insert_pat.append(opt_quoted_table);
 
1738
        dynstr_append_checked(&insert_pat, "INSERT ");
 
1739
      dynstr_append_checked(&insert_pat, insert_option);
 
1740
      dynstr_append_checked(&insert_pat, "INTO ");
 
1741
      dynstr_append_checked(&insert_pat, opt_quoted_table);
1387
1742
      if (complete_insert)
1388
1743
      {
1389
 
        insert_pat.append(" (");
 
1744
        dynstr_append_checked(&insert_pat, " (");
1390
1745
      }
1391
1746
      else
1392
1747
      {
1393
 
        insert_pat.append(" VALUES ");
 
1748
        dynstr_append_checked(&insert_pat, " VALUES ");
1394
1749
        if (!extended_insert)
1395
 
          insert_pat.append("(");
 
1750
          dynstr_append_checked(&insert_pat, "(");
1396
1751
      }
1397
1752
    }
1398
1753
 
1399
 
    while ((row= drizzle_row_next(&result)))
 
1754
    while ((row= mysql_fetch_row(result)))
1400
1755
    {
1401
1756
      if (complete_insert)
1402
1757
      {
1403
1758
        if (init)
1404
1759
        {
1405
 
          insert_pat.append(", ");
 
1760
          dynstr_append_checked(&insert_pat, ", ");
1406
1761
        }
1407
1762
        init=1;
1408
 
        insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
 
1763
        dynstr_append_checked(&insert_pat,
 
1764
                      quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1409
1765
      }
1410
1766
    }
1411
 
    *num_fields= drizzle_result_row_count(&result);
1412
 
    drizzle_result_free(&result);
 
1767
    num_fields= mysql_num_rows(result);
 
1768
    mysql_free_result(result);
1413
1769
  }
1414
1770
  else
1415
1771
  {
1416
 
    verbose_msg(_("%s: Warning: Can't set SQL_QUOTE_SHOW_CREATE option (%s)\n"),
1417
 
                internal::my_progname, drizzle_con_error(&dcon));
 
1772
    verbose_msg("%s: Warning: Can't set SQL_QUOTE_SHOW_CREATE option (%s)\n",
 
1773
                my_progname, mysql_error(mysql));
1418
1774
 
1419
1775
    snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1420
1776
             result_table);
1421
 
    if (drizzleclient_query_with_error_report(&dcon, &result, query_buff, false))
1422
 
      return false;
 
1777
    if (mysql_query_with_error_report(mysql, &result, query_buff))
 
1778
      DBUG_RETURN(0);
1423
1779
 
1424
1780
    /* Make an sql-file, if path was given iow. option -T was given */
1425
1781
    if (!opt_no_create_info)
1427
1783
      if (path)
1428
1784
      {
1429
1785
        if (!(sql_file= open_sql_file_for_table(table)))
1430
 
        {
1431
 
          drizzle_result_free(&result);
1432
 
          return false;
1433
 
        }
 
1786
          DBUG_RETURN(0);
1434
1787
        write_header(sql_file, db);
1435
1788
      }
1436
1789
      if (!opt_xml && opt_comments)
1441
1794
      if (!opt_xml)
1442
1795
        fprintf(sql_file, "CREATE TABLE %s (\n", result_table);
1443
1796
      else
1444
 
        print_xml_tag(sql_file, "\t", "\n", "table_structure", "name=", table,
1445
 
                      NULL);
 
1797
        print_xml_tag(sql_file, "\t", "\n", "table_structure", "name=", table, 
 
1798
                NullS);
1446
1799
      check_io(sql_file);
1447
1800
    }
1448
1801
 
1449
1802
    if (write_data)
1450
1803
    {
1451
1804
      if (opt_replace_into)
1452
 
        insert_pat.append("REPLACE ");
 
1805
        dynstr_append_checked(&insert_pat, "REPLACE ");
1453
1806
      else
1454
 
        insert_pat.append("INSERT ");
1455
 
      insert_pat.append(insert_option);
1456
 
      insert_pat.append("INTO ");
1457
 
      insert_pat.append(result_table);
 
1807
        dynstr_append_checked(&insert_pat, "INSERT ");
 
1808
      dynstr_append_checked(&insert_pat, insert_option);
 
1809
      dynstr_append_checked(&insert_pat, "INTO ");
 
1810
      dynstr_append_checked(&insert_pat, result_table);
1458
1811
      if (complete_insert)
1459
 
        insert_pat.append(" (");
 
1812
        dynstr_append_checked(&insert_pat, " (");
1460
1813
      else
1461
1814
      {
1462
 
        insert_pat.append(" VALUES ");
 
1815
        dynstr_append_checked(&insert_pat, " VALUES ");
1463
1816
        if (!extended_insert)
1464
 
          insert_pat.append("(");
 
1817
          dynstr_append_checked(&insert_pat, "(");
1465
1818
      }
1466
1819
    }
1467
1820
 
1468
 
    while ((row= drizzle_row_next(&result)))
 
1821
    while ((row= mysql_fetch_row(result)))
1469
1822
    {
1470
 
      size_t *lengths= drizzle_row_field_sizes(&result);
 
1823
      ulong *lengths= mysql_fetch_lengths(result);
1471
1824
      if (init)
1472
1825
      {
1473
1826
        if (!opt_xml && !opt_no_create_info)
1476
1829
          check_io(sql_file);
1477
1830
        }
1478
1831
        if (complete_insert)
1479
 
          insert_pat.append(", ");
 
1832
          dynstr_append_checked(&insert_pat, ", ");
1480
1833
      }
1481
1834
      init=1;
1482
1835
      if (complete_insert)
1483
 
        insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
 
1836
        dynstr_append_checked(&insert_pat,
 
1837
                      quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1484
1838
      if (!opt_no_create_info)
1485
1839
      {
1486
1840
        if (opt_xml)
1487
1841
        {
1488
 
          print_xml_row(sql_file, "field", &result, &row);
 
1842
          print_xml_row(sql_file, "field", result, &row);
1489
1843
          continue;
1490
1844
        }
1491
1845
 
1509
1863
        check_io(sql_file);
1510
1864
      }
1511
1865
    }
1512
 
    *num_fields= drizzle_result_row_count(&result);
1513
 
    drizzle_result_free(&result);
1514
 
 
 
1866
    num_fields= mysql_num_rows(result);
 
1867
    mysql_free_result(result);
1515
1868
    if (!opt_no_create_info)
1516
1869
    {
1517
1870
      /* Make an sql-file, if path was given iow. option -T was given */
1518
1871
      char buff[20+FN_REFLEN];
1519
 
      uint32_t keynr,primary_key;
 
1872
      uint keynr,primary_key;
1520
1873
      snprintf(buff, sizeof(buff), "show keys from %s", result_table);
1521
 
      if (drizzleclient_query_with_error_report(&dcon, &result, buff, false))
 
1874
      if (mysql_query_with_error_report(mysql, &result, buff))
1522
1875
      {
1523
 
        fprintf(stderr, _("%s: Can't get keys for table %s\n"),
1524
 
                internal::my_progname, result_table);
 
1876
        if (mysql_errno(mysql) == ER_WRONG_OBJECT)
 
1877
        {
 
1878
          /* it is VIEW */
 
1879
          fputs("\t\t<options Comment=\"view\" />\n", sql_file);
 
1880
          goto continue_xml;
 
1881
        }
 
1882
        fprintf(stderr, "%s: Can't get keys for table %s (%s)\n",
 
1883
                my_progname, result_table, mysql_error(mysql));
1525
1884
        if (path)
1526
 
          fclose(sql_file);
1527
 
        return false;
 
1885
          my_fclose(sql_file, MYF(MY_WME));
 
1886
        DBUG_RETURN(0);
1528
1887
      }
1529
1888
 
1530
1889
      /* Find first which key is primary key */
1531
1890
      keynr=0;
1532
1891
      primary_key=INT_MAX;
1533
 
      while ((row= drizzle_row_next(&result)))
 
1892
      while ((row= mysql_fetch_row(result)))
1534
1893
      {
1535
1894
        if (atoi(row[3]) == 1)
1536
1895
        {
1546
1905
          }
1547
1906
        }
1548
1907
      }
1549
 
      drizzle_row_seek(&result,0);
 
1908
      mysql_data_seek(result,0);
1550
1909
      keynr=0;
1551
 
      while ((row= drizzle_row_next(&result)))
 
1910
      while ((row= mysql_fetch_row(result)))
1552
1911
      {
1553
1912
        if (opt_xml)
1554
1913
        {
1555
 
          print_xml_row(sql_file, "key", &result, &row);
 
1914
          print_xml_row(sql_file, "key", result, &row);
1556
1915
          continue;
1557
1916
        }
1558
1917
 
1576
1935
          fprintf(sql_file, " (%s)",row[7]);      /* Sub key */
1577
1936
        check_io(sql_file);
1578
1937
      }
1579
 
      drizzle_result_free(&result);
 
1938
      mysql_free_result(result);
1580
1939
      if (!opt_xml)
1581
1940
      {
1582
1941
        if (keynr)
1584
1943
        fputs("\n)",sql_file);
1585
1944
        check_io(sql_file);
1586
1945
      }
1587
 
      /* Get DRIZZLE specific create options */
 
1946
 
 
1947
      /* Get MySQL specific create options */
1588
1948
      if (create_options)
1589
1949
      {
1590
 
        char show_name_buff[DRIZZLE_MAX_COLUMN_NAME_SIZE*2+2+24];
 
1950
        char show_name_buff[NAME_LEN*2+2+24];
1591
1951
 
1592
1952
        /* Check memory for quote_for_like() */
1593
1953
        snprintf(buff, sizeof(buff), "show table status like %s",
1594
1954
                 quote_for_like(table, show_name_buff));
1595
1955
 
1596
 
        if (!drizzleclient_query_with_error_report(&dcon, &result, buff, false))
 
1956
        if (mysql_query_with_error_report(mysql, &result, buff))
1597
1957
        {
1598
 
          if (!(row= drizzle_row_next(&result)))
1599
 
          {
1600
 
            fprintf(stderr,
1601
 
                    _("Error: Couldn't read status information for table %s\n"),
1602
 
                    result_table);
 
1958
          if (mysql_errno(mysql) != ER_PARSE_ERROR)
 
1959
          {                                     /* If old MySQL version */
 
1960
            verbose_msg("-- Warning: Couldn't get status information for " \
 
1961
                        "table %s (%s)\n", result_table,mysql_error(mysql));
1603
1962
          }
 
1963
        }
 
1964
        else if (!(row= mysql_fetch_row(result)))
 
1965
        {
 
1966
          fprintf(stderr,
 
1967
                  "Error: Couldn't read status information for table %s (%s)\n",
 
1968
                  result_table,mysql_error(mysql));
 
1969
        }
 
1970
        else
 
1971
        {
 
1972
          if (opt_xml)
 
1973
            print_xml_row(sql_file, "options", result, &row);
1604
1974
          else
1605
1975
          {
1606
 
            if (opt_xml)
1607
 
              print_xml_row(sql_file, "options", &result, &row);
1608
 
            else
1609
 
            {
1610
 
              fputs("/*!",sql_file);
1611
 
              print_value(sql_file,&result,row,"engine=","Engine",0);
1612
 
              print_value(sql_file,&result,row,"","Create_options",0);
1613
 
              print_value(sql_file,&result,row,"comment=","Comment",1);
1614
 
 
1615
 
              fputs(" */",sql_file);
1616
 
              check_io(sql_file);
1617
 
            }
 
1976
            fputs("/*!",sql_file);
 
1977
            print_value(sql_file,result,row,"engine=","Engine",0);
 
1978
            print_value(sql_file,result,row,"","Create_options",0);
 
1979
            print_value(sql_file,result,row,"comment=","Comment",1);
 
1980
            fputs(" */",sql_file);
 
1981
            check_io(sql_file);
1618
1982
          }
1619
 
          drizzle_result_free(&result);
1620
1983
        }
 
1984
        mysql_free_result(result);              /* Is always safe to free */
1621
1985
      }
 
1986
continue_xml:
1622
1987
      if (!opt_xml)
1623
1988
        fputs(";\n", sql_file);
1624
1989
      else
1626
1991
      check_io(sql_file);
1627
1992
    }
1628
1993
  }
1629
 
  if (complete_insert) {
1630
 
    insert_pat.append(") VALUES ");
 
1994
  if (complete_insert)
 
1995
  {
 
1996
    dynstr_append_checked(&insert_pat, ") VALUES ");
1631
1997
    if (!extended_insert)
1632
 
      insert_pat.append("(");
 
1998
      dynstr_append_checked(&insert_pat, "(");
1633
1999
  }
1634
2000
  if (sql_file != md_result_file)
1635
2001
  {
1636
2002
    fputs("\n", sql_file);
1637
2003
    write_footer(sql_file);
1638
 
    fclose(sql_file);
 
2004
    my_fclose(sql_file, MYF(MY_WME));
1639
2005
  }
1640
 
  return true;
 
2006
  DBUG_RETURN((uint) num_fields);
1641
2007
} /* get_table_structure */
1642
2008
 
1643
 
static void add_load_option(string &str, const char *option,
1644
 
                            const char *option_value)
 
2009
static void add_load_option(DYNAMIC_STRING *str, const char *option,
 
2010
                             const char *option_value)
1645
2011
{
1646
2012
  if (!option_value)
1647
2013
  {
1649
2015
    return;
1650
2016
  }
1651
2017
 
1652
 
  str.append(option);
1653
 
 
 
2018
  dynstr_append_checked(str, option);
 
2019
  
1654
2020
  if (strncmp(option_value, "0x", sizeof("0x")-1) == 0)
1655
2021
  {
1656
2022
    /* It's a hex constant, don't escape */
1657
 
    str.append(option_value);
 
2023
    dynstr_append_checked(str, option_value);
1658
2024
  }
1659
2025
  else
1660
2026
  {
1671
2037
  syntax errors from the SQL parser.
1672
2038
*/
1673
2039
 
1674
 
static void field_escape(string &in, const char *from)
 
2040
static void field_escape(DYNAMIC_STRING* in, const char *from)
1675
2041
{
1676
 
  uint32_t end_backslashes= 0;
 
2042
  uint end_backslashes= 0; 
1677
2043
 
1678
 
  in.append("'");
 
2044
  dynstr_append_checked(in, "'");
1679
2045
 
1680
2046
  while (*from)
1681
2047
  {
1682
 
    in.append(from, 1);
 
2048
    dynstr_append_mem_checked(in, from, 1);
1683
2049
 
1684
2050
    if (*from == '\\')
1685
2051
      end_backslashes^=1;    /* find odd number of backslashes */
1687
2053
    {
1688
2054
      if (*from == '\'' && !end_backslashes)
1689
2055
      {
1690
 
        /* We want a duplicate of "'" for DRIZZLE */
1691
 
        in.append("\'");
 
2056
        /* We want a duplicate of "'" for MySQL */
 
2057
        dynstr_append_checked(in, "\'");
1692
2058
      }
1693
2059
      end_backslashes=0;
1694
2060
    }
1696
2062
  }
1697
2063
  /* Add missing backslashes if user has specified odd number of backs.*/
1698
2064
  if (end_backslashes)
1699
 
    in.append("\\");
1700
 
 
1701
 
  in.append("'");
 
2065
    dynstr_append_checked(in, "\\");
 
2066
  
 
2067
  dynstr_append_checked(in, "'");
1702
2068
}
1703
2069
 
1704
2070
 
1705
2071
 
1706
2072
/*
1707
2073
 
1708
 
  SYNOPSIS
 
2074
 SYNOPSIS
1709
2075
  dump_table()
1710
2076
 
1711
2077
  dump_table saves database contents as a series of INSERT statements.
1712
2078
 
1713
2079
  ARGS
1714
 
  table - table name
1715
 
  db    - db name
 
2080
   table - table name
 
2081
   db    - db name
1716
2082
 
1717
 
  RETURNS
1718
 
  void
 
2083
   RETURNS
 
2084
    void
1719
2085
*/
1720
2086
 
1721
2087
 
1722
2088
static void dump_table(char *table, char *db)
1723
2089
{
1724
2090
  char ignore_flag;
1725
 
  char table_buff[DRIZZLE_MAX_TABLE_SIZE+3];
1726
 
  string query_string;
1727
 
  char table_type[DRIZZLE_MAX_TABLE_SIZE];
1728
 
  char *result_table, table_buff2[DRIZZLE_MAX_TABLE_SIZE*2+3], *opt_quoted_table;
 
2091
  char buf[200], table_buff[NAME_LEN+3];
 
2092
  DYNAMIC_STRING query_string;
 
2093
  char table_type[NAME_LEN];
 
2094
  char *result_table, table_buff2[NAME_LEN*2+3], *opt_quoted_table;
1729
2095
  int error= 0;
1730
 
  uint32_t rownr, row_break, total_length, init_length;
1731
 
  uint64_t num_fields= 0;
1732
 
  drizzle_return_t ret;
1733
 
  drizzle_result_st result;
1734
 
  drizzle_column_st *column;
1735
 
  drizzle_row_t row;
1736
 
 
 
2096
  ulong         rownr, row_break, total_length, init_length;
 
2097
  uint num_fields;
 
2098
  MYSQL_RES     *res;
 
2099
  MYSQL_FIELD   *field;
 
2100
  MYSQL_ROW     row;
 
2101
  DBUG_ENTER("dump_table");
1737
2102
 
1738
2103
  /*
1739
2104
    Make sure you get the create table info before the following check for
1740
2105
    --no-data flag below. Otherwise, the create table info won't be printed.
1741
2106
  */
1742
 
  if (!get_table_structure(table, db, table_type, &ignore_flag, &num_fields))
1743
 
  {
1744
 
    maybe_die(EX_TABLE_STATUS, _("Error retrieving table structure for table: \"%s\""), table);
1745
 
    return;
1746
 
  }
 
2107
  num_fields= get_table_structure(table, db, table_type, &ignore_flag);
 
2108
 
 
2109
  /*
 
2110
    The "table" could be a view.  If so, we don't do anything here.
 
2111
  */
 
2112
  if (strcmp(table_type, "VIEW") == 0)
 
2113
    DBUG_VOID_RETURN;
1747
2114
 
1748
2115
  /* Check --no-data flag */
1749
2116
  if (opt_no_data)
1750
2117
  {
1751
 
    verbose_msg(_("-- Skipping dump data for table '%s', --no-data was used\n"),
 
2118
    verbose_msg("-- Skipping dump data for table '%s', --no-data was used\n",
1752
2119
                table);
1753
 
    return;
 
2120
    DBUG_VOID_RETURN;
1754
2121
  }
1755
2122
 
 
2123
  DBUG_PRINT("info",
 
2124
             ("ignore_flag: %x  num_fields: %d", (int) ignore_flag,
 
2125
              num_fields));
1756
2126
  /*
1757
2127
    If the table type is a merge table or any type that has to be
1758
 
    _completely_ ignored and no data dumped
 
2128
     _completely_ ignored and no data dumped
1759
2129
  */
1760
2130
  if (ignore_flag & IGNORE_DATA)
1761
2131
  {
1762
 
    verbose_msg(_("-- Warning: Skipping data for table '%s' because " \
1763
 
                  "it's of type %s\n"), table, table_type);
1764
 
    return;
 
2132
    verbose_msg("-- Warning: Skipping data for table '%s' because " \
 
2133
                "it's of type %s\n", table, table_type);
 
2134
    DBUG_VOID_RETURN;
1765
2135
  }
1766
2136
  /* Check that there are any fields in the table */
1767
2137
  if (num_fields == 0)
1768
2138
  {
1769
 
    verbose_msg(_("-- Skipping dump data for table '%s', it has no fields\n"),
 
2139
    verbose_msg("-- Skipping dump data for table '%s', it has no fields\n",
1770
2140
                table);
1771
 
    return;
 
2141
    DBUG_VOID_RETURN;
 
2142
  }
 
2143
 
 
2144
  /*
 
2145
     Check --skip-events flag: it is not enough to skip creation of events
 
2146
     discarding SHOW CREATE EVENT statements generation. The myslq.event
 
2147
     table data should be skipped too.
 
2148
  */
 
2149
  if (!opt_events && !my_strcasecmp(&my_charset_latin1, db, "mysql") &&
 
2150
      !my_strcasecmp(&my_charset_latin1, table, "event"))
 
2151
  {
 
2152
    verbose_msg("-- Skipping data table mysql.event, --skip-events was used\n");
 
2153
    DBUG_VOID_RETURN;
1772
2154
  }
1773
2155
 
1774
2156
  result_table= quote_name(table,table_buff, 1);
1775
2157
  opt_quoted_table= quote_name(table, table_buff2, 0);
1776
2158
 
1777
 
  verbose_msg(_("-- Sending SELECT query...\n"));
 
2159
  verbose_msg("-- Sending SELECT query...\n");
1778
2160
 
1779
 
  query_string.clear();
1780
 
  query_string.reserve(1024);
 
2161
  init_dynamic_string_checked(&query_string, "", 1024, 1024);
1781
2162
 
1782
2163
  if (path)
1783
2164
  {
1787
2168
      Convert the path to native os format
1788
2169
      and resolve to the full filepath.
1789
2170
    */
1790
 
    internal::convert_dirname(tmp_path,path,NULL);
1791
 
    internal::my_load_path(tmp_path, tmp_path, NULL);
1792
 
    internal::fn_format(filename, table, tmp_path, ".txt", MYF(MY_UNPACK_FILENAME));
 
2171
    convert_dirname(tmp_path,path,NullS);    
 
2172
    my_load_path(tmp_path, tmp_path, NULL);
 
2173
    fn_format(filename, table, tmp_path, ".txt", MYF(MY_UNPACK_FILENAME));
1793
2174
 
1794
2175
    /* Must delete the file that 'INTO OUTFILE' will write to */
1795
 
    internal::my_delete(filename, MYF(0));
 
2176
    my_delete(filename, MYF(0));
 
2177
 
 
2178
    /* convert to a unix path name to stick into the query */
 
2179
    to_unix_path(filename);
1796
2180
 
1797
2181
    /* now build the query string */
1798
2182
 
1799
 
    query_string.append( "SELECT * INTO OUTFILE '");
1800
 
    query_string.append( filename);
1801
 
    query_string.append( "'");
 
2183
    dynstr_append_checked(&query_string, "SELECT * INTO OUTFILE '");
 
2184
    dynstr_append_checked(&query_string, filename);
 
2185
    dynstr_append_checked(&query_string, "'");
1802
2186
 
1803
2187
    if (fields_terminated || enclosed || opt_enclosed || escaped)
1804
 
      query_string.append( " FIELDS");
1805
 
 
1806
 
    add_load_option(query_string, " TERMINATED BY ", fields_terminated);
1807
 
    add_load_option(query_string, " ENCLOSED BY ", enclosed);
1808
 
    add_load_option(query_string, " OPTIONALLY ENCLOSED BY ", opt_enclosed);
1809
 
    add_load_option(query_string, " ESCAPED BY ", escaped);
1810
 
    add_load_option(query_string, " LINES TERMINATED BY ", lines_terminated);
1811
 
 
1812
 
    query_string.append( " FROM ");
1813
 
    query_string.append( result_table);
 
2188
      dynstr_append_checked(&query_string, " FIELDS");
 
2189
    
 
2190
    add_load_option(&query_string, " TERMINATED BY ", fields_terminated);
 
2191
    add_load_option(&query_string, " ENCLOSED BY ", enclosed);
 
2192
    add_load_option(&query_string, " OPTIONALLY ENCLOSED BY ", opt_enclosed);
 
2193
    add_load_option(&query_string, " ESCAPED BY ", escaped);
 
2194
    add_load_option(&query_string, " LINES TERMINATED BY ", lines_terminated);
 
2195
 
 
2196
    dynstr_append_checked(&query_string, " FROM ");
 
2197
    dynstr_append_checked(&query_string, result_table);
1814
2198
 
1815
2199
    if (where)
1816
2200
    {
1817
 
      query_string.append( " WHERE ");
1818
 
      query_string.append( where);
 
2201
      dynstr_append_checked(&query_string, " WHERE ");
 
2202
      dynstr_append_checked(&query_string, where);
1819
2203
    }
1820
2204
 
1821
2205
    if (order_by)
1822
2206
    {
1823
 
      query_string.append( " ORDER BY ");
1824
 
      query_string.append( order_by);
 
2207
      dynstr_append_checked(&query_string, " ORDER BY ");
 
2208
      dynstr_append_checked(&query_string, order_by);
1825
2209
    }
1826
2210
 
1827
 
    if (drizzle_query(&dcon, &result, query_string.c_str(),
1828
 
                      query_string.length(), &ret) == NULL ||
1829
 
        ret != DRIZZLE_RETURN_OK)
 
2211
    if (mysql_real_query(mysql, query_string.str, query_string.length))
1830
2212
    {
1831
 
      DB_error(&result, ret, _("when executing 'SELECT INTO OUTFILE'"));
1832
 
 
1833
 
      return;
 
2213
      DB_error(mysql, "when executing 'SELECT INTO OUTFILE'");
 
2214
      dynstr_free(&query_string);
 
2215
      DBUG_VOID_RETURN;
1834
2216
    }
1835
 
    drizzle_result_free(&result);
1836
2217
  }
1837
2218
  else
1838
2219
  {
1839
2220
    if (!opt_xml && opt_comments)
1840
2221
    {
1841
 
      fprintf(md_result_file,_("\n--\n-- Dumping data for table %s\n--\n"),
 
2222
      fprintf(md_result_file,"\n--\n-- Dumping data for table %s\n--\n",
1842
2223
              result_table);
1843
2224
      check_io(md_result_file);
1844
2225
    }
1845
 
 
1846
 
    query_string.append( "SELECT * FROM ");
1847
 
    query_string.append( result_table);
 
2226
    
 
2227
    dynstr_append_checked(&query_string, "SELECT * FROM ");
 
2228
    dynstr_append_checked(&query_string, result_table);
1848
2229
 
1849
2230
    if (where)
1850
2231
    {
1853
2234
        fprintf(md_result_file, "-- WHERE:  %s\n", where);
1854
2235
        check_io(md_result_file);
1855
2236
      }
1856
 
 
1857
 
      query_string.append( " WHERE ");
1858
 
      query_string.append( where);
 
2237
      
 
2238
      dynstr_append_checked(&query_string, " WHERE ");
 
2239
      dynstr_append_checked(&query_string, where);
1859
2240
    }
1860
2241
    if (order_by)
1861
2242
    {
1864
2245
        fprintf(md_result_file, "-- ORDER BY:  %s\n", order_by);
1865
2246
        check_io(md_result_file);
1866
2247
      }
1867
 
      query_string.append( " ORDER BY ");
1868
 
      query_string.append( order_by);
 
2248
      dynstr_append_checked(&query_string, " ORDER BY ");
 
2249
      dynstr_append_checked(&query_string, order_by);
1869
2250
    }
1870
2251
 
1871
2252
    if (!opt_xml && !opt_compact)
1873
2254
      fputs("\n", md_result_file);
1874
2255
      check_io(md_result_file);
1875
2256
    }
1876
 
    if (drizzleclient_query_with_error_report(&dcon, &result,
1877
 
                                              query_string.c_str(), quick))
1878
 
    {
 
2257
    if (mysql_query_with_error_report(mysql, 0, query_string.str))
 
2258
    {
 
2259
      DB_error(mysql, "when retrieving data from server");
 
2260
      goto err;
 
2261
    }
 
2262
    if (quick)
 
2263
      res=mysql_use_result(mysql);
 
2264
    else
 
2265
      res=mysql_store_result(mysql);
 
2266
    if (!res)
 
2267
    {
 
2268
      DB_error(mysql, "when retrieving data from server");
1879
2269
      goto err;
1880
2270
    }
1881
2271
 
1882
 
    verbose_msg(_("-- Retrieving rows...\n"));
1883
 
    if (drizzle_result_column_count(&result) != num_fields)
 
2272
    verbose_msg("-- Retrieving rows...\n");
 
2273
    if (mysql_num_fields(res) != num_fields)
1884
2274
    {
1885
 
      fprintf(stderr,_("%s: Error in field count for table: %s !  Aborting.\n"),
1886
 
              internal::my_progname, result_table);
 
2275
      fprintf(stderr,"%s: Error in field count for table: %s !  Aborting.\n",
 
2276
              my_progname, result_table);
1887
2277
      error= EX_CONSCHECK;
1888
 
      drizzle_result_free(&result);
1889
2278
      goto err;
1890
2279
    }
1891
2280
 
 
2281
    if (opt_lock)
 
2282
    {
 
2283
      fprintf(md_result_file,"LOCK TABLES %s WRITE;\n", opt_quoted_table);
 
2284
      check_io(md_result_file);
 
2285
    }
1892
2286
    /* Moved disable keys to after lock per bug 15977 */
1893
2287
    if (opt_disable_keys)
1894
2288
    {
1895
 
      fprintf(md_result_file, "ALTER TABLE %s DISABLE KEYS;\n",
1896
 
              opt_quoted_table);
 
2289
      fprintf(md_result_file, "/*!40000 ALTER TABLE %s DISABLE KEYS */;\n",
 
2290
              opt_quoted_table);
1897
2291
      check_io(md_result_file);
1898
2292
    }
1899
2293
 
1900
 
    total_length= DRIZZLE_MAX_LINE_LENGTH;                /* Force row break */
 
2294
    total_length= opt_net_buffer_length;                /* Force row break */
1901
2295
    row_break=0;
1902
2296
    rownr=0;
1903
 
    init_length=(uint32_t) insert_pat.length()+4;
 
2297
    init_length=(uint) insert_pat.length+4;
1904
2298
    if (opt_xml)
1905
2299
      print_xml_tag(md_result_file, "\t", "\n", "table_data", "name=", table,
1906
 
                    NULL);
 
2300
              NullS);
1907
2301
    if (opt_autocommit)
1908
2302
    {
1909
2303
      fprintf(md_result_file, "set autocommit=0;\n");
1910
2304
      check_io(md_result_file);
1911
2305
    }
1912
2306
 
1913
 
    row= NULL;
1914
 
 
1915
 
    while (1)
 
2307
    while ((row= mysql_fetch_row(res)))
1916
2308
    {
1917
 
      uint32_t i;
1918
 
      size_t *lengths;
1919
 
 
1920
 
      if (quick)
1921
 
      {
1922
 
        if (row)
1923
 
          drizzle_row_free(&result, row);
1924
 
 
1925
 
        row= drizzle_row_buffer(&result, &ret);
1926
 
        if (ret != DRIZZLE_RETURN_OK)
1927
 
        {
1928
 
          fprintf(stderr,
1929
 
                  _("%s: Error reading rows for table: %s (%d:%s) ! Aborting.\n"),
1930
 
                  internal::my_progname, result_table, ret, drizzle_con_error(&dcon));
1931
 
          drizzle_result_free(&result);
1932
 
          goto err;
1933
 
        }
1934
 
      }
1935
 
      else
1936
 
        row= drizzle_row_next(&result);
1937
 
 
1938
 
      if (row == NULL)
1939
 
        break;
1940
 
 
1941
 
      lengths= drizzle_row_field_sizes(&result);
1942
 
 
 
2309
      uint i;
 
2310
      ulong *lengths= mysql_fetch_lengths(res);
1943
2311
      rownr++;
1944
 
      if ((rownr % show_progress_size) == 0)
1945
 
      {
1946
 
        verbose_msg(_("-- %"PRIu32" of ~%"PRIu64" rows dumped for table %s\n"), rownr, total_rows, opt_quoted_table);
1947
 
      }
1948
2312
      if (!extended_insert && !opt_xml)
1949
2313
      {
1950
 
        fputs(insert_pat.c_str(),md_result_file);
 
2314
        fputs(insert_pat.str,md_result_file);
1951
2315
        check_io(md_result_file);
1952
2316
      }
1953
 
      drizzle_column_seek(&result,0);
 
2317
      mysql_field_seek(res,0);
1954
2318
 
1955
2319
      if (opt_xml)
1956
2320
      {
1958
2322
        check_io(md_result_file);
1959
2323
      }
1960
2324
 
1961
 
      for (i= 0; i < drizzle_result_column_count(&result); i++)
 
2325
      for (i= 0; i < mysql_num_fields(res); i++)
1962
2326
      {
1963
2327
        int is_blob;
1964
 
        uint32_t length= lengths[i];
 
2328
        ulong length= lengths[i];
1965
2329
 
1966
 
        if (!(column= drizzle_column_next(&result)))
 
2330
        if (!(field= mysql_fetch_field(res)))
1967
2331
          die(EX_CONSCHECK,
1968
 
              _("Not enough fields from table %s! Aborting.\n"),
1969
 
              result_table);
 
2332
                      "Not enough fields from table %s! Aborting.\n",
 
2333
                      result_table);
1970
2334
 
1971
2335
        /*
1972
 
          63 is my_charset_bin. If charsetnr is not 63,
1973
 
          we have not a BLOB but a TEXT column.
1974
 
          we'll dump in hex only BLOB columns.
 
2336
           63 is my_charset_bin. If charsetnr is not 63,
 
2337
           we have not a BLOB but a TEXT column.
 
2338
           we'll dump in hex only BLOB columns.
1975
2339
        */
1976
 
        is_blob= (opt_hex_blob && drizzle_column_charset(column) == 63 &&
1977
 
                  (drizzle_column_type(column) == DRIZZLE_COLUMN_TYPE_VARCHAR ||
1978
 
                   drizzle_column_type(column) == DRIZZLE_COLUMN_TYPE_BLOB)) ? 1 : 0;
 
2340
        is_blob= (opt_hex_blob && field->charsetnr == 63 &&
 
2341
                  (field->type == MYSQL_TYPE_STRING ||
 
2342
                   field->type == MYSQL_TYPE_VARCHAR ||
 
2343
                   field->type == MYSQL_TYPE_BLOB)) ? 1 : 0;
1979
2344
        if (extended_insert && !opt_xml)
1980
2345
        {
1981
2346
          if (i == 0)
1982
 
          {
1983
 
            extended_row.clear();
1984
 
            extended_row.append("(");
1985
 
          }
 
2347
            dynstr_set_checked(&extended_row,"(");
1986
2348
          else
1987
 
            extended_row.append(",");
 
2349
            dynstr_append_checked(&extended_row,",");
1988
2350
 
1989
2351
          if (row[i])
1990
2352
          {
1991
2353
            if (length)
1992
2354
            {
1993
 
              if (!(drizzle_column_flags(column) & DRIZZLE_COLUMN_FLAGS_NUM))
 
2355
              if (!IS_NUM_FIELD(field))
1994
2356
              {
1995
2357
                /*
1996
2358
                  "length * 2 + 2" is OK for both HEX and non-HEX modes:
2000
2362
                  plus 2 bytes for leading and trailing '\'' characters.
2001
2363
                  Also we need to reserve 1 byte for terminating '\0'.
2002
2364
                */
2003
 
                char * tmp_str= (char *)malloc(length * 2 + 2 + 1);
2004
 
                memset(tmp_str, '\0', length * 2 + 2 + 1);
 
2365
                dynstr_realloc_checked(&extended_row,length * 2 + 2 + 1);
2005
2366
                if (opt_hex_blob && is_blob)
2006
2367
                {
2007
 
                  extended_row.append("0x");
2008
 
                  drizzle_hex_string(tmp_str, row[i], length);
2009
 
                  extended_row.append(tmp_str);
 
2368
                  dynstr_append_checked(&extended_row, "0x");
 
2369
                  extended_row.length+= mysql_hex_string(extended_row.str +
 
2370
                                                         extended_row.length,
 
2371
                                                         row[i], length);
 
2372
                  DBUG_ASSERT(extended_row.length+1 <= extended_row.max_length);
 
2373
                  /* mysql_hex_string() already terminated string by '\0' */
 
2374
                  DBUG_ASSERT(extended_row.str[extended_row.length] == '\0');
2010
2375
                }
2011
2376
                else
2012
2377
                {
2013
 
                  extended_row.append("'");
2014
 
                  drizzle_escape_string(tmp_str, row[i],length);
2015
 
                  extended_row.append(tmp_str);
2016
 
                  extended_row.append("'");
 
2378
                  dynstr_append_checked(&extended_row,"'");
 
2379
                  extended_row.length +=
 
2380
                  mysql_real_escape_string(&mysql_connection,
 
2381
                                           &extended_row.str[extended_row.length],
 
2382
                                           row[i],length);
 
2383
                  extended_row.str[extended_row.length]='\0';
 
2384
                  dynstr_append_checked(&extended_row,"'");
2017
2385
                }
2018
 
                free(tmp_str);
2019
2386
              }
2020
2387
              else
2021
2388
              {
2022
2389
                /* change any strings ("inf", "-inf", "nan") into NULL */
2023
2390
                char *ptr= row[i];
2024
2391
                if (my_isalpha(charset_info, *ptr) || (*ptr == '-' &&
2025
 
                                                       my_isalpha(charset_info, ptr[1])))
2026
 
                  extended_row.append( "NULL");
 
2392
                    my_isalpha(charset_info, ptr[1])))
 
2393
                  dynstr_append_checked(&extended_row, "NULL");
2027
2394
                else
2028
2395
                {
2029
 
                  extended_row.append( ptr);
 
2396
                  dynstr_append_checked(&extended_row, ptr);
2030
2397
                }
2031
2398
              }
2032
2399
            }
2033
2400
            else
2034
 
              extended_row.append("''");
 
2401
              dynstr_append_checked(&extended_row,"''");
2035
2402
          }
2036
2403
          else
2037
 
            extended_row.append("NULL");
 
2404
            dynstr_append_checked(&extended_row,"NULL");
2038
2405
        }
2039
2406
        else
2040
2407
        {
2045
2412
          }
2046
2413
          if (row[i])
2047
2414
          {
2048
 
            if (!(drizzle_column_flags(column) & DRIZZLE_COLUMN_FLAGS_NUM))
 
2415
            if (!IS_NUM_FIELD(field))
2049
2416
            {
2050
2417
              if (opt_xml)
2051
2418
              {
2053
2420
                {
2054
2421
                  /* Define xsi:type="xs:hexBinary" for hex encoded data */
2055
2422
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2056
 
                                drizzle_column_name(column), "xsi:type=", "xs:hexBinary", NULL);
 
2423
                                field->name, "xsi:type=", "xs:hexBinary", NullS);
2057
2424
                  print_blob_as_hex(md_result_file, row[i], length);
2058
2425
                }
2059
2426
                else
2060
2427
                {
2061
 
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2062
 
                                drizzle_column_name(column), NULL);
 
2428
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=", 
 
2429
                                field->name, NullS);
2063
2430
                  print_quoted_xml(md_result_file, row[i], length);
2064
2431
                }
2065
2432
                fputs("</field>\n", md_result_file);
2079
2446
              if (opt_xml)
2080
2447
              {
2081
2448
                print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2082
 
                              drizzle_column_name(column), NULL);
 
2449
                        field->name, NullS);
2083
2450
                fputs(!my_isalpha(charset_info, *ptr) ? ptr: "NULL",
2084
2451
                      md_result_file);
2085
2452
                fputs("</field>\n", md_result_file);
2098
2465
              fputs("NULL", md_result_file);
2099
2466
            else
2100
2467
              print_xml_null_tag(md_result_file, "\t\t", "field name=",
2101
 
                                 drizzle_column_name(column), "\n");
 
2468
                                 field->name, "\n");
2102
2469
          }
2103
2470
          check_io(md_result_file);
2104
2471
        }
2112
2479
 
2113
2480
      if (extended_insert)
2114
2481
      {
2115
 
        uint32_t row_length;
2116
 
        extended_row.append(")");
2117
 
        row_length= 2 + extended_row.length();
2118
 
        if (total_length + row_length < DRIZZLE_MAX_LINE_LENGTH)
 
2482
        ulong row_length;
 
2483
        dynstr_append_checked(&extended_row,")");
 
2484
        row_length= 2 + extended_row.length;
 
2485
        if (total_length + row_length < opt_net_buffer_length)
2119
2486
        {
2120
2487
          total_length+= row_length;
2121
2488
          fputc(',',md_result_file);            /* Always row break */
2122
 
          fputs(extended_row.c_str(),md_result_file);
 
2489
          fputs(extended_row.str,md_result_file);
2123
2490
        }
2124
2491
        else
2125
2492
        {
2127
2494
            fputs(";\n", md_result_file);
2128
2495
          row_break=1;                          /* This is first row */
2129
2496
 
2130
 
          fputs(insert_pat.c_str(),md_result_file);
2131
 
          fputs(extended_row.c_str(),md_result_file);
 
2497
          fputs(insert_pat.str,md_result_file);
 
2498
          fputs(extended_row.str,md_result_file);
2132
2499
          total_length= row_length+init_length;
2133
2500
        }
2134
2501
        check_io(md_result_file);
2142
2509
 
2143
2510
    /* XML - close table tag and supress regular output */
2144
2511
    if (opt_xml)
2145
 
      fputs("\t</table_data>\n", md_result_file);
 
2512
        fputs("\t</table_data>\n", md_result_file);
2146
2513
    else if (extended_insert && row_break)
2147
2514
      fputs(";\n", md_result_file);             /* If not empty table */
2148
2515
    fflush(md_result_file);
2149
2516
    check_io(md_result_file);
 
2517
    if (mysql_errno(mysql))
 
2518
    {
 
2519
      snprintf(buf, sizeof(buf),
 
2520
               "%s: Error %d: %s when dumping table %s at row: %ld\n",
 
2521
               my_progname,
 
2522
               mysql_errno(mysql),
 
2523
               mysql_error(mysql),
 
2524
               result_table,
 
2525
               rownr);
 
2526
      fputs(buf,stderr);
 
2527
      error= EX_CONSCHECK;
 
2528
      goto err;
 
2529
    }
2150
2530
 
2151
2531
    /* Moved enable keys to before unlock per bug 15977 */
2152
2532
    if (opt_disable_keys)
2153
2533
    {
2154
 
      fprintf(md_result_file,"ALTER TABLE %s ENABLE KEYS;\n",
 
2534
      fprintf(md_result_file,"/*!40000 ALTER TABLE %s ENABLE KEYS */;\n",
2155
2535
              opt_quoted_table);
2156
2536
      check_io(md_result_file);
2157
2537
    }
 
2538
    if (opt_lock)
 
2539
    {
 
2540
      fputs("UNLOCK TABLES;\n", md_result_file);
 
2541
      check_io(md_result_file);
 
2542
    }
2158
2543
    if (opt_autocommit)
2159
2544
    {
2160
2545
      fprintf(md_result_file, "commit;\n");
2161
2546
      check_io(md_result_file);
2162
2547
    }
2163
 
    drizzle_result_free(&result);
 
2548
    mysql_free_result(res);
2164
2549
  }
2165
 
  return;
 
2550
  dynstr_free(&query_string);
 
2551
  DBUG_VOID_RETURN;
2166
2552
 
2167
2553
err:
 
2554
  dynstr_free(&query_string);
2168
2555
  maybe_exit(error);
2169
 
  return;
 
2556
  DBUG_VOID_RETURN;
2170
2557
} /* dump_table */
2171
2558
 
2172
2559
 
2173
2560
static char *getTableName(int reset)
2174
2561
{
2175
 
  static drizzle_result_st result;
2176
 
  static bool have_result= false;
2177
 
  drizzle_row_t row;
 
2562
  static MYSQL_RES *res= NULL;
 
2563
  MYSQL_ROW    row;
2178
2564
 
2179
 
  if (!have_result)
 
2565
  if (!res)
2180
2566
  {
2181
 
    if (drizzleclient_query_with_error_report(&dcon, &result, "SHOW TABLES", false))
2182
 
      return NULL;
2183
 
    have_result= true;
 
2567
    if (!(res= mysql_list_tables(mysql,NullS)))
 
2568
      return(NULL);
2184
2569
  }
2185
 
 
2186
 
  if ((row= drizzle_row_next(&result)))
2187
 
    return row[0];
 
2570
  if ((row= mysql_fetch_row(res)))
 
2571
    return((char*) row[0]);
2188
2572
 
2189
2573
  if (reset)
2190
 
    drizzle_row_seek(&result, 0);
 
2574
    mysql_data_seek(res,0);      /* We want to read again */
2191
2575
  else
2192
2576
  {
2193
 
    drizzle_result_free(&result);
2194
 
    have_result= false;
 
2577
    mysql_free_result(res);
 
2578
    res= NULL;
2195
2579
  }
2196
 
  return NULL;
 
2580
  return(NULL);
2197
2581
} /* getTableName */
2198
2582
 
2199
2583
 
2200
2584
static int dump_all_databases()
2201
2585
{
2202
 
  drizzle_row_t row;
2203
 
  drizzle_result_st tableres;
 
2586
  MYSQL_ROW row;
 
2587
  MYSQL_RES *tableres;
2204
2588
  int result=0;
2205
2589
 
2206
 
  if (drizzleclient_query_with_error_report(&dcon, &tableres, "SHOW DATABASES", false))
 
2590
  if (mysql_query_with_error_report(mysql, &tableres, "SHOW DATABASES"))
2207
2591
    return 1;
2208
 
  while ((row= drizzle_row_next(&tableres)))
 
2592
  while ((row= mysql_fetch_row(tableres)))
2209
2593
  {
2210
2594
    if (dump_all_tables_in_db(row[0]))
2211
2595
      result=1;
2212
2596
  }
2213
 
  drizzle_result_free(&tableres);
2214
2597
  return result;
2215
2598
}
2216
2599
/* dump_all_databases */
2220
2603
{
2221
2604
  int result=0;
2222
2605
  char **db;
2223
 
 
 
2606
  DBUG_ENTER("dump_databases");
2224
2607
 
2225
2608
  for (db= db_names ; *db ; db++)
2226
2609
  {
2227
2610
    if (dump_all_tables_in_db(*db))
2228
2611
      result=1;
2229
2612
  }
2230
 
  return(result);
 
2613
  DBUG_RETURN(result);
2231
2614
} /* dump_databases */
2232
2615
 
2233
2616
 
2234
2617
/*
2235
 
  Table Specific database initalization.
 
2618
Table Specific database initalization.
2236
2619
 
2237
 
  SYNOPSIS
 
2620
SYNOPSIS
2238
2621
  init_dumping_tables
2239
2622
  qdatabase      quoted name of the database
2240
2623
 
2241
 
  RETURN VALUES
 
2624
RETURN VALUES
2242
2625
  0        Success.
2243
2626
  1        Failure.
2244
2627
*/
2245
2628
 
2246
2629
int init_dumping_tables(char *qdatabase)
2247
2630
{
 
2631
  DBUG_ENTER("init_dumping_tables");
 
2632
 
2248
2633
  if (!opt_create_db)
2249
2634
  {
2250
2635
    char qbuf[256];
2251
 
    drizzle_row_t row;
2252
 
    drizzle_result_st result;
2253
 
    drizzle_return_t ret;
 
2636
    MYSQL_ROW row;
 
2637
    MYSQL_RES *dbinfo;
2254
2638
 
2255
2639
    snprintf(qbuf, sizeof(qbuf),
2256
2640
             "SHOW CREATE DATABASE IF NOT EXISTS %s",
2257
2641
             qdatabase);
2258
2642
 
2259
 
    if (drizzle_query_str(&dcon, &result, qbuf, &ret) == NULL ||
2260
 
        ret != DRIZZLE_RETURN_OK)
 
2643
    if (mysql_query(mysql, qbuf) || !(dbinfo = mysql_store_result(mysql)))
2261
2644
    {
2262
 
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
2263
 
        drizzle_result_free(&result);
2264
 
 
2265
2645
      /* Old server version, dump generic CREATE DATABASE */
2266
2646
      if (opt_drop_database)
2267
2647
        fprintf(md_result_file,
2268
 
                "\nDROP DATABASE IF EXISTS %s;\n",
 
2648
                "\n/*!40000 DROP DATABASE IF EXISTS %s*/;\n",
2269
2649
                qdatabase);
2270
2650
      fprintf(md_result_file,
2271
 
              "\nCREATE DATABASE IF NOT EXISTS %s;\n",
 
2651
              "\nCREATE DATABASE /*!32312 IF NOT EXISTS*/ %s;\n",
2272
2652
              qdatabase);
2273
2653
    }
2274
2654
    else
2275
2655
    {
2276
 
      if (drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
 
2656
      if (opt_drop_database)
 
2657
        fprintf(md_result_file,
 
2658
                "\n/*!40000 DROP DATABASE IF EXISTS %s*/;\n",
 
2659
                qdatabase);
 
2660
      row = mysql_fetch_row(dbinfo);
 
2661
      if (row[1])
2277
2662
      {
2278
 
        if (opt_drop_database)
2279
 
          fprintf(md_result_file,
2280
 
                  "\nDROP DATABASE IF EXISTS %s;\n",
2281
 
                  qdatabase);
2282
 
        row = drizzle_row_next(&result);
2283
 
        if (row != NULL && row[1])
2284
 
        {
2285
 
          fprintf(md_result_file,"\n%s;\n",row[1]);
2286
 
        }
 
2663
        fprintf(md_result_file,"\n%s;\n",row[1]);
2287
2664
      }
2288
 
      drizzle_result_free(&result);
 
2665
      mysql_free_result(dbinfo);
2289
2666
    }
2290
2667
  }
2291
 
  return(0);
 
2668
  DBUG_RETURN(0);
2292
2669
} /* init_dumping_tables */
2293
2670
 
2294
2671
 
2295
2672
static int init_dumping(char *database, int init_func(char*))
2296
2673
{
2297
 
  drizzle_result_st result;
2298
 
  drizzle_return_t ret;
2299
 
 
2300
 
  if (!my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
 
2674
  if (mysql_get_server_version(mysql) >= 50003 &&
 
2675
      !my_strcasecmp(&my_charset_latin1, database, "information_schema"))
2301
2676
    return 1;
2302
2677
 
2303
 
  if (drizzle_select_db(&dcon, &result, database, &ret) == NULL ||
2304
 
      ret != DRIZZLE_RETURN_OK)
 
2678
  if (mysql_select_db(mysql, database))
2305
2679
  {
2306
 
    DB_error(&result, ret, _("when executing 'SELECT INTO OUTFILE'"));
 
2680
    DB_error(mysql, "when selecting the database");
2307
2681
    return 1;                   /* If --force */
2308
2682
  }
2309
 
  drizzle_result_free(&result);
2310
 
 
2311
2683
  if (!path && !opt_xml)
2312
2684
  {
2313
2685
    if (opt_databases || opt_alldbs)
2315
2687
      /*
2316
2688
        length of table name * 2 (if name contains quotes), 2 quotes and 0
2317
2689
      */
2318
 
      char quoted_database_buf[DRIZZLE_MAX_DB_SIZE*2+3];
 
2690
      char quoted_database_buf[NAME_LEN*2+3];
2319
2691
      char *qdatabase= quote_name(database,quoted_database_buf,opt_quoted);
2320
2692
      if (opt_comments)
2321
2693
      {
2331
2703
    }
2332
2704
  }
2333
2705
  if (extended_insert)
2334
 
    extended_row.clear();
 
2706
    init_dynamic_string_checked(&extended_row, "", 1024, 1024);
2335
2707
  return 0;
2336
2708
} /* init_dumping */
2337
2709
 
2338
2710
 
2339
2711
/* Return 1 if we should copy the table */
2340
2712
 
2341
 
static bool include_table(const char *hash_key, size_t key_size)
 
2713
static my_bool include_table(const uchar *hash_key, size_t len)
2342
2714
{
2343
 
  string match(hash_key, key_size);
2344
 
  drizzled::hash_set<string>::iterator iter= ignore_table.find(match);
2345
 
  return (iter == ignore_table.end());
 
2715
  return !hash_search(&ignore_table, hash_key, len);
2346
2716
}
2347
2717
 
2348
2718
 
2349
2719
static int dump_all_tables_in_db(char *database)
2350
2720
{
2351
2721
  char *table;
2352
 
  char hash_key[DRIZZLE_MAX_DB_SIZE+DRIZZLE_MAX_TABLE_SIZE+2];  /* "db.tablename" */
 
2722
  uint numrows;
 
2723
  char table_buff[NAME_LEN*2+3];
 
2724
  char hash_key[2*NAME_LEN+2];  /* "db.tablename" */
2353
2725
  char *afterdot;
2354
 
  drizzle_result_st result;
2355
 
  drizzle_return_t ret;
 
2726
  int using_mysql_db= my_strcasecmp(&my_charset_latin1, database, "mysql");
 
2727
  DBUG_ENTER("dump_all_tables_in_db");
2356
2728
 
2357
 
  memset(hash_key, 0, DRIZZLE_MAX_DB_SIZE+DRIZZLE_MAX_TABLE_SIZE+2);
2358
 
  afterdot= strcpy(hash_key, database) + strlen(database);
 
2729
  afterdot= strmov(hash_key, database);
2359
2730
  *afterdot++= '.';
2360
2731
 
2361
2732
  if (init_dumping(database, init_dumping_tables))
2362
 
    return(1);
 
2733
    DBUG_RETURN(1);
2363
2734
  if (opt_xml)
2364
 
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NULL);
2365
 
  if (flush_logs)
 
2735
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NullS);
 
2736
  if (lock_tables)
2366
2737
  {
2367
 
    if (drizzle_query_str(&dcon, &result, "FLUSH LOGS", &ret) == NULL ||
2368
 
        ret != DRIZZLE_RETURN_OK)
 
2738
    DYNAMIC_STRING query;
 
2739
    init_dynamic_string_checked(&query, "LOCK TABLES ", 256, 1024);
 
2740
    for (numrows= 0 ; (table= getTableName(1)) ; )
2369
2741
    {
2370
 
      DB_error(&result, ret, _("when doing refresh"));
2371
 
      /* We shall continue here, if --force was given */
 
2742
      char *end= strmov(afterdot, table);
 
2743
      if (include_table((uchar*) hash_key,end - hash_key))
 
2744
      {
 
2745
        numrows++;
 
2746
        dynstr_append_checked(&query, quote_name(table, table_buff, 1));
 
2747
        dynstr_append_checked(&query, " READ /*!32311 LOCAL */,");
 
2748
      }
2372
2749
    }
2373
 
    else
2374
 
      drizzle_result_free(&result);
 
2750
    if (numrows && mysql_real_query(mysql, query.str, query.length-1))
 
2751
      DB_error(mysql, "when using LOCK TABLES");
 
2752
            /* We shall continue here, if --force was given */
 
2753
    dynstr_free(&query);
 
2754
  }
 
2755
  if (flush_logs)
 
2756
  {
 
2757
    if (mysql_refresh(mysql, REFRESH_LOG))
 
2758
      DB_error(mysql, "when doing refresh");
 
2759
           /* We shall continue here, if --force was given */
2375
2760
  }
2376
2761
  while ((table= getTableName(0)))
2377
2762
  {
2378
 
    char *end= strcpy(afterdot, table) + strlen(table);
2379
 
    if (include_table(hash_key, end - hash_key))
 
2763
    char *end= strmov(afterdot, table);
 
2764
    if (include_table((uchar*) hash_key, end - hash_key))
2380
2765
    {
2381
2766
      dump_table(table,database);
2382
 
      free(order_by);
 
2767
      my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2383
2768
      order_by= 0;
2384
2769
    }
2385
2770
  }
2388
2773
    fputs("</database>\n", md_result_file);
2389
2774
    check_io(md_result_file);
2390
2775
  }
2391
 
 
2392
 
  return 0;
 
2776
  if (lock_tables)
 
2777
    VOID(mysql_query_with_error_report(mysql, 0, "UNLOCK TABLES"));
 
2778
  if (flush_privileges && using_mysql_db == 0)
 
2779
  {
 
2780
    fprintf(md_result_file,"\n--\n-- Flush Grant Tables \n--\n");
 
2781
    fprintf(md_result_file,"\n/*! FLUSH PRIVILEGES */;\n");
 
2782
  }
 
2783
  DBUG_RETURN(0);
2393
2784
} /* dump_all_tables_in_db */
2394
2785
 
2395
2786
 
2400
2791
  different case (e.g.  T1 vs t1)
2401
2792
 
2402
2793
  RETURN
2403
 
  pointer to the table name
2404
 
  0 if error
 
2794
    pointer to the table name
 
2795
    0 if error
2405
2796
*/
2406
2797
 
2407
 
static char *get_actual_table_name(const char *old_table_name,
2408
 
                                   drizzled::memory::Root *root)
 
2798
static char *get_actual_table_name(const char *old_table_name, MEM_ROOT *root)
2409
2799
{
2410
2800
  char *name= 0;
2411
 
  drizzle_result_st result;
2412
 
  drizzle_row_t  row;
2413
 
  char query[50 + 2*DRIZZLE_MAX_TABLE_SIZE];
 
2801
  MYSQL_RES  *table_res;
 
2802
  MYSQL_ROW  row;
 
2803
  char query[50 + 2*NAME_LEN];
2414
2804
  char show_name_buff[FN_REFLEN];
2415
 
  uint64_t num_rows;
2416
 
 
 
2805
  DBUG_ENTER("get_actual_table_name");
2417
2806
 
2418
2807
  /* Check memory for quote_for_like() */
2419
 
  assert(2*sizeof(old_table_name) < sizeof(show_name_buff));
 
2808
  DBUG_ASSERT(2*sizeof(old_table_name) < sizeof(show_name_buff));
2420
2809
  snprintf(query, sizeof(query), "SHOW TABLES LIKE %s",
2421
2810
           quote_for_like(old_table_name, show_name_buff));
2422
2811
 
2423
 
  if (drizzleclient_query_with_error_report(&dcon, &result, query, false))
2424
 
    return NULL;
 
2812
  if (mysql_query_with_error_report(mysql, 0, query))
 
2813
    return NullS;
2425
2814
 
2426
 
  num_rows= drizzle_result_row_count(&result);
2427
 
  if (num_rows > 0)
 
2815
  if ((table_res= mysql_store_result(mysql)))
2428
2816
  {
2429
 
    size_t *lengths;
2430
 
    /*
2431
 
      Return first row
2432
 
      TODO-> Return all matching rows
2433
 
    */
2434
 
    row= drizzle_row_next(&result);
2435
 
    lengths= drizzle_row_field_sizes(&result);
2436
 
    name= strmake_root(root, row[0], lengths[0]);
 
2817
    my_ulonglong num_rows= mysql_num_rows(table_res);
 
2818
    if (num_rows > 0)
 
2819
    {
 
2820
      ulong *lengths;
 
2821
      /*
 
2822
        Return first row
 
2823
        TODO: Return all matching rows
 
2824
      */
 
2825
      row= mysql_fetch_row(table_res);
 
2826
      lengths= mysql_fetch_lengths(table_res);
 
2827
      name= strmake_root(root, row[0], lengths[0]);
 
2828
    }
 
2829
    mysql_free_result(table_res);
2437
2830
  }
2438
 
  drizzle_result_free(&result);
2439
 
 
2440
 
  return(name);
 
2831
  DBUG_PRINT("exit", ("new_table_name: %s", name));
 
2832
  DBUG_RETURN(name);
2441
2833
}
2442
2834
 
2443
2835
 
2444
2836
static int dump_selected_tables(char *db, char **table_names, int tables)
2445
2837
{
2446
 
  drizzled::memory::Root root;
 
2838
  char table_buff[NAME_LEN*2+3];
 
2839
  DYNAMIC_STRING lock_tables_query;
 
2840
  MEM_ROOT root;
2447
2841
  char **dump_tables, **pos, **end;
2448
 
  drizzle_result_st result;
2449
 
  drizzle_return_t ret;
2450
 
 
 
2842
  DBUG_ENTER("dump_selected_tables");
2451
2843
 
2452
2844
  if (init_dumping(db, init_dumping_tables))
2453
 
    return(1);
 
2845
    DBUG_RETURN(1);
2454
2846
 
2455
 
  init_alloc_root(&root, 8192);
 
2847
  init_alloc_root(&root, 8192, 0);
2456
2848
  if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *))))
2457
 
    die(EX_EOM, _("alloc_root failure."));
 
2849
     die(EX_EOM, "alloc_root failure.");
2458
2850
 
 
2851
  init_dynamic_string_checked(&lock_tables_query, "LOCK TABLES ", 256, 1024);
2459
2852
  for (; tables > 0 ; tables-- , table_names++)
2460
2853
  {
2461
2854
    /* the table name passed on commandline may be wrong case */
2462
2855
    if ((*pos= get_actual_table_name(*table_names, &root)))
2463
2856
    {
 
2857
      /* Add found table name to lock_tables_query */
 
2858
      if (lock_tables)
 
2859
      {
 
2860
        dynstr_append_checked(&lock_tables_query, quote_name(*pos, table_buff, 1));
 
2861
        dynstr_append_checked(&lock_tables_query, " READ /*!32311 LOCAL */,");
 
2862
      }
2464
2863
      pos++;
2465
2864
    }
2466
2865
    else
2467
2866
    {
2468
2867
      if (!ignore_errors)
2469
2868
      {
 
2869
        dynstr_free(&lock_tables_query);
2470
2870
        free_root(&root, MYF(0));
2471
2871
      }
2472
 
      maybe_die(EX_ILLEGAL_TABLE, _("Couldn't find table: \"%s\""), *table_names);
 
2872
      maybe_die(EX_ILLEGAL_TABLE, "Couldn't find table: \"%s\"", *table_names);
2473
2873
      /* We shall countinue here, if --force was given */
2474
2874
    }
2475
2875
  }
2476
2876
  end= pos;
2477
2877
 
 
2878
  if (lock_tables)
 
2879
  {
 
2880
    if (mysql_real_query(mysql, lock_tables_query.str,
 
2881
                         lock_tables_query.length-1))
 
2882
    {
 
2883
      if (!ignore_errors)
 
2884
      {
 
2885
        dynstr_free(&lock_tables_query);
 
2886
        free_root(&root, MYF(0));
 
2887
      }
 
2888
      DB_error(mysql, "when doing LOCK TABLES");
 
2889
       /* We shall countinue here, if --force was given */
 
2890
    }
 
2891
  }
 
2892
  dynstr_free(&lock_tables_query);
2478
2893
  if (flush_logs)
2479
2894
  {
2480
 
    if (drizzle_query_str(&dcon, &result, "FLUSH LOGS", &ret) == NULL ||
2481
 
        ret != DRIZZLE_RETURN_OK)
 
2895
    if (mysql_refresh(mysql, REFRESH_LOG))
2482
2896
    {
2483
2897
      if (!ignore_errors)
2484
2898
        free_root(&root, MYF(0));
2485
 
      DB_error(&result, ret, _("when doing refresh"));
2486
 
      /* We shall countinue here, if --force was given */
 
2899
      DB_error(mysql, "when doing refresh");
2487
2900
    }
2488
 
    else
2489
 
      drizzle_result_free(&result);
 
2901
     /* We shall countinue here, if --force was given */
2490
2902
  }
2491
2903
  if (opt_xml)
2492
 
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NULL);
 
2904
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NullS);
2493
2905
 
2494
2906
  /* Dump each selected table */
2495
2907
  for (pos= dump_tables; pos < end; pos++)
 
2908
  {
 
2909
    DBUG_PRINT("info",("Dumping table %s", *pos));
2496
2910
    dump_table(*pos, db);
 
2911
  }
2497
2912
 
2498
2913
  free_root(&root, MYF(0));
2499
 
  free(order_by);
 
2914
  my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2500
2915
  order_by= 0;
2501
2916
  if (opt_xml)
2502
2917
  {
2503
2918
    fputs("</database>\n", md_result_file);
2504
2919
    check_io(md_result_file);
2505
2920
  }
2506
 
  return 0;
 
2921
  if (lock_tables)
 
2922
    VOID(mysql_query_with_error_report(mysql, 0, "UNLOCK TABLES"));
 
2923
  DBUG_RETURN(0);
2507
2924
} /* dump_selected_tables */
2508
2925
 
2509
 
static int do_flush_tables_read_lock(drizzle_con_st *drizzle_con)
 
2926
 
 
2927
static int do_show_master_status(MYSQL *mysql_con)
 
2928
{
 
2929
  MYSQL_ROW row;
 
2930
  MYSQL_RES *master;
 
2931
  const char *comment_prefix=
 
2932
    (opt_master_data == MYSQL_OPT_MASTER_DATA_COMMENTED_SQL) ? "-- " : "";
 
2933
  if (mysql_query_with_error_report(mysql_con, &master, "SHOW MASTER STATUS"))
 
2934
  {
 
2935
    return 1;
 
2936
  }
 
2937
  else
 
2938
  {
 
2939
    row= mysql_fetch_row(master);
 
2940
    if (row && row[0] && row[1])
 
2941
    {
 
2942
      /* SHOW MASTER STATUS reports file and position */
 
2943
      if (opt_comments)
 
2944
        fprintf(md_result_file,
 
2945
                "\n--\n-- Position to start replication or point-in-time "
 
2946
                "recovery from\n--\n\n");
 
2947
      fprintf(md_result_file,
 
2948
              "%sCHANGE MASTER TO MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;\n",
 
2949
              comment_prefix, row[0], row[1]);
 
2950
      check_io(md_result_file);
 
2951
    }
 
2952
    else if (!ignore_errors)
 
2953
    {
 
2954
      /* SHOW MASTER STATUS reports nothing and --force is not enabled */
 
2955
      my_printf_error(0, "Error: Binlogging on server not active",
 
2956
                      MYF(0));
 
2957
      mysql_free_result(master);
 
2958
      maybe_exit(EX_MYSQLERR);
 
2959
      return 1;
 
2960
    }
 
2961
    mysql_free_result(master);
 
2962
  }
 
2963
  return 0;
 
2964
}
 
2965
 
 
2966
static int do_stop_slave_sql(MYSQL *mysql_con)
 
2967
{
 
2968
  MYSQL_RES *slave;
 
2969
  /* We need to check if the slave sql is running in the first place */
 
2970
  if (mysql_query_with_error_report(mysql_con, &slave, "SHOW SLAVE STATUS"))
 
2971
    return(1);
 
2972
  else
 
2973
  {
 
2974
    MYSQL_ROW row= mysql_fetch_row(slave);
 
2975
    if (row && row[11])
 
2976
    {
 
2977
      /* if SLAVE SQL is not running, we don't stop it */
 
2978
      if (!strcmp(row[11],"No"))
 
2979
      {
 
2980
        mysql_free_result(slave);
 
2981
        /* Silently assume that they don't have the slave running */
 
2982
        return(0);
 
2983
      }
 
2984
    }
 
2985
  }
 
2986
  mysql_free_result(slave);
 
2987
 
 
2988
  /* now, stop slave if running */
 
2989
  if (mysql_query_with_error_report(mysql_con, 0, "STOP SLAVE SQL_THREAD"))
 
2990
    return(1);
 
2991
 
 
2992
  return(0);
 
2993
}
 
2994
 
 
2995
static int add_stop_slave(void)
 
2996
{
 
2997
  if (opt_comments)
 
2998
    fprintf(md_result_file,
 
2999
            "\n--\n-- stop slave statement to make a recovery dump)\n--\n\n");
 
3000
  fprintf(md_result_file, "STOP SLAVE;\n");
 
3001
  return(0);
 
3002
}
 
3003
 
 
3004
static int add_slave_statements(void)
 
3005
{
 
3006
  if (opt_comments)
 
3007
    fprintf(md_result_file,
 
3008
            "\n--\n-- start slave statement to make a recovery dump)\n--\n\n");
 
3009
  fprintf(md_result_file, "START SLAVE;\n");
 
3010
  return(0);
 
3011
}
 
3012
 
 
3013
static int do_show_slave_status(MYSQL *mysql_con)
 
3014
{
 
3015
  MYSQL_RES *slave;
 
3016
  const char *comment_prefix=
 
3017
    (opt_slave_data == MYSQL_OPT_SLAVE_DATA_COMMENTED_SQL) ? "-- " : "";
 
3018
  if (mysql_query_with_error_report(mysql_con, &slave, "SHOW SLAVE STATUS"))
 
3019
  {
 
3020
    if (!ignore_errors)
 
3021
    {
 
3022
      /* SHOW SLAVE STATUS reports nothing and --force is not enabled */
 
3023
      my_printf_error(0, "Error: Slave not set up", MYF(0));
 
3024
    }
 
3025
    return 1;
 
3026
  }
 
3027
  else
 
3028
  {
 
3029
    MYSQL_ROW row= mysql_fetch_row(slave);
 
3030
    if (row && row[9] && row[21])
 
3031
    {
 
3032
      /* SHOW MASTER STATUS reports file and position */
 
3033
      if (opt_comments)
 
3034
        fprintf(md_result_file,
 
3035
                "\n--\n-- Position to start replication or point-in-time "
 
3036
                "recovery from (the master of this slave)\n--\n\n");
 
3037
 
 
3038
      fprintf(md_result_file, "%sCHANGE MASTER TO ", comment_prefix);
 
3039
 
 
3040
      if (opt_include_master_host_port)
 
3041
      {
 
3042
        if (row[1])
 
3043
          fprintf(md_result_file, "MASTER_HOST='%s', ", row[1]);
 
3044
        if (row[3])
 
3045
          fprintf(md_result_file, "MASTER_PORT='%s', ", row[3]);
 
3046
      }
 
3047
      fprintf(md_result_file,
 
3048
              "MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;\n", row[9], row[21]);
 
3049
 
 
3050
      check_io(md_result_file);
 
3051
    }
 
3052
    mysql_free_result(slave);
 
3053
  }
 
3054
  return 0;
 
3055
}
 
3056
 
 
3057
static int do_start_slave_sql(MYSQL *mysql_con)
 
3058
{
 
3059
  MYSQL_RES *slave;
 
3060
  /* We need to check if the slave sql is stopped in the first place */
 
3061
  if (mysql_query_with_error_report(mysql_con, &slave, "SHOW SLAVE STATUS"))
 
3062
    return(1);
 
3063
  else
 
3064
  {
 
3065
    MYSQL_ROW row= mysql_fetch_row(slave);
 
3066
    if (row && row[11])
 
3067
    {
 
3068
      /* if SLAVE SQL is not running, we don't start it */
 
3069
      if (!strcmp(row[11],"Yes"))
 
3070
      {
 
3071
        mysql_free_result(slave);
 
3072
        /* Silently assume that they don't have the slave running */
 
3073
        return(0);
 
3074
      }
 
3075
    }
 
3076
  }
 
3077
  mysql_free_result(slave);
 
3078
 
 
3079
  /* now, start slave if stopped */
 
3080
  if (mysql_query_with_error_report(mysql_con, 0, "START SLAVE"))
 
3081
  {
 
3082
    my_printf_error(0, "Error: Unable to start slave", MYF(0));
 
3083
    return 1;
 
3084
  }
 
3085
  return(0);
 
3086
}
 
3087
 
 
3088
 
 
3089
 
 
3090
static int do_flush_tables_read_lock(MYSQL *mysql_con)
2510
3091
{
2511
3092
  /*
2512
3093
    We do first a FLUSH TABLES. If a long update is running, the FLUSH TABLES
2513
3094
    will wait but will not stall the whole mysqld, and when the long update is
2514
3095
    done the FLUSH TABLES WITH READ LOCK will start and succeed quickly. So,
2515
 
    FLUSH TABLES is to lower the probability of a stage where both drizzled
 
3096
    FLUSH TABLES is to lower the probability of a stage where both mysqldump
2516
3097
    and most client connections are stalled. Of course, if a second long
2517
3098
    update starts between the two FLUSHes, we have that bad stall.
2518
3099
  */
2519
3100
  return
2520
 
    ( drizzleclient_query_with_error_report(drizzle_con, 0, "FLUSH TABLES", false) ||
2521
 
      drizzleclient_query_with_error_report(drizzle_con, 0,
2522
 
                                            "FLUSH TABLES WITH READ LOCK", false) );
2523
 
}
2524
 
 
2525
 
static int do_unlock_tables(drizzle_con_st *drizzle_con)
2526
 
{
2527
 
  return drizzleclient_query_with_error_report(drizzle_con, 0, "UNLOCK TABLES", false);
2528
 
}
2529
 
 
2530
 
static int start_transaction(drizzle_con_st *drizzle_con)
2531
 
{
2532
 
  return (drizzleclient_query_with_error_report(drizzle_con, 0,
2533
 
                                                "SET SESSION TRANSACTION ISOLATION "
2534
 
                                                "LEVEL REPEATABLE READ", false) ||
2535
 
          drizzleclient_query_with_error_report(drizzle_con, 0,
2536
 
                                                "START TRANSACTION "
2537
 
                                                "WITH CONSISTENT SNAPSHOT", false));
2538
 
}
2539
 
 
2540
 
 
2541
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint32_t length,
2542
 
                         char **err_pos, uint32_t *err_len)
 
3101
    ( mysql_query_with_error_report(mysql_con, 0, "FLUSH TABLES") ||
 
3102
      mysql_query_with_error_report(mysql_con, 0,
 
3103
                                    "FLUSH TABLES WITH READ LOCK") );
 
3104
}
 
3105
 
 
3106
 
 
3107
static int do_unlock_tables(MYSQL *mysql_con)
 
3108
{
 
3109
  return mysql_query_with_error_report(mysql_con, 0, "UNLOCK TABLES");
 
3110
}
 
3111
 
 
3112
static int get_bin_log_name(MYSQL *mysql_con,
 
3113
                            char* buff_log_name, uint buff_len)
 
3114
{
 
3115
  MYSQL_RES *res;
 
3116
  MYSQL_ROW row;
 
3117
 
 
3118
  if (mysql_query(mysql_con, "SHOW MASTER STATUS") ||
 
3119
      !(res= mysql_store_result(mysql)))
 
3120
    return 1;
 
3121
 
 
3122
  if (!(row= mysql_fetch_row(res)))
 
3123
  {
 
3124
    mysql_free_result(res);
 
3125
    return 1;
 
3126
  }
 
3127
  /*
 
3128
    Only one row is returned, and the first column is the name of the
 
3129
    active log.
 
3130
  */
 
3131
  strmake(buff_log_name, row[0], buff_len - 1);
 
3132
 
 
3133
  mysql_free_result(res);
 
3134
  return 0;
 
3135
}
 
3136
 
 
3137
static int purge_bin_logs_to(MYSQL *mysql_con, char* log_name)
 
3138
{
 
3139
  DYNAMIC_STRING str;
 
3140
  int err;
 
3141
  init_dynamic_string_checked(&str, "PURGE BINARY LOGS TO '", 1024, 1024);
 
3142
  dynstr_append_checked(&str, log_name);
 
3143
  dynstr_append_checked(&str, "'");
 
3144
  err = mysql_query_with_error_report(mysql_con, 0, str.str);
 
3145
  dynstr_free(&str);
 
3146
  return err;
 
3147
}
 
3148
 
 
3149
 
 
3150
static int start_transaction(MYSQL *mysql_con)
 
3151
{
 
3152
  /*
 
3153
    We use BEGIN for old servers. --single-transaction --master-data will fail
 
3154
    on old servers, but that's ok as it was already silently broken (it didn't
 
3155
    do a consistent read, so better tell people frankly, with the error).
 
3156
 
 
3157
    We want the first consistent read to be used for all tables to dump so we
 
3158
    need the REPEATABLE READ level (not anything lower, for example READ
 
3159
    COMMITTED would give one new consistent read per dumped table).
 
3160
  */
 
3161
  if ((mysql_get_server_version(mysql_con) < 40100) && opt_master_data)
 
3162
  {
 
3163
    fprintf(stderr, "-- %s: the combination of --single-transaction and "
 
3164
            "--master-data requires a MySQL server version of at least 4.1 "
 
3165
            "(current server's version is %s). %s\n",
 
3166
            ignore_errors ? "Warning" : "Error",
 
3167
            mysql_con->server_version ? mysql_con->server_version : "unknown",
 
3168
            ignore_errors ? "Continuing due to --force, backup may not be consistent across all tables!" : "Aborting.");
 
3169
    if (!ignore_errors)
 
3170
      exit(EX_MYSQLERR);
 
3171
  }
 
3172
 
 
3173
  return (mysql_query_with_error_report(mysql_con, 0,
 
3174
                                        "SET SESSION TRANSACTION ISOLATION "
 
3175
                                        "LEVEL REPEATABLE READ") ||
 
3176
          mysql_query_with_error_report(mysql_con, 0,
 
3177
                                        "START TRANSACTION "
 
3178
                                        "/*!40100 WITH CONSISTENT SNAPSHOT */"));
 
3179
}
 
3180
 
 
3181
 
 
3182
static ulong find_set(TYPELIB *lib, const char *x, uint length,
 
3183
                      char **err_pos, uint *err_len)
2543
3184
{
2544
3185
  const char *end= x + length;
2545
 
  uint32_t found= 0;
2546
 
  uint32_t find;
 
3186
  ulong found= 0;
 
3187
  uint find;
2547
3188
  char buff[255];
2548
3189
 
2549
3190
  *err_pos= 0;                  /* No error yet */
2557
3198
    for (;;)
2558
3199
    {
2559
3200
      const char *pos= start;
2560
 
      uint32_t var_len;
 
3201
      uint var_len;
2561
3202
 
2562
3203
      for (; pos != end && *pos != ','; pos++) ;
2563
 
      var_len= (uint32_t) (pos - start);
2564
 
      strncpy(buff, start, min((uint32_t)sizeof(buff), var_len+1));
 
3204
      var_len= (uint) (pos - start);
 
3205
      strmake(buff, start, min(sizeof(buff), var_len));
2565
3206
      find= find_type(buff, lib, var_len);
2566
3207
      if (!find)
2567
3208
      {
2569
3210
        *err_len= var_len;
2570
3211
      }
2571
3212
      else
2572
 
        found|= (uint32_t)((int64_t) 1 << (find - 1));
 
3213
        found|= ((longlong) 1 << (find - 1));
2573
3214
      if (pos == end)
2574
3215
        break;
2575
3216
      start= pos + 1;
2580
3221
 
2581
3222
 
2582
3223
/* Print a value with a prefix on file */
2583
 
static void print_value(FILE *file, drizzle_result_st  *result, drizzle_row_t row,
 
3224
static void print_value(FILE *file, MYSQL_RES  *result, MYSQL_ROW row,
2584
3225
                        const char *prefix, const char *name,
2585
3226
                        int string_value)
2586
3227
{
2587
 
  drizzle_column_st *column;
2588
 
  drizzle_column_seek(result, 0);
 
3228
  MYSQL_FIELD   *field;
 
3229
  mysql_field_seek(result, 0);
2589
3230
 
2590
 
  for ( ; (column= drizzle_column_next(result)) ; row++)
 
3231
  for ( ; (field= mysql_fetch_field(result)) ; row++)
2591
3232
  {
2592
 
    if (!strcmp(drizzle_column_name(column),name))
 
3233
    if (!strcmp(field->name,name))
2593
3234
    {
2594
3235
      if (row[0] && row[0][0] && strcmp(row[0],"0")) /* Skip default */
2595
3236
      {
2596
3237
        fputc(' ',file);
2597
3238
        fputs(prefix, file);
2598
3239
        if (string_value)
2599
 
          unescape(file,row[0],(uint32_t) strlen(row[0]));
 
3240
          unescape(file,row[0],(uint) strlen(row[0]));
2600
3241
        else
2601
3242
          fputs(row[0], file);
2602
3243
        check_io(file);
2607
3248
  return;                                       /* This shouldn't happen */
2608
3249
} /* print_value */
2609
3250
 
2610
 
/**
2611
 
 * Fetches a row from a result based on a field name
2612
 
 * Returns const char* of the data in that row or NULL if not found
2613
 
 */
2614
 
 
2615
 
static const char* fetch_named_row(drizzle_result_st *result, drizzle_row_t row, const char *name)
2616
 
{
2617
 
  drizzle_column_st *column;
2618
 
  drizzle_column_seek(result, 0);
2619
 
 
2620
 
  for ( ; (column= drizzle_column_next(result)) ; row++)
2621
 
  {
2622
 
    if (!strcmp(drizzle_column_name(column),name))
2623
 
    {
2624
 
      if (row[0] && row[0][0] && strcmp(row[0],"0")) /* Skip default */
2625
 
      {
2626
 
        drizzle_column_seek(result, 0);
2627
 
        return row[0];
2628
 
      }
2629
 
    }
2630
 
  }
2631
 
  drizzle_column_seek(result, 0);
2632
 
  return NULL;
2633
 
}
2634
 
 
2635
3251
 
2636
3252
/*
2637
3253
  SYNOPSIS
2638
3254
 
2639
3255
  Check if we the table is one of the table types that should be ignored:
2640
3256
  MRG_ISAM, MRG_MYISAM, if opt_delayed, if that table supports delayed inserts.
2641
 
  If the table should be altogether ignored, it returns a true, false if it
 
3257
  If the table should be altogether ignored, it returns a TRUE, FALSE if it
2642
3258
  should not be ignored. If the user has selected to use INSERT DELAYED, it
2643
3259
  sets the value of the bool pointer supports_delayed_inserts to 0 if not
2644
3260
  supported, 1 if it is supported.
2645
3261
 
2646
3262
  ARGS
2647
3263
 
2648
 
  check_if_ignore_table()
2649
 
  table_name                  Table name to check
2650
 
  table_type                  Type of table
 
3264
    check_if_ignore_table()
 
3265
    table_name                  Table name to check
 
3266
    table_type                  Type of table
2651
3267
 
2652
3268
  GLOBAL VARIABLES
2653
 
  drizzle                       Drizzle connection
2654
 
  verbose                     Write warning messages
 
3269
    mysql                       MySQL connection
 
3270
    verbose                     Write warning messages
2655
3271
 
2656
3272
  RETURN
2657
 
  char (bit value)            See IGNORE_ values at top
 
3273
    char (bit value)            See IGNORE_ values at top
2658
3274
*/
2659
3275
 
2660
3276
char check_if_ignore_table(const char *table_name, char *table_type)
2661
3277
{
2662
3278
  char result= IGNORE_NONE;
2663
3279
  char buff[FN_REFLEN+80], show_name_buff[FN_REFLEN];
2664
 
  const char *number_of_rows= NULL;
2665
 
  drizzle_result_st res;
2666
 
  drizzle_row_t row;
 
3280
  MYSQL_RES *res= NULL;
 
3281
  MYSQL_ROW row;
 
3282
  DBUG_ENTER("check_if_ignore_table");
2667
3283
 
2668
3284
  /* Check memory for quote_for_like() */
2669
 
  assert(2*sizeof(table_name) < sizeof(show_name_buff));
 
3285
  DBUG_ASSERT(2*sizeof(table_name) < sizeof(show_name_buff));
2670
3286
  snprintf(buff, sizeof(buff), "show table status like %s",
2671
3287
           quote_for_like(table_name, show_name_buff));
2672
 
  if (drizzleclient_query_with_error_report(&dcon, &res, buff, false))
 
3288
  if (mysql_query_with_error_report(mysql, &res, buff))
2673
3289
  {
2674
 
    return result;
 
3290
    if (mysql_errno(mysql) != ER_PARSE_ERROR)
 
3291
    {                                   /* If old MySQL version */
 
3292
      verbose_msg("-- Warning: Couldn't get status information for "
 
3293
                  "table %s (%s)\n", table_name, mysql_error(mysql));
 
3294
      DBUG_RETURN(result);                       /* assume table is ok */
 
3295
    }
2675
3296
  }
2676
 
  if (!(row= drizzle_row_next(&res)))
 
3297
  if (!(row= mysql_fetch_row(res)))
2677
3298
  {
2678
3299
    fprintf(stderr,
2679
 
            _("Error: Couldn't read status information for table %s\n"),
2680
 
            table_name);
2681
 
    drizzle_result_free(&res);
2682
 
    return(result);                         /* assume table is ok */
 
3300
            "Error: Couldn't read status information for table %s (%s)\n",
 
3301
            table_name, mysql_error(mysql));
 
3302
    mysql_free_result(res);
 
3303
    DBUG_RETURN(result);                         /* assume table is ok */
2683
3304
  }
 
3305
  if (!(row[1]))
 
3306
    strmake(table_type, "VIEW", NAME_LEN-1);
2684
3307
  else
2685
3308
  {
2686
 
    if ((number_of_rows= fetch_named_row(&res, row, "Rows")) != NULL)
2687
 
    {
2688
 
      total_rows= strtoul(number_of_rows, NULL, 10);
2689
 
    }
2690
 
  }
2691
 
  /*
2692
 
    If the table type matches any of these, we do support delayed inserts.
2693
 
    Note-> we do not want to skip dumping this table if if is not one of
2694
 
    these types, but we do want to use delayed inserts in the dump if
2695
 
    the table type is _NOT_ one of these types
2696
 
  */
2697
 
  {
2698
 
    strncpy(table_type, row[1], DRIZZLE_MAX_TABLE_SIZE-1);
 
3309
    /*
 
3310
      If the table type matches any of these, we do support delayed inserts.
 
3311
      Note: we do not want to skip dumping this table if if is not one of
 
3312
      these types, but we do want to use delayed inserts in the dump if
 
3313
      the table type is _NOT_ one of these types
 
3314
    */
 
3315
    strmake(table_type, row[1], NAME_LEN-1);
2699
3316
    if (opt_delayed)
2700
3317
    {
2701
3318
      if (strcmp(table_type,"MyISAM") &&
 
3319
          strcmp(table_type,"ISAM") &&
2702
3320
          strcmp(table_type,"ARCHIVE") &&
2703
3321
          strcmp(table_type,"HEAP") &&
2704
3322
          strcmp(table_type,"MEMORY"))
2705
3323
        result= IGNORE_INSERT_DELAYED;
2706
3324
    }
 
3325
 
 
3326
    /*
 
3327
      If these two types, we do want to skip dumping the table
 
3328
    */
 
3329
    if (!opt_no_data &&
 
3330
        (!my_strcasecmp(&my_charset_latin1, table_type, "MRG_MyISAM") ||
 
3331
         !strcmp(table_type,"MRG_ISAM")))
 
3332
      result= IGNORE_DATA;
2707
3333
  }
2708
 
  drizzle_result_free(&res);
2709
 
  return(result);
 
3334
  mysql_free_result(res);
 
3335
  DBUG_RETURN(result);
2710
3336
}
2711
3337
 
2712
3338
 
2714
3340
  Get string of comma-separated primary key field names
2715
3341
 
2716
3342
  SYNOPSIS
2717
 
  char *primary_key_fields(const char *table_name)
2718
 
  RETURNS     pointer to allocated buffer (must be freed by caller)
2719
 
  table_name  quoted table name
 
3343
    char *primary_key_fields(const char *table_name)
 
3344
    RETURNS     pointer to allocated buffer (must be freed by caller)
 
3345
    table_name  quoted table name
2720
3346
 
2721
3347
  DESCRIPTION
2722
 
  Use SHOW KEYS FROM table_name, allocate a buffer to hold the
2723
 
  field names, and then build that string and return the pointer
2724
 
  to that buffer.
 
3348
    Use SHOW KEYS FROM table_name, allocate a buffer to hold the
 
3349
    field names, and then build that string and return the pointer
 
3350
    to that buffer.
2725
3351
 
2726
 
  Returns NULL if there is no PRIMARY or UNIQUE key on the table,
2727
 
  or if there is some failure.  It is better to continue to dump
2728
 
  the table unsorted, rather than exit without dumping the data.
 
3352
    Returns NULL if there is no PRIMARY or UNIQUE key on the table,
 
3353
    or if there is some failure.  It is better to continue to dump
 
3354
    the table unsorted, rather than exit without dumping the data.
2729
3355
*/
2730
3356
 
2731
3357
static char *primary_key_fields(const char *table_name)
2732
3358
{
2733
 
  drizzle_result_st res;
2734
 
  drizzle_return_t ret;
2735
 
  drizzle_row_t  row;
 
3359
  MYSQL_RES  *res= NULL;
 
3360
  MYSQL_ROW  row;
2736
3361
  /* SHOW KEYS FROM + table name * 2 (escaped) + 2 quotes + \0 */
2737
 
  char show_keys_buff[15 + DRIZZLE_MAX_TABLE_SIZE * 2 + 3];
2738
 
  uint32_t result_length= 0;
 
3362
  char show_keys_buff[15 + NAME_LEN * 2 + 3];
 
3363
  uint result_length= 0;
2739
3364
  char *result= 0;
2740
 
  char buff[DRIZZLE_MAX_TABLE_SIZE * 2 + 3];
 
3365
  char buff[NAME_LEN * 2 + 3];
2741
3366
  char *quoted_field;
2742
3367
 
2743
3368
  snprintf(show_keys_buff, sizeof(show_keys_buff),
2744
3369
           "SHOW KEYS FROM %s", table_name);
2745
 
  if (drizzle_query_str(&dcon, &res, show_keys_buff, &ret) == NULL ||
2746
 
      ret != DRIZZLE_RETURN_OK)
2747
 
  {
2748
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
2749
 
    {
2750
 
      fprintf(stderr, _("Warning: Couldn't read keys from table %s;"
2751
 
                        " records are NOT sorted (%s)\n"),
2752
 
              table_name, drizzle_result_error(&res));
2753
 
      drizzle_result_free(&res);
2754
 
    }
2755
 
    else
2756
 
    {
2757
 
      fprintf(stderr, _("Warning: Couldn't read keys from table %s;"
2758
 
                        " records are NOT sorted (%s)\n"),
2759
 
              table_name, drizzle_con_error(&dcon));
2760
 
    }
2761
 
 
2762
 
    return result;
2763
 
  }
2764
 
 
2765
 
  if (drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
2766
 
  {
2767
 
    fprintf(stderr, _("Warning: Couldn't read keys from table %s;"
2768
 
                      " records are NOT sorted (%s)\n"),
2769
 
            table_name, drizzle_con_error(&dcon));
2770
 
    return result;
 
3370
  if (mysql_query(mysql, show_keys_buff) ||
 
3371
      !(res= mysql_store_result(mysql)))
 
3372
  {
 
3373
    fprintf(stderr, "Warning: Couldn't read keys from table %s;"
 
3374
            " records are NOT sorted (%s)\n",
 
3375
            table_name, mysql_error(mysql));
 
3376
    /* Don't exit, because it's better to print out unsorted records */
 
3377
    goto cleanup;
2771
3378
  }
2772
3379
 
2773
3380
  /*
2775
3382
   * Note that SHOW KEYS is ordered:  a PRIMARY key is always the first
2776
3383
   * row, and UNIQUE keys come before others.  So we only need to check
2777
3384
   * the first key, not all keys.
2778
 
 */
2779
 
  if ((row= drizzle_row_next(&res)) && atoi(row[1]) == 0)
 
3385
   */
 
3386
  if ((row= mysql_fetch_row(res)) && atoi(row[1]) == 0)
2780
3387
  {
2781
3388
    /* Key is unique */
2782
3389
    do
2783
3390
    {
2784
3391
      quoted_field= quote_name(row[4], buff, 0);
2785
3392
      result_length+= strlen(quoted_field) + 1; /* + 1 for ',' or \0 */
2786
 
    } while ((row= drizzle_row_next(&res)) && atoi(row[3]) > 1);
 
3393
    } while ((row= mysql_fetch_row(res)) && atoi(row[3]) > 1);
2787
3394
  }
2788
3395
 
2789
3396
  /* Build the ORDER BY clause result */
2791
3398
  {
2792
3399
    char *end;
2793
3400
    /* result (terminating \0 is already in result_length) */
2794
 
 
2795
 
    size_t result_length_alloc= result_length + 10;
2796
 
    result= (char *)malloc(result_length_alloc);
 
3401
    result= my_malloc(result_length + 10, MYF(MY_WME));
2797
3402
    if (!result)
2798
3403
    {
2799
 
      fprintf(stderr, _("Error: Not enough memory to store ORDER BY clause\n"));
2800
 
      drizzle_result_free(&res);
2801
 
      return result;
 
3404
      fprintf(stderr, "Error: Not enough memory to store ORDER BY clause\n");
 
3405
      goto cleanup;
2802
3406
    }
2803
 
    drizzle_row_seek(&res, 0);
2804
 
    row= drizzle_row_next(&res);
 
3407
    mysql_data_seek(res, 0);
 
3408
    row= mysql_fetch_row(res);
2805
3409
    quoted_field= quote_name(row[4], buff, 0);
2806
 
    end= strcpy(result, quoted_field) + strlen(quoted_field);
2807
 
    result_length_alloc -= strlen(quoted_field);
2808
 
    while ((row= drizzle_row_next(&res)) && atoi(row[3]) > 1)
 
3410
    end= strmov(result, quoted_field);
 
3411
    while ((row= mysql_fetch_row(res)) && atoi(row[3]) > 1)
2809
3412
    {
2810
3413
      quoted_field= quote_name(row[4], buff, 0);
2811
 
      end+= snprintf(end, result_length_alloc, ",%s",quoted_field);
2812
 
      result_length_alloc -= strlen(quoted_field);
 
3414
      end= strxmov(end, ",", quoted_field, NullS);
2813
3415
    }
2814
3416
  }
2815
3417
 
2816
 
  drizzle_result_free(&res);
 
3418
cleanup:
 
3419
  if (res)
 
3420
    mysql_free_result(res);
 
3421
 
2817
3422
  return result;
2818
3423
}
2819
3424
 
 
3425
/*
 
3426
  The following functions are wrappers for the dynamic string functions
 
3427
  and if they fail, the wrappers will terminate the current process.
 
3428
*/
 
3429
 
 
3430
#define DYNAMIC_STR_ERROR_MSG "Couldn't perform DYNAMIC_STRING operation"
 
3431
 
 
3432
static void init_dynamic_string_checked(DYNAMIC_STRING *str, const char *init_str,
 
3433
                            uint init_alloc, uint alloc_increment)
 
3434
{
 
3435
  if (init_dynamic_string(str, init_str, init_alloc, alloc_increment))
 
3436
    die(EX_MYSQLERR, DYNAMIC_STR_ERROR_MSG);
 
3437
}
 
3438
 
 
3439
static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src)
 
3440
{
 
3441
  if (dynstr_append(dest, src))
 
3442
    die(EX_MYSQLERR, DYNAMIC_STR_ERROR_MSG);
 
3443
}
 
3444
 
 
3445
static void dynstr_set_checked(DYNAMIC_STRING *str, const char *init_str)
 
3446
{
 
3447
  if (dynstr_set(str, init_str))
 
3448
    die(EX_MYSQLERR, DYNAMIC_STR_ERROR_MSG);
 
3449
}
 
3450
 
 
3451
static void dynstr_append_mem_checked(DYNAMIC_STRING *str, const char *append,
 
3452
                          uint length)
 
3453
{
 
3454
  if (dynstr_append_mem(str, append, length))
 
3455
    die(EX_MYSQLERR, DYNAMIC_STR_ERROR_MSG);
 
3456
}
 
3457
 
 
3458
static void dynstr_realloc_checked(DYNAMIC_STRING *str, ulong additional_size)
 
3459
{
 
3460
  if (dynstr_realloc(str, additional_size))
 
3461
    die(EX_MYSQLERR, DYNAMIC_STR_ERROR_MSG);
 
3462
}
 
3463
 
2820
3464
 
2821
3465
int main(int argc, char **argv)
2822
3466
{
 
3467
  char bin_log_name[FN_REFLEN];
2823
3468
  int exit_code;
2824
 
  MY_INIT("drizzledump");
2825
 
  drizzle_result_st result;
 
3469
  MY_INIT("mysqldump");
2826
3470
 
2827
3471
  compatible_mode_normal_str[0]= 0;
 
3472
  default_charset= (char *)mysql_universal_client_charset;
 
3473
  bzero((char*) &ignore_table, sizeof(ignore_table));
2828
3474
 
2829
3475
  exit_code= get_options(&argc, &argv);
2830
3476
  if (exit_code)
2833
3479
    exit(exit_code);
2834
3480
  }
2835
3481
 
 
3482
  if (log_error_file)
 
3483
  {
 
3484
    if(!(stderror_file= freopen(log_error_file, "a+", stderr)))
 
3485
    {
 
3486
      free_resources();
 
3487
      exit(EX_MYSQLERR);
 
3488
    }
 
3489
  }
 
3490
 
2836
3491
  if (connect_to_db(current_host, current_user, opt_password))
2837
3492
  {
2838
3493
    free_resources();
2839
 
    exit(EX_DRIZZLEERR);
 
3494
    exit(EX_MYSQLERR);
2840
3495
  }
2841
3496
  if (!path)
2842
3497
    write_header(md_result_file, *argv);
2843
3498
 
2844
 
  if ((opt_lock_all_tables) && do_flush_tables_read_lock(&dcon))
2845
 
    goto err;
2846
 
  if (opt_single_transaction && start_transaction(&dcon))
2847
 
    goto err;
2848
 
  if (opt_lock_all_tables)
2849
 
  {
2850
 
    if (drizzleclient_query_with_error_report(&dcon, &result, "FLUSH LOGS", false))
2851
 
      goto err;
2852
 
    drizzle_result_free(&result);
 
3499
  if (opt_slave_data && do_stop_slave_sql(mysql))
 
3500
    goto err;
 
3501
 
 
3502
  if ((opt_lock_all_tables || opt_master_data) &&
 
3503
      do_flush_tables_read_lock(mysql))
 
3504
    goto err;
 
3505
  if (opt_single_transaction && start_transaction(mysql))
 
3506
      goto err;
 
3507
  if (opt_delete_master_logs)
 
3508
  {
 
3509
    if (mysql_refresh(mysql, REFRESH_LOG) ||
 
3510
        get_bin_log_name(mysql, bin_log_name, sizeof(bin_log_name)))
 
3511
      goto err;
 
3512
    flush_logs= 0;
 
3513
  }
 
3514
  if (opt_lock_all_tables || opt_master_data)
 
3515
  {
 
3516
    if (flush_logs && mysql_refresh(mysql, REFRESH_LOG))
 
3517
      goto err;
2853
3518
    flush_logs= 0; /* not anymore; that would not be sensible */
2854
3519
  }
2855
 
  if (opt_single_transaction && do_unlock_tables(&dcon)) /* unlock but no commit! */
 
3520
  /* Add 'STOP SLAVE to beginning of dump */
 
3521
  if (opt_slave_apply && add_stop_slave())
 
3522
    goto err;
 
3523
  if (opt_master_data && do_show_master_status(mysql))
 
3524
    goto err;
 
3525
  if (opt_slave_data && do_show_slave_status(mysql))
 
3526
    goto err;
 
3527
  if (opt_single_transaction && do_unlock_tables(mysql)) /* unlock but no commit! */
2856
3528
    goto err;
2857
3529
 
2858
3530
  if (opt_alldbs)
2869
3541
    dump_databases(argv);
2870
3542
  }
2871
3543
 
 
3544
  /* if --dump-slave , start the slave sql thread */
 
3545
  if (opt_slave_data && do_start_slave_sql(mysql))
 
3546
    goto err;
 
3547
 
 
3548
  /* add 'START SLAVE' to end of dump */
 
3549
  if (opt_slave_apply && add_slave_statements())
 
3550
    goto err;
 
3551
 
2872
3552
  /* ensure dumped data flushed */
2873
3553
  if (md_result_file && fflush(md_result_file))
2874
3554
  {
2875
3555
    if (!first_error)
2876
 
      first_error= EX_DRIZZLEERR;
 
3556
      first_error= EX_MYSQLERR;
2877
3557
    goto err;
2878
3558
  }
 
3559
  /* everything successful, purge the old logs files */
 
3560
  if (opt_delete_master_logs && purge_bin_logs_to(mysql, bin_log_name))
 
3561
    goto err;
2879
3562
 
 
3563
#ifdef HAVE_SMEM
 
3564
  my_free(shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
 
3565
#endif
2880
3566
  /*
2881
3567
    No reason to explicitely COMMIT the transaction, neither to explicitely
2882
3568
    UNLOCK TABLES: these will be automatically be done by the server when we