~drizzle-trunk/drizzle/development

1089.7.1 by Stewart Smith
fix drizzledump copyright header.
1
/* Copyright 2000-2008 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
1719.2.3 by Vijay Samuel
Merge removed the drizzled/option.h include from client_priv.h and archive_reader.
2
 * Copyright (C) 2010 Vijay Samuel
1751.4.10 by Andrew Hutchings
Start of data dump support
3
 * Copyright (C) 2010 Andrew Hutchings
1 by brian
clean slate
4
1407 by Brian Aker
Merge Siddharth, ran formatting across it.
5
  This program is free software; you can redistribute it and/or modify
6
  it under the terms of the GNU General Public License as published by
7
  the Free Software Foundation; version 2 of the License.
8
9
  This program is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with this program; if not, write to the Free Software
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
16
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
17
279.2.4 by Monty Taylor
Moved import, check and dump to C++... fixed errors.
18
/* drizzledump.cc  - Dump a tables contents and format to an ASCII file
1089.7.1 by Stewart Smith
fix drizzledump copyright header.
19
20
 * Derived from mysqldump, which originally came from:
1407 by Brian Aker
Merge Siddharth, ran formatting across it.
21
 **
22
 ** The author's original notes follow :-
23
 **
24
 ** AUTHOR: Igor Romanenko (igor@frog.kiev.ua)
25
 ** DATE:   December 3, 1994
26
 ** WARRANTY: None, expressed, impressed, implied
27
 **          or other
28
 ** STATUS: Public domain
1089.7.3 by Stewart Smith
move drizzledump 'by igor, monty, jani and sinisa' to commment rather than --help as having this in --help may be confusing to users in who to blame for bugs.
29
1407 by Brian Aker
Merge Siddharth, ran formatting across it.
30
 * and more work by Monty, Jani & Sinisa
31
 * and all the MySQL developers over the years.
1 by brian
clean slate
32
*/
33
612.2.4 by Monty Taylor
Moved some defines to config.h. Stopped including config.h directly anywhere.
34
#include "client_priv.h"
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
35
#include <string>
1633.5.1 by Vijay Samuel
Merge trunk
36
#include <iostream>
1 by brian
clean slate
37
#include <stdarg.h>
1678.1.1 by Monty Taylor
Removed our unordered wrappers. We use Boost now.
38
#include <boost/unordered_set.hpp>
758.1.2 by Monty Taylor
Fixed missing include.
39
#include <algorithm>
1633.5.1 by Vijay Samuel
Merge trunk
40
#include <fstream>
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
41
#include <drizzled/gettext.h>
1633.5.1 by Vijay Samuel
Merge trunk
42
#include <drizzled/configmake.h>
212.5.42 by Monty Taylor
Ding dong include is dead.
43
#include <drizzled/error.h>
1633.5.1 by Vijay Samuel
Merge trunk
44
#include <boost/program_options.hpp>
1751.4.2 by Andrew Hutchings
Switch to using boost::regex instead of pcre
45
#include <boost/regex.hpp>
1751.4.8 by Andrew Hutchings
Add DOUBLE/DECIMAL and DATE/TIME types with conversions
46
#include <boost/date_time/posix_time/posix_time.hpp>
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
47
#include "drizzledump_data.h"
48
#include "drizzledump_mysql.h"
49
#include "drizzledump_drizzle.h"
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
50
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
51
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
52
using namespace drizzled;
1633.5.1 by Vijay Samuel
Merge trunk
53
namespace po= boost::program_options;
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
54
1 by brian
clean slate
55
/* Exit codes */
56
57
#define EX_USAGE 1
206.3.1 by Patrick Galbraith
Most everything working with client rename
58
#define EX_DRIZZLEERR 2
1 by brian
clean slate
59
#define EX_CONSCHECK 3
60
#define EX_EOM 4
61
#define EX_EOF 5 /* ferror for output file was got */
62
#define EX_ILLEGAL_TABLE 6
756.1.2 by Andrew Hutchings
get_table_structure now returns true/false for success/fail. The field count is now a parameter
63
#define EX_TABLE_STATUS 7
1 by brian
clean slate
64
65
/* index into 'show fields from table' */
66
67
#define SHOW_FIELDNAME  0
68
#define SHOW_TYPE  1
69
#define SHOW_NULL  2
70
#define SHOW_DEFAULT  4
71
#define SHOW_EXTRA  5
72
73
/* Size of buffer for dump's select query */
74
#define QUERY_LENGTH 1536
75
76
/* ignore table flags */
77
#define IGNORE_NONE 0x00 /* no ignore */
78
#define IGNORE_DATA 0x01 /* don't dump data for this table */
79
#define IGNORE_INSERT_DELAYED 0x02 /* table doesn't support INSERT DELAYED */
80
1802.13.4 by Andrew Hutchings
merge with trunk
81
bool opt_alltspcs= false;
82
bool opt_complete_insert= false;
1751.4.23 by Andrew Hutchings
Fix various bugs
83
bool  verbose= false;
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
84
static bool use_drizzle_protocol= false;
1799.7.4 by Andrew Hutchings
Fix up some more options and the docs
85
bool ignore_errors= false;
1059.2.3 by kaleen
code style clean
86
static bool flush_logs= false;
87
static bool create_options= true; 
88
static bool opt_quoted= false;
1751.4.23 by Andrew Hutchings
Fix various bugs
89
bool opt_databases= false; 
90
bool opt_alldbs= false; 
1059.2.3 by kaleen
code style clean
91
static bool opt_lock_all_tables= false;
92
static bool opt_dump_date= true;
1799.7.2 by Andrew Hutchings
Clean up some drizzledump options
93
bool opt_autocommit= false; 
1059.2.3 by kaleen
code style clean
94
static bool opt_single_transaction= false; 
1799.7.6 by Andrew Hutchings
Fix comments appearing in test cases
95
static bool opt_comments;
1633.5.1 by Vijay Samuel
Merge trunk
96
static bool opt_compact;
1751.4.26 by Andrew Hutchings
Fix up for the drizzledump test cases
97
bool opt_ignore= false;
1799.7.2 by Andrew Hutchings
Clean up some drizzledump options
98
bool opt_drop_database;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
99
bool opt_no_create_info;
100
bool opt_no_data= false;
101
bool opt_create_db= false;
102
bool opt_disable_keys= true;
103
bool extended_insert= true;
104
bool opt_replace_into= false;
105
bool opt_drop= true; 
106
uint32_t show_progress_size= 0;
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
107
static string insert_pat;
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
108
static uint32_t opt_drizzle_port= 0;
109
static int first_error= 0;
279.2.6 by Monty Taylor
Replaced DYN string in drizzledump.
110
static string extended_row;
1 by brian
clean slate
111
FILE *md_result_file= 0;
673.5.11 by Andrew Hutchings
Apply -p means port changes to drizzledump
112
FILE *stderror_file= 0;
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
113
std::vector<DrizzleDumpDatabase*> database_store;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
114
DrizzleDumpConnection* db_connection;
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
115
DrizzleDumpConnection* destination_connection;
116
117
enum destinations {
118
  DESTINATION_DB,
119
  DESTINATION_FILES,
120
  DESTINATION_STDOUT
121
};
122
123
int opt_destination= DESTINATION_STDOUT;
124
std::string opt_destination_host;
125
uint16_t opt_destination_port;
126
std::string opt_destination_user;
127
std::string opt_destination_password;
128
std::string opt_destination_database;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
129
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
130
const string progname= "drizzledump";
131
1633.5.1 by Vijay Samuel
Merge trunk
132
string password,
133
  enclosed,
134
  escaped,
135
  current_host,
136
  path,
137
  current_user,
138
  opt_password,
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
139
  opt_protocol,
1633.5.1 by Vijay Samuel
Merge trunk
140
  where;
141
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
142
//static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
1008.3.3 by Stewart Smith
remove unused global variable in drizzledump (from DBUG)
143
1678.1.1 by Monty Taylor
Removed our unordered wrappers. We use Boost now.
144
boost::unordered_set<string> ignore_table;
1 by brian
clean slate
145
1751.4.25 by Andrew Hutchings
Fix error handling
146
void maybe_exit(int error);
1 by brian
clean slate
147
static void die(int error, const char* reason, ...);
1751.4.23 by Andrew Hutchings
Fix various bugs
148
static void write_header(char *db_name);
1633.5.6 by Vijay Samuel
Merge fixes requested by Padraig.
149
static int dump_selected_tables(const string &db, const vector<string> &table_names);
150
static int dump_databases(const vector<string> &db_names);
53.2.4 by Monty Taylor
Changes so that client/ builds cleanly with no warnings.
151
static int dump_all_databases(void);
1751.4.1 by Andrew Hutchings
Add functionality to detect server type we are connecting to
152
int get_server_type();
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
153
void dump_all_tables(void);
154
void generate_dump(void);
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
155
void generate_dump_db(void);
1751.4.1 by Andrew Hutchings
Add functionality to detect server type we are connecting to
156
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
157
void dump_all_tables(void)
158
{
159
  std::vector<DrizzleDumpDatabase*>::iterator i;
160
  for (i= database_store.begin(); i != database_store.end(); ++i)
161
  {
1799.7.4 by Andrew Hutchings
Fix up some more options and the docs
162
    if ((not (*i)->populateTables()) && (not ignore_errors))
1751.4.25 by Andrew Hutchings
Fix error handling
163
      maybe_exit(EX_DRIZZLEERR);
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
164
  }
165
}
166
167
void generate_dump(void)
168
{
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
169
  std::vector<DrizzleDumpDatabase*>::iterator i;
1751.4.23 by Andrew Hutchings
Fix various bugs
170
171
  if (path.empty())
172
  {
173
    cout << endl << "SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;"
174
      << endl << "SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;" << endl;
175
  }
1799.7.2 by Andrew Hutchings
Clean up some drizzledump options
176
177
  if (opt_autocommit)
178
    cout << "SET AUTOCOMMIT=0;" << endl;
179
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
180
  for (i= database_store.begin(); i != database_store.end(); ++i)
181
  {
182
    DrizzleDumpDatabase *database= *i;
183
    cout << *database;
184
  }
1751.4.23 by Andrew Hutchings
Fix various bugs
185
186
  if (path.empty())
187
  {
188
    cout << "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;"
189
      << endl << "SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;" << endl;
190
  }
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
191
}
192
193
void generate_dump_db(void)
194
{
195
  std::vector<DrizzleDumpDatabase*>::iterator i;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
196
  DrizzleStringBuf sbuf(1024);
1802.6.1 by Andrew Hutchings
Add better error handling and exception handling for database connect
197
  try
198
  {
199
    destination_connection= new DrizzleDumpConnection(opt_destination_host,
200
      opt_destination_port, opt_destination_user, opt_destination_password,
201
      false);
202
  }
1966.3.1 by Monty Taylor
Use std::exception instead of catch(...)
203
  catch (std::exception&)
1802.6.1 by Andrew Hutchings
Add better error handling and exception handling for database connect
204
  {
205
    cerr << "Could not connect to destination database server" << endl;
206
    maybe_exit(EX_DRIZZLEERR);
207
  }
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
208
  sbuf.setConnection(destination_connection);
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
209
  std::ostream sout(&sbuf);
1855.1.1 by Andrew Hutchings
Fix several bugs dumping tables with many rows
210
  sout.exceptions(ios_base::badbit);
1751.4.23 by Andrew Hutchings
Fix various bugs
211
212
  if (path.empty())
213
  {
214
    sout << "SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;" << endl;
215
    sout << "SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;" << endl;
216
  }
217
1799.7.2 by Andrew Hutchings
Clean up some drizzledump options
218
  if (opt_autocommit)
219
    cout << "SET AUTOCOMMIT=0;" << endl;
220
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
221
  for (i= database_store.begin(); i != database_store.end(); ++i)
222
  {
1855.1.1 by Andrew Hutchings
Fix several bugs dumping tables with many rows
223
    try
224
    {
225
      DrizzleDumpDatabase *database= *i;
226
      sout << *database;
227
    }
1966.3.1 by Monty Taylor
Use std::exception instead of catch(...)
228
    catch (std::exception&)
1855.1.1 by Andrew Hutchings
Fix several bugs dumping tables with many rows
229
    {
1880.2.1 by Andrew Hutchings
Add quoting to the end of the table definition
230
      std::cout << _("Error inserting into destination database") << std::endl;
1855.1.1 by Andrew Hutchings
Fix several bugs dumping tables with many rows
231
      if (not ignore_errors)
232
        maybe_exit(EX_DRIZZLEERR);
233
    }
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
234
  }
1751.4.23 by Andrew Hutchings
Fix various bugs
235
236
  if (path.empty())
237
  {
238
    sout << "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;" << endl;
239
    sout << "SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;" << endl;
240
  }
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
241
}
242
1 by brian
clean slate
243
/*
244
  exit with message if ferror(file)
245
246
  SYNOPSIS
1407 by Brian Aker
Merge Siddharth, ran formatting across it.
247
  check_io()
248
  file        - checked file
1 by brian
clean slate
249
*/
250
53.2.4 by Monty Taylor
Changes so that client/ builds cleanly with no warnings.
251
static void check_io(FILE *file)
1 by brian
clean slate
252
{
253
  if (ferror(file))
758.2.3 by Andrew Hutchings
Remove compatibility comments from drizzledump
254
    die(EX_EOF, _("Got errno %d on write"), errno);
1 by brian
clean slate
255
}
256
1751.4.23 by Andrew Hutchings
Fix various bugs
257
static void write_header(char *db_name)
1 by brian
clean slate
258
{
1799.7.6 by Andrew Hutchings
Fix comments appearing in test cases
259
  if ((not opt_compact) and (opt_comments))
1751.4.23 by Andrew Hutchings
Fix various bugs
260
  {
261
    cout << "-- drizzledump " << VERSION << " libdrizzle "
262
      << drizzle_version() << ", for " << HOST_VENDOR << "-" << HOST_OS
263
      << " (" << HOST_CPU << ")" << endl << "--" << endl;
264
    cout << "-- Host: " << current_host << "    Database: " << db_name << endl;
265
    cout << "-- ------------------------------------------------------" << endl;
266
    cout << "-- Server version\t" << db_connection->getServerVersion();
267
    if (db_connection->getServerType() == DrizzleDumpConnection::SERVER_MYSQL_FOUND)
268
      cout << " (MySQL server)";
269
    else if (db_connection->getServerType() == DrizzleDumpConnection::SERVER_DRIZZLE_FOUND)
270
      cout << " (Drizzle server)";
271
    cout << endl << endl;
1 by brian
clean slate
272
  }
273
} /* write_header */
274
275
276
static void write_footer(FILE *sql_file)
277
{
1751.4.7 by Andrew Hutchings
Add index support
278
  if (! opt_compact)
1 by brian
clean slate
279
  {
1799.7.6 by Andrew Hutchings
Fix comments appearing in test cases
280
    if (opt_comments)
1 by brian
clean slate
281
    {
1799.7.6 by Andrew Hutchings
Fix comments appearing in test cases
282
      if (opt_dump_date)
283
      {
284
        boost::posix_time::ptime time(boost::posix_time::second_clock::local_time());
285
        fprintf(sql_file, "-- Dump completed on %s\n",
286
          boost::posix_time::to_simple_string(time).c_str());
287
      }
288
      else
289
        fprintf(sql_file, "-- Dump completed\n");
1 by brian
clean slate
290
    }
291
    check_io(sql_file);
292
  }
293
} /* write_footer */
294
1633.5.1 by Vijay Samuel
Merge trunk
295
static int get_options(void)
296
{
1 by brian
clean slate
297
  if (opt_single_transaction && opt_lock_all_tables)
298
  {
758.2.3 by Andrew Hutchings
Remove compatibility comments from drizzledump
299
    fprintf(stderr, _("%s: You can't use --single-transaction and "
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
300
                      "--lock-all-tables at the same time.\n"), progname.c_str());
1 by brian
clean slate
301
    return(EX_USAGE);
302
  }
1633.5.1 by Vijay Samuel
Merge trunk
303
  if ((opt_databases || opt_alldbs) && ! path.empty())
1 by brian
clean slate
304
  {
305
    fprintf(stderr,
758.2.3 by Andrew Hutchings
Remove compatibility comments from drizzledump
306
            _("%s: --databases or --all-databases can't be used with --tab.\n"),
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
307
            progname.c_str());
1 by brian
clean slate
308
    return(EX_USAGE);
309
  }
1633.5.1 by Vijay Samuel
Merge trunk
310
1 by brian
clean slate
311
  if (tty_password)
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
312
    opt_password=client_get_tty_password(NULL);
1 by brian
clean slate
313
  return(0);
314
} /* get_options */
315
316
317
/*
318
  Prints out an error message and kills the process.
319
320
  SYNOPSIS
1407 by Brian Aker
Merge Siddharth, ran formatting across it.
321
  die()
322
  error_num   - process return value
323
  fmt_reason  - a format string for use by vsnprintf.
324
  ...         - variable arguments for above fmt_reason string
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
325
1 by brian
clean slate
326
  DESCRIPTION
1407 by Brian Aker
Merge Siddharth, ran formatting across it.
327
  This call prints out the formatted error message to stderr and then
328
  terminates the process.
1 by brian
clean slate
329
*/
330
static void die(int error_num, const char* fmt_reason, ...)
331
{
332
  char buffer[1000];
333
  va_list args;
334
  va_start(args,fmt_reason);
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
335
  vsnprintf(buffer, sizeof(buffer), fmt_reason, args);
1 by brian
clean slate
336
  va_end(args);
337
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
338
  fprintf(stderr, "%s: %s\n", progname.c_str(), buffer);
1 by brian
clean slate
339
  fflush(stderr);
340
341
  ignore_errors= 0; /* force the exit */
342
  maybe_exit(error_num);
343
}
344
53.2.4 by Monty Taylor
Changes so that client/ builds cleanly with no warnings.
345
static void free_resources(void)
1 by brian
clean slate
346
{
347
  if (md_result_file && md_result_file != stdout)
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
348
    fclose(md_result_file);
1633.5.1 by Vijay Samuel
Merge trunk
349
  opt_password.erase();
1 by brian
clean slate
350
}
351
352
1751.4.25 by Andrew Hutchings
Fix error handling
353
void maybe_exit(int error)
1 by brian
clean slate
354
{
355
  if (!first_error)
356
    first_error= error;
357
  if (ignore_errors)
358
    return;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
359
  delete db_connection;
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
360
  delete destination_connection;
1 by brian
clean slate
361
  free_resources();
362
  exit(error);
363
}
364
365
static int dump_all_databases()
366
{
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
367
  drizzle_row_t row;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
368
  drizzle_result_st *tableres;
1 by brian
clean slate
369
  int result=0;
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
370
  std::string query;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
371
  DrizzleDumpDatabase *database;
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
372
1751.4.23 by Andrew Hutchings
Fix various bugs
373
  if (verbose)
374
    std::cerr << _("-- Retrieving database structures...") << std::endl;
375
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
376
  /* Blocking the MySQL privilege tables too because we can't import them due to bug#646187 */
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
377
  if (db_connection->getServerType() == DrizzleDumpConnection::SERVER_MYSQL_FOUND)
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
378
    query= "SELECT SCHEMA_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME NOT IN ('information_schema', 'performance_schema', 'mysql')";
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
379
  else
380
    query= "SELECT SCHEMA_NAME, DEFAULT_COLLATION_NAME FROM DATA_DICTIONARY.SCHEMAS WHERE SCHEMA_NAME NOT IN ('information_schema','data_dictionary')";
381
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
382
  tableres= db_connection->query(query);
383
  while ((row= drizzle_row_next(tableres)))
1 by brian
clean slate
384
  {
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
385
    std::string database_name(row[0]);
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
386
    if (db_connection->getServerType() == DrizzleDumpConnection::SERVER_MYSQL_FOUND)
387
      database= new DrizzleDumpDatabaseMySQL(database_name, db_connection);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
388
    else
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
389
      database= new DrizzleDumpDatabaseDrizzle(database_name, db_connection);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
390
1751.4.6 by Andrew Hutchings
More fixups
391
    database->setCollate(row[1]);
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
392
    database_store.push_back(database);
1 by brian
clean slate
393
  }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
394
  db_connection->freeResult(tableres);
1 by brian
clean slate
395
  return result;
396
}
397
/* dump_all_databases */
398
399
1633.5.6 by Vijay Samuel
Merge fixes requested by Padraig.
400
static int dump_databases(const vector<string> &db_names)
1 by brian
clean slate
401
{
402
  int result=0;
1633.5.1 by Vijay Samuel
Merge trunk
403
  string temp;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
404
  DrizzleDumpDatabase *database;
405
1633.5.6 by Vijay Samuel
Merge fixes requested by Padraig.
406
  for (vector<string>::const_iterator it= db_names.begin(); it != db_names.end(); ++it)
1 by brian
clean slate
407
  {
1633.5.1 by Vijay Samuel
Merge trunk
408
    temp= *it;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
409
    if (db_connection->getServerType() == DrizzleDumpConnection::SERVER_MYSQL_FOUND)
410
      database= new DrizzleDumpDatabaseMySQL(temp, db_connection);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
411
    else
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
412
      database= new DrizzleDumpDatabaseDrizzle(temp, db_connection);
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
413
    database_store.push_back(database);
1 by brian
clean slate
414
  }
142.1.2 by Patrick
All DBUG_x removed from client/
415
  return(result);
1 by brian
clean slate
416
} /* dump_databases */
417
1633.5.6 by Vijay Samuel
Merge fixes requested by Padraig.
418
static int dump_selected_tables(const string &db, const vector<string> &table_names)
1 by brian
clean slate
419
{
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
420
  DrizzleDumpDatabase *database;
421
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
422
  if (db_connection->getServerType() == DrizzleDumpConnection::SERVER_MYSQL_FOUND)
423
    database= new DrizzleDumpDatabaseMySQL(db, db_connection);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
424
  else
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
425
    database= new DrizzleDumpDatabaseDrizzle(db, db_connection);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
426
1751.4.25 by Andrew Hutchings
Fix error handling
427
  if (not database->populateTables(table_names))
428
  {
429
    delete database;
1799.7.4 by Andrew Hutchings
Fix up some more options and the docs
430
    if (not ignore_errors)
431
      maybe_exit(EX_DRIZZLEERR);
1751.4.25 by Andrew Hutchings
Fix error handling
432
  }
1751.4.23 by Andrew Hutchings
Fix various bugs
433
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
434
  database_store.push_back(database); 
1 by brian
clean slate
435
1054.1.1 by Brian Aker
Remove guts in parser for LOCK TABLE.
436
  return 0;
1 by brian
clean slate
437
} /* dump_selected_tables */
438
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
439
static int do_flush_tables_read_lock()
1 by brian
clean slate
440
{
441
  /*
442
    We do first a FLUSH TABLES. If a long update is running, the FLUSH TABLES
443
    will wait but will not stall the whole mysqld, and when the long update is
444
    done the FLUSH TABLES WITH READ LOCK will start and succeed quickly. So,
1351.1.3 by stefan
some minor mysql->drizzle replacements
445
    FLUSH TABLES is to lower the probability of a stage where both drizzled
1 by brian
clean slate
446
    and most client connections are stalled. Of course, if a second long
447
    update starts between the two FLUSHes, we have that bad stall.
448
  */
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
449
450
   db_connection->queryNoResult("FLUSH TABLES");
451
   db_connection->queryNoResult("FLUSH TABLES WITH READ LOCK");
452
453
  return 0;
454
}
455
456
static int do_unlock_tables()
457
{
458
  db_connection->queryNoResult("UNLOCK TABLES");
459
  return 0;
460
}
461
462
static int start_transaction()
463
{
464
  db_connection->queryNoResult("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ");
465
  db_connection->queryNoResult("START TRANSACTION WITH CONSISTENT SNAPSHOT");
466
  return 0;
1751.4.1 by Andrew Hutchings
Add functionality to detect server type we are connecting to
467
}
468
1 by brian
clean slate
469
int main(int argc, char **argv)
470
{
1633.5.1 by Vijay Samuel
Merge trunk
471
try
472
{
1 by brian
clean slate
473
  int exit_code;
474
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
475
  po::options_description commandline_options(N_("Options used only in command line"));
1633.5.1 by Vijay Samuel
Merge trunk
476
  commandline_options.add_options()
477
  ("all-databases,A", po::value<bool>(&opt_alldbs)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
478
  N_("Dump all the databases. This will be same as --databases with all databases selected."))
1633.5.1 by Vijay Samuel
Merge trunk
479
  ("all-tablespaces,Y", po::value<bool>(&opt_alltspcs)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
480
  N_("Dump all the tablespaces."))
1633.5.1 by Vijay Samuel
Merge trunk
481
  ("complete-insert,c", po::value<bool>(&opt_complete_insert)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
482
  N_("Use complete insert statements."))
1633.5.1 by Vijay Samuel
Merge trunk
483
  ("flush-logs,F", po::value<bool>(&flush_logs)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
484
  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"))
1633.5.1 by Vijay Samuel
Merge trunk
485
  ("force,f", po::value<bool>(&ignore_errors)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
486
  N_("Continue even if we get an sql-error."))
487
  ("help,?", N_("Display this help message and exit."))
1633.5.1 by Vijay Samuel
Merge trunk
488
  ("lock-all-tables,x", po::value<bool>(&opt_lock_all_tables)->default_value(false)->zero_tokens(),
1799.7.4 by Andrew Hutchings
Fix up some more options and the docs
489
  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 off."))
1633.5.1 by Vijay Samuel
Merge trunk
490
  ("single-transaction", po::value<bool>(&opt_single_transaction)->default_value(false)->zero_tokens(),
1799.7.4 by Andrew Hutchings
Fix up some more options and the docs
491
  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."))
1633.5.1 by Vijay Samuel
Merge trunk
492
  ("skip-opt", 
1799.7.4 by Andrew Hutchings
Fix up some more options and the docs
493
  N_("Disable --opt. Disables --add-drop-table, --add-locks, --create-options, ---extended-insert and --disable-keys."))    
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
494
  ("tables", N_("Overrides option --databases (-B)."))
1633.5.1 by Vijay Samuel
Merge trunk
495
  ("show-progress-size", po::value<uint32_t>(&show_progress_size)->default_value(10000),
496
  N_("Number of rows before each output progress report (requires --verbose)."))
497
  ("verbose,v", po::value<bool>(&verbose)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
498
  N_("Print info about the various stages."))
499
  ("version,V", N_("Output version information and exit."))
500
  ("skip-comments", N_("Turn off Comments"))
501
  ("skip-create", N_("Turn off create-options"))
502
  ("skip-extended-insert", N_("Turn off extended-insert"))
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
503
  ("skip-dump-date",N_( "Turn off dump date at the end of the output"))
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
504
  ("no-defaults", N_("Do not read from the configuration files"))
1633.5.1 by Vijay Samuel
Merge trunk
505
  ;
506
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
507
  po::options_description dump_options(N_("Options specific to the drizzle client"));
1633.5.1 by Vijay Samuel
Merge trunk
508
  dump_options.add_options()
509
  ("add-drop-database", po::value<bool>(&opt_drop_database)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
510
  N_("Add a 'DROP DATABASE' before each create."))
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
511
  ("skip-drop-table", N_("Do not add a 'drop table' before each create."))
1633.5.1 by Vijay Samuel
Merge trunk
512
  ("compact", po::value<bool>(&opt_compact)->default_value(false)->zero_tokens(),
1799.7.2 by Andrew Hutchings
Clean up some drizzledump options
513
  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"))
1633.5.1 by Vijay Samuel
Merge trunk
514
  ("databases,B", po::value<bool>(&opt_databases)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
515
  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."))
1751.4.18 by Andrew Hutchings
Make some command line options work again
516
  ("skip-disable-keys,K",
517
  N_("'ALTER TABLE tb_name DISABLE KEYS; and 'ALTER TABLE tb_name ENABLE KEYS; will not be put in the output."))
1633.5.1 by Vijay Samuel
Merge trunk
518
  ("ignore-table", po::value<string>(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
519
  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"))
1633.5.1 by Vijay Samuel
Merge trunk
520
  ("insert-ignore", po::value<bool>(&opt_ignore)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
521
  N_("Insert rows with INSERT IGNORE."))
1633.5.1 by Vijay Samuel
Merge trunk
522
  ("no-autocommit", po::value<bool>(&opt_autocommit)->default_value(false)->zero_tokens(),
1799.7.2 by Andrew Hutchings
Clean up some drizzledump options
523
  N_("Wrap a table's data in START TRANSACTION/COMMIT statements."))
1633.5.1 by Vijay Samuel
Merge trunk
524
  ("no-create-db,n", po::value<bool>(&opt_create_db)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
525
  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."))
1633.5.1 by Vijay Samuel
Merge trunk
526
  ("no-data,d", po::value<bool>(&opt_no_data)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
527
  N_("No row information."))
1633.5.1 by Vijay Samuel
Merge trunk
528
  ("replace", po::value<bool>(&opt_replace_into)->default_value(false)->zero_tokens(),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
529
  N_("Use REPLACE INTO instead of INSERT INTO."))
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
530
  ("destination-type", po::value<string>()->default_value("stdout"),
531
  N_("Where to send output to (stdout|database"))
532
  ("destination-host", po::value<string>(&opt_destination_host)->default_value("localhost"),
1802.6.2 by Andrew Hutchings
Also fix help message
533
  N_("Hostname for destination db server (requires --destination-type=database)"))
1865.3.1 by Andrew Hutchings
Make port 4427 the default for client apps
534
  ("destination-port", po::value<uint16_t>(&opt_destination_port)->default_value(4427),
1802.6.2 by Andrew Hutchings
Also fix help message
535
  N_("Port number for destination db server (requires --destination-type=database)"))
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
536
  ("destination-user", po::value<string>(&opt_destination_user),
1802.6.2 by Andrew Hutchings
Also fix help message
537
  N_("User name for destination db server (resquires --destination-type=database)"))
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
538
  ("destination-password", po::value<string>(&opt_destination_password),
1802.6.2 by Andrew Hutchings
Also fix help message
539
  N_("Password for destination db server (requires --destination-type=database)"))
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
540
  ("destination-database", po::value<string>(&opt_destination_database),
1802.6.2 by Andrew Hutchings
Also fix help message
541
  N_("The database in the destination db server (requires --destination-type=database, not for use with --all-databases)"))
1633.5.1 by Vijay Samuel
Merge trunk
542
  ;
543
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
544
  po::options_description client_options(N_("Options specific to the client"));
1633.5.1 by Vijay Samuel
Merge trunk
545
  client_options.add_options()
546
  ("host,h", po::value<string>(&current_host)->default_value("localhost"),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
547
  N_("Connect to host."))
1633.5.1 by Vijay Samuel
Merge trunk
548
  ("password,P", po::value<string>(&password)->default_value(PASSWORD_SENTINEL),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
549
  N_("Password to use when connecting to server. If password is not given it's solicited on the tty."))
1633.5.1 by Vijay Samuel
Merge trunk
550
  ("port,p", po::value<uint32_t>(&opt_drizzle_port)->default_value(0),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
551
  N_("Port number to use for connection."))
1633.5.1 by Vijay Samuel
Merge trunk
552
  ("user,u", po::value<string>(&current_user)->default_value(""),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
553
  N_("User for login if not current user."))
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
554
  ("protocol",po::value<string>(&opt_protocol)->default_value("mysql"),
555
  N_("The protocol of connection (mysql or drizzle)."))
1633.5.1 by Vijay Samuel
Merge trunk
556
  ;
557
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
558
  po::options_description hidden_options(N_("Hidden Options"));
1633.5.1 by Vijay Samuel
Merge trunk
559
  hidden_options.add_options()
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
560
  ("database-used", po::value<vector<string> >(), N_("Used to select the database"))
561
  ("Table-used", po::value<vector<string> >(), N_("Used to select the tables"))
1633.5.1 by Vijay Samuel
Merge trunk
562
  ;
563
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
564
  po::options_description all_options(N_("Allowed Options + Hidden Options"));
1633.5.1 by Vijay Samuel
Merge trunk
565
  all_options.add(commandline_options).add(dump_options).add(client_options).add(hidden_options);
566
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
567
  po::options_description long_options(N_("Allowed Options"));
1633.5.1 by Vijay Samuel
Merge trunk
568
  long_options.add(commandline_options).add(dump_options).add(client_options);
569
570
  std::string system_config_dir_dump(SYSCONFDIR); 
571
  system_config_dir_dump.append("/drizzle/drizzledump.cnf");
572
573
  std::string system_config_dir_client(SYSCONFDIR); 
574
  system_config_dir_client.append("/drizzle/client.cnf");
575
1671.2.1 by Vijay Samuel
Merge new user config file processing system.
576
  std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
577
1921.3.1 by Andrew Hutchings
Using '~' to represent home is a shell standard, POSIX functions do not understand it. So for home config file loading substitute it for env variable $HOME if possible (also a POSIX standard)
578
  if (user_config_dir.compare(0, 2, "~/") == 0)
579
  {
580
    char *homedir;
581
    homedir= getenv("HOME");
582
    if (homedir != NULL)
583
      user_config_dir.replace(0, 1, homedir);
584
  }
585
1633.5.1 by Vijay Samuel
Merge trunk
586
  po::positional_options_description p;
587
  p.add("database-used", 1);
1633.5.5 by Vijay Samuel
Merge fix for --tab
588
  p.add("Table-used",-1);
1633.5.1 by Vijay Samuel
Merge trunk
589
590
  md_result_file= stdout;
591
592
  po::variables_map vm;
593
1793.3.1 by Andrew Hutchings
Disable boost:po allow_guessing which was making some wrong assumptions
594
  // Disable allow_guessing
595
  int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
596
597
  po::store(po::command_line_parser(argc, argv).style(style).
598
            options(all_options).positional(p).
599
            extra_parser(parse_password_arg).run(), vm);
1633.5.1 by Vijay Samuel
Merge trunk
600
1633.5.6 by Vijay Samuel
Merge fixes requested by Padraig.
601
  if (! vm.count("no-defaults"))
1633.5.1 by Vijay Samuel
Merge trunk
602
  {
1671.2.1 by Vijay Samuel
Merge new user config file processing system.
603
    std::string user_config_dir_dump(user_config_dir);
604
    user_config_dir_dump.append("/drizzle/drizzledump.cnf"); 
605
606
    std::string user_config_dir_client(user_config_dir);
607
    user_config_dir_client.append("/drizzle/client.cnf");
608
609
    ifstream user_dump_ifs(user_config_dir_dump.c_str());
1633.5.1 by Vijay Samuel
Merge trunk
610
    po::store(parse_config_file(user_dump_ifs, dump_options), vm);
1671.2.1 by Vijay Samuel
Merge new user config file processing system.
611
612
    ifstream user_client_ifs(user_config_dir_client.c_str());
613
    po::store(parse_config_file(user_client_ifs, client_options), vm);
614
1633.5.1 by Vijay Samuel
Merge trunk
615
    ifstream system_dump_ifs(system_config_dir_dump.c_str());
1671.2.1 by Vijay Samuel
Merge new user config file processing system.
616
    po::store(parse_config_file(system_dump_ifs, dump_options), vm);
1633.5.1 by Vijay Samuel
Merge trunk
617
618
    ifstream system_client_ifs(system_config_dir_client.c_str());
619
    po::store(parse_config_file(system_client_ifs, client_options), vm);
620
  }
621
622
  po::notify(vm);  
623
  
1720.3.1 by LinuxJedi
Fix for bug #620670:
624
  if ((not vm.count("database-used") && not vm.count("Table-used") 
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
625
    && not opt_alldbs && path.empty())
626
    || (vm.count("help")) || vm.count("version"))
1633.5.1 by Vijay Samuel
Merge trunk
627
  {
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
628
    printf(_("Drizzledump %s build %s, for %s-%s (%s)\n"),
629
      drizzle_version(), VERSION, HOST_VENDOR, HOST_OS, HOST_CPU);
630
    if (vm.count("version"))
631
      exit(0);
1720.3.1 by LinuxJedi
Fix for bug #620670:
632
    puts("");
633
    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"));
634
    puts(_("Dumps definitions and data from a Drizzle database server"));
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
635
    printf(_("Usage: %s [OPTIONS] database [tables]\n"), progname.c_str());
1633.5.1 by Vijay Samuel
Merge trunk
636
    printf(_("OR     %s [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]\n"),
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
637
          progname.c_str());
638
    printf(_("OR     %s [OPTIONS] --all-databases [OPTIONS]\n"), progname.c_str());
1720.3.1 by LinuxJedi
Fix for bug #620670:
639
    cout << long_options;
640
    if (vm.count("help"))
641
      exit(0);
642
    else
643
      exit(1);
1633.5.1 by Vijay Samuel
Merge trunk
644
  }
645
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
646
  /* Inverted Booleans */
647
648
  opt_drop= (vm.count("skip-drop-table")) ? false : true;
1799.7.6 by Andrew Hutchings
Fix comments appearing in test cases
649
  opt_comments= (vm.count("skip-comments")) ? false : true;
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
650
  extended_insert= (vm.count("skip-extended-insert")) ? false : true;
651
  opt_dump_date= (vm.count("skip-dump-date")) ? false : true;
1751.4.18 by Andrew Hutchings
Make some command line options work again
652
  opt_disable_keys= (vm.count("skip-disable-keys")) ? false : true;
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
653
  opt_quoted= (vm.count("skip-quote-names")) ? false : true;
654
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
655
  if (vm.count("protocol"))
656
  {
657
    std::transform(opt_protocol.begin(), opt_protocol.end(),
658
      opt_protocol.begin(), ::tolower);
659
660
    if (not opt_protocol.compare("mysql"))
661
      use_drizzle_protocol=false;
662
    else if (not opt_protocol.compare("drizzle"))
663
      use_drizzle_protocol=true;
664
    else
665
    {
666
      cout << _("Error: Unknown protocol") << " '" << opt_protocol << "'" << endl;
667
      exit(-1);
668
    }
669
  }
670
1633.5.1 by Vijay Samuel
Merge trunk
671
  if (vm.count("port"))
672
  {
673
    /* If the port number is > 65535 it is not a valid port
674
     *        This also helps with potential data loss casting unsigned long to a
675
     *               uint32_t. 
676
     */
677
    if (opt_drizzle_port > 65535)
678
    {
679
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
680
      exit(-1);
681
    }
682
  }
683
1633.5.6 by Vijay Samuel
Merge fixes requested by Padraig.
684
  if(vm.count("password"))
1633.5.1 by Vijay Samuel
Merge trunk
685
  {
686
    if (!opt_password.empty())
687
      opt_password.erase();
688
    if (password == PASSWORD_SENTINEL)
689
    {
690
      opt_password= "";
691
    }
692
    else
693
    {
694
      opt_password= password;
695
      tty_password= false;
696
    }
697
  }
698
  else
699
  {
700
      tty_password= true;
701
  }
702
703
  if (! path.empty())
704
  { 
705
    opt_disable_keys= 0;
706
  }
707
708
  if (vm.count("skip-opt"))
709
  {
1799.7.2 by Andrew Hutchings
Clean up some drizzledump options
710
    extended_insert= opt_drop= create_options= 0;
711
    opt_disable_keys= 0;
1633.5.1 by Vijay Samuel
Merge trunk
712
  }
713
714
  if (opt_compact)
715
  { 
1799.7.6 by Andrew Hutchings
Fix comments appearing in test cases
716
    opt_comments= opt_drop= opt_disable_keys= 0;
1633.5.1 by Vijay Samuel
Merge trunk
717
  }
718
719
  if (vm.count("opt"))
720
  {
1799.7.2 by Andrew Hutchings
Clean up some drizzledump options
721
    extended_insert= opt_drop= create_options= 1;
722
    opt_disable_keys= 1;
1633.5.1 by Vijay Samuel
Merge trunk
723
  }
724
725
  if (vm.count("tables"))
726
  { 
727
    opt_databases= false;
728
  }
729
730
  if (vm.count("ignore-table"))
731
  {
732
    if (!strchr(vm["ignore-table"].as<string>().c_str(), '.'))
733
    {
734
      fprintf(stderr, _("Illegal use of option --ignore-table=<database>.<table>\n"));
735
      exit(EXIT_ARGUMENT_INVALID);
736
    }
737
    string tmpptr(vm["ignore-table"].as<string>());
738
    ignore_table.insert(tmpptr); 
739
  }
1757.5.2 by Andrew Hutchings
Fix broke drizzledump option and tests
740
741
  if (vm.count("skip-create"))
742
  {
743
    opt_create_db= opt_no_create_info= create_options= false;
744
  }
745
 
1633.5.1 by Vijay Samuel
Merge trunk
746
  exit_code= get_options();
1 by brian
clean slate
747
  if (exit_code)
748
  {
53.2.4 by Monty Taylor
Changes so that client/ builds cleanly with no warnings.
749
    free_resources();
1 by brian
clean slate
750
    exit(exit_code);
751
  }
1802.6.1 by Andrew Hutchings
Add better error handling and exception handling for database connect
752
  try
753
  {
754
    db_connection = new DrizzleDumpConnection(current_host, opt_drizzle_port,
755
      current_user, opt_password, use_drizzle_protocol);
756
  }
1966.3.1 by Monty Taylor
Use std::exception instead of catch(...)
757
  catch (std::exception&)
1802.6.1 by Andrew Hutchings
Add better error handling and exception handling for database connect
758
  {
759
    maybe_exit(EX_DRIZZLEERR);
760
  }
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
761
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
762
  if (db_connection->getServerType() == DrizzleDumpConnection::SERVER_MYSQL_FOUND)
763
    db_connection->queryNoResult("SET NAMES 'utf8'");
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
764
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
765
  if (vm.count("destination-type"))
766
  {
767
    string tmp_destination(vm["destination-type"].as<string>());
768
    if (tmp_destination.compare("database") == 0)
769
      opt_destination= DESTINATION_DB;
770
    else if (tmp_destination.compare("stdout") == 0)
771
      opt_destination= DESTINATION_STDOUT;
772
    else
773
      exit(EXIT_ARGUMENT_INVALID);
774
  }
775
776
1633.5.1 by Vijay Samuel
Merge trunk
777
  if (path.empty() && vm.count("database-used"))
778
  {
779
    string database_used= *vm["database-used"].as< vector<string> >().begin();
1751.4.23 by Andrew Hutchings
Fix various bugs
780
    write_header((char *)database_used.c_str());
1633.5.1 by Vijay Samuel
Merge trunk
781
  }
1 by brian
clean slate
782
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
783
  if ((opt_lock_all_tables) && do_flush_tables_read_lock())
1 by brian
clean slate
784
    goto err;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
785
  if (opt_single_transaction && start_transaction())
1407 by Brian Aker
Merge Siddharth, ran formatting across it.
786
    goto err;
1059.1.1 by patg
Removed all master/slave-specific code in drizzledump
787
  if (opt_lock_all_tables)
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
788
    db_connection->queryNoResult("FLUSH LOGS");
789
  if (opt_single_transaction && do_unlock_tables()) /* unlock but no commit! */
1 by brian
clean slate
790
    goto err;
791
792
  if (opt_alldbs)
793
  {
794
    dump_all_databases();
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
795
    dump_all_tables();
1 by brian
clean slate
796
  }
1633.5.6 by Vijay Samuel
Merge fixes requested by Padraig.
797
  if (vm.count("database-used") && vm.count("Table-used") && ! opt_databases)
1 by brian
clean slate
798
  {
1633.5.1 by Vijay Samuel
Merge trunk
799
    string database_used= *vm["database-used"].as< vector<string> >().begin();
1 by brian
clean slate
800
    /* Only one database and selected table(s) */
1633.5.5 by Vijay Samuel
Merge fix for --tab
801
    dump_selected_tables(database_used, vm["Table-used"].as< vector<string> >());
1633.5.1 by Vijay Samuel
Merge trunk
802
  }
803
1633.5.5 by Vijay Samuel
Merge fix for --tab
804
  if (vm.count("Table-used") && opt_databases)
1633.5.1 by Vijay Samuel
Merge trunk
805
  {
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
806
/*
807
 * This is not valid!
1633.5.1 by Vijay Samuel
Merge trunk
808
    vector<string> database_used= vm["database-used"].as< vector<string> >();
1633.5.5 by Vijay Samuel
Merge fix for --tab
809
    vector<string> table_used= vm["Table-used"].as< vector<string> >();
1633.5.1 by Vijay Samuel
Merge trunk
810
811
    for (vector<string>::iterator it= table_used.begin();
812
       it != table_used.end();
813
       ++it)
814
    {
815
      database_used.insert(database_used.end(), *it);
816
    }
817
    dump_databases(database_used);
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
818
    dump_tables();
819
*/
1633.5.1 by Vijay Samuel
Merge trunk
820
  }
821
  
1633.5.5 by Vijay Samuel
Merge fix for --tab
822
  if (vm.count("database-used") && ! vm.count("Table-used"))
1633.5.1 by Vijay Samuel
Merge trunk
823
  {
824
    dump_databases(vm["database-used"].as< vector<string> >());
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
825
    dump_all_tables();
1 by brian
clean slate
826
  }
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
827
  if (opt_destination == DESTINATION_STDOUT)
828
    generate_dump();
829
  else
830
    generate_dump_db();
1751.4.3 by Andrew Hutchings
Rip out tons of code which I'll probably put back cleaner later
831
1 by brian
clean slate
832
  /* ensure dumped data flushed */
833
  if (md_result_file && fflush(md_result_file))
834
  {
835
    if (!first_error)
206.3.1 by Patrick Galbraith
Most everything working with client rename
836
      first_error= EX_DRIZZLEERR;
1 by brian
clean slate
837
    goto err;
838
  }
839
840
  /*
841
    No reason to explicitely COMMIT the transaction, neither to explicitely
842
    UNLOCK TABLES: these will be automatically be done by the server when we
843
    disconnect now. Saves some code here, some network trips, adds nothing to
844
    server.
845
  */
846
err:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
847
  delete db_connection;
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
848
  delete destination_connection;
1633.5.1 by Vijay Samuel
Merge trunk
849
  if (path.empty())
1 by brian
clean slate
850
    write_footer(md_result_file);
851
  free_resources();
852
853
  if (stderror_file)
854
    fclose(stderror_file);
1633.5.1 by Vijay Samuel
Merge trunk
855
}
1 by brian
clean slate
856
1633.5.1 by Vijay Samuel
Merge trunk
857
  catch(exception &err)
858
  {
1633.5.6 by Vijay Samuel
Merge fixes requested by Padraig.
859
    cerr << err.what() << endl;
1633.5.1 by Vijay Samuel
Merge trunk
860
  }
861
  
1 by brian
clean slate
862
  return(first_error);
863
} /* main */