~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/mysqlimport.c

MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include "client_priv.h"
30
30
#include "drizzle_version.h"
31
 
#ifdef HAVE_LIBPTHREAD
32
31
#include <my_pthread.h>
33
 
#endif
34
32
 
35
33
 
36
34
/* Global Thread counter */
37
35
uint counter;
38
 
#ifdef HAVE_LIBPTHREAD
39
36
pthread_mutex_t counter_mutex;
40
37
pthread_cond_t count_threshhold;
41
 
#endif
42
38
 
43
39
static void db_error_with_table(MYSQL *mysql, char *table);
44
40
static void db_error(MYSQL *mysql);
46
42
static char *add_load_option(char *ptr,const char *object,
47
43
                             const char *statement);
48
44
 
49
 
static my_bool  verbose=0,lock_tables=0,ignore_errors=0,opt_delete=0,
 
45
static bool     verbose=0,lock_tables=0,ignore_errors=0,opt_delete=0,
50
46
                replace=0,silent=0,ignore=0,opt_compress=0,
51
47
                opt_low_priority= 0, tty_password= 0;
52
 
static my_bool debug_info_flag= 0, debug_check_flag= 0;
 
48
static bool debug_info_flag= 0, debug_check_flag= 0;
53
49
static uint opt_use_threads=0, opt_local_file=0, my_end_arg= 0;
54
50
static char     *opt_password=0, *current_user=0,
55
51
                *current_host=0, *current_db=0, *fields_terminated=0,
58
54
                *default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
59
55
static uint     opt_mysql_port= 0, opt_protocol= 0;
60
56
static char * opt_mysql_unix_port=0;
61
 
static longlong opt_ignore_lines= -1;
 
57
static int64_t opt_ignore_lines= -1;
62
58
static CHARSET_INFO *charset_info= &my_charset_latin1;
63
59
 
64
 
#ifdef HAVE_SMEM
65
 
static char *shared_memory_base_name=0;
66
 
#endif
67
 
 
68
60
static struct my_option my_long_options[] =
69
61
{
70
62
  {"character-sets-dir", OPT_CHARSETS_DIR,
71
 
   "Directory where character sets are.", (uchar**) &charsets_dir,
72
 
   (uchar**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
63
   "Directory where character sets are.", (char**) &charsets_dir,
 
64
   (char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
73
65
  {"default-character-set", OPT_DEFAULT_CHARSET,
74
 
   "Set the default character set.", (uchar**) &default_charset,
75
 
   (uchar**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
66
   "Set the default character set.", (char**) &default_charset,
 
67
   (char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
76
68
  {"columns", 'c',
77
69
   "Use only these columns to import the data to. Give the column names in a comma separated list. This is same as giving columns to LOAD DATA INFILE.",
78
 
   (uchar**) &opt_columns, (uchar**) &opt_columns, 0, GET_STR, REQUIRED_ARG, 0, 0, 0,
 
70
   (char**) &opt_columns, (char**) &opt_columns, 0, GET_STR, REQUIRED_ARG, 0, 0, 0,
79
71
   0, 0, 0},
80
72
  {"compress", 'C', "Use compression in server/client protocol.",
81
 
   (uchar**) &opt_compress, (uchar**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
73
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
82
74
   0, 0, 0},
83
75
  {"debug",'#', "Output debug log. Often this is 'd:t:o,filename'.", 0, 0, 0,
84
76
   GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
85
77
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
86
 
   (uchar**) &debug_check_flag, (uchar**) &debug_check_flag, 0,
 
78
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
87
79
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
88
80
  {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
89
 
   (uchar**) &debug_info_flag, (uchar**) &debug_info_flag,
 
81
   (char**) &debug_info_flag, (char**) &debug_info_flag,
90
82
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
91
 
  {"delete", 'd', "First delete all rows from table.", (uchar**) &opt_delete,
92
 
   (uchar**) &opt_delete, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
83
  {"delete", 'd', "First delete all rows from table.", (char**) &opt_delete,
 
84
   (char**) &opt_delete, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
93
85
  {"fields-terminated-by", OPT_FTB,
94
 
   "Fields in the textfile are terminated by ...", (uchar**) &fields_terminated,
95
 
   (uchar**) &fields_terminated, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
86
   "Fields in the textfile are terminated by ...", (char**) &fields_terminated,
 
87
   (char**) &fields_terminated, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
96
88
  {"fields-enclosed-by", OPT_ENC,
97
 
   "Fields in the importfile are enclosed by ...", (uchar**) &enclosed,
98
 
   (uchar**) &enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
89
   "Fields in the importfile are enclosed by ...", (char**) &enclosed,
 
90
   (char**) &enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
99
91
  {"fields-optionally-enclosed-by", OPT_O_ENC,
100
 
   "Fields in the i.file are opt. enclosed by ...", (uchar**) &opt_enclosed,
101
 
   (uchar**) &opt_enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
92
   "Fields in the i.file are opt. enclosed by ...", (char**) &opt_enclosed,
 
93
   (char**) &opt_enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
102
94
  {"fields-escaped-by", OPT_ESC, "Fields in the i.file are escaped by ...",
103
 
   (uchar**) &escaped, (uchar**) &escaped, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0,
 
95
   (char**) &escaped, (char**) &escaped, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0,
104
96
   0, 0},
105
97
  {"force", 'f', "Continue even if we get an sql-error.",
106
 
   (uchar**) &ignore_errors, (uchar**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
 
98
   (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
107
99
   0, 0, 0, 0},
108
100
  {"help", '?', "Displays this help and exits.", 0, 0, 0, GET_NO_ARG, NO_ARG,
109
101
   0, 0, 0, 0, 0, 0},
110
 
  {"host", 'h', "Connect to host.", (uchar**) &current_host,
111
 
   (uchar**) &current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
102
  {"host", 'h', "Connect to host.", (char**) &current_host,
 
103
   (char**) &current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
112
104
  {"ignore", 'i', "If duplicate unique key was found, keep old row.",
113
 
   (uchar**) &ignore, (uchar**) &ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
105
   (char**) &ignore, (char**) &ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
114
106
  {"ignore-lines", OPT_IGN_LINES, "Ignore first n lines of data infile.",
115
 
   (uchar**) &opt_ignore_lines, (uchar**) &opt_ignore_lines, 0, GET_LL,
 
107
   (char**) &opt_ignore_lines, (char**) &opt_ignore_lines, 0, GET_LL,
116
108
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
117
109
  {"lines-terminated-by", OPT_LTB, "Lines in the i.file are terminated by ...",
118
 
   (uchar**) &lines_terminated, (uchar**) &lines_terminated, 0, GET_STR,
 
110
   (char**) &lines_terminated, (char**) &lines_terminated, 0, GET_STR,
119
111
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
120
 
  {"local", 'L', "Read all files through the client.", (uchar**) &opt_local_file,
121
 
   (uchar**) &opt_local_file, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
112
  {"local", 'L', "Read all files through the client.", (char**) &opt_local_file,
 
113
   (char**) &opt_local_file, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
122
114
  {"lock-tables", 'l', "Lock all tables for write (this disables threads).",
123
 
    (uchar**) &lock_tables, (uchar**) &lock_tables, 0, GET_BOOL, NO_ARG, 
 
115
    (char**) &lock_tables, (char**) &lock_tables, 0, GET_BOOL, NO_ARG, 
124
116
    0, 0, 0, 0, 0, 0},
125
117
  {"low-priority", OPT_LOW_PRIORITY,
126
 
   "Use LOW_PRIORITY when updating the table.", (uchar**) &opt_low_priority,
127
 
   (uchar**) &opt_low_priority, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
118
   "Use LOW_PRIORITY when updating the table.", (char**) &opt_low_priority,
 
119
   (char**) &opt_low_priority, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
128
120
  {"password", 'p',
129
121
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
130
122
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
134
126
   "/etc/services, "
135
127
#endif
136
128
   "built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
137
 
   (uchar**) &opt_mysql_port,
138
 
   (uchar**) &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
 
129
   (char**) &opt_mysql_port,
 
130
   (char**) &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
139
131
   0},
140
132
  {"protocol", OPT_MYSQL_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
141
133
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
142
134
  {"replace", 'r', "If duplicate unique key was found, replace old row.",
143
 
   (uchar**) &replace, (uchar**) &replace, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
144
 
#ifdef HAVE_SMEM
145
 
  {"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
146
 
   "Base name of shared memory.", (uchar**) &shared_memory_base_name, (uchar**) &shared_memory_base_name,
147
 
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
148
 
#endif
149
 
  {"silent", 's', "Be more silent.", (uchar**) &silent, (uchar**) &silent, 0,
 
135
   (char**) &replace, (char**) &replace, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
136
  {"silent", 's', "Be more silent.", (char**) &silent, (char**) &silent, 0,
150
137
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
151
138
  {"socket", 'S', "Socket file to use for connection.",
152
 
   (uchar**) &opt_mysql_unix_port, (uchar**) &opt_mysql_unix_port, 0, GET_STR,
 
139
   (char**) &opt_mysql_unix_port, (char**) &opt_mysql_unix_port, 0, GET_STR,
153
140
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
154
141
  {"use-threads", OPT_USE_THREADS,
155
142
   "Load files in parallel. The argument is the number "
156
143
   "of threads to use for loading data.",
157
 
   (uchar**) &opt_use_threads, (uchar**) &opt_use_threads, 0, 
 
144
   (char**) &opt_use_threads, (char**) &opt_use_threads, 0, 
158
145
   GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
159
146
#ifndef DONT_ALLOW_USER_CHANGE
160
 
  {"user", 'u', "User for login if not current user.", (uchar**) &current_user,
161
 
   (uchar**) &current_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
147
  {"user", 'u', "User for login if not current user.", (char**) &current_user,
 
148
   (char**) &current_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
162
149
#endif
163
 
  {"verbose", 'v', "Print info about the various stages.", (uchar**) &verbose,
164
 
   (uchar**) &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
150
  {"verbose", 'v', "Print info about the various stages.", (char**) &verbose,
 
151
   (char**) &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
165
152
  {"version", 'V', "Output version information and exit.", 0, 0, 0, GET_NO_ARG,
166
153
   NO_ARG, 0, 0, 0, 0, 0, 0},
167
154
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
199
186
 
200
187
#include <help_end.h>
201
188
 
202
 
static my_bool
 
189
static bool
203
190
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
204
191
               char *argument)
205
192
{
222
209
    opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
223
210
                                    opt->name);
224
211
    break;
225
 
  case '#':
226
 
    DBUG_PUSH(argument ? argument : "d:t:o");
227
 
    debug_check_flag= 1;
228
 
    break;
229
212
  case 'V': print_version(); exit(0);
230
213
  case 'I':
231
214
  case '?':
279
262
{
280
263
  char tablename[FN_REFLEN], hard_path[FN_REFLEN],
281
264
       sql_statement[FN_REFLEN*16+256], *end;
282
 
  DBUG_ENTER("write_to_table");
283
 
  DBUG_PRINT("enter",("filename: %s",filename));
284
265
 
285
266
  fn_format(tablename, filename, "", "", 1 | 2); /* removes path & ext. */
286
267
  if (!opt_local_file)
300
281
    if (mysql_query(mysql, sql_statement))
301
282
    {
302
283
      db_error_with_table(mysql, tablename);
303
 
      DBUG_RETURN(1);
 
284
      return(1);
304
285
    }
305
286
  }
306
287
  to_unix_path(hard_path);
332
313
  end= add_load_option(end, escaped, " ESCAPED BY");
333
314
  end= add_load_option(end, lines_terminated, " LINES TERMINATED BY");
334
315
  if (opt_ignore_lines >= 0)
335
 
    end= strmov(longlong10_to_str(opt_ignore_lines, 
 
316
    end= strmov(int64_t10_to_str(opt_ignore_lines, 
336
317
                                  strmov(end, " IGNORE "),10), " LINES");
337
318
  if (opt_columns)
338
319
    end= strmov(strmov(strmov(end, " ("), opt_columns), ")");
341
322
  if (mysql_query(mysql, sql_statement))
342
323
  {
343
324
    db_error_with_table(mysql, tablename);
344
 
    DBUG_RETURN(1);
 
325
    return(1);
345
326
  }
346
327
  if (!silent)
347
328
  {
351
332
              mysql_info(mysql));
352
333
    }
353
334
  }
354
 
  DBUG_RETURN(0);
 
335
  return(0);
355
336
}
356
337
 
357
338
 
393
374
                  (char*) &opt_local_file);
394
375
  if (opt_protocol)
395
376
    mysql_options(mysql,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
396
 
#ifdef HAVE_SMEM
397
 
  if (shared_memory_base_name)
398
 
    mysql_options(mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
399
 
#endif
400
377
  if (!(mysql_real_connect(mysql,host,user,passwd,
401
378
                           database,opt_mysql_port,opt_mysql_unix_port,
402
379
                           0)))
504
481
 
505
482
int exitcode= 0;
506
483
 
507
 
#ifdef HAVE_LIBPTHREAD
508
484
static pthread_handler_t worker_thread(void *arg)
509
485
{
510
486
  int error;
544
520
 
545
521
  return 0;
546
522
}
547
 
#endif
548
523
 
549
524
 
550
525
int main(int argc, char **argv)
640
615
    db_disconnect(current_host, mysql);
641
616
  }
642
617
  my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
643
 
#ifdef HAVE_SMEM
644
 
  my_free(shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
645
 
#endif
646
618
  free_defaults(argv_to_free);
647
619
  my_end(my_end_arg);
648
620
  return(exitcode);