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