~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

  • Committer: kalebral at gmail
  • Date: 2010-12-04 04:58:08 UTC
  • mto: (1971.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1972.
  • Revision ID: kalebral@gmail.com-20101204045808-acto22oxfg43m02e
a few more updates of files that did not have license or had incorrect license structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
#include "client/get_password.h"
41
41
 
42
 
#include "boost/date_time/posix_time/posix_time.hpp"
 
42
#if TIME_WITH_SYS_TIME
 
43
# include <sys/time.h>
 
44
# include <time.h>
 
45
#else
 
46
# if HAVE_SYS_TIME_H
 
47
#  include <sys/time.h>
 
48
# else
 
49
#  include <time.h>
 
50
# endif
 
51
#endif
43
52
 
44
53
#include <cerrno>
45
54
#include <string>
316
325
  current_password,
317
326
  opt_password,
318
327
  opt_protocol;
319
 
 
320
 
static const char* get_day_name(int day_of_week)
321
 
{
322
 
  switch(day_of_week)
323
 
  {
324
 
  case 0:
325
 
    return _("Sun");
326
 
  case 1:
327
 
    return _("Mon");
328
 
  case 2:
329
 
    return _("Tue");
330
 
  case 3:
331
 
    return _("Wed");
332
 
  case 4:
333
 
    return _("Thu");
334
 
  case 5:
335
 
    return _("Fri");
336
 
  case 6:
337
 
    return _("Sat");
338
 
  }
339
 
 
340
 
  return NULL;
341
 
}
342
 
 
343
 
static const char* get_month_name(int month)
344
 
{
345
 
  switch(month)
346
 
  {
347
 
  case 0:
348
 
    return _("Jan");
349
 
  case 1:
350
 
    return _("Feb");
351
 
  case 2:
352
 
    return _("Mar");
353
 
  case 3:
354
 
    return _("Apr");
355
 
  case 4:
356
 
    return _("May");
357
 
  case 5:
358
 
    return _("Jun");
359
 
  case 6:
360
 
    return _("Jul");
361
 
  case 7:
362
 
    return _("Aug");
363
 
  case 8:
364
 
    return _("Sep");
365
 
  case 9:
366
 
    return _("Oct");
367
 
  case 10:
368
 
    return _("Nov");
369
 
  case 11:
370
 
    return _("Dec");
371
 
  }
372
 
 
373
 
  return NULL;
374
 
}
375
 
 
 
328
// TODO: Need to i18n these
 
329
static const char *day_names[]= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
 
330
static const char *month_names[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul",
 
331
                                  "Aug","Sep","Oct","Nov","Dec"};
376
332
/* @TODO: Remove this */
377
333
#define FN_REFLEN 512
378
334
 
412
368
  com_nopager(string *str, const char*), com_pager(string *str, const char*);
413
369
 
414
370
static int read_and_execute(bool interactive);
415
 
static int sql_connect(const string &host, const string &database, const string &user, const string &password);
 
371
static int sql_connect(const string &host, const string &database, const string &user, const string &password,
 
372
                       uint32_t silent);
416
373
static const char *server_version_string(drizzle_con_st *con);
417
374
static int put_info(const char *str,INFO_TYPE info,uint32_t error,
418
375
                    const char *sql_state);
534
491
  Commands( "tee",    'T', com_tee,    1,
535
492
    N_("Set outfile [to_outfile]. Append everything into given outfile.") ),
536
493
  Commands( "use",    'u', com_use,    1,
537
 
    N_("Use another schema. Takes schema name as argument.") ),
 
494
    N_("Use another database. Takes database name as argument.") ),
538
495
  Commands( "shutdown",    'u', com_shutdown,    1,
539
496
    N_("Shutdown the instance you are connected too.") ),
540
497
  Commands( "warnings", 'W', com_warnings,  0,
1165
1122
static void print_tab_data(drizzle_result_st *result);
1166
1123
static void print_table_data_vertically(drizzle_result_st *result);
1167
1124
static void print_warnings(uint32_t error_code);
1168
 
static boost::posix_time::ptime start_timer(void);
1169
 
static void end_timer(boost::posix_time::ptime, string &buff);
1170
 
static void drizzle_end_timer(boost::posix_time::ptime, string &buff);
1171
 
static void nice_time(boost::posix_time::time_duration duration, string &buff);
 
1125
static uint32_t start_timer(void);
 
1126
static void end_timer(uint32_t start_time,char *buff);
 
1127
static void drizzle_end_timer(uint32_t start_time,char *buff);
 
1128
static void nice_time(double sec,char *buff,bool part_second);
1172
1129
extern "C" void drizzle_end(int sig);
1173
1130
extern "C" void handle_sigint(int sig);
1174
1131
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1324
1281
# if defined(HAVE_LOCALE_H)
1325
1282
  setlocale(LC_ALL, "");
1326
1283
# endif
1327
 
  bindtextdomain("drizzle7", LOCALEDIR);
1328
 
  textdomain("drizzle7");
 
1284
  bindtextdomain("drizzle", LOCALEDIR);
 
1285
  textdomain("drizzle");
1329
1286
#endif
1330
1287
 
1331
 
  po::options_description commandline_options(_("Options used only in command line"));
 
1288
  po::options_description commandline_options(N_("Options used only in command line"));
1332
1289
  commandline_options.add_options()
1333
 
  ("help,?",_("Displays this help and exit."))
1334
 
  ("batch,B",_("Don't use history file. Disable interactive behavior. (Enables --silent)"))
 
1290
  ("help,?",N_("Displays this help and exit."))
 
1291
  ("batch,B",N_("Don't use history file. Disable interactive behavior. (Enables --silent)"))
1335
1292
  ("column-type-info", po::value<bool>(&column_types_flag)->default_value(false)->zero_tokens(),
1336
 
  _("Display column type information."))
 
1293
  N_("Display column type information."))
1337
1294
  ("comments,c", po::value<bool>(&preserve_comments)->default_value(false)->zero_tokens(),
1338
 
  _("Preserve comments. Send comments to the server. The default is --skip-comments (discard comments), enable with --comments"))
 
1295
  N_("Preserve comments. Send comments to the server. The default is --skip-comments (discard comments), enable with --comments"))
1339
1296
  ("vertical,E", po::value<bool>(&vertical)->default_value(false)->zero_tokens(),
1340
 
  _("Print the output of a query (rows) vertically."))
 
1297
  N_("Print the output of a query (rows) vertically."))
1341
1298
  ("force,f", po::value<bool>(&ignore_errors)->default_value(false)->zero_tokens(),
1342
 
  _("Continue even if we get an sql error."))
 
1299
  N_("Continue even if we get an sql error."))
1343
1300
  ("named-commands,G", po::value<bool>(&named_cmds)->default_value(false)->zero_tokens(),
1344
 
  _("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."))
 
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."))
1345
1302
  ("no-beep,b", po::value<bool>(&opt_nobeep)->default_value(false)->zero_tokens(),
1346
 
  _("Turn off beep on error."))
1347
 
  ("disable-line-numbers", _("Do not write line numbers for errors."))
1348
 
  ("disable-column-names", _("Do not write column names in results."))
 
1303
  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."))
1349
1306
  ("skip-column-names,N", 
1350
 
  _("Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead."))
 
1307
  N_("Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead."))
1351
1308
  ("set-variable,O", po::value<string>(),
1352
 
  _("Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value."))
 
1309
  N_("Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value."))
1353
1310
  ("table,t", po::value<bool>(&output_tables)->default_value(false)->zero_tokens(),
1354
 
  _("Output in table format.")) 
 
1311
  N_("Output in table format.")) 
1355
1312
  ("safe-updates,U", po::value<bool>(&safe_updates)->default_value(false)->zero_tokens(),
1356
 
  _("Only allow UPDATE and DELETE that uses keys."))
 
1313
  N_("Only allow UPDATE and DELETE that uses keys."))
1357
1314
  ("i-am-a-dummy,U", po::value<bool>(&safe_updates)->default_value(false)->zero_tokens(),
1358
 
  _("Synonym for option --safe-updates, -U."))
 
1315
  N_("Synonym for option --safe-updates, -U."))
1359
1316
  ("verbose,v", po::value<string>(&opt_verbose)->default_value(""),
1360
 
  _("-v vvv implies that verbose= 3, Used to specify verbose"))
1361
 
  ("version,V", _("Output version information and exit."))
 
1317
  N_("-v vvv implies that verbose= 3, Used to specify verbose"))
 
1318
  ("version,V", N_("Output version information and exit."))
1362
1319
  ("secure-auth", po::value<bool>(&opt_secure_auth)->default_value(false)->zero_tokens(),
1363
 
  _("Refuse client connecting to server if it uses old (pre-4.1.1) protocol"))
 
1320
  N_("Refuse client connecting to server if it uses old (pre-4.1.1) protocol"))
1364
1321
  ("show-warnings", po::value<bool>(&show_warnings)->default_value(false)->zero_tokens(),
1365
 
  _("Show warnings after every statement."))
 
1322
  N_("Show warnings after every statement."))
1366
1323
  ("show-progress-size", po::value<uint32_t>(&show_progress_size)->default_value(0),
1367
 
  _("Number of lines before each import progress report."))
 
1324
  N_("Number of lines before each import progress report."))
1368
1325
  ("ping", po::value<bool>(&opt_ping)->default_value(false)->zero_tokens(),
1369
 
  _("Ping the server to check if it's alive."))
 
1326
  N_("Ping the server to check if it's alive."))
1370
1327
  ("no-defaults", po::value<bool>()->default_value(false)->zero_tokens(),
1371
 
  _("Configuration file defaults are not used if no-defaults is set"))
 
1328
  N_("Configuration file defaults are not used if no-defaults is set"))
1372
1329
  ;
1373
1330
 
1374
 
  po::options_description drizzle_options(_("Options specific to the drizzle client"));
 
1331
  po::options_description drizzle_options(N_("Options specific to the drizzle client"));
1375
1332
  drizzle_options.add_options()
1376
1333
  ("disable-auto-rehash,A",
1377
 
  _("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."))
 
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."))
1378
1335
  ("auto-vertical-output", po::value<bool>(&auto_vertical_output)->default_value(false)->zero_tokens(),
1379
 
  _("Automatically switch to vertical output mode if the result is wider than the terminal width."))
 
1336
  N_("Automatically switch to vertical output mode if the result is wider than the terminal width."))
1380
1337
  ("database,D", po::value<string>(&current_db)->default_value(""),
1381
 
  _("Database to use."))
 
1338
  N_("Database to use."))
1382
1339
  ("default-character-set",po::value<string>(),
1383
 
  _("(not used)"))
 
1340
  N_("(not used)"))
1384
1341
  ("delimiter", po::value<string>(&delimiter_str)->default_value(";"),
1385
 
  _("Delimiter to be used."))
 
1342
  N_("Delimiter to be used."))
1386
1343
  ("execute,e", po::value<string>(),
1387
 
  _("Execute command and quit. (Disables --force and history file)"))
 
1344
  N_("Execute command and quit. (Disables --force and history file)"))
1388
1345
  ("local-infile", po::value<bool>(&opt_local_infile)->default_value(false)->zero_tokens(),
1389
 
  _("Enable LOAD DATA LOCAL INFILE."))
 
1346
  N_("Enable LOAD DATA LOCAL INFILE."))
1390
1347
  ("unbuffered,n", po::value<bool>(&unbuffered)->default_value(false)->zero_tokens(),
1391
 
  _("Flush buffer after each query."))
 
1348
  N_("Flush buffer after each query."))
1392
1349
  ("sigint-ignore", po::value<bool>(&opt_sigint_ignore)->default_value(false)->zero_tokens(),
1393
 
  _("Ignore SIGINT (CTRL-C)"))
 
1350
  N_("Ignore SIGINT (CTRL-C)"))
1394
1351
  ("one-database,o", po::value<bool>(&one_database)->default_value(false)->zero_tokens(),
1395
 
  _("Only update the default database. This is useful for skipping updates to other database in the update log."))
 
1352
  N_("Only update the default database. This is useful for skipping updates to other database in the update log."))
1396
1353
  ("pager", po::value<string>(),
1397
 
  _("Pager to use to display results. If you don't supply an option the default pager is taken from your ENV variable PAGER. Valid pagers are less, more, cat [> filename], etc. See interactive help (\\h) also. This option does not work in batch mode. Disable with --disable-pager. This option is disabled by default."))
 
1354
  N_("Pager to use to display results. If you don't supply an option the default pager is taken from your ENV variable PAGER. Valid pagers are less, more, cat [> filename], etc. See interactive help (\\h) also. This option does not work in batch mode. Disable with --disable-pager. This option is disabled by default."))
1398
1355
  ("disable-pager", po::value<bool>(&opt_nopager)->default_value(false)->zero_tokens(),
1399
 
  _("Disable pager and print to stdout. See interactive help (\\h) also."))
 
1356
  N_("Disable pager and print to stdout. See interactive help (\\h) also."))
1400
1357
  ("prompt", po::value<string>(&current_prompt)->default_value(""),  
1401
 
  _("Set the drizzle prompt to this value."))
 
1358
  N_("Set the drizzle prompt to this value."))
1402
1359
  ("quick,q", po::value<bool>(&quick)->default_value(false)->zero_tokens(),
1403
 
  _("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."))
 
1360
  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."))
1404
1361
  ("raw,r", po::value<bool>(&opt_raw_data)->default_value(false)->zero_tokens(),
1405
 
  _("Write fields without conversion. Used with --batch.")) 
1406
 
  ("disable-reconnect", _("Do not reconnect if the connection is lost."))
 
1362
  N_("Write fields without conversion. Used with --batch.")) 
 
1363
  ("disable-reconnect", N_("Do not reconnect if the connection is lost."))
1407
1364
  ("shutdown", po::value<bool>()->zero_tokens(),
1408
 
  _("Shutdown the server"))
1409
 
  ("silent,s", _("Be more silent. Print results with a tab as separator, each row on new line."))
 
1365
  N_("Shutdown the server"))
 
1366
  ("silent,s", N_("Be more silent. Print results with a tab as separator, each row on new line."))
1410
1367
  ("tee", po::value<string>(),
1411
 
  _("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."))
 
1368
  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."))
1412
1369
  ("disable-tee", po::value<bool>()->default_value(false)->zero_tokens(), 
1413
 
  _("Disable outfile. See interactive help (\\h) also."))
 
1370
  N_("Disable outfile. See interactive help (\\h) also."))
1414
1371
  ("connect-timeout", po::value<uint32_t>(&opt_connect_timeout)->default_value(0)->notifier(&check_timeout_value),
1415
 
  _("Number of seconds before connection timeout."))
 
1372
  N_("Number of seconds before connection timeout."))
1416
1373
  ("max-input-line", po::value<uint32_t>(&opt_max_input_line)->default_value(16*1024L*1024L)->notifier(&check_max_input_line),
1417
 
  _("Max length of input line"))
 
1374
  N_("Max length of input line"))
1418
1375
  ("select-limit", po::value<uint32_t>(&select_limit)->default_value(1000L),
1419
 
  _("Automatic limit for SELECT when using --safe-updates"))
 
1376
  N_("Automatic limit for SELECT when using --safe-updates"))
1420
1377
  ("max-join-size", po::value<uint32_t>(&max_join_size)->default_value(1000000L),
1421
 
  _("Automatic limit for rows in a join when using --safe-updates"))
 
1378
  N_("Automatic limit for rows in a join when using --safe-updates"))
1422
1379
  ;
1423
1380
 
1424
 
  po::options_description client_options(_("Options specific to the client"));
 
1381
  po::options_description client_options(N_("Options specific to the client"));
1425
1382
  client_options.add_options()
1426
1383
  ("host,h", po::value<string>(&current_host)->default_value("localhost"),
1427
 
  _("Connect to host"))
 
1384
  N_("Connect to host"))
1428
1385
  ("password,P", po::value<string>(&current_password)->default_value(PASSWORD_SENTINEL),
1429
 
  _("Password to use when connecting to server. If password is not given it's asked from the tty."))
 
1386
  N_("Password to use when connecting to server. If password is not given it's asked from the tty."))
1430
1387
  ("port,p", po::value<uint32_t>()->default_value(0),
1431
 
  _("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, built-in default"))
1432
 
#ifdef DRIZZLE_ADMIN_TOOL
1433
 
  ("user,u", po::value<string>(&current_user)->default_value("root"),
1434
 
#else
 
1388
  N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, built-in default"))
1435
1389
  ("user,u", po::value<string>(&current_user)->default_value(""),
1436
 
#endif
1437
 
  _("User for login if not current user."))
 
1390
  N_("User for login if not current user."))
1438
1391
  ("protocol",po::value<string>(&opt_protocol)->default_value("mysql"),
1439
 
  _("The protocol of connection (mysql or drizzle)."))
 
1392
  N_("The protocol of connection (mysql or drizzle)."))
1440
1393
  ;
1441
1394
 
1442
 
  po::options_description long_options(_("Allowed Options"));
 
1395
  po::options_description long_options(N_("Allowed Options"));
1443
1396
  long_options.add(commandline_options).add(drizzle_options).add(client_options);
1444
1397
 
1445
1398
  std::string system_config_dir_drizzle(SYSCONFDIR); 
1493
1446
 
1494
1447
  po::notify(vm);
1495
1448
 
1496
 
#ifdef DRIZZLE_ADMIN_TOOL
1497
 
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1498
 
                         getenv("DRIZZLE_PS1") :
1499
 
                         "drizzleadmin> ");
1500
 
#else
1501
1449
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1502
1450
                         getenv("DRIZZLE_PS1") :
1503
1451
                         "drizzle> ");
1504
 
#endif
1505
1452
  if (default_prompt == NULL)
1506
1453
  {
1507
1454
    fprintf(stderr, _("Memory allocation error while constructing initial "
1508
1455
                      "prompt. Aborting.\n"));
1509
1456
    exit(ENOMEM);
1510
1457
  }
1511
 
 
1512
 
  if (current_prompt.empty())
1513
 
    current_prompt= strdup(default_prompt);
1514
 
 
 
1458
  current_prompt= strdup(default_prompt);
1515
1459
  if (current_prompt.empty())
1516
1460
  {
1517
1461
    fprintf(stderr, _("Memory allocation error while constructing initial "
1536
1480
  if (! isatty(0) || ! isatty(1))
1537
1481
  {
1538
1482
    status.setBatch(1); opt_silent=1;
 
1483
    ignore_errors=0;
1539
1484
  }
1540
1485
  else
1541
1486
    status.setAddToHistory(1);
1712
1657
  }
1713
1658
  if (vm.count("silent"))
1714
1659
  {
1715
 
    opt_silent= 2;
 
1660
    opt_silent++;
1716
1661
  }
1717
1662
  
1718
1663
  if (vm.count("help") || vm.count("version"))
1728
1673
           "This is free software,\n"
1729
1674
           "and you are welcome to modify and redistribute it "
1730
1675
           "under the GPL license\n"));
1731
 
    printf(_("Usage: drizzle [OPTIONS] [schema]\n"));
 
1676
    printf(_("Usage: drizzle [OPTIONS] [database]\n"));
1732
1677
    cout << long_options;
1733
1678
    exit(0);
1734
1679
  }
1740
1685
  }
1741
1686
 
1742
1687
  memset(&drizzle, 0, sizeof(drizzle));
1743
 
  if (sql_connect(current_host, current_db, current_user, opt_password))
 
1688
  if (sql_connect(current_host, current_db, current_user, opt_password,opt_silent))
1744
1689
  {
1745
1690
    quick= 1;          // Avoid history
1746
1691
    status.setExitStatus(1);
2582
2527
  drizzle_return_t ret;
2583
2528
  drizzle_result_st databases,tables,fields;
2584
2529
  drizzle_row_t database_row,table_row;
 
2530
  drizzle_column_st *sql_field;
2585
2531
  string tmp_str, tmp_str_lower;
2586
 
  std::string query;
2587
2532
 
2588
2533
  if (status.getBatch() || quick || current_db.empty())
2589
2534
    return;      // We don't need completion in batches
2603
2548
  /* hash Drizzle functions (to be implemented) */
2604
2549
 
2605
2550
  /* hash all database names */
2606
 
  if (drizzle_query_str(&con, &databases, "select schema_name from information_schema.schemata", &ret) != NULL)
 
2551
  if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
2607
2552
  {
2608
2553
    if (ret == DRIZZLE_RETURN_OK)
2609
2554
    {
2623
2568
    drizzle_result_free(&databases);
2624
2569
  }
2625
2570
 
2626
 
  query= "select table_name, column_name from information_schema.columns where table_schema='";
2627
 
  query.append(current_db);
2628
 
  query.append("' order by table_name");
2629
 
  
2630
 
  if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2631
 
                    &ret) != NULL)
 
2571
  /* hash all table names */
 
2572
  if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
2632
2573
  {
2633
 
    if (ret == DRIZZLE_RETURN_OK &&
2634
 
        drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
 
2574
    if (ret != DRIZZLE_RETURN_OK)
 
2575
    {
 
2576
      drizzle_result_free(&tables);
 
2577
      return;
 
2578
    }
 
2579
 
 
2580
    if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
 
2581
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
 
2582
    else
2635
2583
    {
2636
2584
      if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
2637
2585
      {
2641
2589
                      "You can turn off this feature to get a quicker "
2642
2590
                      "startup with -A\n\n"));
2643
2591
      }
2644
 
 
2645
 
      std::string table_name;
2646
 
      while ((table_row=drizzle_row_next(&fields)))
 
2592
      while ((table_row=drizzle_row_next(&tables)))
2647
2593
      {
2648
 
        if (table_name.compare(table_row[0]) != 0)
2649
 
        {
2650
 
          tmp_str= table_row[0];
2651
 
          tmp_str_lower= lower_string(tmp_str);
2652
 
          completion_map[tmp_str_lower]= tmp_str;
2653
 
          table_name= table_row[0];
2654
 
        }
2655
2594
        tmp_str= table_row[0];
2656
 
        tmp_str.append(".");
2657
 
        tmp_str.append(table_row[1]);
2658
 
        tmp_str_lower= lower_string(tmp_str);
2659
 
        completion_map[tmp_str_lower]= tmp_str;
2660
 
 
2661
 
        tmp_str= table_row[1];
2662
 
        tmp_str_lower= lower_string(tmp_str);
2663
 
        completion_map[tmp_str_lower]= tmp_str;
2664
 
      }
2665
 
    }
2666
 
  }
2667
 
  drizzle_result_free(&fields);
 
2595
        tmp_str_lower= lower_string(tmp_str);
 
2596
        completion_map[tmp_str_lower]= tmp_str;
 
2597
      }
 
2598
    }
 
2599
  }
 
2600
  else
 
2601
    return;
 
2602
 
 
2603
  /* hash all field names, both with the table prefix and without it */
 
2604
  if (drizzle_result_row_count(&tables) == 0)
 
2605
  {
 
2606
    drizzle_result_free(&tables);
 
2607
    return;
 
2608
  }
 
2609
 
 
2610
  drizzle_row_seek(&tables, 0);
 
2611
 
 
2612
  while ((table_row=drizzle_row_next(&tables)))
 
2613
  {
 
2614
    string query;
 
2615
 
 
2616
    query.append("show fields in `");
 
2617
    query.append(table_row[0]);
 
2618
    query.append("`");
 
2619
    
 
2620
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
 
2621
                      &ret) != NULL)
 
2622
    {
 
2623
      if (ret == DRIZZLE_RETURN_OK &&
 
2624
          drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
 
2625
      {
 
2626
        while ((sql_field=drizzle_column_next(&fields)))
 
2627
        {
 
2628
          tmp_str=table_row[0];
 
2629
          tmp_str.append(".");
 
2630
          tmp_str.append(drizzle_column_name(sql_field));
 
2631
          tmp_str_lower= lower_string(tmp_str);
 
2632
          completion_map[tmp_str_lower]= tmp_str;
 
2633
 
 
2634
          tmp_str=drizzle_column_name(sql_field);
 
2635
          tmp_str_lower= lower_string(tmp_str);
 
2636
          completion_map[tmp_str_lower]= tmp_str;
 
2637
        }
 
2638
      }
 
2639
      drizzle_result_free(&fields);
 
2640
    }
 
2641
  }
 
2642
  drizzle_result_free(&tables);
2668
2643
  completion_iter= completion_map.begin();
2669
2644
}
2670
2645
 
2809
2784
com_go(string *buffer, const char *)
2810
2785
{
2811
2786
  char          buff[200]; /* about 110 chars used so far */
 
2787
  char          time_buff[52+3+1]; /* time max + space&parens + NUL */
2812
2788
  drizzle_result_st result;
2813
2789
  drizzle_return_t ret;
2814
 
  uint32_t      warnings= 0;
2815
 
  boost::posix_time::ptime timer;
 
2790
  uint32_t      timer, warnings= 0;
2816
2791
  uint32_t      error= 0;
2817
2792
  uint32_t      error_code= 0;
2818
2793
  int           err= 0;
2881
2856
        goto end;
2882
2857
    }
2883
2858
 
2884
 
    string time_buff("");
2885
2859
    if (verbose >= 3 || !opt_silent)
2886
2860
      drizzle_end_timer(timer,time_buff);
 
2861
    else
 
2862
      time_buff[0]= '\0';
2887
2863
 
2888
2864
    /* Every branch must truncate  buff . */
2889
2865
    if (drizzle_result_column_count(&result) > 0)
2934
2910
      if (warnings != 1)
2935
2911
        *pos++= 's';
2936
2912
    }
2937
 
    strcpy(pos, time_buff.c_str());
 
2913
    strcpy(pos, time_buff);
2938
2914
    put_info(buff,INFO_RESULT,0,0);
2939
2915
    if (strcmp(drizzle_result_info(&result), ""))
2940
2916
      put_info(drizzle_result_info(&result),INFO_RESULT,0,0);
3100
3076
  {
3101
3077
    tee_fprintf(PAGER, _("Field %3u:  `%s`\n"
3102
3078
                "Catalog:    `%s`\n"
3103
 
                "Schema:     `%s`\n"
 
3079
                "Database:   `%s`\n"
3104
3080
                "Table:      `%s`\n"
3105
3081
                "Org_table:  `%s`\n"
3106
3082
                "Type:       UTF-8\n"
3739
3715
  }
3740
3716
  else
3741
3717
    opt_rehash= 0;
3742
 
  error=sql_connect(current_host, current_db, current_user, opt_password);
 
3718
  error=sql_connect(current_host, current_db, current_user, opt_password,0);
3743
3719
  opt_rehash= save_rehash;
3744
3720
 
3745
3721
  if (connected)
3746
3722
  {
3747
3723
    sprintf(buff, _("Connection id:    %u"), drizzle_con_thread_id(&con));
3748
3724
    put_info(buff,INFO_INFO,0,0);
3749
 
    sprintf(buff, _("Current schema: %.128s\n"),
 
3725
    sprintf(buff, _("Current database: %.128s\n"),
3750
3726
            !current_db.empty() ? current_db.c_str() : _("*** NONE ***"));
3751
3727
    put_info(buff,INFO_INFO,0,0);
3752
3728
  }
3859
3835
  tmp= get_arg(buff, 0);
3860
3836
  if (!tmp || !*tmp)
3861
3837
  {
3862
 
    put_info(_("USE must be followed by a schema name"), INFO_ERROR, 0, 0);
 
3838
    put_info(_("USE must be followed by a database name"), INFO_ERROR, 0, 0);
3863
3839
    return 0;
3864
3840
  }
3865
3841
  /*
3927
3903
      build_completion_hash(opt_rehash, 1);
3928
3904
  }
3929
3905
 
3930
 
  put_info(_("Schema changed"),INFO_INFO, 0, 0);
 
3906
  put_info(_("Database changed"),INFO_INFO, 0, 0);
3931
3907
  return 0;
3932
3908
}
3933
3909
 
4048
4024
 
4049
4025
 
4050
4026
static int
4051
 
sql_connect(const string &host, const string &database, const string &user, const string &password)
 
4027
sql_connect(const string &host, const string &database, const string &user, const string &password,
 
4028
                 uint32_t silent)
4052
4029
{
4053
4030
  drizzle_return_t ret;
4054
4031
  if (connected)
4058
4035
    drizzle_free(&drizzle);
4059
4036
  }
4060
4037
  drizzle_create(&drizzle);
4061
 
 
4062
 
#ifdef DRIZZLE_ADMIN_TOOL
4063
 
  drizzle_con_options_t options= (drizzle_con_options_t) (DRIZZLE_CON_ADMIN | (use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL));
4064
 
#else
4065
 
  drizzle_con_options_t options= (drizzle_con_options_t) (use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
4066
 
#endif
4067
 
 
4068
4038
  if (drizzle_con_add_tcp(&drizzle, &con, (char *)host.c_str(),
4069
4039
    opt_drizzle_port, (char *)user.c_str(),
4070
4040
    (char *)password.c_str(), (char *)database.c_str(),
4071
 
    options) == NULL)
 
4041
    use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL) == NULL)
4072
4042
  {
4073
4043
    (void) put_error(&con, NULL);
4074
4044
    (void) fflush(stdout);
4097
4067
*/
4098
4068
  if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
4099
4069
  {
4100
 
 
4101
 
    if (opt_silent < 2)
 
4070
    if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
 
4071
                    ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
4102
4072
    {
4103
4073
      (void) put_error(&con, NULL);
4104
4074
      (void) fflush(stdout);
4143
4113
      drizzle_row_t cur=drizzle_row_next(&result);
4144
4114
      if (cur)
4145
4115
      {
4146
 
        tee_fprintf(stdout, _("Current schema:\t%s\n"), cur[0] ? cur[0] : "");
 
4116
        tee_fprintf(stdout, _("Current database:\t%s\n"), cur[0] ? cur[0] : "");
4147
4117
        tee_fprintf(stdout, _("Current user:\t\t%s\n"), cur[1]);
4148
4118
      }
4149
4119
      drizzle_result_free(&result);
4162
4132
  if (skip_updates)
4163
4133
  {
4164
4134
    vidattr(A_BOLD);
4165
 
    tee_fprintf(stdout, _("\nAll updates ignored to this schema\n"));
 
4135
    tee_fprintf(stdout, _("\nAll updates ignored to this database\n"));
4166
4136
    vidattr(A_NORMAL);
4167
4137
  }
4168
4138
  tee_fprintf(stdout, _("Current pager:\t\t%s\n"), pager.c_str());
4388
4358
}
4389
4359
 
4390
4360
#include <sys/times.h>
 
4361
#ifdef _SC_CLK_TCK        // For mit-pthreads
 
4362
#undef CLOCKS_PER_SEC
 
4363
#define CLOCKS_PER_SEC (sysconf(_SC_CLK_TCK))
 
4364
#endif
4391
4365
 
4392
 
static boost::posix_time::ptime start_timer(void)
 
4366
static uint32_t start_timer(void)
4393
4367
{
4394
 
  return boost::posix_time::microsec_clock::universal_time();
 
4368
  struct tms tms_tmp;
 
4369
  return times(&tms_tmp);
4395
4370
}
4396
4371
 
4397
 
static void nice_time(boost::posix_time::time_duration duration, string &buff)
 
4372
 
 
4373
/**
 
4374
   Write as many as 52+1 bytes to buff, in the form of a legible
 
4375
   duration of time.
 
4376
 
 
4377
   len("4294967296 days, 23 hours, 59 minutes, 60.00 seconds")  ->  52
 
4378
*/
 
4379
static void nice_time(double sec,char *buff,bool part_second)
4398
4380
{
 
4381
  uint32_t tmp;
4399
4382
  ostringstream tmp_buff_str;
4400
4383
 
4401
 
  if (duration.hours() > 0)
4402
 
  {
4403
 
    tmp_buff_str << duration.hours();
4404
 
    if (duration.hours() > 1)
 
4384
  if (sec >= 3600.0*24)
 
4385
  {
 
4386
    tmp=(uint32_t) floor(sec/(3600.0*24));
 
4387
    sec-= 3600.0*24*tmp;
 
4388
    tmp_buff_str << tmp;
 
4389
 
 
4390
    if (tmp > 1)
 
4391
      tmp_buff_str << " days ";
 
4392
    else
 
4393
      tmp_buff_str << " day ";
 
4394
 
 
4395
  }
 
4396
  if (sec >= 3600.0)
 
4397
  {
 
4398
    tmp=(uint32_t) floor(sec/3600.0);
 
4399
    sec-=3600.0*tmp;
 
4400
    tmp_buff_str << tmp;
 
4401
 
 
4402
    if (tmp > 1)
4405
4403
      tmp_buff_str << _(" hours ");
4406
4404
    else
4407
4405
      tmp_buff_str << _(" hour ");
4408
4406
  }
4409
 
  if (duration.hours() > 0 || duration.minutes() > 0)
 
4407
  if (sec >= 60.0)
4410
4408
  {
4411
 
    tmp_buff_str << duration.minutes() << _(" min ");
 
4409
    tmp=(uint32_t) floor(sec/60.0);
 
4410
    sec-=60.0*tmp;
 
4411
    tmp_buff_str << tmp << _(" min ");
4412
4412
  }
4413
 
 
4414
 
  tmp_buff_str.precision(duration.num_fractional_digits());
4415
 
 
4416
 
  double seconds= duration.fractional_seconds();
4417
 
 
4418
 
  seconds/= pow(10.0,duration.num_fractional_digits());
4419
 
 
4420
 
  seconds+= duration.seconds();
4421
 
  tmp_buff_str << seconds << _(" sec");
4422
 
 
4423
 
  buff.append(tmp_buff_str.str());
4424
 
}
4425
 
 
4426
 
static void end_timer(boost::posix_time::ptime start_time, string &buff)
4427
 
{
4428
 
  boost::posix_time::ptime end_time= start_timer();
4429
 
  boost::posix_time::time_period duration(start_time, end_time);
4430
 
 
4431
 
  nice_time(duration.length(), buff);
4432
 
}
4433
 
 
4434
 
 
4435
 
static void drizzle_end_timer(boost::posix_time::ptime start_time, string &buff)
4436
 
{
4437
 
  buff.append(" (");
4438
 
  end_timer(start_time,buff);
4439
 
  buff.append(")");
 
4413
  if (part_second)
 
4414
    tmp_buff_str.precision(2);
 
4415
  else
 
4416
    tmp_buff_str.precision(0);
 
4417
  tmp_buff_str << sec << _(" sec");
 
4418
  strcpy(buff, tmp_buff_str.str().c_str());
 
4419
}
 
4420
 
 
4421
 
 
4422
static void end_timer(uint32_t start_time,char *buff)
 
4423
{
 
4424
  nice_time((double) (start_timer() - start_time) /
 
4425
            CLOCKS_PER_SEC,buff,1);
 
4426
}
 
4427
 
 
4428
 
 
4429
static void drizzle_end_timer(uint32_t start_time,char *buff)
 
4430
{
 
4431
  buff[0]=' ';
 
4432
  buff[1]='(';
 
4433
  end_timer(start_time,buff+2);
 
4434
  strcpy(strchr(buff, '\0'),")");
4440
4435
}
4441
4436
 
4442
4437
static const char * construct_prompt()
4575
4570
        add_int_to_prompt(t->tm_sec);
4576
4571
        break;
4577
4572
      case 'w':
4578
 
        processed_prompt->append(get_day_name(t->tm_wday));
 
4573
        processed_prompt->append(day_names[t->tm_wday]);
4579
4574
        break;
4580
4575
      case 'P':
4581
4576
        processed_prompt->append(t->tm_hour < 12 ? "am" : "pm");
4584
4579
        add_int_to_prompt(t->tm_mon+1);
4585
4580
        break;
4586
4581
      case 'O':
4587
 
        processed_prompt->append(get_month_name(t->tm_mon));
 
4582
        processed_prompt->append(month_names[t->tm_mon]);
4588
4583
        break;
4589
4584
      case '\'':
4590
4585
        processed_prompt->append("'");