~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

  • Committer: David Shrewsbury
  • Date: 2010-10-05 20:38:57 UTC
  • mto: (1814.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 1816.
  • Revision ID: shrewsbury.dave@gmail.com-20101005203857-v2xvi0zblliyxxf8
Add options to proto files for use when generating Java source files.

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(),
1330
1326
 
1331
1327
  po::options_description drizzle_options(N_("Options specific to the drizzle client"));
1332
1328
  drizzle_options.add_options()
1333
 
  ("disable-auto-rehash,A",
 
1329
  ("disable-auto-rehash",
1334
1330
  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."))
1335
1331
  ("auto-vertical-output", po::value<bool>(&auto_vertical_output)->default_value(false)->zero_tokens(),
1336
1332
  N_("Automatically switch to vertical output mode if the result is wider than the terminal width."))
1361
1357
  ("raw,r", po::value<bool>(&opt_raw_data)->default_value(false)->zero_tokens(),
1362
1358
  N_("Write fields without conversion. Used with --batch.")) 
1363
1359
  ("disable-reconnect", N_("Do not reconnect if the connection is lost."))
1364
 
  ("shutdown", po::value<bool>()->zero_tokens(),
 
1360
  ("shutdown", po::value<bool>(&opt_shutdown)->default_value(false)->zero_tokens(),
1365
1361
  N_("Shutdown the server"))
1366
1362
  ("silent,s", N_("Be more silent. Print results with a tab as separator, each row on new line."))
1367
1363
  ("tee", po::value<string>(),
1368
1364
  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
1365
  ("disable-tee", po::value<bool>()->default_value(false)->zero_tokens(), 
1370
1366
  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),
 
1367
  ("connect_timeout", po::value<uint32_t>(&opt_connect_timeout)->default_value(0)->notifier(&check_timeout_value),
1372
1368
  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),
 
1369
  ("max_input_line", po::value<uint32_t>(&opt_max_input_line)->default_value(16*1024L*1024L)->notifier(&check_max_input_line),
1374
1370
  N_("Max length of input line"))
1375
 
  ("select-limit", po::value<uint32_t>(&select_limit)->default_value(1000L),
 
1371
  ("select_limit", po::value<uint32_t>(&select_limit)->default_value(1000L),
1376
1372
  N_("Automatic limit for SELECT when using --safe-updates"))
1377
 
  ("max-join-size", po::value<uint32_t>(&max_join_size)->default_value(1000000L),
 
1373
  ("max_join_size", po::value<uint32_t>(&max_join_size)->default_value(1000000L),
1378
1374
  N_("Automatic limit for rows in a join when using --safe-updates"))
1379
1375
  ;
1380
1376
 
1386
1382
  N_("Password to use when connecting to server. If password is not given it's asked from the tty."))
1387
1383
  ("port,p", po::value<uint32_t>()->default_value(0),
1388
1384
  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
1385
  ("user,u", po::value<string>(&current_user)->default_value(""),
1393
 
#endif
1394
1386
  N_("User for login if not current user."))
1395
1387
  ("protocol",po::value<string>(&opt_protocol)->default_value("mysql"),
1396
1388
  N_("The protocol of connection (mysql or drizzle)."))
1406
1398
  system_config_dir_client.append("/drizzle/client.cnf");
1407
1399
 
1408
1400
  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
 
 
 
1401
  
1418
1402
  po::variables_map vm;
1419
1403
 
1420
1404
  po::positional_options_description p;
1436
1420
    user_config_dir_client.append("/drizzle/client.cnf");
1437
1421
 
1438
1422
    ifstream user_drizzle_ifs(user_config_dir_drizzle.c_str());
1439
 
    po::store(dpo::parse_config_file(user_drizzle_ifs, drizzle_options), vm);
 
1423
    po::store(parse_config_file(user_drizzle_ifs, drizzle_options), vm);
1440
1424
 
1441
1425
    ifstream user_client_ifs(user_config_dir_client.c_str());
1442
 
    po::store(dpo::parse_config_file(user_client_ifs, client_options), vm);
 
1426
    po::store(parse_config_file(user_client_ifs, client_options), vm);
1443
1427
 
1444
1428
    ifstream system_drizzle_ifs(system_config_dir_drizzle.c_str());
1445
 
    store(dpo::parse_config_file(system_drizzle_ifs, drizzle_options), vm);
 
1429
    store(parse_config_file(system_drizzle_ifs, drizzle_options), vm);
1446
1430
 
1447
1431
    ifstream system_client_ifs(system_config_dir_client.c_str());
1448
 
    po::store(dpo::parse_config_file(system_client_ifs, client_options), vm);
 
1432
    po::store(parse_config_file(system_client_ifs, client_options), vm);
1449
1433
  }
1450
1434
 
1451
1435
  po::notify(vm);
1452
1436
 
1453
 
#ifdef DRIZZLE_ADMIN_TOOL
1454
 
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1455
 
                         getenv("DRIZZLE_PS1") :
1456
 
                         "drizzleadmin> ");
1457
 
#else
1458
1437
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1459
1438
                         getenv("DRIZZLE_PS1") :
1460
1439
                         "drizzle> ");
1461
 
#endif
1462
1440
  if (default_prompt == NULL)
1463
1441
  {
1464
1442
    fprintf(stderr, _("Memory allocation error while constructing initial "
1517
1495
  opt_rehash= (vm.count("disable-auto-rehash")) ? false : true;
1518
1496
  opt_reconnect= (vm.count("disable-reconnect")) ? false : true;
1519
1497
 
1520
 
  /* Don't rehash with --shutdown */
1521
 
  if (vm.count("shutdown"))
1522
 
  {
1523
 
    opt_rehash= false;
1524
 
    opt_shutdown= true;
1525
 
  }
1526
 
 
1527
1498
  if (vm.count("delimiter"))
1528
1499
  {
1529
1500
    /* Check that delimiter does not contain a backslash */
1823
1794
 
1824
1795
  if (sig >= 0)
1825
1796
    put_info(sig ? _("Aborted") : _("Bye"), INFO_RESULT,0,0);
1826
 
  delete glob_buffer;
1827
 
  delete processed_prompt;
 
1797
  if (glob_buffer)
 
1798
    delete glob_buffer;
 
1799
  if (processed_prompt)
 
1800
    delete processed_prompt;
1828
1801
  opt_password.erase();
1829
1802
  free(histfile);
1830
1803
  free(histfile_tmp);
1848
1821
void handle_sigint(int sig)
1849
1822
{
1850
1823
  char kill_buffer[40];
1851
 
  boost::scoped_ptr<drizzle_con_st> kill_drizzle(new drizzle_con_st);
 
1824
  drizzle_con_st kill_drizzle;
1852
1825
  drizzle_result_st res;
1853
1826
  drizzle_return_t ret;
1854
1827
 
1857
1830
    goto err;
1858
1831
  }
1859
1832
 
1860
 
  if (drizzle_con_add_tcp(&drizzle, kill_drizzle.get(), current_host.c_str(),
 
1833
  if (drizzle_con_add_tcp(&drizzle, &kill_drizzle, current_host.c_str(),
1861
1834
    opt_drizzle_port, current_user.c_str(), opt_password.c_str(), NULL,
1862
1835
    use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL) == NULL)
1863
1836
  {
1868
1841
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1869
1842
          drizzle_con_thread_id(&con));
1870
1843
 
1871
 
  if (drizzle_query_str(kill_drizzle.get(), &res, kill_buffer, &ret) != NULL)
 
1844
  if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
1872
1845
    drizzle_result_free(&res);
1873
1846
 
1874
 
  drizzle_con_free(kill_drizzle.get());
 
1847
  drizzle_con_free(&kill_drizzle);
1875
1848
  tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1876
1849
 
1877
1850
  interrupted_query= 1;
2342
2315
  {
2343
2316
    *out++='\n';
2344
2317
    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
2318
    if ((!*ml_comment || preserve_comments))
2352
2319
      buffer->append(line, length);
2353
2320
  }
2623
2590
  {
2624
2591
    string query;
2625
2592
 
2626
 
    query.append("show fields in `");
 
2593
    query.append("show fields in '");
2627
2594
    query.append(table_row[0]);
2628
 
    query.append("`");
 
2595
    query.append("'");
2629
2596
    
2630
2597
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2631
2598
                      &ret) != NULL)
2695
2662
 The different commands
2696
2663
***************************************************************************/
2697
2664
 
2698
 
int drizzleclient_real_query_for_lazy(const char *buf, size_t length,
 
2665
int drizzleclient_real_query_for_lazy(const char *buf, int length,
2699
2666
                                      drizzle_result_st *result,
2700
2667
                                      uint32_t *error_code)
2701
2668
{
3612
3579
  /* Skip the spaces between the command and the argument */
3613
3580
  while (param && isspace(*param))
3614
3581
    param++;
3615
 
  if (!param || (*param == '\0')) // if pager was not given, use the default
 
3582
  if (!param || !strlen(param)) // if pager was not given, use the default
3616
3583
  {
3617
3584
    if (!default_pager_set)
3618
3585
    {
3917
3884
  return 0;
3918
3885
}
3919
3886
 
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
3887
static int
3959
3888
com_warnings(string *, const char *)
3960
3889
{
4045
3974
    drizzle_free(&drizzle);
4046
3975
  }
4047
3976
  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
3977
  if (drizzle_con_add_tcp(&drizzle, &con, (char *)host.c_str(),
4056
3978
    opt_drizzle_port, (char *)user.c_str(),
4057
3979
    (char *)password.c_str(), (char *)database.c_str(),
4058
 
    options) == NULL)
 
3980
    use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL) == NULL)
4059
3981
  {
4060
3982
    (void) put_error(&con, NULL);
4061
3983
    (void) fflush(stdout);