~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.cc

  • Committer: lbieber
  • Date: 2010-10-02 15:03:20 UTC
  • mfrom: (1808.1.3 build)
  • Revision ID: lbieber@orisndriz08-20101002150320-b4h2za7uf98juxql
Merge Andrew - fix bug 652645 - Bad error handling with Drizzledump connect
Merge Andrew - fix bug 652228 - drizzledump not dumping auto-inc when connecting to drizzle servers
Merge Andrew - fix bug 649844 - Clean up and fix some drizzledump options along with some documentation fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
 
81
81
bool  verbose= false;
82
82
static bool use_drizzle_protocol= false;
83
 
static bool quick= true;
84
 
static bool ignore_errors= false;
85
 
static bool flush_logs= false;
86
 
static bool opt_keywords= false;
87
 
static bool opt_compress= false;
88
 
static bool opt_delayed= false; 
 
83
bool ignore_errors= false;
 
84
bool opt_compress= false;
89
85
static bool create_options= true; 
90
86
static bool opt_quoted= false;
91
87
bool opt_databases= false; 
92
88
bool opt_alldbs= false; 
93
89
static bool opt_lock_all_tables= false;
94
 
static bool opt_set_charset= false; 
95
90
static bool opt_dump_date= true;
96
 
static bool opt_autocommit= false; 
 
91
bool opt_autocommit= false; 
97
92
static bool opt_single_transaction= false; 
98
93
static bool opt_comments;
99
94
static bool opt_compact;
100
 
static bool opt_order_by_primary=false; 
101
95
bool opt_ignore= false;
102
 
static bool opt_complete_insert= false;
103
 
static bool opt_drop_database;
104
 
static bool opt_alltspcs= false;
 
96
bool opt_drop_database;
105
97
bool opt_no_create_info;
106
98
bool opt_no_data= false;
107
99
bool opt_create_db= false;
110
102
bool opt_replace_into= false;
111
103
bool opt_drop= true; 
112
104
uint32_t show_progress_size= 0;
113
 
//static uint64_t total_rows= 0;
114
105
static string insert_pat;
115
 
//static char *order_by= NULL;
116
106
static uint32_t opt_drizzle_port= 0;
117
107
static int first_error= 0;
118
108
static string extended_row;
141
131
  enclosed,
142
132
  escaped,
143
133
  current_host,
144
 
  opt_enclosed,
145
 
  fields_terminated,
146
134
  path,
147
 
  lines_terminated,
148
135
  current_user,
149
136
  opt_password,
150
137
  opt_protocol,
170
157
  std::vector<DrizzleDumpDatabase*>::iterator i;
171
158
  for (i= database_store.begin(); i != database_store.end(); ++i)
172
159
  {
173
 
    if (not (*i)->populateTables())
 
160
    if ((not (*i)->populateTables()) && (not ignore_errors))
174
161
      maybe_exit(EX_DRIZZLEERR);
175
162
  }
176
163
}
178
165
void generate_dump(void)
179
166
{
180
167
  std::vector<DrizzleDumpDatabase*>::iterator i;
181
 
  if (opt_set_charset)
182
 
    cout << endl << "SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;" << endl;
183
168
 
184
169
  if (path.empty())
185
170
  {
186
171
    cout << endl << "SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;"
187
172
      << endl << "SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;" << endl;
188
173
  }
 
174
 
 
175
  if (opt_autocommit)
 
176
    cout << "SET AUTOCOMMIT=0;" << endl;
 
177
 
189
178
  for (i= database_store.begin(); i != database_store.end(); ++i)
190
179
  {
191
180
    DrizzleDumpDatabase *database= *i;
197
186
    cout << "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;"
198
187
      << endl << "SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;" << endl;
199
188
  }
200
 
  if (opt_set_charset)
201
 
    cout << "SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;" << endl;
202
189
}
203
190
 
204
191
void generate_dump_db(void)
205
192
{
206
193
  std::vector<DrizzleDumpDatabase*>::iterator i;
207
194
  DrizzleStringBuf sbuf(1024);
208
 
  destination_connection= new DrizzleDumpConnection(opt_destination_host,
209
 
    opt_destination_port, opt_destination_user, opt_destination_password,
210
 
    false);
 
195
  try
 
196
  {
 
197
    destination_connection= new DrizzleDumpConnection(opt_destination_host,
 
198
      opt_destination_port, opt_destination_user, opt_destination_password,
 
199
      false);
 
200
  }
 
201
  catch (...)
 
202
  {
 
203
    cerr << "Could not connect to destination database server" << endl;
 
204
    maybe_exit(EX_DRIZZLEERR);
 
205
  }
211
206
  sbuf.setConnection(destination_connection);
212
207
  std::ostream sout(&sbuf);
213
 
  if (opt_set_charset)
214
 
    sout << "SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;" << endl;
215
208
 
216
209
  if (path.empty())
217
210
  {
219
212
    sout << "SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;" << endl;
220
213
  }
221
214
 
 
215
  if (opt_autocommit)
 
216
    cout << "SET AUTOCOMMIT=0;" << endl;
 
217
 
222
218
  for (i= database_store.begin(); i != database_store.end(); ++i)
223
219
  {
224
220
    DrizzleDumpDatabase *database= *i;
230
226
    sout << "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;" << endl;
231
227
    sout << "SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;" << endl;
232
228
  }
233
 
  if (opt_set_charset)
234
 
    sout << "SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;" << endl;
235
229
}
236
230
 
237
231
/*
288
282
 
289
283
static int get_options(void)
290
284
{
291
 
 
292
 
  if (path.empty() && (! enclosed.empty() || ! opt_enclosed.empty() || ! escaped.empty() || ! lines_terminated.empty() ||
293
 
                ! fields_terminated.empty()))
294
 
  {
295
 
    fprintf(stderr,
296
 
            _("%s: You must use option --tab with --fields-...\n"), progname.c_str());
297
 
    return(EX_USAGE);
298
 
  }
299
 
 
300
285
  if (opt_single_transaction && opt_lock_all_tables)
301
286
  {
302
287
    fprintf(stderr, _("%s: You can't use --single-transaction and "
303
288
                      "--lock-all-tables at the same time.\n"), progname.c_str());
304
289
    return(EX_USAGE);
305
290
  }
306
 
  if (! enclosed.empty() && ! opt_enclosed.empty())
307
 
  {
308
 
    fprintf(stderr, _("%s: You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n"), progname.c_str());
309
 
    return(EX_USAGE);
310
 
  }
311
291
  if ((opt_databases || opt_alldbs) && ! path.empty())
312
292
  {
313
293
    fprintf(stderr,
436
416
  if (not database->populateTables(table_names))
437
417
  {
438
418
    delete database;
439
 
    maybe_exit(EX_DRIZZLEERR);
 
419
    if (not ignore_errors)
 
420
      maybe_exit(EX_DRIZZLEERR);
440
421
  }
441
422
 
442
423
  database_store.push_back(database); 
484
465
  commandline_options.add_options()
485
466
  ("all-databases,A", po::value<bool>(&opt_alldbs)->default_value(false)->zero_tokens(),
486
467
  N_("Dump all the databases. This will be same as --databases with all databases selected."))
487
 
  ("all-tablespaces,Y", po::value<bool>(&opt_alltspcs)->default_value(false)->zero_tokens(),
488
 
  N_("Dump all the tablespaces."))
489
 
  ("complete-insert,c", po::value<bool>(&opt_complete_insert)->default_value(false)->zero_tokens(),
490
 
  N_("Use complete insert statements."))
491
468
  ("compress,C", po::value<bool>(&opt_compress)->default_value(false)->zero_tokens(),
492
469
  N_("Use compression in server/client protocol."))
493
 
  ("flush-logs,F", po::value<bool>(&flush_logs)->default_value(false)->zero_tokens(),
494
 
  N_("Flush logs file in server before starting dump. Note that if you dump many databases at once (using the option --databases= or --all-databases), the logs will be flushed for each database dumped. The exception is when using --lock-all-tables in this case the logs will be flushed only once, corresponding to the moment all tables are locked. So if you want your dump and the log flush to happen at the same exact moment you should use --lock-all-tables or --flush-logs"))
495
470
  ("force,f", po::value<bool>(&ignore_errors)->default_value(false)->zero_tokens(),
496
471
  N_("Continue even if we get an sql-error."))
497
472
  ("help,?", N_("Display this help message and exit."))
498
473
  ("lock-all-tables,x", po::value<bool>(&opt_lock_all_tables)->default_value(false)->zero_tokens(),
499
 
  N_("Locks all tables across all databases. This is achieved by taking a global read lock for the duration of the whole dump. Automatically turns --single-transaction and --lock-tables off."))
500
 
  ("order-by-primary", po::value<bool>(&opt_order_by_primary)->default_value(false)->zero_tokens(),
501
 
  N_("Sorts each table's rows by primary key, or first unique key, if such a key exists.  Useful when dumping a MyISAM table to be loaded into an InnoDB table, but will make the dump itself take considerably longer."))
 
474
  N_("Locks all tables across all databases. This is achieved by taking a global read lock for the duration of the whole dump. Automatically turns --single-transaction off."))
502
475
  ("single-transaction", po::value<bool>(&opt_single_transaction)->default_value(false)->zero_tokens(),
503
 
  N_("Creates a consistent snapshot by dumping all tables in a single transaction. Works ONLY for tables stored in storage engines which support multiversioning (currently only InnoDB does); the dump is NOT guaranteed to be consistent for other storage engines. While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents), no other connection should use the following statements: ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not isolated from them. Option automatically turns off --lock-tables."))
504
 
  ("opt", N_("Same as --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. Enabled by default, disable with --skip-opt.")) 
 
476
  N_("Creates a consistent snapshot by dumping all tables in a single transaction. Works ONLY for tables stored in storage engines which support multiversioning (currently only InnoDB does); the dump is NOT guaranteed to be consistent for other storage engines. While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents), no other connection should use the following statements: ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not isolated from them."))
505
477
  ("skip-opt", 
506
 
  N_("Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys."))    
 
478
  N_("Disable --opt. Disables --add-drop-table, --add-locks, --create-options, ---extended-insert and --disable-keys."))    
507
479
  ("tables", N_("Overrides option --databases (-B)."))
508
480
  ("show-progress-size", po::value<uint32_t>(&show_progress_size)->default_value(10000),
509
481
  N_("Number of rows before each output progress report (requires --verbose)."))
522
494
  ("add-drop-database", po::value<bool>(&opt_drop_database)->default_value(false)->zero_tokens(),
523
495
  N_("Add a 'DROP DATABASE' before each create."))
524
496
  ("skip-drop-table", N_("Do not add a 'drop table' before each create."))
525
 
  ("allow-keywords", po::value<bool>(&opt_keywords)->default_value(false)->zero_tokens(),
526
 
  N_("Allow creation of column names that are keywords."))
527
497
  ("compact", po::value<bool>(&opt_compact)->default_value(false)->zero_tokens(),
528
 
  N_("Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs.  Enables options --skip-add-drop-table --no-set-names --skip-disable-keys --skip-add-locks"))
 
498
  N_("Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs.  Enables options --skip-add-drop-table --no-set-names --skip-disable-keys"))
529
499
  ("databases,B", po::value<bool>(&opt_databases)->default_value(false)->zero_tokens(),
530
500
  N_("To dump several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames. 'USE db_name;' will be included in the output."))
531
 
  ("delayed-insert", po::value<bool>(&opt_delayed)->default_value(false)->zero_tokens(),
532
 
  N_("Insert rows with INSERT DELAYED;"))
533
501
  ("skip-disable-keys,K",
534
502
  N_("'ALTER TABLE tb_name DISABLE KEYS; and 'ALTER TABLE tb_name ENABLE KEYS; will not be put in the output."))
535
503
  ("ignore-table", po::value<string>(),
536
504
  N_("Do not dump the specified table. To specify more than one table to ignore, use the directive multiple times, once for each table.  Each table must be specified with both database and table names, e.g. --ignore-table=database.table"))
537
505
  ("insert-ignore", po::value<bool>(&opt_ignore)->default_value(false)->zero_tokens(),
538
506
  N_("Insert rows with INSERT IGNORE."))
539
 
  ("lines-terminated-by", po::value<string>(&lines_terminated)->default_value(""),
540
 
  N_("Lines in the i.file are terminated by ..."))
541
507
  ("no-autocommit", po::value<bool>(&opt_autocommit)->default_value(false)->zero_tokens(),
542
 
  N_("Wrap tables with autocommit/commit statements."))
 
508
  N_("Wrap a table's data in START TRANSACTION/COMMIT statements."))
543
509
  ("no-create-db,n", po::value<bool>(&opt_create_db)->default_value(false)->zero_tokens(),
544
510
  N_("'CREATE DATABASE IF NOT EXISTS db_name;' will not be put in the output. The above line will be added otherwise, if --databases or --all-databases option was given."))
545
 
  ("no-create-info,t", po::value<bool>(&opt_no_create_info)->default_value(false)->zero_tokens(),
546
 
  N_("Don't write table creation info."))
547
511
  ("no-data,d", po::value<bool>(&opt_no_data)->default_value(false)->zero_tokens(),
548
512
  N_("No row information."))
549
 
  ("no-set-names,N", N_("Deprecated. Use --skip-set-charset instead."))
550
 
  ("set-charset", po::value<bool>(&opt_set_charset)->default_value(false)->zero_tokens(),
551
 
  N_("Enable set-name"))
552
 
  ("slow", N_("Buffer query instead of dumping directly to stdout."))
553
513
  ("replace", po::value<bool>(&opt_replace_into)->default_value(false)->zero_tokens(),
554
514
  N_("Use REPLACE INTO instead of INSERT INTO."))
555
 
  ("result-file,r", po::value<string>(),
556
 
  N_("Direct output to a given file. This option should be used in MSDOS, because it prevents new line '\\n' from being converted to '\\r\\n' (carriage return + line feed)."))
557
515
  ("destination-type", po::value<string>()->default_value("stdout"),
558
516
  N_("Where to send output to (stdout|database"))
559
517
  ("destination-host", po::value<string>(&opt_destination_host)->default_value("localhost"),
560
 
  N_("Hostname for destination db server (requires --destination=database)"))
 
518
  N_("Hostname for destination db server (requires --destination-type=database)"))
561
519
  ("destination-port", po::value<uint16_t>(&opt_destination_port)->default_value(3306),
562
 
  N_("Port number for destination db server (requires --destination=database)"))
 
520
  N_("Port number for destination db server (requires --destination-type=database)"))
563
521
  ("destination-user", po::value<string>(&opt_destination_user),
564
 
  N_("User name for destination db server (resquires --destination=database)"))
 
522
  N_("User name for destination db server (resquires --destination-type=database)"))
565
523
  ("destination-password", po::value<string>(&opt_destination_password),
566
 
  N_("Password for destination db server (requires --destination=database)"))
 
524
  N_("Password for destination db server (requires --destination-type=database)"))
567
525
  ("destination-database", po::value<string>(&opt_destination_database),
568
 
  N_("The database in the destination db server (requires --destination=database, not for use with --all-databases)"))
 
526
  N_("The database in the destination db server (requires --destination-type=database, not for use with --all-databases)"))
569
527
  ;
570
528
 
571
529
  po::options_description client_options(N_("Options specific to the client"));
669
627
  extended_insert= (vm.count("skip-extended-insert")) ? false : true;
670
628
  opt_dump_date= (vm.count("skip-dump-date")) ? false : true;
671
629
  opt_disable_keys= (vm.count("skip-disable-keys")) ? false : true;
672
 
  quick= (vm.count("slow")) ? false : true;
673
630
  opt_quoted= (vm.count("skip-quote-names")) ? false : true;
674
631
 
675
632
  if (vm.count("protocol"))
720
677
      tty_password= true;
721
678
  }
722
679
 
723
 
  if (vm.count("result-file"))
724
 
  {
725
 
    if (!(md_result_file= fopen(vm["result-file"].as<string>().c_str(), "w")))
726
 
      exit(1);
727
 
  }
728
 
 
729
 
  if (vm.count("no-set-names"))
730
 
  {
731
 
    opt_set_charset= 0;
732
 
  }
733
 
 
734
680
  if (! path.empty())
735
681
  { 
736
682
    opt_disable_keys= 0;
738
684
 
739
685
  if (vm.count("skip-opt"))
740
686
  {
741
 
    extended_insert= opt_drop= quick= create_options= 0;
742
 
    opt_disable_keys= opt_set_charset= 0;
 
687
    extended_insert= opt_drop= create_options= 0;
 
688
    opt_disable_keys= 0;
743
689
  }
744
690
 
745
691
  if (opt_compact)
746
692
  { 
747
693
    opt_comments= opt_drop= opt_disable_keys= 0;
748
 
    opt_set_charset= 0;
749
694
  }
750
695
 
751
696
  if (vm.count("opt"))
752
697
  {
753
 
    extended_insert= opt_drop= quick= create_options= 1;
754
 
    opt_disable_keys= opt_set_charset= 1;
 
698
    extended_insert= opt_drop= create_options= 1;
 
699
    opt_disable_keys= 1;
755
700
  }
756
701
 
757
702
  if (vm.count("tables"))
781
726
    free_resources();
782
727
    exit(exit_code);
783
728
  }
784
 
 
785
 
  db_connection = new DrizzleDumpConnection(current_host, opt_drizzle_port,
786
 
    current_user, opt_password, use_drizzle_protocol);
 
729
  try
 
730
  {
 
731
    db_connection = new DrizzleDumpConnection(current_host, opt_drizzle_port,
 
732
      current_user, opt_password, use_drizzle_protocol);
 
733
  }
 
734
  catch (...)
 
735
  {
 
736
    maybe_exit(EX_DRIZZLEERR);
 
737
  }
787
738
 
788
739
  if (db_connection->getServerType() == DrizzleDumpConnection::SERVER_MYSQL_FOUND)
789
740
    db_connection->queryNoResult("SET NAMES 'utf8'");