~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.cc

  • Committer: Andrew Hutchings
  • Date: 2010-09-26 08:33:47 UTC
  • mto: (1795.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1796.
  • Revision ID: andrew@linuxjedi.co.uk-20100926083347-fzsc9e7w0j4u7bj1
Disable boost:po allow_guessing which was making some wrong assumptions

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
  You should have received a copy of the GNU General Public License
15
15
  along with this program; if not, write to the Free Software
16
 
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
16
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
17
17
 
18
18
/* drizzledump.cc  - Dump a tables contents and format to an ASCII file
19
19
 
78
78
#define IGNORE_DATA 0x01 /* don't dump data for this table */
79
79
#define IGNORE_INSERT_DELAYED 0x02 /* table doesn't support INSERT DELAYED */
80
80
 
81
 
bool opt_alltspcs= false;
82
 
bool opt_complete_insert= false;
83
81
bool  verbose= false;
84
82
static bool use_drizzle_protocol= false;
85
 
bool ignore_errors= false;
 
83
static bool quick= true;
 
84
static bool ignore_errors= false;
86
85
static bool flush_logs= false;
 
86
static bool opt_keywords= false;
 
87
static bool opt_compress= false;
 
88
static bool opt_delayed= false; 
87
89
static bool create_options= true; 
88
90
static bool opt_quoted= false;
89
91
bool opt_databases= false; 
90
92
bool opt_alldbs= false; 
91
93
static bool opt_lock_all_tables= false;
 
94
static bool opt_set_charset= false; 
92
95
static bool opt_dump_date= true;
93
 
bool opt_autocommit= false; 
 
96
static bool opt_autocommit= false; 
94
97
static bool opt_single_transaction= false; 
95
98
static bool opt_comments;
96
99
static bool opt_compact;
 
100
static bool opt_order_by_primary=false; 
97
101
bool opt_ignore= false;
98
 
bool opt_drop_database;
 
102
static bool opt_complete_insert= false;
 
103
static bool opt_drop_database;
 
104
static bool opt_alltspcs= false;
99
105
bool opt_no_create_info;
100
106
bool opt_no_data= false;
101
107
bool opt_create_db= false;
104
110
bool opt_replace_into= false;
105
111
bool opt_drop= true; 
106
112
uint32_t show_progress_size= 0;
 
113
//static uint64_t total_rows= 0;
107
114
static string insert_pat;
 
115
//static char *order_by= NULL;
108
116
static uint32_t opt_drizzle_port= 0;
109
117
static int first_error= 0;
110
118
static string extended_row;
133
141
  enclosed,
134
142
  escaped,
135
143
  current_host,
 
144
  opt_enclosed,
 
145
  fields_terminated,
136
146
  path,
 
147
  lines_terminated,
137
148
  current_user,
138
149
  opt_password,
139
150
  opt_protocol,
159
170
  std::vector<DrizzleDumpDatabase*>::iterator i;
160
171
  for (i= database_store.begin(); i != database_store.end(); ++i)
161
172
  {
162
 
    if ((not (*i)->populateTables()) && (not ignore_errors))
 
173
    if (not (*i)->populateTables())
163
174
      maybe_exit(EX_DRIZZLEERR);
164
175
  }
165
176
}
167
178
void generate_dump(void)
168
179
{
169
180
  std::vector<DrizzleDumpDatabase*>::iterator i;
 
181
  if (opt_set_charset)
 
182
    cout << endl << "SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;" << endl;
170
183
 
171
184
  if (path.empty())
172
185
  {
173
186
    cout << endl << "SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;"
174
187
      << endl << "SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;" << endl;
175
188
  }
176
 
 
177
 
  if (opt_autocommit)
178
 
    cout << "SET AUTOCOMMIT=0;" << endl;
179
 
 
180
189
  for (i= database_store.begin(); i != database_store.end(); ++i)
181
190
  {
182
191
    DrizzleDumpDatabase *database= *i;
188
197
    cout << "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;"
189
198
      << endl << "SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;" << endl;
190
199
  }
 
200
  if (opt_set_charset)
 
201
    cout << "SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;" << endl;
191
202
}
192
203
 
193
204
void generate_dump_db(void)
194
205
{
195
206
  std::vector<DrizzleDumpDatabase*>::iterator i;
196
207
  DrizzleStringBuf sbuf(1024);
197
 
  try
198
 
  {
199
 
    destination_connection= new DrizzleDumpConnection(opt_destination_host,
200
 
      opt_destination_port, opt_destination_user, opt_destination_password,
201
 
      false);
202
 
  }
203
 
  catch (std::exception&)
204
 
  {
205
 
    cerr << "Could not connect to destination database server" << endl;
206
 
    maybe_exit(EX_DRIZZLEERR);
207
 
  }
 
208
  destination_connection= new DrizzleDumpConnection(opt_destination_host,
 
209
    opt_destination_port, opt_destination_user, opt_destination_password,
 
210
    false);
208
211
  sbuf.setConnection(destination_connection);
209
212
  std::ostream sout(&sbuf);
210
 
  sout.exceptions(ios_base::badbit);
 
213
  if (opt_set_charset)
 
214
    sout << "SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;" << endl;
211
215
 
212
216
  if (path.empty())
213
217
  {
215
219
    sout << "SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;" << endl;
216
220
  }
217
221
 
218
 
  if (opt_autocommit)
219
 
    cout << "SET AUTOCOMMIT=0;" << endl;
220
 
 
221
222
  for (i= database_store.begin(); i != database_store.end(); ++i)
222
223
  {
223
 
    try
224
 
    {
225
 
      DrizzleDumpDatabase *database= *i;
226
 
      sout << *database;
227
 
    }
228
 
    catch (std::exception&)
229
 
    {
230
 
      std::cout << _("Error inserting into destination database") << std::endl;
231
 
      if (not ignore_errors)
232
 
        maybe_exit(EX_DRIZZLEERR);
233
 
    }
 
224
    DrizzleDumpDatabase *database= *i;
 
225
    sout << *database;
234
226
  }
235
227
 
236
228
  if (path.empty())
238
230
    sout << "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;" << endl;
239
231
    sout << "SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;" << endl;
240
232
  }
 
233
  if (opt_set_charset)
 
234
    sout << "SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;" << endl;
241
235
}
242
236
 
243
237
/*
294
288
 
295
289
static int get_options(void)
296
290
{
 
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
 
297
300
  if (opt_single_transaction && opt_lock_all_tables)
298
301
  {
299
302
    fprintf(stderr, _("%s: You can't use --single-transaction and "
300
303
                      "--lock-all-tables at the same time.\n"), progname.c_str());
301
304
    return(EX_USAGE);
302
305
  }
 
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
  }
303
311
  if ((opt_databases || opt_alldbs) && ! path.empty())
304
312
  {
305
313
    fprintf(stderr,
357
365
  if (ignore_errors)
358
366
    return;
359
367
  delete db_connection;
360
 
  delete destination_connection;
 
368
  if (destination_connection)
 
369
    delete destination_connection;
361
370
  free_resources();
362
371
  exit(error);
363
372
}
427
436
  if (not database->populateTables(table_names))
428
437
  {
429
438
    delete database;
430
 
    if (not ignore_errors)
431
 
      maybe_exit(EX_DRIZZLEERR);
 
439
    maybe_exit(EX_DRIZZLEERR);
432
440
  }
433
441
 
434
442
  database_store.push_back(database); 
480
488
  N_("Dump all the tablespaces."))
481
489
  ("complete-insert,c", po::value<bool>(&opt_complete_insert)->default_value(false)->zero_tokens(),
482
490
  N_("Use complete insert statements."))
 
491
  ("compress,C", po::value<bool>(&opt_compress)->default_value(false)->zero_tokens(),
 
492
  N_("Use compression in server/client protocol."))
483
493
  ("flush-logs,F", po::value<bool>(&flush_logs)->default_value(false)->zero_tokens(),
484
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"))
485
495
  ("force,f", po::value<bool>(&ignore_errors)->default_value(false)->zero_tokens(),
486
496
  N_("Continue even if we get an sql-error."))
487
497
  ("help,?", N_("Display this help message and exit."))
488
498
  ("lock-all-tables,x", po::value<bool>(&opt_lock_all_tables)->default_value(false)->zero_tokens(),
489
 
  N_("Locks all tables across all databases. This is achieved by taking a global read lock for the duration of the whole dump. Automatically turns --single-transaction off."))
 
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."))
490
502
  ("single-transaction", po::value<bool>(&opt_single_transaction)->default_value(false)->zero_tokens(),
491
 
  N_("Creates a consistent snapshot by dumping all tables in a single transaction. Works ONLY for tables stored in storage engines which support multiversioning (currently only InnoDB does); the dump is NOT guaranteed to be consistent for other storage engines. While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents), no other connection should use the following statements: ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not isolated from them."))
 
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.")) 
492
505
  ("skip-opt", 
493
 
  N_("Disable --opt. Disables --add-drop-table, --add-locks, --create-options, ---extended-insert and --disable-keys."))    
 
506
  N_("Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys."))    
494
507
  ("tables", N_("Overrides option --databases (-B)."))
495
508
  ("show-progress-size", po::value<uint32_t>(&show_progress_size)->default_value(10000),
496
509
  N_("Number of rows before each output progress report (requires --verbose)."))
509
522
  ("add-drop-database", po::value<bool>(&opt_drop_database)->default_value(false)->zero_tokens(),
510
523
  N_("Add a 'DROP DATABASE' before each create."))
511
524
  ("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."))
512
527
  ("compact", po::value<bool>(&opt_compact)->default_value(false)->zero_tokens(),
513
 
  N_("Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs.  Enables options --skip-add-drop-table --no-set-names --skip-disable-keys"))
 
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"))
514
529
  ("databases,B", po::value<bool>(&opt_databases)->default_value(false)->zero_tokens(),
515
530
  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;"))
516
533
  ("skip-disable-keys,K",
517
534
  N_("'ALTER TABLE tb_name DISABLE KEYS; and 'ALTER TABLE tb_name ENABLE KEYS; will not be put in the output."))
518
535
  ("ignore-table", po::value<string>(),
519
536
  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"))
520
537
  ("insert-ignore", po::value<bool>(&opt_ignore)->default_value(false)->zero_tokens(),
521
538
  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 ..."))
522
541
  ("no-autocommit", po::value<bool>(&opt_autocommit)->default_value(false)->zero_tokens(),
523
 
  N_("Wrap a table's data in START TRANSACTION/COMMIT statements."))
 
542
  N_("Wrap tables with autocommit/commit statements."))
524
543
  ("no-create-db,n", po::value<bool>(&opt_create_db)->default_value(false)->zero_tokens(),
525
544
  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."))
526
547
  ("no-data,d", po::value<bool>(&opt_no_data)->default_value(false)->zero_tokens(),
527
548
  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."))
528
553
  ("replace", po::value<bool>(&opt_replace_into)->default_value(false)->zero_tokens(),
529
554
  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)."))
530
557
  ("destination-type", po::value<string>()->default_value("stdout"),
531
558
  N_("Where to send output to (stdout|database"))
532
559
  ("destination-host", po::value<string>(&opt_destination_host)->default_value("localhost"),
533
 
  N_("Hostname for destination db server (requires --destination-type=database)"))
534
 
  ("destination-port", po::value<uint16_t>(&opt_destination_port)->default_value(4427),
535
 
  N_("Port number for destination db server (requires --destination-type=database)"))
 
560
  N_("Hostname for destination db server (requires --destination=database)"))
 
561
  ("destination-port", po::value<uint16_t>(&opt_destination_port)->default_value(3306),
 
562
  N_("Port number for destination db server (requires --destination=database)"))
536
563
  ("destination-user", po::value<string>(&opt_destination_user),
537
 
  N_("User name for destination db server (resquires --destination-type=database)"))
 
564
  N_("User name for destination db server (resquires --destination=database)"))
538
565
  ("destination-password", po::value<string>(&opt_destination_password),
539
 
  N_("Password for destination db server (requires --destination-type=database)"))
 
566
  N_("Password for destination db server (requires --destination=database)"))
540
567
  ("destination-database", po::value<string>(&opt_destination_database),
541
 
  N_("The database in the destination db server (requires --destination-type=database, not for use with --all-databases)"))
 
568
  N_("The database in the destination db server (requires --destination=database, not for use with --all-databases)"))
542
569
  ;
543
570
 
544
571
  po::options_description client_options(N_("Options specific to the client"));
575
602
 
576
603
  std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
577
604
 
578
 
  if (user_config_dir.compare(0, 2, "~/") == 0)
579
 
  {
580
 
    char *homedir;
581
 
    homedir= getenv("HOME");
582
 
    if (homedir != NULL)
583
 
      user_config_dir.replace(0, 1, homedir);
584
 
  }
585
 
 
586
605
  po::positional_options_description p;
587
606
  p.add("database-used", 1);
588
607
  p.add("Table-used",-1);
650
669
  extended_insert= (vm.count("skip-extended-insert")) ? false : true;
651
670
  opt_dump_date= (vm.count("skip-dump-date")) ? false : true;
652
671
  opt_disable_keys= (vm.count("skip-disable-keys")) ? false : true;
 
672
  quick= (vm.count("slow")) ? false : true;
653
673
  opt_quoted= (vm.count("skip-quote-names")) ? false : true;
654
674
 
655
675
  if (vm.count("protocol"))
700
720
      tty_password= true;
701
721
  }
702
722
 
 
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
 
703
734
  if (! path.empty())
704
735
  { 
705
736
    opt_disable_keys= 0;
707
738
 
708
739
  if (vm.count("skip-opt"))
709
740
  {
710
 
    extended_insert= opt_drop= create_options= 0;
711
 
    opt_disable_keys= 0;
 
741
    extended_insert= opt_drop= quick= create_options= 0;
 
742
    opt_disable_keys= opt_set_charset= 0;
712
743
  }
713
744
 
714
745
  if (opt_compact)
715
746
  { 
716
747
    opt_comments= opt_drop= opt_disable_keys= 0;
 
748
    opt_set_charset= 0;
717
749
  }
718
750
 
719
751
  if (vm.count("opt"))
720
752
  {
721
 
    extended_insert= opt_drop= create_options= 1;
722
 
    opt_disable_keys= 1;
 
753
    extended_insert= opt_drop= quick= create_options= 1;
 
754
    opt_disable_keys= opt_set_charset= 1;
723
755
  }
724
756
 
725
757
  if (vm.count("tables"))
749
781
    free_resources();
750
782
    exit(exit_code);
751
783
  }
752
 
  try
753
 
  {
754
 
    db_connection = new DrizzleDumpConnection(current_host, opt_drizzle_port,
755
 
      current_user, opt_password, use_drizzle_protocol);
756
 
  }
757
 
  catch (std::exception&)
758
 
  {
759
 
    maybe_exit(EX_DRIZZLEERR);
760
 
  }
 
784
 
 
785
  db_connection = new DrizzleDumpConnection(current_host, opt_drizzle_port,
 
786
    current_user, opt_password, use_drizzle_protocol);
761
787
 
762
788
  if (db_connection->getServerType() == DrizzleDumpConnection::SERVER_MYSQL_FOUND)
763
789
    db_connection->queryNoResult("SET NAMES 'utf8'");
801
827
    dump_selected_tables(database_used, vm["Table-used"].as< vector<string> >());
802
828
  }
803
829
 
804
 
  if (vm.count("Table-used") and opt_databases)
 
830
  if (vm.count("Table-used") && opt_databases)
805
831
  {
 
832
/*
 
833
 * This is not valid!
806
834
    vector<string> database_used= vm["database-used"].as< vector<string> >();
807
835
    vector<string> table_used= vm["Table-used"].as< vector<string> >();
808
836
 
812
840
    {
813
841
      database_used.insert(database_used.end(), *it);
814
842
    }
815
 
 
816
843
    dump_databases(database_used);
817
 
    dump_all_tables();
 
844
    dump_tables();
 
845
*/
818
846
  }
819
 
 
 
847
  
820
848
  if (vm.count("database-used") && ! vm.count("Table-used"))
821
849
  {
822
850
    dump_databases(vm["database-used"].as< vector<string> >());
823
851
    dump_all_tables();
824
852
  }
825
 
 
826
853
  if (opt_destination == DESTINATION_STDOUT)
827
854
    generate_dump();
828
855
  else
844
871
  */
845
872
err:
846
873
  delete db_connection;
847
 
  delete destination_connection;
 
874
  if (destination_connection)
 
875
    delete destination_connection;
848
876
  if (path.empty())
849
877
    write_footer(md_result_file);
850
878
  free_resources();