~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

  • Committer: Joseph Daly
  • Date: 2010-09-12 02:04:41 UTC
  • mto: (1759.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1760.
  • Revision ID: jdaly@rx7-20100912020441-3hc8uahm6ar25i52
fix spacing

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
#include <cassert>
62
62
#include <stdarg.h>
63
63
#include <math.h>
64
 
#include <memory>
65
64
#include "client/linebuffer.h"
66
65
#include <signal.h>
67
66
#include <sys/ioctl.h>
159
158
#define vidattr(A) {}      // Can't get this to work
160
159
#endif
161
160
#include <boost/program_options.hpp>
162
 
#include <boost/scoped_ptr.hpp>
163
 
#include "drizzled/program_options/config_file.h"
164
161
 
165
162
using namespace std;
166
163
namespace po=boost::program_options;
167
 
namespace dpo=drizzled::program_options;
168
164
 
169
165
/* Don't try to make a nice table if the data is too big */
170
166
const uint32_t MAX_COLUMN_LENGTH= 1024;
180
176
 
181
177
  Status(int in_exit_status, 
182
178
         uint32_t in_query_start_line,
183
 
         char *in_file_name,
 
179
         char *in_file_name,
184
180
         LineBuffer *in_line_buff,
185
 
         bool in_batch,
186
 
         bool in_add_to_history)
 
181
         bool in_batch,
 
182
         bool in_add_to_history)
187
183
    :
188
184
    exit_status(in_exit_status),
189
185
    query_start_line(in_query_start_line),
193
189
    add_to_history(in_add_to_history)
194
190
    {}
195
191
 
196
 
  Status() :
197
 
    exit_status(0),
198
 
    query_start_line(0),
199
 
    file_name(NULL),
200
 
    line_buff(NULL),
201
 
    batch(false),        
202
 
    add_to_history(false)
203
 
  {}
 
192
  Status()
 
193
    :
 
194
    exit_status(),
 
195
    query_start_line(),
 
196
    file_name(),
 
197
    line_buff(),
 
198
    batch(),        
 
199
    add_to_history()
 
200
    {}
204
201
  
205
202
  int getExitStatus() const
206
203
  {
290
287
  connected= false, opt_raw_data= false, unbuffered= false,
291
288
  output_tables= false, opt_rehash= true, skip_updates= false,
292
289
  safe_updates= false, one_database= false,
293
 
  opt_shutdown= false, opt_ping= false,
 
290
  opt_compress= false, opt_shutdown= false, opt_ping= false,
294
291
  vertical= false, line_numbers= true, column_names= true,
295
292
  opt_nopager= true, opt_outfile= false, named_cmds= false,
296
293
  opt_nobeep= false, opt_reconnect= true,
341
338
static uint32_t delimiter_length= 1;
342
339
unsigned short terminal_width= 80;
343
340
 
344
 
int drizzleclient_real_query_for_lazy(const char *buf, size_t length,
 
341
int drizzleclient_real_query_for_lazy(const char *buf, int length,
345
342
                                      drizzle_result_st *result,
346
343
                                      uint32_t *error_code);
347
344
int drizzleclient_store_result_for_lazy(drizzle_result_st *result);
360
357
  com_help(string *str,const char*), com_clear(string *str,const char*),
361
358
  com_connect(string *str,const char*), com_status(string *str,const char*),
362
359
  com_use(string *str,const char*), com_source(string *str, const char*),
363
 
  com_shutdown(string *str,const char*),
364
360
  com_rehash(string *str, const char*), com_tee(string *str, const char*),
365
361
  com_notee(string *str, const char*),
366
362
  com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
492
488
    N_("Set outfile [to_outfile]. Append everything into given outfile.") ),
493
489
  Commands( "use",    'u', com_use,    1,
494
490
    N_("Use another database. Takes database name as argument.") ),
495
 
  Commands( "shutdown",    'u', com_shutdown,    1,
496
 
    N_("Shutdown the instance you are connected too.") ),
497
491
  Commands( "warnings", 'W', com_warnings,  0,
498
492
    N_("Show warnings after every statement.") ),
499
493
  Commands( "nowarning", 'w', com_nowarnings, 0,
1293
1287
  N_("Display column type information."))
1294
1288
  ("comments,c", po::value<bool>(&preserve_comments)->default_value(false)->zero_tokens(),
1295
1289
  N_("Preserve comments. Send comments to the server. The default is --skip-comments (discard comments), enable with --comments"))
 
1290
  ("compress,C", po::value<bool>(&opt_compress)->default_value(false)->zero_tokens(),
 
1291
  N_("Use compression in server/client protocol."))  
1296
1292
  ("vertical,E", po::value<bool>(&vertical)->default_value(false)->zero_tokens(),
1297
1293
  N_("Print the output of a query (rows) vertically."))
1298
1294
  ("force,f", po::value<bool>(&ignore_errors)->default_value(false)->zero_tokens(),
1299
1295
  N_("Continue even if we get an sql error."))
1300
1296
  ("named-commands,G", po::value<bool>(&named_cmds)->default_value(false)->zero_tokens(),
1301
 
  N_("Enable named commands. Named commands mean this program's internal commands; see drizzle> help . When enabled, the named commands can be used from any line of the query, otherwise only from the first line, before an enter."))
 
1297
  N_("Enable named commands. Named commands mean this program's internal commands; see drizzle> help . When enabled, the named commands can be used from any line of the query, otherwise only from the first line, before an enter. Disable with --disable-named-commands. This option is disabled by default."))
 
1298
  ("no-named-commands,g",
 
1299
  N_("Named commands are disabled. Use \\* form only, or use named commands only in the beginning of a line ending with a semicolon (;) Since version 10.9 the client now starts with this option ENABLED by default! Disable with '-G'. Long format commands still work from the first line. WARNING: option deprecated; use --disable-named-commands instead."))
 
1300
  ("ignore-spaces,i", N_("Ignore space after function names."))
1302
1301
  ("no-beep,b", po::value<bool>(&opt_nobeep)->default_value(false)->zero_tokens(),
1303
1302
  N_("Turn off beep on error."))
1304
 
  ("disable-line-numbers", N_("Do not write line numbers for errors."))
1305
 
  ("disable-column-names", N_("Do not write column names in results."))
 
1303
  ("line-numbers", po::value<bool>(&line_numbers)->default_value(true)->zero_tokens(),
 
1304
  N_("Write line numbers for errors."))
 
1305
  ("skip-line-numbers,L", 
 
1306
  N_("Don't write line number for errors. WARNING: -L is deprecated, use long version of this option instead."))
 
1307
  ("column-name", po::value<bool>(&column_names)->default_value(true)->zero_tokens(),
 
1308
  N_("Write column names in results."))
1306
1309
  ("skip-column-names,N", 
1307
1310
  N_("Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead."))
1308
1311
  ("set-variable,O", po::value<string>(),
1309
1312
  N_("Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value."))
1310
1313
  ("table,t", po::value<bool>(&output_tables)->default_value(false)->zero_tokens(),
1311
1314
  N_("Output in table format.")) 
1312
 
  ("safe-updates,U", po::value<bool>(&safe_updates)->default_value(false)->zero_tokens(),
 
1315
  ("safe-updates,U", po::value<bool>(&safe_updates)->default_value(0)->zero_tokens(),
1313
1316
  N_("Only allow UPDATE and DELETE that uses keys."))
1314
 
  ("i-am-a-dummy,U", po::value<bool>(&safe_updates)->default_value(false)->zero_tokens(),
 
1317
  ("i-am-a-dummy,U", po::value<bool>(&safe_updates)->default_value(0)->zero_tokens(),
1315
1318
  N_("Synonym for option --safe-updates, -U."))
1316
1319
  ("verbose,v", po::value<string>(&opt_verbose)->default_value(""),
1317
1320
  N_("-v vvv implies that verbose= 3, Used to specify verbose"))
1330
1333
 
1331
1334
  po::options_description drizzle_options(N_("Options specific to the drizzle client"));
1332
1335
  drizzle_options.add_options()
1333
 
  ("disable-auto-rehash,A",
1334
 
  N_("Disable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time."))
 
1336
  ("auto-rehash", po::value<bool>(&opt_rehash)->default_value(true)->zero_tokens(),
 
1337
  N_("Enable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. Disable with --disable-auto-rehash."))
 
1338
  ("no-auto-rehash,A",N_("No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of drizzle_st and disables rehashing on reconnect. WARNING: options deprecated; use --disable-auto-rehash instead."))
1335
1339
  ("auto-vertical-output", po::value<bool>(&auto_vertical_output)->default_value(false)->zero_tokens(),
1336
1340
  N_("Automatically switch to vertical output mode if the result is wider than the terminal width."))
1337
1341
  ("database,D", po::value<string>(&current_db)->default_value(""),
1343
1347
  ("execute,e", po::value<string>(),
1344
1348
  N_("Execute command and quit. (Disables --force and history file)"))
1345
1349
  ("local-infile", po::value<bool>(&opt_local_infile)->default_value(false)->zero_tokens(),
1346
 
  N_("Enable LOAD DATA LOCAL INFILE."))
 
1350
  N_("Enable/disable LOAD DATA LOCAL INFILE."))
1347
1351
  ("unbuffered,n", po::value<bool>(&unbuffered)->default_value(false)->zero_tokens(),
1348
1352
  N_("Flush buffer after each query."))
1349
1353
  ("sigint-ignore", po::value<bool>(&opt_sigint_ignore)->default_value(false)->zero_tokens(),
1360
1364
  N_("Don't cache result, print it row by row. This may slow down the server if the output is suspended. Doesn't use history file."))
1361
1365
  ("raw,r", po::value<bool>(&opt_raw_data)->default_value(false)->zero_tokens(),
1362
1366
  N_("Write fields without conversion. Used with --batch.")) 
1363
 
  ("disable-reconnect", N_("Do not reconnect if the connection is lost."))
1364
 
  ("shutdown", po::value<bool>()->zero_tokens(),
 
1367
  ("reconnect", po::value<bool>(&opt_reconnect)->default_value(true)->zero_tokens(),
 
1368
  N_("Reconnect if the connection is lost. Disable with --disable-reconnect. This option is enabled by default."))
 
1369
  ("shutdown", po::value<bool>(&opt_shutdown)->default_value(false)->zero_tokens(),
1365
1370
  N_("Shutdown the server"))
1366
1371
  ("silent,s", N_("Be more silent. Print results with a tab as separator, each row on new line."))
1367
1372
  ("tee", po::value<string>(),
1368
1373
  N_("Append everything into outfile. See interactive help (\\h) also. Does not work in batch mode. Disable with --disable-tee. This option is disabled by default."))
1369
1374
  ("disable-tee", po::value<bool>()->default_value(false)->zero_tokens(), 
1370
1375
  N_("Disable outfile. See interactive help (\\h) also."))
1371
 
  ("connect-timeout", po::value<uint32_t>(&opt_connect_timeout)->default_value(0)->notifier(&check_timeout_value),
 
1376
  ("wait,w", N_("Wait and retry if connection is down."))
 
1377
  ("connect_timeout", po::value<uint32_t>(&opt_connect_timeout)->default_value(0)->notifier(&check_timeout_value),
1372
1378
  N_("Number of seconds before connection timeout."))
1373
 
  ("max-input-line", po::value<uint32_t>(&opt_max_input_line)->default_value(16*1024L*1024L)->notifier(&check_max_input_line),
 
1379
  ("max_input_line", po::value<uint32_t>(&opt_max_input_line)->default_value(16*1024L*1024L)->notifier(&check_max_input_line),
1374
1380
  N_("Max length of input line"))
1375
 
  ("select-limit", po::value<uint32_t>(&select_limit)->default_value(1000L),
 
1381
  ("select_limit", po::value<uint32_t>(&select_limit)->default_value(1000L),
1376
1382
  N_("Automatic limit for SELECT when using --safe-updates"))
1377
 
  ("max-join-size", po::value<uint32_t>(&max_join_size)->default_value(1000000L),
 
1383
  ("max_join_size", po::value<uint32_t>(&max_join_size)->default_value(1000000L),
1378
1384
  N_("Automatic limit for rows in a join when using --safe-updates"))
1379
1385
  ;
1380
1386
 
1386
1392
  N_("Password to use when connecting to server. If password is not given it's asked from the tty."))
1387
1393
  ("port,p", po::value<uint32_t>()->default_value(0),
1388
1394
  N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, built-in default"))
1389
 
#ifdef DRIZZLE_ADMIN_TOOL
1390
 
  ("user,u", po::value<string>(&current_user)->default_value("root"),
1391
 
#else
1392
1395
  ("user,u", po::value<string>(&current_user)->default_value(""),
1393
 
#endif
1394
1396
  N_("User for login if not current user."))
1395
1397
  ("protocol",po::value<string>(&opt_protocol)->default_value("mysql"),
1396
1398
  N_("The protocol of connection (mysql or drizzle)."))
1406
1408
  system_config_dir_client.append("/drizzle/client.cnf");
1407
1409
 
1408
1410
  std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
1409
 
 
1410
 
  if (user_config_dir.compare(0, 2, "~/") == 0)
1411
 
  {
1412
 
    char *homedir;
1413
 
    homedir= getenv("HOME");
1414
 
    if (homedir != NULL)
1415
 
      user_config_dir.replace(0, 1, homedir);
1416
 
  }
1417
 
 
 
1411
  
1418
1412
  po::variables_map vm;
1419
1413
 
1420
1414
  po::positional_options_description p;
1421
1415
  p.add("database", 1);
1422
1416
 
1423
 
  // Disable allow_guessing
1424
 
  int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
1425
 
 
1426
1417
  po::store(po::command_line_parser(argc, argv).options(long_options).
1427
 
            style(style).positional(p).extra_parser(parse_password_arg).run(),
1428
 
            vm);
 
1418
            positional(p).extra_parser(parse_password_arg).run(), vm);
1429
1419
 
1430
1420
  if (! vm["no-defaults"].as<bool>())
1431
1421
  {
1436
1426
    user_config_dir_client.append("/drizzle/client.cnf");
1437
1427
 
1438
1428
    ifstream user_drizzle_ifs(user_config_dir_drizzle.c_str());
1439
 
    po::store(dpo::parse_config_file(user_drizzle_ifs, drizzle_options), vm);
 
1429
    po::store(parse_config_file(user_drizzle_ifs, drizzle_options), vm);
1440
1430
 
1441
1431
    ifstream user_client_ifs(user_config_dir_client.c_str());
1442
 
    po::store(dpo::parse_config_file(user_client_ifs, client_options), vm);
 
1432
    po::store(parse_config_file(user_client_ifs, client_options), vm);
1443
1433
 
1444
1434
    ifstream system_drizzle_ifs(system_config_dir_drizzle.c_str());
1445
 
    store(dpo::parse_config_file(system_drizzle_ifs, drizzle_options), vm);
 
1435
    store(parse_config_file(system_drizzle_ifs, drizzle_options), vm);
1446
1436
 
1447
1437
    ifstream system_client_ifs(system_config_dir_client.c_str());
1448
 
    po::store(dpo::parse_config_file(system_client_ifs, client_options), vm);
 
1438
    po::store(parse_config_file(system_client_ifs, client_options), vm);
1449
1439
  }
1450
1440
 
1451
1441
  po::notify(vm);
1452
1442
 
1453
 
#ifdef DRIZZLE_ADMIN_TOOL
1454
 
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1455
 
                         getenv("DRIZZLE_PS1") :
1456
 
                         "drizzleadmin> ");
1457
 
#else
1458
1443
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1459
1444
                         getenv("DRIZZLE_PS1") :
1460
1445
                         "drizzle> ");
1461
 
#endif
1462
1446
  if (default_prompt == NULL)
1463
1447
  {
1464
1448
    fprintf(stderr, _("Memory allocation error while constructing initial "
1510
1494
      close(stdout_fileno_copy);             /* Clean up dup(). */
1511
1495
  }
1512
1496
 
1513
 
  /* Inverted Booleans */
1514
 
 
1515
 
  line_numbers= (vm.count("disable-line-numbers")) ? false : true;
1516
 
  column_names= (vm.count("disable-column-names")) ? false : true;
1517
 
  opt_rehash= (vm.count("disable-auto-rehash")) ? false : true;
1518
 
  opt_reconnect= (vm.count("disable-reconnect")) ? false : true;
1519
 
 
1520
 
  /* Don't rehash with --shutdown */
1521
 
  if (vm.count("shutdown"))
1522
 
  {
1523
 
    opt_rehash= false;
1524
 
    opt_shutdown= true;
1525
 
  }
1526
 
 
1527
1497
  if (vm.count("delimiter"))
1528
1498
  {
1529
1499
    /* Check that delimiter does not contain a backslash */
1823
1793
 
1824
1794
  if (sig >= 0)
1825
1795
    put_info(sig ? _("Aborted") : _("Bye"), INFO_RESULT,0,0);
1826
 
  delete glob_buffer;
1827
 
  delete processed_prompt;
 
1796
  if (glob_buffer)
 
1797
    delete glob_buffer;
 
1798
  if (processed_prompt)
 
1799
    delete processed_prompt;
1828
1800
  opt_password.erase();
1829
1801
  free(histfile);
1830
1802
  free(histfile_tmp);
1848
1820
void handle_sigint(int sig)
1849
1821
{
1850
1822
  char kill_buffer[40];
1851
 
  boost::scoped_ptr<drizzle_con_st> kill_drizzle(new drizzle_con_st);
 
1823
  drizzle_con_st kill_drizzle;
1852
1824
  drizzle_result_st res;
1853
1825
  drizzle_return_t ret;
1854
1826
 
1857
1829
    goto err;
1858
1830
  }
1859
1831
 
1860
 
  if (drizzle_con_add_tcp(&drizzle, kill_drizzle.get(), current_host.c_str(),
 
1832
  if (drizzle_con_add_tcp(&drizzle, &kill_drizzle, current_host.c_str(),
1861
1833
    opt_drizzle_port, current_user.c_str(), opt_password.c_str(), NULL,
1862
1834
    use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL) == NULL)
1863
1835
  {
1868
1840
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1869
1841
          drizzle_con_thread_id(&con));
1870
1842
 
1871
 
  if (drizzle_query_str(kill_drizzle.get(), &res, kill_buffer, &ret) != NULL)
 
1843
  if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
1872
1844
    drizzle_result_free(&res);
1873
1845
 
1874
 
  drizzle_con_free(kill_drizzle.get());
 
1846
  drizzle_con_free(&kill_drizzle);
1875
1847
  tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1876
1848
 
1877
1849
  interrupted_query= 1;
2342
2314
  {
2343
2315
    *out++='\n';
2344
2316
    uint32_t length=(uint32_t) (out-line);
2345
 
    if ((buffer->length() + length) > opt_max_input_line)
2346
 
    {
2347
 
      status.setExitStatus(1);
2348
 
      put_info(_("Not found a delimiter within max_input_line of input"), INFO_ERROR, 0, 0);
2349
 
      return 1;
2350
 
    }
2351
2317
    if ((!*ml_comment || preserve_comments))
2352
2318
      buffer->append(line, length);
2353
2319
  }
2623
2589
  {
2624
2590
    string query;
2625
2591
 
2626
 
    query.append("show fields in `");
 
2592
    query.append("show fields in '");
2627
2593
    query.append(table_row[0]);
2628
 
    query.append("`");
 
2594
    query.append("'");
2629
2595
    
2630
2596
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2631
2597
                      &ret) != NULL)
2695
2661
 The different commands
2696
2662
***************************************************************************/
2697
2663
 
2698
 
int drizzleclient_real_query_for_lazy(const char *buf, size_t length,
 
2664
int drizzleclient_real_query_for_lazy(const char *buf, int length,
2699
2665
                                      drizzle_result_st *result,
2700
2666
                                      uint32_t *error_code)
2701
2667
{
3612
3578
  /* Skip the spaces between the command and the argument */
3613
3579
  while (param && isspace(*param))
3614
3580
    param++;
3615
 
  if (!param || (*param == '\0')) // if pager was not given, use the default
 
3581
  if (!param || !strlen(param)) // if pager was not given, use the default
3616
3582
  {
3617
3583
    if (!default_pager_set)
3618
3584
    {
3917
3883
  return 0;
3918
3884
}
3919
3885
 
3920
 
static int com_shutdown(string *, const char *)
3921
 
{
3922
 
  drizzle_result_st result;
3923
 
  drizzle_return_t ret;
3924
 
 
3925
 
  if (verbose)
3926
 
  {
3927
 
    printf(_("shutting down drizzled"));
3928
 
    if (opt_drizzle_port > 0)
3929
 
      printf(_(" on port %d"), opt_drizzle_port);
3930
 
    printf("... ");
3931
 
  }
3932
 
 
3933
 
  if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
3934
 
                       &ret) == NULL || ret != DRIZZLE_RETURN_OK)
3935
 
  {
3936
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
3937
 
    {
3938
 
      fprintf(stderr, _("shutdown failed; error: '%s'"),
3939
 
              drizzle_result_error(&result));
3940
 
      drizzle_result_free(&result);
3941
 
    }
3942
 
    else
3943
 
    {
3944
 
      fprintf(stderr, _("shutdown failed; error: '%s'"),
3945
 
              drizzle_con_error(&con));
3946
 
    }
3947
 
    return false;
3948
 
  }
3949
 
 
3950
 
  drizzle_result_free(&result);
3951
 
 
3952
 
  if (verbose)
3953
 
    printf(_("done\n"));
3954
 
 
3955
 
  return false;
3956
 
}
3957
 
 
3958
3886
static int
3959
3887
com_warnings(string *, const char *)
3960
3888
{
4045
3973
    drizzle_free(&drizzle);
4046
3974
  }
4047
3975
  drizzle_create(&drizzle);
4048
 
 
4049
 
#ifdef DRIZZLE_ADMIN_TOOL
4050
 
  drizzle_con_options_t options= (drizzle_con_options_t) (DRIZZLE_CON_ADMIN | (use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL));
4051
 
#else
4052
 
  drizzle_con_options_t options= (drizzle_con_options_t) (use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
4053
 
#endif
4054
 
 
4055
3976
  if (drizzle_con_add_tcp(&drizzle, &con, (char *)host.c_str(),
4056
3977
    opt_drizzle_port, (char *)user.c_str(),
4057
3978
    (char *)password.c_str(), (char *)database.c_str(),
4058
 
    options) == NULL)
 
3979
    use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL) == NULL)
4059
3980
  {
4060
3981
    (void) put_error(&con, NULL);
4061
3982
    (void) fflush(stdout);