~mkindahl/drizzle/remove-mem-casts

« back to all changes in this revision

Viewing changes to client/drizzleimport.c

  • Committer: Brian Aker
  • Date: 2008-07-26 04:51:46 UTC
  • mfrom: (202.1.25 codestyle)
  • Revision ID: brian@tangent.org-20080726045146-ax7ofn8aqnkycjl3
Merge from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
 
1
/* Copyright (C) 2008 Drizzle Open Source Development Team
2
2
 
3
3
   This program is free software; you can redistribute it and/or modify
4
4
   it under the terms of the GNU General Public License as published by
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/*
17
 
**         mysqlimport.c  - Imports all given files
18
 
**                          into a table(s).
 
17
**     drizzleimport.c  - Imports all given files
 
18
**          into a table(s).
19
19
**
20
 
**                         *************************
21
 
**                         *                       *
22
 
**                         * AUTHOR: Monty & Jani  *
23
 
**                         * DATE:   June 24, 1997 *
24
 
**                         *                       *
25
 
**                         *************************
 
20
**         *************************
 
21
**         *         *
 
22
**         * AUTHOR: Monty & Jani  *
 
23
**         * DATE:   June 24, 1997 *
 
24
**         *         *
 
25
**         *************************
26
26
*/
27
27
#define IMPORT_VERSION "3.7"
28
28
 
36
36
pthread_mutex_t counter_mutex;
37
37
pthread_cond_t count_threshhold;
38
38
 
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);
44
44
 
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;
59
59
 
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, "
127
127
#endif
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,
131
131
   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**) &current_user,
155
155
};
156
156
 
157
157
 
158
 
static const char *load_default_groups[]= { "mysqlimport","client",0 };
 
158
static const char *load_default_groups[]= { "drizzleimport","client",0 };
159
159
 
160
160
static void print_version(void)
161
161
{
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);
164
164
}
165
165
 
166
166
 
167
167
static void usage(void)
168
168
{
169
169
  print_version();
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");
172
172
  printf("\
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");
178
178
 
184
184
 
185
185
static bool
186
186
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
187
 
               char *argument)
 
187
         char *argument)
188
188
{
189
189
  switch(optid) {
190
190
  case 'p':
193
193
      char *start=argument;
194
194
      my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
195
195
      opt_password=my_strdup(argument,MYF(MY_FAE));
196
 
      while (*argument) *argument++= 'x';               /* Destroy argument */
 
196
      while (*argument) *argument++= 'x';    /* Destroy argument */
197
197
      if (*start)
198
 
        start[1]=0;                             /* Cut length of argument */
 
198
  start[1]=0;        /* Cut length of argument */
199
199
      tty_password= 0;
200
200
    }
201
201
    else
202
202
      tty_password= 1;
203
203
    break;
204
 
  case OPT_MYSQL_PROTOCOL:
 
204
  case OPT_DRIZZLE_PROTOCOL:
205
205
    opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
206
206
                                    opt->name);
207
207
    break;
238
238
  }
239
239
  if (strcmp(default_charset, charset_info->csname) &&
240
240
      !(charset_info= get_charset_by_csname(default_charset,
241
 
                                            MY_CS_PRIMARY, MYF(MY_WME))))
 
241
                MY_CS_PRIMARY, MYF(MY_WME))))
242
242
    exit(1);
243
243
  if (*argc < 2)
244
244
  {
254
254
 
255
255
 
256
256
 
257
 
static int write_to_table(char *filename, MYSQL *mysql)
 
257
static int write_to_table(char *filename, DRIZZLE *drizzle)
258
258
{
259
259
  char tablename[FN_REFLEN], hard_path[FN_REFLEN],
260
260
       sql_statement[FN_REFLEN*16+256], *end;
274
274
#else
275
275
    sprintf(sql_statement, "DELETE FROM %s", tablename);
276
276
#endif
277
 
    if (mysql_query(mysql, sql_statement))
 
277
    if (drizzle_query(drizzle, sql_statement))
278
278
    {
279
 
      db_error_with_table(mysql, tablename);
 
279
      db_error_with_table(drizzle, tablename);
280
280
      return(1);
281
281
    }
282
282
  }
285
285
  {
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);
289
289
    else
290
290
      fprintf(stdout, "Loading data from SERVER file: %s into %s\n",
291
 
              hard_path, tablename);
 
291
        hard_path, tablename);
292
292
  }
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);
297
297
  if (replace)
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");
314
314
  if (opt_columns)
315
315
    end= strmov(strmov(strmov(end, " ("), opt_columns), ")");
316
316
  *end= '\0';
317
317
 
318
 
  if (mysql_query(mysql, sql_statement))
 
318
  if (drizzle_query(drizzle, sql_statement))
319
319
  {
320
 
    db_error_with_table(mysql, tablename);
 
320
    db_error_with_table(drizzle, tablename);
321
321
    return(1);
322
322
  }
323
323
  if (!silent)
324
324
  {
325
 
    if (mysql_info(mysql)) /* If NULL-pointer, print nothing */
 
325
    if (drizzle_info(drizzle)) /* If NULL-pointer, print nothing */
326
326
    {
327
327
      fprintf(stdout, "%s.%s: %s\n", current_db, tablename,
328
 
              mysql_info(mysql));
 
328
        drizzle_info(drizzle));
329
329
    }
330
330
  }
331
331
  return(0);
333
333
 
334
334
 
335
335
 
336
 
static void lock_table(MYSQL *mysql, int tablecount, char **raw_tablename)
 
336
static void lock_table(DRIZZLE *drizzle, int tablecount, char **raw_tablename)
337
337
{
338
338
  DYNAMIC_STRING query;
339
339
  int i;
348
348
    dynstr_append(&query, tablename);
349
349
    dynstr_append(&query, " WRITE,");
350
350
  }
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 */
353
353
}
354
354
 
355
355
 
356
356
 
357
357
 
358
 
static MYSQL *db_connect(char *host, char *database,
 
358
static DRIZZLE *db_connect(char *host, char *database,
359
359
                         char *user, char *passwd)
360
360
{
361
 
  MYSQL *mysql;
 
361
  DRIZZLE *drizzle;
362
362
  if (verbose)
363
363
    fprintf(stdout, "Connecting to %s\n", host ? host : "localhost");
364
 
  if (!(mysql= mysql_init(NULL)))
 
364
  if (!(drizzle= drizzle_create(NULL)))
365
365
    return 0;
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,
375
375
                           0)))
376
376
  {
377
 
    ignore_errors=0;      /* NO RETURN FROM db_error */
378
 
    db_error(mysql);
 
377
    ignore_errors=0;    /* NO RETURN FROM db_error */
 
378
    db_error(drizzle);
379
379
  }
380
 
  mysql->reconnect= 0;
 
380
  drizzle->reconnect= 0;
381
381
  if (verbose)
382
382
    fprintf(stdout, "Selecting database %s\n", database);
383
 
  if (mysql_select_db(mysql, database))
 
383
  if (drizzle_select_db(drizzle, database))
384
384
  {
385
385
    ignore_errors=0;
386
 
    db_error(mysql);
 
386
    db_error(drizzle);
387
387
  }
388
 
  return mysql;
 
388
  return drizzle;
389
389
}
390
390
 
391
391
 
392
392
 
393
 
static void db_disconnect(char *host, MYSQL *mysql)
 
393
static void db_disconnect(char *host, DRIZZLE *drizzle)
394
394
{
395
395
  if (verbose)
396
396
    fprintf(stdout, "Disconnecting from %s\n", host ? host : "localhost");
397
 
  mysql_close(mysql);
 
397
  drizzle_close(drizzle);
398
398
}
399
399
 
400
400
 
401
401
 
402
 
static void safe_exit(int error, MYSQL *mysql)
 
402
static void safe_exit(int error, DRIZZLE *drizzle)
403
403
{
404
404
  if (ignore_errors)
405
405
    return;
406
 
  if (mysql)
407
 
    mysql_close(mysql);
 
406
  if (drizzle)
 
407
    drizzle_close(drizzle);
408
408
  exit(error);
409
409
}
410
410
 
411
411
 
412
412
 
413
 
static void db_error_with_table(MYSQL *mysql, char *table)
 
413
static void db_error_with_table(DRIZZLE *drizzle, char *table)
414
414
{
415
415
  my_printf_error(0,"Error: %d, %s, when using table: %s",
416
 
                  MYF(0), mysql_errno(mysql), mysql_error(mysql), table);
417
 
  safe_exit(1, mysql);
 
416
      MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle), table);
 
417
  safe_exit(1, drizzle);
418
418
}
419
419
 
420
420
 
421
421
 
422
 
static void db_error(MYSQL *mysql)
 
422
static void db_error(DRIZZLE *drizzle)
423
423
{
424
 
  my_printf_error(0,"Error: %d %s", MYF(0), mysql_errno(mysql), mysql_error(mysql));
425
 
  safe_exit(1, mysql);
 
424
  my_printf_error(0,"Error: %d %s", MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle));
 
425
  safe_exit(1, drizzle);
426
426
}
427
427
 
428
428
 
429
429
static char *add_load_option(char *ptr, const char *object,
430
 
                             const char *statement)
 
430
           const char *statement)
431
431
{
432
432
  if (object)
433
433
  {
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.
453
 
*/ 
 
453
*/
454
454
 
455
455
static char *field_escape(char *to,const char *from,uint length)
456
456
{
457
457
  const char *end;
458
 
  uint end_backslashes=0; 
 
458
  uint end_backslashes=0;
459
459
 
460
460
  for (end= from+length; from != end; from++)
461
461
  {
462
462
    *to++= *from;
463
463
    if (*from == '\\')
464
464
      end_backslashes^=1;    /* find odd number of backslashes */
465
 
    else 
 
465
    else
466
466
    {
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;
470
470
    }
471
471
  }
472
472
  /* Add missing backslashes if user has specified odd number of backs.*/
473
473
  if (end_backslashes)
474
 
    *to++= '\\';          
 
474
    *to++= '\\';         
475
475
  return to;
476
476
}
477
477
 
481
481
{
482
482
  int error;
483
483
  char *raw_table_name= (char *)arg;
484
 
  MYSQL *mysql= 0;
 
484
  DRIZZLE *drizzle= 0;
485
485
 
486
 
  if (mysql_thread_init())
 
486
  if (drizzle_thread_init())
487
487
    goto error;
488
 
  
489
 
  if (!(mysql= db_connect(current_host,current_db,current_user,opt_password)))
 
488
 
 
489
  if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
490
490
  {
491
491
    goto error;
492
492
  }
493
493
 
494
 
  if (mysql_query(mysql, "/*!40101 set @@character_set_database=binary */;"))
 
494
  if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
495
495
  {
496
 
    db_error(mysql); /* We shall countinue here, if --force was given */
 
496
    db_error(drizzle); /* We shall countinue here, if --force was given */
497
497
    goto error;
498
498
  }
499
499
 
500
500
  /*
501
501
    We are not currently catching the error here.
502
502
  */
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)
505
505
      exitcode= error;
506
506
 
507
507
error:
508
 
  if (mysql)
509
 
    db_disconnect(current_host, mysql);
 
508
  if (drizzle)
 
509
    db_disconnect(current_host, drizzle);
510
510
 
511
511
  pthread_mutex_lock(&counter_mutex);
512
512
  counter--;
559
559
      counter++;
560
560
      pthread_mutex_unlock(&counter_mutex);
561
561
      /* now create the thread */
562
 
      if (pthread_create(&mainthread, &attr, worker_thread, 
 
562
      if (pthread_create(&mainthread, &attr, worker_thread,
563
563
                         (void *)*argv) != 0)
564
564
      {
565
565
        pthread_mutex_lock(&counter_mutex);
589
589
  else
590
590
#endif
591
591
  {
592
 
    MYSQL *mysql= 0;
593
 
    if (!(mysql= db_connect(current_host,current_db,current_user,opt_password)))
 
592
    DRIZZLE *drizzle= 0;
 
593
    if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
594
594
    {
595
595
      free_defaults(argv_to_free);
596
596
      return(1); /* purecov: deadcode */
597
597
    }
598
598
 
599
 
    if (mysql_query(mysql, "/*!40101 set @@character_set_database=binary */;"))
 
599
    if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
600
600
    {
601
 
      db_error(mysql); /* We shall countinue here, if --force was given */
 
601
      db_error(drizzle); /* We shall countinue here, if --force was given */
602
602
      return(1);
603
603
    }
604
604
 
605
605
    if (lock_tables)
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)
610
610
          exitcode= error;
611
 
    db_disconnect(current_host, mysql);
 
611
    db_disconnect(current_host, drizzle);
612
612
  }
613
613
  my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
614
614
  free_defaults(argv_to_free);