~drizzle-trunk/drizzle/development

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