14
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
** mysqlimport.c - Imports all given files
17
** drizzleimport.c - Imports all given files
20
** *************************
22
** * AUTHOR: Monty & Jani *
23
** * DATE: June 24, 1997 *
25
** *************************
20
** *************************
22
** * AUTHOR: Monty & Jani *
23
** * DATE: June 24, 1997 *
25
** *************************
27
27
#define IMPORT_VERSION "3.7"
36
36
pthread_mutex_t counter_mutex;
37
37
pthread_cond_t count_threshhold;
39
static void db_error_with_table(MYSQL *mysql, char *table);
40
static void db_error(MYSQL *mysql);
39
static void db_error_with_table(DRIZZLE *drizzle, char *table);
40
static void db_error(DRIZZLE *drizzle);
41
41
static char *field_escape(char *to,const char *from,uint length);
42
42
static char *add_load_option(char *ptr,const char *object,
43
const char *statement);
43
const char *statement);
45
static bool verbose=0,lock_tables=0,ignore_errors=0,opt_delete=0,
46
replace=0,silent=0,ignore=0,opt_compress=0,
45
static bool verbose=0,lock_tables=0,ignore_errors=0,opt_delete=0,
46
replace=0,silent=0,ignore=0,opt_compress=0,
47
47
opt_low_priority= 0, tty_password= 0;
48
48
static bool debug_info_flag= 0, debug_check_flag= 0;
49
49
static uint opt_use_threads=0, opt_local_file=0, my_end_arg= 0;
50
static char *opt_password=0, *current_user=0,
51
*current_host=0, *current_db=0, *fields_terminated=0,
52
*lines_terminated=0, *enclosed=0, *opt_enclosed=0,
53
*escaped=0, *opt_columns=0,
54
*default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
55
static uint opt_mysql_port= 0, opt_protocol= 0;
56
static char * opt_mysql_unix_port=0;
50
static char *opt_password=0, *current_user=0,
51
*current_host=0, *current_db=0, *fields_terminated=0,
52
*lines_terminated=0, *enclosed=0, *opt_enclosed=0,
53
*escaped=0, *opt_columns=0,
54
*default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
55
static uint opt_drizzle_port= 0, opt_protocol= 0;
56
static char * opt_drizzle_unix_port=0;
57
57
static int64_t opt_ignore_lines= -1;
58
58
static CHARSET_INFO *charset_info= &my_charset_latin1;
112
112
{"local", 'L', "Read all files through the client.", (char**) &opt_local_file,
113
113
(char**) &opt_local_file, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
114
114
{"lock-tables", 'l', "Lock all tables for write (this disables threads).",
115
(char**) &lock_tables, (char**) &lock_tables, 0, GET_BOOL, NO_ARG,
115
(char**) &lock_tables, (char**) &lock_tables, 0, GET_BOOL, NO_ARG,
116
116
0, 0, 0, 0, 0, 0},
117
117
{"low-priority", OPT_LOW_PRIORITY,
118
118
"Use LOW_PRIORITY when updating the table.", (char**) &opt_low_priority,
126
126
"/etc/services, "
128
128
"built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
129
(char**) &opt_mysql_port,
130
(char**) &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
129
(char**) &opt_drizzle_port,
130
(char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
132
{"protocol", OPT_MYSQL_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
132
{"protocol", OPT_DRIZZLE_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
133
133
0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
134
134
{"replace", 'r', "If duplicate unique key was found, replace old row.",
135
135
(char**) &replace, (char**) &replace, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
136
136
{"silent", 's', "Be more silent.", (char**) &silent, (char**) &silent, 0,
137
137
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
138
138
{"socket", 'S', "Socket file to use for connection.",
139
(char**) &opt_mysql_unix_port, (char**) &opt_mysql_unix_port, 0, GET_STR,
139
(char**) &opt_drizzle_unix_port, (char**) &opt_drizzle_unix_port, 0, GET_STR,
140
140
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
141
141
{"use-threads", OPT_USE_THREADS,
142
142
"Load files in parallel. The argument is the number "
143
143
"of threads to use for loading data.",
144
(char**) &opt_use_threads, (char**) &opt_use_threads, 0,
144
(char**) &opt_use_threads, (char**) &opt_use_threads, 0,
145
145
GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
146
146
#ifndef DONT_ALLOW_USER_CHANGE
147
147
{"user", 'u', "User for login if not current user.", (char**) ¤t_user,
158
static const char *load_default_groups[]= { "mysqlimport","client",0 };
158
static const char *load_default_groups[]= { "drizzleimport","client",0 };
160
160
static void print_version(void)
162
162
printf("%s Ver %s Distrib %s, for %s (%s)\n" ,my_progname,
163
IMPORT_VERSION, MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
163
IMPORT_VERSION, MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
167
167
static void usage(void)
170
puts("Copyright (C) 2000-2006 MySQL AB");
170
puts("Copyright (C) 2008 Drizzle Open Source Development Team");
171
171
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");
173
173
Loads tables from text files in various formats. The base name of the\n\
174
174
text file must be the name of the table that should be used.\n\
175
If one uses sockets to connect to the MySQL server, the server will open and\n\
175
If one uses sockets to connect to the Drizzle server, the server will open and\n\
176
176
read the text file directly. In other cases the client will open the text\n\
177
177
file. The SQL command 'LOAD DATA INFILE' is used to import the rows.\n");
286
286
if (opt_local_file)
287
287
fprintf(stdout, "Loading data from LOCAL file: %s into %s\n",
288
hard_path, tablename);
288
hard_path, tablename);
290
290
fprintf(stdout, "Loading data from SERVER file: %s into %s\n",
291
hard_path, tablename);
291
hard_path, tablename);
293
293
sprintf(sql_statement, "LOAD DATA %s %s INFILE '%s'",
294
opt_low_priority ? "LOW_PRIORITY" : "",
295
opt_local_file ? "LOCAL" : "", hard_path);
294
opt_low_priority ? "LOW_PRIORITY" : "",
295
opt_local_file ? "LOCAL" : "", hard_path);
296
296
end= strend(sql_statement);
298
298
end= strmov(end, " REPLACE");
305
305
end= add_load_option(end, fields_terminated, " TERMINATED BY");
306
306
end= add_load_option(end, enclosed, " ENCLOSED BY");
307
307
end= add_load_option(end, opt_enclosed,
308
" OPTIONALLY ENCLOSED BY");
308
" OPTIONALLY ENCLOSED BY");
309
309
end= add_load_option(end, escaped, " ESCAPED BY");
310
310
end= add_load_option(end, lines_terminated, " LINES TERMINATED BY");
311
311
if (opt_ignore_lines >= 0)
312
end= strmov(int64_t10_to_str(opt_ignore_lines,
313
strmov(end, " IGNORE "),10), " LINES");
312
end= strmov(int64_t10_to_str(opt_ignore_lines,
313
strmov(end, " IGNORE "),10), " LINES");
315
315
end= strmov(strmov(strmov(end, " ("), opt_columns), ")");
318
if (mysql_query(mysql, sql_statement))
318
if (drizzle_query(drizzle, sql_statement))
320
db_error_with_table(mysql, tablename);
320
db_error_with_table(drizzle, tablename);
325
if (mysql_info(mysql)) /* If NULL-pointer, print nothing */
325
if (drizzle_info(drizzle)) /* If NULL-pointer, print nothing */
327
327
fprintf(stdout, "%s.%s: %s\n", current_db, tablename,
328
drizzle_info(drizzle));
348
348
dynstr_append(&query, tablename);
349
349
dynstr_append(&query, " WRITE,");
351
if (mysql_real_query(mysql, query.str, query.length-1))
352
db_error(mysql); /* We shall countinue here, if --force was given */
351
if (drizzle_real_query(drizzle, query.str, query.length-1))
352
db_error(drizzle); /* We shall countinue here, if --force was given */
358
static MYSQL *db_connect(char *host, char *database,
358
static DRIZZLE *db_connect(char *host, char *database,
359
359
char *user, char *passwd)
363
363
fprintf(stdout, "Connecting to %s\n", host ? host : "localhost");
364
if (!(mysql= mysql_init(NULL)))
364
if (!(drizzle= drizzle_create(NULL)))
366
366
if (opt_compress)
367
mysql_options(mysql,MYSQL_OPT_COMPRESS,NullS);
367
drizzle_options(drizzle,DRIZZLE_OPT_COMPRESS,NullS);
368
368
if (opt_local_file)
369
mysql_options(mysql,MYSQL_OPT_LOCAL_INFILE,
370
(char*) &opt_local_file);
369
drizzle_options(drizzle,DRIZZLE_OPT_LOCAL_INFILE,
370
(char*) &opt_local_file);
371
371
if (opt_protocol)
372
mysql_options(mysql,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
373
if (!(mysql_real_connect(mysql,host,user,passwd,
374
database,opt_mysql_port,opt_mysql_unix_port,
372
drizzle_options(drizzle,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
373
if (!(drizzle_connect(drizzle,host,user,passwd,
374
database,opt_drizzle_port,opt_drizzle_unix_port,
377
ignore_errors=0; /* NO RETURN FROM db_error */
377
ignore_errors=0; /* NO RETURN FROM db_error */
380
drizzle->reconnect= 0;
382
382
fprintf(stdout, "Selecting database %s\n", database);
383
if (mysql_select_db(mysql, database))
383
if (drizzle_select_db(drizzle, database))
393
static void db_disconnect(char *host, MYSQL *mysql)
393
static void db_disconnect(char *host, DRIZZLE *drizzle)
396
396
fprintf(stdout, "Disconnecting from %s\n", host ? host : "localhost");
397
drizzle_close(drizzle);
402
static void safe_exit(int error, MYSQL *mysql)
402
static void safe_exit(int error, DRIZZLE *drizzle)
404
404
if (ignore_errors)
407
drizzle_close(drizzle);
413
static void db_error_with_table(MYSQL *mysql, char *table)
413
static void db_error_with_table(DRIZZLE *drizzle, char *table)
415
415
my_printf_error(0,"Error: %d, %s, when using table: %s",
416
MYF(0), mysql_errno(mysql), mysql_error(mysql), table);
416
MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle), table);
417
safe_exit(1, drizzle);
422
static void db_error(MYSQL *mysql)
422
static void db_error(DRIZZLE *drizzle)
424
my_printf_error(0,"Error: %d %s", MYF(0), mysql_errno(mysql), mysql_error(mysql));
424
my_printf_error(0,"Error: %d %s", MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle));
425
safe_exit(1, drizzle);
429
429
static char *add_load_option(char *ptr, const char *object,
430
const char *statement)
430
const char *statement)
450
450
** "'", "\", "\\" (escaped backslash), "\t" (tab), "\n" (newline)
451
451
** This is done by doubleing ' and add a end -\ if needed to avoid
452
452
** syntax errors from the SQL parser.
455
455
static char *field_escape(char *to,const char *from,uint length)
458
uint end_backslashes=0;
458
uint end_backslashes=0;
460
460
for (end= from+length; from != end; from++)
463
463
if (*from == '\\')
464
464
end_backslashes^=1; /* find odd number of backslashes */
467
467
if (*from == '\'' && !end_backslashes)
468
*to++= *from; /* We want a dublicate of "'" for MySQL */
468
*to++= *from; /* We want a dublicate of "'" for DRIZZLE */
469
469
end_backslashes=0;
472
472
/* Add missing backslashes if user has specified odd number of backs.*/
473
473
if (end_backslashes)
483
483
char *raw_table_name= (char *)arg;
486
if (mysql_thread_init())
486
if (drizzle_thread_init())
489
if (!(mysql= db_connect(current_host,current_db,current_user,opt_password)))
489
if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
494
if (mysql_query(mysql, "/*!40101 set @@character_set_database=binary */;"))
494
if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
496
db_error(mysql); /* We shall countinue here, if --force was given */
496
db_error(drizzle); /* We shall countinue here, if --force was given */
501
501
We are not currently catching the error here.
503
if((error= write_to_table(raw_table_name, mysql)))
503
if((error= write_to_table(raw_table_name, drizzle)))
504
504
if (exitcode == 0)
509
db_disconnect(current_host, mysql);
509
db_disconnect(current_host, drizzle);
511
511
pthread_mutex_lock(&counter_mutex);
593
if (!(mysql= db_connect(current_host,current_db,current_user,opt_password)))
593
if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
595
595
free_defaults(argv_to_free);
596
596
return(1); /* purecov: deadcode */
599
if (mysql_query(mysql, "/*!40101 set @@character_set_database=binary */;"))
599
if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
601
db_error(mysql); /* We shall countinue here, if --force was given */
601
db_error(drizzle); /* We shall countinue here, if --force was given */
606
lock_table(mysql, argc, argv);
606
lock_table(drizzle, argc, argv);
607
607
for (; *argv != NULL; argv++)
608
if ((error= write_to_table(*argv, mysql)))
608
if ((error= write_to_table(*argv, drizzle)))
609
609
if (exitcode == 0)
611
db_disconnect(current_host, mysql);
611
db_disconnect(current_host, drizzle);
613
613
my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
614
614
free_defaults(argv_to_free);