~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-10-16 19:31:35 UTC
  • mto: (1859.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1860.
  • Revision ID: osullivan.padraig@gmail.com-20101016193135-75fdbfizty54g9of
Replaced a uint8_t member with a standard bitset since it was being used as a bitset....

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
161
#include "drizzled/program_options/config_file.h"
164
162
 
165
163
using namespace std;
180
178
 
181
179
  Status(int in_exit_status, 
182
180
         uint32_t in_query_start_line,
183
 
         char *in_file_name,
 
181
         char *in_file_name,
184
182
         LineBuffer *in_line_buff,
185
 
         bool in_batch,
186
 
         bool in_add_to_history)
 
183
         bool in_batch,
 
184
         bool in_add_to_history)
187
185
    :
188
186
    exit_status(in_exit_status),
189
187
    query_start_line(in_query_start_line),
193
191
    add_to_history(in_add_to_history)
194
192
    {}
195
193
 
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
 
  {}
 
194
  Status()
 
195
    :
 
196
    exit_status(),
 
197
    query_start_line(),
 
198
    file_name(),
 
199
    line_buff(),
 
200
    batch(),        
 
201
    add_to_history()
 
202
    {}
204
203
  
205
204
  int getExitStatus() const
206
205
  {
341
340
static uint32_t delimiter_length= 1;
342
341
unsigned short terminal_width= 80;
343
342
 
344
 
int drizzleclient_real_query_for_lazy(const char *buf, size_t length,
 
343
int drizzleclient_real_query_for_lazy(const char *buf, int length,
345
344
                                      drizzle_result_st *result,
346
345
                                      uint32_t *error_code);
347
346
int drizzleclient_store_result_for_lazy(drizzle_result_st *result);
360
359
  com_help(string *str,const char*), com_clear(string *str,const char*),
361
360
  com_connect(string *str,const char*), com_status(string *str,const char*),
362
361
  com_use(string *str,const char*), com_source(string *str, const char*),
363
 
  com_shutdown(string *str,const char*),
364
362
  com_rehash(string *str, const char*), com_tee(string *str, const char*),
365
363
  com_notee(string *str, const char*),
366
364
  com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
492
490
    N_("Set outfile [to_outfile]. Append everything into given outfile.") ),
493
491
  Commands( "use",    'u', com_use,    1,
494
492
    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
493
  Commands( "warnings", 'W', com_warnings,  0,
498
494
    N_("Show warnings after every statement.") ),
499
495
  Commands( "nowarning", 'w', com_nowarnings, 0,
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;
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 "
1823
1801
 
1824
1802
  if (sig >= 0)
1825
1803
    put_info(sig ? _("Aborted") : _("Bye"), INFO_RESULT,0,0);
1826
 
  delete glob_buffer;
1827
 
  delete processed_prompt;
 
1804
  if (glob_buffer)
 
1805
    delete glob_buffer;
 
1806
  if (processed_prompt)
 
1807
    delete processed_prompt;
1828
1808
  opt_password.erase();
1829
1809
  free(histfile);
1830
1810
  free(histfile_tmp);
1848
1828
void handle_sigint(int sig)
1849
1829
{
1850
1830
  char kill_buffer[40];
1851
 
  boost::scoped_ptr<drizzle_con_st> kill_drizzle(new drizzle_con_st);
 
1831
  drizzle_con_st kill_drizzle;
1852
1832
  drizzle_result_st res;
1853
1833
  drizzle_return_t ret;
1854
1834
 
1857
1837
    goto err;
1858
1838
  }
1859
1839
 
1860
 
  if (drizzle_con_add_tcp(&drizzle, kill_drizzle.get(), current_host.c_str(),
 
1840
  if (drizzle_con_add_tcp(&drizzle, &kill_drizzle, current_host.c_str(),
1861
1841
    opt_drizzle_port, current_user.c_str(), opt_password.c_str(), NULL,
1862
1842
    use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL) == NULL)
1863
1843
  {
1868
1848
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1869
1849
          drizzle_con_thread_id(&con));
1870
1850
 
1871
 
  if (drizzle_query_str(kill_drizzle.get(), &res, kill_buffer, &ret) != NULL)
 
1851
  if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
1872
1852
    drizzle_result_free(&res);
1873
1853
 
1874
 
  drizzle_con_free(kill_drizzle.get());
 
1854
  drizzle_con_free(&kill_drizzle);
1875
1855
  tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1876
1856
 
1877
1857
  interrupted_query= 1;
2342
2322
  {
2343
2323
    *out++='\n';
2344
2324
    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
2325
    if ((!*ml_comment || preserve_comments))
2352
2326
      buffer->append(line, length);
2353
2327
  }
2623
2597
  {
2624
2598
    string query;
2625
2599
 
2626
 
    query.append("show fields in `");
 
2600
    query.append("show fields in '");
2627
2601
    query.append(table_row[0]);
2628
 
    query.append("`");
 
2602
    query.append("'");
2629
2603
    
2630
2604
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2631
2605
                      &ret) != NULL)
2695
2669
 The different commands
2696
2670
***************************************************************************/
2697
2671
 
2698
 
int drizzleclient_real_query_for_lazy(const char *buf, size_t length,
 
2672
int drizzleclient_real_query_for_lazy(const char *buf, int length,
2699
2673
                                      drizzle_result_st *result,
2700
2674
                                      uint32_t *error_code)
2701
2675
{
3612
3586
  /* Skip the spaces between the command and the argument */
3613
3587
  while (param && isspace(*param))
3614
3588
    param++;
3615
 
  if (!param || (*param == '\0')) // if pager was not given, use the default
 
3589
  if (!param || !strlen(param)) // if pager was not given, use the default
3616
3590
  {
3617
3591
    if (!default_pager_set)
3618
3592
    {
3917
3891
  return 0;
3918
3892
}
3919
3893
 
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
3894
static int
3959
3895
com_warnings(string *, const char *)
3960
3896
{
4045
3981
    drizzle_free(&drizzle);
4046
3982
  }
4047
3983
  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
3984
  if (drizzle_con_add_tcp(&drizzle, &con, (char *)host.c_str(),
4056
3985
    opt_drizzle_port, (char *)user.c_str(),
4057
3986
    (char *)password.c_str(), (char *)database.c_str(),
4058
 
    options) == NULL)
 
3987
    use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL) == NULL)
4059
3988
  {
4060
3989
    (void) put_error(&con, NULL);
4061
3990
    (void) fflush(stdout);