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