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