76
#ifdef HAVE_LIBREADLINE
77
# if defined(HAVE_READLINE_READLINE_H)
78
# include <readline/readline.h>
79
# elif defined(HAVE_READLINE_H)
80
# include <readline.h>
81
# else /* !defined(HAVE_READLINE_H) */
82
extern char *readline ();
83
# endif /* !defined(HAVE_READLINE_H) */
85
#else /* !defined(HAVE_READLINE_READLINE_H) */
87
# error Readline Required
88
#endif /* HAVE_LIBREADLINE */
102
#undef bcmp // Fix problem with new readline
90
#ifdef HAVE_READLINE_HISTORY
91
# if defined(HAVE_READLINE_HISTORY_H)
92
# include <readline/history.h>
93
# elif defined(HAVE_HISTORY_H)
95
# else /* !defined(HAVE_HISTORY_H) */
96
extern void add_history ();
97
extern int write_history ();
98
extern int read_history ();
99
# endif /* defined(HAVE_READLINE_HISTORY_H) */
101
#endif /* HAVE_READLINE_HISTORY */
104
#ifdef HAVE_READLINE_HISTORY_H
105
#include <readline/history.h>
107
#include <readline/readline.h>
104
110
Make the old readline interface look like the new one.
167
152
typedef enum enum_info_type INFO_TYPE;
169
154
static DRIZZLE drizzle; /* The connection */
170
static bool ignore_errors= false, quick= false,
171
connected= false, opt_raw_data= false, unbuffered= false,
172
output_tables= false, opt_rehash= true, skip_updates= false,
173
safe_updates= false, one_database= false,
174
opt_compress= false, using_opt_local_infile= false,
175
vertical= false, line_numbers= true, column_names= true,
176
opt_nopager= true, opt_outfile= false, named_cmds= false,
177
tty_password= false, opt_nobeep= false, opt_reconnect= true,
178
default_charset_used= false, opt_secure_auth= false,
179
default_pager_set= false, opt_sigint_ignore= false,
180
auto_vertical_output= false,
181
show_warnings= false, executing_query= false, interrupted_query= false;
182
static uint32_t show_progress_size= 0;
155
static bool ignore_errors=0,quick=0,
156
connected=0,opt_raw_data=0,unbuffered=0,output_tables=0,
157
opt_rehash=1,skip_updates=0,safe_updates=0,one_database=0,
158
opt_compress=0, using_opt_local_infile=0,
159
vertical=0, line_numbers=1, column_names=1,
160
opt_nopager=1, opt_outfile=0, named_cmds= 0,
161
tty_password= 0, opt_nobeep=0, opt_reconnect=1,
162
default_charset_used= 0, opt_secure_auth= 0,
163
default_pager_set= 0, opt_sigint_ignore= 0,
164
auto_vertical_output= 0,
165
show_warnings= 0, executing_query= 0, interrupted_query= 0;
183
166
static bool debug_info_flag, debug_check_flag;
184
167
static bool column_types_flag;
185
static bool preserve_comments= false;
186
static uint32_t opt_max_allowed_packet, opt_net_buffer_length,
188
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
168
static bool preserve_comments= 0;
169
static uint32_t opt_max_allowed_packet, opt_net_buffer_length;
170
static int verbose=0,opt_silent=0,opt_drizzle_port=0, opt_local_infile=0;
189
171
static uint my_end_arg;
190
static char * opt_drizzle_unix_port= NULL;
191
static int connect_flag= CLIENT_INTERACTIVE;
192
static char *current_host, *current_db, *current_user= NULL,
193
*opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
172
static char * opt_drizzle_unix_port=0;
173
static int connect_flag=CLIENT_INTERACTIVE;
174
static char *current_host,*current_db,*current_user=0,*opt_password=0,
175
*delimiter_str= 0,* current_prompt= 0;
194
176
static char *histfile;
195
177
static char *histfile_tmp;
196
178
static string *glob_buffer;
197
179
static string *processed_prompt= NULL;
198
180
static char *default_prompt= NULL;
199
static char *full_username= NULL,*part_username= NULL;
181
static char *full_username=0,*part_username=0;
200
182
static STATUS status;
201
183
static uint32_t select_limit;
202
184
static uint32_t max_join_size;
203
185
static uint32_t opt_connect_timeout= 0;
186
static char drizzle_charsets_dir[FN_REFLEN+1];
204
187
// TODO: Need to i18n these
205
static const char *day_names[]= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
206
static const char *month_names[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul",
188
static const char *day_names[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
189
static const char *month_names[]={"Jan","Feb","Mar","Apr","May","Jun","Jul",
207
190
"Aug","Sep","Oct","Nov","Dec"};
208
191
static char default_pager[FN_REFLEN];
209
192
static char pager[FN_REFLEN], outfile[FN_REFLEN];
283
266
N_("Send command to drizzle server, display result vertically.")},
284
267
{ "exit", 'q', com_quit, 0, N_("Exit drizzle. Same as quit.")},
285
268
{ "go", 'g', com_go, 0, N_("Send command to drizzle server.") },
286
{ "help", 'h', com_help, 0, N_("Display this help.") },
269
{ "help", 'h', com_help, 1, N_("Display this help.") },
287
270
{ "nopager",'n', com_nopager,0, N_("Disable pager, print to stdout.") },
288
271
{ "notee", 't', com_notee, 0, N_("Don't write into outfile.") },
289
272
{ "pager", 'P', com_pager, 1,
1049
1036
MY_INIT(argv[0]);
1050
1037
delimiter_str= delimiter;
1051
default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1052
getenv("DRIZZLE_PS1") :
1055
if (default_prompt == NULL)
1057
fprintf(stderr, _("Memory allocation error while constructing initial "
1058
"prompt. Aborting.\n"));
1061
current_prompt= strdup(default_prompt);
1062
if (current_prompt == NULL)
1064
fprintf(stderr, _("Memory allocation error while constructing initial "
1065
"prompt. Aborting.\n"));
1038
default_prompt= my_strdup(getenv("DRIZZLE_PS1") ?
1039
getenv("DRIZZLE_PS1") :
1040
"drizzle>> ", MYF(0));
1041
current_prompt= my_strdup(default_prompt, MYF(0));
1068
1042
processed_prompt= new string();
1069
1043
processed_prompt->reserve(32);
1071
1045
prompt_counter=0;
1073
1047
outfile[0]=0; // no (default) outfile
1074
strcpy(pager, "stdout"); // the default, if --pager wasn't given
1048
my_stpcpy(pager, "stdout"); // the default, if --pager wasn't given
1076
1050
char *tmp=getenv("PAGER");
1077
1051
if (tmp && strlen(tmp))
1079
1053
default_pager_set= 1;
1080
strcpy(default_pager, tmp);
1054
my_stpcpy(default_pager, tmp);
1083
1057
if (!isatty(0) || !isatty(1))
1261
kill_drizzle= drizzleclient_create(kill_drizzle);
1262
if (!drizzleclient_connect(kill_drizzle,current_host, current_user, opt_password,
1237
kill_drizzle= drizzle_create(kill_drizzle);
1238
if (!drizzle_connect(kill_drizzle,current_host, current_user, opt_password,
1263
1239
"", opt_drizzle_port, opt_drizzle_unix_port,0))
1268
1244
/* kill_buffer is always big enough because max length of %lu is 15 */
1269
sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u", drizzleclient_thread_id(&drizzle));
1270
drizzleclient_real_query(kill_drizzle, kill_buffer, strlen(kill_buffer));
1271
drizzleclient_close(kill_drizzle);
1245
sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u", drizzle_thread_id(&drizzle));
1246
drizzle_real_query(kill_drizzle, kill_buffer, strlen(kill_buffer));
1247
drizzle_close(kill_drizzle);
1272
1248
tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1274
1250
interrupted_query= 1;
1308
1284
(char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1310
1286
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},
1287
{"character-sets-dir", OPT_CHARSETS_DIR,
1288
N_("Directory where character sets are."), (char**) &charsets_dir,
1289
(char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1311
1290
{"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
1312
1291
(char**) &column_types_flag, (char**) &column_types_flag,
1313
1292
0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1381
1360
{"no-pager", OPT_NOPAGER,
1382
1361
N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
1383
1362
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1385
1364
N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1386
1365
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1387
{"port", 'p', N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, ")
1366
{"port", 'P', N_("Port number to use for connection or 0 for default to, in order of preference, my.cnf, $DRIZZLE_TCP_PORT, ")
1388
1367
N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
1389
0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1368
(char**) &opt_drizzle_port,
1369
(char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1390
1370
{"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
1391
1371
(char**) ¤t_prompt, (char**) ¤t_prompt, 0, GET_STR_ALLOC,
1392
1372
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1373
{"protocol", OPT_DRIZZLE_PROTOCOL, N_("The protocol of connection (tcp,socket,pipe,memory)."),
1374
0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1394
1376
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."),
1395
1377
(char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1455
1437
{"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1456
1438
(char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1457
1439
0, 0, 0, 0, 0, 0},
1458
{"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1459
(char**) &show_progress_size, (char**) &show_progress_size, 0, GET_ULONG, REQUIRED_ARG,
1461
1440
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1467
1446
const char* readline= "readline";
1469
1448
printf(_("%s Ver %s Distrib %s, for %s (%s) using %s %s\n"),
1470
my_progname, VER, drizzleclient_get_client_info(),
1449
my_progname, VER, drizzle_get_client_info(),
1471
1450
SYSTEM_TYPE, MACHINE_TYPE,
1472
1451
readline, rl_library_version);
1476
printf(_("Copyright (C) 2008 Sun Microsystems\n"
1477
"This software comes with ABSOLUTELY NO WARRANTY. "
1478
"This is free software,\n"
1479
"and you are welcome to modify and redistribute it "
1480
"under the GPL license\n"));
1456
Copyright (C) 2000-2008 MySQL AB\n \
1457
This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n \
1458
and you are welcome to modify and redistribute it under the GPL license\n"));
1481
1459
printf(_("Usage: %s [OPTIONS] [database]\n"), my_progname);
1482
1460
my_print_help(my_long_options);
1483
print_defaults("drizzle", load_default_groups);
1461
print_defaults("my", load_default_groups);
1484
1462
my_print_variables(my_long_options);
1489
get_one_option(int optid, const struct my_option *, char *argument)
1467
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
1491
char *endchar= NULL;
1492
uint64_t temp_drizzle_port= 0;
1494
1470
switch(optid) {
1471
case OPT_CHARSETS_DIR:
1472
strmake(drizzle_charsets_dir, argument, sizeof(drizzle_charsets_dir) - 1);
1473
charsets_dir = drizzle_charsets_dir;
1495
1475
case OPT_DEFAULT_CHARSET:
1496
1476
default_charset_used= 1;
1498
1478
case OPT_DELIMITER:
1499
1479
if (argument == disabled_my_option)
1501
strcpy(delimiter, DEFAULT_DELIMITER);
1481
my_stpcpy(delimiter, DEFAULT_DELIMITER);
1505
1485
/* Check that delimiter does not contain a backslash */
1506
1486
if (!strstr(argument, "\\"))
1508
strncpy(delimiter, argument, sizeof(delimiter) - 1);
1488
strmake(delimiter, argument, sizeof(delimiter) - 1);
1512
1492
put_info(_("DELIMITER cannot contain a backslash character"),
1513
1493
INFO_ERROR,0,0);
1517
1497
delimiter_length= (uint)strlen(delimiter);
1518
1498
delimiter_str= delimiter;
1520
1500
case OPT_LOCAL_INFILE:
1521
using_opt_local_infile= 1;
1501
using_opt_local_infile=1;
1524
1504
if (argument == disabled_my_option)
1580
1560
one_database= skip_updates= 1;
1583
temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
1584
/* if there is an alpha character this is not a valid port */
1585
if (strlen(endchar) != 0)
1587
put_info(_("Non-integer value supplied for port. If you are trying to enter a password please use --password instead."), INFO_ERROR, 0, 0);
1590
/* If the port number is > 65535 it is not a valid port
1591
This also helps with potential data loss casting unsigned long to a
1593
if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
1595
put_info(_("Value supplied for port is not valid."), INFO_ERROR, 0, 0);
1600
opt_drizzle_port= (uint32_t) temp_drizzle_port;
1604
/* Don't require password */
1605
1563
if (argument == disabled_my_option)
1607
argument= (char*) "";
1564
argument= (char*) ""; // Don't require password
1611
1567
char *start= argument;
1612
1568
free(opt_password);
1613
1569
opt_password= strdup(argument);
1616
/* Overwriting password with 'x' */
1570
while (*argument) *argument++= 'x'; // Destroy argument
1623
1573
tty_password= 0;
1627
1576
tty_password= 1;
1631
1579
if (argument == disabled_my_option)
2361
2308
/* hash Drizzle functions (to be implemented) */
2363
2310
/* hash all database names */
2364
if (drizzleclient_query(&drizzle,"show databases") == 0)
2311
if (drizzle_query(&drizzle,"show databases") == 0)
2366
if (!(databases = drizzleclient_store_result(&drizzle)))
2367
put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
2313
if (!(databases = drizzle_store_result(&drizzle)))
2314
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2370
while ((database_row=drizzleclient_fetch_row(databases)))
2317
while ((database_row=drizzle_fetch_row(databases)))
2372
2319
char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
2374
2321
add_word(&ht,(char*) str);
2376
drizzleclient_free_result(databases);
2323
drizzle_free_result(databases);
2379
2326
/* hash all table names */
2380
if (drizzleclient_query(&drizzle,"show tables")==0)
2327
if (drizzle_query(&drizzle,"show tables")==0)
2382
if (!(tables = drizzleclient_store_result(&drizzle)))
2383
put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
2329
if (!(tables = drizzle_store_result(&drizzle)))
2330
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2386
if (drizzleclient_num_rows(tables) > 0 && !opt_silent && write_info)
2333
if (drizzle_num_rows(tables) > 0 && !opt_silent && write_info)
2388
2335
tee_fprintf(stdout, _("\
2389
2336
Reading table information for completion of table and column names\n \
2390
2337
You can turn off this feature to get a quicker startup with -A\n\n"));
2392
while ((table_row=drizzleclient_fetch_row(tables)))
2339
while ((table_row=drizzle_fetch_row(tables)))
2394
2341
char *str=strdup_root(&hash_mem_root, (char*) table_row[0]);
2407
drizzleclient_data_seek(tables,0);
2354
drizzle_data_seek(tables,0);
2408
2355
if (!(field_names= (char ***) alloc_root(&hash_mem_root,sizeof(char **) *
2409
(uint) (drizzleclient_num_rows(tables)+1))))
2356
(uint) (drizzle_num_rows(tables)+1))))
2411
drizzleclient_free_result(tables);
2358
drizzle_free_result(tables);
2415
while ((table_row=drizzleclient_fetch_row(tables)))
2362
while ((table_row=drizzle_fetch_row(tables)))
2417
if ((fields=drizzleclient_list_fields(&drizzle,(const char*) table_row[0],NULL)))
2364
if ((fields=drizzle_list_fields(&drizzle,(const char*) table_row[0],NULL)))
2419
num_fields=drizzleclient_num_fields(fields);
2366
num_fields=drizzle_num_fields(fields);
2420
2367
if (!(field_names[i] = (char **) alloc_root(&hash_mem_root,
2421
2368
sizeof(char *) *
2422
2369
(num_fields*2+1))))
2424
drizzleclient_free_result(fields);
2371
drizzle_free_result(fields);
2427
2374
field_names[i][num_fields*2]= '\0';
2429
while ((sql_field=drizzleclient_fetch_field(fields)))
2376
while ((sql_field=drizzle_fetch_field(fields)))
2431
2378
sprintf(buf,"%.64s.%.64s",table_row[0],sql_field->name);
2432
2379
field_names[i][j] = strdup_root(&hash_mem_root,buf);
2533
int drizzleclient_store_result_for_lazy(DRIZZLE_RES **result)
2480
int drizzle_store_result_for_lazy(DRIZZLE_RES **result)
2535
if ((*result=drizzleclient_store_result(&drizzle)))
2482
if ((*result=drizzle_store_result(&drizzle)))
2538
if (drizzleclient_error(&drizzle)[0])
2485
if (drizzle_error(&drizzle)[0])
2539
2486
return put_error(&drizzle);
2490
static void print_help_item(DRIZZLE_ROW *cur, int num_name, int num_cat, char *last_char)
2492
char ccat= (*cur)[num_cat][0];
2493
if (*last_char != ccat)
2495
put_info(ccat == 'Y' ? _("categories:") : _("topics:"), INFO_INFO,0,0);
2498
tee_fprintf(PAGER, " %s\n", (*cur)[num_name]);
2502
static int com_server_help(string *buffer,
2503
const char *line __attribute__((unused)),
2507
const char *server_cmd= buffer->c_str();
2509
DRIZZLE_RES *result;
2512
if (help_arg[0] != '\'')
2514
char *end_arg= strchr(help_arg, '\0');
2517
while (my_isspace(charset_info,*end_arg))
2521
(void) strxnmov(cmd_buf, sizeof(cmd_buf), "help '", help_arg, "'", NULL);
2522
server_cmd= cmd_buf;
2525
if (!connected && reconnect())
2528
if ((error= drizzle_real_query_for_lazy(server_cmd,(int)strlen(server_cmd))) ||
2529
(error= drizzle_store_result_for_lazy(&result)))
2534
unsigned int num_fields= drizzle_num_fields(result);
2535
uint64_t num_rows= drizzle_num_rows(result);
2536
drizzle_fetch_fields(result);
2537
if (num_fields==3 && num_rows==1)
2539
if (!(cur= drizzle_fetch_row(result)))
2546
tee_fprintf(PAGER, _("Name: \'%s\'\n"), cur[0]);
2547
tee_fprintf(PAGER, _("Description:\n%s"), cur[1]);
2548
if (cur[2] && *((char*)cur[2]))
2549
tee_fprintf(PAGER, _("Examples:\n%s"), cur[2]);
2550
tee_fprintf(PAGER, "\n");
2553
else if (num_fields >= 2 && num_rows)
2558
int num_name= 0, num_cat= 0;
2560
if (num_fields == 2)
2562
put_info(_("Many help items for your request exist."), INFO_INFO,0,0);
2563
put_info(_("To make a more specific request, please type 'help <item>',\nwhere <item> is one of the following"), INFO_INFO,0,0);
2567
else if ((cur= drizzle_fetch_row(result)))
2569
tee_fprintf(PAGER, _("You asked for help about help category: '%s'\n"), cur[0]);
2570
put_info(_("For more information, type 'help <item>', where <item> is one of the following"), INFO_INFO,0,0);
2573
print_help_item(&cur,1,2,&last_char);
2576
while ((cur= drizzle_fetch_row(result)))
2577
print_help_item(&cur,num_name,num_cat,&last_char);
2578
tee_fprintf(PAGER, "\n");
2583
put_info(_("\nNothing found"), INFO_INFO,0,0);
2584
put_info(_("Please try to run 'help contents' for a list of all accessible topics\n"), INFO_INFO,0,0);
2589
drizzle_free_result(result);
2544
com_help(string *buffer, const char *)
2594
com_help(string *buffer __attribute__((unused)),
2595
const char *line __attribute__((unused)))
2546
2597
register int i, j;
2547
char buff[32], *end;
2598
char * help_arg= strchr(line,' '), buff[32], *end;
2601
while (my_isspace(charset_info,*help_arg))
2603
if (*help_arg) return com_server_help(buffer,line,help_arg);
2549
2606
put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
2550
2607
if (!named_cmds)
2551
2608
put_info(_("Note that all text commands must be first on line and end with ';'"),INFO_INFO,0,0);
2552
2609
for (i = 0; commands[i].name; i++)
2554
end= strcpy(buff, commands[i].name);
2555
end+= strlen(commands[i].name);
2611
end= my_stpcpy(buff, commands[i].name);
2556
2612
for (j= (int)strlen(commands[i].name); j < 10; j++)
2557
end= strcpy(end, " ")+1;
2613
end= my_stpcpy(end, " ");
2558
2614
if (commands[i].func)
2559
2615
tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
2560
2616
commands[i].cmd_char, _(commands[i].doc));
2562
tee_fprintf(stdout, "\n");
2618
if (connected && drizzle_get_server_version(&drizzle) >= 40100)
2619
put_info(_("\nFor server side help, type 'help contents'\n"), INFO_INFO,0,0);
2569
com_clear(string *buffer, const char *)
2625
com_clear(string *buffer,
2626
const char *line __attribute__((unused)))
2571
2628
if (status.add_to_history)
2572
2629
fix_history(buffer);
2679
2737
print_table_data(result);
2681
2739
ngettext("%ld row in set","%ld rows in set",
2682
(long) drizzleclient_num_rows(result)),
2683
(long) drizzleclient_num_rows(result));
2740
(long) drizzle_num_rows(result)),
2741
(long) drizzle_num_rows(result));
2685
if (drizzleclient_errno(&drizzle))
2743
if (drizzle_errno(&drizzle))
2686
2744
error= put_error(&drizzle);
2689
else if (drizzleclient_affected_rows(&drizzle) == ~(uint64_t) 0)
2690
strcpy(buff,_("Query OK"));
2747
else if (drizzle_affected_rows(&drizzle) == ~(uint64_t) 0)
2748
my_stpcpy(buff,_("Query OK"));
2692
2750
sprintf(buff, ngettext("Query OK, %ld row affected",
2693
2751
"Query OK, %ld rows affected",
2694
(long) drizzleclient_affected_rows(&drizzle)),
2695
(long) drizzleclient_affected_rows(&drizzle));
2752
(long) drizzle_affected_rows(&drizzle)),
2753
(long) drizzle_affected_rows(&drizzle));
2697
2755
pos= strchr(buff, '\0');
2698
if ((warnings= drizzleclient_warning_count(&drizzle)))
2756
if ((warnings= drizzle_warning_count(&drizzle)))
2702
pos= int10_to_str(warnings, pos, 10);
2703
pos= strcpy(pos, " warning")+8;
2760
pos=int10_to_str(warnings, pos, 10);
2761
pos=my_stpcpy(pos, " warning");
2704
2762
if (warnings != 1)
2707
strcpy(pos, time_buff);
2765
my_stpcpy(pos, time_buff);
2708
2766
put_info(buff,INFO_RESULT,0,0);
2709
if (drizzleclient_info(&drizzle))
2710
put_info(drizzleclient_info(&drizzle),INFO_RESULT,0,0);
2767
if (drizzle_info(&drizzle))
2768
put_info(drizzle_info(&drizzle),INFO_RESULT,0,0);
2711
2769
put_info("",INFO_RESULT,0,0); // Empty row
2713
if (result && !drizzleclient_eof(result)) /* Something wrong when using quick */
2771
if (result && !drizzle_eof(result)) /* Something wrong when using quick */
2714
2772
error= put_error(&drizzle);
2715
2773
else if (unbuffered)
2716
2774
fflush(stdout);
2717
drizzleclient_free_result(result);
2718
} while (!(err= drizzleclient_next_result(&drizzle)));
2775
drizzle_free_result(result);
2776
} while (!(err= drizzle_next_result(&drizzle)));
2720
2778
error= put_error(&drizzle);
2882
2938
DRIZZLE_FIELD *field;
2883
2939
bool *num_flag;
2884
2940
string separator;
2886
2942
separator.reserve(256);
2888
num_flag=(bool*) malloc(sizeof(bool)*drizzleclient_num_fields(result));
2944
num_flag=(bool*) my_malloc(sizeof(bool)*drizzle_num_fields(result),
2889
2946
if (column_types_flag)
2891
2948
print_field_types(result);
2892
if (!drizzleclient_num_rows(result))
2949
if (!drizzle_num_rows(result))
2894
drizzleclient_field_seek(result,0);
2951
drizzle_field_seek(result,0);
2896
2953
separator.append("+");
2897
while ((field = drizzleclient_fetch_field(result)))
2954
while ((field = drizzle_fetch_field(result)))
2899
uint32_t x, length= 0;
2903
/* Check if the max_byte value is really the maximum in terms
2904
of visual length since multibyte characters can affect the
2905
length of the separator. */
2906
length= charset_info->cset->numcells(charset_info,
2908
field->name+field->name_length);
2910
if (field->name_length == field->max_length)
2912
if (length < field->max_length)
2913
field->max_length= length;
2917
length= field->name_length;
2956
uint32_t length= column_names ? field->name_length : 0;
2922
2958
length=max(length,field->length);
2947
2983
tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
2948
2984
MAX_COLUMN_LENGTH),
2950
num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) ||
2986
num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) ||
2951
2987
(field->type == DRIZZLE_TYPE_NEWDECIMAL));
2953
2989
(void) tee_fputs("\n", PAGER);
2954
2990
tee_puts((char*) separator.c_str(), PAGER);
2957
while ((cur= drizzleclient_fetch_row(result)))
2993
while ((cur= drizzle_fetch_row(result)))
2959
2995
if (interrupted_query)
2961
uint32_t *lengths= drizzleclient_fetch_lengths(result);
2997
uint32_t *lengths= drizzle_fetch_lengths(result);
2962
2998
(void) tee_fputs("| ", PAGER);
2963
drizzleclient_field_seek(result, 0);
2964
for (uint off= 0; off < drizzleclient_num_fields(result); off++)
2999
drizzle_field_seek(result, 0);
3000
for (uint off= 0; off < drizzle_num_fields(result); off++)
2966
3002
const char *buffer;
2967
3003
uint data_length;
3113
3149
field->max_length=length;
3116
drizzleclient_field_seek(result,0);
3117
for (uint row_count=1; (cur= drizzleclient_fetch_row(result)); row_count++)
3152
drizzle_field_seek(result,0);
3153
for (uint row_count=1; (cur= drizzle_fetch_row(result)); row_count++)
3119
3155
if (interrupted_query)
3121
drizzleclient_field_seek(result,0);
3157
drizzle_field_seek(result,0);
3122
3158
tee_fprintf(PAGER,
3123
3159
"*************************** %d. row ***************************\n", row_count);
3124
for (uint off=0; off < drizzleclient_num_fields(result); off++)
3160
for (uint off=0; off < drizzle_num_fields(result); off++)
3126
field= drizzleclient_fetch_field(result);
3162
field= drizzle_fetch_field(result);
3127
3163
tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
3128
3164
tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3326
3361
tee_fprintf(stdout, "Default pager wasn't set, using stdout.\n");
3328
strcpy(pager, "stdout");
3363
my_stpcpy(pager, "stdout");
3332
strcpy(pager, default_pager);
3367
my_stpcpy(pager, default_pager);
3336
end= strncpy(pager_name, param, sizeof(pager_name)-1);
3337
end+= strlen(pager_name);
3371
end= strmake(pager_name, param, sizeof(pager_name)-1);
3338
3372
while (end > pager_name && (my_isspace(charset_info,end[-1]) ||
3339
3373
my_iscntrl(charset_info,end[-1])))
3342
strcpy(pager, pager_name);
3343
strcpy(default_pager, pager_name);
3376
my_stpcpy(pager, pager_name);
3377
my_stpcpy(default_pager, pager_name);
3346
3380
tee_fprintf(stdout, "PAGER set to '%s'\n", pager);
3696
drizzleclient_close(&drizzle);
3733
drizzle_close(&drizzle);
3698
drizzleclient_create(&drizzle);
3735
drizzle_create(&drizzle);
3699
3736
if (opt_connect_timeout)
3701
3738
uint timeout=opt_connect_timeout;
3702
drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3739
drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3703
3740
(char*) &timeout);
3705
3742
if (opt_compress)
3706
drizzleclient_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
3743
drizzle_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
3707
3744
if (opt_secure_auth)
3708
drizzleclient_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
3745
drizzle_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
3709
3746
if (using_opt_local_infile)
3710
drizzleclient_options(&drizzle,DRIZZLE_OPT_LOCAL_INFILE, (char*) &opt_local_infile);
3747
drizzle_options(&drizzle,DRIZZLE_OPT_LOCAL_INFILE, (char*) &opt_local_infile);
3711
3748
if (safe_updates)
3713
3750
char init_command[100];
3714
3751
sprintf(init_command,
3715
3752
"SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3716
",MAX_JOIN_SIZE=%"PRIu32,
3753
",SQL_MAX_JOIN_SIZE=%"PRIu32,
3717
3754
select_limit, max_join_size);
3718
drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3755
drizzle_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3720
if (!drizzleclient_connect(&drizzle, host, user, password,
3757
if (!drizzle_connect(&drizzle, host, user, password,
3721
3758
database, opt_drizzle_port, opt_drizzle_unix_port,
3722
3759
connect_flag | CLIENT_MULTI_STATEMENTS))
3725
(drizzleclient_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3726
drizzleclient_errno(&drizzle) != CR_CONNECTION_ERROR))
3762
(drizzle_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3763
drizzle_errno(&drizzle) != CR_CONNECTION_ERROR))
3728
3765
(void) put_error(&drizzle);
3729
3766
(void) fflush(stdout);
3749
3787
usage(1); /* Print version */
3752
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzleclient_thread_id(&drizzle));
3790
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_thread_id(&drizzle));
3754
3792
Don't remove "limit 1",
3755
3793
it is protection againts SQL_SELECT_LIMIT=0
3757
if (!drizzleclient_query(&drizzle,"select DATABASE(), USER() limit 1") &&
3758
(result=drizzleclient_use_result(&drizzle)))
3795
if (!drizzle_query(&drizzle,"select DATABASE(), USER() limit 1") &&
3796
(result=drizzle_use_result(&drizzle)))
3760
DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
3798
DRIZZLE_ROW cur=drizzle_fetch_row(result);
3763
3801
tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3764
3802
tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3766
drizzleclient_free_result(result);
3804
drizzle_free_result(result);
3768
3806
tee_puts("SSL:\t\t\tNot in use", stdout);
3784
3822
tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
3785
3823
tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
3786
3824
tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&drizzle));
3787
tee_fprintf(stdout, "Protocol version:\t%d\n", drizzleclient_get_proto_info(&drizzle));
3788
tee_fprintf(stdout, "Connection:\t\t%s\n", drizzleclient_get_host_info(&drizzle));
3789
if ((id= drizzleclient_insert_id(&drizzle)))
3825
tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_get_proto_info(&drizzle));
3826
tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_get_host_info(&drizzle));
3827
if ((id= drizzle_insert_id(&drizzle)))
3790
3828
tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
3792
3830
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3793
if (!drizzleclient_query(&drizzle,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
3794
(result=drizzleclient_use_result(&drizzle)))
3831
if (!drizzle_query(&drizzle,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
3832
(result=drizzle_use_result(&drizzle)))
3796
DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
3834
DRIZZLE_ROW cur=drizzle_fetch_row(result);
3799
3837
tee_fprintf(stdout, "Server characterset:\t%s\n", cur[2] ? cur[2] : "");
3830
3868
static const char *
3831
3869
server_version_string(DRIZZLE *con)
3833
static string buf("");
3834
static bool server_version_string_reserved= false;
3871
static char buf[MAX_SERVER_VERSION_LENGTH] = "";
3836
if (!server_version_string_reserved)
3838
buf.reserve(MAX_SERVER_VERSION_LENGTH);
3839
server_version_string_reserved= true;
3841
3873
/* Only one thread calls this, so no synchronization is needed */
3842
3874
if (buf[0] == '\0')
3844
3877
DRIZZLE_RES *result;
3846
buf.append(drizzleclient_get_server_info(con));
3879
bufp= my_stpncpy(buf, drizzle_get_server_info(con), sizeof buf);
3848
3881
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3849
if (!drizzleclient_query(con, "select @@version_comment limit 1") &&
3850
(result = drizzleclient_use_result(con)))
3882
if (!drizzle_query(con, "select @@version_comment limit 1") &&
3883
(result = drizzle_use_result(con)))
3852
DRIZZLE_ROW cur = drizzleclient_fetch_row(result);
3885
DRIZZLE_ROW cur = drizzle_fetch_row(result);
3853
3886
if (cur && cur[0])
3888
bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NULL);
3858
drizzleclient_free_result(result);
3890
drizzle_free_result(result);
3893
/* str*nmov doesn't guarantee NUL-termination */
3894
if (bufp == buf + sizeof buf)
3895
buf[sizeof buf - 1] = '\0';
4022
4058
if (sec >= 3600.0*24)
4024
4060
tmp=(uint32_t) floor(sec/(3600.0*24));
4025
sec-= 3600.0*24*tmp;
4026
buff= int10_to_str((long) tmp, buff, 10);
4029
buff= strcpy(buff," days ")+6;
4031
buff= strcpy(buff," day ")+5;
4062
buff=int10_to_str((long) tmp, buff, 10);
4063
buff=my_stpcpy(buff,tmp > 1 ? " days " : " day ");
4034
4065
if (sec >= 3600.0)
4036
4067
tmp=(uint32_t) floor(sec/3600.0);
4037
4068
sec-=3600.0*tmp;
4038
4069
buff=int10_to_str((long) tmp, buff, 10);
4041
buff= strcpy(buff, " hours ")+7;
4043
buff= strcpy(buff, " hour ")+6;
4070
buff=my_stpcpy(buff,tmp > 1 ? " hours " : " hour ");
4045
4072
if (sec >= 60.0)
4047
4074
tmp=(uint32_t) floor(sec/60.0);
4049
4076
buff=int10_to_str((long) tmp, buff, 10);
4050
buff= strcpy(buff," min ")+5;
4077
buff=my_stpcpy(buff," min ");
4052
4079
if (part_second)
4053
4080
sprintf(buff,"%.2f sec",sec);
4260
4290
free(part_username);
4262
4292
DRIZZLE_RES *result;
4263
if (!drizzleclient_query(&drizzle,"select USER()") &&
4264
(result=drizzleclient_use_result(&drizzle)))
4293
if (!drizzle_query(&drizzle,"select USER()") &&
4294
(result=drizzle_use_result(&drizzle)))
4266
DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
4296
DRIZZLE_ROW cur=drizzle_fetch_row(result);
4267
4297
full_username= strdup(cur[0]);
4268
4298
part_username= strdup(strtok(cur[0],"@"));
4269
(void) drizzleclient_fetch_row(result); // Read eof
4299
(void) drizzle_fetch_row(result); // Read eof
4273
static int com_prompt(string *, const char *line)
4303
static int com_prompt(string *buffer __attribute__((unused)),
4275
const char *ptr=strchr(line, ' ');
4306
char *ptr=strchr(line, ' ');
4308
free(current_prompt);
4309
current_prompt= strdup(ptr ? ptr+1 : default_prompt);
4277
4311
tee_fprintf(stdout, "Returning to default PROMPT of %s\n",
4278
4312
default_prompt);
4280
char * tmpptr= strdup(ptr ? ptr+1 : default_prompt);
4282
tee_fprintf(stdout, "Memory allocation error. Not changing prompt\n");
4285
free(current_prompt);
4286
current_prompt= tmpptr;
4287
4314
tee_fprintf(stdout, "PROMPT set to '%s'\n", current_prompt);