~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to client/drizzle.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-04-17 01:38:47 UTC
  • mfrom: (1237.9.238 bad-staging)
  • Revision ID: osullivan.padraig@gmail.com-20100417013847-ibjioqsfbmf5yg4g
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
89
89
#  endif /* !defined(HAVE_READLINE_H) */
90
90
char *cmdline = NULL;
91
91
#else /* !defined(HAVE_READLINE_READLINE_H) */
92
 
  /* no readline */
 
92
/* no readline */
93
93
#  error Readline Required
94
94
#endif /* HAVE_LIBREADLINE */
95
95
 
103
103
extern int write_history ();
104
104
extern int read_history ();
105
105
#  endif /* defined(HAVE_READLINE_HISTORY_H) */
106
 
    /* no history */
 
106
/* no history */
107
107
#endif /* HAVE_READLINE_HISTORY */
108
108
 
109
109
/**
110
 
 Make the old readline interface look like the new one.
 
110
  Make the old readline interface look like the new one.
111
111
*/
112
112
#ifndef HAVE_RL_COMPLETION
113
113
typedef char **rl_completion_func_t(const char *, int, int);
176
176
static drizzle_st drizzle;      /* The library handle */
177
177
static drizzle_con_st con;      /* The connection */
178
178
static bool ignore_errors= false, quick= false,
179
 
  connected= false, opt_raw_data= false, unbuffered= false,
180
 
  output_tables= false, opt_rehash= true, skip_updates= false,
181
 
  safe_updates= false, one_database= false,
182
 
  opt_compress= false, opt_shutdown= false, opt_ping= false,
183
 
  vertical= false, line_numbers= true, column_names= true,
184
 
  opt_nopager= true, opt_outfile= false, named_cmds= false,
185
 
  tty_password= false, opt_nobeep= false, opt_reconnect= true,
186
 
  default_charset_used= false, opt_secure_auth= false,
187
 
  default_pager_set= false, opt_sigint_ignore= false,
188
 
  auto_vertical_output= false,
189
 
  show_warnings= false, executing_query= false, interrupted_query= false,
190
 
  opt_mysql= false;
 
179
            connected= false, opt_raw_data= false, unbuffered= false,
 
180
            output_tables= false, opt_rehash= true, skip_updates= false,
 
181
            safe_updates= false, one_database= false,
 
182
            opt_compress= false, opt_shutdown= false, opt_ping= false,
 
183
            vertical= false, line_numbers= true, column_names= true,
 
184
            opt_nopager= true, opt_outfile= false, named_cmds= false,
 
185
            tty_password= false, opt_nobeep= false, opt_reconnect= true,
 
186
            default_charset_used= false, opt_secure_auth= false,
 
187
            default_pager_set= false, opt_sigint_ignore= false,
 
188
            auto_vertical_output= false,
 
189
            show_warnings= false, executing_query= false, interrupted_query= false,
 
190
            opt_mysql= false;
191
191
static uint32_t  show_progress_size= 0;
192
192
static bool column_types_flag;
193
193
static bool preserve_comments= false;
195
195
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
196
196
static drizzle_capabilities_t connect_flag= DRIZZLE_CAPABILITIES_NONE;
197
197
static char *current_host, *current_db, *current_user= NULL,
198
 
  *opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
 
198
            *opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
199
199
static char *histfile;
200
200
static char *histfile_tmp;
201
201
static string *glob_buffer;
209
209
// TODO: Need to i18n these
210
210
static const char *day_names[]= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
211
211
static const char *month_names[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul",
212
 
                                  "Aug","Sep","Oct","Nov","Dec"};
 
212
  "Aug","Sep","Oct","Nov","Dec"};
213
213
static char default_pager[FN_REFLEN];
214
214
static char pager[FN_REFLEN], outfile[FN_REFLEN];
215
215
static FILE *PAGER, *OUTFILE;
234
234
/* The names of functions that actually do the manipulation. */
235
235
static int get_options(int argc,char **argv);
236
236
static int com_quit(string *str,const char*),
237
 
  com_go(string *str,const char*), com_ego(string *str,const char*),
238
 
  com_print(string *str,const char*),
239
 
  com_help(string *str,const char*), com_clear(string *str,const char*),
240
 
  com_connect(string *str,const char*), com_status(string *str,const char*),
241
 
  com_use(string *str,const char*), com_source(string *str, const char*),
242
 
  com_rehash(string *str, const char*), com_tee(string *str, const char*),
243
 
  com_notee(string *str, const char*),
244
 
  com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
245
 
  com_warnings(string *str, const char*), com_nowarnings(string *str, const char*),
246
 
  com_nopager(string *str, const char*), com_pager(string *str, const char*);
 
237
           com_go(string *str,const char*), com_ego(string *str,const char*),
 
238
           com_print(string *str,const char*),
 
239
           com_help(string *str,const char*), com_clear(string *str,const char*),
 
240
           com_connect(string *str,const char*), com_status(string *str,const char*),
 
241
           com_use(string *str,const char*), com_source(string *str, const char*),
 
242
           com_rehash(string *str, const char*), com_tee(string *str, const char*),
 
243
           com_notee(string *str, const char*),
 
244
           com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
 
245
           com_warnings(string *str, const char*), com_nowarnings(string *str, const char*),
 
246
           com_nopager(string *str, const char*), com_pager(string *str, const char*);
247
247
 
248
248
static int read_and_execute(bool interactive);
249
249
static int sql_connect(char *host,char *database,char *user,char *password,
266
266
static const char * strcont(register const char *str, register const char *set);
267
267
 
268
268
/* A structure which contains information on the commands this program
269
 
   can understand. */
 
269
  can understand. */
270
270
typedef struct {
271
271
  const char *name;        /* User printable name of the function. */
272
272
  char cmd_char;        /* msql command character */
1027
1027
  Shutdown the server that we are currently connected to.
1028
1028
 
1029
1029
  @retval
1030
 
    true success
 
1030
  true success
1031
1031
  @retval
1032
 
    false failure
 
1032
  false failure
1033
1033
*/
1034
1034
static bool server_shutdown(void)
1035
1035
{
1073
1073
  Ping the server that we are currently connected to.
1074
1074
 
1075
1075
  @retval
1076
 
    true success
 
1076
  true success
1077
1077
  @retval
1078
 
    false failure
 
1078
  false failure
1079
1079
*/
1080
1080
static bool server_ping(void)
1081
1081
{
1110
1110
  Execute command(s) specified by the user.
1111
1111
 
1112
1112
  @param error  error status of command execution.
1113
 
                If an error had occurred, this variable will be set
1114
 
                to 1 whereas on success, it shall be set to 0. This
1115
 
                value will be supplied to the exit() function used
1116
 
                by the caller.
 
1113
  If an error had occurred, this variable will be set
 
1114
  to 1 whereas on success, it shall be set to 0. This
 
1115
  value will be supplied to the exit() function used
 
1116
  by the caller.
1117
1117
 
1118
1118
  @retval
1119
 
    false no commands were executed
 
1119
  false no commands were executed
1120
1120
  @retval
1121
 
    true  at least one command was executed
 
1121
  true  at least one command was executed
1122
1122
*/
1123
1123
static bool execute_commands(int *error)
1124
1124
{
1156
1156
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1157
1157
                         getenv("DRIZZLE_PS1") :
1158
1158
                         "drizzle> ");
1159
 
  
 
1159
 
1160
1160
  if (default_prompt == NULL)
1161
1161
  {
1162
1162
    fprintf(stderr, _("Memory allocation error while constructing initial "
1268
1268
  glob_buffer= new string();
1269
1269
  glob_buffer->reserve(512);
1270
1270
 
1271
 
  char * output_buff= (char *)malloc(512);
1272
 
  memset(output_buff, '\0', 512);
1273
 
 
1274
 
  sprintf(output_buff,
1275
 
          _("Your Drizzle connection id is %u\nServer version: %s\n"),
1276
 
          drizzle_con_thread_id(&con),
1277
 
          server_version_string(&con));
1278
 
  put_info(output_buff, INFO_INFO, 0, 0);
 
1271
  ostringstream output_buff;
 
1272
  output_buff << _("Your Drizzle connection id is ");
 
1273
  output_buff << drizzle_con_thread_id(&con);
 
1274
  output_buff << "\n";
 
1275
  output_buff << _("Server version: ");
 
1276
  output_buff << server_version_string(&con) << "\n";
 
1277
  put_info(output_buff.str().c_str(), INFO_INFO, 0, 0);
1279
1278
 
1280
1279
  initialize_readline(current_prompt);
1281
1280
  if (!status.batch && !quick)
1285
1284
      histfile= strdup(getenv("DRIZZLE_HISTFILE"));
1286
1285
    else if (getenv("HOME"))
1287
1286
    {
1288
 
      histfile=(char*) malloc(strlen(getenv("HOME")) + strlen("/.drizzle_history") + 2);
 
1287
      size_t histfile_size = strlen(getenv("HOME")) +
 
1288
        strlen("/.drizzle_history") + 2;
 
1289
      histfile=(char*) malloc(histfile_size);
1289
1290
      if (histfile)
1290
 
        sprintf(histfile,"%s/.drizzle_history",getenv("HOME"));
 
1291
        snprintf(histfile, histfile_size, "%s/.drizzle_history",getenv("HOME"));
1291
1292
      char link_name[FN_REFLEN];
1292
1293
      ssize_t sym_link_size= readlink(histfile,link_name,FN_REFLEN-1);
1293
1294
      if (sym_link_size >= 0)
1306
1307
      if (verbose)
1307
1308
        tee_fprintf(stdout, _("Reading history-file %s\n"),histfile);
1308
1309
      read_history(histfile);
1309
 
      if (!(histfile_tmp= (char*) malloc((uint32_t) strlen(histfile) + 5)))
 
1310
      size_t histfile_tmp_size = strlen(histfile) + 5;
 
1311
      if (!(histfile_tmp= (char*) malloc(histfile_tmp_size)))
1310
1312
      {
1311
1313
        fprintf(stderr, _("Couldn't allocate memory for temp histfile!\n"));
1312
1314
        exit(1);
1313
1315
      }
1314
 
      sprintf(histfile_tmp, "%s.TMP", histfile);
 
1316
      snprintf(histfile_tmp, histfile_tmp_size, "%s.TMP", histfile);
1315
1317
    }
1316
1318
  }
1317
1319
 
1388
1390
  }
1389
1391
 
1390
1392
  /* kill_buffer is always big enough because max length of %lu is 15 */
1391
 
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1392
 
          drizzle_con_thread_id(&con));
 
1393
  snprintf(kill_buffer, sizeof(kill_buffer), "KILL /*!50000 QUERY */ %u",
 
1394
           drizzle_con_thread_id(&con));
1393
1395
 
1394
1396
  if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
1395
1397
    drizzle_result_free(&res);
1416
1418
}
1417
1419
#endif
1418
1420
 
1419
 
static struct my_option my_long_options[] =
 
1421
static struct option my_long_options[] =
1420
1422
{
1421
1423
  {"help", '?', N_("Display this help and exit."), 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
1422
 
   0, 0, 0, 0, 0},
 
1424
    0, 0, 0, 0, 0},
1423
1425
  {"help", 'I', N_("Synonym for -?"), 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
1424
 
   0, 0, 0, 0, 0},
 
1426
    0, 0, 0, 0, 0},
1425
1427
  {"auto-rehash", OPT_AUTO_REHASH,
1426
 
   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."),
1427
 
   (char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
1428
 
   0, 0},
 
1428
    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."),
 
1429
    (char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
 
1430
    0, 0},
1429
1431
  {"no-auto-rehash", 'A',
1430
 
   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."),
1431
 
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1432
    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."),
 
1433
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1432
1434
  {"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT,
1433
 
   N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
1434
 
   (char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1435
    N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
 
1436
    (char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1435
1437
  {"batch", 'B',
1436
 
   N_("Don't use history file. Disable interactive behavior. (Enables --silent)"), 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1438
    N_("Don't use history file. Disable interactive behavior. (Enables --silent)"), 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1437
1439
  {"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
1438
 
   (char**) &column_types_flag, (char**) &column_types_flag,
1439
 
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1440
    (char**) &column_types_flag, (char**) &column_types_flag,
 
1441
    0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1440
1442
  {"comments", 'c', N_("Preserve comments. Send comments to the server. The default is --skip-comments (discard comments), enable with --comments"),
1441
 
   (char**) &preserve_comments, (char**) &preserve_comments,
1442
 
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1443
    (char**) &preserve_comments, (char**) &preserve_comments,
 
1444
    0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1443
1445
  {"compress", 'C', N_("Use compression in server/client protocol."),
1444
 
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
1445
 
   0, 0, 0},
 
1446
    (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
1447
    0, 0, 0},
1446
1448
  {"database", 'D', N_("Database to use."), (char**) &current_db,
1447
 
   (char**) &current_db, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1449
    (char**) &current_db, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1448
1450
  {"default-character-set", OPT_DEFAULT_CHARSET,
1449
 
   N_("(not used)"), 0,
1450
 
   0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1451
    N_("(not used)"), 0,
 
1452
    0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1451
1453
  {"delimiter", OPT_DELIMITER, N_("Delimiter to be used."), (char**) &delimiter_str,
1452
 
   (char**) &delimiter_str, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1454
    (char**) &delimiter_str, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1453
1455
  {"execute", 'e', N_("Execute command and quit. (Disables --force and history file)"), 0,
1454
 
   0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1456
    0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1455
1457
  {"vertical", 'E', N_("Print the output of a query (rows) vertically."),
1456
 
   (char**) &vertical, (char**) &vertical, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
1457
 
   0},
 
1458
    (char**) &vertical, (char**) &vertical, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
 
1459
    0},
1458
1460
  {"force", 'f', N_("Continue even if we get an sql error."),
1459
 
   (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
1460
 
   0, 0, 0, 0},
 
1461
    (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
 
1462
    0, 0, 0, 0},
1461
1463
  {"named-commands", 'G',
1462
 
   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."),
1463
 
   (char**) &named_cmds, (char**) &named_cmds, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
1464
 
   0, 0},
 
1464
    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."),
 
1465
    (char**) &named_cmds, (char**) &named_cmds, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
1466
    0, 0},
1465
1467
  {"no-named-commands", 'g',
1466
 
   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."),
1467
 
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1468
    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."),
 
1469
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1468
1470
  {"ignore-spaces", 'i', N_("Ignore space after function names."), 0, 0, 0,
1469
 
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1471
    GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1470
1472
  {"local-infile", OPT_LOCAL_INFILE, N_("Enable/disable LOAD DATA LOCAL INFILE."),
1471
 
   (char**) &opt_local_infile,
1472
 
   (char**) &opt_local_infile, 0, GET_BOOL, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
1473
    (char**) &opt_local_infile,
 
1474
    (char**) &opt_local_infile, 0, GET_BOOL, OPT_ARG, 0, 0, 0, 0, 0, 0},
1473
1475
  {"no-beep", 'b', N_("Turn off beep on error."), (char**) &opt_nobeep,
1474
 
   (char**) &opt_nobeep, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1476
    (char**) &opt_nobeep, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1475
1477
  {"host", 'h', N_("Connect to host."), (char**) &current_host,
1476
 
   (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1478
    (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1477
1479
  {"line-numbers", OPT_LINE_NUMBERS, N_("Write line numbers for errors."),
1478
 
   (char**) &line_numbers, (char**) &line_numbers, 0, GET_BOOL,
1479
 
   NO_ARG, 1, 0, 0, 0, 0, 0},
 
1480
    (char**) &line_numbers, (char**) &line_numbers, 0, GET_BOOL,
 
1481
    NO_ARG, 1, 0, 0, 0, 0, 0},
1480
1482
  {"skip-line-numbers", 'L', N_("Don't write line number for errors. WARNING: -L is deprecated, use long version of this option instead."), 0, 0, 0, GET_NO_ARG,
1481
 
   NO_ARG, 0, 0, 0, 0, 0, 0},
 
1483
    NO_ARG, 0, 0, 0, 0, 0, 0},
1482
1484
  {"unbuffered", 'n', N_("Flush buffer after each query."), (char**) &unbuffered,
1483
 
   (char**) &unbuffered, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1485
    (char**) &unbuffered, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1484
1486
  {"column-names", OPT_COLUMN_NAMES, N_("Write column names in results."),
1485
 
   (char**) &column_names, (char**) &column_names, 0, GET_BOOL,
1486
 
   NO_ARG, 1, 0, 0, 0, 0, 0},
 
1487
    (char**) &column_names, (char**) &column_names, 0, GET_BOOL,
 
1488
    NO_ARG, 1, 0, 0, 0, 0, 0},
1487
1489
  {"skip-column-names", 'N',
1488
 
   N_("Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead."),
1489
 
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1490
    N_("Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead."),
 
1491
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1490
1492
  {"set-variable", 'O',
1491
 
   N_("Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value."),
1492
 
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1493
    N_("Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value."),
 
1494
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1493
1495
  {"sigint-ignore", OPT_SIGINT_IGNORE, N_("Ignore SIGINT (CTRL-C)"),
1494
 
   (char**) &opt_sigint_ignore,  (char**) &opt_sigint_ignore, 0, GET_BOOL,
1495
 
   NO_ARG, 0, 0, 0, 0, 0, 0},
 
1496
    (char**) &opt_sigint_ignore,  (char**) &opt_sigint_ignore, 0, GET_BOOL,
 
1497
    NO_ARG, 0, 0, 0, 0, 0, 0},
1496
1498
  {"one-database", 'o',
1497
 
   N_("Only update the default database. This is useful for skipping updates to other database in the update log."),
1498
 
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1499
    N_("Only update the default database. This is useful for skipping updates to other database in the update log."),
 
1500
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1499
1501
  {"pager", OPT_PAGER,
1500
 
   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."),
1501
 
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
1502
    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."),
 
1503
    0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1502
1504
  {"no-pager", OPT_NOPAGER,
1503
 
   N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
1504
 
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1505
    N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
 
1506
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1505
1507
  {"password", 'P',
1506
 
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1507
 
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
1508
    N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
 
1509
    0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1508
1510
  {"port", 'p', N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, ")
1509
 
   N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
1510
 
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1511
    N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
 
1512
    0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1511
1513
  {"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
1512
 
   (char**) &current_prompt, (char**) &current_prompt, 0, GET_STR_ALLOC,
1513
 
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1514
    (char**) &current_prompt, (char**) &current_prompt, 0, GET_STR_ALLOC,
 
1515
    REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1514
1516
  {"quick", 'q',
1515
 
   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."),
1516
 
   (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1517
    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."),
 
1518
    (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1517
1519
  {"raw", 'r', N_("Write fields without conversion. Used with --batch."),
1518
 
   (char**) &opt_raw_data, (char**) &opt_raw_data, 0, GET_BOOL, NO_ARG, 0, 0, 0,
1519
 
   0, 0, 0},
 
1520
    (char**) &opt_raw_data, (char**) &opt_raw_data, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
1521
    0, 0, 0},
1520
1522
  {"reconnect", OPT_RECONNECT, N_("Reconnect if the connection is lost. Disable with --disable-reconnect. This option is enabled by default."),
1521
 
   (char**) &opt_reconnect, (char**) &opt_reconnect, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
1523
    (char**) &opt_reconnect, (char**) &opt_reconnect, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
1522
1524
  {"shutdown", OPT_SHUTDOWN, N_("Shutdown the server."),
1523
 
   (char**) &opt_shutdown, (char**) &opt_shutdown, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1525
    (char**) &opt_shutdown, (char**) &opt_shutdown, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1524
1526
  {"silent", 's', N_("Be more silent. Print results with a tab as separator, each row on new line."), 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0,
1525
 
   0, 0},
 
1527
    0, 0},
1526
1528
  {"table", 't', N_("Output in table format."), (char**) &output_tables,
1527
 
   (char**) &output_tables, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1529
    (char**) &output_tables, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1528
1530
  {"tee", OPT_TEE,
1529
 
   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."),
1530
 
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1531
    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."),
 
1532
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1531
1533
  {"no-tee", OPT_NOTEE, N_("Disable outfile. See interactive help (\\h) also. WARNING: option deprecated; use --disable-tee instead"), 0, 0, 0, GET_NO_ARG,
1532
 
   NO_ARG, 0, 0, 0, 0, 0, 0},
 
1534
    NO_ARG, 0, 0, 0, 0, 0, 0},
1533
1535
  {"user", 'u', N_("User for login if not current user."), (char**) &current_user,
1534
 
   (char**) &current_user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1536
    (char**) &current_user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1535
1537
  {"safe-updates", 'U', N_("Only allow UPDATE and DELETE that uses keys."),
1536
 
   (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
1537
 
   0, 0, 0, 0},
 
1538
    (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
 
1539
    0, 0, 0, 0},
1538
1540
  {"i-am-a-dummy", 'U', N_("Synonym for option --safe-updates, -U."),
1539
 
   (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
1540
 
   0, 0, 0, 0},
 
1541
    (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
 
1542
    0, 0, 0, 0},
1541
1543
  {"verbose", 'v', N_("Write more. (-v -v -v gives the table output format)."), 0,
1542
 
   0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1544
    0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1543
1545
  {"version", 'V', N_("Output version information and exit."), 0, 0, 0,
1544
 
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1546
    GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1545
1547
  {"wait", 'w', N_("Wait and retry if connection is down."), 0, 0, 0, GET_NO_ARG,
1546
 
   NO_ARG, 0, 0, 0, 0, 0, 0},
 
1548
    NO_ARG, 0, 0, 0, 0, 0, 0},
1547
1549
  {"connect_timeout", OPT_CONNECT_TIMEOUT,
1548
 
   N_("Number of seconds before connection timeout."),
1549
 
   (char**) &opt_connect_timeout,
1550
 
   (char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 0, 0, 3600*12, 0,
1551
 
   0, 0},
 
1550
    N_("Number of seconds before connection timeout."),
 
1551
    (char**) &opt_connect_timeout,
 
1552
    (char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 0, 0, 3600*12, 0,
 
1553
    0, 0},
1552
1554
  {"max_input_line", OPT_MAX_INPUT_LINE,
1553
 
   N_("Max length of input line"),
1554
 
   (char**) &opt_max_input_line, (char**) &opt_max_input_line, 0,
1555
 
   GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1556
 
   (int64_t) 2*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
 
1555
    N_("Max length of input line"),
 
1556
    (char**) &opt_max_input_line, (char**) &opt_max_input_line, 0,
 
1557
    GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
 
1558
    (int64_t) 2*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
1557
1559
  {"select_limit", OPT_SELECT_LIMIT,
1558
 
   N_("Automatic limit for SELECT when using --safe-updates"),
1559
 
   (char**) &select_limit,
1560
 
   (char**) &select_limit, 0, GET_UINT32, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1561
 
   0, 1, 0},
 
1560
    N_("Automatic limit for SELECT when using --safe-updates"),
 
1561
    (char**) &select_limit,
 
1562
    (char**) &select_limit, 0, GET_UINT32, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
 
1563
    0, 1, 0},
1562
1564
  {"max_join_size", OPT_MAX_JOIN_SIZE,
1563
 
   N_("Automatic limit for rows in a join when using --safe-updates"),
1564
 
   (char**) &max_join_size,
1565
 
   (char**) &max_join_size, 0, GET_UINT32, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1566
 
   0, 1, 0},
 
1565
    N_("Automatic limit for rows in a join when using --safe-updates"),
 
1566
    (char**) &max_join_size,
 
1567
    (char**) &max_join_size, 0, GET_UINT32, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
 
1568
    0, 1, 0},
1567
1569
  {"secure-auth", OPT_SECURE_AUTH, N_("Refuse client connecting to server if it uses old (pre-4.1.1) protocol"), (char**) &opt_secure_auth,
1568
 
   (char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1570
    (char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1569
1571
  {"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1570
 
   (char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1571
 
   0, 0, 0, 0, 0, 0},
 
1572
    (char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
 
1573
    0, 0, 0, 0, 0, 0},
1572
1574
  {"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1573
 
   (char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
1574
 
   0, 0, 0, 0, 0, 0},
 
1575
    (char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
 
1576
    0, 0, 0, 0, 0, 0},
1575
1577
  {"ping", OPT_PING, N_("Ping the server to check if it's alive."),
1576
 
   (char**) &opt_ping, (char**) &opt_ping, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1578
    (char**) &opt_ping, (char**) &opt_ping, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1577
1579
  {"mysql", 'm', N_("Use MySQL Protocol."),
1578
 
   (char**) &opt_mysql, (char**) &opt_mysql, 0, GET_BOOL, NO_ARG, 1, 0, 0,
1579
 
   0, 0, 0},
 
1580
    (char**) &opt_mysql, (char**) &opt_mysql, 0, GET_BOOL, NO_ARG, 1, 0, 0,
 
1581
    0, 0, 0},
1580
1582
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1581
1583
};
1582
1584
 
1604
1606
}
1605
1607
 
1606
1608
 
1607
 
static bool get_one_option(int optid, const struct my_option *, char *argument)
 
1609
static int get_one_option(int optid, const struct option *, char *argument)
1608
1610
{
1609
1611
  char *endchar= NULL;
1610
1612
  uint64_t temp_drizzle_port= 0;
1629
1631
      {
1630
1632
        put_info(_("DELIMITER cannot contain a backslash character"),
1631
1633
                 INFO_ERROR,0,0);
1632
 
        return false;
 
1634
        return EXIT_ARGUMENT_INVALID;
1633
1635
      }
1634
1636
    }
1635
1637
    delimiter_length= (uint32_t)strlen(delimiter);
1704
1706
    if (strlen(endchar) != 0)
1705
1707
    {
1706
1708
      put_info(_("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead."), INFO_ERROR, 0, 0);
1707
 
      return false;
 
1709
      return EXIT_ARGUMENT_INVALID;
1708
1710
    }
1709
1711
    /* If the port number is > 65535 it is not a valid port
1710
 
       This also helps with potential data loss casting unsigned long to a
1711
 
       uint32_t. */
 
1712
      This also helps with potential data loss casting unsigned long to a
 
1713
      uint32_t. */
1712
1714
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
1713
1715
    {
1714
1716
      put_info(_("Value supplied for port is not valid."), INFO_ERROR, 0, 0);
1715
 
      return false;
 
1717
      return EXIT_ARGUMENT_INVALID;
1716
1718
    }
1717
1719
    else
1718
1720
    {
1969
1971
 
1970
1972
 
1971
1973
static bool add_line(string *buffer, char *line, char *in_string,
1972
 
                        bool *ml_comment)
 
1974
                     bool *ml_comment)
1973
1975
{
1974
1976
  unsigned char inchar;
1975
1977
  char *pos, *out;
2238
2240
}
2239
2241
 
2240
2242
/*****************************************************************
2241
 
            Interface to Readline Completion
2242
 
******************************************************************/
 
2243
  Interface to Readline Completion
 
2244
 ******************************************************************/
2243
2245
 
2244
2246
 
2245
2247
static char **mysql_completion (const char *text, int start, int end);
2477
2479
      if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
2478
2480
      {
2479
2481
        tee_fprintf(stdout, _("\
2480
 
Reading table information for completion of table and column names\n    \
2481
 
You can turn off this feature to get a quicker startup with -A\n\n"));
 
2482
                              Reading table information for completion of table and column names\n    \
 
2483
                              You can turn off this feature to get a quicker startup with -A\n\n"));
2482
2484
      }
2483
2485
      while ((table_row=drizzle_row_next(&tables)))
2484
2486
      {
2507
2509
    query.append("show fields in '");
2508
2510
    query.append(table_row[0]);
2509
2511
    query.append("'");
2510
 
    
 
2512
 
2511
2513
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2512
2514
                      &ret) != NULL)
2513
2515
    {
2573
2575
}
2574
2576
 
2575
2577
/***************************************************************************
2576
 
 The different commands
2577
 
***************************************************************************/
 
2578
  The different commands
 
2579
 ***************************************************************************/
2578
2580
 
2579
2581
int drizzleclient_real_query_for_lazy(const char *buf, int length,
2580
2582
                                      drizzle_result_st *result,
2615
2617
    return 0;
2616
2618
 
2617
2619
  if (drizzle_con_error(&con)[0])
2618
 
    return put_error(&con, result);
 
2620
  {
 
2621
    int ret = put_error(&con, result);
 
2622
    drizzle_result_free(result);
 
2623
    return ret;
 
2624
  }
2619
2625
  return 0;
2620
2626
}
2621
2627
 
2656
2662
 
2657
2663
/*
2658
2664
  Execute command
2659
 
  Returns: 0  if ok
2660
 
  -1 if not fatal error
2661
 
  1  if fatal error
 
2665
Returns: 0  if ok
 
2666
-1 if not fatal error
 
2667
1  if fatal error
2662
2668
*/
2663
2669
static int
2664
2670
com_go(string *buffer, const char *)
2759
2765
          print_tab_data(&result);
2760
2766
        else
2761
2767
          print_table_data(&result);
2762
 
        sprintf(buff,
2763
 
                ngettext("%ld row in set","%ld rows in set",
2764
 
                         (long) drizzle_result_row_count(&result)),
2765
 
                (long) drizzle_result_row_count(&result));
 
2768
        snprintf(buff, sizeof(buff), 
 
2769
                 ngettext("%ld row in set","%ld rows in set",
 
2770
                          (long) drizzle_result_row_count(&result)),
 
2771
                 (long) drizzle_result_row_count(&result));
2766
2772
        end_pager();
2767
2773
        if (drizzle_result_error_code(&result))
2768
2774
          error= put_error(&con, &result);
2771
2777
    else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2772
2778
      strcpy(buff,_("Query OK"));
2773
2779
    else
2774
 
      sprintf(buff, ngettext("Query OK, %ld row affected",
2775
 
                             "Query OK, %ld rows affected",
2776
 
                             (long) drizzle_result_affected_rows(&result)),
2777
 
              (long) drizzle_result_affected_rows(&result));
 
2780
      snprintf(buff, sizeof(buff), ngettext("Query OK, %ld row affected",
 
2781
                                            "Query OK, %ld rows affected",
 
2782
                                            (long) drizzle_result_affected_rows(&result)),
 
2783
               (long) drizzle_result_affected_rows(&result));
2778
2784
 
2779
2785
    pos= strchr(buff, '\0');
2780
2786
    if ((warnings= drizzle_result_warning_count(&result)))
2783
2789
      *pos++= ' ';
2784
2790
      char warnings_buff[20];
2785
2791
      memset(warnings_buff,0,20);
2786
 
      sprintf(warnings_buff, "%d", warnings);
 
2792
      snprintf(warnings_buff, sizeof(warnings_buff), "%d", warnings);
2787
2793
      strcpy(pos, warnings_buff);
2788
2794
      pos+= strlen(warnings_buff);
2789
2795
      pos= strcpy(pos, " warning")+8;
2901
2907
static const char *fieldtype2str(drizzle_column_type_t type)
2902
2908
{
2903
2909
  switch (type) {
2904
 
    case DRIZZLE_COLUMN_TYPE_BLOB:        return "BLOB";
2905
 
    case DRIZZLE_COLUMN_TYPE_DATE:        return "DATE";
2906
 
    case DRIZZLE_COLUMN_TYPE_DATETIME:    return "DATETIME";
2907
 
    case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:  return "DECIMAL";
2908
 
    case DRIZZLE_COLUMN_TYPE_DOUBLE:      return "DOUBLE";
2909
 
    case DRIZZLE_COLUMN_TYPE_ENUM:        return "ENUM";
2910
 
    case DRIZZLE_COLUMN_TYPE_LONG:        return "LONG";
2911
 
    case DRIZZLE_COLUMN_TYPE_LONGLONG:    return "LONGLONG";
2912
 
    case DRIZZLE_COLUMN_TYPE_NULL:        return "NULL";
2913
 
    case DRIZZLE_COLUMN_TYPE_TIMESTAMP:   return "TIMESTAMP";
2914
 
    default:                     return "?-unknown-?";
 
2910
  case DRIZZLE_COLUMN_TYPE_BLOB:        return "BLOB";
 
2911
  case DRIZZLE_COLUMN_TYPE_DATE:        return "DATE";
 
2912
  case DRIZZLE_COLUMN_TYPE_DATETIME:    return "DATETIME";
 
2913
  case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:  return "DECIMAL";
 
2914
  case DRIZZLE_COLUMN_TYPE_DOUBLE:      return "DOUBLE";
 
2915
  case DRIZZLE_COLUMN_TYPE_ENUM:        return "ENUM";
 
2916
  case DRIZZLE_COLUMN_TYPE_LONG:        return "LONG";
 
2917
  case DRIZZLE_COLUMN_TYPE_LONGLONG:    return "LONGLONG";
 
2918
  case DRIZZLE_COLUMN_TYPE_NULL:        return "NULL";
 
2919
  case DRIZZLE_COLUMN_TYPE_TIMESTAMP:   return "TIMESTAMP";
 
2920
  default:                     return "?-unknown-?";
2915
2921
  }
2916
2922
}
2917
2923
 
2921
2927
  *s=0;
2922
2928
#define ff2s_check_flag(X)                                              \
2923
2929
  if (f & DRIZZLE_COLUMN_FLAGS_ ## X) { s=strcpy(s, # X " ")+strlen(# X " "); \
2924
 
                        f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
 
2930
    f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
2925
2931
  ff2s_check_flag(NOT_NULL);
2926
2932
  ff2s_check_flag(PRI_KEY);
2927
2933
  ff2s_check_flag(UNIQUE_KEY);
2942
2948
  ff2s_check_flag(ON_UPDATE_NOW);
2943
2949
#undef ff2s_check_flag
2944
2950
  if (f)
2945
 
    sprintf(s, " unknows=0x%04x", f);
 
2951
    snprintf(s, sizeof(buf), " unknows=0x%04x", f);
2946
2952
  return buf;
2947
2953
}
2948
2954
 
3008
3014
      uint32_t name_length= strlen(drizzle_column_name(field));
3009
3015
 
3010
3016
      /* Check if the max_byte value is really the maximum in terms
3011
 
         of visual length since multibyte characters can affect the
3012
 
         length of the separator. */
 
3017
        of visual length since multibyte characters can affect the
 
3018
        length of the separator. */
3013
3019
      length= charset_info->cset->numcells(charset_info,
3014
3020
                                           drizzle_column_name(field),
3015
3021
                                           drizzle_column_name(field) +
3025
3031
        length= name_length;
3026
3032
      }
3027
3033
    }
3028
 
  
 
3034
 
3029
3035
    if (quick)
3030
3036
      length=max(length,drizzle_column_size(field));
3031
3037
    else
3052
3058
    {
3053
3059
      uint32_t name_length= (uint32_t) strlen(drizzle_column_name(field));
3054
3060
      uint32_t numcells= charset_info->cset->numcells(charset_info,
3055
 
                                                  drizzle_column_name(field),
3056
 
                                                  drizzle_column_name(field) +
3057
 
                                                  name_length);
 
3061
                                                      drizzle_column_name(field),
 
3062
                                                      drizzle_column_name(field) +
 
3063
                                                      name_length);
3058
3064
      uint32_t display_length= drizzle_column_max_size(field) + name_length -
3059
 
                               numcells;
 
3065
        numcells;
3060
3066
      tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
3061
3067
                                             MAX_COLUMN_LENGTH),
3062
3068
                  drizzle_column_name(field));
3141
3147
}
3142
3148
 
3143
3149
/**
3144
 
   Return the length of a field after it would be rendered into text.
3145
 
 
3146
 
   This doesn't know or care about multibyte characters.  Assume we're
3147
 
   using such a charset.  We can't know that all of the upcoming rows
3148
 
   for this column will have bytes that each render into some fraction
3149
 
   of a character.  It's at least possible that a row has bytes that
3150
 
   all render into one character each, and so the maximum length is
3151
 
   still the number of bytes.  (Assumption 1:  This can't be better
3152
 
   because we can never know the number of characters that the DB is
3153
 
   going to send -- only the number of bytes.  2: Chars <= Bytes.)
3154
 
 
3155
 
   @param  field  Pointer to a field to be inspected
3156
 
 
3157
 
   @returns  number of character positions to be used, at most
 
3150
  Return the length of a field after it would be rendered into text.
 
3151
 
 
3152
  This doesn't know or care about multibyte characters.  Assume we're
 
3153
  using such a charset.  We can't know that all of the upcoming rows
 
3154
  for this column will have bytes that each render into some fraction
 
3155
  of a character.  It's at least possible that a row has bytes that
 
3156
  all render into one character each, and so the maximum length is
 
3157
  still the number of bytes.  (Assumption 1:  This can't be better
 
3158
  because we can never know the number of characters that the DB is
 
3159
  going to send -- only the number of bytes.  2: Chars <= Bytes.)
 
3160
 
 
3161
  @param  field  Pointer to a field to be inspected
 
3162
 
 
3163
  @returns  number of character positions to be used, at most
3158
3164
*/
3159
3165
static int get_field_disp_length(drizzle_column_st *field)
3160
3166
{
3166
3172
    length= max(length, (uint32_t)drizzle_column_max_size(field));
3167
3173
 
3168
3174
  if (length < 4 &&
3169
 
    !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
 
3175
      !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
3170
3176
  {
3171
3177
    length= 4;        /* Room for "NULL" */
3172
3178
  }
3175
3181
}
3176
3182
 
3177
3183
/**
3178
 
   For a new result, return the max number of characters that any
3179
 
   upcoming row may return.
3180
 
 
3181
 
   @param  result  Pointer to the result to judge
3182
 
 
3183
 
   @returns  The max number of characters in any row of this result
 
3184
  For a new result, return the max number of characters that any
 
3185
  upcoming row may return.
 
3186
 
 
3187
  @param  result  Pointer to the result to judge
 
3188
 
 
3189
  @returns  The max number of characters in any row of this result
3184
3190
*/
3185
3191
static int get_result_width(drizzle_result_st *result)
3186
3192
{
3304
3310
    warning.
3305
3311
  */
3306
3312
  if (!cur || (num_rows == 1 &&
3307
 
      error_code == (uint32_t) strtoul(cur[1], NULL, 10)))
 
3313
               error_code == (uint32_t) strtoul(cur[1], NULL, 10)))
3308
3314
  {
3309
3315
    goto end;
3310
3316
  }
3599
3605
 
3600
3606
  if (connected)
3601
3607
  {
3602
 
    sprintf(buff,"Connection id:    %u",drizzle_con_thread_id(&con));
 
3608
    snprintf(buff, sizeof(buff), "Connection id:    %u",drizzle_con_thread_id(&con));
3603
3609
    put_info(buff,INFO_INFO,0,0);
3604
 
    sprintf(buff,"Current database: %.128s\n",
3605
 
            current_db ? current_db : "*** NONE ***");
 
3610
    snprintf(buff, sizeof(buff), "Current database: %.128s\n",
 
3611
             current_db ? current_db : "*** NONE ***");
3606
3612
    put_info(buff,INFO_INFO,0,0);
3607
3613
  }
3608
3614
  return error;
3637
3643
  if (!(sql_file = fopen(source_name, "r")))
3638
3644
  {
3639
3645
    char buff[FN_REFLEN+60];
3640
 
    sprintf(buff,"Failed to open file '%s', error: %d", source_name,errno);
 
3646
    snprintf(buff, sizeof(buff), "Failed to open file '%s', error: %d", source_name,errno);
3641
3647
    return put_info(buff, INFO_ERROR, 0 ,0);
3642
3648
  }
3643
3649
 
3865
3871
 
3866
3872
static int
3867
3873
sql_connect(char *host,char *database,char *user,char *password,
3868
 
                 uint32_t silent)
 
3874
            uint32_t silent)
3869
3875
{
3870
3876
  drizzle_return_t ret;
3871
3877
 
3884
3890
    return 1;
3885
3891
  }
3886
3892
 
3887
 
/* XXX add this back in
3888
 
  if (opt_connect_timeout)
3889
 
  {
 
3893
  /* XXX add this back in
 
3894
    if (opt_connect_timeout)
 
3895
    {
3890
3896
    uint32_t timeout=opt_connect_timeout;
3891
3897
    drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3892
 
                  (char*) &timeout);
3893
 
  }
3894
 
*/
 
3898
    (char*) &timeout);
 
3899
    }
 
3900
  */
3895
3901
 
3896
 
/* XXX Do we need this?
3897
 
  if (safe_updates)
3898
 
  {
 
3902
  /* XXX Do we need this?
 
3903
    if (safe_updates)
 
3904
    {
3899
3905
    char init_command[100];
3900
3906
    sprintf(init_command,
3901
 
            "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3902
 
            ",MAX_JOIN_SIZE=%"PRIu32,
3903
 
            select_limit, max_join_size);
 
3907
    "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
 
3908
    ",MAX_JOIN_SIZE=%"PRIu32,
 
3909
    select_limit, max_join_size);
3904
3910
    drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3905
 
  }
3906
 
*/
 
3911
    }
 
3912
  */
3907
3913
  if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
3908
3914
  {
3909
3915
    if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
3925
3931
static int
3926
3932
com_status(string *, const char *)
3927
3933
{
3928
 
/*
3929
 
  char buff[40];
3930
 
  uint64_t id;
3931
 
*/
 
3934
  /*
 
3935
    char buff[40];
 
3936
    uint64_t id;
 
3937
  */
3932
3938
  drizzle_result_st result;
3933
3939
  drizzle_return_t ret;
3934
3940
 
3976
3982
  tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&con));
3977
3983
  tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_con_protocol_version(&con));
3978
3984
  tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_con_host(&con));
3979
 
/* XXX need to save this from result
3980
 
  if ((id= drizzleclient_insert_id(&drizzle)))
 
3985
  /* XXX need to save this from result
 
3986
    if ((id= drizzleclient_insert_id(&drizzle)))
3981
3987
    tee_fprintf(stdout, "Insert id:\t\t%s\n", internal::llstr(id, buff));
3982
 
*/
 
3988
  */
3983
3989
 
3984
 
  if (strcmp(drizzle_con_uds(&con), ""))
 
3990
  if (drizzle_con_uds(&con))
3985
3991
    tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle_con_uds(&con));
3986
3992
  else
3987
3993
    tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle_con_port(&con));
3992
3998
    tee_fprintf(stdout, "\nNote that you are running in safe_update_mode:\n");
3993
3999
    vidattr(A_NORMAL);
3994
4000
    tee_fprintf(stdout, "\
3995
 
UPDATEs and DELETEs that don't use a key in the WHERE clause are not allowed.\n\
3996
 
(One can force an UPDATE/DELETE by adding LIMIT # at the end of the command.)\n \
3997
 
SELECT has an automatic 'LIMIT %lu' if LIMIT is not used.\n             \
3998
 
Max number of examined row combination in a join is set to: %lu\n\n",
 
4001
                UPDATEs and DELETEs that don't use a key in the WHERE clause are not allowed.\n\
 
4002
                (One can force an UPDATE/DELETE by adding LIMIT # at the end of the command.)\n \
 
4003
                SELECT has an automatic 'LIMIT %lu' if LIMIT is not used.\n             \
 
4004
                Max number of examined row combination in a join is set to: %lu\n\n",
3999
4005
                select_limit, max_join_size);
4000
4006
  }
4001
4007
  tee_puts("--------------\n", stdout);
4130
4136
 
4131
4137
  return put_info(error, INFO_ERROR,
4132
4138
                  res == NULL ? drizzle_con_error_code(local_con) :
4133
 
                                drizzle_result_error_code(res),
 
4139
                  drizzle_result_error_code(res),
4134
4140
                  res == NULL ? drizzle_con_sqlstate(local_con) :
4135
 
                                drizzle_result_sqlstate(res));
 
4141
                  drizzle_result_sqlstate(res));
4136
4142
}
4137
4143
 
4138
4144
 
4205
4211
 
4206
4212
 
4207
4213
/**
4208
 
   Write as many as 52+1 bytes to buff, in the form of a legible
4209
 
   duration of time.
 
4214
  Write as many as 52+1 bytes to buff, in the form of a legible
 
4215
  duration of time.
4210
4216
 
4211
 
   len("4294967296 days, 23 hours, 59 minutes, 60.00 seconds")  ->  52
 
4217
  len("4294967296 days, 23 hours, 59 minutes, 60.00 seconds")  ->  52
4212
4218
*/
4213
4219
static void nice_time(double sec,char *buff,bool part_second)
4214
4220
{
4309
4315
        processed_prompt->append(current_db ? current_db : "(none)");
4310
4316
        break;
4311
4317
      case 'h':
4312
 
      {
4313
 
        const char *prompt;
4314
 
        prompt= connected ? drizzle_con_host(&con) : "not_connected";
4315
 
        if (strstr(prompt, "Localhost"))
4316
 
          processed_prompt->append("localhost");
4317
 
        else
4318
4318
        {
4319
 
          const char *end=strrchr(prompt,' ');
4320
 
          if (end != NULL)
4321
 
            processed_prompt->append(prompt, (end-prompt));
 
4319
          const char *prompt;
 
4320
          prompt= connected ? drizzle_con_host(&con) : "not_connected";
 
4321
          if (strstr(prompt, "Localhost"))
 
4322
            processed_prompt->append("localhost");
 
4323
          else
 
4324
          {
 
4325
            const char *end=strrchr(prompt,' ');
 
4326
            if (end != NULL)
 
4327
              processed_prompt->append(prompt, (end-prompt));
 
4328
          }
 
4329
          break;
4322
4330
        }
4323
 
        break;
4324
 
      }
4325
4331
      case 'p':
4326
 
      {
4327
 
        if (!connected)
4328
4332
        {
4329
 
          processed_prompt->append("not_connected");
4330
 
          break;
4331
 
        }
 
4333
          if (!connected)
 
4334
          {
 
4335
            processed_prompt->append("not_connected");
 
4336
            break;
 
4337
          }
4332
4338
 
4333
 
        if (strcmp(drizzle_con_uds(&con), ""))
4334
 
        {
4335
 
          const char *pos=strrchr(drizzle_con_uds(&con),'/');
4336
 
          processed_prompt->append(pos ? pos+1 : drizzle_con_uds(&con));
 
4339
          if (drizzle_con_uds(&con))
 
4340
          {
 
4341
            const char *pos=strrchr(drizzle_con_uds(&con),'/');
 
4342
            processed_prompt->append(pos ? pos+1 : drizzle_con_uds(&con));
 
4343
          }
 
4344
          else
 
4345
            add_int_to_prompt(drizzle_con_port(&con));
4337
4346
        }
4338
 
        else
4339
 
          add_int_to_prompt(drizzle_con_port(&con));
4340
 
      }
4341
 
      break;
 
4347
        break;
4342
4348
      case 'U':
4343
4349
        if (!full_username)
4344
4350
          init_username();
4448
4454
 
4449
4455
static void init_username()
4450
4456
{
4451
 
/* XXX need this?
4452
 
  free(full_username);
4453
 
  free(part_username);
 
4457
  /* XXX need this?
 
4458
    free(full_username);
 
4459
    free(part_username);
4454
4460
 
4455
 
  drizzle_result_st *result;
4456
 
  if (!drizzleclient_query(&drizzle,"select USER()") &&
4457
 
      (result=drizzleclient_use_result(&drizzle)))
4458
 
  {
 
4461
    drizzle_result_st *result;
 
4462
    if (!drizzleclient_query(&drizzle,"select USER()") &&
 
4463
    (result=drizzleclient_use_result(&drizzle)))
 
4464
    {
4459
4465
    drizzle_row_t cur=drizzleclient_fetch_row(result);
4460
4466
    full_username= strdup(cur[0]);
4461
4467
    part_username= strdup(strtok(cur[0],"@"));
4462
4468
    (void) drizzleclient_fetch_row(result);        // Read eof
4463
 
  }
4464
 
*/
 
4469
    }
 
4470
  */
4465
4471
}
4466
4472
 
4467
4473
static int com_prompt(string *, const char *line)
4484
4490
}
4485
4491
 
4486
4492
/*
4487
 
    strcont(str, set) if str contanies any character in the string set.
4488
 
    The result is the position of the first found character in str, or NULL
4489
 
    if there isn't anything found.
 
4493
  strcont(str, set) if str contanies any character in the string set.
 
4494
  The result is the position of the first found character in str, or NULL
 
4495
  if there isn't anything found.
4490
4496
*/
4491
4497
 
4492
4498
static const char * strcont(register const char *str, register const char *set)