~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleimport.cc

  • Committer: Brian Aker
  • Date: 2009-02-12 22:45:08 UTC
  • Revision ID: brian@tangent.org-20090212224508-mrd9jwgn1zjdpqdk
Minor refactoring (we will need to disconnect the code from the include
file).

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"
 
29
#include "client_priv.h"
30
30
#include <string>
31
31
 
32
 
#include "client_priv.h"
33
32
#include <pthread.h>
34
33
 
 
34
/* Added this for string translation. */
 
35
#include <drizzled/gettext.h>
 
36
 
35
37
using namespace std;
36
38
 
37
39
/* Global Thread counter */
45
47
static char *add_load_option(char *ptr,const char *object,
46
48
           const char *statement);
47
49
 
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= false, opt_compress= false, opt_low_priority= false,
 
53
            tty_password= false;
 
54
static bool debug_info_flag= false, debug_check_flag= false;
 
55
static uint 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 uint opt_protocol= 0;
 
62
static uint32_t opt_drizzle_port= 0;
 
63
static char * opt_drizzle_unix_port= 0;
60
64
static int64_t opt_ignore_lines= -1;
61
65
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
62
66
 
63
67
static struct my_option my_long_options[] =
64
68
{
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
69
  {"default-character-set", OPT_DEFAULT_CHARSET,
69
70
   "Set the default character set.", (char**) &default_charset,
70
71
   (char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
120
121
  {"low-priority", OPT_LOW_PRIORITY,
121
122
   "Use LOW_PRIORITY when updating the table.", (char**) &opt_low_priority,
122
123
   (char**) &opt_low_priority, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
123
 
  {"password", 'p',
 
124
  {"password", 'P',
124
125
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
125
126
   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, "
 
127
  {"port", 'p', "Port number to use for connection or 0 for default to, in "
 
128
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
128
129
   "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,
131
 
   0},
 
130
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 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.",
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, drizzleclient_get_client_info(),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("my",load_default_groups);
 
179
  print_defaults("drizzle",load_default_groups);
181
180
  my_print_help(my_long_options);
182
181
  my_print_variables(my_long_options);
183
182
}
184
183
 
185
 
static bool
186
 
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
187
 
         char *argument)
 
184
extern "C"
 
185
bool get_one_option(int optid, const struct my_option *, char *argument)
188
186
{
 
187
  char *endchar= NULL;
 
188
  uint64_t temp_drizzle_port= 0;
 
189
 
189
190
  switch(optid) {
190
191
  case 'p':
 
192
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
 
193
    /* if there is an alpha character this is not a valid port */
 
194
    if (strlen(endchar) != 0)
 
195
    {
 
196
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
 
197
      exit(1);
 
198
    }
 
199
    /* If the port number is > 65535 it is not a valid port
 
200
       This also helps with potential data loss casting unsigned long to a
 
201
       uint32_t. */
 
202
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
 
203
    {
 
204
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
205
      exit(1);
 
206
    }
 
207
    else
 
208
    {
 
209
      opt_drizzle_port= (uint32_t) temp_drizzle_port;
 
210
    }
 
211
    break;
 
212
  case 'P':
191
213
    if (argument)
192
214
    {
193
215
      char *start=argument;
194
 
      free(opt_password);
195
 
      opt_password=my_strdup(argument,MYF(MY_FAE));
196
 
      while (*argument) *argument++= 'x';    /* Destroy argument */
 
216
      if (opt_password)
 
217
        free(opt_password);
 
218
      opt_password = strdup(argument);
 
219
      if (opt_password == NULL)
 
220
      {
 
221
        fprintf(stderr, "Memory allocation error while copying password. "
 
222
                        "Aborting.\n");
 
223
        exit(ENOMEM);
 
224
      }
 
225
      while (*argument)
 
226
      {
 
227
        /* Overwriting password with 'x' */
 
228
        *argument++= 'x';
 
229
      }
197
230
      if (*start)
198
 
  start[1]=0;        /* Cut length of argument */
 
231
      {
 
232
        /* Cut length of argument */
 
233
        start[1]= 0;
 
234
      }
199
235
      tty_password= 0;
200
236
    }
201
237
    else
235
271
    return(1);
236
272
  }
237
273
  if (strcmp(default_charset, charset_info->csname) &&
238
 
      !(charset_info= get_charset_by_csname(default_charset,
239
 
                MY_CS_PRIMARY, MYF(MY_WME))))
 
274
      !(charset_info= get_charset_by_csname(default_charset, MY_CS_PRIMARY)))
240
275
    exit(1);
241
276
  if (*argc < 2)
242
277
  {
246
281
  current_db= *((*argv)++);
247
282
  (*argc)--;
248
283
  if (tty_password)
249
 
    opt_password=get_tty_password(NULL);
 
284
    opt_password=drizzleclient_get_tty_password(NULL);
250
285
  return(0);
251
286
}
252
287
 
259
294
 
260
295
  fn_format(tablename, filename, "", "", 1 | 2); /* removes path & ext. */
261
296
  if (!opt_local_file)
262
 
    my_stpcpy(hard_path,filename);
 
297
    strcpy(hard_path,filename);
263
298
  else
264
299
    my_load_path(hard_path, filename, NULL); /* filename includes the path */
265
300
 
272
307
#else
273
308
    sprintf(sql_statement, "DELETE FROM %s", tablename);
274
309
#endif
275
 
    if (drizzle_query(drizzle, sql_statement))
 
310
    if (drizzleclient_query(drizzle, sql_statement))
276
311
    {
277
312
      db_error_with_table(drizzle, tablename);
278
313
      return(1);
279
314
    }
280
315
  }
281
 
  to_unix_path(hard_path);
282
316
  if (verbose)
283
317
  {
284
318
    if (opt_local_file)
293
327
    opt_local_file ? "LOCAL" : "", hard_path);
294
328
  end= strchr(sql_statement, '\0');
295
329
  if (opt_replace)
296
 
    end= my_stpcpy(end, " REPLACE");
 
330
    end= strcpy(end, " REPLACE")+8;
297
331
  if (ignore)
298
 
    end= my_stpcpy(end, " IGNORE");
299
 
  end= my_stpcpy(my_stpcpy(end, " INTO TABLE "), tablename);
 
332
    end= strcpy(end, " IGNORE")+7;
 
333
 
 
334
  end+= sprintf(end, " INTO TABLE %s", tablename);
300
335
 
301
336
  if (fields_terminated || enclosed || opt_enclosed || escaped)
302
 
      end= my_stpcpy(end, " FIELDS");
 
337
      end= strcpy(end, " FIELDS")+7;
303
338
  end= add_load_option(end, fields_terminated, " TERMINATED BY");
304
339
  end= add_load_option(end, enclosed, " ENCLOSED BY");
305
340
  end= add_load_option(end, opt_enclosed,
307
342
  end= add_load_option(end, escaped, " ESCAPED BY");
308
343
  end= add_load_option(end, lines_terminated, " LINES TERMINATED BY");
309
344
  if (opt_ignore_lines >= 0)
310
 
    end= my_stpcpy(int64_t10_to_str(opt_ignore_lines,
311
 
          my_stpcpy(end, " IGNORE "),10), " LINES");
 
345
  {
 
346
    end= strcpy(end, " IGNORE ")+8;
 
347
    end= int64_t2str(opt_ignore_lines, end, 10);
 
348
    end= strcpy(end, " LINES")+6;
 
349
  }
312
350
  if (opt_columns)
313
 
    end= my_stpcpy(my_stpcpy(my_stpcpy(end, " ("), opt_columns), ")");
 
351
  {
 
352
    end= strcpy(end, " (")+2;
 
353
    end= strcpy(end, opt_columns)+strlen(opt_columns);
 
354
    end= strcpy(end, ")")+1;
 
355
  }
314
356
  *end= '\0';
315
357
 
316
 
  if (drizzle_query(drizzle, sql_statement))
 
358
  if (drizzleclient_query(drizzle, sql_statement))
317
359
  {
318
360
    db_error_with_table(drizzle, tablename);
319
361
    return(1);
320
362
  }
321
363
  if (!silent)
322
364
  {
323
 
    if (drizzle_info(drizzle)) /* If NULL-pointer, print nothing */
 
365
    if (drizzleclient_info(drizzle)) /* If NULL-pointer, print nothing */
324
366
    {
325
367
      fprintf(stdout, "%s.%s: %s\n", current_db, tablename,
326
 
        drizzle_info(drizzle));
 
368
        drizzleclient_info(drizzle));
327
369
    }
328
370
  }
329
371
  return(0);
346
388
    query.append(tablename);
347
389
    query.append(" WRITE,");
348
390
  }
349
 
  if (drizzle_real_query(drizzle, query.c_str(), query.length()-1))
 
391
  if (drizzleclient_real_query(drizzle, query.c_str(), query.length()-1))
350
392
    db_error(drizzle); /* We shall countinue here, if --force was given */
351
393
}
352
394
 
359
401
  DRIZZLE *drizzle;
360
402
  if (verbose)
361
403
    fprintf(stdout, "Connecting to %s\n", host ? host : "localhost");
362
 
  if (!(drizzle= drizzle_create(NULL)))
 
404
  if (!(drizzle= drizzleclient_create(NULL)))
363
405
    return 0;
364
406
  if (opt_compress)
365
 
    drizzle_options(drizzle,DRIZZLE_OPT_COMPRESS,NULL);
 
407
    drizzleclient_options(drizzle,DRIZZLE_OPT_COMPRESS,NULL);
366
408
  if (opt_local_file)
367
 
    drizzle_options(drizzle,DRIZZLE_OPT_LOCAL_INFILE,
 
409
    drizzleclient_options(drizzle,DRIZZLE_OPT_LOCAL_INFILE,
368
410
      (char*) &opt_local_file);
369
411
  if (opt_protocol)
370
 
    drizzle_options(drizzle,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
371
 
  if (!(drizzle_connect(drizzle,host,user,passwd,
 
412
    drizzleclient_options(drizzle,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
 
413
  if (!(drizzleclient_connect(drizzle,host,user,passwd,
372
414
                           database,opt_drizzle_port,opt_drizzle_unix_port,
373
415
                           0)))
374
416
  {
378
420
  drizzle->reconnect= 0;
379
421
  if (verbose)
380
422
    fprintf(stdout, "Selecting database %s\n", database);
381
 
  if (drizzle_select_db(drizzle, database))
 
423
  if (drizzleclient_select_db(drizzle, database))
382
424
  {
383
425
    ignore_errors=0;
384
426
    db_error(drizzle);
392
434
{
393
435
  if (verbose)
394
436
    fprintf(stdout, "Disconnecting from %s\n", host ? host : "localhost");
395
 
  drizzle_close(drizzle);
 
437
  drizzleclient_close(drizzle);
396
438
}
397
439
 
398
440
 
402
444
  if (ignore_errors)
403
445
    return;
404
446
  if (drizzle)
405
 
    drizzle_close(drizzle);
 
447
    drizzleclient_close(drizzle);
406
448
  exit(error);
407
449
}
408
450
 
411
453
static void db_error_with_table(DRIZZLE *drizzle, char *table)
412
454
{
413
455
  my_printf_error(0,"Error: %d, %s, when using table: %s",
414
 
      MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle), table);
 
456
      MYF(0), drizzleclient_errno(drizzle), drizzleclient_error(drizzle), table);
415
457
  safe_exit(1, drizzle);
416
458
}
417
459
 
419
461
 
420
462
static void db_error(DRIZZLE *drizzle)
421
463
{
422
 
  my_printf_error(0,"Error: %d %s", MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle));
 
464
  my_printf_error(0,"Error: %d %s", MYF(0), drizzleclient_errno(drizzle), drizzleclient_error(drizzle));
423
465
  safe_exit(1, drizzle);
424
466
}
425
467
 
431
473
  {
432
474
    /* Don't escape hex constants */
433
475
    if (object[0] == '0' && (object[1] == 'x' || object[1] == 'X'))
434
 
      ptr= strxmov(ptr," ",statement," ",object,NULL);
 
476
      ptr+= sprintf(ptr, " %s %s", statement, object);
435
477
    else
436
478
    {
437
479
      /* char constant; escape */
438
 
      ptr= strxmov(ptr," ",statement," '",NULL);
 
480
      ptr+= sprintf(ptr, " %s '", statement); 
439
481
      ptr= field_escape(ptr,object,(uint) strlen(object));
440
482
      *ptr++= '\'';
441
483
    }
469
511
  }
470
512
  /* Add missing backslashes if user has specified odd number of backs.*/
471
513
  if (end_backslashes)
472
 
    *to++= '\\';         
 
514
    *to++= '\\';
473
515
  return to;
474
516
}
475
517
 
476
518
int exitcode= 0;
477
519
 
478
 
static void * worker_thread(void *arg)
 
520
extern "C"
 
521
void * worker_thread(void *arg)
479
522
{
480
523
  int error;
481
524
  char *raw_table_name= (char *)arg;
486
529
    goto error;
487
530
  }
488
531
 
489
 
  if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
 
532
  if (drizzleclient_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
490
533
  {
491
534
    db_error(drizzle); /* We shall countinue here, if --force was given */
492
535
    goto error;
519
562
  char **argv_to_free;
520
563
  MY_INIT(argv[0]);
521
564
 
522
 
  load_defaults("my",load_default_groups,&argc,&argv);
 
565
  load_defaults("drizzle",load_default_groups,&argc,&argv);
523
566
  /* argv is changed in the program */
524
567
  argv_to_free= argv;
525
568
  if (get_options(&argc, &argv))
591
634
      return(1); /* purecov: deadcode */
592
635
    }
593
636
 
594
 
    if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
 
637
    if (drizzleclient_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
595
638
    {
596
639
      db_error(drizzle); /* We shall countinue here, if --force was given */
597
640
      return(1);