~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleimport.cc

MErgeĀ VJ

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
2
 
   Copyright (C) 2008-2009 Sun Microsystems, Inc
3
 
 
4
 
   This program is free software; you can redistribute it and/or modify
5
 
   it under the terms of the GNU General Public License as published by
6
 
   the Free Software Foundation; version 2 of the License.
7
 
 
8
 
   This program is distributed in the hope that it will be useful,
9
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
   GNU General Public License for more details.
12
 
 
13
 
   You should have received a copy of the GNU General Public License
14
 
   along with this program; if not, write to the Free Software
15
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
1
/* 
 
2
  Copyright (C) 2010 Vijay Samuel
 
3
  Copyright (C) 2000-2006 MySQL AB
 
4
  Copyright (C) 2008-2009 Sun Microsystems, Inc
 
5
 
 
6
  This program is free software; you can redistribute it and/or modify
 
7
  it under the terms of the GNU General Public License as published by
 
8
  the Free Software Foundation; version 2 of the License.
 
9
 
 
10
  This program is distributed in the hope that it will be useful,
 
11
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
  GNU General Public License for more details.
 
14
 
 
15
  You should have received a copy of the GNU General Public License
 
16
  along with this program; if not, write to the Free Software
 
17
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
16
18
 
17
19
/*
18
20
**     drizzleimport.c  - Imports all given files
30
32
#include "client_priv.h"
31
33
#include <string>
32
34
#include <sstream>
33
 
 
 
35
#include <iostream>
 
36
#include <fstream>
 
37
#include <boost/program_options.hpp>
34
38
#include <pthread.h>
35
39
 
36
40
/* Added this for string translation. */
37
41
#include <drizzled/gettext.h>
 
42
#include <drizzled/configmake.h>
38
43
 
 
44
namespace po= boost::program_options;
39
45
using namespace std;
40
46
using namespace drizzled;
41
47
 
57
63
static bool verbose= false, lock_tables= false, ignore_errors= false,
58
64
            opt_delete= false, opt_replace= false, silent= false,
59
65
            ignore_unique= false, opt_low_priority= false,
60
 
            tty_password= false, opt_mysql= false;
 
66
            tty_password= false, opt_mysql= false, opt_local_file;
61
67
 
62
 
static uint32_t opt_use_threads= 0, opt_local_file= 0;
63
 
static char  *opt_password= NULL, *current_user= NULL,
64
 
    *current_host= NULL, *current_db= NULL, *fields_terminated= NULL,
65
 
    *lines_terminated= NULL, *enclosed= NULL, *opt_enclosed= NULL,
66
 
    *escaped= NULL, *opt_columns= NULL;
 
68
static uint32_t opt_use_threads= 0;
67
69
static uint32_t opt_drizzle_port= 0;
68
70
static int64_t opt_ignore_lines= -1;
69
71
 
70
 
static struct option my_long_options[] =
71
 
{
72
 
  {"columns", 'c',
73
 
   "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.",
74
 
   (char**) &opt_columns, (char**) &opt_columns, 0, GET_STR, REQUIRED_ARG, 0, 0, 0,
75
 
   0, 0, 0},
76
 
  {"debug",'#', "Output debug log. Often this is 'd:t:o,filename'.", 0, 0, 0,
77
 
   GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
78
 
  {"delete", 'd', "First delete all rows from table.", (char**) &opt_delete,
79
 
   (char**) &opt_delete, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
80
 
  {"fields-terminated-by", OPT_FTB,
81
 
   "Fields in the textfile are terminated by ...", (char**) &fields_terminated,
82
 
   (char**) &fields_terminated, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
83
 
  {"fields-enclosed-by", OPT_ENC,
84
 
   "Fields in the importfile are enclosed by ...", (char**) &enclosed,
85
 
   (char**) &enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
86
 
  {"fields-optionally-enclosed-by", OPT_O_ENC,
87
 
   "Fields in the i.file are opt. enclosed by ...", (char**) &opt_enclosed,
88
 
   (char**) &opt_enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
89
 
  {"fields-escaped-by", OPT_ESC, "Fields in the i.file are escaped by ...",
90
 
   (char**) &escaped, (char**) &escaped, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0,
91
 
   0, 0},
92
 
  {"force", 'f', "Continue even if we get an sql-error.",
93
 
   (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
94
 
   0, 0, 0, 0},
95
 
  {"help", '?', "Displays this help and exits.", 0, 0, 0, GET_NO_ARG, NO_ARG,
96
 
   0, 0, 0, 0, 0, 0},
97
 
  {"host", 'h', "Connect to host.", (char**) &current_host,
98
 
   (char**) &current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
99
 
  {"ignore", 'i', "If duplicate unique key was found, keep old row.",
100
 
   (char**) &ignore_unique, (char**) &ignore_unique, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
101
 
  {"ignore-lines", OPT_IGN_LINES, "Ignore first n lines of data infile.",
102
 
   (char**) &opt_ignore_lines, (char**) &opt_ignore_lines, 0, GET_LL,
103
 
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
104
 
  {"lines-terminated-by", OPT_LTB, "Lines in the i.file are terminated by ...",
105
 
   (char**) &lines_terminated, (char**) &lines_terminated, 0, GET_STR,
106
 
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
107
 
  {"local", 'L', "Read all files through the client.", (char**) &opt_local_file,
108
 
   (char**) &opt_local_file, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
109
 
  {"lock-tables", 'l', "Lock all tables for write (this disables threads).",
110
 
    (char**) &lock_tables, (char**) &lock_tables, 0, GET_BOOL, NO_ARG,
111
 
    0, 0, 0, 0, 0, 0},
112
 
  {"low-priority", OPT_LOW_PRIORITY,
113
 
   "Use LOW_PRIORITY when updating the table.", (char**) &opt_low_priority,
114
 
   (char**) &opt_low_priority, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
115
 
  {"mysql", 'm', N_("Use MySQL Protocol."),
116
 
   (char**) &opt_mysql, (char**) &opt_mysql, 0, GET_BOOL, NO_ARG, 1, 0, 0,
117
 
   0, 0, 0},
118
 
  {"password", 'P',
119
 
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
120
 
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
121
 
  {"port", 'p', "Port number to use for connection or 0 for default to, in "
122
 
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
123
 
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
124
 
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
125
 
  {"protocol", OPT_DRIZZLE_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
126
 
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
127
 
  {"replace", 'r', "If duplicate unique key was found, replace old row.",
128
 
   (char**) &opt_replace, (char**) &opt_replace, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
129
 
  {"silent", 's', "Be more silent.", (char**) &silent, (char**) &silent, 0,
130
 
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
131
 
  {"use-threads", OPT_USE_THREADS,
132
 
   "Load files in parallel. The argument is the number "
133
 
   "of threads to use for loading data.",
134
 
   (char**) &opt_use_threads, (char**) &opt_use_threads, 0,
135
 
   GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
136
 
  {"user", 'u', "User for login if not current user.", (char**) &current_user,
137
 
   (char**) &current_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
138
 
  {"verbose", 'v', "Print info about the various stages.", (char**) &verbose,
139
 
   (char**) &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
140
 
  {"version", 'V', "Output version information and exit.", 0, 0, 0, GET_NO_ARG,
141
 
   NO_ARG, 0, 0, 0, 0, 0, 0},
142
 
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
143
 
};
144
 
 
145
 
 
146
 
static const char *load_default_groups[]= { "drizzleimport","client",0 };
147
 
 
148
 
static void print_version(void)
149
 
{
150
 
  printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n" ,internal::my_progname,
151
 
    IMPORT_VERSION, drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
152
 
}
153
 
 
154
 
 
155
 
static void usage(void)
156
 
{
157
 
  print_version();
158
 
  puts("Copyright (C) 2008 Drizzle Open Source Development Team");
159
 
  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");
160
 
  printf("\
161
 
Loads tables from text files in various formats.  The base name of the\n\
162
 
text file must be the name of the table that should be used.\n\
163
 
If one uses sockets to connect to the Drizzle server, the server will open and\n\
164
 
read the text file directly. In other cases the client will open the text\n\
165
 
file. The SQL command 'LOAD DATA INFILE' is used to import the rows.\n");
166
 
 
167
 
  printf("\nUsage: %s [OPTIONS] database textfile...",internal::my_progname);
168
 
  internal::print_defaults("drizzle",load_default_groups);
169
 
  my_print_help(my_long_options);
170
 
  my_print_variables(my_long_options);
171
 
}
172
 
 
173
 
static int get_one_option(int optid, const struct option *, char *argument)
174
 
{
175
 
  char *endchar= NULL;
176
 
  uint64_t temp_drizzle_port= 0;
177
 
 
178
 
  switch(optid) {
179
 
  case 'p':
180
 
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
181
 
    /* if there is an alpha character this is not a valid port */
182
 
    if (strlen(endchar) != 0)
183
 
    {
184
 
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
185
 
      return EXIT_ARGUMENT_INVALID;
186
 
    }
187
 
    /* If the port number is > 65535 it is not a valid port
188
 
       This also helps with potential data loss casting unsigned long to a
189
 
       uint32_t. */
190
 
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
191
 
    {
192
 
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
193
 
      return EXIT_ARGUMENT_INVALID;
194
 
    }
195
 
    else
196
 
    {
197
 
      opt_drizzle_port= (uint32_t) temp_drizzle_port;
198
 
    }
199
 
    break;
200
 
  case 'P':
201
 
    if (argument)
202
 
    {
203
 
      char *start=argument;
204
 
      if (opt_password)
205
 
        free(opt_password);
206
 
      opt_password = strdup(argument);
207
 
      if (opt_password == NULL)
208
 
      {
209
 
        fprintf(stderr, "Memory allocation error while copying password. "
210
 
                        "Aborting.\n");
211
 
        return EXIT_OUT_OF_MEMORY;
212
 
      }
213
 
      while (*argument)
214
 
      {
215
 
        /* Overwriting password with 'x' */
216
 
        *argument++= 'x';
217
 
      }
218
 
      if (*start)
219
 
      {
220
 
        /* Cut length of argument */
221
 
        start[1]= 0;
222
 
      }
223
 
      tty_password= 0;
224
 
    }
225
 
    else
226
 
      tty_password= 1;
227
 
    break;
228
 
  case OPT_DRIZZLE_PROTOCOL:
229
 
    break;
230
 
  case 'V': print_version(); exit(0);
231
 
  case 'I':
232
 
  case '?':
233
 
    usage();
234
 
    exit(0);
235
 
  }
236
 
  return 0;
237
 
}
238
 
 
239
 
 
240
 
static int get_options(int *argc, char ***argv)
241
 
{
242
 
  int ho_error;
243
 
 
244
 
  if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
245
 
    exit(ho_error);
246
 
 
247
 
  if (enclosed && opt_enclosed)
 
72
std::string opt_columns,
 
73
  opt_enclosed,
 
74
  escaped,
 
75
  password,
 
76
  current_db,
 
77
  lines_terminated,
 
78
  current_user,
 
79
  opt_password,
 
80
  enclosed,  
 
81
  current_host,
 
82
  fields_terminated;
 
83
 
 
84
 
 
85
static int get_options(void)
 
86
{
 
87
 
 
88
  if (! enclosed.empty() && ! opt_enclosed.empty())
248
89
  {
249
90
    fprintf(stderr, "You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n");
250
91
    return(1);
254
95
    fprintf(stderr, "You can't use --ignore_unique (-i) and --replace (-r) at the same time.\n");
255
96
    return(1);
256
97
  }
257
 
  if (*argc < 2)
258
 
  {
259
 
    usage();
260
 
    return 1;
261
 
  }
262
 
  current_db= *((*argv)++);
263
 
  (*argc)--;
 
98
 
264
99
  if (tty_password)
265
100
    opt_password=client_get_tty_password(NULL);
266
101
  return(0);
318
153
 
319
154
  end+= sprintf(end, " INTO TABLE %s", tablename);
320
155
 
321
 
  if (fields_terminated || enclosed || opt_enclosed || escaped)
 
156
  if (! fields_terminated.empty() || ! enclosed.empty() || ! opt_enclosed.empty() || ! escaped.empty())
322
157
      end= strcpy(end, " FIELDS")+7;
323
 
  end= add_load_option(end, fields_terminated, " TERMINATED BY");
324
 
  end= add_load_option(end, enclosed, " ENCLOSED BY");
325
 
  end= add_load_option(end, opt_enclosed,
 
158
  end= add_load_option(end, (char *)fields_terminated.c_str(), " TERMINATED BY");
 
159
  end= add_load_option(end, (char *)enclosed.c_str(), " ENCLOSED BY");
 
160
  end= add_load_option(end, (char *)opt_enclosed.c_str(),
326
161
           " OPTIONALLY ENCLOSED BY");
327
 
  end= add_load_option(end, escaped, " ESCAPED BY");
328
 
  end= add_load_option(end, lines_terminated, " LINES TERMINATED BY");
 
162
  end= add_load_option(end, (char *)escaped.c_str(), " ESCAPED BY");
 
163
  end= add_load_option(end, (char *)lines_terminated.c_str(), " LINES TERMINATED BY");
329
164
  if (opt_ignore_lines >= 0)
330
165
  {
331
166
    end= strcpy(end, " IGNORE ")+8;
334
169
    end= strcpy(end, buffer.str().c_str())+ buffer.str().size();
335
170
    end= strcpy(end, " LINES")+6;
336
171
  }
337
 
  if (opt_columns)
 
172
  if (! opt_columns.empty())
338
173
  {
339
174
    end= strcpy(end, " (")+2;
340
 
    end= strcpy(end, opt_columns)+strlen(opt_columns);
 
175
    end= strcpy(end, (char *)opt_columns.c_str()+opt_columns.length());
341
176
    end= strcpy(end, ")")+1;
342
177
  }
343
178
  *end= '\0';
352
187
  {
353
188
    if (strcmp(drizzle_result_info(&result), ""))
354
189
    {
355
 
      fprintf(stdout, "%s.%s: %s\n", current_db, tablename,
 
190
      fprintf(stdout, "%s.%s: %s\n", current_db.c_str(), tablename,
356
191
        drizzle_result_info(&result));
357
192
    }
358
193
  }
390
225
}
391
226
 
392
227
 
393
 
static drizzle_con_st *db_connect(char *host, char *database,
394
 
                                  char *user, char *passwd)
 
228
static drizzle_con_st *db_connect(const string host, const string database,
 
229
                                  const string user, const string passwd)
395
230
{
396
231
  drizzle_st *drizzle;
397
232
  drizzle_con_st *con;
398
233
  drizzle_return_t ret;
399
234
 
400
235
  if (verbose)
401
 
    fprintf(stdout, "Connecting to %s\n", host ? host : "localhost");
 
236
    fprintf(stdout, "Connecting to %s\n", ! host.empty() ? host.c_str() : "localhost");
402
237
  if (!(drizzle= drizzle_create(NULL)))
403
238
    return 0;
404
 
  if (!(con= drizzle_con_add_tcp(drizzle,NULL,host,opt_drizzle_port,user,passwd,
405
 
                                 database, opt_mysql ? DRIZZLE_CON_MYSQL : DRIZZLE_CON_NONE)))
 
239
  if (!(con= drizzle_con_add_tcp(drizzle,NULL,(char *)host.c_str(),opt_drizzle_port,(char *)user.c_str(),(char *)passwd.c_str(),
 
240
                                 (char *)database.c_str(), opt_mysql ? DRIZZLE_CON_MYSQL : DRIZZLE_CON_NONE)))
406
241
  {
407
242
    return 0;
408
243
  }
414
249
  }
415
250
 
416
251
  if (verbose)
417
 
    fprintf(stdout, "Selecting database %s\n", database);
 
252
    fprintf(stdout, "Selecting database %s\n", database.c_str());
418
253
 
419
254
  return con;
420
255
}
421
256
 
422
257
 
423
258
 
424
 
static void db_disconnect(char *host, drizzle_con_st *con)
 
259
static void db_disconnect(const string host, drizzle_con_st *con)
425
260
{
426
261
  if (verbose)
427
 
    fprintf(stdout, "Disconnecting from %s\n", host ? host : "localhost");
 
262
    fprintf(stdout, "Disconnecting from %s\n", ! host.empty() ? host.c_str() : "localhost");
428
263
  drizzle_free(drizzle_con_drizzle(con));
429
264
}
430
265
 
557
392
 
558
393
int main(int argc, char **argv)
559
394
{
 
395
try
 
396
{
560
397
  int error=0;
561
 
  char **argv_to_free;
562
 
  MY_INIT(argv[0]);
563
 
 
564
 
  internal::load_defaults("drizzle",load_default_groups,&argc,&argv);
565
 
  /* argv is changed in the program */
566
 
  argv_to_free= argv;
567
 
  if (get_options(&argc, &argv))
568
 
  {
569
 
    internal::free_defaults(argv_to_free);
 
398
 
 
399
  po::options_description commandline_options("Options used only in command line");
 
400
  commandline_options.add_options()
 
401
 
 
402
  ("debug,#", po::value<string>(),
 
403
  "Output debug log. Often this is 'd:t:o,filename'.")
 
404
  ("delete,d", po::value<bool>(&opt_delete)->default_value(false)->zero_tokens(),
 
405
  "First delete all rows from table.")
 
406
  ("help,?", "Displays this help and exits.")
 
407
  ("ignore,i", po::value<bool>(&ignore_unique)->default_value(false)->zero_tokens(),
 
408
  "If duplicate unique key was found, keep old row.")
 
409
  ("lock-tables,l", po::value<bool>(&lock_tables)->default_value(false)->zero_tokens(),
 
410
  "Lock all tables for write (this disables threads).")
 
411
  ("low-priority", po::value<bool>(&opt_low_priority)->default_value(false)->zero_tokens(),
 
412
  "Use LOW_PRIORITY when updating the table.")
 
413
  ("replace,r", po::value<bool>(&opt_replace)->default_value(false)->zero_tokens(),
 
414
  "If duplicate unique key was found, replace old row.")
 
415
  ("verbose,v", po::value<bool>(&verbose)->default_value(false)->zero_tokens(),
 
416
  "Print info about the various stages.")
 
417
  ("version,V", "Output version information and exit.")
 
418
  ;
 
419
 
 
420
  po::options_description import_options("Options specific to the drizzleimport");
 
421
  import_options.add_options()
 
422
  ("columns,C", po::value<string>(&opt_columns)->default_value(""),
 
423
  "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.")
 
424
  ("fields-terminated-by", po::value<string>(&fields_terminated)->default_value(""),
 
425
  "Fields in the textfile are terminated by ...")
 
426
  ("fields-enclosed-by", po::value<string>(&enclosed)->default_value(""),
 
427
  "Fields in the importfile are enclosed by ...")
 
428
  ("fields-optionally-enclosed-by", po::value<string>(&opt_enclosed)->default_value(""),
 
429
  "Fields in the i.file are opt. enclosed by ...")
 
430
  ("fields-escaped-by", po::value<string>(&escaped)->default_value(""),
 
431
  "Fields in the i.file are escaped by ...")
 
432
  ("force,f", po::value<bool>(&ignore_errors)->default_value(false)->zero_tokens(),
 
433
  "Continue even if we get an sql-error.")
 
434
  ("ignore-lines", po::value<int64_t>(&opt_ignore_lines)->default_value(0),
 
435
  "Ignore first n lines of data infile.")
 
436
  ("lines-terminated-by", po::value<string>(&lines_terminated)->default_value(""),
 
437
  "Lines in the i.file are terminated by ...")
 
438
  ("local,L", po::value<bool>(&opt_local_file)->default_value(false)->zero_tokens(),
 
439
  "Read all files through the client.")
 
440
  ("silent,s", po::value<bool>(&silent)->default_value(false)->zero_tokens(),
 
441
  "Be more silent.")
 
442
  ("use-threads", po::value<uint32_t>(&opt_use_threads)->default_value(0),
 
443
  "Load files in parallel. The argument is the number of threads to use for loading data.")
 
444
  ;
 
445
 
 
446
  po::options_description client_options("Options specific to the client");
 
447
  client_options.add_options()
 
448
  ("host,h", po::value<string>(&current_host)->default_value("localhost"),
 
449
  "Connect to host.")
 
450
  ("mysql,m", po::value<bool>(&opt_mysql)->default_value(true)->zero_tokens(),
 
451
  N_("Use MySQL Protocol."))
 
452
  ("password,P", po::value<string>(&password),
 
453
  "Password to use when connecting to server. If password is not given it's asked from the tty." )
 
454
  ("port,p", po::value<uint32_t>(&opt_drizzle_port)->default_value(0),
 
455
  "Port number to use for connection") 
 
456
  ("protocol", po::value<string>(),
 
457
  "The protocol of connection (tcp,socket,pipe,memory).")
 
458
  ("user,u", po::value<string>(&current_user)->default_value(""),
 
459
  "User for login if not current user.")
 
460
  ;
 
461
 
 
462
  po::options_description long_options("Allowed Options");
 
463
  long_options.add(commandline_options).add(import_options).add(client_options);
 
464
 
 
465
  std::string system_config_dir_import(SYSCONFDIR); 
 
466
  system_config_dir_import.append("/drizzle/drizzleimport.cnf");
 
467
 
 
468
  std::string system_config_dir_client(SYSCONFDIR); 
 
469
  system_config_dir_client.append("/drizzle/client.cnf");
 
470
  
 
471
  po::variables_map vm;
 
472
  po::store(po::command_line_parser(argc, argv).options(long_options).run(), vm);
 
473
 
 
474
  ifstream user_import_ifs("~/.drizzle/drizzleimport.cnf");
 
475
  po::store(parse_config_file(user_import_ifs, import_options), vm);
 
476
 
 
477
  ifstream system_import_ifs(system_config_dir_import.c_str());
 
478
  store(parse_config_file(system_import_ifs, import_options), vm);
 
479
 
 
480
  ifstream user_client_ifs("~/.drizzle/client.cnf");
 
481
  po::store(parse_config_file(user_client_ifs, client_options), vm);
 
482
 
 
483
  ifstream system_client_ifs(system_config_dir_client.c_str());
 
484
  po::store(parse_config_file(system_client_ifs, client_options), vm);
 
485
 
 
486
  po::notify(vm);
 
487
 
 
488
  if (vm.count("port"))
 
489
  {
 
490
    
 
491
    /* If the port number is > 65535 it is not a valid port
 
492
       This also helps with potential data loss casting unsigned long to a
 
493
       uint32_t. */
 
494
    if (opt_drizzle_port > 65535)
 
495
    {
 
496
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
497
      exit(EXIT_ARGUMENT_INVALID);
 
498
    }
 
499
  }
 
500
 
 
501
  if (vm.count("password"))
 
502
  {
 
503
    if (!opt_password.empty())
 
504
      opt_password.erase();
 
505
    opt_password= password;
 
506
    if (opt_password.c_str() == NULL)
 
507
    {
 
508
      fprintf(stderr, _("Memory allocation error while copying password. "
 
509
                        "Aborting.\n"));
 
510
      exit(EXIT_OUT_OF_MEMORY);
 
511
    }
 
512
    char *start= (char *)password.c_str();
 
513
    char *temp_pass= (char *)password.c_str();
 
514
    while (*temp_pass)
 
515
    {
 
516
        /* Overwriting password with 'x' */
 
517
        *temp_pass++= 'x';
 
518
    }
 
519
    if (*start)
 
520
    {
 
521
      start[1]= 0;
 
522
    }
 
523
  }
 
524
  else
 
525
  {
 
526
      tty_password= true;
 
527
  }
 
528
 
 
529
  if (vm.count("version"))
 
530
  {
 
531
    printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n" ,internal::my_progname,
 
532
    IMPORT_VERSION, drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
 
533
  }
 
534
  
 
535
  if (vm.count("help") || argc < 2)
 
536
  {
 
537
    printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n" ,internal::my_progname,
 
538
    IMPORT_VERSION, drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
 
539
    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");
 
540
    printf("\
 
541
    Loads tables from text files in various formats.  The base name of the\n\
 
542
    text file must be the name of the table that should be used.\n\
 
543
    If one uses sockets to connect to the Drizzle server, the server will open and\n\
 
544
    read the text file directly. In other cases the client will open the text\n\
 
545
    file. The SQL command 'LOAD DATA INFILE' is used to import the rows.\n");
 
546
 
 
547
    printf("\nUsage: %s [OPTIONS] database textfile...",internal::my_progname);
 
548
    cout<<long_options;
 
549
    exit(0);
 
550
  }
 
551
 
 
552
 
 
553
  if (get_options())
 
554
  {
570
555
    return(1);
571
556
  }
 
557
  
 
558
  current_db= (*argv)++;
 
559
  argc--;
572
560
 
573
561
#ifdef HAVE_LIBPTHREAD
574
562
  if (opt_use_threads && !lock_tables)
631
619
    drizzle_return_t ret;
632
620
    if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
633
621
    {
634
 
      internal::free_defaults(argv_to_free);
635
622
      return(1);
636
623
    }
637
624
 
655
642
          exitcode= error;
656
643
    db_disconnect(current_host, con);
657
644
  }
658
 
  free(opt_password);
659
 
  internal::free_defaults(argv_to_free);
660
 
  internal::my_end();
 
645
  opt_password.empty();
 
646
}
 
647
  catch(exception &err)
 
648
  {
 
649
    cerr<<err.what()<<endl;
 
650
  }
661
651
  return(exitcode);
662
652
}