~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.cc

Merge of Jay

Show diffs side-by-side

added added

removed removed

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