~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleimport.c

  • Committer: Monty Taylor
  • Date: 2008-08-04 19:37:18 UTC
  • mto: (261.2.2 codestyle)
  • mto: This revision was merged to the branch mainline in revision 262.
  • Revision ID: monty@inaugust.com-20080804193718-f0rz13uli4429ozb
Changed gettext_noop() to N_()

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
*/
27
27
#define IMPORT_VERSION "3.7"
28
28
 
29
 
#include <config.h>
30
 
#include <string>
31
 
 
32
29
#include "client_priv.h"
33
 
#include <pthread.h>
 
30
#include <mysys/my_pthread.h>
34
31
 
35
 
using namespace std;
36
32
 
37
33
/* Global Thread counter */
38
34
uint counter;
46
42
           const char *statement);
47
43
 
48
44
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;
 
45
    replace=0,silent=0,ignore=0,opt_compress=0,
 
46
                opt_low_priority= 0, tty_password= 0;
51
47
static bool debug_info_flag= 0, debug_check_flag= 0;
52
48
static uint opt_use_threads=0, opt_local_file=0, my_end_arg= 0;
53
49
static char  *opt_password=0, *current_user=0,
54
50
    *current_host=0, *current_db=0, *fields_terminated=0,
55
51
    *lines_terminated=0, *enclosed=0, *opt_enclosed=0,
56
52
    *escaped=0, *opt_columns=0,
57
 
    *default_charset= (char*) DRIZZLE_DEFAULT_CHARSET_NAME;
 
53
    *default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
58
54
static uint     opt_drizzle_port= 0, opt_protocol= 0;
59
55
static char * opt_drizzle_unix_port=0;
60
56
static int64_t opt_ignore_lines= -1;
61
 
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
 
57
static CHARSET_INFO *charset_info= &my_charset_latin1;
62
58
 
63
59
static struct my_option my_long_options[] =
64
60
{
124
120
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
125
121
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
126
122
  {"port", 'P', "Port number to use for connection or 0 for default to, in "
127
 
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
128
 
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
 
123
   "order of preference, my.cnf, $MYSQL_TCP_PORT, "
 
124
#if MYSQL_PORT_DEFAULT == 0
 
125
   "/etc/services, "
 
126
#endif
 
127
   "built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
129
128
   (char**) &opt_drizzle_port,
130
129
   (char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
131
130
   0},
132
131
  {"protocol", OPT_DRIZZLE_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
133
132
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
134
133
  {"replace", 'r', "If duplicate unique key was found, replace old row.",
135
 
   (char**) &opt_replace, (char**) &opt_replace, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
134
   (char**) &replace, (char**) &replace, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
136
135
  {"silent", 's', "Be more silent.", (char**) &silent, (char**) &silent, 0,
137
136
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
138
137
  {"socket", 'S', "Socket file to use for connection.",
160
159
static void print_version(void)
161
160
{
162
161
  printf("%s  Ver %s Distrib %s, for %s (%s)\n" ,my_progname,
163
 
    IMPORT_VERSION, drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
162
    IMPORT_VERSION, MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
164
163
}
165
164
 
166
165
 
177
176
file. The SQL command 'LOAD DATA INFILE' is used to import the rows.\n");
178
177
 
179
178
  printf("\nUsage: %s [OPTIONS] database textfile...",my_progname);
180
 
  print_defaults("drizzle",load_default_groups);
 
179
  print_defaults("my",load_default_groups);
181
180
  my_print_help(my_long_options);
182
181
  my_print_variables(my_long_options);
183
182
}
191
190
    if (argument)
192
191
    {
193
192
      char *start=argument;
194
 
      free(opt_password);
 
193
      my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
195
194
      opt_password=my_strdup(argument,MYF(MY_FAE));
196
195
      while (*argument) *argument++= 'x';    /* Destroy argument */
197
196
      if (*start)
202
201
      tty_password= 1;
203
202
    break;
204
203
  case OPT_DRIZZLE_PROTOCOL:
 
204
    opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
 
205
                                    opt->name);
205
206
    break;
206
207
  case 'V': print_version(); exit(0);
207
208
  case 'I':
229
230
    fprintf(stderr, "You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n");
230
231
    return(1);
231
232
  }
232
 
  if (opt_replace && ignore)
 
233
  if (replace && ignore)
233
234
  {
234
235
    fprintf(stderr, "You can't use --ignore (-i) and --replace (-r) at the same time.\n");
235
236
    return(1);
246
247
  current_db= *((*argv)++);
247
248
  (*argc)--;
248
249
  if (tty_password)
249
 
    opt_password=get_tty_password(NULL);
 
250
    opt_password=get_tty_password(NullS);
250
251
  return(0);
251
252
}
252
253
 
259
260
 
260
261
  fn_format(tablename, filename, "", "", 1 | 2); /* removes path & ext. */
261
262
  if (!opt_local_file)
262
 
    my_stpcpy(hard_path,filename);
 
263
    strmov(hard_path,filename);
263
264
  else
264
265
    my_load_path(hard_path, filename, NULL); /* filename includes the path */
265
266
 
291
292
  sprintf(sql_statement, "LOAD DATA %s %s INFILE '%s'",
292
293
    opt_low_priority ? "LOW_PRIORITY" : "",
293
294
    opt_local_file ? "LOCAL" : "", hard_path);
294
 
  end= strchr(sql_statement, '\0');
295
 
  if (opt_replace)
296
 
    end= my_stpcpy(end, " REPLACE");
 
295
  end= strend(sql_statement);
 
296
  if (replace)
 
297
    end= strmov(end, " REPLACE");
297
298
  if (ignore)
298
 
    end= my_stpcpy(end, " IGNORE");
299
 
  end= my_stpcpy(my_stpcpy(end, " INTO TABLE "), tablename);
 
299
    end= strmov(end, " IGNORE");
 
300
  end= strmov(strmov(end, " INTO TABLE "), tablename);
300
301
 
301
302
  if (fields_terminated || enclosed || opt_enclosed || escaped)
302
 
      end= my_stpcpy(end, " FIELDS");
 
303
      end= strmov(end, " FIELDS");
303
304
  end= add_load_option(end, fields_terminated, " TERMINATED BY");
304
305
  end= add_load_option(end, enclosed, " ENCLOSED BY");
305
306
  end= add_load_option(end, opt_enclosed,
307
308
  end= add_load_option(end, escaped, " ESCAPED BY");
308
309
  end= add_load_option(end, lines_terminated, " LINES TERMINATED BY");
309
310
  if (opt_ignore_lines >= 0)
310
 
    end= my_stpcpy(int64_t10_to_str(opt_ignore_lines,
311
 
          my_stpcpy(end, " IGNORE "),10), " LINES");
 
311
    end= strmov(int64_t10_to_str(opt_ignore_lines,
 
312
          strmov(end, " IGNORE "),10), " LINES");
312
313
  if (opt_columns)
313
 
    end= my_stpcpy(my_stpcpy(my_stpcpy(end, " ("), opt_columns), ")");
 
314
    end= strmov(strmov(strmov(end, " ("), opt_columns), ")");
314
315
  *end= '\0';
315
316
 
316
317
  if (drizzle_query(drizzle, sql_statement))
333
334
 
334
335
static void lock_table(DRIZZLE *drizzle, int tablecount, char **raw_tablename)
335
336
{
336
 
  string query;
 
337
  DYNAMIC_STRING query;
337
338
  int i;
338
339
  char tablename[FN_REFLEN];
339
340
 
340
341
  if (verbose)
341
342
    fprintf(stdout, "Locking tables for write\n");
342
 
  query.append("LOCK TABLES ");
 
343
  init_dynamic_string(&query, "LOCK TABLES ", 256, 1024);
343
344
  for (i=0 ; i < tablecount ; i++)
344
345
  {
345
346
    fn_format(tablename, raw_tablename[i], "", "", 1 | 2);
346
 
    query.append(tablename);
347
 
    query.append(" WRITE,");
 
347
    dynstr_append(&query, tablename);
 
348
    dynstr_append(&query, " WRITE,");
348
349
  }
349
 
  if (drizzle_real_query(drizzle, query.c_str(), query.length()-1))
 
350
  if (drizzle_real_query(drizzle, query.str, query.length-1))
350
351
    db_error(drizzle); /* We shall countinue here, if --force was given */
351
352
}
352
353
 
362
363
  if (!(drizzle= drizzle_create(NULL)))
363
364
    return 0;
364
365
  if (opt_compress)
365
 
    drizzle_options(drizzle,DRIZZLE_OPT_COMPRESS,NULL);
 
366
    drizzle_options(drizzle,DRIZZLE_OPT_COMPRESS,NullS);
366
367
  if (opt_local_file)
367
368
    drizzle_options(drizzle,DRIZZLE_OPT_LOCAL_INFILE,
368
369
      (char*) &opt_local_file);
431
432
  {
432
433
    /* Don't escape hex constants */
433
434
    if (object[0] == '0' && (object[1] == 'x' || object[1] == 'X'))
434
 
      ptr= strxmov(ptr," ",statement," ",object,NULL);
 
435
      ptr= strxmov(ptr," ",statement," ",object,NullS);
435
436
    else
436
437
    {
437
438
      /* char constant; escape */
438
 
      ptr= strxmov(ptr," ",statement," '",NULL);
 
439
      ptr= strxmov(ptr," ",statement," '",NullS);
439
440
      ptr= field_escape(ptr,object,(uint) strlen(object));
440
441
      *ptr++= '\'';
441
442
    }
475
476
 
476
477
int exitcode= 0;
477
478
 
478
 
static void * worker_thread(void *arg)
 
479
static pthread_handler_t worker_thread(void *arg)
479
480
{
480
481
  int error;
481
482
  char *raw_table_name= (char *)arg;
482
483
  DRIZZLE *drizzle= 0;
483
484
 
 
485
  if (drizzle_thread_init())
 
486
    goto error;
 
487
 
484
488
  if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
485
489
  {
486
490
    goto error;
519
523
  char **argv_to_free;
520
524
  MY_INIT(argv[0]);
521
525
 
522
 
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
526
  load_defaults("my",load_default_groups,&argc,&argv);
523
527
  /* argv is changed in the program */
524
528
  argv_to_free= argv;
525
529
  if (get_options(&argc, &argv))
537
541
    pthread_attr_setdetachstate(&attr,
538
542
                                PTHREAD_CREATE_DETACHED);
539
543
 
540
 
    pthread_mutex_init(&counter_mutex, NULL);
541
 
    pthread_cond_init(&count_threshhold, NULL);
 
544
    VOID(pthread_mutex_init(&counter_mutex, NULL));
 
545
    VOID(pthread_cond_init(&count_threshhold, NULL));
542
546
 
543
547
    for (counter= 0; *argv != NULL; argv++) /* Loop through tables */
544
548
    {
577
581
      pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
578
582
    }
579
583
    pthread_mutex_unlock(&counter_mutex);
580
 
    pthread_mutex_destroy(&counter_mutex);
581
 
    pthread_cond_destroy(&count_threshhold);
 
584
    VOID(pthread_mutex_destroy(&counter_mutex));
 
585
    VOID(pthread_cond_destroy(&count_threshhold));
582
586
    pthread_attr_destroy(&attr);
583
587
  }
584
588
  else
605
609
          exitcode= error;
606
610
    db_disconnect(current_host, drizzle);
607
611
  }
608
 
  free(opt_password);
 
612
  my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
609
613
  free_defaults(argv_to_free);
610
614
  my_end(my_end_arg);
611
615
  return(exitcode);