102
#undef bcmp // Fix problem with new readline
75
#ifdef HAVE_LIBREADLINE
76
# if defined(HAVE_READLINE_READLINE_H)
77
# include <readline/readline.h>
78
# elif defined(HAVE_READLINE_H)
79
# include <readline.h>
80
# else /* !defined(HAVE_READLINE_H) */
81
extern char *readline ();
82
# endif /* !defined(HAVE_READLINE_H) */
84
#else /* !defined(HAVE_READLINE_READLINE_H) */
86
# error Readline Required
87
#endif /* HAVE_LIBREADLINE */
104
#ifdef HAVE_READLINE_HISTORY_H
105
#include <readline/history.h>
107
#include <readline/readline.h>
89
#ifdef HAVE_READLINE_HISTORY
90
# if defined(HAVE_READLINE_HISTORY_H)
91
# include <readline/history.h>
92
# elif defined(HAVE_HISTORY_H)
94
# else /* !defined(HAVE_HISTORY_H) */
95
extern void add_history ();
96
extern int write_history ();
97
extern int read_history ();
98
# endif /* defined(HAVE_READLINE_HISTORY_H) */
100
#endif /* HAVE_READLINE_HISTORY */
110
103
Make the old readline interface look like the new one.
154
static map<string, string>::iterator completion_iter;
155
static map<string, string>::iterator completion_end;
156
static map<string, string> completion_map;
157
static string completion_string;
149
159
static char **defaults_argv;
151
161
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
152
162
typedef enum enum_info_type INFO_TYPE;
154
164
static DRIZZLE drizzle; /* The connection */
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;
165
static bool ignore_errors= false, quick= false,
166
connected= false, opt_raw_data= false, unbuffered= false,
167
output_tables= false, opt_rehash= true, skip_updates= false,
168
safe_updates= false, one_database= false,
170
vertical= false, line_numbers= true, column_names= true,
171
opt_nopager= true, opt_outfile= false, named_cmds= false,
172
tty_password= false, opt_nobeep= false, opt_reconnect= true,
173
default_charset_used= false, opt_secure_auth= false,
174
default_pager_set= false, opt_sigint_ignore= false,
175
auto_vertical_output= false,
176
show_warnings= false, executing_query= false, interrupted_query= false;
177
static uint32_t show_progress_size= 0;
166
178
static bool debug_info_flag, debug_check_flag;
167
179
static bool column_types_flag;
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;
171
static uint my_end_arg;
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;
180
static bool preserve_comments= false;
181
static uint32_t opt_max_allowed_packet, opt_net_buffer_length,
183
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
184
static uint32_t my_end_arg;
185
static char * opt_drizzle_unix_port= NULL;
186
static int connect_flag= 0;
187
static char *current_host, *current_db, *current_user= NULL,
188
*opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
176
189
static char *histfile;
177
190
static char *histfile_tmp;
178
191
static string *glob_buffer;
179
192
static string *processed_prompt= NULL;
180
193
static char *default_prompt= NULL;
181
static char *full_username=0,*part_username=0;
194
static char *full_username= NULL,*part_username= NULL;
182
195
static STATUS status;
183
196
static uint32_t select_limit;
184
197
static uint32_t max_join_size;
185
198
static uint32_t opt_connect_timeout= 0;
186
static char drizzle_charsets_dir[FN_REFLEN+1];
187
199
// TODO: Need to i18n these
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",
200
static const char *day_names[]= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
201
static const char *month_names[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul",
190
202
"Aug","Sep","Oct","Nov","Dec"};
191
203
static char default_pager[FN_REFLEN];
192
204
static char pager[FN_REFLEN], outfile[FN_REFLEN];
193
205
static FILE *PAGER, *OUTFILE;
194
static MEM_ROOT hash_mem_root;
195
static uint prompt_counter;
206
static uint32_t prompt_counter;
196
207
static char delimiter[16]= DEFAULT_DELIMITER;
197
static uint delimiter_length= 1;
208
static uint32_t delimiter_length= 1;
198
209
unsigned short terminal_width= 80;
200
211
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
202
int drizzle_real_query_for_lazy(const char *buf, int length);
203
int drizzle_store_result_for_lazy(DRIZZLE_RES **result);
213
int drizzleclient_real_query_for_lazy(const char *buf, int length);
214
int drizzleclient_store_result_for_lazy(DRIZZLE_RES **result);
206
217
void tee_fprintf(FILE *file, const char *fmt, ...);
266
277
N_("Send command to drizzle server, display result vertically.")},
267
278
{ "exit", 'q', com_quit, 0, N_("Exit drizzle. Same as quit.")},
268
279
{ "go", 'g', com_go, 0, N_("Send command to drizzle server.") },
269
{ "help", 'h', com_help, 1, N_("Display this help.") },
280
{ "help", 'h', com_help, 0, N_("Display this help.") },
270
281
{ "nopager",'n', com_nopager,0, N_("Disable pager, print to stdout.") },
271
282
{ "notee", 't', com_notee, 0, N_("Don't write into outfile.") },
272
283
{ "pager", 'P', com_pager, 1,
963
961
{ "SUBSTR", 0, 0, 0, ""},
964
962
{ "SUBSTRING", 0, 0, 0, ""},
965
963
{ "SUBSTRING_INDEX", 0, 0, 0, ""},
966
{ "SUBTIME", 0, 0, 0, ""},
967
964
{ "SUM", 0, 0, 0, ""},
968
965
{ "SYSDATE", 0, 0, 0, ""},
969
966
{ "SYSTEM_USER", 0, 0, 0, ""},
970
967
{ "TAN", 0, 0, 0, ""},
971
968
{ "TIME_FORMAT", 0, 0, 0, ""},
972
{ "TIME_TO_SEC", 0, 0, 0, ""},
973
{ "TIMEDIFF", 0, 0, 0, ""},
974
969
{ "TO_DAYS", 0, 0, 0, ""},
975
970
{ "TOUCHES", 0, 0, 0, ""},
976
971
{ "TRIM", 0, 0, 0, ""},
1284
1286
(char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1286
1288
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},
1290
1289
{"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
1291
1290
(char**) &column_types_flag, (char**) &column_types_flag,
1292
1291
0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1360
1359
{"no-pager", OPT_NOPAGER,
1361
1360
N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
1362
1361
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1364
1363
N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1365
1364
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1366
{"port", 'P', N_("Port number to use for connection or 0 for default to, in order of preference, my.cnf, $DRIZZLE_TCP_PORT, ")
1365
{"port", 'p', N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, ")
1367
1366
N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
1368
(char**) &opt_drizzle_port,
1369
(char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1367
0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1370
1368
{"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
1371
1369
(char**) ¤t_prompt, (char**) ¤t_prompt, 0, GET_STR_ALLOC,
1372
1370
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},
1376
1372
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."),
1377
1373
(char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1411
1407
{"connect_timeout", OPT_CONNECT_TIMEOUT,
1412
1408
N_("Number of seconds before connection timeout."),
1413
1409
(char**) &opt_connect_timeout,
1414
(char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 0, 0, 3600*12, 0,
1410
(char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 0, 0, 3600*12, 0,
1416
1412
{"max_allowed_packet", OPT_MAX_ALLOWED_PACKET,
1417
1413
N_("Max packet length to send to, or receive from server"),
1418
1414
(char**) &opt_max_allowed_packet, (char**) &opt_max_allowed_packet, 0,
1419
GET_ULONG, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1415
GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1420
1416
(int64_t) 2*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
1421
1417
{"net_buffer_length", OPT_NET_BUFFER_LENGTH,
1422
1418
N_("Buffer for TCP/IP and socket communication"),
1423
(char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0, GET_ULONG,
1419
(char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0, GET_UINT32,
1424
1420
REQUIRED_ARG, 16384, 1024, 512*1024*1024L, MALLOC_OVERHEAD, 1024, 0},
1425
1421
{"select_limit", OPT_SELECT_LIMIT,
1426
1422
N_("Automatic limit for SELECT when using --safe-updates"),
1427
1423
(char**) &select_limit,
1428
(char**) &select_limit, 0, GET_ULONG, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1424
(char**) &select_limit, 0, GET_UINT32, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1430
1426
{"max_join_size", OPT_MAX_JOIN_SIZE,
1431
1427
N_("Automatic limit for rows in a join when using --safe-updates"),
1432
1428
(char**) &max_join_size,
1433
(char**) &max_join_size, 0, GET_ULONG, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1429
(char**) &max_join_size, 0, GET_UINT32, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1435
1431
{"secure-auth", OPT_SECURE_AUTH, N_("Refuse client connecting to server if it uses old (pre-4.1.1) protocol"), (char**) &opt_secure_auth,
1436
1432
(char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1437
1433
{"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1438
1434
(char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1439
1435
0, 0, 0, 0, 0, 0},
1436
{"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1437
(char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
1440
1439
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1446
1445
const char* readline= "readline";
1448
1447
printf(_("%s Ver %s Distrib %s, for %s (%s) using %s %s\n"),
1449
my_progname, VER, drizzle_get_client_info(),
1448
my_progname, VER.c_str(), drizzleclient_get_client_info(),
1450
1449
SYSTEM_TYPE, MACHINE_TYPE,
1451
1450
readline, rl_library_version);
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"));
1454
printf(_("Copyright (C) 2008 Sun Microsystems\n"
1455
"This software comes with ABSOLUTELY NO WARRANTY. "
1456
"This is free software,\n"
1457
"and you are welcome to modify and redistribute it "
1458
"under the GPL license\n"));
1459
1459
printf(_("Usage: %s [OPTIONS] [database]\n"), my_progname);
1460
1460
my_print_help(my_long_options);
1461
print_defaults("my", load_default_groups);
1461
print_defaults("drizzle", load_default_groups);
1462
1462
my_print_variables(my_long_options);
1467
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
1467
get_one_option(int optid, const struct my_option *, char *argument)
1469
char *endchar= NULL;
1470
uint64_t temp_drizzle_port= 0;
1470
1472
switch(optid) {
1471
case OPT_CHARSETS_DIR:
1472
strmake(drizzle_charsets_dir, argument, sizeof(drizzle_charsets_dir) - 1);
1473
charsets_dir = drizzle_charsets_dir;
1475
1473
case OPT_DEFAULT_CHARSET:
1476
1474
default_charset_used= 1;
1478
1476
case OPT_DELIMITER:
1479
1477
if (argument == disabled_my_option)
1481
my_stpcpy(delimiter, DEFAULT_DELIMITER);
1479
strcpy(delimiter, DEFAULT_DELIMITER);
1485
1483
/* Check that delimiter does not contain a backslash */
1486
1484
if (!strstr(argument, "\\"))
1488
strmake(delimiter, argument, sizeof(delimiter) - 1);
1486
strncpy(delimiter, argument, sizeof(delimiter) - 1);
1492
1490
put_info(_("DELIMITER cannot contain a backslash character"),
1493
1491
INFO_ERROR,0,0);
1497
delimiter_length= (uint)strlen(delimiter);
1495
delimiter_length= (uint32_t)strlen(delimiter);
1498
1496
delimiter_str= delimiter;
1500
case OPT_LOCAL_INFILE:
1501
using_opt_local_infile=1;
1504
1499
if (argument == disabled_my_option)
2194
2222
return (char**) 0;
2198
static char *new_command_generator(const char *text,int state)
2225
inline string lower_string(const string &from_string)
2227
string to_string= from_string;
2228
transform(to_string.begin(), to_string.end(),
2229
to_string.begin(), ::tolower);
2232
inline string lower_string(const char * from_string)
2234
string to_string= from_string;
2235
return lower_string(to_string);
2239
class CompletionMatch :
2240
public unary_function<const string&, bool>
2245
CompletionMatch(string text) : match_text(text) {}
2246
inline bool operator() (const pair<string,string> &match_against) const
2249
lower_string(match_against.first.substr(0,match_text.size()));
2250
return match_func(sub_match,match_text);
2257
char *new_command_generator(const char *text, int state)
2207
textlen=(uint) strlen(text);
2210
{ /* lookup in the hash */
2215
b = find_all_matches(&ht,text,(uint) strlen(text),&len);
2223
ptr= strdup(e->str);
2229
{ /* traverse the entire hash, ugly but works */
2233
/* find the first used bucket */
2234
for (i=0 ; i < ht.nTableSize ; i++)
2236
if (ht.arBuckets[i])
2238
b = ht.arBuckets[i];
2246
{ /* find valid entry in bucket */
2247
if ((uint) strlen(e->str) == b->nKeyLength)
2248
ptr = strdup(e->str);
2249
/* find the next used entry */
2252
{ /* find the next used bucket */
2256
for (i++ ; i<ht.nTableSize; i++)
2258
if (ht.arBuckets[i])
2260
b = ht.arBuckets[i];
2262
completion_string= lower_string(text);
2263
if (completion_string.size() == 0)
2265
completion_iter= completion_map.begin();
2266
completion_end= completion_map.end();
2270
completion_iter= find_if(completion_map.begin(), completion_map.end(),
2271
CompletionMatch<equal_to<string> >(completion_string));
2272
completion_end= find_if(completion_iter, completion_map.end(),
2273
CompletionMatch<not_equal_to<string> >(completion_string));
2276
if (completion_iter == completion_end || (size_t)state > completion_map.size())
2278
char *result= (char *)malloc((*completion_iter).second.size()+1);
2279
strcpy(result, (*completion_iter).second.c_str());
2277
2284
/* Build up the completion hash */
2279
2286
static void build_completion_hash(bool rehash, bool write_info)
2296
/* Free old used memory */
2299
completion_hash_clean(&ht);
2300
free_root(&hash_mem_root,MYF(0));
2301
completion_map.clear();
2302
2303
/* hash this file's known subset of SQL commands */
2303
2304
while (cmd->name) {
2304
add_word(&ht,(char*) cmd->name);
2306
tmp_str_lower= lower_string(tmp_str);
2307
completion_map[tmp_str_lower]= tmp_str;
2308
2311
/* hash Drizzle functions (to be implemented) */
2310
2313
/* hash all database names */
2311
if (drizzle_query(&drizzle,"show databases") == 0)
2314
if (drizzleclient_query(&drizzle,"show databases") == 0)
2313
if (!(databases = drizzle_store_result(&drizzle)))
2314
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2316
if (!(databases = drizzleclient_store_result(&drizzle)))
2317
put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
2317
while ((database_row=drizzle_fetch_row(databases)))
2320
while ((database_row=drizzleclient_fetch_row(databases)))
2319
char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
2321
add_word(&ht,(char*) str);
2322
tmp_str= database_row[0];
2323
tmp_str_lower= lower_string(tmp_str);
2324
completion_map[tmp_str_lower]= tmp_str;
2323
drizzle_free_result(databases);
2326
drizzleclient_free_result(databases);
2326
2329
/* hash all table names */
2327
if (drizzle_query(&drizzle,"show tables")==0)
2330
if (drizzleclient_query(&drizzle,"show tables")==0)
2329
if (!(tables = drizzle_store_result(&drizzle)))
2330
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2332
if (!(tables = drizzleclient_store_result(&drizzle)))
2333
put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
2333
if (drizzle_num_rows(tables) > 0 && !opt_silent && write_info)
2336
if (drizzleclient_num_rows(tables) > 0 && !opt_silent && write_info)
2335
2338
tee_fprintf(stdout, _("\
2336
2339
Reading table information for completion of table and column names\n \
2337
2340
You can turn off this feature to get a quicker startup with -A\n\n"));
2339
while ((table_row=drizzle_fetch_row(tables)))
2342
while ((table_row=drizzleclient_fetch_row(tables)))
2341
char *str=strdup_root(&hash_mem_root, (char*) table_row[0]);
2343
!completion_hash_exists(&ht,(char*) str, (uint) strlen(str)))
2344
tmp_str= table_row[0];
2345
tmp_str_lower= lower_string(tmp_str);
2346
completion_map[tmp_str_lower]= tmp_str;
2354
drizzle_data_seek(tables,0);
2355
if (!(field_names= (char ***) alloc_root(&hash_mem_root,sizeof(char **) *
2356
(uint) (drizzle_num_rows(tables)+1))))
2358
drizzle_free_result(tables);
2362
while ((table_row=drizzle_fetch_row(tables)))
2364
if ((fields=drizzle_list_fields(&drizzle,(const char*) table_row[0],NULL)))
2356
drizzleclient_data_seek(tables,0);
2358
while ((table_row=drizzleclient_fetch_row(tables)))
2362
query.append("show fields in `");
2363
query.append(table_row[0]);
2366
if (drizzleclient_query(&drizzle, query.c_str()) == 0)
2366
num_fields=drizzle_num_fields(fields);
2367
if (!(field_names[i] = (char **) alloc_root(&hash_mem_root,
2371
drizzle_free_result(fields);
2374
field_names[i][num_fields*2]= '\0';
2376
while ((sql_field=drizzle_fetch_field(fields)))
2378
sprintf(buf,"%.64s.%.64s",table_row[0],sql_field->name);
2379
field_names[i][j] = strdup_root(&hash_mem_root,buf);
2380
add_word(&ht,field_names[i][j]);
2381
field_names[i][num_fields+j] = strdup_root(&hash_mem_root,
2383
if (!completion_hash_exists(&ht,field_names[i][num_fields+j],
2384
(uint) strlen(field_names[i][num_fields+j])))
2385
add_word(&ht,field_names[i][num_fields+j]);
2388
drizzle_free_result(fields);
2368
fields= drizzleclient_store_result(&drizzle);
2371
while ((sql_field=drizzleclient_fetch_field(fields)))
2373
tmp_str=table_row[0];
2374
tmp_str.append(".");
2375
tmp_str.append(sql_field->name);
2376
tmp_str_lower= lower_string(tmp_str);
2377
completion_map[tmp_str_lower]= tmp_str;
2379
tmp_str=sql_field->name;
2380
tmp_str_lower= lower_string(tmp_str);
2381
completion_map[tmp_str_lower]= tmp_str;
2384
drizzleclient_free_result(fields);
2395
drizzle_free_result(tables);
2396
field_names[i]=0; // End pointer
2388
drizzleclient_free_result(tables);
2389
completion_iter= completion_map.begin();
2400
2392
/* for gnu readline */
2480
int drizzle_store_result_for_lazy(DRIZZLE_RES **result)
2472
int drizzleclient_store_result_for_lazy(DRIZZLE_RES **result)
2482
if ((*result=drizzle_store_result(&drizzle)))
2474
if ((*result=drizzleclient_store_result(&drizzle)))
2485
if (drizzle_error(&drizzle)[0])
2477
if (drizzleclient_error(&drizzle)[0])
2486
2478
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);
2594
com_help(string *buffer __attribute__((unused)),
2595
const char *line __attribute__((unused)))
2483
com_help(string *buffer, const char *)
2597
2485
register int i, j;
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);
2486
char buff[32], *end;
2606
2488
put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
2607
2489
if (!named_cmds)
2608
2490
put_info(_("Note that all text commands must be first on line and end with ';'"),INFO_INFO,0,0);
2609
2491
for (i = 0; commands[i].name; i++)
2611
end= my_stpcpy(buff, commands[i].name);
2493
end= strcpy(buff, commands[i].name);
2494
end+= strlen(commands[i].name);
2612
2495
for (j= (int)strlen(commands[i].name); j < 10; j++)
2613
end= my_stpcpy(end, " ");
2496
end= strcpy(end, " ")+1;
2614
2497
if (commands[i].func)
2615
2498
tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
2616
2499
commands[i].cmd_char, _(commands[i].doc));
2618
if (connected && drizzle_get_server_version(&drizzle) >= 40100)
2619
put_info(_("\nFor server side help, type 'help contents'\n"), INFO_INFO,0,0);
2501
tee_fprintf(stdout, "\n");
2625
com_clear(string *buffer,
2626
const char *line __attribute__((unused)))
2508
com_clear(string *buffer, const char *)
2628
2510
if (status.add_to_history)
2629
2511
fix_history(buffer);
2737
2618
print_table_data(result);
2739
2620
ngettext("%ld row in set","%ld rows in set",
2740
(long) drizzle_num_rows(result)),
2741
(long) drizzle_num_rows(result));
2621
(long) drizzleclient_num_rows(result)),
2622
(long) drizzleclient_num_rows(result));
2743
if (drizzle_errno(&drizzle))
2624
if (drizzleclient_errno(&drizzle))
2744
2625
error= put_error(&drizzle);
2747
else if (drizzle_affected_rows(&drizzle) == ~(uint64_t) 0)
2748
my_stpcpy(buff,_("Query OK"));
2628
else if (drizzleclient_affected_rows(&drizzle) == ~(uint64_t) 0)
2629
strcpy(buff,_("Query OK"));
2750
2631
sprintf(buff, ngettext("Query OK, %ld row affected",
2751
2632
"Query OK, %ld rows affected",
2752
(long) drizzle_affected_rows(&drizzle)),
2753
(long) drizzle_affected_rows(&drizzle));
2633
(long) drizzleclient_affected_rows(&drizzle)),
2634
(long) drizzleclient_affected_rows(&drizzle));
2755
2636
pos= strchr(buff, '\0');
2756
if ((warnings= drizzle_warning_count(&drizzle)))
2637
if ((warnings= drizzleclient_warning_count(&drizzle)))
2760
pos=int10_to_str(warnings, pos, 10);
2761
pos=my_stpcpy(pos, " warning");
2641
pos= int10_to_str(warnings, pos, 10);
2642
pos= strcpy(pos, " warning")+8;
2762
2643
if (warnings != 1)
2765
my_stpcpy(pos, time_buff);
2646
strcpy(pos, time_buff);
2766
2647
put_info(buff,INFO_RESULT,0,0);
2767
if (drizzle_info(&drizzle))
2768
put_info(drizzle_info(&drizzle),INFO_RESULT,0,0);
2648
if (drizzleclient_info(&drizzle))
2649
put_info(drizzleclient_info(&drizzle),INFO_RESULT,0,0);
2769
2650
put_info("",INFO_RESULT,0,0); // Empty row
2771
if (result && !drizzle_eof(result)) /* Something wrong when using quick */
2652
if (result && !drizzleclient_eof(result)) /* Something wrong when using quick */
2772
2653
error= put_error(&drizzle);
2773
2654
else if (unbuffered)
2774
2655
fflush(stdout);
2775
drizzle_free_result(result);
2776
} while (!(err= drizzle_next_result(&drizzle)));
2656
drizzleclient_free_result(result);
2657
} while (!(err= drizzleclient_next_result(&drizzle)));
2778
2659
error= put_error(&drizzle);
2971
2874
tee_puts((char*) separator.c_str(), PAGER);
2972
2875
if (column_names)
2974
drizzle_field_seek(result,0);
2877
drizzleclient_field_seek(result,0);
2975
2878
(void) tee_fputs("|", PAGER);
2976
for (uint off=0; (field = drizzle_fetch_field(result)) ; off++)
2879
for (uint32_t off=0; (field = drizzleclient_fetch_field(result)) ; off++)
2978
uint name_length= (uint) strlen(field->name);
2979
uint numcells= charset_info->cset->numcells(charset_info,
2881
uint32_t name_length= (uint32_t) strlen(field->name);
2882
uint32_t numcells= charset_info->cset->numcells(charset_info,
2981
2884
field->name + name_length);
2982
2885
uint32_t display_length= field->max_length + name_length - numcells;
2983
2886
tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
2984
2887
MAX_COLUMN_LENGTH),
2986
num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) ||
2889
num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) ||
2987
2890
(field->type == DRIZZLE_TYPE_NEWDECIMAL));
2989
2892
(void) tee_fputs("\n", PAGER);
2990
2893
tee_puts((char*) separator.c_str(), PAGER);
2993
while ((cur= drizzle_fetch_row(result)))
2896
while ((cur= drizzleclient_fetch_row(result)))
2995
2898
if (interrupted_query)
2997
uint32_t *lengths= drizzle_fetch_lengths(result);
2900
uint32_t *lengths= drizzleclient_fetch_lengths(result);
2998
2901
(void) tee_fputs("| ", PAGER);
2999
drizzle_field_seek(result, 0);
3000
for (uint off= 0; off < drizzle_num_fields(result); off++)
2902
drizzleclient_field_seek(result, 0);
2903
for (uint32_t off= 0; off < drizzleclient_num_fields(result); off++)
3002
2905
const char *buffer;
3004
uint field_max_length;
3005
uint visible_length;
2906
uint32_t data_length;
2907
uint32_t field_max_length;
2908
uint32_t visible_length;
2909
uint32_t extra_padding;
3008
2911
if (cur[off] == NULL)
3138
3041
print_table_data_vertically(DRIZZLE_RES *result)
3140
3043
DRIZZLE_ROW cur;
3044
uint32_t max_length=0;
3142
3045
DRIZZLE_FIELD *field;
3144
while ((field = drizzle_fetch_field(result)))
3047
while ((field = drizzleclient_fetch_field(result)))
3146
uint length= field->name_length;
3049
uint32_t length= field->name_length;
3147
3050
if (length > max_length)
3148
3051
max_length= length;
3149
3052
field->max_length=length;
3152
drizzle_field_seek(result,0);
3153
for (uint row_count=1; (cur= drizzle_fetch_row(result)); row_count++)
3055
drizzleclient_field_seek(result,0);
3056
for (uint32_t row_count=1; (cur= drizzleclient_fetch_row(result)); row_count++)
3155
3058
if (interrupted_query)
3157
drizzle_field_seek(result,0);
3060
drizzleclient_field_seek(result,0);
3158
3061
tee_fprintf(PAGER,
3159
3062
"*************************** %d. row ***************************\n", row_count);
3160
for (uint off=0; off < drizzle_num_fields(result); off++)
3063
for (uint32_t off=0; off < drizzleclient_num_fields(result); off++)
3162
field= drizzle_fetch_field(result);
3065
field= drizzleclient_fetch_field(result);
3163
3066
tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
3164
3067
tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3727
3629
sql_connect(char *host,char *database,char *user,char *password,
3733
drizzle_close(&drizzle);
3635
drizzleclient_close(&drizzle);
3735
drizzle_create(&drizzle);
3637
drizzleclient_create(&drizzle);
3736
3638
if (opt_connect_timeout)
3738
uint timeout=opt_connect_timeout;
3739
drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3640
uint32_t timeout=opt_connect_timeout;
3641
drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3740
3642
(char*) &timeout);
3742
3644
if (opt_compress)
3743
drizzle_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
3645
drizzleclient_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
3744
3646
if (opt_secure_auth)
3745
drizzle_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
3746
if (using_opt_local_infile)
3747
drizzle_options(&drizzle,DRIZZLE_OPT_LOCAL_INFILE, (char*) &opt_local_infile);
3647
drizzleclient_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
3748
3648
if (safe_updates)
3750
3650
char init_command[100];
3751
3651
sprintf(init_command,
3752
3652
"SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3753
",SQL_MAX_JOIN_SIZE=%"PRIu32,
3653
",MAX_JOIN_SIZE=%"PRIu32,
3754
3654
select_limit, max_join_size);
3755
drizzle_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3655
drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3757
if (!drizzle_connect(&drizzle, host, user, password,
3657
if (!drizzleclient_connect(&drizzle, host, user, password,
3758
3658
database, opt_drizzle_port, opt_drizzle_unix_port,
3759
3659
connect_flag | CLIENT_MULTI_STATEMENTS))
3762
(drizzle_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3763
drizzle_errno(&drizzle) != CR_CONNECTION_ERROR))
3662
(drizzleclient_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3663
drizzleclient_errno(&drizzle) != CR_CONNECTION_ERROR))
3765
3665
(void) put_error(&drizzle);
3766
3666
(void) fflush(stdout);
3822
3721
tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
3823
3722
tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
3824
3723
tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&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)))
3724
tee_fprintf(stdout, "Protocol version:\t%d\n", drizzleclient_get_proto_info(&drizzle));
3725
tee_fprintf(stdout, "Connection:\t\t%s\n", drizzleclient_get_host_info(&drizzle));
3726
if ((id= drizzleclient_insert_id(&drizzle)))
3828
3727
tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
3830
3729
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
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)))
3730
if (!drizzleclient_query(&drizzle,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
3731
(result=drizzleclient_use_result(&drizzle)))
3834
DRIZZLE_ROW cur=drizzle_fetch_row(result);
3733
DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
3837
3736
tee_fprintf(stdout, "Server characterset:\t%s\n", cur[2] ? cur[2] : "");
3868
3767
static const char *
3869
3768
server_version_string(DRIZZLE *con)
3871
static char buf[MAX_SERVER_VERSION_LENGTH] = "";
3770
static string buf("");
3771
static bool server_version_string_reserved= false;
3773
if (!server_version_string_reserved)
3775
buf.reserve(MAX_SERVER_VERSION_LENGTH);
3776
server_version_string_reserved= true;
3873
3778
/* Only one thread calls this, so no synchronization is needed */
3874
3779
if (buf[0] == '\0')
3877
3781
DRIZZLE_RES *result;
3879
bufp= my_stpncpy(buf, drizzle_get_server_info(con), sizeof buf);
3783
buf.append(drizzleclient_get_server_info(con));
3881
3785
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3882
if (!drizzle_query(con, "select @@version_comment limit 1") &&
3883
(result = drizzle_use_result(con)))
3786
if (!drizzleclient_query(con, "select @@version_comment limit 1") &&
3787
(result = drizzleclient_use_result(con)))
3885
DRIZZLE_ROW cur = drizzle_fetch_row(result);
3789
DRIZZLE_ROW cur = drizzleclient_fetch_row(result);
3886
3790
if (cur && cur[0])
3888
bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NULL);
3890
drizzle_free_result(result);
3795
drizzleclient_free_result(result);
3893
/* str*nmov doesn't guarantee NUL-termination */
3894
if (bufp == buf + sizeof buf)
3895
buf[sizeof buf - 1] = '\0';
3902
put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
3803
put_info(const char *str,INFO_TYPE info_type, uint32_t error, const char *sqlstate)
3904
3805
FILE *file= (info_type == INFO_ERROR ? stderr : stdout);
3905
3806
static int inited=0;
4058
3959
if (sec >= 3600.0*24)
4060
3961
tmp=(uint32_t) floor(sec/(3600.0*24));
4062
buff=int10_to_str((long) tmp, buff, 10);
4063
buff=my_stpcpy(buff,tmp > 1 ? " days " : " day ");
3962
sec-= 3600.0*24*tmp;
3963
buff= int10_to_str((long) tmp, buff, 10);
3966
buff= strcpy(buff," days ")+6;
3968
buff= strcpy(buff," day ")+5;
4065
3971
if (sec >= 3600.0)
4067
3973
tmp=(uint32_t) floor(sec/3600.0);
4068
3974
sec-=3600.0*tmp;
4069
3975
buff=int10_to_str((long) tmp, buff, 10);
4070
buff=my_stpcpy(buff,tmp > 1 ? " hours " : " hour ");
3978
buff= strcpy(buff, " hours ")+7;
3980
buff= strcpy(buff, " hour ")+6;
4072
3982
if (sec >= 60.0)
4074
3984
tmp=(uint32_t) floor(sec/60.0);
4076
3986
buff=int10_to_str((long) tmp, buff, 10);
4077
buff=my_stpcpy(buff," min ");
3987
buff= strcpy(buff," min ")+5;
4079
3989
if (part_second)
4080
3990
sprintf(buff,"%.2f sec",sec);
4290
4197
free(part_username);
4292
4199
DRIZZLE_RES *result;
4293
if (!drizzle_query(&drizzle,"select USER()") &&
4294
(result=drizzle_use_result(&drizzle)))
4200
if (!drizzleclient_query(&drizzle,"select USER()") &&
4201
(result=drizzleclient_use_result(&drizzle)))
4296
DRIZZLE_ROW cur=drizzle_fetch_row(result);
4203
DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
4297
4204
full_username= strdup(cur[0]);
4298
4205
part_username= strdup(strtok(cur[0],"@"));
4299
(void) drizzle_fetch_row(result); // Read eof
4206
(void) drizzleclient_fetch_row(result); // Read eof
4303
static int com_prompt(string *buffer __attribute__((unused)),
4210
static int com_prompt(string *, const char *line)
4306
char *ptr=strchr(line, ' ');
4308
free(current_prompt);
4309
current_prompt= strdup(ptr ? ptr+1 : default_prompt);
4212
const char *ptr=strchr(line, ' ');
4311
4214
tee_fprintf(stdout, "Returning to default PROMPT of %s\n",
4312
4215
default_prompt);
4217
char * tmpptr= strdup(ptr ? ptr+1 : default_prompt);
4219
tee_fprintf(stdout, "Memory allocation error. Not changing prompt\n");
4222
free(current_prompt);
4223
current_prompt= tmpptr;
4314
4224
tee_fprintf(stdout, "PROMPT set to '%s'\n", current_prompt);