~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>
37
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
38
#include <drizzled/gettext.h>
39
212.5.42 by Monty Taylor
Ding dong include is dead.
40
#include <drizzled/error.h>
1 by brian
clean slate
41
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
42
using namespace std;
1 by brian
clean slate
43
/* Exit codes */
44
45
#define EX_USAGE 1
206.3.1 by Patrick Galbraith
Most everything working with client rename
46
#define EX_DRIZZLEERR 2
1 by brian
clean slate
47
#define EX_CONSCHECK 3
48
#define EX_EOM 4
49
#define EX_EOF 5 /* ferror for output file was got */
50
#define EX_ILLEGAL_TABLE 6
51
52
/* index into 'show fields from table' */
53
54
#define SHOW_FIELDNAME  0
55
#define SHOW_TYPE  1
56
#define SHOW_NULL  2
57
#define SHOW_DEFAULT  4
58
#define SHOW_EXTRA  5
59
60
/* Size of buffer for dump's select query */
61
#define QUERY_LENGTH 1536
62
63
/* ignore table flags */
64
#define IGNORE_NONE 0x00 /* no ignore */
65
#define IGNORE_DATA 0x01 /* don't dump data for this table */
66
#define IGNORE_INSERT_DELAYED 0x02 /* table doesn't support INSERT DELAYED */
67
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
68
static void add_load_option(string &str, const char *option,
69
                            const char *option_value);
288 by Brian Aker
ulong cleanp in client apps
70
static uint32_t find_set(TYPELIB *lib, const char *x, uint length,
1 by brian
clean slate
71
                      char **err_pos, uint *err_len);
72
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
73
static void field_escape(string &in, const char *from);
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
74
static bool  verbose= false, opt_no_create_info= false, opt_no_data= false,
75
                quick= true, extended_insert= true,
76
                lock_tables= true, ignore_errors= false, flush_logs= false,
673.5.14 by Andrew Hutchings
Merge with trunk
77
                opt_drop= true, opt_keywords= false,
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
78
                opt_lock= true, opt_compress= false,
79
                opt_delayed= false, create_options= true, opt_quoted= false,
80
                opt_databases= false, opt_alldbs= false, opt_create_db= false,
81
                opt_lock_all_tables= false,
82
                opt_set_charset= false, opt_dump_date= true,
83
                opt_autocommit= false, opt_disable_keys= true, opt_xml= false,
84
                opt_delete_master_logs= false, tty_password= false,
85
                opt_single_transaction= false, opt_comments= false,
86
                opt_compact= false, opt_hex_blob= false, 
87
                opt_order_by_primary=false, opt_ignore= false,
88
                opt_complete_insert= false, opt_drop_database= false,
89
                opt_replace_into= false,
90
                opt_routines= false,
91
                opt_slave_apply= false,
92
                opt_include_master_host_port= false,
673.5.14 by Andrew Hutchings
Merge with trunk
93
                opt_alltspcs= false;
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
94
static bool debug_info_flag= false, debug_check_flag= false;
288 by Brian Aker
ulong cleanp in client apps
95
static uint32_t opt_max_allowed_packet, opt_net_buffer_length;
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
96
static DRIZZLE drizzle_connection, *drizzle= 0;
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
97
static string insert_pat;
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
98
static char  *opt_password= NULL, *current_user= NULL,
99
             *current_host= NULL, *path= NULL, *fields_terminated= NULL,
100
             *lines_terminated= NULL, *enclosed= NULL, *opt_enclosed= NULL,
101
             *escaped= NULL,
102
             *where= NULL, *order_by= NULL,
103
             *opt_compatible_mode_str= NULL,
754.1.4 by Monty Taylor
Merged from Mark.
104
             *err_ptr= NULL;
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
105
static char **defaults_argv= NULL;
1 by brian
clean slate
106
static char compatible_mode_normal_str[255];
107
/* Server supports character_set_results session variable? */
163 by Brian Aker
Merge Monty's code.
108
static bool server_supports_switching_charsets= true;
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
/**
964
  Switch charset for results to some specified charset.  If the server does not
965
  support character_set_results variable, nothing can be done here.  As for
966
  whether something should be done here, future new callers of this function
967
  should be aware that the server lacking the facility of switching charsets is
968
  treated as success.
969
970
  @note  If the server lacks support, then nothing is changed and no error
971
         condition is returned.
972
973
  @returns  whether there was an error or not
974
*/
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
975
static int switch_character_set_results(const char *cs_name)
1 by brian
clean slate
976
{
977
  char query_buffer[QUERY_LENGTH];
978
  size_t query_length;
979
980
  /* Server lacks facility.  This is not an error, by arbitrary decision . */
981
  if (!server_supports_switching_charsets)
163 by Brian Aker
Merge Monty's code.
982
    return false;
1 by brian
clean slate
983
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
984
  query_length= snprintf(query_buffer,
985
                         sizeof (query_buffer),
986
                         "SET SESSION character_set_results = '%s'",
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
987
                         cs_name);
1 by brian
clean slate
988
206.3.1 by Patrick Galbraith
Most everything working with client rename
989
  return drizzle_real_query(drizzle, query_buffer, query_length);
1 by brian
clean slate
990
}
991
992
/*
993
  Open a new .sql file to dump the table or view into
994
995
  SYNOPSIS
996
    open_sql_file_for_table
997
    name      name of the table or view
998
999
  RETURN VALUES
1000
    0        Failed to open file
1001
    > 0      Handle of the open file
1002
*/
1003
static FILE* open_sql_file_for_table(const char* table)
1004
{
1005
  FILE* res;
1006
  char filename[FN_REFLEN], tmp_path[FN_REFLEN];
461 by Monty Taylor
Removed NullS. bu-bye.
1007
  convert_dirname(tmp_path,path,NULL);
1 by brian
clean slate
1008
  res= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4),
1009
                O_WRONLY, MYF(MY_WME));
1010
  return res;
1011
}
1012
1013
53.2.4 by Monty Taylor
Changes so that client/ builds cleanly with no warnings.
1014
static void free_resources(void)
1 by brian
clean slate
1015
{
1016
  if (md_result_file && md_result_file != stdout)
1017
    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.
1018
  free(opt_password);
1 by brian
clean slate
1019
  if (hash_inited(&ignore_table))
1020
    hash_free(&ignore_table);
1021
  if (defaults_argv)
1022
    free_defaults(defaults_argv);
1023
  my_end(my_end_arg);
1024
}
1025
1026
1027
static void maybe_exit(int error)
1028
{
1029
  if (!first_error)
1030
    first_error= error;
1031
  if (ignore_errors)
1032
    return;
206.3.1 by Patrick Galbraith
Most everything working with client rename
1033
  if (drizzle)
1034
    drizzle_close(drizzle);
1 by brian
clean slate
1035
  free_resources();
1036
  exit(error);
1037
}
1038
1039
1040
/*
1041
  db_connect -- connects to the host and selects DB.
1042
*/
1043
1044
static int connect_to_db(char *host, char *user,char *passwd)
1045
{
1046
  verbose_msg("-- Connecting to %s...\n", host ? host : "localhost");
202.2.4 by Monty Taylor
Merged from Patrick.
1047
  drizzle_create(&drizzle_connection);
1 by brian
clean slate
1048
  if (opt_compress)
461 by Monty Taylor
Removed NullS. bu-bye.
1049
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_COMPRESS,NULL);
206.3.1 by Patrick Galbraith
Most everything working with client rename
1050
  if (!(drizzle= drizzle_connect(&drizzle_connection,host,user,passwd,
1051
                                  NULL,opt_drizzle_port, NULL,
1 by brian
clean slate
1052
                                  0)))
1053
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1054
    DB_error(&drizzle_connection, "when trying to connect");
142.1.2 by Patrick
All DBUG_x removed from client/
1055
    return(1);
1 by brian
clean slate
1056
  }
1057
142.1.2 by Patrick
All DBUG_x removed from client/
1058
  return(0);
1 by brian
clean slate
1059
} /* connect_to_db */
1060
1061
1062
/*
1063
** dbDisconnect -- disconnects from the host.
1064
*/
1065
static void dbDisconnect(char *host)
1066
{
1067
  verbose_msg("-- Disconnecting from %s...\n", host ? host : "localhost");
206.3.1 by Patrick Galbraith
Most everything working with client rename
1068
  drizzle_close(drizzle);
1 by brian
clean slate
1069
} /* dbDisconnect */
1070
1071
1072
static void unescape(FILE *file,char *pos,uint length)
1073
{
1074
  char *tmp;
142.1.2 by Patrick
All DBUG_x removed from client/
1075
656.1.20 by Monty Taylor
Removed my_strdup, my_malloc, my_realloc from client/
1076
  if (!(tmp=(char*) malloc(length*2+1)))
206.3.1 by Patrick Galbraith
Most everything working with client rename
1077
    die(EX_DRIZZLEERR, "Couldn't allocate memory");
1 by brian
clean slate
1078
383.1.38 by Monty Taylor
Reworked drizzle_escape_string.
1079
  drizzle_escape_string(tmp, pos, length);
1 by brian
clean slate
1080
  fputc('\'', file);
1081
  fputs(tmp, file);
1082
  fputc('\'', file);
1083
  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.
1084
  free(tmp);
142.1.2 by Patrick
All DBUG_x removed from client/
1085
  return;
1 by brian
clean slate
1086
} /* unescape */
1087
1088
143 by Brian Aker
Bool cleanup.
1089
static bool test_if_special_chars(const char *str)
1 by brian
clean slate
1090
{
1091
  for ( ; *str ; str++)
1092
    if (!my_isvar(charset_info,*str) && *str != '$')
1093
      return 1;
1094
  return 0;
1095
} /* test_if_special_chars */
1096
1097
1098
1099
/*
1100
  quote_name(name, buff, force)
1101
1102
  Quotes char string, taking into account compatible mode
1103
1104
  Args
1105
1106
  name                 Unquoted string containing that which will be quoted
1107
  buff                 The buffer that contains the quoted value, also returned
1108
  force                Flag to make it ignore 'test_if_special_chars'
1109
1110
  Returns
1111
1112
  buff                 quoted string
1113
1114
*/
143 by Brian Aker
Bool cleanup.
1115
static char *quote_name(const char *name, char *buff, bool force)
1 by brian
clean slate
1116
{
1117
  char *to= buff;
1118
  char qtype= (opt_compatible_mode & MASK_ANSI_QUOTES) ? '\"' : '`';
1119
1120
  if (!force && !opt_quoted && !test_if_special_chars(name))
1121
    return (char*) name;
1122
  *to++= qtype;
1123
  while (*name)
1124
  {
1125
    if (*name == qtype)
1126
      *to++= qtype;
1127
    *to++= *name++;
1128
  }
1129
  to[0]= qtype;
1130
  to[1]= 0;
1131
  return buff;
1132
} /* quote_name */
1133
1134
1135
/*
1136
  Quote a table name so it can be used in "SHOW TABLES LIKE <tabname>"
1137
1138
  SYNOPSIS
1139
    quote_for_like()
1140
    name     name of the table
1141
    buff     quoted name of the table
1142
1143
  DESCRIPTION
1144
    Quote \, _, ' and % characters
1145
206.3.1 by Patrick Galbraith
Most everything working with client rename
1146
    Note: Because DRIZZLE uses the C escape syntax in strings
1 by brian
clean slate
1147
    (for example, '\n' to represent newline), you must double
1148
    any '\' that you use in your LIKE  strings. For example, to
1149
    search for '\n', specify it as '\\n'. To search for '\', specify
1150
    it as '\\\\' (the backslashes are stripped once by the parser
1151
    and another time when the pattern match is done, leaving a
1152
    single backslash to be matched).
1153
1154
    Example: "t\1" => "t\\\\1"
1155
1156
*/
1157
static char *quote_for_like(const char *name, char *buff)
1158
{
1159
  char *to= buff;
1160
  *to++= '\'';
1161
  while (*name)
1162
  {
1163
    if (*name == '\\')
1164
    {
1165
      *to++='\\';
1166
      *to++='\\';
1167
      *to++='\\';
1168
    }
1169
    else if (*name == '\'' || *name == '_'  || *name == '%')
1170
      *to++= '\\';
1171
    *to++= *name++;
1172
  }
1173
  to[0]= '\'';
1174
  to[1]= 0;
1175
  return buff;
1176
}
1177
1178
1179
/*
1180
  Quote and print a string.
1181
1182
  SYNOPSIS
1183
    print_quoted_xml()
1184
    xml_file    - output file
1185
    str         - string to print
1186
    len         - its length
1187
1188
  DESCRIPTION
1189
    Quote '<' '>' '&' '\"' chars and print a string to the xml_file.
1190
*/
1191
288 by Brian Aker
ulong cleanp in client apps
1192
static void print_quoted_xml(FILE *xml_file, const char *str, uint32_t len)
1 by brian
clean slate
1193
{
1194
  const char *end;
1195
1196
  for (end= str + len; str != end; str++)
1197
  {
1198
    switch (*str) {
1199
    case '<':
1200
      fputs("&lt;", xml_file);
1201
      break;
1202
    case '>':
1203
      fputs("&gt;", xml_file);
1204
      break;
1205
    case '&':
1206
      fputs("&amp;", xml_file);
1207
      break;
1208
    case '\"':
1209
      fputs("&quot;", xml_file);
1210
      break;
1211
    default:
1212
      fputc(*str, xml_file);
1213
      break;
1214
    }
1215
  }
1216
  check_io(xml_file);
1217
}
1218
1219
1220
/*
1221
  Print xml tag. Optionally add attribute(s).
1222
1223
  SYNOPSIS
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1224
    print_xml_tag(xml_file, sbeg, send, tag_name, first_attribute_name,
461 by Monty Taylor
Removed NullS. bu-bye.
1225
                    ..., attribute_name_n, attribute_value_n, NULL)
1 by brian
clean slate
1226
    xml_file              - output file
1227
    sbeg                  - line beginning
1228
    line_end              - line ending
1229
    tag_name              - XML tag name.
1230
    first_attribute_name  - tag and first attribute
1231
    first_attribute_value - (Implied) value of first attribute
1232
    attribute_name_n      - attribute n
1233
    attribute_value_n     - value of attribute n
1234
1235
  DESCRIPTION
1236
    Print XML tag with any number of attribute="value" pairs to the xml_file.
1237
1238
    Format is:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1239
      sbeg<tag_name first_attribute_name="first_attribute_value" ...
1 by brian
clean slate
1240
      attribute_name_n="attribute_value_n">send
1241
  NOTE
1242
    Additional arguments must be present in attribute/value pairs.
1243
    The last argument should be the null character pointer.
1244
    All attribute_value arguments MUST be NULL terminated strings.
1245
    All attribute_value arguments will be quoted before output.
1246
*/
1247
1248
static void print_xml_tag(FILE * xml_file, const char* sbeg,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1249
                          const char* line_end,
1250
                          const char* tag_name,
1 by brian
clean slate
1251
                          const char* first_attribute_name, ...)
1252
{
1253
  va_list arg_list;
1254
  const char *attribute_name, *attribute_value;
1255
1256
  fputs(sbeg, xml_file);
1257
  fputc('<', xml_file);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1258
  fputs(tag_name, xml_file);
1 by brian
clean slate
1259
1260
  va_start(arg_list, first_attribute_name);
1261
  attribute_name= first_attribute_name;
461 by Monty Taylor
Removed NullS. bu-bye.
1262
  while (attribute_name != NULL)
1 by brian
clean slate
1263
  {
1264
    attribute_value= va_arg(arg_list, char *);
461 by Monty Taylor
Removed NullS. bu-bye.
1265
    assert(attribute_value != NULL);
1 by brian
clean slate
1266
1267
    fputc(' ', xml_file);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1268
    fputs(attribute_name, xml_file);
1 by brian
clean slate
1269
    fputc('\"', xml_file);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1270
1 by brian
clean slate
1271
    print_quoted_xml(xml_file, attribute_value, strlen(attribute_value));
1272
    fputc('\"', xml_file);
1273
1274
    attribute_name= va_arg(arg_list, char *);
1275
  }
1276
  va_end(arg_list);
1277
1278
  fputc('>', xml_file);
1279
  fputs(line_end, xml_file);
1280
  check_io(xml_file);
1281
}
1282
1283
1284
/*
1285
  Print xml tag with for a field that is null
1286
1287
  SYNOPSIS
1288
    print_xml_null_tag()
1289
    xml_file    - output file
1290
    sbeg        - line beginning
1291
    stag_atr    - tag and attribute
1292
    sval        - value of attribute
1293
    line_end        - line ending
1294
1295
  DESCRIPTION
1296
    Print tag with one attribute to the xml_file. Format is:
1297
      <stag_atr="sval" xsi:nil="true"/>
1298
  NOTE
1299
    sval MUST be a NULL terminated string.
1300
    sval string will be qouted before output.
1301
*/
1302
1303
static void print_xml_null_tag(FILE * xml_file, const char* sbeg,
1304
                               const char* stag_atr, const char* sval,
1305
                               const char* line_end)
1306
{
1307
  fputs(sbeg, xml_file);
1308
  fputs("<", xml_file);
1309
  fputs(stag_atr, xml_file);
1310
  fputs("\"", xml_file);
1311
  print_quoted_xml(xml_file, sval, strlen(sval));
1312
  fputs("\" xsi:nil=\"true\" />", xml_file);
1313
  fputs(line_end, xml_file);
1314
  check_io(xml_file);
1315
}
1316
1317
1318
/*
1319
  Print xml tag with many attributes.
1320
1321
  SYNOPSIS
1322
    print_xml_row()
1323
    xml_file    - output file
1324
    row_name    - xml tag name
1325
    tableRes    - query result
1326
    row         - result row
1327
1328
  DESCRIPTION
1329
    Print tag with many attribute to the xml_file. Format is:
1330
      \t\t<row_name Atr1="Val1" Atr2="Val2"... />
1331
  NOTE
1332
    All atributes and values will be quoted before output.
1333
*/
1334
1335
static void print_xml_row(FILE *xml_file, const char *row_name,
206.3.1 by Patrick Galbraith
Most everything working with client rename
1336
                          DRIZZLE_RES *tableRes, DRIZZLE_ROW *row)
1 by brian
clean slate
1337
{
1338
  uint i;
206.3.1 by Patrick Galbraith
Most everything working with client rename
1339
  DRIZZLE_FIELD *field;
1340
  uint32_t *lengths= drizzle_fetch_lengths(tableRes);
1 by brian
clean slate
1341
1342
  fprintf(xml_file, "\t\t<%s", row_name);
1343
  check_io(xml_file);
206.3.1 by Patrick Galbraith
Most everything working with client rename
1344
  drizzle_field_seek(tableRes, 0);
1345
  for (i= 0; (field= drizzle_fetch_field(tableRes)); i++)
1 by brian
clean slate
1346
  {
1347
    if ((*row)[i])
1348
    {
1349
      fputc(' ', xml_file);
1350
      print_quoted_xml(xml_file, field->name, field->name_length);
1351
      fputs("=\"", xml_file);
1352
      print_quoted_xml(xml_file, (*row)[i], lengths[i]);
1353
      fputc('"', xml_file);
1354
      check_io(xml_file);
1355
    }
1356
  }
1357
  fputs(" />\n", xml_file);
1358
  check_io(xml_file);
1359
}
1360
1361
1362
/*
1363
  Print hex value for blob data.
1364
1365
  SYNOPSIS
1366
    print_blob_as_hex()
1367
    output_file         - output file
1368
    str                 - string to print
1369
    len                 - its length
1370
1371
  DESCRIPTION
1372
    Print hex value for blob data.
1373
*/
1374
288 by Brian Aker
ulong cleanp in client apps
1375
static void print_blob_as_hex(FILE *output_file, const char *str, uint32_t len)
1 by brian
clean slate
1376
{
1377
    /* sakaik got the idea to to provide blob's in hex notation. */
1378
    const char *ptr= str, *end= ptr + len;
1379
    for (; ptr < end ; ptr++)
481 by Brian Aker
Remove all of uchar.
1380
      fprintf(output_file, "%02X", *((unsigned char *)ptr));
1 by brian
clean slate
1381
    check_io(output_file);
1382
}
1383
1384
/*
1385
  get_table_structure -- retrievs database structure, prints out corresponding
1386
  CREATE statement and fills out insert_pat if the table is the type we will
1387
  be dumping.
1388
1389
  ARGS
1390
    table       - table name
1391
    db          - db name
1392
    table_type  - table type, e.g. "MyISAM" or "InnoDB", but also "VIEW"
1393
    ignore_flag - what we must particularly ignore - see IGNORE_ defines above
1394
1395
  RETURN
1396
    number of fields in table, 0 if error
1397
*/
1398
1399
static uint get_table_structure(char *table, char *db, char *table_type,
1400
                                char *ignore_flag)
1401
{
143 by Brian Aker
Bool cleanup.
1402
  bool    init=0, delayed, write_data, complete_insert;
157 by Brian Aker
Second pass cleanup on removal of my_uint types
1403
  uint64_t num_fields;
1 by brian
clean slate
1404
  char       *result_table, *opt_quoted_table;
1405
  const char *insert_option;
1406
  char	     name_buff[NAME_LEN+3],table_buff[NAME_LEN*2+3];
1407
  char       table_buff2[NAME_LEN*2+3], query_buff[QUERY_LENGTH];
1408
  FILE       *sql_file= md_result_file;
1409
  int        len;
206.3.1 by Patrick Galbraith
Most everything working with client rename
1410
  DRIZZLE_RES  *result;
1411
  DRIZZLE_ROW  row;
1 by brian
clean slate
1412
1413
  *ignore_flag= check_if_ignore_table(table, table_type);
1414
1415
  delayed= opt_delayed;
1416
  if (delayed && (*ignore_flag & IGNORE_INSERT_DELAYED))
1417
  {
1418
    delayed= 0;
1419
    verbose_msg("-- Warning: Unable to use delayed inserts for table '%s' "
1420
                "because it's of type %s\n", table, table_type);
1421
  }
1422
1423
  complete_insert= 0;
1424
  if ((write_data= !(*ignore_flag & IGNORE_DATA)))
1425
  {
1426
    complete_insert= opt_complete_insert;
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
1427
    insert_pat.clear();
1 by brian
clean slate
1428
  }
1429
1430
  insert_option= ((delayed && opt_ignore) ? " DELAYED IGNORE " :
1431
                  delayed ? " DELAYED " : opt_ignore ? " IGNORE " : "");
1432
1433
  verbose_msg("-- Retrieving table structure for table %s...\n", table);
1434
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1435
  len= snprintf(query_buff, sizeof(query_buff),
1436
                "SET OPTION SQL_QUOTE_SHOW_CREATE=%d",
1437
                (opt_quoted || opt_keywords));
1 by brian
clean slate
1438
1439
  result_table=     quote_name(table, table_buff, 1);
1440
  opt_quoted_table= quote_name(table, table_buff2, 0);
1441
1442
  if (opt_order_by_primary)
1443
  {
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.
1444
    free(order_by);
1 by brian
clean slate
1445
    order_by= primary_key_fields(result_table);
1446
  }
1447
206.3.1 by Patrick Galbraith
Most everything working with client rename
1448
  if (!opt_xml && !drizzle_query_with_error_report(drizzle, 0, query_buff))
1 by brian
clean slate
1449
  {
1450
    /* using SHOW CREATE statement */
1451
    if (!opt_no_create_info)
1452
    {
1453
      /* Make an sql-file, if path was given iow. option -T was given */
1454
      char buff[20+FN_REFLEN];
236.3.5 by Andrey Hristov
Constify libdrizzle functions
1455
      const DRIZZLE_FIELD *field;
1 by brian
clean slate
1456
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1457
      snprintf(buff, sizeof(buff), "show create table %s", result_table);
1 by brian
clean slate
1458
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
1459
      if (switch_character_set_results("binary") ||
206.3.1 by Patrick Galbraith
Most everything working with client rename
1460
          drizzle_query_with_error_report(drizzle, &result, buff) ||
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
1461
          switch_character_set_results(default_charset))
142.1.2 by Patrick
All DBUG_x removed from client/
1462
        return(0);
1 by brian
clean slate
1463
1464
      if (path)
1465
      {
1466
        if (!(sql_file= open_sql_file_for_table(table)))
142.1.2 by Patrick
All DBUG_x removed from client/
1467
          return(0);
1 by brian
clean slate
1468
1469
        write_header(sql_file, db);
1470
      }
1471
      if (!opt_xml && opt_comments)
1472
      {
1473
        fprintf(sql_file, "\n--\n-- Table structure for table %s\n--\n\n",
1474
                result_table);
1475
        check_io(sql_file);
1476
      }
1477
      if (opt_drop)
1478
      {
1479
      /*
739 by Brian Aker
Removed dead table map code in replication. Also refactored drizzledump a
1480
        Even if the "table" is a view, we do a DROP TABLE here.
1 by brian
clean slate
1481
       */
1482
        fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n",
1483
                opt_quoted_table);
1484
        check_io(sql_file);
1485
      }
1486
206.3.1 by Patrick Galbraith
Most everything working with client rename
1487
      field= drizzle_fetch_field_direct(result, 0);
1 by brian
clean slate
1488
206.3.1 by Patrick Galbraith
Most everything working with client rename
1489
      row= drizzle_fetch_row(result);
1 by brian
clean slate
1490
377.1.4 by Brian Aker
Big, fat, UTF-8 patch. This fixes some of the oddities around only one
1491
      fprintf(sql_file, "%s;\n", row[1]);
1 by brian
clean slate
1492
1493
      check_io(sql_file);
206.3.1 by Patrick Galbraith
Most everything working with client rename
1494
      drizzle_free_result(result);
1 by brian
clean slate
1495
    }
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1496
    snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1497
             result_table);
206.3.1 by Patrick Galbraith
Most everything working with client rename
1498
    if (drizzle_query_with_error_report(drizzle, &result, query_buff))
1 by brian
clean slate
1499
    {
1500
      if (path)
1501
        my_fclose(sql_file, MYF(MY_WME));
142.1.2 by Patrick
All DBUG_x removed from client/
1502
      return(0);
1 by brian
clean slate
1503
    }
1504
1505
    /*
1506
      If write_data is true, then we build up insert statements for
1507
      the table's data. Note: in subsequent lines of code, this test
1508
      will have to be performed each time we are appending to
1509
      insert_pat.
1510
    */
1511
    if (write_data)
1512
    {
1513
      if (opt_replace_into)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1514
        insert_pat.append("REPLACE ");
1 by brian
clean slate
1515
      else
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1516
        insert_pat.append("INSERT ");
1517
      insert_pat.append(insert_option);
1518
      insert_pat.append("INTO ");
1519
      insert_pat.append(opt_quoted_table);
1 by brian
clean slate
1520
      if (complete_insert)
1521
      {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1522
        insert_pat.append(" (");
1 by brian
clean slate
1523
      }
1524
      else
1525
      {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1526
        insert_pat.append(" VALUES ");
1 by brian
clean slate
1527
        if (!extended_insert)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1528
          insert_pat.append("(");
1 by brian
clean slate
1529
      }
1530
    }
1531
206.3.1 by Patrick Galbraith
Most everything working with client rename
1532
    while ((row= drizzle_fetch_row(result)))
1 by brian
clean slate
1533
    {
1534
      if (complete_insert)
1535
      {
1536
        if (init)
1537
        {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1538
          insert_pat.append(", ");
1 by brian
clean slate
1539
        }
1540
        init=1;
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1541
        insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1 by brian
clean slate
1542
      }
1543
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
1544
    num_fields= drizzle_num_rows(result);
1545
    drizzle_free_result(result);
1 by brian
clean slate
1546
  }
1547
  else
1548
  {
1549
    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
1550
                my_progname, drizzle_error(drizzle));
1 by brian
clean slate
1551
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1552
    snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1553
             result_table);
206.3.1 by Patrick Galbraith
Most everything working with client rename
1554
    if (drizzle_query_with_error_report(drizzle, &result, query_buff))
142.1.2 by Patrick
All DBUG_x removed from client/
1555
      return(0);
1 by brian
clean slate
1556
1557
    /* Make an sql-file, if path was given iow. option -T was given */
1558
    if (!opt_no_create_info)
1559
    {
1560
      if (path)
1561
      {
1562
        if (!(sql_file= open_sql_file_for_table(table)))
142.1.2 by Patrick
All DBUG_x removed from client/
1563
          return(0);
1 by brian
clean slate
1564
        write_header(sql_file, db);
1565
      }
1566
      if (!opt_xml && opt_comments)
1567
        fprintf(sql_file, "\n--\n-- Table structure for table %s\n--\n\n",
1568
                result_table);
1569
      if (opt_drop)
1570
        fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n", result_table);
1571
      if (!opt_xml)
1572
        fprintf(sql_file, "CREATE TABLE %s (\n", result_table);
1573
      else
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1574
        print_xml_tag(sql_file, "\t", "\n", "table_structure", "name=", table,
461 by Monty Taylor
Removed NullS. bu-bye.
1575
                NULL);
1 by brian
clean slate
1576
      check_io(sql_file);
1577
    }
1578
1579
    if (write_data)
1580
    {
1581
      if (opt_replace_into)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1582
        insert_pat.append("REPLACE ");
1 by brian
clean slate
1583
      else
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1584
        insert_pat.append("INSERT ");
1585
      insert_pat.append(insert_option);
1586
      insert_pat.append("INTO ");
1587
      insert_pat.append(result_table);
1 by brian
clean slate
1588
      if (complete_insert)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1589
        insert_pat.append(" (");
1 by brian
clean slate
1590
      else
1591
      {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1592
        insert_pat.append(" VALUES ");
1 by brian
clean slate
1593
        if (!extended_insert)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1594
          insert_pat.append("(");
1 by brian
clean slate
1595
      }
1596
    }
1597
206.3.1 by Patrick Galbraith
Most everything working with client rename
1598
    while ((row= drizzle_fetch_row(result)))
1 by brian
clean slate
1599
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1600
      uint32_t *lengths= drizzle_fetch_lengths(result);
1 by brian
clean slate
1601
      if (init)
1602
      {
1603
        if (!opt_xml && !opt_no_create_info)
1604
        {
1605
          fputs(",\n",sql_file);
1606
          check_io(sql_file);
1607
        }
1608
        if (complete_insert)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1609
          insert_pat.append(", ");
1 by brian
clean slate
1610
      }
1611
      init=1;
1612
      if (complete_insert)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1613
        insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1 by brian
clean slate
1614
      if (!opt_no_create_info)
1615
      {
1616
        if (opt_xml)
1617
        {
1618
          print_xml_row(sql_file, "field", result, &row);
1619
          continue;
1620
        }
1621
1622
        if (opt_keywords)
1623
          fprintf(sql_file, "  %s.%s %s", result_table,
1624
                  quote_name(row[SHOW_FIELDNAME],name_buff, 0),
1625
                  row[SHOW_TYPE]);
1626
        else
1627
          fprintf(sql_file, "  %s %s", quote_name(row[SHOW_FIELDNAME],
1628
                                                  name_buff, 0),
1629
                  row[SHOW_TYPE]);
1630
        if (row[SHOW_DEFAULT])
1631
        {
1632
          fputs(" DEFAULT ", sql_file);
1633
          unescape(sql_file, row[SHOW_DEFAULT], lengths[SHOW_DEFAULT]);
1634
        }
1635
        if (!row[SHOW_NULL][0])
1636
          fputs(" NOT NULL", sql_file);
1637
        if (row[SHOW_EXTRA][0])
1638
          fprintf(sql_file, " %s",row[SHOW_EXTRA]);
1639
        check_io(sql_file);
1640
      }
1641
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
1642
    num_fields= drizzle_num_rows(result);
1643
    drizzle_free_result(result);
1 by brian
clean slate
1644
    if (!opt_no_create_info)
1645
    {
1646
      /* Make an sql-file, if path was given iow. option -T was given */
1647
      char buff[20+FN_REFLEN];
1648
      uint keynr,primary_key;
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1649
      snprintf(buff, sizeof(buff), "show keys from %s", result_table);
206.3.1 by Patrick Galbraith
Most everything working with client rename
1650
      if (drizzle_query_with_error_report(drizzle, &result, buff))
1 by brian
clean slate
1651
      {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1652
        if (drizzle_errno(drizzle) == ER_WRONG_OBJECT)
1 by brian
clean slate
1653
        {
1654
          /* it is VIEW */
1655
          fputs("\t\t<options Comment=\"view\" />\n", sql_file);
1656
          goto continue_xml;
1657
        }
1658
        fprintf(stderr, "%s: Can't get keys for table %s (%s)\n",
206.3.1 by Patrick Galbraith
Most everything working with client rename
1659
                my_progname, result_table, drizzle_error(drizzle));
1 by brian
clean slate
1660
        if (path)
1661
          my_fclose(sql_file, MYF(MY_WME));
142.1.2 by Patrick
All DBUG_x removed from client/
1662
        return(0);
1 by brian
clean slate
1663
      }
1664
1665
      /* Find first which key is primary key */
1666
      keynr=0;
1667
      primary_key=INT_MAX;
206.3.1 by Patrick Galbraith
Most everything working with client rename
1668
      while ((row= drizzle_fetch_row(result)))
1 by brian
clean slate
1669
      {
1670
        if (atoi(row[3]) == 1)
1671
        {
1672
          keynr++;
1673
#ifdef FORCE_PRIMARY_KEY
1674
          if (atoi(row[1]) == 0 && primary_key == INT_MAX)
1675
            primary_key=keynr;
1676
#endif
1677
          if (!strcmp(row[2],"PRIMARY"))
1678
          {
1679
            primary_key=keynr;
1680
            break;
1681
          }
1682
        }
1683
      }
206.3.1 by Patrick Galbraith
Most everything working with client rename
1684
      drizzle_data_seek(result,0);
1 by brian
clean slate
1685
      keynr=0;
206.3.1 by Patrick Galbraith
Most everything working with client rename
1686
      while ((row= drizzle_fetch_row(result)))
1 by brian
clean slate
1687
      {
1688
        if (opt_xml)
1689
        {
1690
          print_xml_row(sql_file, "key", result, &row);
1691
          continue;
1692
        }
1693
1694
        if (atoi(row[3]) == 1)
1695
        {
1696
          if (keynr++)
1697
            putc(')', sql_file);
1698
          if (atoi(row[1]))       /* Test if duplicate key */
1699
            /* Duplicate allowed */
1700
            fprintf(sql_file, ",\n  KEY %s (",quote_name(row[2],name_buff,0));
1701
          else if (keynr == primary_key)
1702
            fputs(",\n  PRIMARY KEY (",sql_file); /* First UNIQUE is primary */
1703
          else
1704
            fprintf(sql_file, ",\n  UNIQUE %s (",quote_name(row[2],name_buff,
1705
                                                            0));
1706
        }
1707
        else
1708
          putc(',', sql_file);
1709
        fputs(quote_name(row[4], name_buff, 0), sql_file);
1710
        if (row[7])
1711
          fprintf(sql_file, " (%s)",row[7]);      /* Sub key */
1712
        check_io(sql_file);
1713
      }
206.3.1 by Patrick Galbraith
Most everything working with client rename
1714
      drizzle_free_result(result);
1 by brian
clean slate
1715
      if (!opt_xml)
1716
      {
1717
        if (keynr)
1718
          putc(')', sql_file);
1719
        fputs("\n)",sql_file);
1720
        check_io(sql_file);
1721
      }
1722
206.3.1 by Patrick Galbraith
Most everything working with client rename
1723
      /* Get DRIZZLE specific create options */
1 by brian
clean slate
1724
      if (create_options)
1725
      {
1726
        char show_name_buff[NAME_LEN*2+2+24];
1727
1728
        /* Check memory for quote_for_like() */
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1729
        snprintf(buff, sizeof(buff), "show table status like %s",
1730
                 quote_for_like(table, show_name_buff));
1 by brian
clean slate
1731
206.3.1 by Patrick Galbraith
Most everything working with client rename
1732
        if (drizzle_query_with_error_report(drizzle, &result, buff))
1 by brian
clean slate
1733
        {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1734
          if (drizzle_errno(drizzle) != ER_PARSE_ERROR)
1735
          {                                     /* If old DRIZZLE version */
1 by brian
clean slate
1736
            verbose_msg("-- Warning: Couldn't get status information for " \
206.3.1 by Patrick Galbraith
Most everything working with client rename
1737
                        "table %s (%s)\n", result_table,drizzle_error(drizzle));
1 by brian
clean slate
1738
          }
1739
        }
206.3.1 by Patrick Galbraith
Most everything working with client rename
1740
        else if (!(row= drizzle_fetch_row(result)))
1 by brian
clean slate
1741
        {
1742
          fprintf(stderr,
1743
                  "Error: Couldn't read status information for table %s (%s)\n",
206.3.1 by Patrick Galbraith
Most everything working with client rename
1744
                  result_table,drizzle_error(drizzle));
1 by brian
clean slate
1745
        }
1746
        else
1747
        {
1748
          if (opt_xml)
1749
            print_xml_row(sql_file, "options", result, &row);
1750
          else
1751
          {
1752
            fputs("/*!",sql_file);
1753
            print_value(sql_file,result,row,"engine=","Engine",0);
1754
            print_value(sql_file,result,row,"","Create_options",0);
1755
            print_value(sql_file,result,row,"comment=","Comment",1);
1756
            fputs(" */",sql_file);
1757
            check_io(sql_file);
1758
          }
1759
        }
206.3.1 by Patrick Galbraith
Most everything working with client rename
1760
        drizzle_free_result(result);              /* Is always safe to free */
1 by brian
clean slate
1761
      }
1762
continue_xml:
1763
      if (!opt_xml)
1764
        fputs(";\n", sql_file);
1765
      else
1766
        fputs("\t</table_structure>\n", sql_file);
1767
      check_io(sql_file);
1768
    }
1769
  }
1770
  if (complete_insert)
1771
  {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1772
    insert_pat.append(") VALUES ");
1 by brian
clean slate
1773
    if (!extended_insert)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1774
      insert_pat.append("(");
1 by brian
clean slate
1775
  }
1776
  if (sql_file != md_result_file)
1777
  {
1778
    fputs("\n", sql_file);
1779
    write_footer(sql_file);
1780
    my_fclose(sql_file, MYF(MY_WME));
1781
  }
142.1.2 by Patrick
All DBUG_x removed from client/
1782
  return((uint) num_fields);
1 by brian
clean slate
1783
} /* get_table_structure */
1784
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1785
static void add_load_option(string &str, const char *option,
1786
                            const char *option_value)
1 by brian
clean slate
1787
{
1788
  if (!option_value)
1789
  {
1790
    /* Null value means we don't add this option. */
1791
    return;
1792
  }
1793
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1794
  str.append(option);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1795
1 by brian
clean slate
1796
  if (strncmp(option_value, "0x", sizeof("0x")-1) == 0)
1797
  {
1798
    /* It's a hex constant, don't escape */
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1799
    str.append(option_value);
1 by brian
clean slate
1800
  }
1801
  else
1802
  {
1803
    /* char constant; escape */
1804
    field_escape(str, option_value);
1805
  }
1806
}
1807
1808
1809
/*
1810
  Allow the user to specify field terminator strings like:
1811
  "'", "\", "\\" (escaped backslash), "\t" (tab), "\n" (newline)
1812
  This is done by doubling ' and add a end -\ if needed to avoid
1813
  syntax errors from the SQL parser.
1814
*/
1815
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1816
static void field_escape(string &in, const char *from)
1 by brian
clean slate
1817
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1818
  uint end_backslashes= 0;
1 by brian
clean slate
1819
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1820
  in.append("'");
1 by brian
clean slate
1821
1822
  while (*from)
1823
  {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1824
    in.append(from, 1);
1 by brian
clean slate
1825
1826
    if (*from == '\\')
1827
      end_backslashes^=1;    /* find odd number of backslashes */
1828
    else
1829
    {
1830
      if (*from == '\'' && !end_backslashes)
1831
      {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1832
        /* We want a duplicate of "'" for DRIZZLE */
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1833
        in.append("\'");
1 by brian
clean slate
1834
      }
1835
      end_backslashes=0;
1836
    }
1837
    from++;
1838
  }
1839
  /* Add missing backslashes if user has specified odd number of backs.*/
1840
  if (end_backslashes)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1841
    in.append("\\");
1842
1843
  in.append("'");
1 by brian
clean slate
1844
}
1845
1846
1847
1848
/*
1849
1850
 SYNOPSIS
1851
  dump_table()
1852
1853
  dump_table saves database contents as a series of INSERT statements.
1854
1855
  ARGS
1856
   table - table name
1857
   db    - db name
1858
1859
   RETURNS
1860
    void
1861
*/
1862
1863
1864
static void dump_table(char *table, char *db)
1865
{
1866
  char ignore_flag;
1867
  char buf[200], table_buff[NAME_LEN+3];
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1868
  string query_string;
1 by brian
clean slate
1869
  char table_type[NAME_LEN];
1870
  char *result_table, table_buff2[NAME_LEN*2+3], *opt_quoted_table;
1871
  int error= 0;
288 by Brian Aker
ulong cleanp in client apps
1872
  uint32_t         rownr, row_break, total_length, init_length;
1 by brian
clean slate
1873
  uint num_fields;
206.3.1 by Patrick Galbraith
Most everything working with client rename
1874
  DRIZZLE_RES     *res;
1875
  DRIZZLE_FIELD   *field;
1876
  DRIZZLE_ROW     row;
142.1.2 by Patrick
All DBUG_x removed from client/
1877
1 by brian
clean slate
1878
1879
  /*
1880
    Make sure you get the create table info before the following check for
1881
    --no-data flag below. Otherwise, the create table info won't be printed.
1882
  */
1883
  num_fields= get_table_structure(table, db, table_type, &ignore_flag);
1884
1885
  /* Check --no-data flag */
1886
  if (opt_no_data)
1887
  {
1888
    verbose_msg("-- Skipping dump data for table '%s', --no-data was used\n",
1889
                table);
142.1.2 by Patrick
All DBUG_x removed from client/
1890
    return;
1 by brian
clean slate
1891
  }
1892
1893
  /*
1894
    If the table type is a merge table or any type that has to be
1895
     _completely_ ignored and no data dumped
1896
  */
1897
  if (ignore_flag & IGNORE_DATA)
1898
  {
1899
    verbose_msg("-- Warning: Skipping data for table '%s' because " \
1900
                "it's of type %s\n", table, table_type);
142.1.2 by Patrick
All DBUG_x removed from client/
1901
    return;
1 by brian
clean slate
1902
  }
1903
  /* Check that there are any fields in the table */
1904
  if (num_fields == 0)
1905
  {
1906
    verbose_msg("-- Skipping dump data for table '%s', it has no fields\n",
1907
                table);
142.1.2 by Patrick
All DBUG_x removed from client/
1908
    return;
1 by brian
clean slate
1909
  }
1910
1911
  result_table= quote_name(table,table_buff, 1);
1912
  opt_quoted_table= quote_name(table, table_buff2, 0);
1913
1914
  verbose_msg("-- Sending SELECT query...\n");
1915
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1916
  query_string.clear();
1917
  query_string.reserve(1024);
1 by brian
clean slate
1918
1919
  if (path)
1920
  {
1921
    char filename[FN_REFLEN], tmp_path[FN_REFLEN];
1922
1923
    /*
1924
      Convert the path to native os format
1925
      and resolve to the full filepath.
1926
    */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1927
    convert_dirname(tmp_path,path,NULL);
1 by brian
clean slate
1928
    my_load_path(tmp_path, tmp_path, NULL);
1929
    fn_format(filename, table, tmp_path, ".txt", MYF(MY_UNPACK_FILENAME));
1930
1931
    /* Must delete the file that 'INTO OUTFILE' will write to */
1932
    my_delete(filename, MYF(0));
1933
1934
    /* convert to a unix path name to stick into the query */
1935
    to_unix_path(filename);
1936
1937
    /* now build the query string */
1938
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1939
    query_string.append( "SELECT * INTO OUTFILE '");
1940
    query_string.append( filename);
1941
    query_string.append( "'");
1 by brian
clean slate
1942
1943
    if (fields_terminated || enclosed || opt_enclosed || escaped)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1944
      query_string.append( " FIELDS");
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1945
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1946
    add_load_option(query_string, " TERMINATED BY ", fields_terminated);
1947
    add_load_option(query_string, " ENCLOSED BY ", enclosed);
1948
    add_load_option(query_string, " OPTIONALLY ENCLOSED BY ", opt_enclosed);
1949
    add_load_option(query_string, " ESCAPED BY ", escaped);
1950
    add_load_option(query_string, " LINES TERMINATED BY ", lines_terminated);
1 by brian
clean slate
1951
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1952
    query_string.append( " FROM ");
1953
    query_string.append( result_table);
1 by brian
clean slate
1954
1955
    if (where)
1956
    {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1957
      query_string.append( " WHERE ");
1958
      query_string.append( where);
1 by brian
clean slate
1959
    }
1960
1961
    if (order_by)
1962
    {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1963
      query_string.append( " ORDER BY ");
1964
      query_string.append( order_by);
1 by brian
clean slate
1965
    }
1966
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1967
    if (drizzle_real_query(drizzle, query_string.c_str(), query_string.length()))
1 by brian
clean slate
1968
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1969
      DB_error(drizzle, "when executing 'SELECT INTO OUTFILE'");
142.1.2 by Patrick
All DBUG_x removed from client/
1970
      return;
1 by brian
clean slate
1971
    }
1972
  }
1973
  else
1974
  {
1975
    if (!opt_xml && opt_comments)
1976
    {
1977
      fprintf(md_result_file,"\n--\n-- Dumping data for table %s\n--\n",
1978
              result_table);
1979
      check_io(md_result_file);
1980
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1981
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1982
    query_string.append( "SELECT * FROM ");
1983
    query_string.append( result_table);
1 by brian
clean slate
1984
1985
    if (where)
1986
    {
1987
      if (!opt_xml && opt_comments)
1988
      {
1989
        fprintf(md_result_file, "-- WHERE:  %s\n", where);
1990
        check_io(md_result_file);
1991
      }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1992
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
1993
      query_string.append( " WHERE ");
1994
      query_string.append( where);
1 by brian
clean slate
1995
    }
1996
    if (order_by)
1997
    {
1998
      if (!opt_xml && opt_comments)
1999
      {
2000
        fprintf(md_result_file, "-- ORDER BY:  %s\n", order_by);
2001
        check_io(md_result_file);
2002
      }
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2003
      query_string.append( " ORDER BY ");
2004
      query_string.append( order_by);
1 by brian
clean slate
2005
    }
2006
2007
    if (!opt_xml && !opt_compact)
2008
    {
2009
      fputs("\n", md_result_file);
2010
      check_io(md_result_file);
2011
    }
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2012
    if (drizzle_query_with_error_report(drizzle, 0, query_string.c_str()))
1 by brian
clean slate
2013
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2014
      DB_error(drizzle, "when retrieving data from server");
1 by brian
clean slate
2015
      goto err;
2016
    }
2017
    if (quick)
206.3.1 by Patrick Galbraith
Most everything working with client rename
2018
      res=drizzle_use_result(drizzle);
1 by brian
clean slate
2019
    else
206.3.1 by Patrick Galbraith
Most everything working with client rename
2020
      res=drizzle_store_result(drizzle);
1 by brian
clean slate
2021
    if (!res)
2022
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2023
      DB_error(drizzle, "when retrieving data from server");
1 by brian
clean slate
2024
      goto err;
2025
    }
2026
2027
    verbose_msg("-- Retrieving rows...\n");
206.3.1 by Patrick Galbraith
Most everything working with client rename
2028
    if (drizzle_num_fields(res) != num_fields)
1 by brian
clean slate
2029
    {
2030
      fprintf(stderr,"%s: Error in field count for table: %s !  Aborting.\n",
2031
              my_progname, result_table);
2032
      error= EX_CONSCHECK;
2033
      goto err;
2034
    }
2035
2036
    if (opt_lock)
2037
    {
2038
      fprintf(md_result_file,"LOCK TABLES %s WRITE;\n", opt_quoted_table);
2039
      check_io(md_result_file);
2040
    }
2041
    /* Moved disable keys to after lock per bug 15977 */
2042
    if (opt_disable_keys)
2043
    {
2044
      fprintf(md_result_file, "/*!40000 ALTER TABLE %s DISABLE KEYS */;\n",
2045
	      opt_quoted_table);
2046
      check_io(md_result_file);
2047
    }
2048
2049
    total_length= opt_net_buffer_length;                /* Force row break */
2050
    row_break=0;
2051
    rownr=0;
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2052
    init_length=(uint) insert_pat.length()+4;
1 by brian
clean slate
2053
    if (opt_xml)
2054
      print_xml_tag(md_result_file, "\t", "\n", "table_data", "name=", table,
461 by Monty Taylor
Removed NullS. bu-bye.
2055
              NULL);
1 by brian
clean slate
2056
    if (opt_autocommit)
2057
    {
2058
      fprintf(md_result_file, "set autocommit=0;\n");
2059
      check_io(md_result_file);
2060
    }
2061
206.3.1 by Patrick Galbraith
Most everything working with client rename
2062
    while ((row= drizzle_fetch_row(res)))
1 by brian
clean slate
2063
    {
2064
      uint i;
206.3.1 by Patrick Galbraith
Most everything working with client rename
2065
      uint32_t *lengths= drizzle_fetch_lengths(res);
1 by brian
clean slate
2066
      rownr++;
2067
      if (!extended_insert && !opt_xml)
2068
      {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2069
        fputs(insert_pat.c_str(),md_result_file);
1 by brian
clean slate
2070
        check_io(md_result_file);
2071
      }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2072
      drizzle_field_seek(res,0);
1 by brian
clean slate
2073
2074
      if (opt_xml)
2075
      {
2076
        fputs("\t<row>\n", md_result_file);
2077
        check_io(md_result_file);
2078
      }
2079
206.3.1 by Patrick Galbraith
Most everything working with client rename
2080
      for (i= 0; i < drizzle_num_fields(res); i++)
1 by brian
clean slate
2081
      {
2082
        int is_blob;
288 by Brian Aker
ulong cleanp in client apps
2083
        uint32_t length= lengths[i];
1 by brian
clean slate
2084
206.3.1 by Patrick Galbraith
Most everything working with client rename
2085
        if (!(field= drizzle_fetch_field(res)))
1 by brian
clean slate
2086
          die(EX_CONSCHECK,
2087
                      "Not enough fields from table %s! Aborting.\n",
2088
                      result_table);
2089
2090
        /*
2091
           63 is my_charset_bin. If charsetnr is not 63,
2092
           we have not a BLOB but a TEXT column.
2093
           we'll dump in hex only BLOB columns.
2094
        */
2095
        is_blob= (opt_hex_blob && field->charsetnr == 63 &&
241 by Brian Aker
First pass of CHAR removal.
2096
                  (field->type == DRIZZLE_TYPE_VARCHAR ||
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2097
                   field->type == DRIZZLE_TYPE_BLOB)) ? 1 : 0;
1 by brian
clean slate
2098
        if (extended_insert && !opt_xml)
2099
        {
2100
          if (i == 0)
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2101
          {
2102
            extended_row.clear();
2103
            extended_row.append("(");
2104
          }
1 by brian
clean slate
2105
          else
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2106
            extended_row.append(",");
1 by brian
clean slate
2107
2108
          if (row[i])
2109
          {
2110
            if (length)
2111
            {
383.1.45 by Monty Taylor
libdrizzle.h cleanup. Removed some unused things. Started splitting header into
2112
              if (!(field->type & NUM_FLAG))
1 by brian
clean slate
2113
              {
2114
                /*
2115
                  "length * 2 + 2" is OK for both HEX and non-HEX modes:
2116
                  - In HEX mode we need exactly 2 bytes per character
2117
                  plus 2 bytes for '0x' prefix.
2118
                  - In non-HEX mode we need up to 2 bytes per character,
2119
                  plus 2 bytes for leading and trailing '\'' characters.
2120
                  Also we need to reserve 1 byte for terminating '\0'.
2121
                */
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2122
                char * tmp_str= (char *)malloc(length * 2 + 2 + 1);
2123
                memset(tmp_str, '\0', length * 2 + 2 + 1);
1 by brian
clean slate
2124
                if (opt_hex_blob && is_blob)
2125
                {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2126
                  extended_row.append("0x");
584.3.3 by Moriyoshi Koizumi
Incorporating changes proposed by mtaylor.
2127
                  octet2hex(tmp_str, row[i], length);
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2128
                  extended_row.append(tmp_str);
1 by brian
clean slate
2129
                }
2130
                else
2131
                {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2132
                  extended_row.append("'");
383.1.38 by Monty Taylor
Reworked drizzle_escape_string.
2133
                  drizzle_escape_string(tmp_str,
2134
                                        row[i],length);
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2135
                  extended_row.append(tmp_str);
2136
                  extended_row.append("'");
1 by brian
clean slate
2137
                }
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2138
                free(tmp_str);
1 by brian
clean slate
2139
              }
2140
              else
2141
              {
2142
                /* change any strings ("inf", "-inf", "nan") into NULL */
2143
                char *ptr= row[i];
2144
                if (my_isalpha(charset_info, *ptr) || (*ptr == '-' &&
2145
                    my_isalpha(charset_info, ptr[1])))
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2146
                  extended_row.append( "NULL");
1 by brian
clean slate
2147
                else
2148
                {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2149
                  extended_row.append( ptr);
1 by brian
clean slate
2150
                }
2151
              }
2152
            }
2153
            else
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2154
              extended_row.append("''");
1 by brian
clean slate
2155
          }
2156
          else
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2157
            extended_row.append("NULL");
1 by brian
clean slate
2158
        }
2159
        else
2160
        {
2161
          if (i && !opt_xml)
2162
          {
2163
            fputc(',', md_result_file);
2164
            check_io(md_result_file);
2165
          }
2166
          if (row[i])
2167
          {
383.1.45 by Monty Taylor
libdrizzle.h cleanup. Removed some unused things. Started splitting header into
2168
            if (!(field->type & NUM_FLAG))
1 by brian
clean slate
2169
            {
2170
              if (opt_xml)
2171
              {
2172
                if (opt_hex_blob && is_blob && length)
2173
                {
2174
                  /* Define xsi:type="xs:hexBinary" for hex encoded data */
2175
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
461 by Monty Taylor
Removed NullS. bu-bye.
2176
                                field->name, "xsi:type=", "xs:hexBinary", NULL);
1 by brian
clean slate
2177
                  print_blob_as_hex(md_result_file, row[i], length);
2178
                }
2179
                else
2180
                {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2181
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
461 by Monty Taylor
Removed NullS. bu-bye.
2182
                                field->name, NULL);
1 by brian
clean slate
2183
                  print_quoted_xml(md_result_file, row[i], length);
2184
                }
2185
                fputs("</field>\n", md_result_file);
2186
              }
2187
              else if (opt_hex_blob && is_blob && length)
2188
              {
2189
                fputs("0x", md_result_file);
2190
                print_blob_as_hex(md_result_file, row[i], length);
2191
              }
2192
              else
2193
                unescape(md_result_file, row[i], length);
2194
            }
2195
            else
2196
            {
2197
              /* change any strings ("inf", "-inf", "nan") into NULL */
2198
              char *ptr= row[i];
2199
              if (opt_xml)
2200
              {
2201
                print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
461 by Monty Taylor
Removed NullS. bu-bye.
2202
                        field->name, NULL);
1 by brian
clean slate
2203
                fputs(!my_isalpha(charset_info, *ptr) ? ptr: "NULL",
2204
                      md_result_file);
2205
                fputs("</field>\n", md_result_file);
2206
              }
2207
              else if (my_isalpha(charset_info, *ptr) ||
2208
                       (*ptr == '-' && my_isalpha(charset_info, ptr[1])))
2209
                fputs("NULL", md_result_file);
2210
              else
2211
                fputs(ptr, md_result_file);
2212
            }
2213
          }
2214
          else
2215
          {
2216
            /* The field value is NULL */
2217
            if (!opt_xml)
2218
              fputs("NULL", md_result_file);
2219
            else
2220
              print_xml_null_tag(md_result_file, "\t\t", "field name=",
2221
                                 field->name, "\n");
2222
          }
2223
          check_io(md_result_file);
2224
        }
2225
      }
2226
2227
      if (opt_xml)
2228
      {
2229
        fputs("\t</row>\n", md_result_file);
2230
        check_io(md_result_file);
2231
      }
2232
2233
      if (extended_insert)
2234
      {
288 by Brian Aker
ulong cleanp in client apps
2235
        uint32_t row_length;
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2236
        extended_row.append(")");
2237
        row_length= 2 + extended_row.length();
1 by brian
clean slate
2238
        if (total_length + row_length < opt_net_buffer_length)
2239
        {
2240
          total_length+= row_length;
2241
          fputc(',',md_result_file);            /* Always row break */
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2242
          fputs(extended_row.c_str(),md_result_file);
1 by brian
clean slate
2243
        }
2244
        else
2245
        {
2246
          if (row_break)
2247
            fputs(";\n", md_result_file);
2248
          row_break=1;                          /* This is first row */
2249
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2250
          fputs(insert_pat.c_str(),md_result_file);
2251
          fputs(extended_row.c_str(),md_result_file);
1 by brian
clean slate
2252
          total_length= row_length+init_length;
2253
        }
2254
        check_io(md_result_file);
2255
      }
2256
      else if (!opt_xml)
2257
      {
2258
        fputs(");\n", md_result_file);
2259
        check_io(md_result_file);
2260
      }
2261
    }
2262
2263
    /* XML - close table tag and supress regular output */
2264
    if (opt_xml)
2265
        fputs("\t</table_data>\n", md_result_file);
2266
    else if (extended_insert && row_break)
2267
      fputs(";\n", md_result_file);             /* If not empty table */
2268
    fflush(md_result_file);
2269
    check_io(md_result_file);
206.3.1 by Patrick Galbraith
Most everything working with client rename
2270
    if (drizzle_errno(drizzle))
1 by brian
clean slate
2271
    {
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
2272
      snprintf(buf, sizeof(buf),
288 by Brian Aker
ulong cleanp in client apps
2273
               "%s: Error %d: %s when dumping table %s at row: %d\n",
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
2274
               my_progname,
206.3.1 by Patrick Galbraith
Most everything working with client rename
2275
               drizzle_errno(drizzle),
2276
               drizzle_error(drizzle),
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
2277
               result_table,
2278
               rownr);
1 by brian
clean slate
2279
      fputs(buf,stderr);
2280
      error= EX_CONSCHECK;
2281
      goto err;
2282
    }
2283
2284
    /* Moved enable keys to before unlock per bug 15977 */
2285
    if (opt_disable_keys)
2286
    {
2287
      fprintf(md_result_file,"/*!40000 ALTER TABLE %s ENABLE KEYS */;\n",
2288
              opt_quoted_table);
2289
      check_io(md_result_file);
2290
    }
2291
    if (opt_lock)
2292
    {
2293
      fputs("UNLOCK TABLES;\n", md_result_file);
2294
      check_io(md_result_file);
2295
    }
2296
    if (opt_autocommit)
2297
    {
2298
      fprintf(md_result_file, "commit;\n");
2299
      check_io(md_result_file);
2300
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2301
    drizzle_free_result(res);
1 by brian
clean slate
2302
  }
142.1.2 by Patrick
All DBUG_x removed from client/
2303
  return;
1 by brian
clean slate
2304
2305
err:
2306
  maybe_exit(error);
142.1.2 by Patrick
All DBUG_x removed from client/
2307
  return;
1 by brian
clean slate
2308
} /* dump_table */
2309
2310
2311
static char *getTableName(int reset)
2312
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2313
  static DRIZZLE_RES *res= NULL;
2314
  DRIZZLE_ROW    row;
1 by brian
clean slate
2315
2316
  if (!res)
2317
  {
461 by Monty Taylor
Removed NullS. bu-bye.
2318
    if (!(res= drizzle_list_tables(drizzle,NULL)))
1 by brian
clean slate
2319
      return(NULL);
2320
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2321
  if ((row= drizzle_fetch_row(res)))
1 by brian
clean slate
2322
    return((char*) row[0]);
2323
2324
  if (reset)
206.3.1 by Patrick Galbraith
Most everything working with client rename
2325
    drizzle_data_seek(res,0);      /* We want to read again */
1 by brian
clean slate
2326
  else
2327
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2328
    drizzle_free_result(res);
1 by brian
clean slate
2329
    res= NULL;
2330
  }
2331
  return(NULL);
2332
} /* getTableName */
2333
2334
2335
static int dump_all_databases()
2336
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2337
  DRIZZLE_ROW row;
2338
  DRIZZLE_RES *tableres;
1 by brian
clean slate
2339
  int result=0;
2340
206.3.1 by Patrick Galbraith
Most everything working with client rename
2341
  if (drizzle_query_with_error_report(drizzle, &tableres, "SHOW DATABASES"))
1 by brian
clean slate
2342
    return 1;
206.3.1 by Patrick Galbraith
Most everything working with client rename
2343
  while ((row= drizzle_fetch_row(tableres)))
1 by brian
clean slate
2344
  {
2345
    if (dump_all_tables_in_db(row[0]))
2346
      result=1;
2347
  }
2348
  return result;
2349
}
2350
/* dump_all_databases */
2351
2352
2353
static int dump_databases(char **db_names)
2354
{
2355
  int result=0;
2356
  char **db;
142.1.2 by Patrick
All DBUG_x removed from client/
2357
1 by brian
clean slate
2358
2359
  for (db= db_names ; *db ; db++)
2360
  {
2361
    if (dump_all_tables_in_db(*db))
2362
      result=1;
2363
  }
142.1.2 by Patrick
All DBUG_x removed from client/
2364
  return(result);
1 by brian
clean slate
2365
} /* dump_databases */
2366
2367
2368
/*
2369
Table Specific database initalization.
2370
2371
SYNOPSIS
2372
  init_dumping_tables
2373
  qdatabase      quoted name of the database
2374
2375
RETURN VALUES
2376
  0        Success.
2377
  1        Failure.
2378
*/
2379
2380
int init_dumping_tables(char *qdatabase)
2381
{
142.1.2 by Patrick
All DBUG_x removed from client/
2382
1 by brian
clean slate
2383
2384
  if (!opt_create_db)
2385
  {
2386
    char qbuf[256];
206.3.1 by Patrick Galbraith
Most everything working with client rename
2387
    DRIZZLE_ROW row;
2388
    DRIZZLE_RES *dbinfo;
1 by brian
clean slate
2389
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
2390
    snprintf(qbuf, sizeof(qbuf),
2391
             "SHOW CREATE DATABASE IF NOT EXISTS %s",
2392
             qdatabase);
1 by brian
clean slate
2393
206.3.1 by Patrick Galbraith
Most everything working with client rename
2394
    if (drizzle_query(drizzle, qbuf) || !(dbinfo = drizzle_store_result(drizzle)))
1 by brian
clean slate
2395
    {
2396
      /* Old server version, dump generic CREATE DATABASE */
2397
      if (opt_drop_database)
2398
        fprintf(md_result_file,
2399
                "\n/*!40000 DROP DATABASE IF EXISTS %s*/;\n",
2400
                qdatabase);
2401
      fprintf(md_result_file,
2402
              "\nCREATE DATABASE /*!32312 IF NOT EXISTS*/ %s;\n",
2403
              qdatabase);
2404
    }
2405
    else
2406
    {
2407
      if (opt_drop_database)
2408
        fprintf(md_result_file,
2409
                "\n/*!40000 DROP DATABASE IF EXISTS %s*/;\n",
2410
                qdatabase);
206.3.1 by Patrick Galbraith
Most everything working with client rename
2411
      row = drizzle_fetch_row(dbinfo);
1 by brian
clean slate
2412
      if (row[1])
2413
      {
2414
        fprintf(md_result_file,"\n%s;\n",row[1]);
2415
      }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2416
      drizzle_free_result(dbinfo);
1 by brian
clean slate
2417
    }
2418
  }
142.1.2 by Patrick
All DBUG_x removed from client/
2419
  return(0);
1 by brian
clean slate
2420
} /* init_dumping_tables */
2421
2422
2423
static int init_dumping(char *database, int init_func(char*))
2424
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2425
  if (drizzle_get_server_version(drizzle) >= 50003 &&
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
2426
      !my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
1 by brian
clean slate
2427
    return 1;
2428
206.3.1 by Patrick Galbraith
Most everything working with client rename
2429
  if (drizzle_select_db(drizzle, database))
1 by brian
clean slate
2430
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2431
    DB_error(drizzle, "when selecting the database");
1 by brian
clean slate
2432
    return 1;                   /* If --force */
2433
  }
2434
  if (!path && !opt_xml)
2435
  {
2436
    if (opt_databases || opt_alldbs)
2437
    {
2438
      /*
2439
        length of table name * 2 (if name contains quotes), 2 quotes and 0
2440
      */
2441
      char quoted_database_buf[NAME_LEN*2+3];
2442
      char *qdatabase= quote_name(database,quoted_database_buf,opt_quoted);
2443
      if (opt_comments)
2444
      {
2445
        fprintf(md_result_file,"\n--\n-- Current Database: %s\n--\n", qdatabase);
2446
        check_io(md_result_file);
2447
      }
2448
2449
      /* Call the view or table specific function */
2450
      init_func(qdatabase);
2451
2452
      fprintf(md_result_file,"\nUSE %s;\n", qdatabase);
2453
      check_io(md_result_file);
2454
    }
2455
  }
2456
  if (extended_insert)
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2457
    extended_row.clear();
1 by brian
clean slate
2458
  return 0;
2459
} /* init_dumping */
2460
2461
2462
/* Return 1 if we should copy the table */
2463
481 by Brian Aker
Remove all of uchar.
2464
static bool include_table(const unsigned char *hash_key, size_t len)
1 by brian
clean slate
2465
{
2466
  return !hash_search(&ignore_table, hash_key, len);
2467
}
2468
2469
2470
static int dump_all_tables_in_db(char *database)
2471
{
2472
  char *table;
2473
  uint numrows;
2474
  char table_buff[NAME_LEN*2+3];
2475
  char hash_key[2*NAME_LEN+2];  /* "db.tablename" */
2476
  char *afterdot;
2477
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2478
  afterdot= strcpy(hash_key, database) + strlen(database);
1 by brian
clean slate
2479
  *afterdot++= '.';
2480
2481
  if (init_dumping(database, init_dumping_tables))
142.1.2 by Patrick
All DBUG_x removed from client/
2482
    return(1);
1 by brian
clean slate
2483
  if (opt_xml)
461 by Monty Taylor
Removed NullS. bu-bye.
2484
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NULL);
1 by brian
clean slate
2485
  if (lock_tables)
2486
  {
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2487
    string query("LOCK TABLES ");
1 by brian
clean slate
2488
    for (numrows= 0 ; (table= getTableName(1)) ; )
2489
    {
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2490
      char *end= strcpy(afterdot, table) + strlen(table);
481 by Brian Aker
Remove all of uchar.
2491
      if (include_table((unsigned char*) hash_key,end - hash_key))
1 by brian
clean slate
2492
      {
2493
        numrows++;
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2494
        query.append( quote_name(table, table_buff, 1));
2495
        query.append( " READ /*!32311 LOCAL */,");
1 by brian
clean slate
2496
      }
2497
    }
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2498
    if (numrows && drizzle_real_query(drizzle, query.c_str(), query.length()-1))
206.3.1 by Patrick Galbraith
Most everything working with client rename
2499
      DB_error(drizzle, "when using LOCK TABLES");
1 by brian
clean slate
2500
            /* We shall continue here, if --force was given */
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2501
    query.clear();
1 by brian
clean slate
2502
  }
2503
  if (flush_logs)
2504
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2505
    if (drizzle_refresh(drizzle, REFRESH_LOG))
2506
      DB_error(drizzle, "when doing refresh");
1 by brian
clean slate
2507
           /* We shall continue here, if --force was given */
2508
  }
2509
  while ((table= getTableName(0)))
2510
  {
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2511
    char *end= strcpy(afterdot, table) + strlen(table);
481 by Brian Aker
Remove all of uchar.
2512
    if (include_table((unsigned char*) hash_key, end - hash_key))
1 by brian
clean slate
2513
    {
2514
      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.
2515
      free(order_by);
1 by brian
clean slate
2516
      order_by= 0;
2517
    }
2518
  }
2519
  if (opt_xml)
2520
  {
2521
    fputs("</database>\n", md_result_file);
2522
    check_io(md_result_file);
2523
  }
2524
  if (lock_tables)
398.1.10 by Monty Taylor
Actually removed VOID() this time.
2525
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
739 by Brian Aker
Removed dead table map code in replication. Also refactored drizzledump a
2526
142.1.2 by Patrick
All DBUG_x removed from client/
2527
  return(0);
1 by brian
clean slate
2528
} /* dump_all_tables_in_db */
2529
2530
2531
/*
2532
  get_actual_table_name -- executes a SHOW TABLES LIKE '%s' to get the actual
2533
  table name from the server for the table name given on the command line.
2534
  we do this because the table name given on the command line may be a
2535
  different case (e.g.  T1 vs t1)
2536
2537
  RETURN
2538
    pointer to the table name
2539
    0 if error
2540
*/
2541
2542
static char *get_actual_table_name(const char *old_table_name, MEM_ROOT *root)
2543
{
2544
  char *name= 0;
206.3.1 by Patrick Galbraith
Most everything working with client rename
2545
  DRIZZLE_RES  *table_res;
2546
  DRIZZLE_ROW  row;
1 by brian
clean slate
2547
  char query[50 + 2*NAME_LEN];
2548
  char show_name_buff[FN_REFLEN];
142.1.2 by Patrick
All DBUG_x removed from client/
2549
1 by brian
clean slate
2550
2551
  /* Check memory for quote_for_like() */
142.1.2 by Patrick
All DBUG_x removed from client/
2552
  assert(2*sizeof(old_table_name) < sizeof(show_name_buff));
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
2553
  snprintf(query, sizeof(query), "SHOW TABLES LIKE %s",
2554
           quote_for_like(old_table_name, show_name_buff));
1 by brian
clean slate
2555
206.3.1 by Patrick Galbraith
Most everything working with client rename
2556
  if (drizzle_query_with_error_report(drizzle, 0, query))
461 by Monty Taylor
Removed NullS. bu-bye.
2557
    return NULL;
1 by brian
clean slate
2558
206.3.1 by Patrick Galbraith
Most everything working with client rename
2559
  if ((table_res= drizzle_store_result(drizzle)))
1 by brian
clean slate
2560
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2561
    uint64_t num_rows= drizzle_num_rows(table_res);
1 by brian
clean slate
2562
    if (num_rows > 0)
2563
    {
77.1.107 by Monty Taylor
Fixed build warnings.
2564
      uint32_t *lengths;
1 by brian
clean slate
2565
      /*
2566
        Return first row
2567
        TODO: Return all matching rows
2568
      */
206.3.1 by Patrick Galbraith
Most everything working with client rename
2569
      row= drizzle_fetch_row(table_res);
2570
      lengths= drizzle_fetch_lengths(table_res);
1 by brian
clean slate
2571
      name= strmake_root(root, row[0], lengths[0]);
2572
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2573
    drizzle_free_result(table_res);
1 by brian
clean slate
2574
  }
142.1.2 by Patrick
All DBUG_x removed from client/
2575
  return(name);
1 by brian
clean slate
2576
}
2577
2578
2579
static int dump_selected_tables(char *db, char **table_names, int tables)
2580
{
2581
  char table_buff[NAME_LEN*2+3];
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2582
  string lock_tables_query("LOCK TABLES ");
1 by brian
clean slate
2583
  MEM_ROOT root;
2584
  char **dump_tables, **pos, **end;
142.1.2 by Patrick
All DBUG_x removed from client/
2585
1 by brian
clean slate
2586
2587
  if (init_dumping(db, init_dumping_tables))
142.1.2 by Patrick
All DBUG_x removed from client/
2588
    return(1);
1 by brian
clean slate
2589
2590
  init_alloc_root(&root, 8192, 0);
2591
  if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *))))
2592
     die(EX_EOM, "alloc_root failure.");
2593
2594
  for (; tables > 0 ; tables-- , table_names++)
2595
  {
2596
    /* the table name passed on commandline may be wrong case */
2597
    if ((*pos= get_actual_table_name(*table_names, &root)))
2598
    {
2599
      /* Add found table name to lock_tables_query */
2600
      if (lock_tables)
2601
      {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2602
        lock_tables_query.append( quote_name(*pos, table_buff, 1));
2603
        lock_tables_query.append( " READ /*!32311 LOCAL */,");
1 by brian
clean slate
2604
      }
2605
      pos++;
2606
    }
2607
    else
2608
    {
2609
      if (!ignore_errors)
2610
      {
2611
        free_root(&root, MYF(0));
2612
      }
2613
      maybe_die(EX_ILLEGAL_TABLE, "Couldn't find table: \"%s\"", *table_names);
2614
      /* We shall countinue here, if --force was given */
2615
    }
2616
  }
2617
  end= pos;
2618
2619
  if (lock_tables)
2620
  {
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2621
    if (drizzle_real_query(drizzle, lock_tables_query.c_str(),
2622
                         lock_tables_query.length()-1))
1 by brian
clean slate
2623
    {
2624
      if (!ignore_errors)
2625
      {
2626
        free_root(&root, MYF(0));
2627
      }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2628
      DB_error(drizzle, "when doing LOCK TABLES");
1 by brian
clean slate
2629
       /* We shall countinue here, if --force was given */
2630
    }
2631
  }
2632
  if (flush_logs)
2633
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2634
    if (drizzle_refresh(drizzle, REFRESH_LOG))
1 by brian
clean slate
2635
    {
2636
      if (!ignore_errors)
2637
        free_root(&root, MYF(0));
206.3.1 by Patrick Galbraith
Most everything working with client rename
2638
      DB_error(drizzle, "when doing refresh");
1 by brian
clean slate
2639
    }
2640
     /* We shall countinue here, if --force was given */
2641
  }
2642
  if (opt_xml)
461 by Monty Taylor
Removed NullS. bu-bye.
2643
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NULL);
1 by brian
clean slate
2644
2645
  /* Dump each selected table */
2646
  for (pos= dump_tables; pos < end; pos++)
2647
    dump_table(*pos, db);
2648
2649
  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.
2650
  free(order_by);
1 by brian
clean slate
2651
  order_by= 0;
2652
  if (opt_xml)
2653
  {
2654
    fputs("</database>\n", md_result_file);
2655
    check_io(md_result_file);
2656
  }
2657
  if (lock_tables)
398.1.10 by Monty Taylor
Actually removed VOID() this time.
2658
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
142.1.2 by Patrick
All DBUG_x removed from client/
2659
  return(0);
1 by brian
clean slate
2660
} /* dump_selected_tables */
2661
2662
206.3.1 by Patrick Galbraith
Most everything working with client rename
2663
static int do_show_master_status(DRIZZLE *drizzle_con)
1 by brian
clean slate
2664
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2665
  DRIZZLE_ROW row;
2666
  DRIZZLE_RES *master;
1 by brian
clean slate
2667
  const char *comment_prefix=
206.3.1 by Patrick Galbraith
Most everything working with client rename
2668
    (opt_master_data == DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL) ? "-- " : "";
2669
  if (drizzle_query_with_error_report(drizzle_con, &master, "SHOW MASTER STATUS"))
1 by brian
clean slate
2670
  {
2671
    return 1;
2672
  }
2673
  else
2674
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2675
    row= drizzle_fetch_row(master);
1 by brian
clean slate
2676
    if (row && row[0] && row[1])
2677
    {
2678
      /* SHOW MASTER STATUS reports file and position */
2679
      if (opt_comments)
2680
        fprintf(md_result_file,
2681
                "\n--\n-- Position to start replication or point-in-time "
2682
                "recovery from\n--\n\n");
2683
      fprintf(md_result_file,
2684
              "%sCHANGE MASTER TO MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;\n",
2685
              comment_prefix, row[0], row[1]);
2686
      check_io(md_result_file);
2687
    }
2688
    else if (!ignore_errors)
2689
    {
2690
      /* SHOW MASTER STATUS reports nothing and --force is not enabled */
2691
      my_printf_error(0, "Error: Binlogging on server not active",
2692
                      MYF(0));
206.3.1 by Patrick Galbraith
Most everything working with client rename
2693
      drizzle_free_result(master);
2694
      maybe_exit(EX_DRIZZLEERR);
1 by brian
clean slate
2695
      return 1;
2696
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2697
    drizzle_free_result(master);
1 by brian
clean slate
2698
  }
2699
  return 0;
2700
}
2701
206.3.1 by Patrick Galbraith
Most everything working with client rename
2702
static int do_stop_slave_sql(DRIZZLE *drizzle_con)
1 by brian
clean slate
2703
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2704
  DRIZZLE_RES *slave;
1 by brian
clean slate
2705
  /* 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
2706
  if (drizzle_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS"))
1 by brian
clean slate
2707
    return(1);
2708
  else
2709
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2710
    DRIZZLE_ROW row= drizzle_fetch_row(slave);
1 by brian
clean slate
2711
    if (row && row[11])
2712
    {
2713
      /* if SLAVE SQL is not running, we don't stop it */
2714
      if (!strcmp(row[11],"No"))
2715
      {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2716
        drizzle_free_result(slave);
1 by brian
clean slate
2717
        /* Silently assume that they don't have the slave running */
2718
        return(0);
2719
      }
2720
    }
2721
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2722
  drizzle_free_result(slave);
1 by brian
clean slate
2723
2724
  /* now, stop slave if running */
206.3.1 by Patrick Galbraith
Most everything working with client rename
2725
  if (drizzle_query_with_error_report(drizzle_con, 0, "STOP SLAVE SQL_THREAD"))
1 by brian
clean slate
2726
    return(1);
2727
2728
  return(0);
2729
}
2730
2731
static int add_stop_slave(void)
2732
{
2733
  if (opt_comments)
2734
    fprintf(md_result_file,
2735
            "\n--\n-- stop slave statement to make a recovery dump)\n--\n\n");
2736
  fprintf(md_result_file, "STOP SLAVE;\n");
2737
  return(0);
2738
}
2739
2740
static int add_slave_statements(void)
2741
{
2742
  if (opt_comments)
2743
    fprintf(md_result_file,
2744
            "\n--\n-- start slave statement to make a recovery dump)\n--\n\n");
2745
  fprintf(md_result_file, "START SLAVE;\n");
2746
  return(0);
2747
}
2748
206.3.1 by Patrick Galbraith
Most everything working with client rename
2749
static int do_show_slave_status(DRIZZLE *drizzle_con)
1 by brian
clean slate
2750
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2751
  DRIZZLE_RES *slave;
1 by brian
clean slate
2752
  const char *comment_prefix=
206.3.1 by Patrick Galbraith
Most everything working with client rename
2753
    (opt_slave_data == DRIZZLE_OPT_SLAVE_DATA_COMMENTED_SQL) ? "-- " : "";
2754
  if (drizzle_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS"))
1 by brian
clean slate
2755
  {
2756
    if (!ignore_errors)
2757
    {
2758
      /* SHOW SLAVE STATUS reports nothing and --force is not enabled */
2759
      my_printf_error(0, "Error: Slave not set up", MYF(0));
2760
    }
2761
    return 1;
2762
  }
2763
  else
2764
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2765
    DRIZZLE_ROW row= drizzle_fetch_row(slave);
1 by brian
clean slate
2766
    if (row && row[9] && row[21])
2767
    {
2768
      /* SHOW MASTER STATUS reports file and position */
2769
      if (opt_comments)
2770
        fprintf(md_result_file,
2771
                "\n--\n-- Position to start replication or point-in-time "
2772
                "recovery from (the master of this slave)\n--\n\n");
2773
2774
      fprintf(md_result_file, "%sCHANGE MASTER TO ", comment_prefix);
2775
2776
      if (opt_include_master_host_port)
2777
      {
2778
        if (row[1])
2779
          fprintf(md_result_file, "MASTER_HOST='%s', ", row[1]);
2780
        if (row[3])
2781
          fprintf(md_result_file, "MASTER_PORT='%s', ", row[3]);
2782
      }
2783
      fprintf(md_result_file,
2784
              "MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;\n", row[9], row[21]);
2785
2786
      check_io(md_result_file);
2787
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2788
    drizzle_free_result(slave);
1 by brian
clean slate
2789
  }
2790
  return 0;
2791
}
2792
206.3.1 by Patrick Galbraith
Most everything working with client rename
2793
static int do_start_slave_sql(DRIZZLE *drizzle_con)
1 by brian
clean slate
2794
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2795
  DRIZZLE_RES *slave;
1 by brian
clean slate
2796
  /* 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
2797
  if (drizzle_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS"))
1 by brian
clean slate
2798
    return(1);
2799
  else
2800
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2801
    DRIZZLE_ROW row= drizzle_fetch_row(slave);
1 by brian
clean slate
2802
    if (row && row[11])
2803
    {
2804
      /* if SLAVE SQL is not running, we don't start it */
2805
      if (!strcmp(row[11],"Yes"))
2806
      {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2807
        drizzle_free_result(slave);
1 by brian
clean slate
2808
        /* Silently assume that they don't have the slave running */
2809
        return(0);
2810
      }
2811
    }
2812
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
2813
  drizzle_free_result(slave);
1 by brian
clean slate
2814
2815
  /* now, start slave if stopped */
206.3.1 by Patrick Galbraith
Most everything working with client rename
2816
  if (drizzle_query_with_error_report(drizzle_con, 0, "START SLAVE"))
1 by brian
clean slate
2817
  {
2818
    my_printf_error(0, "Error: Unable to start slave", MYF(0));
2819
    return 1;
2820
  }
2821
  return(0);
2822
}
2823
2824
2825
206.3.1 by Patrick Galbraith
Most everything working with client rename
2826
static int do_flush_tables_read_lock(DRIZZLE *drizzle_con)
1 by brian
clean slate
2827
{
2828
  /*
2829
    We do first a FLUSH TABLES. If a long update is running, the FLUSH TABLES
2830
    will wait but will not stall the whole mysqld, and when the long update is
2831
    done the FLUSH TABLES WITH READ LOCK will start and succeed quickly. So,
2832
    FLUSH TABLES is to lower the probability of a stage where both mysqldump
2833
    and most client connections are stalled. Of course, if a second long
2834
    update starts between the two FLUSHes, we have that bad stall.
2835
  */
2836
  return
206.3.1 by Patrick Galbraith
Most everything working with client rename
2837
    ( drizzle_query_with_error_report(drizzle_con, 0, "FLUSH TABLES") ||
2838
      drizzle_query_with_error_report(drizzle_con, 0,
1 by brian
clean slate
2839
                                    "FLUSH TABLES WITH READ LOCK") );
2840
}
2841
2842
206.3.1 by Patrick Galbraith
Most everything working with client rename
2843
static int do_unlock_tables(DRIZZLE *drizzle_con)
1 by brian
clean slate
2844
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2845
  return drizzle_query_with_error_report(drizzle_con, 0, "UNLOCK TABLES");
1 by brian
clean slate
2846
}
2847
206.3.1 by Patrick Galbraith
Most everything working with client rename
2848
static int get_bin_log_name(DRIZZLE *drizzle_con,
1 by brian
clean slate
2849
                            char* buff_log_name, uint buff_len)
2850
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2851
  DRIZZLE_RES *res;
2852
  DRIZZLE_ROW row;
1 by brian
clean slate
2853
206.3.1 by Patrick Galbraith
Most everything working with client rename
2854
  if (drizzle_query(drizzle_con, "SHOW MASTER STATUS") ||
2855
      !(res= drizzle_store_result(drizzle)))
1 by brian
clean slate
2856
    return 1;
2857
206.3.1 by Patrick Galbraith
Most everything working with client rename
2858
  if (!(row= drizzle_fetch_row(res)))
1 by brian
clean slate
2859
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
2860
    drizzle_free_result(res);
1 by brian
clean slate
2861
    return 1;
2862
  }
2863
  /*
2864
    Only one row is returned, and the first column is the name of the
2865
    active log.
2866
  */
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
2867
  strncpy(buff_log_name, row[0], buff_len - 1);
1 by brian
clean slate
2868
206.3.1 by Patrick Galbraith
Most everything working with client rename
2869
  drizzle_free_result(res);
1 by brian
clean slate
2870
  return 0;
2871
}
2872
206.3.1 by Patrick Galbraith
Most everything working with client rename
2873
static int purge_bin_logs_to(DRIZZLE *drizzle_con, char* log_name)
1 by brian
clean slate
2874
{
2875
  int err;
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
2876
  string str= "PURGE BINARY LOGS TO '";
2877
  str.append(log_name);
2878
  str.append("'");
2879
  err = drizzle_query_with_error_report(drizzle_con, 0, str.c_str());
1 by brian
clean slate
2880
  return err;
2881
}
2882
2883
206.3.1 by Patrick Galbraith
Most everything working with client rename
2884
static int start_transaction(DRIZZLE *drizzle_con)
1 by brian
clean slate
2885
{
2886
  /*
2887
    We use BEGIN for old servers. --single-transaction --master-data will fail
2888
    on old servers, but that's ok as it was already silently broken (it didn't
2889
    do a consistent read, so better tell people frankly, with the error).
2890
2891
    We want the first consistent read to be used for all tables to dump so we
2892
    need the REPEATABLE READ level (not anything lower, for example READ
2893
    COMMITTED would give one new consistent read per dumped table).
2894
  */
206.3.1 by Patrick Galbraith
Most everything working with client rename
2895
  if ((drizzle_get_server_version(drizzle_con) < 40100) && opt_master_data)
1 by brian
clean slate
2896
  {
2897
    fprintf(stderr, "-- %s: the combination of --single-transaction and "
206.3.1 by Patrick Galbraith
Most everything working with client rename
2898
            "--master-data requires a DRIZZLE server version of at least 4.1 "
1 by brian
clean slate
2899
            "(current server's version is %s). %s\n",
2900
            ignore_errors ? "Warning" : "Error",
206.3.1 by Patrick Galbraith
Most everything working with client rename
2901
            drizzle_con->server_version ? drizzle_con->server_version : "unknown",
1 by brian
clean slate
2902
            ignore_errors ? "Continuing due to --force, backup may not be consistent across all tables!" : "Aborting.");
2903
    if (!ignore_errors)
206.3.1 by Patrick Galbraith
Most everything working with client rename
2904
      exit(EX_DRIZZLEERR);
1 by brian
clean slate
2905
  }
2906
206.3.1 by Patrick Galbraith
Most everything working with client rename
2907
  return (drizzle_query_with_error_report(drizzle_con, 0,
1 by brian
clean slate
2908
                                        "SET SESSION TRANSACTION ISOLATION "
2909
                                        "LEVEL REPEATABLE READ") ||
206.3.1 by Patrick Galbraith
Most everything working with client rename
2910
          drizzle_query_with_error_report(drizzle_con, 0,
1 by brian
clean slate
2911
                                        "START TRANSACTION "
2912
                                        "/*!40100 WITH CONSISTENT SNAPSHOT */"));
2913
}
2914
2915
288 by Brian Aker
ulong cleanp in client apps
2916
static uint32_t find_set(TYPELIB *lib, const char *x, uint length,
1 by brian
clean slate
2917
                      char **err_pos, uint *err_len)
2918
{
2919
  const char *end= x + length;
288 by Brian Aker
ulong cleanp in client apps
2920
  uint32_t found= 0;
1 by brian
clean slate
2921
  uint find;
2922
  char buff[255];
2923
2924
  *err_pos= 0;                  /* No error yet */
2925
  while (end > x && my_isspace(charset_info, end[-1]))
2926
    end--;
2927
2928
  *err_len= 0;
2929
  if (x != end)
2930
  {
2931
    const char *start= x;
2932
    for (;;)
2933
    {
2934
      const char *pos= start;
297 by Brian Aker
Final ulong cleanup in clients
2935
      uint32_t var_len;
1 by brian
clean slate
2936
2937
      for (; pos != end && *pos != ','; pos++) ;
297 by Brian Aker
Final ulong cleanup in clients
2938
      var_len= (uint32_t) (pos - start);
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
2939
      strncpy(buff, start, min((uint32_t)sizeof(buff), var_len));
1 by brian
clean slate
2940
      find= find_type(buff, lib, var_len);
2941
      if (!find)
2942
      {
2943
        *err_pos= (char*) start;
2944
        *err_len= var_len;
2945
      }
2946
      else
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2947
        found|= (uint32_t)((int64_t) 1 << (find - 1));
1 by brian
clean slate
2948
      if (pos == end)
2949
        break;
2950
      start= pos + 1;
2951
    }
2952
  }
2953
  return found;
2954
}
2955
2956
2957
/* Print a value with a prefix on file */
206.3.1 by Patrick Galbraith
Most everything working with client rename
2958
static void print_value(FILE *file, DRIZZLE_RES  *result, DRIZZLE_ROW row,
1 by brian
clean slate
2959
                        const char *prefix, const char *name,
2960
                        int string_value)
2961
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
2962
  DRIZZLE_FIELD   *field;
2963
  drizzle_field_seek(result, 0);
1 by brian
clean slate
2964
206.3.1 by Patrick Galbraith
Most everything working with client rename
2965
  for ( ; (field= drizzle_fetch_field(result)) ; row++)
1 by brian
clean slate
2966
  {
2967
    if (!strcmp(field->name,name))
2968
    {
2969
      if (row[0] && row[0][0] && strcmp(row[0],"0")) /* Skip default */
2970
      {
2971
        fputc(' ',file);
2972
        fputs(prefix, file);
2973
        if (string_value)
2974
          unescape(file,row[0],(uint) strlen(row[0]));
2975
        else
2976
          fputs(row[0], file);
2977
        check_io(file);
2978
        return;
2979
      }
2980
    }
2981
  }
2982
  return;                                       /* This shouldn't happen */
2983
} /* print_value */
2984
2985
2986
/*
2987
  SYNOPSIS
2988
2989
  Check if we the table is one of the table types that should be ignored:
2990
  MRG_ISAM, MRG_MYISAM, if opt_delayed, if that table supports delayed inserts.
163 by Brian Aker
Merge Monty's code.
2991
  If the table should be altogether ignored, it returns a true, false if it
1 by brian
clean slate
2992
  should not be ignored. If the user has selected to use INSERT DELAYED, it
2993
  sets the value of the bool pointer supports_delayed_inserts to 0 if not
2994
  supported, 1 if it is supported.
2995
2996
  ARGS
2997
2998
    check_if_ignore_table()
2999
    table_name                  Table name to check
3000
    table_type                  Type of table
3001
3002
  GLOBAL VARIABLES
206.3.1 by Patrick Galbraith
Most everything working with client rename
3003
    drizzle                       Drizzle connection
1 by brian
clean slate
3004
    verbose                     Write warning messages
3005
3006
  RETURN
3007
    char (bit value)            See IGNORE_ values at top
3008
*/
3009
3010
char check_if_ignore_table(const char *table_name, char *table_type)
3011
{
3012
  char result= IGNORE_NONE;
3013
  char buff[FN_REFLEN+80], show_name_buff[FN_REFLEN];
206.3.1 by Patrick Galbraith
Most everything working with client rename
3014
  DRIZZLE_RES *res= NULL;
3015
  DRIZZLE_ROW row;
142.1.2 by Patrick
All DBUG_x removed from client/
3016
1 by brian
clean slate
3017
3018
  /* Check memory for quote_for_like() */
142.1.2 by Patrick
All DBUG_x removed from client/
3019
  assert(2*sizeof(table_name) < sizeof(show_name_buff));
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
3020
  snprintf(buff, sizeof(buff), "show table status like %s",
3021
           quote_for_like(table_name, show_name_buff));
206.3.1 by Patrick Galbraith
Most everything working with client rename
3022
  if (drizzle_query_with_error_report(drizzle, &res, buff))
1 by brian
clean slate
3023
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
3024
    if (drizzle_errno(drizzle) != ER_PARSE_ERROR)
3025
    {                                   /* If old DRIZZLE version */
1 by brian
clean slate
3026
      verbose_msg("-- Warning: Couldn't get status information for "
206.3.1 by Patrick Galbraith
Most everything working with client rename
3027
                  "table %s (%s)\n", table_name, drizzle_error(drizzle));
142.1.2 by Patrick
All DBUG_x removed from client/
3028
      return(result);                       /* assume table is ok */
1 by brian
clean slate
3029
    }
3030
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
3031
  if (!(row= drizzle_fetch_row(res)))
1 by brian
clean slate
3032
  {
3033
    fprintf(stderr,
3034
            "Error: Couldn't read status information for table %s (%s)\n",
206.3.1 by Patrick Galbraith
Most everything working with client rename
3035
            table_name, drizzle_error(drizzle));
3036
    drizzle_free_result(res);
142.1.2 by Patrick
All DBUG_x removed from client/
3037
    return(result);                         /* assume table is ok */
1 by brian
clean slate
3038
  }
739 by Brian Aker
Removed dead table map code in replication. Also refactored drizzledump a
3039
3040
  /*
3041
    If the table type matches any of these, we do support delayed inserts.
3042
    Note: we do not want to skip dumping this table if if is not one of
3043
    these types, but we do want to use delayed inserts in the dump if
3044
    the table type is _NOT_ one of these types
3045
    */
1 by brian
clean slate
3046
  {
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
3047
    strncpy(table_type, row[1], NAME_LEN-1);
1 by brian
clean slate
3048
    if (opt_delayed)
3049
    {
3050
      if (strcmp(table_type,"MyISAM") &&
3051
          strcmp(table_type,"ARCHIVE") &&
3052
          strcmp(table_type,"HEAP") &&
3053
          strcmp(table_type,"MEMORY"))
3054
        result= IGNORE_INSERT_DELAYED;
3055
    }
3056
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
3057
  drizzle_free_result(res);
142.1.2 by Patrick
All DBUG_x removed from client/
3058
  return(result);
1 by brian
clean slate
3059
}
3060
3061
3062
/*
3063
  Get string of comma-separated primary key field names
3064
3065
  SYNOPSIS
3066
    char *primary_key_fields(const char *table_name)
3067
    RETURNS     pointer to allocated buffer (must be freed by caller)
3068
    table_name  quoted table name
3069
3070
  DESCRIPTION
3071
    Use SHOW KEYS FROM table_name, allocate a buffer to hold the
3072
    field names, and then build that string and return the pointer
3073
    to that buffer.
3074
3075
    Returns NULL if there is no PRIMARY or UNIQUE key on the table,
3076
    or if there is some failure.  It is better to continue to dump
3077
    the table unsorted, rather than exit without dumping the data.
3078
*/
3079
3080
static char *primary_key_fields(const char *table_name)
3081
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
3082
  DRIZZLE_RES  *res= NULL;
3083
  DRIZZLE_ROW  row;
1 by brian
clean slate
3084
  /* SHOW KEYS FROM + table name * 2 (escaped) + 2 quotes + \0 */
3085
  char show_keys_buff[15 + NAME_LEN * 2 + 3];
3086
  uint result_length= 0;
3087
  char *result= 0;
3088
  char buff[NAME_LEN * 2 + 3];
3089
  char *quoted_field;
3090
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
3091
  snprintf(show_keys_buff, sizeof(show_keys_buff),
3092
           "SHOW KEYS FROM %s", table_name);
206.3.1 by Patrick Galbraith
Most everything working with client rename
3093
  if (drizzle_query(drizzle, show_keys_buff) ||
3094
      !(res= drizzle_store_result(drizzle)))
1 by brian
clean slate
3095
  {
3096
    fprintf(stderr, "Warning: Couldn't read keys from table %s;"
3097
            " records are NOT sorted (%s)\n",
206.3.1 by Patrick Galbraith
Most everything working with client rename
3098
            table_name, drizzle_error(drizzle));
1 by brian
clean slate
3099
    /* Don't exit, because it's better to print out unsorted records */
3100
    goto cleanup;
3101
  }
3102
3103
  /*
3104
   * Figure out the length of the ORDER BY clause result.
3105
   * Note that SHOW KEYS is ordered:  a PRIMARY key is always the first
3106
   * row, and UNIQUE keys come before others.  So we only need to check
3107
   * the first key, not all keys.
3108
   */
206.3.1 by Patrick Galbraith
Most everything working with client rename
3109
  if ((row= drizzle_fetch_row(res)) && atoi(row[1]) == 0)
1 by brian
clean slate
3110
  {
3111
    /* Key is unique */
3112
    do
3113
    {
3114
      quoted_field= quote_name(row[4], buff, 0);
3115
      result_length+= strlen(quoted_field) + 1; /* + 1 for ',' or \0 */
206.3.1 by Patrick Galbraith
Most everything working with client rename
3116
    } while ((row= drizzle_fetch_row(res)) && atoi(row[3]) > 1);
1 by brian
clean slate
3117
  }
3118
3119
  /* Build the ORDER BY clause result */
3120
  if (result_length)
3121
  {
3122
    char *end;
3123
    /* result (terminating \0 is already in result_length) */
656.1.20 by Monty Taylor
Removed my_strdup, my_malloc, my_realloc from client/
3124
    result= (char *)malloc(result_length + 10);
1 by brian
clean slate
3125
    if (!result)
3126
    {
3127
      fprintf(stderr, "Error: Not enough memory to store ORDER BY clause\n");
3128
      goto cleanup;
3129
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
3130
    drizzle_data_seek(res, 0);
3131
    row= drizzle_fetch_row(res);
1 by brian
clean slate
3132
    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
3133
    end= strcpy(result, quoted_field) + strlen(quoted_field);
206.3.1 by Patrick Galbraith
Most everything working with client rename
3134
    while ((row= drizzle_fetch_row(res)) && atoi(row[3]) > 1)
1 by brian
clean slate
3135
    {
3136
      quoted_field= quote_name(row[4], buff, 0);
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
3137
      end+= sprintf(end,",%s",quoted_field);
1 by brian
clean slate
3138
    }
3139
  }
3140
3141
cleanup:
3142
  if (res)
206.3.1 by Patrick Galbraith
Most everything working with client rename
3143
    drizzle_free_result(res);
1 by brian
clean slate
3144
3145
  return result;
3146
}
3147
3148
3149
int main(int argc, char **argv)
3150
{
3151
  char bin_log_name[FN_REFLEN];
3152
  int exit_code;
3153
  MY_INIT("mysqldump");
3154
3155
  compatible_mode_normal_str[0]= 0;
206.3.1 by Patrick Galbraith
Most everything working with client rename
3156
  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().
3157
  memset(&ignore_table, 0, sizeof(ignore_table));
1 by brian
clean slate
3158
3159
  exit_code= get_options(&argc, &argv);
3160
  if (exit_code)
3161
  {
53.2.4 by Monty Taylor
Changes so that client/ builds cleanly with no warnings.
3162
    free_resources();
1 by brian
clean slate
3163
    exit(exit_code);
3164
  }
3165
3166
  if (connect_to_db(current_host, current_user, opt_password))
3167
  {
53.2.4 by Monty Taylor
Changes so that client/ builds cleanly with no warnings.
3168
    free_resources();
206.3.1 by Patrick Galbraith
Most everything working with client rename
3169
    exit(EX_DRIZZLEERR);
1 by brian
clean slate
3170
  }
3171
  if (!path)
3172
    write_header(md_result_file, *argv);
3173
206.3.1 by Patrick Galbraith
Most everything working with client rename
3174
  if (opt_slave_data && do_stop_slave_sql(drizzle))
1 by brian
clean slate
3175
    goto err;
3176
3177
  if ((opt_lock_all_tables || opt_master_data) &&
206.3.1 by Patrick Galbraith
Most everything working with client rename
3178
      do_flush_tables_read_lock(drizzle))
1 by brian
clean slate
3179
    goto err;
206.3.1 by Patrick Galbraith
Most everything working with client rename
3180
  if (opt_single_transaction && start_transaction(drizzle))
1 by brian
clean slate
3181
      goto err;
3182
  if (opt_delete_master_logs)
3183
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
3184
    if (drizzle_refresh(drizzle, REFRESH_LOG) ||
3185
        get_bin_log_name(drizzle, bin_log_name, sizeof(bin_log_name)))
1 by brian
clean slate
3186
      goto err;
3187
    flush_logs= 0;
3188
  }
3189
  if (opt_lock_all_tables || opt_master_data)
3190
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
3191
    if (flush_logs && drizzle_refresh(drizzle, REFRESH_LOG))
1 by brian
clean slate
3192
      goto err;
3193
    flush_logs= 0; /* not anymore; that would not be sensible */
3194
  }
3195
  /* Add 'STOP SLAVE to beginning of dump */
3196
  if (opt_slave_apply && add_stop_slave())
3197
    goto err;
206.3.1 by Patrick Galbraith
Most everything working with client rename
3198
  if (opt_master_data && do_show_master_status(drizzle))
3199
    goto err;
3200
  if (opt_slave_data && do_show_slave_status(drizzle))
3201
    goto err;
3202
  if (opt_single_transaction && do_unlock_tables(drizzle)) /* unlock but no commit! */
1 by brian
clean slate
3203
    goto err;
3204
3205
  if (opt_alldbs)
3206
  {
3207
    dump_all_databases();
3208
  }
3209
  else if (argc > 1 && !opt_databases)
3210
  {
3211
    /* Only one database and selected table(s) */
3212
    dump_selected_tables(*argv, (argv + 1), (argc - 1));
3213
  }
3214
  else
3215
  {
3216
    dump_databases(argv);
3217
  }
3218
3219
  /* if --dump-slave , start the slave sql thread */
206.3.1 by Patrick Galbraith
Most everything working with client rename
3220
  if (opt_slave_data && do_start_slave_sql(drizzle))
1 by brian
clean slate
3221
    goto err;
3222
3223
  /* add 'START SLAVE' to end of dump */
3224
  if (opt_slave_apply && add_slave_statements())
3225
    goto err;
3226
3227
  /* ensure dumped data flushed */
3228
  if (md_result_file && fflush(md_result_file))
3229
  {
3230
    if (!first_error)
206.3.1 by Patrick Galbraith
Most everything working with client rename
3231
      first_error= EX_DRIZZLEERR;
1 by brian
clean slate
3232
    goto err;
3233
  }
3234
  /* everything successful, purge the old logs files */
206.3.1 by Patrick Galbraith
Most everything working with client rename
3235
  if (opt_delete_master_logs && purge_bin_logs_to(drizzle, bin_log_name))
1 by brian
clean slate
3236
    goto err;
3237
3238
  /*
3239
    No reason to explicitely COMMIT the transaction, neither to explicitely
3240
    UNLOCK TABLES: these will be automatically be done by the server when we
3241
    disconnect now. Saves some code here, some network trips, adds nothing to
3242
    server.
3243
  */
3244
err:
3245
  dbDisconnect(current_host);
3246
  if (!path)
3247
    write_footer(md_result_file);
3248
  free_resources();
3249
3250
  if (stderror_file)
3251
    fclose(stderror_file);
3252
3253
  return(first_error);
3254
} /* main */