148
141
uint32_t query_start_line;
150
LineBuffer *line_buff;
143
LINE_BUFFER *line_buff;
151
144
bool batch,add_to_history;
155
static map<string, string>::iterator completion_iter;
156
static map<string, string>::iterator completion_end;
157
static map<string, string> completion_map;
158
static string completion_string;
160
149
static char **defaults_argv;
162
151
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
163
152
typedef enum enum_info_type INFO_TYPE;
165
static drizzle_st drizzle; /* The library handle */
166
static drizzle_con_st con; /* The connection */
167
static bool ignore_errors= false, quick= false,
168
connected= false, opt_raw_data= false, unbuffered= false,
169
output_tables= false, opt_rehash= true, skip_updates= false,
170
safe_updates= false, one_database= false,
171
opt_compress= false, opt_shutdown= false, opt_ping= false,
172
vertical= false, line_numbers= true, column_names= true,
173
opt_nopager= true, opt_outfile= false, named_cmds= false,
174
tty_password= false, opt_nobeep= false, opt_reconnect= true,
175
default_charset_used= false, opt_secure_auth= false,
176
default_pager_set= false, opt_sigint_ignore= false,
177
auto_vertical_output= false,
178
show_warnings= false, executing_query= false, interrupted_query= false;
179
static uint32_t show_progress_size= 0;
154
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;
180
166
static bool debug_info_flag, debug_check_flag;
181
167
static bool column_types_flag;
182
static bool preserve_comments= false;
183
static uint32_t opt_max_input_line, opt_drizzle_port= 0;
184
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
185
static uint32_t my_end_arg;
186
static char * opt_drizzle_unix_port= NULL;
187
static drizzle_capabilities_t connect_flag= DRIZZLE_CAPABILITIES_NONE;
188
static char *current_host, *current_db, *current_user= NULL,
189
*opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
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;
190
176
static char *histfile;
191
177
static char *histfile_tmp;
192
178
static string *glob_buffer;
193
179
static string *processed_prompt= NULL;
194
180
static char *default_prompt= NULL;
195
static char *full_username= NULL,*part_username= NULL;
181
static char *full_username=0,*part_username=0;
196
182
static STATUS status;
197
183
static uint32_t select_limit;
198
184
static uint32_t max_join_size;
199
185
static uint32_t opt_connect_timeout= 0;
186
static char drizzle_charsets_dir[FN_REFLEN+1];
200
187
// TODO: Need to i18n these
201
static const char *day_names[]= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
202
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",
203
190
"Aug","Sep","Oct","Nov","Dec"};
204
191
static char default_pager[FN_REFLEN];
205
192
static char pager[FN_REFLEN], outfile[FN_REFLEN];
206
193
static FILE *PAGER, *OUTFILE;
207
static uint32_t prompt_counter;
194
static MEM_ROOT hash_mem_root;
195
static uint prompt_counter;
208
196
static char delimiter[16]= DEFAULT_DELIMITER;
209
static uint32_t delimiter_length= 1;
197
static uint delimiter_length= 1;
210
198
unsigned short terminal_width= 80;
212
200
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
214
int drizzleclient_real_query_for_lazy(const char *buf, int length,
215
drizzle_result_st *result,
216
uint32_t *error_code);
217
int drizzleclient_store_result_for_lazy(drizzle_result_st *result);
202
int drizzle_real_query_for_lazy(const char *buf, int length);
203
int drizzle_store_result_for_lazy(DRIZZLE_RES **result);
220
206
void tee_fprintf(FILE *file, const char *fmt, ...);
272
258
static COMMANDS commands[] = {
273
{ "?", '?', com_help, 0, N_("Synonym for `help'.") },
259
{ "?", '?', com_help, 1, N_("Synonym for `help'.") },
274
260
{ "clear", 'c', com_clear, 0, N_("Clear command.")},
275
261
{ "connect",'r', com_connect,1,
276
N_("Reconnect to the server. Optional arguments are db and host.")},
262
N_("Reconnect to the server. Optional arguments are db and host." }),
277
263
{ "delimiter", 'd', com_delimiter, 1,
278
264
N_("Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.") },
279
265
{ "ego", 'G', com_ego, 0,
280
266
N_("Send command to drizzle server, display result vertically.")},
281
267
{ "exit", 'q', com_quit, 0, N_("Exit drizzle. Same as quit.")},
282
268
{ "go", 'g', com_go, 0, N_("Send command to drizzle server.") },
283
{ "help", 'h', com_help, 0, N_("Display this help.") },
269
{ "help", 'h', com_help, 1, N_("Display this help.") },
284
270
{ "nopager",'n', com_nopager,0, N_("Disable pager, print to stdout.") },
285
271
{ "notee", 't', com_notee, 0, N_("Don't write into outfile.") },
286
272
{ "pager", 'P', com_pager, 1,
793
789
{ "ATAN2", 0, 0, 0, ""},
794
790
{ "BENCHMARK", 0, 0, 0, ""},
795
791
{ "BIN", 0, 0, 0, ""},
792
{ "BIT_COUNT", 0, 0, 0, ""},
796
793
{ "BIT_OR", 0, 0, 0, ""},
797
794
{ "BIT_AND", 0, 0, 0, ""},
798
795
{ "BIT_XOR", 0, 0, 0, ""},
799
796
{ "CAST", 0, 0, 0, ""},
800
797
{ "CEIL", 0, 0, 0, ""},
801
798
{ "CEILING", 0, 0, 0, ""},
799
{ "BIT_LENGTH", 0, 0, 0, ""},
802
800
{ "CENTROID", 0, 0, 0, ""},
803
801
{ "CHAR_LENGTH", 0, 0, 0, ""},
804
802
{ "CHARACTER_LENGTH", 0, 0, 0, ""},
962
963
{ "SUBSTR", 0, 0, 0, ""},
963
964
{ "SUBSTRING", 0, 0, 0, ""},
964
965
{ "SUBSTRING_INDEX", 0, 0, 0, ""},
966
{ "SUBTIME", 0, 0, 0, ""},
965
967
{ "SUM", 0, 0, 0, ""},
966
968
{ "SYSDATE", 0, 0, 0, ""},
967
969
{ "SYSTEM_USER", 0, 0, 0, ""},
968
970
{ "TAN", 0, 0, 0, ""},
969
971
{ "TIME_FORMAT", 0, 0, 0, ""},
972
{ "TIME_TO_SEC", 0, 0, 0, ""},
973
{ "TIMEDIFF", 0, 0, 0, ""},
970
974
{ "TO_DAYS", 0, 0, 0, ""},
971
975
{ "TOUCHES", 0, 0, 0, ""},
972
976
{ "TRIM", 0, 0, 0, ""},
1003
1007
static bool add_line(string *buffer,char *line,char *in_string,
1004
1008
bool *ml_comment);
1005
1009
static void remove_cntrl(string *buffer);
1006
static void print_table_data(drizzle_result_st *result);
1007
static void print_tab_data(drizzle_result_st *result);
1008
static void print_table_data_vertically(drizzle_result_st *result);
1009
static void print_warnings(uint32_t error_code);
1010
static void print_table_data(DRIZZLE_RES *result);
1011
static void print_tab_data(DRIZZLE_RES *result);
1012
static void print_table_data_vertically(DRIZZLE_RES *result);
1013
static void print_warnings(void);
1010
1014
static uint32_t start_timer(void);
1011
1015
static void end_timer(uint32_t start_time,char *buff);
1012
1016
static void drizzle_end_timer(uint32_t start_time,char *buff);
1013
1017
static void nice_time(double sec,char *buff,bool part_second);
1014
extern "C" void drizzle_end(int sig);
1015
extern "C" void handle_sigint(int sig);
1018
extern RETSIGTYPE drizzle_end(int sig);
1019
extern RETSIGTYPE handle_sigint(int sig);
1016
1020
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1017
static void window_resize(int sig);
1021
static RETSIGTYPE window_resize(int sig);
1020
static inline int is_prefix(const char *s, const char *t)
1023
if (*s++ != *t++) return 0;
1024
return 1; /* WRONG */
1028
Shutdown the server that we are currently connected to.
1035
static bool server_shutdown(void)
1037
drizzle_result_st result;
1038
drizzle_return_t ret;
1042
printf("shutting down drizzled");
1043
if (opt_drizzle_port > 0)
1044
printf(" on port %d", opt_drizzle_port);
1048
if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
1049
&ret) == NULL || ret != DRIZZLE_RETURN_OK)
1051
if (ret == DRIZZLE_RETURN_ERROR_CODE)
1053
fprintf(stderr, "shutdown failed; error: '%s'",
1054
drizzle_result_error(&result));
1055
drizzle_result_free(&result);
1059
fprintf(stderr, "shutdown failed; error: '%s'",
1060
drizzle_con_error(&con));
1065
drizzle_result_free(&result);
1074
Ping the server that we are currently connected to.
1081
static bool server_ping(void)
1083
drizzle_result_st result;
1084
drizzle_return_t ret;
1086
if (drizzle_ping(&con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
1089
printf("drizzled is alive\n");
1093
if (ret == DRIZZLE_RETURN_ERROR_CODE)
1095
fprintf(stderr, "ping failed; error: '%s'",
1096
drizzle_result_error(&result));
1097
drizzle_result_free(&result);
1101
fprintf(stderr, "drizzled won't answer to ping, error: '%s'",
1102
drizzle_con_error(&con));
1106
drizzle_result_free(&result);
1111
Execute command(s) specified by the user.
1113
@param error error status of command execution.
1114
If an error had occurred, this variable will be set
1115
to 1 whereas on success, it shall be set to 0. This
1116
value will be supplied to the exit() function used
1120
false no commands were executed
1122
true at least one command was executed
1124
static bool execute_commands(int *error)
1126
bool executed= false;
1131
if (server_ping() == false)
1138
if (server_shutdown() == false)
1145
1024
int main(int argc,char *argv[])
1147
1028
#if defined(ENABLE_NLS)
1148
1029
# if defined(HAVE_LOCALE_H)
1149
1030
setlocale(LC_ALL, "");
1369
1224
If query is in process, kill query
1370
1225
no query in process, terminate like previous behavior
1373
void handle_sigint(int sig)
1227
RETSIGTYPE handle_sigint(int sig)
1375
1229
char kill_buffer[40];
1376
drizzle_con_st kill_drizzle;
1377
drizzle_result_st res;
1378
drizzle_return_t ret;
1230
DRIZZLE *kill_drizzle= NULL;
1380
1232
/* terminate if no query being executed, or we already tried interrupting */
1381
1233
if (!executing_query || interrupted_query) {
1385
if (drizzle_con_add_tcp(&drizzle, &kill_drizzle, current_host,
1386
opt_drizzle_port, current_user, opt_password, NULL,
1387
DRIZZLE_CON_NONE) == NULL)
1237
kill_drizzle= drizzle_create(kill_drizzle);
1238
if (!drizzle_connect(kill_drizzle,current_host, current_user, opt_password,
1239
"", opt_drizzle_port, opt_drizzle_unix_port,0))
1392
1244
/* kill_buffer is always big enough because max length of %lu is 15 */
1393
sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1394
drizzle_con_thread_id(&con));
1396
if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
1397
drizzle_result_free(&res);
1399
drizzle_con_free(&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);
1400
1248
tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1402
1250
interrupted_query= 1;
1429
1277
(char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
1431
1279
{"no-auto-rehash", 'A',
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."),
1280
N_("No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of DRIZZLE and disables rehashing on reconnect. WARNING: options deprecated; use --disable-auto-rehash instead."),
1433
1281
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1434
1282
{"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT,
1435
1283
N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
1436
1284
(char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1438
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},
1439
1290
{"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
1440
1291
(char**) &column_types_flag, (char**) &column_types_flag,
1441
1292
0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1509
1360
{"no-pager", OPT_NOPAGER,
1510
1361
N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
1511
1362
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1513
1364
N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1514
1365
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1515
{"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, ")
1516
1367
N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
1517
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},
1518
1370
{"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
1519
1371
(char**) ¤t_prompt, (char**) ¤t_prompt, 0, GET_STR_ALLOC,
1520
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},
1522
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."),
1523
1377
(char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1557
1411
{"connect_timeout", OPT_CONNECT_TIMEOUT,
1558
1412
N_("Number of seconds before connection timeout."),
1559
1413
(char**) &opt_connect_timeout,
1560
(char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 0, 0, 3600*12, 0,
1414
(char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 0, 0, 3600*12, 0,
1562
{"max_input_line", OPT_MAX_INPUT_LINE,
1563
N_("Max length of input line"),
1564
(char**) &opt_max_input_line, (char**) &opt_max_input_line, 0,
1565
GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1416
{"max_allowed_packet", OPT_MAX_ALLOWED_PACKET,
1417
N_("Max packet length to send to, or receive from server"),
1418
(char**) &opt_max_allowed_packet, (char**) &opt_max_allowed_packet, 0,
1419
GET_ULONG, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1566
1420
(int64_t) 2*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
1421
{"net_buffer_length", OPT_NET_BUFFER_LENGTH,
1422
N_("Buffer for TCP/IP and socket communication"),
1423
(char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0, GET_ULONG,
1424
REQUIRED_ARG, 16384, 1024, 512*1024*1024L, MALLOC_OVERHEAD, 1024, 0},
1567
1425
{"select_limit", OPT_SELECT_LIMIT,
1568
1426
N_("Automatic limit for SELECT when using --safe-updates"),
1569
1427
(char**) &select_limit,
1570
(char**) &select_limit, 0, GET_UINT32, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1428
(char**) &select_limit, 0, GET_ULONG, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1572
1430
{"max_join_size", OPT_MAX_JOIN_SIZE,
1573
1431
N_("Automatic limit for rows in a join when using --safe-updates"),
1574
1432
(char**) &max_join_size,
1575
(char**) &max_join_size, 0, GET_UINT32, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1433
(char**) &max_join_size, 0, GET_ULONG, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1577
1435
{"secure-auth", OPT_SECURE_AUTH, N_("Refuse client connecting to server if it uses old (pre-4.1.1) protocol"), (char**) &opt_secure_auth,
1578
1436
(char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1579
1437
{"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1580
1438
(char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1581
1439
0, 0, 0, 0, 0, 0},
1582
{"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1583
(char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
1585
{"ping", OPT_PING, N_("Ping the server to check if it's alive."),
1586
(char**) &opt_ping, (char**) &opt_ping, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1587
1440
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1593
1446
const char* readline= "readline";
1595
printf(_("%s Ver %s Distrib %s, for %s-%s (%s) using %s %s\n"),
1596
my_progname, VER.c_str(), drizzle_version(),
1597
HOST_VENDOR, HOST_OS, HOST_CPU,
1448
printf(_("%s Ver %s Distrib %s, for %s (%s) using %s %s\n"),
1449
my_progname, VER, drizzle_get_client_info(),
1450
SYSTEM_TYPE, MACHINE_TYPE,
1598
1451
readline, rl_library_version);
1602
printf(_("Copyright (C) 2008 Sun Microsystems\n"
1603
"This software comes with ABSOLUTELY NO WARRANTY. "
1604
"This is free software,\n"
1605
"and you are welcome to modify and redistribute it "
1606
"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"));
1607
1459
printf(_("Usage: %s [OPTIONS] [database]\n"), my_progname);
1608
1460
my_print_help(my_long_options);
1609
print_defaults("drizzle", load_default_groups);
1461
print_defaults("my", load_default_groups);
1610
1462
my_print_variables(my_long_options);
1615
get_one_option(int optid, const struct my_option *, char *argument)
1467
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
1617
char *endchar= NULL;
1618
uint64_t temp_drizzle_port= 0;
1620
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;
1621
1475
case OPT_DEFAULT_CHARSET:
1622
1476
default_charset_used= 1;
1624
1478
case OPT_DELIMITER:
1625
1479
if (argument == disabled_my_option)
1627
strcpy(delimiter, DEFAULT_DELIMITER);
1481
my_stpcpy(delimiter, DEFAULT_DELIMITER);
1631
1485
/* Check that delimiter does not contain a backslash */
1632
1486
if (!strstr(argument, "\\"))
1634
strncpy(delimiter, argument, sizeof(delimiter) - 1);
1488
strmake(delimiter, argument, sizeof(delimiter) - 1);
1638
1492
put_info(_("DELIMITER cannot contain a backslash character"),
1639
1493
INFO_ERROR,0,0);
1643
delimiter_length= (uint32_t)strlen(delimiter);
1497
delimiter_length= (uint)strlen(delimiter);
1644
1498
delimiter_str= delimiter;
1500
case OPT_LOCAL_INFILE:
1501
using_opt_local_infile=1;
1647
1504
if (argument == disabled_my_option)
2364
2194
return (char**) 0;
2367
inline string lower_string(const string &from_string)
2369
string to_string= from_string;
2370
transform(to_string.begin(), to_string.end(),
2371
to_string.begin(), ::tolower);
2374
inline string lower_string(const char * from_string)
2376
string to_string= from_string;
2377
return lower_string(to_string);
2381
class CompletionMatch :
2382
public unary_function<const string&, bool>
2387
CompletionMatch(string text) : match_text(text) {}
2388
inline bool operator() (const pair<string,string> &match_against) const
2391
lower_string(match_against.first.substr(0,match_text.size()));
2392
return match_func(sub_match,match_text);
2399
char *new_command_generator(const char *text, int state)
2198
static char *new_command_generator(const char *text,int state)
2404
completion_string= lower_string(text);
2405
if (completion_string.size() == 0)
2407
completion_iter= completion_map.begin();
2408
completion_end= completion_map.end();
2412
completion_iter= find_if(completion_map.begin(), completion_map.end(),
2413
CompletionMatch<equal_to<string> >(completion_string));
2414
completion_end= find_if(completion_iter, completion_map.end(),
2415
CompletionMatch<not_equal_to<string> >(completion_string));
2418
if (completion_iter == completion_end || (size_t)state > completion_map.size())
2420
char *result= (char *)malloc((*completion_iter).second.size()+1);
2421
strcpy(result, (*completion_iter).second.c_str());
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];
2426
2277
/* Build up the completion hash */
2428
2279
static void build_completion_hash(bool rehash, bool write_info)
2430
2281
COMMANDS *cmd=commands;
2431
drizzle_return_t ret;
2432
drizzle_result_st databases,tables,fields;
2433
drizzle_row_t database_row,table_row;
2434
drizzle_column_st *sql_field;
2435
string tmp_str, tmp_str_lower;
2282
DRIZZLE_RES *databases=0,*tables=0;
2283
DRIZZLE_RES *fields;
2284
static char ***field_names= 0;
2285
DRIZZLE_ROW database_row,table_row;
2286
DRIZZLE_FIELD *sql_field;
2287
char buf[NAME_LEN*2+2]; // table name plus field name plus 2
2437
2291
if (status.batch || quick || !current_db)
2438
2292
return; // We don't need completion in batches
2442
completion_map.clear();
2296
/* Free old used memory */
2299
completion_hash_clean(&ht);
2300
free_root(&hash_mem_root,MYF(0));
2444
2302
/* hash this file's known subset of SQL commands */
2445
2303
while (cmd->name) {
2447
tmp_str_lower= lower_string(tmp_str);
2448
completion_map[tmp_str_lower]= tmp_str;
2304
add_word(&ht,(char*) cmd->name);
2452
2308
/* hash Drizzle functions (to be implemented) */
2454
2310
/* hash all database names */
2455
if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
2311
if (drizzle_query(&drizzle,"show databases") == 0)
2457
if (ret == DRIZZLE_RETURN_OK)
2313
if (!(databases = drizzle_store_result(&drizzle)))
2314
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2459
if (drizzle_result_buffer(&databases) != DRIZZLE_RETURN_OK)
2460
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2317
while ((database_row=drizzle_fetch_row(databases)))
2463
while ((database_row=drizzle_row_next(&databases)))
2465
tmp_str= database_row[0];
2466
tmp_str_lower= lower_string(tmp_str);
2467
completion_map[tmp_str_lower]= tmp_str;
2319
char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
2321
add_word(&ht,(char*) str);
2323
drizzle_free_result(databases);
2472
drizzle_result_free(&databases);
2475
2326
/* hash all table names */
2476
if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
2327
if (drizzle_query(&drizzle,"show tables")==0)
2478
if (ret != DRIZZLE_RETURN_OK)
2480
drizzle_result_free(&tables);
2484
if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
2329
if (!(tables = drizzle_store_result(&drizzle)))
2485
2330
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2488
if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
2333
if (drizzle_num_rows(tables) > 0 && !opt_silent && write_info)
2490
2335
tee_fprintf(stdout, _("\
2491
2336
Reading table information for completion of table and column names\n \
2492
2337
You can turn off this feature to get a quicker startup with -A\n\n"));
2494
while ((table_row=drizzle_row_next(&tables)))
2339
while ((table_row=drizzle_fetch_row(tables)))
2496
tmp_str= table_row[0];
2497
tmp_str_lower= lower_string(tmp_str);
2498
completion_map[tmp_str_lower]= tmp_str;
2341
char *str=strdup_root(&hash_mem_root, (char*) table_row[0]);
2343
!completion_hash_exists(&ht,(char*) str, (uint) strlen(str)))
2505
2349
/* hash all field names, both with the table prefix and without it */
2506
if (drizzle_result_row_count(&tables) == 0)
2508
drizzle_result_free(&tables);
2512
drizzle_row_seek(&tables, 0);
2514
while ((table_row=drizzle_row_next(&tables)))
2518
query.append("show fields in '");
2519
query.append(table_row[0]);
2522
if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2350
if (!tables) /* no tables */
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)))
2525
if (ret == DRIZZLE_RETURN_OK &&
2526
drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2528
while ((sql_field=drizzle_column_next(&fields)))
2530
tmp_str=table_row[0];
2531
tmp_str.append(".");
2532
tmp_str.append(drizzle_column_name(sql_field));
2533
tmp_str_lower= lower_string(tmp_str);
2534
completion_map[tmp_str_lower]= tmp_str;
2536
tmp_str=drizzle_column_name(sql_field);
2537
tmp_str_lower= lower_string(tmp_str);
2538
completion_map[tmp_str_lower]= tmp_str;
2541
drizzle_result_free(&fields);
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);
2544
drizzle_result_free(&tables);
2545
completion_iter= completion_map.begin();
2395
drizzle_free_result(tables);
2396
field_names[i]=0; // End pointer
2548
2400
/* for gnu readline */
2611
2461
The different commands
2612
2462
***************************************************************************/
2614
int drizzleclient_real_query_for_lazy(const char *buf, int length,
2615
drizzle_result_st *result,
2616
uint32_t *error_code)
2464
int drizzle_real_query_for_lazy(const char *buf, int length)
2618
drizzle_return_t ret;
2620
for (uint32_t retry=0;; retry++)
2466
for (uint retry=0;; retry++)
2623
if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2624
ret == DRIZZLE_RETURN_OK)
2469
if (!drizzle_real_query(&drizzle,buf,length))
2628
error= put_error(&con, result);
2630
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2632
*error_code= drizzle_result_error_code(result);
2633
drizzle_result_free(result);
2636
if (ret != DRIZZLE_RETURN_SERVER_GONE || retry > 1 ||
2471
error= put_error(&drizzle);
2472
if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR || retry > 1 ||
2637
2473
!opt_reconnect)
2642
2475
if (reconnect())
2647
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
2480
int drizzle_store_result_for_lazy(DRIZZLE_RES **result)
2649
if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
2482
if ((*result=drizzle_store_result(&drizzle)))
2652
if (drizzle_con_error(&con)[0])
2653
return put_error(&con, result);
2485
if (drizzle_error(&drizzle)[0])
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);
2658
com_help(string *buffer, const char *)
2594
com_help(string *buffer __attribute__((unused)),
2595
const char *line __attribute__((unused)))
2660
2597
register int i, j;
2661
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);
2663
2606
put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
2664
2607
if (!named_cmds)
2665
2608
put_info(_("Note that all text commands must be first on line and end with ';'"),INFO_INFO,0,0);
2666
2609
for (i = 0; commands[i].name; i++)
2668
end= strcpy(buff, commands[i].name);
2669
end+= strlen(commands[i].name);
2611
end= my_stpcpy(buff, commands[i].name);
2670
2612
for (j= (int)strlen(commands[i].name); j < 10; j++)
2671
end= strcpy(end, " ")+1;
2613
end= my_stpcpy(end, " ");
2672
2614
if (commands[i].func)
2673
2615
tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
2674
2616
commands[i].cmd_char, _(commands[i].doc));
2676
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);
2683
com_clear(string *buffer, const char *)
2625
com_clear(string *buffer,
2626
const char *line __attribute__((unused)))
2685
2628
if (status.add_to_history)
2686
2629
fix_history(buffer);
2777
2719
time_buff[0]= '\0';
2779
2721
/* Every branch must truncate buff . */
2780
if (drizzle_result_column_count(&result) > 0)
2782
if (!quick && drizzle_result_row_count(&result) == 0 &&
2724
if (!drizzle_num_rows(result) && ! quick && !column_types_flag)
2785
strcpy(buff, _("Empty set"));
2726
my_stpcpy(buff, _("Empty set"));
2790
2731
if (vertical || (auto_vertical_output &&
2791
(terminal_width < get_result_width(&result))))
2792
print_table_data_vertically(&result);
2732
(terminal_width < get_result_width(result))))
2733
print_table_data_vertically(result);
2793
2734
else if (opt_silent && verbose <= 2 && !output_tables)
2794
print_tab_data(&result);
2735
print_tab_data(result);
2796
print_table_data(&result);
2737
print_table_data(result);
2798
2739
ngettext("%ld row in set","%ld rows in set",
2799
(long) drizzle_result_row_count(&result)),
2800
(long) drizzle_result_row_count(&result));
2740
(long) drizzle_num_rows(result)),
2741
(long) drizzle_num_rows(result));
2802
if (drizzle_result_error_code(&result))
2803
error= put_error(&con, &result);
2743
if (drizzle_errno(&drizzle))
2744
error= put_error(&drizzle);
2806
else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2807
strcpy(buff,_("Query OK"));
2747
else if (drizzle_affected_rows(&drizzle) == ~(uint64_t) 0)
2748
my_stpcpy(buff,_("Query OK"));
2809
2750
sprintf(buff, ngettext("Query OK, %ld row affected",
2810
2751
"Query OK, %ld rows affected",
2811
(long) drizzle_result_affected_rows(&result)),
2812
(long) drizzle_result_affected_rows(&result));
2752
(long) drizzle_affected_rows(&drizzle)),
2753
(long) drizzle_affected_rows(&drizzle));
2814
2755
pos= strchr(buff, '\0');
2815
if ((warnings= drizzle_result_warning_count(&result)))
2756
if ((warnings= drizzle_warning_count(&drizzle)))
2819
char warnings_buff[20];
2820
memset(warnings_buff,0,20);
2821
sprintf(warnings_buff, "%d", warnings);
2822
strcpy(pos, warnings_buff);
2823
pos+= strlen(warnings_buff);
2824
pos= strcpy(pos, " warning")+8;
2760
pos=int10_to_str(warnings, pos, 10);
2761
pos=my_stpcpy(pos, " warning");
2825
2762
if (warnings != 1)
2828
strcpy(pos, time_buff);
2765
my_stpcpy(pos, time_buff);
2829
2766
put_info(buff,INFO_RESULT,0,0);
2830
if (strcmp(drizzle_result_info(&result), ""))
2831
put_info(drizzle_result_info(&result),INFO_RESULT,0,0);
2767
if (drizzle_info(&drizzle))
2768
put_info(drizzle_info(&drizzle),INFO_RESULT,0,0);
2832
2769
put_info("",INFO_RESULT,0,0); // Empty row
2771
if (result && !drizzle_eof(result)) /* Something wrong when using quick */
2772
error= put_error(&drizzle);
2773
else if (unbuffered)
2835
2774
fflush(stdout);
2836
drizzle_result_free(&result);
2838
if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2840
if (drizzle_result_read(&con, &result, &ret) == NULL ||
2841
ret != DRIZZLE_RETURN_OK)
2843
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2845
error_code= drizzle_result_error_code(&result);
2846
drizzle_result_free(&result);
2849
error= put_error(&con, NULL);
2854
} while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2775
drizzle_free_result(result);
2776
} while (!(err= drizzle_next_result(&drizzle)));
2856
error= put_error(&con, NULL);
2778
error= put_error(&drizzle);
2860
2782
/* Show warnings if any or error occured */
2861
2783
if (show_warnings == 1 && (warnings >= 1 || error))
2862
print_warnings(error_code);
2864
2786
if (!error && !status.batch &&
2865
drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2787
(drizzle.server_status & SERVER_STATUS_DB_DROPPED))
2867
2788
get_current_db();
2870
2790
executing_query= 0;
2871
2791
return error; /* New command follows */
2936
static const char *fieldtype2str(drizzle_column_type_t type)
2855
static const char *fieldtype2str(enum enum_field_types type)
2938
2857
switch (type) {
2939
case DRIZZLE_COLUMN_TYPE_BLOB: return "BLOB";
2940
case DRIZZLE_COLUMN_TYPE_DATE: return "DATE";
2941
case DRIZZLE_COLUMN_TYPE_DATETIME: return "DATETIME";
2942
case DRIZZLE_COLUMN_TYPE_NEWDECIMAL: return "DECIMAL";
2943
case DRIZZLE_COLUMN_TYPE_DOUBLE: return "DOUBLE";
2944
case DRIZZLE_COLUMN_TYPE_ENUM: return "ENUM";
2945
case DRIZZLE_COLUMN_TYPE_LONG: return "LONG";
2946
case DRIZZLE_COLUMN_TYPE_LONGLONG: return "LONGLONG";
2947
case DRIZZLE_COLUMN_TYPE_NULL: return "NULL";
2948
case DRIZZLE_COLUMN_TYPE_TIMESTAMP: return "TIMESTAMP";
2949
case DRIZZLE_COLUMN_TYPE_TINY: return "TINY";
2950
case DRIZZLE_COLUMN_TYPE_VIRTUAL: return "VIRTUAL";
2858
case DRIZZLE_TYPE_BLOB: return "BLOB";
2859
case DRIZZLE_TYPE_NEWDATE: return "DATE";
2860
case DRIZZLE_TYPE_DATETIME: return "DATETIME";
2861
case DRIZZLE_TYPE_NEWDECIMAL: return "DECIMAL";
2862
case DRIZZLE_TYPE_DOUBLE: return "DOUBLE";
2863
case DRIZZLE_TYPE_ENUM: return "ENUM";
2864
case DRIZZLE_TYPE_LONG: return "LONG";
2865
case DRIZZLE_TYPE_LONGLONG: return "LONGLONG";
2866
case DRIZZLE_TYPE_NULL: return "NULL";
2867
case DRIZZLE_TYPE_TIME: return "TIME";
2868
case DRIZZLE_TYPE_TIMESTAMP: return "TIMESTAMP";
2869
case DRIZZLE_TYPE_TINY: return "TINY";
2951
2870
default: return "?-unknown-?";
2955
static char *fieldflags2str(uint32_t f) {
2874
static char *fieldflags2str(uint f) {
2956
2875
static char buf[1024];
2959
2878
#define ff2s_check_flag(X) \
2960
if (f & DRIZZLE_COLUMN_FLAGS_ ## X) { s=strcpy(s, # X " ")+strlen(# X " "); \
2961
f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
2879
if (f & X ## _FLAG) { s=my_stpcpy(s, # X " "); f &= ~ X ## _FLAG; }
2962
2880
ff2s_check_flag(NOT_NULL);
2963
2881
ff2s_check_flag(PRI_KEY);
2964
2882
ff2s_check_flag(UNIQUE_KEY);
3003
2921
"Decimals: %u\n"
3004
2922
"Flags: %s\n\n",
3006
drizzle_column_name(field), drizzle_column_catalog(field),
3007
drizzle_column_db(field), drizzle_column_table(field),
3008
drizzle_column_orig_table(field),
3009
fieldtype2str(drizzle_column_type(field)),
3010
get_charset_name(drizzle_column_charset(field)),
3011
drizzle_column_charset(field), drizzle_column_size(field),
3012
drizzle_column_max_size(field), drizzle_column_decimals(field),
3013
fieldflags2str(drizzle_column_flags(field)));
2924
field->name, field->catalog, field->db, field->table,
2925
field->org_table, fieldtype2str(field->type),
2926
get_charset_name(field->charsetnr), field->charsetnr,
2927
field->length, field->max_length, field->decimals,
2928
fieldflags2str(field->flags));
3015
2930
tee_puts("", PAGER);
3020
print_table_data(drizzle_result_st *result)
2935
print_table_data(DRIZZLE_RES *result)
3023
drizzle_return_t ret;
3024
drizzle_column_st *field;
2938
DRIZZLE_FIELD *field;
3026
2940
string separator;
3028
2942
separator.reserve(256);
3030
num_flag=(bool*) malloc(sizeof(bool)*drizzle_result_column_count(result));
2944
num_flag=(bool*) my_malloc(sizeof(bool)*drizzle_num_fields(result),
3031
2946
if (column_types_flag)
3033
2948
print_field_types(result);
3034
if (!drizzle_result_row_count(result))
2949
if (!drizzle_num_rows(result))
3036
drizzle_column_seek(result,0);
2951
drizzle_field_seek(result,0);
3038
2953
separator.append("+");
3039
while ((field = drizzle_column_next(result)))
2954
while ((field = drizzle_fetch_field(result)))
3041
uint32_t x, length= 0;
3045
uint32_t name_length= strlen(drizzle_column_name(field));
3047
/* Check if the max_byte value is really the maximum in terms
3048
of visual length since multibyte characters can affect the
3049
length of the separator. */
3050
length= charset_info->cset->numcells(charset_info,
3051
drizzle_column_name(field),
3052
drizzle_column_name(field) +
3055
if (name_length == drizzle_column_max_size(field))
3057
if (length < drizzle_column_max_size(field))
3058
drizzle_column_set_max_size(field, length);
3062
length= name_length;
2956
uint32_t length= column_names ? field->name_length : 0;
3067
length=max(length,drizzle_column_size(field));
2958
length=max(length,field->length);
3069
length=max(length,(uint32_t)drizzle_column_max_size(field));
3071
!(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
2960
length=max(length,field->max_length);
2961
if (length < 4 && !(field->flags & NOT_NULL_FLAG))
3073
2962
// Room for "NULL"
3076
drizzle_column_set_max_size(field, length);
2964
field->max_length=length;
3078
2966
for (x=0; x< (length+2); x++)
3079
2967
separator.append("-");
3080
2968
separator.append("+");
3083
2971
tee_puts((char*) separator.c_str(), PAGER);
3084
2972
if (column_names)
3086
drizzle_column_seek(result,0);
2974
drizzle_field_seek(result,0);
3087
2975
(void) tee_fputs("|", PAGER);
3088
for (uint32_t off=0; (field = drizzle_column_next(result)) ; off++)
2976
for (uint off=0; (field = drizzle_fetch_field(result)) ; off++)
3090
uint32_t name_length= (uint32_t) strlen(drizzle_column_name(field));
3091
uint32_t numcells= charset_info->cset->numcells(charset_info,
3092
drizzle_column_name(field),
3093
drizzle_column_name(field) +
3095
uint32_t display_length= drizzle_column_max_size(field) + name_length -
2978
uint name_length= (uint) strlen(field->name);
2979
uint numcells= charset_info->cset->numcells(charset_info,
2981
field->name + name_length);
2982
uint32_t display_length= field->max_length + name_length - numcells;
3097
2983
tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
3098
2984
MAX_COLUMN_LENGTH),
3099
drizzle_column_name(field));
3100
num_flag[off]= ((drizzle_column_type(field) <= DRIZZLE_COLUMN_TYPE_LONGLONG) ||
3101
(drizzle_column_type(field) == DRIZZLE_COLUMN_TYPE_NEWDECIMAL));
2986
num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) ||
2987
(field->type == DRIZZLE_TYPE_NEWDECIMAL));
3103
2989
(void) tee_fputs("\n", PAGER);
3104
2990
tee_puts((char*) separator.c_str(), PAGER);
2993
while ((cur= drizzle_fetch_row(result)))
3111
cur= drizzle_row_buffer(result, &ret);
3112
if (ret != DRIZZLE_RETURN_OK)
3114
(void)put_error(&con, result);
3119
cur= drizzle_row_next(result);
3121
if (cur == NULL || interrupted_query)
2995
if (interrupted_query)
3124
size_t *lengths= drizzle_row_field_sizes(result);
2997
uint32_t *lengths= drizzle_fetch_lengths(result);
3125
2998
(void) tee_fputs("| ", PAGER);
3126
drizzle_column_seek(result, 0);
3127
for (uint32_t off= 0; off < drizzle_result_column_count(result); off++)
2999
drizzle_field_seek(result, 0);
3000
for (uint off= 0; off < drizzle_num_fields(result); off++)
3129
3002
const char *buffer;
3130
uint32_t data_length;
3131
uint32_t field_max_length;
3132
uint32_t visible_length;
3133
uint32_t extra_padding;
3004
uint field_max_length;
3005
uint visible_length;
3135
3008
if (cur[off] == NULL)
3270
print_table_data_vertically(drizzle_result_st *result)
3138
print_table_data_vertically(DRIZZLE_RES *result)
3273
drizzle_return_t ret;
3274
uint32_t max_length=0;
3275
drizzle_column_st *field;
3142
DRIZZLE_FIELD *field;
3277
while ((field = drizzle_column_next(result)))
3144
while ((field = drizzle_fetch_field(result)))
3279
uint32_t length= strlen(drizzle_column_name(field));
3146
uint length= field->name_length;
3280
3147
if (length > max_length)
3281
3148
max_length= length;
3282
drizzle_column_set_max_size(field, length);
3149
field->max_length=length;
3285
for (uint32_t row_count=1;; row_count++)
3152
drizzle_field_seek(result,0);
3153
for (uint row_count=1; (cur= drizzle_fetch_row(result)); row_count++)
3289
cur= drizzle_row_buffer(result, &ret);
3290
if (ret != DRIZZLE_RETURN_OK)
3292
(void)put_error(&con, result);
3297
cur= drizzle_row_next(result);
3299
if (cur == NULL || interrupted_query)
3155
if (interrupted_query)
3301
drizzle_column_seek(result,0);
3157
drizzle_field_seek(result,0);
3302
3158
tee_fprintf(PAGER,
3303
3159
"*************************** %d. row ***************************\n", row_count);
3304
for (uint32_t off=0; off < drizzle_result_column_count(result); off++)
3160
for (uint off=0; off < drizzle_num_fields(result); off++)
3306
field= drizzle_column_next(result);
3307
tee_fprintf(PAGER, "%*s: ",(int) max_length,drizzle_column_name(field));
3162
field= drizzle_fetch_field(result);
3163
tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
3308
3164
tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3311
drizzle_row_free(result, cur);
3316
3170
/* print_warnings should be called right after executing a statement */
3318
static void print_warnings(uint32_t error_code)
3172
static void print_warnings()
3321
drizzle_result_st result;
3175
DRIZZLE_RES *result;
3323
3177
uint64_t num_rows;
3324
uint32_t new_code= 0;
3179
/* Save current error before calling "show warnings" */
3180
uint error= drizzle_errno(&drizzle);
3326
3182
/* Get the warnings */
3327
3183
query= "show warnings";
3328
drizzleclient_real_query_for_lazy(query, strlen(query),&result,&new_code);
3329
drizzleclient_store_result_for_lazy(&result);
3184
drizzle_real_query_for_lazy(query, strlen(query));
3185
drizzle_store_result_for_lazy(&result);
3331
3187
/* Bail out when no warnings */
3332
if (!(num_rows= drizzle_result_row_count(&result)))
3188
if (!(num_rows= drizzle_num_rows(result)))
3335
cur= drizzle_row_next(&result);
3191
cur= drizzle_fetch_row(result);
3338
3194
Don't print a duplicate of the current error. It is possible for SHOW
3369
3222
if (opt_raw_data)
3370
3223
tee_fputs(pos, PAGER);
3371
3224
else for (const char *end=pos+length ; pos != end ; pos++)
3374
if (use_mb(charset_info) &&
3375
(l = my_ismbchar(charset_info, pos, end)))
3378
tee_putc(*pos++, PAGER);
3383
tee_fputs("\\0", PAGER); // This makes everything hard
3384
else if (*pos == '\t')
3385
tee_fputs("\\t", PAGER); // This would destroy tab format
3386
else if (*pos == '\n')
3387
tee_fputs("\\n", PAGER); // This too
3388
else if (*pos == '\\')
3389
tee_fputs("\\\\", PAGER);
3391
tee_putc(*pos, PAGER);
3228
if (use_mb(charset_info) &&
3229
(l = my_ismbchar(charset_info, pos, end)))
3232
tee_putc(*pos++, PAGER);
3238
tee_fputs("\\0", PAGER); // This makes everything hard
3239
else if (*pos == '\t')
3240
tee_fputs("\\t", PAGER); // This would destroy tab format
3241
else if (*pos == '\n')
3242
tee_fputs("\\n", PAGER); // This too
3243
else if (*pos == '\\')
3244
tee_fputs("\\\\", PAGER);
3246
tee_putc(*pos, PAGER);
3398
print_tab_data(drizzle_result_st *result)
3253
print_tab_data(DRIZZLE_RES *result)
3401
drizzle_return_t ret;
3402
drizzle_column_st *field;
3256
DRIZZLE_FIELD *field;
3405
3259
if (opt_silent < 2 && column_names)
3408
while ((field = drizzle_column_next(result)))
3262
while ((field = drizzle_fetch_field(result)))
3411
3265
(void) tee_fputs("\t", PAGER);
3412
(void) tee_fputs(drizzle_column_name(field), PAGER);
3266
(void) tee_fputs(field->name, PAGER);
3414
3268
(void) tee_fputs("\n", PAGER);
3270
while ((cur = drizzle_fetch_row(result)))
3420
cur= drizzle_row_buffer(result, &ret);
3421
if (ret != DRIZZLE_RETURN_OK)
3423
(void)put_error(&con, result);
3428
cur= drizzle_row_next(result);
3433
lengths= drizzle_row_field_sizes(result);
3272
lengths= drizzle_fetch_lengths(result);
3434
3273
safe_put_field(cur[0],lengths[0]);
3435
for (uint32_t off=1 ; off < drizzle_result_column_count(result); off++)
3274
for (uint off=1 ; off < drizzle_num_fields(result); off++)
3437
3276
(void) tee_fputs("\t", PAGER);
3438
3277
safe_put_field(cur[off], lengths[off]);
3440
3279
(void) tee_fputs("\n", PAGER);
3442
drizzle_row_free(result, cur);
3447
com_tee(string *, const char *line )
3284
com_tee(string *buffer __attribute__((unused)), const char *line )
3449
char file_name[FN_REFLEN], *end;
3286
char file_name[FN_REFLEN], *end, *param;
3452
3288
if (status.batch)
3904
3727
sql_connect(char *host,char *database,char *user,char *password,
3907
drizzle_return_t ret;
3912
drizzle_con_free(&con);
3913
drizzle_free(&drizzle);
3733
drizzle_close(&drizzle);
3915
3735
drizzle_create(&drizzle);
3916
if (drizzle_con_add_tcp(&drizzle, &con, host, opt_drizzle_port, user,
3917
password, database, DRIZZLE_CON_NONE) == NULL)
3919
(void) put_error(&con, NULL);
3920
(void) fflush(stdout);
3924
/* XXX add this back in
3925
3736
if (opt_connect_timeout)
3927
uint32_t timeout=opt_connect_timeout;
3928
drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3738
uint timeout=opt_connect_timeout;
3739
drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3929
3740
(char*) &timeout);
3933
/* XXX Do we need this?
3743
drizzle_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
3744
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);
3934
3748
if (safe_updates)
3936
3750
char init_command[100];
3937
3751
sprintf(init_command,
3938
3752
"SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3939
",MAX_JOIN_SIZE=%"PRIu32,
3753
",SQL_MAX_JOIN_SIZE=%"PRIu32,
3940
3754
select_limit, max_join_size);
3941
drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3755
drizzle_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3944
if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
3757
if (!drizzle_connect(&drizzle, host, user, password,
3758
database, opt_drizzle_port, opt_drizzle_unix_port,
3759
connect_flag | CLIENT_MULTI_STATEMENTS))
3946
if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
3947
ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
3762
(drizzle_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3763
drizzle_errno(&drizzle) != CR_CONNECTION_ERROR))
3949
(void) put_error(&con, NULL);
3765
(void) put_error(&drizzle);
3950
3766
(void) fflush(stdout);
3951
3767
return ignore_errors ? -1 : 1; // Abort
3953
3769
return -1; // Retryable
3957
3772
drizzle.reconnect= debug_info_flag; // We want to know if this happens
3959
3773
build_completion_hash(opt_rehash, 1);
3965
com_status(string *, const char *)
3779
com_status(string *buffer __attribute__((unused)),
3780
const char *line __attribute__((unused)))
3971
drizzle_result_st result;
3972
drizzle_return_t ret;
3784
DRIZZLE_RES *result;
3974
3786
tee_puts("--------------", stdout);
3975
3787
usage(1); /* Print version */
3978
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_con_thread_id(&con));
3790
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_thread_id(&drizzle));
3980
3792
Don't remove "limit 1",
3981
3793
it is protection againts SQL_SELECT_LIMIT=0
3983
if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
3984
&ret) != NULL && ret == DRIZZLE_RETURN_OK &&
3985
drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
3795
if (!drizzle_query(&drizzle,"select DATABASE(), USER() limit 1") &&
3796
(result=drizzle_use_result(&drizzle)))
3987
drizzle_row_t cur=drizzle_row_next(&result);
3798
DRIZZLE_ROW cur=drizzle_fetch_row(result);
3990
3801
tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3991
3802
tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3993
drizzle_result_free(&result);
3804
drizzle_free_result(result);
3995
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
3996
drizzle_result_free(&result);
3997
3806
tee_puts("SSL:\t\t\tNot in use", stdout);
4012
3821
tee_fprintf(stdout, "Current pager:\t\t%s\n", pager);
4013
3822
tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
4014
3823
tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
4015
tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&con));
4016
tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_con_protocol_version(&con));
4017
tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_con_host(&con));
4018
/* XXX need to save this from result
4019
if ((id= drizzleclient_insert_id(&drizzle)))
3824
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)))
4020
3828
tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
4023
if (strcmp(drizzle_con_uds(&con), ""))
4024
tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle_con_uds(&con));
3830
/* "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)))
3834
DRIZZLE_ROW cur=drizzle_fetch_row(result);
3837
tee_fprintf(stdout, "Server characterset:\t%s\n", cur[2] ? cur[2] : "");
3838
tee_fprintf(stdout, "Db characterset:\t%s\n", cur[3] ? cur[3] : "");
3839
tee_fprintf(stdout, "Client characterset:\t%s\n", cur[0] ? cur[0] : "");
3840
tee_fprintf(stdout, "Conn. characterset:\t%s\n", cur[1] ? cur[1] : "");
3842
drizzle_free_result(result);
3845
if (strstr(drizzle_get_host_info(&drizzle),"TCP/IP") || ! drizzle.unix_socket)
3846
tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle.port);
4026
tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle_con_port(&con));
3848
tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle.unix_socket);
3849
if (drizzle.net.compress)
3850
tee_fprintf(stdout, "Protocol:\t\tCompressed\n");
4028
3852
if (safe_updates)
4044
3868
static const char *
4045
server_version_string(drizzle_con_st *local_con)
3869
server_version_string(DRIZZLE *con)
4047
static string buf("");
4048
static bool server_version_string_reserved= false;
3871
static char buf[MAX_SERVER_VERSION_LENGTH] = "";
4050
if (!server_version_string_reserved)
4052
buf.reserve(MAX_SERVER_VERSION_LENGTH);
4053
server_version_string_reserved= true;
4055
3873
/* Only one thread calls this, so no synchronization is needed */
4056
3874
if (buf[0] == '\0')
4058
drizzle_result_st result;
4059
drizzle_return_t ret;
3877
DRIZZLE_RES *result;
4061
buf.append(drizzle_con_server_version(local_con));
3879
bufp= my_stpncpy(buf, drizzle_get_server_info(con), sizeof buf);
4063
3881
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
4064
(void)drizzle_query_str(local_con, &result,
4065
"select @@version_comment limit 1", &ret);
4066
if (ret == DRIZZLE_RETURN_OK &&
4067
drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
3882
if (!drizzle_query(con, "select @@version_comment limit 1") &&
3883
(result = drizzle_use_result(con)))
4069
drizzle_row_t cur = drizzle_row_next(&result);
3885
DRIZZLE_ROW cur = drizzle_fetch_row(result);
4070
3886
if (cur && cur[0])
3888
bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NULL);
4075
drizzle_result_free(&result);
3890
drizzle_free_result(result);
4077
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
4078
drizzle_result_free(&result);
3893
/* str*nmov doesn't guarantee NUL-termination */
3894
if (bufp == buf + sizeof buf)
3895
buf[sizeof buf - 1] = '\0';
4085
put_info(const char *str,INFO_TYPE info_type, uint32_t error, const char *sqlstate)
3902
put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
4087
3904
FILE *file= (info_type == INFO_ERROR ? stderr : stdout);
4088
3905
static int inited=0;
4481
4280
static void add_int_to_prompt(int toadd)
4483
ostringstream buffer;
4485
processed_prompt->append(buffer.str().c_str());
4283
int10_to_str(toadd, buffer, 10);
4284
processed_prompt->append(buffer);
4488
4287
static void init_username()
4491
4289
free(full_username);
4492
4290
free(part_username);
4494
drizzle_result_st *result;
4495
if (!drizzleclient_query(&drizzle,"select USER()") &&
4496
(result=drizzleclient_use_result(&drizzle)))
4292
DRIZZLE_RES *result;
4293
if (!drizzle_query(&drizzle,"select USER()") &&
4294
(result=drizzle_use_result(&drizzle)))
4498
drizzle_row_t cur=drizzleclient_fetch_row(result);
4296
DRIZZLE_ROW cur=drizzle_fetch_row(result);
4499
4297
full_username= strdup(cur[0]);
4500
4298
part_username= strdup(strtok(cur[0],"@"));
4501
(void) drizzleclient_fetch_row(result); // Read eof
4299
(void) drizzle_fetch_row(result); // Read eof
4506
static int com_prompt(string *, const char *line)
4303
static int com_prompt(string *buffer __attribute__((unused)),
4508
const char *ptr=strchr(line, ' ');
4306
char *ptr=strchr(line, ' ');
4308
free(current_prompt);
4309
current_prompt= strdup(ptr ? ptr+1 : default_prompt);
4510
4311
tee_fprintf(stdout, "Returning to default PROMPT of %s\n",
4511
4312
default_prompt);
4513
char * tmpptr= strdup(ptr ? ptr+1 : default_prompt);
4515
tee_fprintf(stdout, "Memory allocation error. Not changing prompt\n");
4518
free(current_prompt);
4519
current_prompt= tmpptr;
4520
4314
tee_fprintf(stdout, "PROMPT set to '%s'\n", current_prompt);