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
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
472
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());
473
extra_parser(parse_password_arg).run(), vm);
475
ifstream user_import_ifs("~/.drizzle/drizzleimport.cnf");
448
476
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
478
ifstream system_import_ifs(system_config_dir_import.c_str());
454
479
store(parse_config_file(system_import_ifs, import_options), vm);
481
ifstream user_client_ifs("~/.drizzle/client.cnf");
482
po::store(parse_config_file(user_client_ifs, client_options), vm);
456
484
ifstream system_client_ifs(system_config_dir_client.c_str());
457
485
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
489
if (vm.count("port"))
509
522
if (vm.count("version"))
511
printf("%s Ver %s Distrib %s, for %s-%s (%s)\n", program_name,
524
printf("%s Ver %s Distrib %s, for %s-%s (%s)\n" ,internal::my_progname,
512
525
IMPORT_VERSION, drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
515
528
if (vm.count("help") || argc < 2)
517
printf("%s Ver %s Distrib %s, for %s-%s (%s)\n", program_name,
530
printf("%s Ver %s Distrib %s, for %s-%s (%s)\n" ,internal::my_progname,
518
531
IMPORT_VERSION, drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
519
532
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");
524
537
read the text file directly. In other cases the client will open the text\n\
525
538
file. The SQL command 'LOAD DATA INFILE' is used to import the rows.\n");
527
printf("\nUsage: %s [OPTIONS] database textfile...", program_name);
540
printf("\nUsage: %s [OPTIONS] database textfile...",internal::my_progname);
528
541
cout<<long_options;
569
583
pthread_mutex_lock(&counter_mutex);
571
585
pthread_mutex_unlock(&counter_mutex);
572
fprintf(stderr,"%s: Could not create thread\n", program_name);
586
fprintf(stderr,"%s: Could not create thread\n",
587
internal::my_progname);
590
605
pthread_attr_destroy(&attr);
610
drizzle_con_st *con= 0;
611
drizzle_result_st result;
612
drizzle_return_t ret;
596
613
if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
618
if (drizzle_query_str(con, &result,
619
"/*!40101 set @@character_set_database=binary */;",
621
ret != DRIZZLE_RETURN_OK)
623
db_error(con, &result, ret, NULL);
624
/* We shall countinue here, if --force was given */
628
drizzle_result_free(&result);
631
lock_table(con, argc, argv);
601
632
for (; *argv != NULL; argv++)
602
633
if ((error= write_to_table(*argv, con)))
603
634
if (exitcode == 0)