27
27
#define IMPORT_VERSION "3.7"
29
#include "client_priv.h"
32
#include "client_priv.h"
33
32
#include <pthread.h>
34
/* Added this for string translation. */
35
#include <drizzled/gettext.h>
35
37
using namespace std;
37
39
/* Global Thread counter */
39
41
pthread_mutex_t counter_mutex;
40
42
pthread_cond_t count_threshhold;
42
static void db_error_with_table(DRIZZLE *drizzle, char *table);
43
static void db_error(DRIZZLE *drizzle);
44
static char *field_escape(char *to,const char *from,uint length);
44
static void db_error(drizzle_con_st *con, drizzle_result_st *result,
45
drizzle_return_t ret, char *table);
46
static char *field_escape(char *to,const char *from,uint32_t length);
45
47
static char *add_load_option(char *ptr,const char *object,
46
48
const char *statement);
48
static bool verbose=0,lock_tables=0,ignore_errors=0,opt_delete=0,
49
opt_replace=0,silent=0,ignore=0,opt_compress=0,
50
opt_low_priority= 0, tty_password= 0;
51
static bool debug_info_flag= 0, debug_check_flag= 0;
52
static uint opt_use_threads=0, opt_local_file=0, my_end_arg= 0;
53
static char *opt_password=0, *current_user=0,
54
*current_host=0, *current_db=0, *fields_terminated=0,
55
*lines_terminated=0, *enclosed=0, *opt_enclosed=0,
56
*escaped=0, *opt_columns=0,
50
static bool verbose= false, lock_tables= false, ignore_errors= false,
51
opt_delete= false, opt_replace= false, silent= false,
52
ignore_unique= false, opt_compress= false, opt_low_priority= false,
54
static bool debug_info_flag= false, debug_check_flag= false;
55
static uint32_t opt_use_threads= 0, opt_local_file= 0, my_end_arg= 0;
56
static char *opt_password= NULL, *current_user= NULL,
57
*current_host= NULL, *current_db= NULL, *fields_terminated= NULL,
58
*lines_terminated= NULL, *enclosed= NULL, *opt_enclosed= NULL,
59
*escaped= NULL, *opt_columns= NULL,
57
60
*default_charset= (char*) DRIZZLE_DEFAULT_CHARSET_NAME;
58
static uint opt_drizzle_port= 0, opt_protocol= 0;
59
static char * opt_drizzle_unix_port=0;
61
static uint32_t opt_drizzle_port= 0;
62
static char * opt_drizzle_unix_port= 0;
60
63
static int64_t opt_ignore_lines= -1;
61
64
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
63
66
static struct my_option my_long_options[] =
65
{"character-sets-dir", OPT_CHARSETS_DIR,
66
"Directory where character sets are.", (char**) &charsets_dir,
67
(char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
68
68
{"default-character-set", OPT_DEFAULT_CHARSET,
69
69
"Set the default character set.", (char**) &default_charset,
70
70
(char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
105
105
{"host", 'h', "Connect to host.", (char**) ¤t_host,
106
106
(char**) ¤t_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
107
107
{"ignore", 'i', "If duplicate unique key was found, keep old row.",
108
(char**) &ignore, (char**) &ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
108
(char**) &ignore_unique, (char**) &ignore_unique, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
109
109
{"ignore-lines", OPT_IGN_LINES, "Ignore first n lines of data infile.",
110
110
(char**) &opt_ignore_lines, (char**) &opt_ignore_lines, 0, GET_LL,
111
111
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
120
120
{"low-priority", OPT_LOW_PRIORITY,
121
121
"Use LOW_PRIORITY when updating the table.", (char**) &opt_low_priority,
122
122
(char**) &opt_low_priority, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
124
124
"Password to use when connecting to server. If password is not given it's asked from the tty.",
125
125
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
126
{"port", 'P', "Port number to use for connection or 0 for default to, in "
127
"order of preference, my.cnf, $DRIZZLE_TCP_PORT, "
126
{"port", 'p', "Port number to use for connection or 0 for default to, in "
127
"order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
128
128
"built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
129
(char**) &opt_drizzle_port,
130
(char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
129
0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
132
130
{"protocol", OPT_DRIZZLE_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
133
131
0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
134
132
{"replace", 'r', "If duplicate unique key was found, replace old row.",
177
175
file. The SQL command 'LOAD DATA INFILE' is used to import the rows.\n");
179
177
printf("\nUsage: %s [OPTIONS] database textfile...",my_progname);
180
print_defaults("my",load_default_groups);
178
print_defaults("drizzle",load_default_groups);
181
179
my_print_help(my_long_options);
182
180
my_print_variables(my_long_options);
186
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
184
bool get_one_option(int optid, const struct my_option *, char *argument)
187
uint64_t temp_drizzle_port= 0;
191
temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
192
/* if there is an alpha character this is not a valid port */
193
if (strlen(endchar) != 0)
195
fprintf(stderr, _("Non-integer value supplied for port. If you are trying to enter a password please use --password instead.\n"));
198
/* If the port number is > 65535 it is not a valid port
199
This also helps with potential data loss casting unsigned long to a
201
if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
203
fprintf(stderr, _("Value supplied for port is not valid.\n"));
208
opt_drizzle_port= (uint32_t) temp_drizzle_port;
193
214
char *start=argument;
195
opt_password=my_strdup(argument,MYF(MY_FAE));
196
while (*argument) *argument++= 'x'; /* Destroy argument */
217
opt_password = strdup(argument);
218
if (opt_password == NULL)
220
fprintf(stderr, "Memory allocation error while copying password. "
226
/* Overwriting password with 'x' */
198
start[1]=0; /* Cut length of argument */
231
/* Cut length of argument */
246
280
current_db= *((*argv)++);
248
282
if (tty_password)
249
opt_password=get_tty_password(NULL);
283
opt_password=client_get_tty_password(NULL);
255
static int write_to_table(char *filename, DRIZZLE *drizzle)
289
static int write_to_table(char *filename, drizzle_con_st *con)
257
291
char tablename[FN_REFLEN], hard_path[FN_REFLEN],
258
292
sql_statement[FN_REFLEN*16+256], *end;
293
drizzle_result_st result;
294
drizzle_return_t ret;
260
296
fn_format(tablename, filename, "", "", 1 | 2); /* removes path & ext. */
261
297
if (!opt_local_file)
262
my_stpcpy(hard_path,filename);
298
strcpy(hard_path,filename);
264
300
my_load_path(hard_path, filename, NULL); /* filename includes the path */
293
330
opt_local_file ? "LOCAL" : "", hard_path);
294
331
end= strchr(sql_statement, '\0');
296
end= my_stpcpy(end, " REPLACE");
298
end= my_stpcpy(end, " IGNORE");
299
end= my_stpcpy(my_stpcpy(end, " INTO TABLE "), tablename);
333
end= strcpy(end, " REPLACE")+8;
335
end= strcpy(end, " IGNORE")+7;
337
end+= sprintf(end, " INTO TABLE %s", tablename);
301
339
if (fields_terminated || enclosed || opt_enclosed || escaped)
302
end= my_stpcpy(end, " FIELDS");
340
end= strcpy(end, " FIELDS")+7;
303
341
end= add_load_option(end, fields_terminated, " TERMINATED BY");
304
342
end= add_load_option(end, enclosed, " ENCLOSED BY");
305
343
end= add_load_option(end, opt_enclosed,
307
345
end= add_load_option(end, escaped, " ESCAPED BY");
308
346
end= add_load_option(end, lines_terminated, " LINES TERMINATED BY");
309
347
if (opt_ignore_lines >= 0)
310
end= my_stpcpy(int64_t10_to_str(opt_ignore_lines,
311
my_stpcpy(end, " IGNORE "),10), " LINES");
349
end= strcpy(end, " IGNORE ")+8;
350
ostringstream buffer;
351
buffer << opt_ignore_lines;
352
end= strcpy(end, buffer.str().c_str())+ buffer.str().size();
353
end= strcpy(end, " LINES")+6;
313
end= my_stpcpy(my_stpcpy(my_stpcpy(end, " ("), opt_columns), ")");
357
end= strcpy(end, " (")+2;
358
end= strcpy(end, opt_columns)+strlen(opt_columns);
359
end= strcpy(end, ")")+1;
316
if (drizzle_query(drizzle, sql_statement))
363
if (drizzle_query_str(con, &result, sql_statement, &ret) == NULL ||
364
ret != DRIZZLE_RETURN_OK)
318
db_error_with_table(drizzle, tablename);
366
db_error(con, &result, ret, tablename);
323
if (drizzle_info(drizzle)) /* If NULL-pointer, print nothing */
371
if (strcmp(drizzle_result_info(&result), ""))
325
373
fprintf(stdout, "%s.%s: %s\n", current_db, tablename,
326
drizzle_info(drizzle));
374
drizzle_result_info(&result));
377
drizzle_result_free(&result);
334
static void lock_table(DRIZZLE *drizzle, int tablecount, char **raw_tablename)
382
static void lock_table(drizzle_con_st *con, int tablecount, char **raw_tablename)
338
386
char tablename[FN_REFLEN];
387
drizzle_result_st result;
388
drizzle_return_t ret;
341
391
fprintf(stdout, "Locking tables for write\n");
346
396
query.append(tablename);
347
397
query.append(" WRITE,");
349
if (drizzle_real_query(drizzle, query.c_str(), query.length()-1))
350
db_error(drizzle); /* We shall countinue here, if --force was given */
399
if (drizzle_query(con, &result, query.c_str(), query.length()-1,
401
ret != DRIZZLE_RETURN_OK)
403
db_error(con, &result, ret, NULL);
404
/* We shall countinue here, if --force was given */
407
drizzle_result_free(&result);
356
static DRIZZLE *db_connect(char *host, char *database,
357
char *user, char *passwd)
411
static drizzle_con_st *db_connect(char *host, char *database,
412
char *user, char *passwd)
416
drizzle_return_t ret;
361
419
fprintf(stdout, "Connecting to %s\n", host ? host : "localhost");
362
420
if (!(drizzle= drizzle_create(NULL)))
365
drizzle_options(drizzle,DRIZZLE_OPT_COMPRESS,NULL);
367
drizzle_options(drizzle,DRIZZLE_OPT_LOCAL_INFILE,
368
(char*) &opt_local_file);
370
drizzle_options(drizzle,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
371
if (!(drizzle_connect(drizzle,host,user,passwd,
372
database,opt_drizzle_port,opt_drizzle_unix_port,
422
if (!(con= drizzle_con_add_tcp(drizzle,NULL,host,opt_drizzle_port,user,passwd,
423
database, DRIZZLE_CON_NONE)))
428
if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
375
430
ignore_errors=0; /* NO RETURN FROM db_error */
431
db_error(con, NULL, ret, NULL);
378
drizzle->reconnect= 0;
380
435
fprintf(stdout, "Selecting database %s\n", database);
381
if (drizzle_select_db(drizzle, database))
391
static void db_disconnect(char *host, DRIZZLE *drizzle)
442
static void db_disconnect(char *host, drizzle_con_st *con)
394
445
fprintf(stdout, "Disconnecting from %s\n", host ? host : "localhost");
395
drizzle_close(drizzle);
446
drizzle_free(drizzle_con_drizzle(con));
400
static void safe_exit(int error, DRIZZLE *drizzle)
451
static void safe_exit(int error, drizzle_con_st *con)
402
453
if (ignore_errors)
405
drizzle_close(drizzle);
456
drizzle_free(drizzle_con_drizzle(con));
411
static void db_error_with_table(DRIZZLE *drizzle, char *table)
413
my_printf_error(0,"Error: %d, %s, when using table: %s",
414
MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle), table);
415
safe_exit(1, drizzle);
420
static void db_error(DRIZZLE *drizzle)
422
my_printf_error(0,"Error: %d %s", MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle));
423
safe_exit(1, drizzle);
462
static void db_error(drizzle_con_st *con, drizzle_result_st *result,
463
drizzle_return_t ret, char *table)
465
if (ret == DRIZZLE_RETURN_ERROR_CODE)
467
my_printf_error(0,"Error: %d, %s%s%s", MYF(0),
468
drizzle_result_error_code(result),
469
drizzle_result_error(result),
470
table ? ", when using table: " : "", table ? table : "");
471
drizzle_result_free(result);
475
my_printf_error(0,"Error: %d, %s%s%s", MYF(0), ret, drizzle_con_error(con),
476
table ? ", when using table: " : "", table ? table : "");
432
488
/* Don't escape hex constants */
433
489
if (object[0] == '0' && (object[1] == 'x' || object[1] == 'X'))
434
ptr= strxmov(ptr," ",statement," ",object,NULL);
490
ptr+= sprintf(ptr, " %s %s", statement, object);
437
493
/* char constant; escape */
438
ptr= strxmov(ptr," ",statement," '",NULL);
439
ptr= field_escape(ptr,object,(uint) strlen(object));
494
ptr+= sprintf(ptr, " %s '", statement);
495
ptr= field_escape(ptr,object,(uint32_t) strlen(object));
470
526
/* Add missing backslashes if user has specified odd number of backs.*/
471
527
if (end_backslashes)
478
static void * worker_thread(void *arg)
535
void * worker_thread(void *arg)
481
538
char *raw_table_name= (char *)arg;
539
drizzle_con_st *con= NULL;
540
drizzle_result_st result;
541
drizzle_return_t ret;
484
if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
543
if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
489
if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
548
if (drizzle_query_str(con, &result,
549
"/*!40101 set @@character_set_database=binary */;",
551
ret != DRIZZLE_RETURN_OK)
491
db_error(drizzle); /* We shall countinue here, if --force was given */
553
db_error(con, &result, ret, NULL);
554
/* We shall countinue here, if --force was given */
496
559
We are not currently catching the error here.
498
if((error= write_to_table(raw_table_name, drizzle)))
561
if((error= write_to_table(raw_table_name, con)))
499
562
if (exitcode == 0)
504
db_disconnect(current_host, drizzle);
567
db_disconnect(current_host, con);
506
569
pthread_mutex_lock(&counter_mutex);
588
if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
650
drizzle_con_st *con= 0;
651
drizzle_result_st result;
652
drizzle_return_t ret;
653
if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
590
655
free_defaults(argv_to_free);
591
656
return(1); /* purecov: deadcode */
594
if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
659
if (drizzle_query_str(con, &result,
660
"/*!40101 set @@character_set_database=binary */;",
662
ret != DRIZZLE_RETURN_OK)
596
db_error(drizzle); /* We shall countinue here, if --force was given */
664
db_error(con, &result, ret, NULL);
665
/* We shall countinue here, if --force was given */
669
drizzle_result_free(&result);
601
lock_table(drizzle, argc, argv);
672
lock_table(con, argc, argv);
602
673
for (; *argv != NULL; argv++)
603
if ((error= write_to_table(*argv, drizzle)))
674
if ((error= write_to_table(*argv, con)))
604
675
if (exitcode == 0)
606
db_disconnect(current_host, drizzle);
677
db_disconnect(current_host, con);
608
679
free(opt_password);
609
680
free_defaults(argv_to_free);