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