~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.cc

Remove dead memset call.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Copyright 2000-2008 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
 
2
 * Copyright (C) 2010 Vijay Samuel
2
3
 
3
4
  This program is free software; you can redistribute it and/or modify
4
5
  it under the terms of the GNU General Public License as published by
31
32
 
32
33
#include "client_priv.h"
33
34
#include <string>
34
 
 
 
35
#include <iostream>
35
36
#include "drizzled/internal/my_sys.h"
36
37
#include "drizzled/internal/m_string.h"
37
38
#include "drizzled/charset_info.h"
38
 
#include "drizzled/hash.h"
39
39
#include <stdarg.h>
 
40
#include <boost/unordered_set.hpp>
40
41
#include <algorithm>
41
 
 
 
42
#include <fstream>
42
43
#include <drizzled/gettext.h>
43
 
 
 
44
#include <drizzled/configmake.h>
44
45
#include <drizzled/error.h>
 
46
#include <boost/program_options.hpp>
45
47
 
46
48
using namespace std;
47
49
using namespace drizzled;
 
50
namespace po= boost::program_options;
48
51
 
49
52
/* Exit codes */
50
53
 
74
77
#define IGNORE_INSERT_DELAYED 0x02 /* table doesn't support INSERT DELAYED */
75
78
 
76
79
static void add_load_option(string &str, const char *option,
77
 
                            const char *option_value);
78
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint32_t length,
79
 
                         char **err_pos, uint32_t *err_len);
 
80
                            const string &option_value);
80
81
 
81
 
static void field_escape(string &in, const char *from);
 
82
static void field_escape(string &in, const string &from);
82
83
static bool  verbose= false;
83
 
static bool opt_no_create_info= false;
 
84
static bool opt_no_create_info;
84
85
static bool opt_no_data= false;
85
 
static bool opt_mysql= false;
 
86
static bool use_drizzle_protocol= false;
86
87
static bool quick= true;
87
88
static bool extended_insert= true;
88
89
static bool ignore_errors= false;
101
102
static bool opt_dump_date= true;
102
103
static bool opt_autocommit= false; 
103
104
static bool opt_disable_keys= true;
104
 
static bool opt_xml= false;
105
 
static bool tty_password= false;
 
105
static bool opt_xml;
106
106
static bool opt_single_transaction= false; 
107
 
static bool opt_comments= false;
108
 
static bool opt_compact= false;
 
107
static bool opt_comments;
 
108
static bool opt_compact;
109
109
static bool opt_hex_blob= false;
110
110
static bool opt_order_by_primary=false; 
111
111
static bool opt_ignore= false;
112
112
static bool opt_complete_insert= false;
113
 
static bool opt_drop_database= false;
 
113
static bool opt_drop_database;
114
114
static bool opt_replace_into= false;
115
 
static bool opt_routines= false;
116
115
static bool opt_alltspcs= false;
117
116
static uint32_t show_progress_size= 0;
118
117
static uint64_t total_rows= 0;
119
118
static drizzle_st drizzle;
120
119
static drizzle_con_st dcon;
121
120
static string insert_pat;
122
 
static char  *opt_password= NULL;
123
 
static char *current_user= NULL;
124
 
static char  *current_host= NULL;
125
 
static char *path= NULL;
126
 
static char *fields_terminated= NULL;
127
 
static char *lines_terminated= NULL; 
128
 
static char *enclosed= NULL;
129
 
static char *opt_enclosed= NULL;
130
 
static char *escaped= NULL;
131
 
static char *where= NULL; 
132
121
static char *order_by= NULL;
133
 
static char *opt_compatible_mode_str= NULL;
134
 
static char *err_ptr= NULL;
135
 
static char **defaults_argv= NULL;
136
 
static char compatible_mode_normal_str[255];
137
 
static uint32_t opt_compatible_mode= 0;
138
122
static uint32_t opt_drizzle_port= 0;
139
123
static int first_error= 0;
140
124
static string extended_row;
141
125
FILE *md_result_file= 0;
142
126
FILE *stderror_file= 0;
143
127
 
 
128
const string progname= "drizzledump";
 
129
 
 
130
string password,
 
131
  enclosed,
 
132
  escaped,
 
133
  current_host,
 
134
  opt_enclosed,
 
135
  fields_terminated,
 
136
  path,
 
137
  lines_terminated,
 
138
  current_user,
 
139
  opt_password,
 
140
  opt_protocol,
 
141
  where;
 
142
 
144
143
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
145
144
 
146
 
static const char *compatible_mode_names[]=
147
 
{
148
 
  "MYSQL323", "MYSQL40", "POSTGRESQL", "ORACLE", "MSSQL", "DB2",
149
 
  "MAXDB", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS",
150
 
  "ANSI",
151
 
  NULL
152
 
};
153
 
static TYPELIB compatible_mode_typelib= {array_elements(compatible_mode_names) - 1,
154
 
  "", compatible_mode_names, NULL};
155
 
 
156
 
drizzled::hash_set<string> ignore_table;
157
 
 
158
 
static struct option my_long_options[] =
159
 
{
160
 
  {"all", 'a', "Deprecated. Use --create-options instead.",
161
 
    (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
162
 
    0, 0, 0, 0, 0},
163
 
  {"all-databases", 'A',
164
 
    "Dump all the databases. This will be same as --databases with all databases selected.",
165
 
    (char**) &opt_alldbs, (char**) &opt_alldbs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
166
 
    0, 0},
167
 
  {"all-tablespaces", 'Y',
168
 
    "Dump all the tablespaces.",
169
 
    (char**) &opt_alltspcs, (char**) &opt_alltspcs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
170
 
    0, 0},
171
 
  {"add-drop-database", OPT_DROP_DATABASE, "Add a 'DROP DATABASE' before each create.",
172
 
    (char**) &opt_drop_database, (char**) &opt_drop_database, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
173
 
    0},
174
 
  {"add-drop-table", OPT_DROP, "Add a 'drop table' before each create.",
175
 
    (char**) &opt_drop, (char**) &opt_drop, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0,
176
 
    0},
177
 
  {"allow-keywords", OPT_KEYWORDS,
178
 
    "Allow creation of column names that are keywords.", (char**) &opt_keywords,
179
 
    (char**) &opt_keywords, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
180
 
  {"comments", 'i', "Write additional information.",
181
 
    (char**) &opt_comments, (char**) &opt_comments, 0, GET_BOOL, NO_ARG,
182
 
    1, 0, 0, 0, 0, 0},
183
 
  {"compatible", OPT_COMPATIBLE,
184
 
    "Change the dump to be compatible with a given mode. By default tables are dumped in a format optimized for MySQL. Legal modes are: ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_table_options, no_field_options. One can use several modes separated by commas. Note: Requires DRIZZLE server version 4.1.0 or higher. This option is ignored with earlier server versions.",
185
 
    (char**) &opt_compatible_mode_str, (char**) &opt_compatible_mode_str, 0,
186
 
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
187
 
  {"compact", OPT_COMPACT,
188
 
    "Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs.  Enables options --skip-add-drop-table --no-set-names --skip-disable-keys --skip-add-locks",
189
 
    (char**) &opt_compact, (char**) &opt_compact, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
190
 
    0, 0},
191
 
  {"complete-insert", 'c', "Use complete insert statements.",
192
 
    (char**) &opt_complete_insert, (char**) &opt_complete_insert, 0, GET_BOOL,
193
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
194
 
  {"compress", 'C', "Use compression in server/client protocol.",
195
 
    (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
196
 
    0, 0, 0},
197
 
  {"create-options", OPT_CREATE_OPTIONS,
198
 
    "Include all DRIZZLE specific create options.",
199
 
    (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
200
 
    0, 0, 0, 0, 0},
201
 
  {"databases", 'B',
202
 
    "To dump several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames. 'USE db_name;' will be included in the output.",
203
 
    (char**) &opt_databases, (char**) &opt_databases, 0, GET_BOOL, NO_ARG, 0, 0,
204
 
    0, 0, 0, 0},
205
 
  {"delayed-insert", OPT_DELAYED, "Insert rows with INSERT DELAYED; ",
206
 
    (char**) &opt_delayed, (char**) &opt_delayed, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
207
 
    0, 0},
208
 
  {"disable-keys", 'K',
209
 
    "'ALTER TABLE tb_name DISABLE KEYS; and 'ALTER TABLE tb_name ENABLE KEYS; will be put in the output.", (char**) &opt_disable_keys,
210
 
    (char**) &opt_disable_keys, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
211
 
  {"extended-insert", 'e',
212
 
    "Allows utilization of the new, much faster INSERT syntax.",
213
 
    (char**) &extended_insert, (char**) &extended_insert, 0, GET_BOOL, NO_ARG,
214
 
    1, 0, 0, 0, 0, 0},
215
 
  {"fields-terminated-by", OPT_FTB,
216
 
    "Fields in the textfile are terminated by ...", (char**) &fields_terminated,
217
 
    (char**) &fields_terminated, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
218
 
  {"fields-enclosed-by", OPT_ENC,
219
 
    "Fields in the importfile are enclosed by ...", (char**) &enclosed,
220
 
    (char**) &enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
221
 
  {"fields-optionally-enclosed-by", OPT_O_ENC,
222
 
    "Fields in the i.file are opt. enclosed by ...", (char**) &opt_enclosed,
223
 
    (char**) &opt_enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
224
 
  {"fields-escaped-by", OPT_ESC, "Fields in the i.file are escaped by ...",
225
 
    (char**) &escaped, (char**) &escaped, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
226
 
  {"flush-logs", 'F', "Flush logs file in server before starting dump. "
227
 
    "Note that if you dump many databases at once (using the option "
228
 
      "--databases= or --all-databases), the logs will be flushed for "
229
 
      "each database dumped. The exception is when using --lock-all-tables "
230
 
      "in this case the logs will be flushed only once, corresponding "
231
 
      "to the moment all tables are locked. So if you want your dump and "
232
 
      "the log flush to happen at the same exact moment you should use "
233
 
      "--lock-all-tables or --flush-logs",
234
 
    (char**) &flush_logs, (char**) &flush_logs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
235
 
    0, 0},
236
 
  {"force", 'f', "Continue even if we get an sql-error.",
237
 
    (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG,
238
 
    0, 0, 0, 0, 0, 0},
239
 
  {"help", '?', "Display this help message and exit.", 0, 0, 0, GET_NO_ARG,
240
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
241
 
  {"hex-blob", OPT_HEXBLOB, "Dump binary strings (BINARY, "
242
 
    "VARBINARY, BLOB) in hexadecimal format.",
243
 
    (char**) &opt_hex_blob, (char**) &opt_hex_blob, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
244
 
  {"host", 'h', "Connect to host.", (char**) &current_host,
245
 
    (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
246
 
  {"ignore-table", OPT_IGNORE_TABLE,
247
 
    "Do not dump the specified table. To specify more than one table to ignore, "
248
 
      "use the directive multiple times, once for each table.  Each table must "
249
 
      "be specified with both database and table names, e.g. --ignore-table=database.table",
250
 
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
251
 
  {"insert-ignore", OPT_INSERT_IGNORE, "Insert rows with INSERT IGNORE.",
252
 
    (char**) &opt_ignore, (char**) &opt_ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
253
 
    0, 0},
254
 
  {"lines-terminated-by", OPT_LTB, "Lines in the i.file are terminated by ...",
255
 
    (char**) &lines_terminated, (char**) &lines_terminated, 0, GET_STR,
256
 
    REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
257
 
  {"lock-all-tables", 'x', "Locks all tables across all databases. This "
258
 
    "is achieved by taking a global read lock for the duration of the whole "
259
 
      "dump. Automatically turns --single-transaction and --lock-tables off.",
260
 
    (char**) &opt_lock_all_tables, (char**) &opt_lock_all_tables, 0, GET_BOOL, NO_ARG,
261
 
    0, 0, 0, 0, 0, 0},
262
 
  {"mysql", 'm', N_("Use MySQL Protocol."),
263
 
    (char**) &opt_mysql, (char**) &opt_mysql, 0, GET_BOOL, NO_ARG, 1, 0, 0,
264
 
    0, 0, 0},
265
 
  {"no-autocommit", OPT_AUTOCOMMIT,
266
 
    "Wrap tables with autocommit/commit statements.",
267
 
    (char**) &opt_autocommit, (char**) &opt_autocommit, 0, GET_BOOL, NO_ARG,
268
 
    0, 0, 0, 0, 0, 0},
269
 
  {"no-create-db", 'n',
270
 
    "'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.}.",
271
 
    (char**) &opt_create_db, (char**) &opt_create_db, 0, GET_BOOL, NO_ARG, 0, 0,
272
 
    0, 0, 0, 0},
273
 
  {"no-create-info", 't', "Don't write table creation info.",
274
 
    (char**) &opt_no_create_info, (char**) &opt_no_create_info, 0, GET_BOOL,
275
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
276
 
  {"no-data", 'd', "No row information.", (char**) &opt_no_data,
277
 
    (char**) &opt_no_data, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
278
 
  {"no-set-names", 'N',
279
 
    "Deprecated. Use --skip-set-charset instead.",
280
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
281
 
  {"opt", OPT_OPTIMIZE,
282
 
    "Same as --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. Enabled by default, disable with --skip-opt.",
283
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
284
 
  {"order-by-primary", OPT_ORDER_BY_PRIMARY,
285
 
    "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.",
286
 
    (char**) &opt_order_by_primary, (char**) &opt_order_by_primary, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
287
 
  {"password", 'P',
288
 
    "Password to use when connecting to server. If password is not given it's solicited on the tty.",
289
 
    0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
290
 
  {"port", 'p', "Port number to use for connection.", 
291
 
    0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
292
 
  {"quick", 'q', "Don't buffer query, dump directly to stdout.",
293
 
    (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
294
 
  {"quote-names",'Q', "Quote table and column names with backticks (`).",
295
 
    (char**) &opt_quoted, (char**) &opt_quoted, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
296
 
    0, 0},
297
 
  {"replace", OPT_DRIZZLE_REPLACE_INTO, "Use REPLACE INTO instead of INSERT INTO.",
298
 
    (char**) &opt_replace_into, (char**) &opt_replace_into, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
299
 
    0, 0},
300
 
  {"result-file", 'r',
301
 
    "Direct output to a given file. This option should be used in MSDOS, because it prevents new line '\\n' from being converted to '\\r\\n' (carriage return + line feed).",
302
 
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
303
 
  {"routines", 'R', "Dump stored routines (functions and procedures).",
304
 
    (char**) &opt_routines, (char**) &opt_routines, 0, GET_BOOL,
305
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
306
 
  {"single-transaction", OPT_TRANSACTION,
307
 
    "Creates a consistent snapshot by dumping all tables in a single "
308
 
      "transaction. Works ONLY for tables stored in storage engines which "
309
 
      "support multiversioning (currently only InnoDB does); the dump is NOT "
310
 
      "guaranteed to be consistent for other storage engines. "
311
 
      "While a --single-transaction dump is in process, to ensure a valid "
312
 
      "dump file (correct table contents), no other "
313
 
      "connection should use the following statements: ALTER TABLE, DROP "
314
 
      "TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not "
315
 
      "isolated from them. Option automatically turns off --lock-tables.",
316
 
    (char**) &opt_single_transaction, (char**) &opt_single_transaction, 0,
317
 
    GET_BOOL, NO_ARG,  0, 0, 0, 0, 0, 0},
318
 
  {"dump-date", OPT_DUMP_DATE, "Put a dump date to the end of the output.",
319
 
    (char**) &opt_dump_date, (char**) &opt_dump_date, 0,
320
 
    GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
321
 
  {"skip-opt", OPT_SKIP_OPTIMIZATION,
322
 
    "Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys.",
323
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
324
 
  {"tab",'T',
325
 
    "Creates tab separated textfile for each table to given path. (creates .sql and .txt files). NOTE: This only works if drizzledump is run on the same machine as the drizzled daemon.",
326
 
    (char**) &path, (char**) &path, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
327
 
  {"tables", OPT_TABLES, "Overrides option --databases (-B).",
328
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
329
 
  {"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of rows before each output progress report (requires --verbose)."),
330
 
    (char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
331
 
    10000, 0, 0, 0, 0, 0},
332
 
  {"user", 'u', "User for login if not current user.",
333
 
    (char**) &current_user, (char**) &current_user, 0, GET_STR, REQUIRED_ARG,
334
 
    0, 0, 0, 0, 0, 0},
335
 
  {"verbose", 'v', "Print info about the various stages.",
336
 
    (char**) &verbose, (char**) &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
337
 
  {"version",'V', "Output version information and exit.", 0, 0, 0,
338
 
    GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
339
 
  {"where", 'w', "Dump only selected records; QUOTES mandatory!",
340
 
    (char**) &where, (char**) &where, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
341
 
  {"xml", 'X', "Dump a database as well formed XML.", 0, 0, 0, GET_NO_ARG,
342
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
343
 
  {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
344
 
};
345
 
 
346
 
static const char *load_default_groups[]= { "drizzledump","client",0 };
 
145
boost::unordered_set<string> ignore_table;
347
146
 
348
147
static void maybe_exit(int error);
349
148
static void die(int error, const char* reason, ...);
354
153
                        int string_value);
355
154
static const char* fetch_named_row(drizzle_result_st *result, drizzle_row_t row,
356
155
                                   const char* name);
357
 
static int dump_selected_tables(char *db, char **table_names, int tables);
 
156
static int dump_selected_tables(const string &db, const vector<string> &table_names);
358
157
static int dump_all_tables_in_db(char *db);
359
158
static int init_dumping_tables(char *);
360
159
static int init_dumping(char *, int init_func(char*));
361
 
static int dump_databases(char **);
 
160
static int dump_databases(const vector<string> &db_names);
362
161
static int dump_all_databases(void);
363
162
static char *quote_name(const char *name, char *buff, bool force);
364
163
char check_if_ignore_table(const char *table_name, char *table_type);
372
171
  fmt   format specifier
373
172
  ...   variable number of parameters
374
173
*/
 
174
 
375
175
static void verbose_msg(const char *fmt, ...)
376
176
{
377
177
  va_list args;
399
199
    die(EX_EOF, _("Got errno %d on write"), errno);
400
200
}
401
201
 
402
 
static void print_version(void)
403
 
{
404
 
  printf(_("%s  Drizzle %s libdrizzle %s, for %s-%s (%s)\n"), internal::my_progname,
405
 
         VERSION, drizzle_version(), HOST_VENDOR, HOST_OS, HOST_CPU);
406
 
} /* print_version */
407
 
 
408
 
 
409
 
static void short_usage_sub(void)
410
 
{
411
 
  printf(_("Usage: %s [OPTIONS] database [tables]\n"), internal::my_progname);
412
 
  printf(_("OR     %s [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]\n"),
413
 
         internal::my_progname);
414
 
  printf(_("OR     %s [OPTIONS] --all-databases [OPTIONS]\n"), internal::my_progname);
415
 
}
416
 
 
417
 
 
418
 
static void usage(void)
419
 
{
420
 
  print_version();
421
 
  puts("");
422
 
  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"));
423
 
  puts(_("Dumps definitions and data from a Drizzle database server"));
424
 
  short_usage_sub();
425
 
  internal::print_defaults("drizzle",load_default_groups);
426
 
  my_print_help(my_long_options);
427
 
  my_print_variables(my_long_options);
428
 
} /* usage */
429
 
 
430
 
 
431
 
static void short_usage(void)
432
 
{
433
 
  short_usage_sub();
434
 
  printf(_("For more options, use %s --help\n"), internal::my_progname);
435
 
}
436
 
 
437
202
static void write_header(FILE *sql_file, char *db_name)
438
203
{
439
204
  if (opt_xml)
449
214
    fputs(">\n", sql_file);
450
215
    check_io(sql_file);
451
216
  }
452
 
  else if (!opt_compact)
453
 
  {
 
217
  else if (! opt_compact)
 
218
  { 
454
219
    if (opt_comments)
455
220
    {
456
221
      fprintf(sql_file,
457
222
              "-- drizzledump %s libdrizzle %s, for %s-%s (%s)\n--\n",
458
223
              VERSION, drizzle_version(), HOST_VENDOR, HOST_OS, HOST_CPU);
459
224
      fprintf(sql_file, "-- Host: %s    Database: %s\n",
460
 
              current_host ? current_host : "localhost", db_name ? db_name :
 
225
              ! current_host.empty() ? current_host.c_str() : "localhost", db_name ? db_name :
461
226
              "");
462
227
      fputs("-- ------------------------------------------------------\n",
463
228
            sql_file);
468
233
      fprintf(sql_file,
469
234
              "\nSET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;\n");
470
235
 
471
 
    if (path == NULL)
 
236
    if (path.empty())
472
237
    {
473
238
      fprintf(md_result_file,"SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;\nSET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;\n");
474
239
    }
484
249
    fputs("</drizzledump>\n", sql_file);
485
250
    check_io(sql_file);
486
251
  }
487
 
  else if (opt_compact == false)
 
252
  else if (! opt_compact)
488
253
  {
489
 
    if (path == NULL)
 
254
    if (path.empty())
490
255
    {
491
256
      fprintf(md_result_file,"SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;\nSET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;\n");
492
257
    }
508
273
  }
509
274
} /* write_footer */
510
275
 
511
 
 
512
 
static int get_one_option(int optid, const struct option *, char *argument)
513
 
{
514
 
  char *endchar= NULL;
515
 
  uint64_t temp_drizzle_port= 0;
516
 
 
517
 
  switch (optid) {
518
 
  case 'p':
519
 
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
520
 
    /* if there is an alpha character this is not a valid port */
521
 
    if (strlen(endchar) != 0)
522
 
    {
523
 
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
524
 
      return EXIT_ARGUMENT_INVALID;
525
 
    }
526
 
    /* If the port number is > 65535 it is not a valid port
527
 
     *        This also helps with potential data loss casting unsigned long to a
528
 
     *               uint32_t. */
529
 
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
530
 
    {
531
 
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
532
 
      return EXIT_ARGUMENT_INVALID;
533
 
    }
534
 
    else
535
 
    {
536
 
      opt_drizzle_port= (uint32_t) temp_drizzle_port;
537
 
    }
538
 
    break;
539
 
  case 'P':
540
 
    if (argument)
541
 
    {
542
 
      char *start= argument;
543
 
      if (opt_password)
544
 
        free(opt_password);
545
 
      opt_password= strdup(argument);
546
 
      if (opt_password == NULL)
547
 
      {
548
 
        fprintf(stderr, _("Memory allocation error while copying password. "
549
 
                          "Aborting.\n"));
550
 
        return EXIT_OUT_OF_MEMORY;
551
 
      }
552
 
      while (*argument)
553
 
      {
554
 
        /* Overwriting password with 'x' */
555
 
        *argument++= 'x';
556
 
      }
557
 
      if (*start)
558
 
      {
559
 
        /* Cut length of argument */
560
 
        start[1]= 0;
561
 
      }
562
 
      tty_password= 0;
563
 
    }
564
 
    else
565
 
    {
566
 
      tty_password= 1;
567
 
    }
568
 
    break;
569
 
  case 'r':
570
 
    if (!(md_result_file= fopen(argument, "w")))
571
 
      exit(1);
572
 
    break;
573
 
  case 'N':
574
 
    opt_set_charset= 0;
575
 
    break;
576
 
  case 'T':
577
 
    opt_disable_keys=0;
578
 
 
579
 
    if (strlen(argument) >= FN_REFLEN)
580
 
    {
581
 
      /*
582
 
        This check is made because the some the file functions below
583
 
        have FN_REFLEN sized stack allocated buffers and will cause
584
 
        a crash even if the input destination buffer is large enough
585
 
        to hold the output.
586
 
      */
587
 
      fprintf(stderr, _("Input filename too long: %s"), argument);
588
 
      return EXIT_ARGUMENT_INVALID;
589
 
    }
590
 
 
591
 
    break;
592
 
  case 'V': print_version(); exit(0);
593
 
  case 'X':
594
 
            opt_xml= 1;
595
 
            extended_insert= opt_drop=
596
 
              opt_disable_keys= opt_autocommit= opt_create_db= 0;
597
 
            break;
598
 
  case 'I':
599
 
  case '?':
600
 
            usage();
601
 
            exit(0);
602
 
  case (int) OPT_OPTIMIZE:
603
 
            extended_insert= opt_drop= quick= create_options=
604
 
              opt_disable_keys= opt_set_charset= 1;
605
 
            break;
606
 
  case (int) OPT_SKIP_OPTIMIZATION:
607
 
            extended_insert= opt_drop= quick= create_options=
608
 
              opt_disable_keys= opt_set_charset= 0;
609
 
            break;
610
 
  case (int) OPT_COMPACT:
611
 
            if (opt_compact)
612
 
            {
613
 
              opt_comments= opt_drop= opt_disable_keys= 0;
614
 
              opt_set_charset= 0;
615
 
            }
616
 
  case (int) OPT_TABLES:
617
 
            opt_databases=0;
618
 
            break;
619
 
  case (int) OPT_IGNORE_TABLE:
620
 
            {
621
 
              if (!strchr(argument, '.'))
622
 
              {
623
 
                fprintf(stderr, _("Illegal use of option --ignore-table=<database>.<table>\n"));
624
 
                return EXIT_ARGUMENT_INVALID;
625
 
              }
626
 
              string tmpptr(argument);
627
 
              ignore_table.insert(tmpptr); 
628
 
              break;
629
 
            }
630
 
  case (int) OPT_COMPATIBLE:
631
 
            {
632
 
              char buff[255];
633
 
              char *end= compatible_mode_normal_str;
634
 
              uint32_t i;
635
 
              uint32_t mode;
636
 
              uint32_t error_len;
637
 
 
638
 
              opt_quoted= 1;
639
 
              opt_set_charset= 0;
640
 
              opt_compatible_mode_str= argument;
641
 
              opt_compatible_mode= find_set(&compatible_mode_typelib,
642
 
                                            argument, strlen(argument),
643
 
                                            &err_ptr, &error_len);
644
 
              if (error_len)
645
 
              {
646
 
                strncpy(buff, err_ptr, min((uint32_t)sizeof(buff), error_len+1));
647
 
                fprintf(stderr, _("Invalid mode to --compatible: %s\n"), buff);
648
 
                return EXIT_ARGUMENT_INVALID;
649
 
              }
650
 
              mode= opt_compatible_mode;
651
 
              for (i= 0, mode= opt_compatible_mode; mode; mode>>= 1, i++)
652
 
              {
653
 
                if (mode & 1)
654
 
                {
655
 
                  uint32_t len = strlen(compatible_mode_names[i]);
656
 
                  end= strcpy(end, compatible_mode_names[i]) + len;
657
 
                  end= strcpy(end, ",")+1;
658
 
                }
659
 
              }
660
 
              if (end!=compatible_mode_normal_str)
661
 
                end[-1]= 0;
662
 
            }
663
 
  }
664
 
  return 0;
665
 
}
666
 
 
667
 
static int get_options(int *argc, char ***argv)
668
 
{
669
 
  int ho_error;
670
 
 
671
 
  md_result_file= stdout;
672
 
  internal::load_defaults("drizzle",load_default_groups,argc,argv);
673
 
  defaults_argv= *argv;
674
 
 
675
 
  if ((ho_error= handle_options(argc, argv, my_long_options, get_one_option)))
676
 
    return(ho_error);
677
 
 
678
 
  if (!path && (enclosed || opt_enclosed || escaped || lines_terminated ||
679
 
                fields_terminated))
 
276
static int get_options(void)
 
277
{
 
278
 
 
279
  if (path.empty() && (! enclosed.empty() || ! opt_enclosed.empty() || ! escaped.empty() || ! lines_terminated.empty() ||
 
280
                ! fields_terminated.empty()))
680
281
  {
681
282
    fprintf(stderr,
682
 
            _("%s: You must use option --tab with --fields-...\n"), internal::my_progname);
 
283
            _("%s: You must use option --tab with --fields-...\n"), progname.c_str());
683
284
    return(EX_USAGE);
684
285
  }
685
286
 
686
287
  if (opt_single_transaction && opt_lock_all_tables)
687
288
  {
688
289
    fprintf(stderr, _("%s: You can't use --single-transaction and "
689
 
                      "--lock-all-tables at the same time.\n"), internal::my_progname);
 
290
                      "--lock-all-tables at the same time.\n"), progname.c_str());
690
291
    return(EX_USAGE);
691
292
  }
692
 
  if (enclosed && opt_enclosed)
 
293
  if (! enclosed.empty() && ! opt_enclosed.empty())
693
294
  {
694
 
    fprintf(stderr, _("%s: You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n"), internal::my_progname);
 
295
    fprintf(stderr, _("%s: You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n"), progname.c_str());
695
296
    return(EX_USAGE);
696
297
  }
697
 
  if ((opt_databases || opt_alldbs) && path)
 
298
  if ((opt_databases || opt_alldbs) && ! path.empty())
698
299
  {
699
300
    fprintf(stderr,
700
301
            _("%s: --databases or --all-databases can't be used with --tab.\n"),
701
 
            internal::my_progname);
 
302
            progname.c_str());
702
303
    return(EX_USAGE);
703
304
  }
704
 
  if ((*argc < 1 && !opt_alldbs) || (*argc > 0 && opt_alldbs))
705
 
  {
706
 
    short_usage();
707
 
    return EX_USAGE;
708
 
  }
 
305
 
709
306
  if (tty_password)
710
307
    opt_password=client_get_tty_password(NULL);
711
308
  return(0);
755
352
  vsnprintf(buffer, sizeof(buffer), fmt_reason, args);
756
353
  va_end(args);
757
354
 
758
 
  fprintf(stderr, "%s: %s\n", internal::my_progname, buffer);
 
355
  fprintf(stderr, "%s: %s\n", progname.c_str(), buffer);
759
356
  fflush(stderr);
760
357
 
761
358
  ignore_errors= 0; /* force the exit */
789
386
  vsnprintf(buffer, sizeof(buffer), fmt_reason, args);
790
387
  va_end(args);
791
388
 
792
 
  fprintf(stderr, "%s: %s\n", internal::my_progname, buffer);
 
389
  fprintf(stderr, "%s: %s\n", progname.c_str(), buffer);
793
390
  fflush(stderr);
794
391
 
795
392
  maybe_exit(error_num);
868
465
{
869
466
  FILE* res;
870
467
  char filename[FN_REFLEN], tmp_path[FN_REFLEN];
871
 
  internal::convert_dirname(tmp_path,path,NULL);
 
468
  internal::convert_dirname(tmp_path,(char *)path.c_str(),NULL);
872
469
  res= fopen(internal::fn_format(filename, table, tmp_path, ".sql", 4), "w");
873
470
 
874
471
  return res;
879
476
{
880
477
  if (md_result_file && md_result_file != stdout)
881
478
    fclose(md_result_file);
882
 
  free(opt_password);
883
 
  if (defaults_argv)
884
 
    internal::free_defaults(defaults_argv);
 
479
  opt_password.erase();
885
480
  internal::my_end();
886
481
}
887
482
 
903
498
  db_connect -- connects to the host and selects DB.
904
499
*/
905
500
 
906
 
static int connect_to_db(char *host, char *user,char *passwd)
 
501
static int connect_to_db(string host, string user,string passwd)
907
502
{
908
503
  drizzle_return_t ret;
909
504
 
910
 
  verbose_msg(_("-- Connecting to %s...\n"), host ? host : "localhost");
 
505
  verbose_msg(_("-- Connecting to %s, using protocol %s...\n"), ! host.empty() ? (char *)host.c_str() : "localhost", opt_protocol.c_str());
911
506
  drizzle_create(&drizzle);
912
507
  drizzle_con_create(&drizzle, &dcon);
913
 
  drizzle_con_set_tcp(&dcon, host, opt_drizzle_port);
914
 
  drizzle_con_set_auth(&dcon, user, passwd);
915
 
  if (opt_mysql)
916
 
    drizzle_con_add_options(&dcon, DRIZZLE_CON_MYSQL);
 
508
  drizzle_con_set_tcp(&dcon, (char *)host.c_str(), opt_drizzle_port);
 
509
  drizzle_con_set_auth(&dcon, (char *)user.c_str(), (char *)passwd.c_str());
 
510
  drizzle_con_add_options(&dcon, use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
917
511
  ret= drizzle_con_connect(&dcon);
918
512
  if (ret != DRIZZLE_RETURN_OK)
919
513
  {
928
522
/*
929
523
 ** dbDisconnect -- disconnects from the host.
930
524
*/
931
 
static void dbDisconnect(char *host)
 
525
static void dbDisconnect(string &host)
932
526
{
933
 
  verbose_msg(_("-- Disconnecting from %s...\n"), host ? host : "localhost");
 
527
  verbose_msg(_("-- Disconnecting from %s...\n"), ! host.empty() ? host.c_str() : "localhost");
934
528
  drizzle_con_free(&dcon);
935
529
  drizzle_free(&drizzle);
936
530
} /* dbDisconnect */
1310
904
    order_by= primary_key_fields(result_table);
1311
905
  }
1312
906
 
1313
 
  if (!opt_xml)
1314
 
  {
 
907
  if (! opt_xml)
 
908
  { 
1315
909
    /* using SHOW CREATE statement */
1316
 
    if (!opt_no_create_info)
1317
 
    {
 
910
    if (! opt_no_create_info)
 
911
    { 
1318
912
      /* Make an sql-file, if path was given iow. option -T was given */
1319
913
      char buff[20+FN_REFLEN];
1320
914
      const drizzle_column_st *column;
1324
918
      if (drizzleclient_query_with_error_report(&dcon, &result, buff, false))
1325
919
        return false;
1326
920
 
1327
 
      if (path)
 
921
      if (! path.empty())
1328
922
      {
1329
923
        if (!(sql_file= open_sql_file_for_table(table)))
1330
924
        {
1341
935
        check_io(sql_file);
1342
936
      }
1343
937
      if (opt_drop)
1344
 
      {
 
938
      { 
1345
939
        /*
1346
940
          Even if the "table" is a view, we do a DROP TABLE here.
1347
941
        */
1364
958
 
1365
959
    if (drizzleclient_query_with_error_report(&dcon, &result, query_buff, false))
1366
960
    {
1367
 
      if (path)
 
961
      if (! path.empty())
1368
962
        fclose(sql_file);
1369
963
      return false;
1370
964
    }
1414
1008
  else
1415
1009
  {
1416
1010
    verbose_msg(_("%s: Warning: Can't set SQL_QUOTE_SHOW_CREATE option (%s)\n"),
1417
 
                internal::my_progname, drizzle_con_error(&dcon));
 
1011
                progname.c_str(), drizzle_con_error(&dcon));
1418
1012
 
1419
1013
    snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1420
1014
             result_table);
1422
1016
      return false;
1423
1017
 
1424
1018
    /* Make an sql-file, if path was given iow. option -T was given */
1425
 
    if (!opt_no_create_info)
 
1019
    if (! opt_no_create_info)
1426
1020
    {
1427
 
      if (path)
 
1021
      if (! path.empty())
1428
1022
      {
1429
1023
        if (!(sql_file= open_sql_file_for_table(table)))
1430
1024
        {
1521
1115
      if (drizzleclient_query_with_error_report(&dcon, &result, buff, false))
1522
1116
      {
1523
1117
        fprintf(stderr, _("%s: Can't get keys for table %s\n"),
1524
 
                internal::my_progname, result_table);
1525
 
        if (path)
 
1118
                progname.c_str(), result_table);
 
1119
        if (! path.empty())
1526
1120
          fclose(sql_file);
1527
1121
        return false;
1528
1122
      }
1641
1235
} /* get_table_structure */
1642
1236
 
1643
1237
static void add_load_option(string &str, const char *option,
1644
 
                            const char *option_value)
 
1238
                            const string &option_value)
1645
1239
{
1646
 
  if (!option_value)
 
1240
  if (option_value.empty())
1647
1241
  {
1648
1242
    /* Null value means we don't add this option. */
1649
1243
    return;
1651
1245
 
1652
1246
  str.append(option);
1653
1247
 
1654
 
  if (strncmp(option_value, "0x", sizeof("0x")-1) == 0)
 
1248
  if (option_value.compare(0, 2, "0x") == 0)
1655
1249
  {
1656
1250
    /* It's a hex constant, don't escape */
1657
1251
    str.append(option_value);
1671
1265
  syntax errors from the SQL parser.
1672
1266
*/
1673
1267
 
1674
 
static void field_escape(string &in, const char *from)
 
1268
static void field_escape(string &in, const string &from)
1675
1269
{
1676
1270
  uint32_t end_backslashes= 0;
1677
1271
 
1678
1272
  in.append("'");
1679
1273
 
1680
 
  while (*from)
 
1274
  string::const_iterator it= from.begin();
 
1275
  while (it != from.end())
1681
1276
  {
1682
 
    in.append(from, 1);
 
1277
    in.push_back(*it);
1683
1278
 
1684
 
    if (*from == '\\')
1685
 
      end_backslashes^=1;    /* find odd number of backslashes */
 
1279
    if (*it == '\\')
 
1280
      end_backslashes^= 1;    /* find odd number of backslashes */
1686
1281
    else
1687
1282
    {
1688
 
      if (*from == '\'' && !end_backslashes)
 
1283
      if (*it == '\'' && !end_backslashes)
1689
1284
      {
1690
1285
        /* We want a duplicate of "'" for DRIZZLE */
1691
 
        in.append("\'");
 
1286
        in.push_back('\'');
1692
1287
      }
1693
1288
      end_backslashes=0;
1694
1289
    }
1695
 
    from++;
 
1290
    ++it;
1696
1291
  }
1697
1292
  /* Add missing backslashes if user has specified odd number of backs.*/
1698
1293
  if (end_backslashes)
1779
1374
  query_string.clear();
1780
1375
  query_string.reserve(1024);
1781
1376
 
1782
 
  if (path)
 
1377
  if (! path.empty())
1783
1378
  {
1784
1379
    char filename[FN_REFLEN], tmp_path[FN_REFLEN];
1785
1380
 
1787
1382
      Convert the path to native os format
1788
1383
      and resolve to the full filepath.
1789
1384
    */
1790
 
    internal::convert_dirname(tmp_path,path,NULL);
 
1385
    internal::convert_dirname(tmp_path,(char *)path.c_str(),NULL);
1791
1386
    internal::my_load_path(tmp_path, tmp_path, NULL);
1792
1387
    internal::fn_format(filename, table, tmp_path, ".txt", MYF(MY_UNPACK_FILENAME));
1793
1388
 
1800
1395
    query_string.append( filename);
1801
1396
    query_string.append( "'");
1802
1397
 
1803
 
    if (fields_terminated || enclosed || opt_enclosed || escaped)
 
1398
    if (! fields_terminated.empty() || ! enclosed.empty() || ! opt_enclosed.empty() || ! escaped.empty())
1804
1399
      query_string.append( " FIELDS");
1805
1400
 
1806
1401
    add_load_option(query_string, " TERMINATED BY ", fields_terminated);
1809
1404
    add_load_option(query_string, " ESCAPED BY ", escaped);
1810
1405
    add_load_option(query_string, " LINES TERMINATED BY ", lines_terminated);
1811
1406
 
1812
 
    query_string.append( " FROM ");
1813
 
    query_string.append( result_table);
 
1407
    query_string.append(" FROM ");
 
1408
    query_string.append(result_table);
1814
1409
 
1815
 
    if (where)
 
1410
    if (! where.empty())
1816
1411
    {
1817
 
      query_string.append( " WHERE ");
1818
 
      query_string.append( where);
 
1412
      query_string.append(" WHERE ");
 
1413
      query_string.append(where);
1819
1414
    }
1820
1415
 
1821
1416
    if (order_by)
1822
1417
    {
1823
 
      query_string.append( " ORDER BY ");
1824
 
      query_string.append( order_by);
 
1418
      query_string.append(" ORDER BY ");
 
1419
      query_string.append(order_by);
1825
1420
    }
1826
1421
 
1827
1422
    if (drizzle_query(&dcon, &result, query_string.c_str(),
1834
1429
    }
1835
1430
    drizzle_result_free(&result);
1836
1431
  }
 
1432
 
1837
1433
  else
1838
1434
  {
1839
1435
    if (!opt_xml && opt_comments)
1846
1442
    query_string.append( "SELECT * FROM ");
1847
1443
    query_string.append( result_table);
1848
1444
 
1849
 
    if (where)
 
1445
    if (! where.empty())
1850
1446
    {
1851
1447
      if (!opt_xml && opt_comments)
1852
1448
      {
1853
 
        fprintf(md_result_file, "-- WHERE:  %s\n", where);
 
1449
        fprintf(md_result_file, "-- WHERE:  %s\n", where.c_str());
1854
1450
        check_io(md_result_file);
1855
1451
      }
1856
1452
 
1857
1453
      query_string.append( " WHERE ");
1858
 
      query_string.append( where);
 
1454
      query_string.append( (char *)where.c_str());
1859
1455
    }
1860
1456
    if (order_by)
1861
1457
    {
1883
1479
    if (drizzle_result_column_count(&result) != num_fields)
1884
1480
    {
1885
1481
      fprintf(stderr,_("%s: Error in field count for table: %s !  Aborting.\n"),
1886
 
              internal::my_progname, result_table);
 
1482
              progname.c_str(), result_table);
1887
1483
      error= EX_CONSCHECK;
1888
1484
      drizzle_result_free(&result);
1889
1485
      goto err;
1896
1492
              opt_quoted_table);
1897
1493
      check_io(md_result_file);
1898
1494
    }
1899
 
 
 
1495
    
1900
1496
    total_length= DRIZZLE_MAX_LINE_LENGTH;                /* Force row break */
1901
1497
    row_break=0;
1902
1498
    rownr=0;
1927
1523
        {
1928
1524
          fprintf(stderr,
1929
1525
                  _("%s: Error reading rows for table: %s (%d:%s) ! Aborting.\n"),
1930
 
                  internal::my_progname, result_table, ret, drizzle_con_error(&dcon));
 
1526
                  progname.c_str(), result_table, ret, drizzle_con_error(&dcon));
1931
1527
          drizzle_result_free(&result);
1932
1528
          goto err;
1933
1529
        }
2216
1812
/* dump_all_databases */
2217
1813
 
2218
1814
 
2219
 
static int dump_databases(char **db_names)
 
1815
static int dump_databases(const vector<string> &db_names)
2220
1816
{
2221
1817
  int result=0;
2222
 
  char **db;
2223
 
 
2224
 
 
2225
 
  for (db= db_names ; *db ; db++)
 
1818
  string temp;
 
1819
  for (vector<string>::const_iterator it= db_names.begin(); it != db_names.end(); ++it)
2226
1820
  {
2227
 
    if (dump_all_tables_in_db(*db))
 
1821
    temp= *it;
 
1822
    if (dump_all_tables_in_db((char *)temp.c_str()))
2228
1823
      result=1;
2229
1824
  }
2230
1825
  return(result);
2296
1891
{
2297
1892
  drizzle_result_st result;
2298
1893
  drizzle_return_t ret;
2299
 
 
2300
 
  if (!my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
2301
 
    return 1;
 
1894
  char qbuf[512];
 
1895
  
 
1896
  /* If this DB contains non-standard tables we don't want it */
 
1897
 
 
1898
  snprintf(qbuf, sizeof(qbuf), "SELECT TABLE_NAME FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='%s' AND TABLE_TYPE != 'STANDARD'", database);
 
1899
  
 
1900
  if (drizzle_query_str(&dcon, &result, qbuf, &ret) != NULL)
 
1901
  {
 
1902
    drizzle_result_buffer(&result);
 
1903
    if (drizzle_result_row_count(&result) > 0)
 
1904
    {
 
1905
      drizzle_result_free(&result);
 
1906
      return 1;
 
1907
    }
 
1908
  }
 
1909
 
 
1910
  drizzle_result_free(&result);
2302
1911
 
2303
1912
  if (drizzle_select_db(&dcon, &result, database, &ret) == NULL ||
2304
1913
      ret != DRIZZLE_RETURN_OK)
2308
1917
  }
2309
1918
  drizzle_result_free(&result);
2310
1919
 
2311
 
  if (!path && !opt_xml)
 
1920
  if (path.empty() && !opt_xml)
2312
1921
  {
2313
1922
    if (opt_databases || opt_alldbs)
2314
1923
    {
2341
1950
static bool include_table(const char *hash_key, size_t key_size)
2342
1951
{
2343
1952
  string match(hash_key, key_size);
2344
 
  drizzled::hash_set<string>::iterator iter= ignore_table.find(match);
 
1953
  boost::unordered_set<string>::iterator iter= ignore_table.find(match);
2345
1954
  return (iter == ignore_table.end());
2346
1955
}
2347
1956
 
2433
2042
    */
2434
2043
    row= drizzle_row_next(&result);
2435
2044
    lengths= drizzle_row_field_sizes(&result);
2436
 
    name= strmake_root(root, row[0], lengths[0]);
 
2045
    name= root->strmake_root(row[0], lengths[0]);
2437
2046
  }
2438
2047
  drizzle_result_free(&result);
2439
2048
 
2441
2050
}
2442
2051
 
2443
2052
 
2444
 
static int dump_selected_tables(char *db, char **table_names, int tables)
 
2053
static int dump_selected_tables(const string &db, const vector<string> &table_names)
2445
2054
{
2446
2055
  drizzled::memory::Root root;
2447
2056
  char **dump_tables, **pos, **end;
2449
2058
  drizzle_return_t ret;
2450
2059
 
2451
2060
 
2452
 
  if (init_dumping(db, init_dumping_tables))
 
2061
  if (init_dumping((char *)db.c_str(), init_dumping_tables))
2453
2062
    return(1);
2454
2063
 
2455
 
  init_alloc_root(&root, 8192);
2456
 
  if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *))))
 
2064
  root.init_alloc_root(8192);
 
2065
  if (!(dump_tables= pos= (char**) root.alloc_root(table_names.size() * sizeof(char *))))
2457
2066
    die(EX_EOM, _("alloc_root failure."));
2458
2067
 
2459
 
  for (; tables > 0 ; tables-- , table_names++)
 
2068
  for (vector<string>::const_iterator it= table_names.begin(); it != table_names.end(); ++it)
2460
2069
  {
 
2070
    string temp= *it;
2461
2071
    /* the table name passed on commandline may be wrong case */
2462
 
    if ((*pos= get_actual_table_name(*table_names, &root)))
 
2072
    if ((*pos= get_actual_table_name(temp.c_str(), &root)))
2463
2073
    {
2464
2074
      pos++;
2465
2075
    }
2467
2077
    {
2468
2078
      if (!ignore_errors)
2469
2079
      {
2470
 
        free_root(&root, MYF(0));
 
2080
        root.free_root(MYF(0));
2471
2081
      }
2472
 
      maybe_die(EX_ILLEGAL_TABLE, _("Couldn't find table: \"%s\""), *table_names);
 
2082
      maybe_die(EX_ILLEGAL_TABLE, _("Couldn't find table: \"%s\""),(char *) temp.c_str());
2473
2083
      /* We shall countinue here, if --force was given */
2474
2084
    }
2475
2085
  }
2481
2091
        ret != DRIZZLE_RETURN_OK)
2482
2092
    {
2483
2093
      if (!ignore_errors)
2484
 
        free_root(&root, MYF(0));
 
2094
        root.free_root(MYF(0));
2485
2095
      DB_error(&result, ret, _("when doing refresh"));
2486
2096
      /* We shall countinue here, if --force was given */
2487
2097
    }
2489
2099
      drizzle_result_free(&result);
2490
2100
  }
2491
2101
  if (opt_xml)
2492
 
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NULL);
 
2102
    print_xml_tag(md_result_file, "", "\n", "database", "name=", (char *)db.c_str(), NULL);
2493
2103
 
2494
2104
  /* Dump each selected table */
2495
2105
  for (pos= dump_tables; pos < end; pos++)
2496
 
    dump_table(*pos, db);
 
2106
    dump_table(*pos, (char *)db.c_str());
2497
2107
 
2498
 
  free_root(&root, MYF(0));
 
2108
  root.free_root(MYF(0));
2499
2109
  free(order_by);
2500
2110
  order_by= 0;
2501
2111
  if (opt_xml)
2538
2148
}
2539
2149
 
2540
2150
 
2541
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint32_t length,
2542
 
                         char **err_pos, uint32_t *err_len)
2543
 
{
2544
 
  const char *end= x + length;
2545
 
  uint32_t found= 0;
2546
 
  uint32_t find;
2547
 
  char buff[255];
2548
 
 
2549
 
  *err_pos= 0;                  /* No error yet */
2550
 
  while (end > x && my_isspace(charset_info, end[-1]))
2551
 
    end--;
2552
 
 
2553
 
  *err_len= 0;
2554
 
  if (x != end)
2555
 
  {
2556
 
    const char *start= x;
2557
 
    for (;;)
2558
 
    {
2559
 
      const char *pos= start;
2560
 
      uint32_t var_len;
2561
 
 
2562
 
      for (; pos != end && *pos != ','; pos++) ;
2563
 
      var_len= (uint32_t) (pos - start);
2564
 
      strncpy(buff, start, min((uint32_t)sizeof(buff), var_len+1));
2565
 
      find= find_type(buff, lib, var_len);
2566
 
      if (!find)
2567
 
      {
2568
 
        *err_pos= (char*) start;
2569
 
        *err_len= var_len;
2570
 
      }
2571
 
      else
2572
 
        found|= (uint32_t)((int64_t) 1 << (find - 1));
2573
 
      if (pos == end)
2574
 
        break;
2575
 
      start= pos + 1;
2576
 
    }
2577
 
  }
2578
 
  return found;
2579
 
}
2580
 
 
2581
 
 
2582
2151
/* Print a value with a prefix on file */
2583
2152
static void print_value(FILE *file, drizzle_result_st  *result, drizzle_row_t row,
2584
2153
                        const char *prefix, const char *name,
2817
2386
  return result;
2818
2387
}
2819
2388
 
2820
 
 
2821
2389
int main(int argc, char **argv)
2822
2390
{
 
2391
try
 
2392
{
2823
2393
  int exit_code;
2824
 
  MY_INIT("drizzledump");
2825
2394
  drizzle_result_st result;
2826
2395
 
2827
 
  compatible_mode_normal_str[0]= 0;
2828
 
 
2829
 
  exit_code= get_options(&argc, &argv);
 
2396
  po::options_description commandline_options(N_("Options used only in command line"));
 
2397
  commandline_options.add_options()
 
2398
  ("all-databases,A", po::value<bool>(&opt_alldbs)->default_value(false)->zero_tokens(),
 
2399
  N_("Dump all the databases. This will be same as --databases with all databases selected."))
 
2400
  ("all-tablespaces,Y", po::value<bool>(&opt_alltspcs)->default_value(false)->zero_tokens(),
 
2401
  N_("Dump all the tablespaces."))
 
2402
  ("complete-insert,c", po::value<bool>(&opt_complete_insert)->default_value(false)->zero_tokens(),
 
2403
  N_("Use complete insert statements."))
 
2404
  ("compress,C", po::value<bool>(&opt_compress)->default_value(false)->zero_tokens(),
 
2405
  N_("Use compression in server/client protocol."))
 
2406
  ("flush-logs,F", po::value<bool>(&flush_logs)->default_value(false)->zero_tokens(),
 
2407
  N_("Flush logs file in server before starting dump. Note that if you dump many databases at once (using the option --databases= or --all-databases), the logs will be flushed for each database dumped. The exception is when using --lock-all-tables in this case the logs will be flushed only once, corresponding to the moment all tables are locked. So if you want your dump and the log flush to happen at the same exact moment you should use --lock-all-tables or --flush-logs"))
 
2408
  ("force,f", po::value<bool>(&ignore_errors)->default_value(false)->zero_tokens(),
 
2409
  N_("Continue even if we get an sql-error."))
 
2410
  ("help,?", N_("Display this help message and exit."))
 
2411
  ("lock-all-tables,x", po::value<bool>(&opt_lock_all_tables)->default_value(false)->zero_tokens(),
 
2412
  N_("Locks all tables across all databases. This is achieved by taking a global read lock for the duration of the whole dump. Automatically turns --single-transaction and --lock-tables off."))
 
2413
  ("order-by-primary", po::value<bool>(&opt_order_by_primary)->default_value(false)->zero_tokens(),
 
2414
  N_("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."))
 
2415
  ("single-transaction", po::value<bool>(&opt_single_transaction)->default_value(false)->zero_tokens(),
 
2416
  N_("Creates a consistent snapshot by dumping all tables in a single transaction. Works ONLY for tables stored in storage engines which support multiversioning (currently only InnoDB does); the dump is NOT guaranteed to be consistent for other storage engines. While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents), no other connection should use the following statements: ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not isolated from them. Option automatically turns off --lock-tables."))
 
2417
  ("opt", N_("Same as --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. Enabled by default, disable with --skip-opt.")) 
 
2418
  ("skip-opt", 
 
2419
  N_("Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys."))    
 
2420
  ("tables", N_("Overrides option --databases (-B)."))
 
2421
  ("show-progress-size", po::value<uint32_t>(&show_progress_size)->default_value(10000),
 
2422
  N_("Number of rows before each output progress report (requires --verbose)."))
 
2423
  ("verbose,v", po::value<bool>(&verbose)->default_value(false)->zero_tokens(),
 
2424
  N_("Print info about the various stages."))
 
2425
  ("version,V", N_("Output version information and exit."))
 
2426
  ("xml,X", N_("Dump a database as well formed XML."))
 
2427
  ("skip-comments", N_("Turn off Comments"))
 
2428
  ("skip-create", N_("Turn off create-options"))
 
2429
  ("skip-extended-insert", N_("Turn off extended-insert"))
 
2430
  ("skip-dump-date",N_( "Turn off dump-date"))
 
2431
  ("no-defaults", N_("Do not read from the configuration files"))
 
2432
  ;
 
2433
 
 
2434
  po::options_description dump_options(N_("Options specific to the drizzle client"));
 
2435
  dump_options.add_options()
 
2436
  ("add-drop-database", po::value<bool>(&opt_drop_database)->default_value(false)->zero_tokens(),
 
2437
  N_("Add a 'DROP DATABASE' before each create."))
 
2438
  ("add-drop-table", po::value<bool>(&opt_drop)->default_value(true)->zero_tokens(),
 
2439
  N_("Add a 'drop table' before each create."))
 
2440
  ("allow-keywords", po::value<bool>(&opt_keywords)->default_value(false)->zero_tokens(),
 
2441
  N_("Allow creation of column names that are keywords."))
 
2442
  ("comments,i", po::value<bool>(&opt_comments)->default_value(true)->zero_tokens(),
 
2443
  N_("Write additional information."))
 
2444
  ("compact", po::value<bool>(&opt_compact)->default_value(false)->zero_tokens(),
 
2445
  N_("Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs.  Enables options --skip-add-drop-table --no-set-names --skip-disable-keys --skip-add-locks"))
 
2446
  ("create-options", po::value<bool>(&create_options)->default_value(true)->zero_tokens(),
 
2447
  N_("Include all DRIZZLE specific create options."))
 
2448
  ("dump-date", po::value<bool>(&opt_dump_date)->default_value(true)->zero_tokens(),
 
2449
  N_("Put a dump date to the end of the output."))
 
2450
  ("databases,B", po::value<bool>(&opt_databases)->default_value(false)->zero_tokens(),
 
2451
  N_("To dump several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames. 'USE db_name;' will be included in the output."))
 
2452
  ("delayed-insert", po::value<bool>(&opt_delayed)->default_value(false)->zero_tokens(),
 
2453
  N_("Insert rows with INSERT DELAYED;"))
 
2454
  ("disable-keys,K", po::value<bool>(&opt_disable_keys)->default_value(true)->zero_tokens(),
 
2455
  N_("'ALTER TABLE tb_name DISABLE KEYS; and 'ALTER TABLE tb_name ENABLE KEYS; will be put in the output."))
 
2456
  ("extended-insert,e", po::value<bool>(&extended_insert)->default_value(true)->zero_tokens(),
 
2457
  N_("Allows utilization of the new, much faster INSERT syntax."))
 
2458
  ("fields-terminated-by", po::value<string>(&fields_terminated)->default_value(""),
 
2459
  N_("Fields in the textfile are terminated by ..."))
 
2460
  ("fields-enclosed-by", po::value<string>(&enclosed)->default_value(""),
 
2461
  N_("Fields in the importfile are enclosed by ..."))
 
2462
  ("fields-optionally-enclosed-by", po::value<string>(&opt_enclosed)->default_value(""),
 
2463
  N_("Fields in the i.file are opt. enclosed by ..."))
 
2464
  ("fields-escaped-by", po::value<string>(&escaped)->default_value(""),
 
2465
  N_("Fields in the i.file are escaped by ..."))
 
2466
  ("hex-blob", po::value<bool>(&opt_hex_blob)->default_value(false)->zero_tokens(),
 
2467
  "Dump binary strings (BINARY, VARBINARY, BLOB) in hexadecimal format.")
 
2468
  ("ignore-table", po::value<string>(),
 
2469
  N_("Do not dump the specified table. To specify more than one table to ignore, use the directive multiple times, once for each table.  Each table must be specified with both database and table names, e.g. --ignore-table=database.table"))
 
2470
  ("insert-ignore", po::value<bool>(&opt_ignore)->default_value(false)->zero_tokens(),
 
2471
  N_("Insert rows with INSERT IGNORE."))
 
2472
  ("lines-terminated-by", po::value<string>(&lines_terminated)->default_value(""),
 
2473
  N_("Lines in the i.file are terminated by ..."))
 
2474
  ("no-autocommit", po::value<bool>(&opt_autocommit)->default_value(false)->zero_tokens(),
 
2475
  N_("Wrap tables with autocommit/commit statements."))
 
2476
  ("no-create-db,n", po::value<bool>(&opt_create_db)->default_value(false)->zero_tokens(),
 
2477
  N_("'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."))
 
2478
  ("no-create-info,t", po::value<bool>(&opt_no_create_info)->default_value(false)->zero_tokens(),
 
2479
  N_("Don't write table creation info."))
 
2480
  ("no-data,d", po::value<bool>(&opt_no_data)->default_value(false)->zero_tokens(),
 
2481
  N_("No row information."))
 
2482
  ("no-set-names,N", N_("Deprecated. Use --skip-set-charset instead."))
 
2483
  ("set-charset", po::value<bool>(&opt_set_charset)->default_value(false)->zero_tokens(),
 
2484
  N_("Enable set-name"))
 
2485
  ("quick,q", po::value<bool>(&quick)->default_value(true)->zero_tokens(),
 
2486
  N_("Don't buffer query, dump directly to stdout."))
 
2487
  ("quote-names,Q", po::value<bool>(&opt_quoted)->default_value(true)->zero_tokens(),
 
2488
  N_("Quote table and column names with backticks (`)."))
 
2489
  ("replace", po::value<bool>(&opt_replace_into)->default_value(false)->zero_tokens(),
 
2490
  N_("Use REPLACE INTO instead of INSERT INTO."))
 
2491
  ("result-file,r", po::value<string>(),
 
2492
  N_("Direct output to a given file. This option should be used in MSDOS, because it prevents new line '\\n' from being converted to '\\r\\n' (carriage return + line feed)."))
 
2493
  ("tab,T", po::value<string>(&path)->default_value(""),
 
2494
  N_("Creates tab separated textfile for each table to given path. (creates .sql and .txt files). NOTE: This only works if drizzledump is run on the same machine as the drizzled daemon."))
 
2495
  ("where,w", po::value<string>(&where)->default_value(""),
 
2496
  N_("Dump only selected records; QUOTES mandatory!"))
 
2497
  ;
 
2498
 
 
2499
  po::options_description client_options(N_("Options specific to the client"));
 
2500
  client_options.add_options()
 
2501
  ("host,h", po::value<string>(&current_host)->default_value("localhost"),
 
2502
  N_("Connect to host."))
 
2503
  ("password,P", po::value<string>(&password)->default_value(PASSWORD_SENTINEL),
 
2504
  N_("Password to use when connecting to server. If password is not given it's solicited on the tty."))
 
2505
  ("port,p", po::value<uint32_t>(&opt_drizzle_port)->default_value(0),
 
2506
  N_("Port number to use for connection."))
 
2507
  ("user,u", po::value<string>(&current_user)->default_value(""),
 
2508
  N_("User for login if not current user."))
 
2509
  ("protocol",po::value<string>(&opt_protocol)->default_value("mysql"),
 
2510
  N_("The protocol of connection (mysql or drizzle)."))
 
2511
  ;
 
2512
 
 
2513
  po::options_description hidden_options(N_("Hidden Options"));
 
2514
  hidden_options.add_options()
 
2515
  ("database-used", po::value<vector<string> >(), N_("Used to select the database"))
 
2516
  ("Table-used", po::value<vector<string> >(), N_("Used to select the tables"))
 
2517
  ;
 
2518
 
 
2519
  po::options_description all_options(N_("Allowed Options + Hidden Options"));
 
2520
  all_options.add(commandline_options).add(dump_options).add(client_options).add(hidden_options);
 
2521
 
 
2522
  po::options_description long_options(N_("Allowed Options"));
 
2523
  long_options.add(commandline_options).add(dump_options).add(client_options);
 
2524
 
 
2525
  std::string system_config_dir_dump(SYSCONFDIR); 
 
2526
  system_config_dir_dump.append("/drizzle/drizzledump.cnf");
 
2527
 
 
2528
  std::string system_config_dir_client(SYSCONFDIR); 
 
2529
  system_config_dir_client.append("/drizzle/client.cnf");
 
2530
 
 
2531
  std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
 
2532
 
 
2533
  po::positional_options_description p;
 
2534
  p.add("database-used", 1);
 
2535
  p.add("Table-used",-1);
 
2536
 
 
2537
  md_result_file= stdout;
 
2538
 
 
2539
  po::variables_map vm;
 
2540
 
 
2541
  po::store(po::command_line_parser(argc, argv).options(all_options).
 
2542
            positional(p).extra_parser(parse_password_arg).run(), vm);
 
2543
 
 
2544
  if (! vm.count("no-defaults"))
 
2545
  {
 
2546
    std::string user_config_dir_dump(user_config_dir);
 
2547
    user_config_dir_dump.append("/drizzle/drizzledump.cnf"); 
 
2548
 
 
2549
    std::string user_config_dir_client(user_config_dir);
 
2550
    user_config_dir_client.append("/drizzle/client.cnf");
 
2551
 
 
2552
    ifstream user_dump_ifs(user_config_dir_dump.c_str());
 
2553
    po::store(parse_config_file(user_dump_ifs, dump_options), vm);
 
2554
 
 
2555
    ifstream user_client_ifs(user_config_dir_client.c_str());
 
2556
    po::store(parse_config_file(user_client_ifs, client_options), vm);
 
2557
 
 
2558
    ifstream system_dump_ifs(system_config_dir_dump.c_str());
 
2559
    po::store(parse_config_file(system_dump_ifs, dump_options), vm);
 
2560
 
 
2561
    ifstream system_client_ifs(system_config_dir_client.c_str());
 
2562
    po::store(parse_config_file(system_client_ifs, client_options), vm);
 
2563
  }
 
2564
 
 
2565
  po::notify(vm);  
 
2566
  
 
2567
  if ((not vm.count("database-used") && not vm.count("Table-used") 
 
2568
    && not opt_alldbs && path.empty())
 
2569
    || (vm.count("help")) || vm.count("version"))
 
2570
  {
 
2571
    printf(_("Drizzledump %s build %s, for %s-%s (%s)\n"),
 
2572
      drizzle_version(), VERSION, HOST_VENDOR, HOST_OS, HOST_CPU);
 
2573
    if (vm.count("version"))
 
2574
      exit(0);
 
2575
    puts("");
 
2576
    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"));
 
2577
    puts(_("Dumps definitions and data from a Drizzle database server"));
 
2578
    printf(_("Usage: %s [OPTIONS] database [tables]\n"), progname.c_str());
 
2579
    printf(_("OR     %s [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]\n"),
 
2580
          progname.c_str());
 
2581
    printf(_("OR     %s [OPTIONS] --all-databases [OPTIONS]\n"), progname.c_str());
 
2582
    cout << long_options;
 
2583
    if (vm.count("help"))
 
2584
      exit(0);
 
2585
    else
 
2586
      exit(1);
 
2587
  }
 
2588
 
 
2589
  if (vm.count("protocol"))
 
2590
  {
 
2591
    std::transform(opt_protocol.begin(), opt_protocol.end(),
 
2592
      opt_protocol.begin(), ::tolower);
 
2593
 
 
2594
    if (not opt_protocol.compare("mysql"))
 
2595
      use_drizzle_protocol=false;
 
2596
    else if (not opt_protocol.compare("drizzle"))
 
2597
      use_drizzle_protocol=true;
 
2598
    else
 
2599
    {
 
2600
      cout << _("Error: Unknown protocol") << " '" << opt_protocol << "'" << endl;
 
2601
      exit(-1);
 
2602
    }
 
2603
  }
 
2604
 
 
2605
  if (vm.count("port"))
 
2606
  {
 
2607
    /* If the port number is > 65535 it is not a valid port
 
2608
     *        This also helps with potential data loss casting unsigned long to a
 
2609
     *               uint32_t. 
 
2610
     */
 
2611
    if (opt_drizzle_port > 65535)
 
2612
    {
 
2613
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
2614
      exit(-1);
 
2615
    }
 
2616
  }
 
2617
 
 
2618
  if(vm.count("password"))
 
2619
  {
 
2620
    if (!opt_password.empty())
 
2621
      opt_password.erase();
 
2622
    if (password == PASSWORD_SENTINEL)
 
2623
    {
 
2624
      opt_password= "";
 
2625
    }
 
2626
    else
 
2627
    {
 
2628
      opt_password= password;
 
2629
      tty_password= false;
 
2630
    }
 
2631
  }
 
2632
  else
 
2633
  {
 
2634
      tty_password= true;
 
2635
  }
 
2636
 
 
2637
  if (vm.count("result-file"))
 
2638
  {
 
2639
    if (!(md_result_file= fopen(vm["result-file"].as<string>().c_str(), "w")))
 
2640
      exit(1);
 
2641
  }
 
2642
 
 
2643
  if (vm.count("no-set-names"))
 
2644
  {
 
2645
    opt_set_charset= 0;
 
2646
  }
 
2647
 
 
2648
  if (! path.empty())
 
2649
  { 
 
2650
    opt_disable_keys= 0;
 
2651
 
 
2652
    if (vm["tab"].as<string>().length() >= FN_REFLEN)
 
2653
    {
 
2654
      /*
 
2655
        This check is made because the some the file functions below
 
2656
        have FN_REFLEN sized stack allocated buffers and will cause
 
2657
        a crash even if the input destination buffer is large enough
 
2658
        to hold the output.
 
2659
      */
 
2660
      fprintf(stderr, _("Input filename too long: %s"), vm["tab"].as<string>().c_str());
 
2661
      exit(EXIT_ARGUMENT_INVALID);
 
2662
    }
 
2663
  }
 
2664
 
 
2665
  if (vm.count("xml"))
 
2666
  { 
 
2667
    opt_xml= 1;
 
2668
    extended_insert= opt_drop= opt_disable_keys= opt_autocommit= opt_create_db= 0;
 
2669
  }
 
2670
  
 
2671
  if (vm.count("skip-opt"))
 
2672
  {
 
2673
    extended_insert= opt_drop= quick= create_options= 0;
 
2674
    opt_disable_keys= opt_set_charset= 0;
 
2675
  }
 
2676
 
 
2677
  if (opt_compact)
 
2678
  { 
 
2679
    opt_comments= opt_drop= opt_disable_keys= 0;
 
2680
    opt_set_charset= 0;
 
2681
  }
 
2682
 
 
2683
  if (vm.count("opt"))
 
2684
  {
 
2685
    extended_insert= opt_drop= quick= create_options= 1;
 
2686
    opt_disable_keys= opt_set_charset= 1;
 
2687
  }
 
2688
 
 
2689
  if (vm.count("tables"))
 
2690
  { 
 
2691
    opt_databases= false;
 
2692
  }
 
2693
 
 
2694
  if (vm.count("ignore-table"))
 
2695
  {
 
2696
    if (!strchr(vm["ignore-table"].as<string>().c_str(), '.'))
 
2697
    {
 
2698
      fprintf(stderr, _("Illegal use of option --ignore-table=<database>.<table>\n"));
 
2699
      exit(EXIT_ARGUMENT_INVALID);
 
2700
    }
 
2701
    string tmpptr(vm["ignore-table"].as<string>());
 
2702
    ignore_table.insert(tmpptr); 
 
2703
  }
 
2704
  
 
2705
  if (vm.count("skip-create"))
 
2706
  {
 
2707
    opt_create_db= opt_no_create_info= create_options= false;
 
2708
  }
 
2709
 
 
2710
  if (vm.count("skip-comments"))
 
2711
  {
 
2712
    opt_comments= false; 
 
2713
  }
 
2714
 
 
2715
  if (vm.count("skip-extended-insert"))
 
2716
  {
 
2717
    extended_insert= false; 
 
2718
  }
 
2719
 
 
2720
  if (vm.count("skip-dump-date"))
 
2721
  {
 
2722
    opt_dump_date= false; 
 
2723
  } 
 
2724
 
 
2725
  exit_code= get_options();
2830
2726
  if (exit_code)
2831
2727
  {
2832
2728
    free_resources();
2838
2734
    free_resources();
2839
2735
    exit(EX_DRIZZLEERR);
2840
2736
  }
2841
 
  if (!path)
2842
 
    write_header(md_result_file, *argv);
 
2737
  if (path.empty() && vm.count("database-used"))
 
2738
  {
 
2739
    string database_used= *vm["database-used"].as< vector<string> >().begin();
 
2740
    write_header(md_result_file, (char *)database_used.c_str());
 
2741
  }
2843
2742
 
2844
2743
  if ((opt_lock_all_tables) && do_flush_tables_read_lock(&dcon))
2845
2744
    goto err;
2859
2758
  {
2860
2759
    dump_all_databases();
2861
2760
  }
2862
 
  else if (argc > 1 && !opt_databases)
 
2761
  if (vm.count("database-used") && vm.count("Table-used") && ! opt_databases)
2863
2762
  {
 
2763
    string database_used= *vm["database-used"].as< vector<string> >().begin();
2864
2764
    /* Only one database and selected table(s) */
2865
 
    dump_selected_tables(*argv, (argv + 1), (argc - 1));
2866
 
  }
2867
 
  else
2868
 
  {
2869
 
    dump_databases(argv);
 
2765
    dump_selected_tables(database_used, vm["Table-used"].as< vector<string> >());
 
2766
  }
 
2767
 
 
2768
  if (vm.count("Table-used") && opt_databases)
 
2769
  {
 
2770
    vector<string> database_used= vm["database-used"].as< vector<string> >();
 
2771
    vector<string> table_used= vm["Table-used"].as< vector<string> >();
 
2772
 
 
2773
    for (vector<string>::iterator it= table_used.begin();
 
2774
       it != table_used.end();
 
2775
       ++it)
 
2776
    {
 
2777
      database_used.insert(database_used.end(), *it);
 
2778
    }
 
2779
    dump_databases(database_used);
 
2780
  }
 
2781
  
 
2782
  if (vm.count("database-used") && ! vm.count("Table-used"))
 
2783
  {
 
2784
    dump_databases(vm["database-used"].as< vector<string> >());
2870
2785
  }
2871
2786
 
2872
2787
  /* ensure dumped data flushed */
2885
2800
  */
2886
2801
err:
2887
2802
  dbDisconnect(current_host);
2888
 
  if (!path)
 
2803
  if (path.empty())
2889
2804
    write_footer(md_result_file);
2890
2805
  free_resources();
2891
2806
 
2892
2807
  if (stderror_file)
2893
2808
    fclose(stderror_file);
 
2809
}
2894
2810
 
 
2811
  catch(exception &err)
 
2812
  {
 
2813
    cerr << err.what() << endl;
 
2814
  }
 
2815
  
2895
2816
  return(first_error);
2896
2817
} /* main */