2
2
Copyright (C) 2010 Vijay Samuel
3
Copyright (C) 2010 Brian Aker
4
3
Copyright (C) 2000-2006 MySQL AB
5
Copyright (C) 2008-2009 Sun Microsystems, Inc.
4
Copyright (C) 2008-2009 Sun Microsystems, Inc
7
6
This program is free software; you can redistribute it and/or modify
8
7
it under the terms of the GNU General Public License as published by
16
15
You should have received a copy of the GNU General Public License
17
16
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
20
#define IMPORT_VERSION "4.0"
20
** drizzleimport.c - Imports all given files
23
** *************************
25
** * AUTHOR: Monty & Jani *
26
** * DATE: June 24, 1997 *
28
** *************************
30
#define IMPORT_VERSION "3.7"
22
32
#include "client_priv.h"
52
60
static char *add_load_option(char *ptr,const char *object,
53
61
const char *statement);
55
static bool verbose= false, ignore_errors= false,
63
static bool verbose= false, lock_tables= false, ignore_errors= false,
56
64
opt_delete= false, opt_replace= false, silent= false,
57
65
ignore_unique= false, opt_low_priority= false,
58
use_drizzle_protocol= false, opt_local_file;
66
tty_password= false, opt_mysql= false, opt_local_file;
60
static uint32_t opt_use_threads;
68
static uint32_t opt_use_threads= 0;
61
69
static uint32_t opt_drizzle_port= 0;
62
70
static int64_t opt_ignore_lines= -1;
104
111
drizzle_return_t ret;
106
113
internal::fn_format(tablename, filename, "", "", 1 | 2); /* removes path & ext. */
107
if (not opt_local_file)
108
115
strcpy(hard_path,filename);
110
117
internal::my_load_path(hard_path, filename, NULL); /* filename includes the path */
199
static void lock_table(drizzle_con_st *con, int tablecount, char **raw_tablename)
203
char tablename[FN_REFLEN];
204
drizzle_result_st result;
205
drizzle_return_t ret;
208
fprintf(stdout, "Locking tables for write\n");
209
query.append("LOCK TABLES ");
210
for (i=0 ; i < tablecount ; i++)
212
internal::fn_format(tablename, raw_tablename[i], "", "", 1 | 2);
213
query.append(tablename);
214
query.append(" WRITE,");
216
if (drizzle_query(con, &result, query.c_str(), query.length()-1,
218
ret != DRIZZLE_RETURN_OK)
220
db_error(con, &result, ret, NULL);
221
/* We shall countinue here, if --force was given */
224
drizzle_result_free(&result);
192
228
static drizzle_con_st *db_connect(const string host, const string database,
193
229
const string user, const string passwd)
197
233
drizzle_return_t ret;
200
fprintf(stdout, "Connecting to %s, using protocol %s...\n", ! host.empty() ? host.c_str() : "localhost", opt_protocol.c_str());
236
fprintf(stdout, "Connecting to %s\n", ! host.empty() ? host.c_str() : "localhost");
201
237
if (!(drizzle= drizzle_create(NULL)))
203
239
if (!(con= drizzle_con_add_tcp(drizzle,NULL,(char *)host.c_str(),opt_drizzle_port,(char *)user.c_str(),(char *)passwd.c_str(),
204
(char *)database.c_str(), use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL)))
240
(char *)database.c_str(), opt_mysql ? DRIZZLE_CON_MYSQL : DRIZZLE_CON_NONE)))
246
282
if (ret == DRIZZLE_RETURN_ERROR_CODE)
248
fprintf(stdout, "Error: %d, %s%s%s",
249
drizzle_result_error_code(result),
250
drizzle_result_error(result),
251
table ? ", when using table: " : "", table ? table : "");
284
my_printf_error(0,"Error: %d, %s%s%s", MYF(0),
285
drizzle_result_error_code(result),
286
drizzle_result_error(result),
287
table ? ", when using table: " : "", table ? table : "");
252
288
drizzle_result_free(result);
256
fprintf(stdout, "Error: %d, %s%s%s", ret, drizzle_con_error(con),
257
table ? ", when using table: " : "", table ? table : "");
292
my_printf_error(0,"Error: %d, %s%s%s", MYF(0), ret, drizzle_con_error(con),
293
table ? ", when using table: " : "", table ? table : "");
260
296
safe_exit(1, con);
316
352
char *raw_table_name= (char *)arg;
353
drizzle_con_st *con= NULL;
354
drizzle_result_st result;
355
drizzle_return_t ret;
319
357
if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
362
if (drizzle_query_str(con, &result,
363
"/*!40101 set @@character_set_database=binary */;",
365
ret != DRIZZLE_RETURN_OK)
367
db_error(con, &result, ret, NULL);
368
/* We shall countinue here, if --force was given */
325
373
We are not currently catching the error here.
327
if ((error= write_to_table(raw_table_name, con)))
375
if((error= write_to_table(raw_table_name, con)))
329
376
if (exitcode == 0)
337
381
db_disconnect(current_host, con);
340
383
pthread_mutex_lock(&counter_mutex);
342
385
pthread_cond_signal(&count_threshhold);
343
386
pthread_mutex_unlock(&counter_mutex);
387
internal::my_thread_end();
362
406
("help,?", "Displays this help and exits.")
363
407
("ignore,i", po::value<bool>(&ignore_unique)->default_value(false)->zero_tokens(),
364
408
"If duplicate unique key was found, keep old row.")
409
("lock-tables,l", po::value<bool>(&lock_tables)->default_value(false)->zero_tokens(),
410
"Lock all tables for write (this disables threads).")
365
411
("low-priority", po::value<bool>(&opt_low_priority)->default_value(false)->zero_tokens(),
366
412
"Use LOW_PRIORITY when updating the table.")
367
413
("replace,r", po::value<bool>(&opt_replace)->default_value(false)->zero_tokens(),
393
439
"Read all files through the client.")
394
440
("silent,s", po::value<bool>(&silent)->default_value(false)->zero_tokens(),
395
441
"Be more silent.")
396
("use-threads", po::value<uint32_t>(&opt_use_threads)->default_value(4),
397
"Load files in parallel. The argument is the number of threads to use for loading data (default is 4.")
442
("use-threads", po::value<uint32_t>(&opt_use_threads)->default_value(0),
443
"Load files in parallel. The argument is the number of threads to use for loading data.")
400
446
po::options_description client_options("Options specific to the client");
401
447
client_options.add_options()
402
448
("host,h", po::value<string>(¤t_host)->default_value("localhost"),
403
449
"Connect to host.")
450
("mysql,m", po::value<bool>(&opt_mysql)->default_value(true)->zero_tokens(),
451
N_("Use MySQL Protocol."))
404
452
("password,P", po::value<string>(&password),
405
453
"Password to use when connecting to server. If password is not given it's asked from the tty." )
406
454
("port,p", po::value<uint32_t>(&opt_drizzle_port)->default_value(0),
407
455
"Port number to use for connection")
408
("protocol", po::value<string>(&opt_protocol)->default_value("mysql"),
409
"The protocol of connection (mysql or drizzle).")
456
("protocol", po::value<string>(),
457
"The protocol of connection (tcp,socket,pipe,memory).")
410
458
("user,u", po::value<string>(¤t_user)->default_value(""),
411
459
"User for login if not current user.")
420
468
std::string system_config_dir_client(SYSCONFDIR);
421
469
system_config_dir_client.append("/drizzle/client.cnf");
423
std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
425
if (user_config_dir.compare(0, 2, "~/") == 0)
428
homedir= getenv("HOME");
430
user_config_dir.replace(0, 1, homedir);
433
471
po::variables_map vm;
435
// Disable allow_guessing
436
int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
438
po::store(po::command_line_parser(argc, argv).options(long_options).
439
style(style).extra_parser(parse_password_arg).run(), vm);
441
std::string user_config_dir_import(user_config_dir);
442
user_config_dir_import.append("/drizzle/drizzleimport.cnf");
444
std::string user_config_dir_client(user_config_dir);
445
user_config_dir_client.append("/drizzle/client.cnf");
447
ifstream user_import_ifs(user_config_dir_import.c_str());
472
po::store(po::command_line_parser(argc, argv).options(long_options).run(), vm);
474
ifstream user_import_ifs("~/.drizzle/drizzleimport.cnf");
448
475
po::store(parse_config_file(user_import_ifs, import_options), vm);
450
ifstream user_client_ifs(user_config_dir_client.c_str());
451
po::store(parse_config_file(user_client_ifs, client_options), vm);
453
477
ifstream system_import_ifs(system_config_dir_import.c_str());
454
478
store(parse_config_file(system_import_ifs, import_options), vm);
480
ifstream user_client_ifs("~/.drizzle/client.cnf");
481
po::store(parse_config_file(user_client_ifs, client_options), vm);
456
483
ifstream system_client_ifs(system_config_dir_client.c_str());
457
484
po::store(parse_config_file(system_client_ifs, client_options), vm);
460
if (vm.count("protocol"))
462
std::transform(opt_protocol.begin(), opt_protocol.end(),
463
opt_protocol.begin(), ::tolower);
465
if (not opt_protocol.compare("mysql"))
466
use_drizzle_protocol=false;
467
else if (not opt_protocol.compare("drizzle"))
468
use_drizzle_protocol=true;
471
cout << _("Error: Unknown protocol") << " '" << opt_protocol << "'" << endl;
476
488
if (vm.count("port"))
489
if( vm.count("password") )
501
if (vm.count("password"))
491
503
if (!opt_password.empty())
492
504
opt_password.erase();
493
if (password == PASSWORD_SENTINEL)
499
opt_password= password;
505
opt_password= password;
506
if (opt_password.c_str() == NULL)
508
fprintf(stderr, _("Memory allocation error while copying password. "
510
exit(EXIT_OUT_OF_MEMORY);
512
char *start= (char *)password.c_str();
513
char *temp_pass= (char *)password.c_str();
516
/* Overwriting password with 'x' */
505
526
tty_password= true;
509
529
if (vm.count("version"))
511
printf("%s Ver %s Distrib %s, for %s-%s (%s)\n", program_name,
531
printf("%s Ver %s Distrib %s, for %s-%s (%s)\n" ,internal::my_progname,
512
532
IMPORT_VERSION, drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
515
535
if (vm.count("help") || argc < 2)
517
printf("%s Ver %s Distrib %s, for %s-%s (%s)\n", program_name,
537
printf("%s Ver %s Distrib %s, for %s-%s (%s)\n" ,internal::my_progname,
518
538
IMPORT_VERSION, drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
519
539
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");
569
590
pthread_mutex_lock(&counter_mutex);
571
592
pthread_mutex_unlock(&counter_mutex);
572
fprintf(stderr,"%s: Could not create thread\n", program_name);
593
fprintf(stderr,"%s: Could not create thread\n",
594
internal::my_progname);
590
612
pthread_attr_destroy(&attr);
617
drizzle_con_st *con= 0;
618
drizzle_result_st result;
619
drizzle_return_t ret;
596
620
if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
625
if (drizzle_query_str(con, &result,
626
"/*!40101 set @@character_set_database=binary */;",
628
ret != DRIZZLE_RETURN_OK)
630
db_error(con, &result, ret, NULL);
631
/* We shall countinue here, if --force was given */
635
drizzle_result_free(&result);
638
lock_table(con, argc, argv);
601
639
for (; *argv != NULL; argv++)
602
640
if ((error= write_to_table(*argv, con)))
603
641
if (exitcode == 0)