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;
144
160
static char **defaults_argv;
146
162
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
147
163
typedef enum enum_info_type INFO_TYPE;
149
static DRIZZLE drizzle; /* The connection */
150
static bool ignore_errors=0,quick=0,
151
connected=0,opt_raw_data=0,unbuffered=0,output_tables=0,
152
opt_rehash=1,skip_updates=0,safe_updates=0,one_database=0,
153
opt_compress=0, using_opt_local_infile=0,
154
vertical=0, line_numbers=1, column_names=1,
155
opt_nopager=1, opt_outfile=0, named_cmds= 0,
156
tty_password= 0, opt_nobeep=0, opt_reconnect=1,
157
default_charset_used= 0, opt_secure_auth= 0,
158
default_pager_set= 0, opt_sigint_ignore= 0,
159
auto_vertical_output= 0,
160
show_warnings= 0, executing_query= 0, interrupted_query= 0;
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;
161
180
static bool debug_info_flag, debug_check_flag;
162
181
static bool column_types_flag;
163
static bool preserve_comments= 0;
164
static uint32_t opt_max_allowed_packet, opt_net_buffer_length;
165
static int verbose=0,opt_silent=0,opt_drizzle_port=0, opt_local_infile=0;
166
static uint my_end_arg;
167
static char * opt_drizzle_unix_port=0;
168
static int connect_flag=CLIENT_INTERACTIVE;
169
static char *current_host,*current_db,*current_user=0,*opt_password=0,
170
*delimiter_str= 0,* current_prompt= 0,
171
*default_charset= (char*) DRIZZLE_DEFAULT_CHARSET_NAME;
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;
172
190
static char *histfile;
173
191
static char *histfile_tmp;
174
192
static string *glob_buffer;
175
193
static string *processed_prompt= NULL;
176
194
static char *default_prompt= NULL;
177
static char *full_username=0,*part_username=0;
195
static char *full_username= NULL,*part_username= NULL;
178
196
static STATUS status;
179
197
static uint32_t select_limit;
180
198
static uint32_t max_join_size;
181
199
static uint32_t opt_connect_timeout= 0;
182
static char drizzle_charsets_dir[FN_REFLEN+1];
183
200
// TODO: Need to i18n these
184
static const char *day_names[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
185
static const char *month_names[]={"Jan","Feb","Mar","Apr","May","Jun","Jul",
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",
186
203
"Aug","Sep","Oct","Nov","Dec"};
187
204
static char default_pager[FN_REFLEN];
188
205
static char pager[FN_REFLEN], outfile[FN_REFLEN];
189
206
static FILE *PAGER, *OUTFILE;
190
static MEM_ROOT hash_mem_root;
191
static uint prompt_counter;
207
static uint32_t prompt_counter;
192
208
static char delimiter[16]= DEFAULT_DELIMITER;
193
static uint delimiter_length= 1;
209
static uint32_t delimiter_length= 1;
194
210
unsigned short terminal_width= 80;
196
static uint opt_protocol= DRIZZLE_PROTOCOL_TCP;
197
static const CHARSET_INFO *charset_info= &my_charset_latin1;
212
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
199
int drizzle_real_query_for_lazy(const char *buf, int length);
200
int drizzle_store_result_for_lazy(DRIZZLE_RES **result);
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);
203
220
void tee_fprintf(FILE *file, const char *fmt, ...);
216
233
com_connect(string *str,const char*), com_status(string *str,const char*),
217
234
com_use(string *str,const char*), com_source(string *str, const char*),
218
235
com_rehash(string *str, const char*), com_tee(string *str, const char*),
219
com_notee(string *str, const char*), com_charset(string *str,const char*),
236
com_notee(string *str, const char*),
220
237
com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
221
238
com_warnings(string *str, const char*), com_nowarnings(string *str, const char*),
222
239
com_nopager(string *str, const char*), com_pager(string *str, const char*);
224
241
static int read_and_execute(bool interactive);
225
242
static int sql_connect(char *host,char *database,char *user,char *password,
227
static const char *server_version_string(DRIZZLE *drizzle);
228
static int put_info(const char *str,INFO_TYPE info,uint error,
244
static const char *server_version_string(drizzle_con_st *con);
245
static int put_info(const char *str,INFO_TYPE info,uint32_t error,
229
246
const char *sql_state);
230
static int put_error(DRIZZLE *drizzle);
247
static int put_error(drizzle_con_st *con, drizzle_result_st *res);
231
248
static void safe_put_field(const char *pos,uint32_t length);
232
249
static void init_pager(void);
233
250
static void end_pager(void);
255
272
static COMMANDS commands[] = {
256
{ "?", '?', com_help, 1, N_("Synonym for `help'.") },
273
{ "?", '?', com_help, 0, N_("Synonym for `help'.") },
257
274
{ "clear", 'c', com_clear, 0, N_("Clear command.")},
258
275
{ "connect",'r', com_connect,1,
259
N_("Reconnect to the server. Optional arguments are db and host." }),
276
N_("Reconnect to the server. Optional arguments are db and host.")},
260
277
{ "delimiter", 'd', com_delimiter, 1,
261
278
N_("Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.") },
262
279
{ "ego", 'G', com_ego, 0,
263
280
N_("Send command to drizzle server, display result vertically.")},
264
281
{ "exit", 'q', com_quit, 0, N_("Exit drizzle. Same as quit.")},
265
282
{ "go", 'g', com_go, 0, N_("Send command to drizzle server.") },
266
{ "help", 'h', com_help, 1, N_("Display this help.") },
283
{ "help", 'h', com_help, 0, N_("Display this help.") },
267
284
{ "nopager",'n', com_nopager,0, N_("Disable pager, print to stdout.") },
268
285
{ "notee", 't', com_notee, 0, N_("Don't write into outfile.") },
269
286
{ "pager", 'P', com_pager, 1,
788
793
{ "ATAN2", 0, 0, 0, ""},
789
794
{ "BENCHMARK", 0, 0, 0, ""},
790
795
{ "BIN", 0, 0, 0, ""},
791
{ "BIT_COUNT", 0, 0, 0, ""},
792
796
{ "BIT_OR", 0, 0, 0, ""},
793
797
{ "BIT_AND", 0, 0, 0, ""},
794
798
{ "BIT_XOR", 0, 0, 0, ""},
795
799
{ "CAST", 0, 0, 0, ""},
796
800
{ "CEIL", 0, 0, 0, ""},
797
801
{ "CEILING", 0, 0, 0, ""},
798
{ "BIT_LENGTH", 0, 0, 0, ""},
799
802
{ "CENTROID", 0, 0, 0, ""},
800
803
{ "CHAR_LENGTH", 0, 0, 0, ""},
801
804
{ "CHARACTER_LENGTH", 0, 0, 0, ""},
962
962
{ "SUBSTR", 0, 0, 0, ""},
963
963
{ "SUBSTRING", 0, 0, 0, ""},
964
964
{ "SUBSTRING_INDEX", 0, 0, 0, ""},
965
{ "SUBTIME", 0, 0, 0, ""},
966
965
{ "SUM", 0, 0, 0, ""},
967
966
{ "SYSDATE", 0, 0, 0, ""},
968
967
{ "SYSTEM_USER", 0, 0, 0, ""},
969
968
{ "TAN", 0, 0, 0, ""},
970
969
{ "TIME_FORMAT", 0, 0, 0, ""},
971
{ "TIME_TO_SEC", 0, 0, 0, ""},
972
{ "TIMEDIFF", 0, 0, 0, ""},
973
970
{ "TO_DAYS", 0, 0, 0, ""},
974
971
{ "TOUCHES", 0, 0, 0, ""},
975
972
{ "TRIM", 0, 0, 0, ""},
1006
1003
static bool add_line(string *buffer,char *line,char *in_string,
1007
1004
bool *ml_comment);
1008
1005
static void remove_cntrl(string *buffer);
1009
static void print_table_data(DRIZZLE_RES *result);
1010
static void print_tab_data(DRIZZLE_RES *result);
1011
static void print_table_data_vertically(DRIZZLE_RES *result);
1012
static void print_warnings(void);
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);
1013
1010
static uint32_t start_timer(void);
1014
1011
static void end_timer(uint32_t start_time,char *buff);
1015
1012
static void drizzle_end_timer(uint32_t start_time,char *buff);
1016
1013
static void nice_time(double sec,char *buff,bool part_second);
1017
extern sig_handler drizzle_end(int sig);
1018
extern sig_handler handle_sigint(int sig);
1014
extern "C" void drizzle_end(int sig);
1015
extern "C" void handle_sigint(int sig);
1019
1016
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1020
static sig_handler window_resize(int sig);
1017
static void 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)
1023
1145
int main(int argc,char *argv[])
1224
1371
If query is in process, kill query
1225
1372
no query in process, terminate like previous behavior
1227
sig_handler handle_sigint(int sig)
1375
void handle_sigint(int sig)
1229
1377
char kill_buffer[40];
1230
DRIZZLE *kill_drizzle= NULL;
1378
drizzle_con_st kill_drizzle;
1379
drizzle_result_st res;
1380
drizzle_return_t ret;
1232
1382
/* terminate if no query being executed, or we already tried interrupting */
1233
1383
if (!executing_query || interrupted_query) {
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))
1387
if (drizzle_con_add_tcp(&drizzle, &kill_drizzle, current_host,
1388
opt_drizzle_port, current_user, opt_password, NULL,
1389
DRIZZLE_CON_NONE) == NULL)
1244
1394
/* kill_buffer is always big enough because max length of %lu is 15 */
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);
1395
sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1396
drizzle_con_thread_id(&con));
1398
if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
1399
drizzle_result_free(&res);
1401
drizzle_con_free(&kill_drizzle);
1248
1402
tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1250
1404
interrupted_query= 1;
1277
1431
(char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
1279
1433
{"no-auto-rehash", 'A',
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."),
1434
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."),
1281
1435
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1282
1436
{"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT,
1283
1437
N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
1284
1438
(char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1286
1440
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
1441
{"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
1291
1442
(char**) &column_types_flag, (char**) &column_types_flag,
1292
1443
0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1304
1455
{"database", 'D', N_("Database to use."), (char**) ¤t_db,
1305
1456
(char**) ¤t_db, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1306
1457
{"default-character-set", OPT_DEFAULT_CHARSET,
1307
N_("Set the default character set."), (char**) &default_charset,
1308
(char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1458
N_("(not used)"), 0,
1459
0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1309
1460
{"delimiter", OPT_DELIMITER, N_("Delimiter to be used."), (char**) &delimiter_str,
1310
1461
(char**) &delimiter_str, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1311
1462
{"execute", 'e', N_("Execute command and quit. (Disables --force and history file)"), 0,
1360
1511
{"no-pager", OPT_NOPAGER,
1361
1512
N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
1362
1513
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1364
1515
N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1365
1516
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, ")
1517
{"port", 'p', N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, ")
1367
1518
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},
1519
0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1370
1520
{"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
1371
1521
(char**) ¤t_prompt, (char**) ¤t_prompt, 0, GET_STR_ALLOC,
1372
1522
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
1524
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
1525
(char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1411
1559
{"connect_timeout", OPT_CONNECT_TIMEOUT,
1412
1560
N_("Number of seconds before connection timeout."),
1413
1561
(char**) &opt_connect_timeout,
1414
(char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 0, 0, 3600*12, 0,
1562
(char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 0, 0, 3600*12, 0,
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,
1564
{"max_input_line", OPT_MAX_INPUT_LINE,
1565
N_("Max length of input line"),
1566
(char**) &opt_max_input_line, (char**) &opt_max_input_line, 0,
1567
GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1420
1568
(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},
1425
1569
{"select_limit", OPT_SELECT_LIMIT,
1426
1570
N_("Automatic limit for SELECT when using --safe-updates"),
1427
1571
(char**) &select_limit,
1428
(char**) &select_limit, 0, GET_ULONG, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1572
(char**) &select_limit, 0, GET_UINT32, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1430
1574
{"max_join_size", OPT_MAX_JOIN_SIZE,
1431
1575
N_("Automatic limit for rows in a join when using --safe-updates"),
1432
1576
(char**) &max_join_size,
1433
(char**) &max_join_size, 0, GET_ULONG, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1577
(char**) &max_join_size, 0, GET_UINT32, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1435
1579
{"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
1580
(char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1437
1581
{"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1438
1582
(char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1439
1583
0, 0, 0, 0, 0, 0},
1584
{"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1585
(char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
1587
{"ping", OPT_PING, N_("Ping the server to check if it's alive."),
1588
(char**) &opt_ping, (char**) &opt_ping, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1440
1589
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1446
1595
const char* readline= "readline";
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,
1597
printf(_("%s Ver %s Distrib %s, for %s-%s (%s) using %s %s\n"),
1598
my_progname, VER.c_str(), drizzle_version(),
1599
HOST_VENDOR, HOST_OS, HOST_CPU,
1451
1600
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"));
1604
printf(_("Copyright (C) 2008 Sun Microsystems\n"
1605
"This software comes with ABSOLUTELY NO WARRANTY. "
1606
"This is free software,\n"
1607
"and you are welcome to modify and redistribute it "
1608
"under the GPL license\n"));
1459
1609
printf(_("Usage: %s [OPTIONS] [database]\n"), my_progname);
1460
1610
my_print_help(my_long_options);
1461
print_defaults("my", load_default_groups);
1611
print_defaults("drizzle", load_default_groups);
1462
1612
my_print_variables(my_long_options);
1467
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
1617
get_one_option(int optid, const struct my_option *, char *argument)
1619
char *endchar= NULL;
1620
uint64_t temp_drizzle_port= 0;
1470
1622
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
1623
case OPT_DEFAULT_CHARSET:
1476
1624
default_charset_used= 1;
1478
1626
case OPT_DELIMITER:
1479
1627
if (argument == disabled_my_option)
1481
stpcpy(delimiter, DEFAULT_DELIMITER);
1629
strcpy(delimiter, DEFAULT_DELIMITER);
1485
1633
/* Check that delimiter does not contain a backslash */
1486
1634
if (!strstr(argument, "\\"))
1488
strmake(delimiter, argument, sizeof(delimiter) - 1);
1636
strncpy(delimiter, argument, sizeof(delimiter) - 1);
1492
1640
put_info(_("DELIMITER cannot contain a backslash character"),
1493
1641
INFO_ERROR,0,0);
1497
delimiter_length= (uint)strlen(delimiter);
1645
delimiter_length= (uint32_t)strlen(delimiter);
1498
1646
delimiter_str= delimiter;
1500
case OPT_LOCAL_INFILE:
1501
using_opt_local_infile=1;
1504
1649
if (argument == disabled_my_option)
2198
2367
return (char**) 0;
2202
static char *new_command_generator(const char *text,int state)
2370
inline string lower_string(const string &from_string)
2372
string to_string= from_string;
2373
transform(to_string.begin(), to_string.end(),
2374
to_string.begin(), ::tolower);
2377
inline string lower_string(const char * from_string)
2379
string to_string= from_string;
2380
return lower_string(to_string);
2384
class CompletionMatch :
2385
public unary_function<const string&, bool>
2390
CompletionMatch(string text) : match_text(text) {}
2391
inline bool operator() (const pair<string,string> &match_against) const
2394
lower_string(match_against.first.substr(0,match_text.size()));
2395
return match_func(sub_match,match_text);
2402
char *new_command_generator(const char *text, int state)
2211
textlen=(uint) strlen(text);
2214
{ /* lookup in the hash */
2219
b = find_all_matches(&ht,text,(uint) strlen(text),&len);
2227
ptr= strdup(e->str);
2233
{ /* traverse the entire hash, ugly but works */
2237
/* find the first used bucket */
2238
for (i=0 ; i < ht.nTableSize ; i++)
2240
if (ht.arBuckets[i])
2242
b = ht.arBuckets[i];
2250
{ /* find valid entry in bucket */
2251
if ((uint) strlen(e->str) == b->nKeyLength)
2252
ptr = strdup(e->str);
2253
/* find the next used entry */
2256
{ /* find the next used bucket */
2260
for (i++ ; i<ht.nTableSize; i++)
2262
if (ht.arBuckets[i])
2264
b = ht.arBuckets[i];
2407
completion_string= lower_string(text);
2408
if (completion_string.size() == 0)
2410
completion_iter= completion_map.begin();
2411
completion_end= completion_map.end();
2415
completion_iter= find_if(completion_map.begin(), completion_map.end(),
2416
CompletionMatch<equal_to<string> >(completion_string));
2417
completion_end= find_if(completion_iter, completion_map.end(),
2418
CompletionMatch<not_equal_to<string> >(completion_string));
2421
if (completion_iter == completion_end || (size_t)state > completion_map.size())
2423
char *result= (char *)malloc((*completion_iter).second.size()+1);
2424
strcpy(result, (*completion_iter).second.c_str());
2281
2429
/* Build up the completion hash */
2283
2431
static void build_completion_hash(bool rehash, bool write_info)
2285
2433
COMMANDS *cmd=commands;
2286
DRIZZLE_RES *databases=0,*tables=0;
2287
DRIZZLE_RES *fields;
2288
static char ***field_names= 0;
2289
DRIZZLE_ROW database_row,table_row;
2290
DRIZZLE_FIELD *sql_field;
2291
char buf[NAME_LEN*2+2]; // table name plus field name plus 2
2434
drizzle_return_t ret;
2435
drizzle_result_st databases,tables,fields;
2436
drizzle_row_t database_row,table_row;
2437
drizzle_column_st *sql_field;
2438
string tmp_str, tmp_str_lower;
2295
2440
if (status.batch || quick || !current_db)
2296
2441
return; // We don't need completion in batches
2300
/* Free old used memory */
2303
completion_hash_clean(&ht);
2304
free_root(&hash_mem_root,MYF(0));
2445
completion_map.clear();
2306
2447
/* hash this file's known subset of SQL commands */
2307
2448
while (cmd->name) {
2308
add_word(&ht,(char*) cmd->name);
2450
tmp_str_lower= lower_string(tmp_str);
2451
completion_map[tmp_str_lower]= tmp_str;
2312
2455
/* hash Drizzle functions (to be implemented) */
2314
2457
/* hash all database names */
2315
if (drizzle_query(&drizzle,"show databases") == 0)
2458
if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
2317
if (!(databases = drizzle_store_result(&drizzle)))
2318
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2460
if (ret == DRIZZLE_RETURN_OK)
2321
while ((database_row=drizzle_fetch_row(databases)))
2462
if (drizzle_result_buffer(&databases) != DRIZZLE_RETURN_OK)
2463
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2323
char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
2325
add_word(&ht,(char*) str);
2466
while ((database_row=drizzle_row_next(&databases)))
2468
tmp_str= database_row[0];
2469
tmp_str_lower= lower_string(tmp_str);
2470
completion_map[tmp_str_lower]= tmp_str;
2327
drizzle_free_result(databases);
2475
drizzle_result_free(&databases);
2330
2478
/* hash all table names */
2331
if (drizzle_query(&drizzle,"show tables")==0)
2479
if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
2333
if (!(tables = drizzle_store_result(&drizzle)))
2481
if (ret != DRIZZLE_RETURN_OK)
2483
drizzle_result_free(&tables);
2487
if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
2334
2488
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2337
if (drizzle_num_rows(tables) > 0 && !opt_silent && write_info)
2491
if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
2339
2493
tee_fprintf(stdout, _("\
2340
2494
Reading table information for completion of table and column names\n \
2341
2495
You can turn off this feature to get a quicker startup with -A\n\n"));
2343
while ((table_row=drizzle_fetch_row(tables)))
2497
while ((table_row=drizzle_row_next(&tables)))
2345
char *str=strdup_root(&hash_mem_root, (char*) table_row[0]);
2347
!completion_hash_exists(&ht,(char*) str, (uint) strlen(str)))
2499
tmp_str= table_row[0];
2500
tmp_str_lower= lower_string(tmp_str);
2501
completion_map[tmp_str_lower]= tmp_str;
2353
2508
/* hash all field names, both with the table prefix and without it */
2354
if (!tables) /* no tables */
2358
drizzle_data_seek(tables,0);
2359
if (!(field_names= (char ***) alloc_root(&hash_mem_root,sizeof(char **) *
2360
(uint) (drizzle_num_rows(tables)+1))))
2362
drizzle_free_result(tables);
2366
while ((table_row=drizzle_fetch_row(tables)))
2368
if ((fields=drizzle_list_fields(&drizzle,(const char*) table_row[0],NullS)))
2509
if (drizzle_result_row_count(&tables) == 0)
2511
drizzle_result_free(&tables);
2515
drizzle_row_seek(&tables, 0);
2517
while ((table_row=drizzle_row_next(&tables)))
2521
query.append("show fields in '");
2522
query.append(table_row[0]);
2525
if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2370
num_fields=drizzle_num_fields(fields);
2371
if (!(field_names[i] = (char **) alloc_root(&hash_mem_root,
2375
drizzle_free_result(fields);
2378
field_names[i][num_fields*2]= '\0';
2380
while ((sql_field=drizzle_fetch_field(fields)))
2382
sprintf(buf,"%.64s.%.64s",table_row[0],sql_field->name);
2383
field_names[i][j] = strdup_root(&hash_mem_root,buf);
2384
add_word(&ht,field_names[i][j]);
2385
field_names[i][num_fields+j] = strdup_root(&hash_mem_root,
2387
if (!completion_hash_exists(&ht,field_names[i][num_fields+j],
2388
(uint) strlen(field_names[i][num_fields+j])))
2389
add_word(&ht,field_names[i][num_fields+j]);
2392
drizzle_free_result(fields);
2528
if (ret == DRIZZLE_RETURN_OK &&
2529
drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2531
while ((sql_field=drizzle_column_next(&fields)))
2533
tmp_str=table_row[0];
2534
tmp_str.append(".");
2535
tmp_str.append(drizzle_column_name(sql_field));
2536
tmp_str_lower= lower_string(tmp_str);
2537
completion_map[tmp_str_lower]= tmp_str;
2539
tmp_str=drizzle_column_name(sql_field);
2540
tmp_str_lower= lower_string(tmp_str);
2541
completion_map[tmp_str_lower]= tmp_str;
2544
drizzle_result_free(&fields);
2399
drizzle_free_result(tables);
2400
field_names[i]=0; // End pointer
2547
drizzle_result_free(&tables);
2548
completion_iter= completion_map.begin();
2404
2551
/* for gnu readline */
2465
2616
The different commands
2466
2617
***************************************************************************/
2468
int drizzle_real_query_for_lazy(const char *buf, int length)
2619
int drizzleclient_real_query_for_lazy(const char *buf, int length,
2620
drizzle_result_st *result,
2621
uint32_t *error_code)
2470
for (uint retry=0;; retry++)
2623
drizzle_return_t ret;
2625
for (uint32_t retry=0;; retry++)
2473
if (!drizzle_real_query(&drizzle,buf,length))
2628
if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2629
ret == DRIZZLE_RETURN_OK)
2475
error= put_error(&drizzle);
2476
if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR || retry > 1 ||
2633
error= put_error(&con, result);
2635
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2637
*error_code= drizzle_result_error_code(result);
2638
drizzle_result_free(result);
2641
if (ret != DRIZZLE_RETURN_SERVER_GONE || retry > 1 ||
2477
2642
!opt_reconnect)
2479
2647
if (reconnect())
2484
int drizzle_store_result_for_lazy(DRIZZLE_RES **result)
2652
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
2486
if ((*result=drizzle_store_result(&drizzle)))
2654
if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
2489
if (drizzle_error(&drizzle)[0])
2490
return put_error(&drizzle);
2657
if (drizzle_con_error(&con)[0])
2658
return put_error(&con, result);
2494
static void print_help_item(DRIZZLE_ROW *cur, int num_name, int num_cat, char *last_char)
2496
char ccat= (*cur)[num_cat][0];
2497
if (*last_char != ccat)
2499
put_info(ccat == 'Y' ? _("categories:") : _("topics:"), INFO_INFO,0,0);
2502
tee_fprintf(PAGER, " %s\n", (*cur)[num_name]);
2506
static int com_server_help(string *buffer,
2507
const char *line __attribute__((unused)),
2511
const char *server_cmd= buffer->c_str();
2513
DRIZZLE_RES *result;
2516
if (help_arg[0] != '\'')
2518
char *end_arg= strend(help_arg);
2521
while (my_isspace(charset_info,*end_arg))
2525
(void) strxnmov(cmd_buf, sizeof(cmd_buf), "help '", help_arg, "'", NullS);
2526
server_cmd= cmd_buf;
2529
if (!connected && reconnect())
2532
if ((error= drizzle_real_query_for_lazy(server_cmd,(int)strlen(server_cmd))) ||
2533
(error= drizzle_store_result_for_lazy(&result)))
2538
unsigned int num_fields= drizzle_num_fields(result);
2539
uint64_t num_rows= drizzle_num_rows(result);
2540
drizzle_fetch_fields(result);
2541
if (num_fields==3 && num_rows==1)
2543
if (!(cur= drizzle_fetch_row(result)))
2550
tee_fprintf(PAGER, _("Name: \'%s\'\n"), cur[0]);
2551
tee_fprintf(PAGER, _("Description:\n%s"), cur[1]);
2552
if (cur[2] && *((char*)cur[2]))
2553
tee_fprintf(PAGER, _("Examples:\n%s"), cur[2]);
2554
tee_fprintf(PAGER, "\n");
2557
else if (num_fields >= 2 && num_rows)
2562
int num_name= 0, num_cat= 0;
2564
if (num_fields == 2)
2566
put_info(_("Many help items for your request exist."), INFO_INFO,0,0);
2567
put_info(_("To make a more specific request, please type 'help <item>',\nwhere <item> is one of the following"), INFO_INFO,0,0);
2571
else if ((cur= drizzle_fetch_row(result)))
2573
tee_fprintf(PAGER, _("You asked for help about help category: '%s'\n"), cur[0]);
2574
put_info(_("For more information, type 'help <item>', where <item> is one of the following"), INFO_INFO,0,0);
2577
print_help_item(&cur,1,2,&last_char);
2580
while ((cur= drizzle_fetch_row(result)))
2581
print_help_item(&cur,num_name,num_cat,&last_char);
2582
tee_fprintf(PAGER, "\n");
2587
put_info(_("\nNothing found"), INFO_INFO,0,0);
2588
put_info(_("Please try to run 'help contents' for a list of all accessible topics\n"), INFO_INFO,0,0);
2593
drizzle_free_result(result);
2598
com_help(string *buffer __attribute__((unused)),
2599
const char *line __attribute__((unused)))
2663
com_help(string *buffer, const char *)
2601
2665
register int i, j;
2602
char * help_arg= strchr(line,' '), buff[32], *end;
2605
while (my_isspace(charset_info,*help_arg))
2607
if (*help_arg) return com_server_help(buffer,line,help_arg);
2666
char buff[32], *end;
2610
2668
put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
2611
2669
if (!named_cmds)
2612
2670
put_info(_("Note that all text commands must be first on line and end with ';'"),INFO_INFO,0,0);
2613
2671
for (i = 0; commands[i].name; i++)
2615
end= stpcpy(buff, commands[i].name);
2673
end= strcpy(buff, commands[i].name);
2674
end+= strlen(commands[i].name);
2616
2675
for (j= (int)strlen(commands[i].name); j < 10; j++)
2617
end= stpcpy(end, " ");
2676
end= strcpy(end, " ")+1;
2618
2677
if (commands[i].func)
2619
2678
tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
2620
2679
commands[i].cmd_char, _(commands[i].doc));
2622
if (connected && drizzle_get_server_version(&drizzle) >= 40100)
2623
put_info(_("\nFor server side help, type 'help contents'\n"), INFO_INFO,0,0);
2681
tee_fprintf(stdout, "\n");
2629
com_clear(string *buffer,
2630
const char *line __attribute__((unused)))
2688
com_clear(string *buffer, const char *)
2632
2690
if (status.add_to_history)
2633
2691
fix_history(buffer);
2748
2782
time_buff[0]= '\0';
2750
2784
/* Every branch must truncate buff . */
2785
if (drizzle_result_column_count(&result) > 0)
2753
if (!drizzle_num_rows(result) && ! quick && !column_types_flag)
2787
if (!quick && drizzle_result_row_count(&result) == 0 &&
2755
stpcpy(buff, _("Empty set"));
2790
strcpy(buff, _("Empty set"));
2760
2795
if (vertical || (auto_vertical_output &&
2761
(terminal_width < get_result_width(result))))
2762
print_table_data_vertically(result);
2796
(terminal_width < get_result_width(&result))))
2797
print_table_data_vertically(&result);
2763
2798
else if (opt_silent && verbose <= 2 && !output_tables)
2764
print_tab_data(result);
2799
print_tab_data(&result);
2766
print_table_data(result);
2801
print_table_data(&result);
2768
2803
ngettext("%ld row in set","%ld rows in set",
2769
(long) drizzle_num_rows(result)),
2770
(long) drizzle_num_rows(result));
2804
(long) drizzle_result_row_count(&result)),
2805
(long) drizzle_result_row_count(&result));
2772
if (drizzle_errno(&drizzle))
2773
error= put_error(&drizzle);
2807
if (drizzle_result_error_code(&result))
2808
error= put_error(&con, &result);
2776
else if (drizzle_affected_rows(&drizzle) == ~(uint64_t) 0)
2777
stpcpy(buff,_("Query OK"));
2811
else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2812
strcpy(buff,_("Query OK"));
2779
2814
sprintf(buff, ngettext("Query OK, %ld row affected",
2780
2815
"Query OK, %ld rows affected",
2781
(long) drizzle_affected_rows(&drizzle)),
2782
(long) drizzle_affected_rows(&drizzle));
2816
(long) drizzle_result_affected_rows(&result)),
2817
(long) drizzle_result_affected_rows(&result));
2785
if ((warnings= drizzle_warning_count(&drizzle)))
2819
pos= strchr(buff, '\0');
2820
if ((warnings= drizzle_result_warning_count(&result)))
2789
pos=int10_to_str(warnings, pos, 10);
2790
pos=stpcpy(pos, " warning");
2824
char warnings_buff[20];
2825
memset(warnings_buff,0,20);
2826
sprintf(warnings_buff, "%d", warnings);
2827
strcpy(pos, warnings_buff);
2828
pos+= strlen(warnings_buff);
2829
pos= strcpy(pos, " warning")+8;
2791
2830
if (warnings != 1)
2794
stpcpy(pos, time_buff);
2833
strcpy(pos, time_buff);
2795
2834
put_info(buff,INFO_RESULT,0,0);
2796
if (drizzle_info(&drizzle))
2797
put_info(drizzle_info(&drizzle),INFO_RESULT,0,0);
2835
if (strcmp(drizzle_result_info(&result), ""))
2836
put_info(drizzle_result_info(&result),INFO_RESULT,0,0);
2798
2837
put_info("",INFO_RESULT,0,0); // Empty row
2800
if (result && !drizzle_eof(result)) /* Something wrong when using quick */
2801
error= put_error(&drizzle);
2802
else if (unbuffered)
2803
2840
fflush(stdout);
2804
drizzle_free_result(result);
2805
} while (!(err= drizzle_next_result(&drizzle)));
2841
drizzle_result_free(&result);
2843
if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2845
if (drizzle_result_read(&con, &result, &ret) == NULL ||
2846
ret != DRIZZLE_RETURN_OK)
2848
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2850
error_code= drizzle_result_error_code(&result);
2851
drizzle_result_free(&result);
2854
error= put_error(&con, NULL);
2859
} while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2807
error= put_error(&drizzle);
2861
error= put_error(&con, NULL);
2811
2865
/* Show warnings if any or error occured */
2812
2866
if (show_warnings == 1 && (warnings >= 1 || error))
2867
print_warnings(error_code);
2815
2869
if (!error && !status.batch &&
2816
(drizzle.server_status & SERVER_STATUS_DB_DROPPED))
2870
drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2817
2872
get_current_db();
2819
2875
executing_query= 0;
2820
2876
return error; /* New command follows */
2884
static const char *fieldtype2str(enum enum_field_types type)
2941
static const char *fieldtype2str(drizzle_column_type_t type)
2886
2943
switch (type) {
2887
case DRIZZLE_TYPE_BLOB: return "BLOB";
2888
case DRIZZLE_TYPE_NEWDATE: return "DATE";
2889
case DRIZZLE_TYPE_DATETIME: return "DATETIME";
2890
case DRIZZLE_TYPE_NEWDECIMAL: return "DECIMAL";
2891
case DRIZZLE_TYPE_DOUBLE: return "DOUBLE";
2892
case DRIZZLE_TYPE_ENUM: return "ENUM";
2893
case DRIZZLE_TYPE_LONG: return "LONG";
2894
case DRIZZLE_TYPE_LONGLONG: return "LONGLONG";
2895
case DRIZZLE_TYPE_NULL: return "NULL";
2896
case DRIZZLE_TYPE_SHORT: return "SHORT";
2897
case DRIZZLE_TYPE_TIME: return "TIME";
2898
case DRIZZLE_TYPE_TIMESTAMP: return "TIMESTAMP";
2899
case DRIZZLE_TYPE_TINY: return "TINY";
2944
case DRIZZLE_COLUMN_TYPE_BLOB: return "BLOB";
2945
case DRIZZLE_COLUMN_TYPE_DATE: return "DATE";
2946
case DRIZZLE_COLUMN_TYPE_DATETIME: return "DATETIME";
2947
case DRIZZLE_COLUMN_TYPE_NEWDECIMAL: return "DECIMAL";
2948
case DRIZZLE_COLUMN_TYPE_DOUBLE: return "DOUBLE";
2949
case DRIZZLE_COLUMN_TYPE_ENUM: return "ENUM";
2950
case DRIZZLE_COLUMN_TYPE_LONG: return "LONG";
2951
case DRIZZLE_COLUMN_TYPE_LONGLONG: return "LONGLONG";
2952
case DRIZZLE_COLUMN_TYPE_NULL: return "NULL";
2953
case DRIZZLE_COLUMN_TYPE_TIMESTAMP: return "TIMESTAMP";
2954
case DRIZZLE_COLUMN_TYPE_TINY: return "TINY";
2955
case DRIZZLE_COLUMN_TYPE_VIRTUAL: return "VIRTUAL";
2900
2956
default: return "?-unknown-?";
2904
static char *fieldflags2str(uint f) {
2960
static char *fieldflags2str(uint32_t f) {
2905
2961
static char buf[1024];
2908
2964
#define ff2s_check_flag(X) \
2909
if (f & X ## _FLAG) { s=stpcpy(s, # X " "); f &= ~ X ## _FLAG; }
2965
if (f & DRIZZLE_COLUMN_FLAGS_ ## X) { s=strcpy(s, # X " ")+strlen(# X " "); \
2966
f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
2910
2967
ff2s_check_flag(NOT_NULL);
2911
2968
ff2s_check_flag(PRI_KEY);
2912
2969
ff2s_check_flag(UNIQUE_KEY);
2951
3008
"Decimals: %u\n"
2952
3009
"Flags: %s\n\n",
2954
field->name, field->catalog, field->db, field->table,
2955
field->org_table, fieldtype2str(field->type),
2956
get_charset_name(field->charsetnr), field->charsetnr,
2957
field->length, field->max_length, field->decimals,
2958
fieldflags2str(field->flags));
3011
drizzle_column_name(field), drizzle_column_catalog(field),
3012
drizzle_column_db(field), drizzle_column_table(field),
3013
drizzle_column_orig_table(field),
3014
fieldtype2str(drizzle_column_type(field)),
3015
get_charset_name(drizzle_column_charset(field)),
3016
drizzle_column_charset(field), drizzle_column_size(field),
3017
drizzle_column_max_size(field), drizzle_column_decimals(field),
3018
fieldflags2str(drizzle_column_flags(field)));
2960
3020
tee_puts("", PAGER);
2965
print_table_data(DRIZZLE_RES *result)
3025
print_table_data(drizzle_result_st *result)
2968
DRIZZLE_FIELD *field;
3028
drizzle_return_t ret;
3029
drizzle_column_st *field;
2970
3031
string separator;
2972
3033
separator.reserve(256);
2974
num_flag=(bool*) my_malloc(sizeof(bool)*drizzle_num_fields(result),
3035
num_flag=(bool*) malloc(sizeof(bool)*drizzle_result_column_count(result));
2976
3036
if (column_types_flag)
2978
3038
print_field_types(result);
2979
if (!drizzle_num_rows(result))
3039
if (!drizzle_result_row_count(result))
2981
drizzle_field_seek(result,0);
3041
drizzle_column_seek(result,0);
2983
3043
separator.append("+");
2984
while ((field = drizzle_fetch_field(result)))
3044
while ((field = drizzle_column_next(result)))
2986
uint32_t length= column_names ? field->name_length : 0;
3046
uint32_t x, length= 0;
3050
uint32_t name_length= strlen(drizzle_column_name(field));
3052
/* Check if the max_byte value is really the maximum in terms
3053
of visual length since multibyte characters can affect the
3054
length of the separator. */
3055
length= charset_info->cset->numcells(charset_info,
3056
drizzle_column_name(field),
3057
drizzle_column_name(field) +
3060
if (name_length == drizzle_column_max_size(field))
3062
if (length < drizzle_column_max_size(field))
3063
drizzle_column_set_max_size(field, length);
3067
length= name_length;
2988
length=max(length,field->length);
3072
length=max(length,drizzle_column_size(field));
2990
length=max(length,field->max_length);
2991
if (length < 4 && !IS_NOT_NULL(field->flags))
3074
length=max(length,(uint32_t)drizzle_column_max_size(field));
3076
!(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
2992
3078
// Room for "NULL"
2994
field->max_length=length;
3081
drizzle_column_set_max_size(field, length);
2996
3083
for (x=0; x< (length+2); x++)
2997
3084
separator.append("-");
2998
3085
separator.append("+");
3001
3088
tee_puts((char*) separator.c_str(), PAGER);
3002
3089
if (column_names)
3004
drizzle_field_seek(result,0);
3091
drizzle_column_seek(result,0);
3005
3092
(void) tee_fputs("|", PAGER);
3006
for (uint off=0; (field = drizzle_fetch_field(result)) ; off++)
3093
for (uint32_t off=0; (field = drizzle_column_next(result)) ; off++)
3008
uint name_length= (uint) strlen(field->name);
3009
uint numcells= charset_info->cset->numcells(charset_info,
3011
field->name + name_length);
3012
uint32_t display_length= field->max_length + name_length - numcells;
3095
uint32_t name_length= (uint32_t) strlen(drizzle_column_name(field));
3096
uint32_t numcells= charset_info->cset->numcells(charset_info,
3097
drizzle_column_name(field),
3098
drizzle_column_name(field) +
3100
uint32_t display_length= drizzle_column_max_size(field) + name_length -
3013
3102
tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
3014
3103
MAX_COLUMN_LENGTH),
3016
num_flag[off]= IS_NUM(field->type);
3104
drizzle_column_name(field));
3105
num_flag[off]= ((drizzle_column_type(field) <= DRIZZLE_COLUMN_TYPE_LONGLONG) ||
3106
(drizzle_column_type(field) == DRIZZLE_COLUMN_TYPE_NEWDECIMAL));
3018
3108
(void) tee_fputs("\n", PAGER);
3019
3109
tee_puts((char*) separator.c_str(), PAGER);
3022
while ((cur= drizzle_fetch_row(result)))
3024
if (interrupted_query)
3116
cur= drizzle_row_buffer(result, &ret);
3117
if (ret != DRIZZLE_RETURN_OK)
3119
(void)put_error(&con, result);
3124
cur= drizzle_row_next(result);
3126
if (cur == NULL || interrupted_query)
3026
uint32_t *lengths= drizzle_fetch_lengths(result);
3129
size_t *lengths= drizzle_row_field_sizes(result);
3027
3130
(void) tee_fputs("| ", PAGER);
3028
drizzle_field_seek(result, 0);
3029
for (uint off= 0; off < drizzle_num_fields(result); off++)
3131
drizzle_column_seek(result, 0);
3132
for (uint32_t off= 0; off < drizzle_result_column_count(result); off++)
3031
3134
const char *buffer;
3033
uint field_max_length;
3034
uint visible_length;
3135
uint32_t data_length;
3136
uint32_t field_max_length;
3137
uint32_t visible_length;
3138
uint32_t extra_padding;
3037
3140
if (cur[off] == NULL)
3167
print_table_data_vertically(DRIZZLE_RES *result)
3275
print_table_data_vertically(drizzle_result_st *result)
3171
DRIZZLE_FIELD *field;
3278
drizzle_return_t ret;
3279
uint32_t max_length=0;
3280
drizzle_column_st *field;
3173
while ((field = drizzle_fetch_field(result)))
3282
while ((field = drizzle_column_next(result)))
3175
uint length= field->name_length;
3284
uint32_t length= strlen(drizzle_column_name(field));
3176
3285
if (length > max_length)
3177
3286
max_length= length;
3178
field->max_length=length;
3287
drizzle_column_set_max_size(field, length);
3181
drizzle_field_seek(result,0);
3182
for (uint row_count=1; (cur= drizzle_fetch_row(result)); row_count++)
3290
for (uint32_t row_count=1;; row_count++)
3184
if (interrupted_query)
3294
cur= drizzle_row_buffer(result, &ret);
3295
if (ret != DRIZZLE_RETURN_OK)
3297
(void)put_error(&con, result);
3302
cur= drizzle_row_next(result);
3304
if (cur == NULL || interrupted_query)
3186
drizzle_field_seek(result,0);
3306
drizzle_column_seek(result,0);
3187
3307
tee_fprintf(PAGER,
3188
3308
"*************************** %d. row ***************************\n", row_count);
3189
for (uint off=0; off < drizzle_num_fields(result); off++)
3309
for (uint32_t off=0; off < drizzle_result_column_count(result); off++)
3191
field= drizzle_fetch_field(result);
3192
tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
3311
field= drizzle_column_next(result);
3312
tee_fprintf(PAGER, "%*s: ",(int) max_length,drizzle_column_name(field));
3193
3313
tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3316
drizzle_row_free(result, cur);
3199
3321
/* print_warnings should be called right after executing a statement */
3201
static void print_warnings()
3323
static void print_warnings(uint32_t error_code)
3204
DRIZZLE_RES *result;
3326
drizzle_result_st result;
3206
3328
uint64_t num_rows;
3208
/* Save current error before calling "show warnings" */
3209
uint error= drizzle_errno(&drizzle);
3329
uint32_t new_code= 0;
3211
3331
/* Get the warnings */
3212
3332
query= "show warnings";
3213
drizzle_real_query_for_lazy(query, strlen(query));
3214
drizzle_store_result_for_lazy(&result);
3333
drizzleclient_real_query_for_lazy(query, strlen(query),&result,&new_code);
3334
drizzleclient_store_result_for_lazy(&result);
3216
3336
/* Bail out when no warnings */
3217
if (!(num_rows= drizzle_num_rows(result)))
3337
if (!(num_rows= drizzle_result_row_count(&result)))
3220
cur= drizzle_fetch_row(result);
3340
cur= drizzle_row_next(&result);
3223
3343
Don't print a duplicate of the current error. It is possible for SHOW
3282
print_tab_data(DRIZZLE_RES *result)
3405
print_tab_data(drizzle_result_st *result)
3285
DRIZZLE_FIELD *field;
3408
drizzle_return_t ret;
3409
drizzle_column_st *field;
3288
3412
if (opt_silent < 2 && column_names)
3291
while ((field = drizzle_fetch_field(result)))
3415
while ((field = drizzle_column_next(result)))
3294
3418
(void) tee_fputs("\t", PAGER);
3295
(void) tee_fputs(field->name, PAGER);
3419
(void) tee_fputs(drizzle_column_name(field), PAGER);
3297
3421
(void) tee_fputs("\n", PAGER);
3299
while ((cur = drizzle_fetch_row(result)))
3301
lengths= drizzle_fetch_lengths(result);
3427
cur= drizzle_row_buffer(result, &ret);
3428
if (ret != DRIZZLE_RETURN_OK)
3430
(void)put_error(&con, result);
3435
cur= drizzle_row_next(result);
3440
lengths= drizzle_row_field_sizes(result);
3302
3441
safe_put_field(cur[0],lengths[0]);
3303
for (uint off=1 ; off < drizzle_num_fields(result); off++)
3442
for (uint32_t off=1 ; off < drizzle_result_column_count(result); off++)
3305
3444
(void) tee_fputs("\t", PAGER);
3306
3445
safe_put_field(cur[off], lengths[off]);
3308
3447
(void) tee_fputs("\n", PAGER);
3449
drizzle_row_free(result, cur);
3313
com_tee(string *buffer __attribute__((unused)), const char *line )
3454
com_tee(string *, const char *line )
3315
char file_name[FN_REFLEN], *end, *param;
3456
char file_name[FN_REFLEN], *end;
3317
3459
if (status.batch)
3750
3903
valid_arg= ptr != start;
3751
return valid_arg ? start : NullS;
3904
return valid_arg ? start : NULL;
3756
3909
sql_connect(char *host,char *database,char *user,char *password,
3912
drizzle_return_t ret;
3762
drizzle_close(&drizzle);
3917
drizzle_con_free(&con);
3918
drizzle_free(&drizzle);
3764
3920
drizzle_create(&drizzle);
3921
if (drizzle_con_add_tcp(&drizzle, &con, host, opt_drizzle_port, user,
3922
password, database, DRIZZLE_CON_NONE) == NULL)
3924
(void) put_error(&con, NULL);
3925
(void) fflush(stdout);
3929
/* XXX add this back in
3765
3930
if (opt_connect_timeout)
3767
uint timeout=opt_connect_timeout;
3768
drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3932
uint32_t timeout=opt_connect_timeout;
3933
drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3769
3934
(char*) &timeout);
3772
drizzle_options(&drizzle,DRIZZLE_OPT_COMPRESS,NullS);
3773
if (opt_secure_auth)
3774
drizzle_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
3775
if (using_opt_local_infile)
3776
drizzle_options(&drizzle,DRIZZLE_OPT_LOCAL_INFILE, (char*) &opt_local_infile);
3777
drizzle_options(&drizzle,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
3938
/* XXX Do we need this?
3778
3939
if (safe_updates)
3780
3941
char init_command[100];
3781
3942
sprintf(init_command,
3782
3943
"SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3783
",SQL_MAX_JOIN_SIZE=%"PRIu32,
3944
",MAX_JOIN_SIZE=%"PRIu32,
3784
3945
select_limit, max_join_size);
3785
drizzle_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3946
drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3787
if (default_charset_used)
3788
drizzle_options(&drizzle, DRIZZLE_SET_CHARSET_NAME, default_charset);
3789
if (!drizzle_connect(&drizzle, host, user, password,
3790
database, opt_drizzle_port, opt_drizzle_unix_port,
3791
connect_flag | CLIENT_MULTI_STATEMENTS))
3949
if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
3794
(drizzle_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3795
drizzle_errno(&drizzle) != CR_CONNECTION_ERROR))
3951
if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
3952
ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
3797
(void) put_error(&drizzle);
3954
(void) put_error(&con, NULL);
3798
3955
(void) fflush(stdout);
3799
3956
return ignore_errors ? -1 : 1; // Abort
3801
3958
return -1; // Retryable
3804
3962
drizzle.reconnect= debug_info_flag; // We want to know if this happens
3805
3964
build_completion_hash(opt_rehash, 1);
3811
com_status(string *buffer __attribute__((unused)),
3812
const char *line __attribute__((unused)))
3970
com_status(string *, const char *)
3816
DRIZZLE_RES *result;
3976
drizzle_result_st result;
3977
drizzle_return_t ret;
3818
3979
tee_puts("--------------", stdout);
3819
3980
usage(1); /* Print version */
3822
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_thread_id(&drizzle));
3983
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_con_thread_id(&con));
3824
3985
Don't remove "limit 1",
3825
3986
it is protection againts SQL_SELECT_LIMIT=0
3827
if (!drizzle_query(&drizzle,"select DATABASE(), USER() limit 1") &&
3828
(result=drizzle_use_result(&drizzle)))
3988
if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
3989
&ret) != NULL && ret == DRIZZLE_RETURN_OK &&
3990
drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
3830
DRIZZLE_ROW cur=drizzle_fetch_row(result);
3992
drizzle_row_t cur=drizzle_row_next(&result);
3833
3995
tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3834
3996
tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3836
drizzle_free_result(result);
3998
drizzle_result_free(&result);
4000
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
4001
drizzle_result_free(&result);
3838
4002
tee_puts("SSL:\t\t\tNot in use", stdout);
3853
4017
tee_fprintf(stdout, "Current pager:\t\t%s\n", pager);
3854
4018
tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
3855
4019
tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
3856
tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&drizzle));
3857
tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_get_proto_info(&drizzle));
3858
tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_get_host_info(&drizzle));
3859
if ((id= drizzle_insert_id(&drizzle)))
4020
tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&con));
4021
tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_con_protocol_version(&con));
4022
tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_con_host(&con));
4023
/* XXX need to save this from result
4024
if ((id= drizzleclient_insert_id(&drizzle)))
3860
4025
tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
3862
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3863
if (!drizzle_query(&drizzle,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
3864
(result=drizzle_use_result(&drizzle)))
3866
DRIZZLE_ROW cur=drizzle_fetch_row(result);
3869
tee_fprintf(stdout, "Server characterset:\t%s\n", cur[2] ? cur[2] : "");
3870
tee_fprintf(stdout, "Db characterset:\t%s\n", cur[3] ? cur[3] : "");
3871
tee_fprintf(stdout, "Client characterset:\t%s\n", cur[0] ? cur[0] : "");
3872
tee_fprintf(stdout, "Conn. characterset:\t%s\n", cur[1] ? cur[1] : "");
3874
drizzle_free_result(result);
3878
/* Probably pre-4.1 server */
3879
tee_fprintf(stdout, "Client characterset:\t%s\n", charset_info->csname);
3880
tee_fprintf(stdout, "Server characterset:\t%s\n", drizzle.charset->csname);
3883
if (strstr(drizzle_get_host_info(&drizzle),"TCP/IP") || ! drizzle.unix_socket)
3884
tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle.port);
3886
tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle.unix_socket);
3887
if (drizzle.net.compress)
3888
tee_fprintf(stdout, "Protocol:\t\tCompressed\n");
4028
if (strcmp(drizzle_con_uds(&con), ""))
4029
tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle_con_uds(&con));
4031
tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle_con_port(&con));
3890
4033
if (safe_updates)
3906
4049
static const char *
3907
server_version_string(DRIZZLE *con)
4050
server_version_string(drizzle_con_st *local_con)
3909
static char buf[MAX_SERVER_VERSION_LENGTH] = "";
4052
static string buf("");
4053
static bool server_version_string_reserved= false;
4055
if (!server_version_string_reserved)
4057
buf.reserve(MAX_SERVER_VERSION_LENGTH);
4058
server_version_string_reserved= true;
3911
4060
/* Only one thread calls this, so no synchronization is needed */
3912
4061
if (buf[0] == '\0')
3915
DRIZZLE_RES *result;
4063
drizzle_result_st result;
4064
drizzle_return_t ret;
3917
bufp= stpncpy(buf, drizzle_get_server_info(con), sizeof buf);
4066
buf.append(drizzle_con_server_version(local_con));
3919
4068
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3920
if (!drizzle_query(con, "select @@version_comment limit 1") &&
3921
(result = drizzle_use_result(con)))
4069
(void)drizzle_query_str(local_con, &result,
4070
"select @@version_comment limit 1", &ret);
4071
if (ret == DRIZZLE_RETURN_OK &&
4072
drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
3923
DRIZZLE_ROW cur = drizzle_fetch_row(result);
4074
drizzle_row_t cur = drizzle_row_next(&result);
3924
4075
if (cur && cur[0])
3926
bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NullS);
3928
drizzle_free_result(result);
4080
drizzle_result_free(&result);
3931
/* str*nmov doesn't guarantee NUL-termination */
3932
if (bufp == buf + sizeof buf)
3933
buf[sizeof buf - 1] = '\0';
4082
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
4083
drizzle_result_free(&result);
3940
put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
4090
put_info(const char *str,INFO_TYPE info_type, uint32_t error, const char *sqlstate)
3942
4092
FILE *file= (info_type == INFO_ERROR ? stderr : stdout);
3943
4093
static int inited=0;
4318
4486
static void add_int_to_prompt(int toadd)
4321
int10_to_str(toadd, buffer, 10);
4322
processed_prompt->append(buffer);
4488
ostringstream buffer;
4490
processed_prompt->append(buffer.str().c_str());
4325
4493
static void init_username()
4327
my_free(full_username,MYF(MY_ALLOW_ZERO_PTR));
4328
my_free(part_username,MYF(MY_ALLOW_ZERO_PTR));
4496
free(full_username);
4497
free(part_username);
4330
DRIZZLE_RES *result;
4331
if (!drizzle_query(&drizzle,"select USER()") &&
4332
(result=drizzle_use_result(&drizzle)))
4499
drizzle_result_st *result;
4500
if (!drizzleclient_query(&drizzle,"select USER()") &&
4501
(result=drizzleclient_use_result(&drizzle)))
4334
DRIZZLE_ROW cur=drizzle_fetch_row(result);
4503
drizzle_row_t cur=drizzleclient_fetch_row(result);
4335
4504
full_username= strdup(cur[0]);
4336
4505
part_username= strdup(strtok(cur[0],"@"));
4337
(void) drizzle_fetch_row(result); // Read eof
4506
(void) drizzleclient_fetch_row(result); // Read eof
4341
static int com_prompt(string *buffer __attribute__((unused)),
4511
static int com_prompt(string *, const char *line)
4344
char *ptr=strchr(line, ' ');
4346
my_free(current_prompt, MYF(MY_ALLOW_ZERO_PTR));
4347
current_prompt= strdup(ptr ? ptr+1 : default_prompt);
4513
const char *ptr=strchr(line, ' ');
4349
4515
tee_fprintf(stdout, "Returning to default PROMPT of %s\n",
4350
4516
default_prompt);
4518
char * tmpptr= strdup(ptr ? ptr+1 : default_prompt);
4520
tee_fprintf(stdout, "Memory allocation error. Not changing prompt\n");
4523
free(current_prompt);
4524
current_prompt= tmpptr;
4352
4525
tee_fprintf(stdout, "PROMPT set to '%s'\n", current_prompt);
4357
4531
strcont(str, set) if str contanies any character in the string set.
4358
The result is the position of the first found character in str, or NullS
4532
The result is the position of the first found character in str, or NULL
4359
4533
if there isn't anything found.