156
static map<string, string>::iterator completion_iter;
157
static map<string, string>::iterator completion_end;
158
static map<string, string> completion_map;
159
static string completion_string;
149
161
static char **defaults_argv;
151
163
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
152
164
typedef enum enum_info_type INFO_TYPE;
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;
166
static drizzle_st drizzle; /* The library handle */
167
static drizzle_con_st con; /* The connection */
168
static bool ignore_errors= false, quick= false,
169
connected= false, opt_raw_data= false, unbuffered= false,
170
output_tables= false, opt_rehash= true, skip_updates= false,
171
safe_updates= false, one_database= false,
173
vertical= false, line_numbers= true, column_names= true,
174
opt_nopager= true, opt_outfile= false, named_cmds= false,
175
tty_password= false, opt_nobeep= false, opt_reconnect= true,
176
default_charset_used= false, opt_secure_auth= false,
177
default_pager_set= false, opt_sigint_ignore= false,
178
auto_vertical_output= false,
179
show_warnings= false, executing_query= false, interrupted_query= false;
180
static uint32_t show_progress_size= 0;
166
181
static bool debug_info_flag, debug_check_flag;
167
182
static bool column_types_flag;
168
static bool preserve_comments= 0;
169
static uint32_t opt_max_allowed_packet, opt_net_buffer_length;
170
static int verbose=0,opt_silent=0,opt_drizzle_port=0, opt_local_infile=0;
171
static uint my_end_arg;
172
static char * opt_drizzle_unix_port=0;
173
static int connect_flag=CLIENT_INTERACTIVE;
174
static char *current_host,*current_db,*current_user=0,*opt_password=0,
175
*delimiter_str= 0,* current_prompt= 0;
183
static bool preserve_comments= false;
184
static uint32_t opt_max_input_line, opt_drizzle_port= 0;
185
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
186
static uint32_t my_end_arg;
187
static char * opt_drizzle_unix_port= NULL;
188
static drizzle_capabilities_t connect_flag= DRIZZLE_CAPABILITIES_NONE;
189
static char *current_host, *current_db, *current_user= NULL,
190
*opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
176
191
static char *histfile;
177
192
static char *histfile_tmp;
178
193
static string *glob_buffer;
179
194
static string *processed_prompt= NULL;
180
195
static char *default_prompt= NULL;
181
static char *full_username=0,*part_username=0;
196
static char *full_username= NULL,*part_username= NULL;
182
197
static STATUS status;
183
198
static uint32_t select_limit;
184
199
static uint32_t max_join_size;
185
200
static uint32_t opt_connect_timeout= 0;
186
static char drizzle_charsets_dir[FN_REFLEN+1];
187
201
// TODO: Need to i18n these
188
static const char *day_names[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
189
static const char *month_names[]={"Jan","Feb","Mar","Apr","May","Jun","Jul",
202
static const char *day_names[]= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
203
static const char *month_names[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul",
190
204
"Aug","Sep","Oct","Nov","Dec"};
191
205
static char default_pager[FN_REFLEN];
192
206
static char pager[FN_REFLEN], outfile[FN_REFLEN];
193
207
static FILE *PAGER, *OUTFILE;
194
static MEM_ROOT hash_mem_root;
195
static uint prompt_counter;
208
static uint32_t prompt_counter;
196
209
static char delimiter[16]= DEFAULT_DELIMITER;
197
static uint delimiter_length= 1;
210
static uint32_t delimiter_length= 1;
198
211
unsigned short terminal_width= 80;
200
213
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
202
int drizzle_real_query_for_lazy(const char *buf, int length);
203
int drizzle_store_result_for_lazy(DRIZZLE_RES **result);
215
int drizzleclient_real_query_for_lazy(const char *buf, int length,
216
drizzle_result_st *result,
217
uint32_t *error_code);
218
int drizzleclient_store_result_for_lazy(drizzle_result_st *result);
206
221
void tee_fprintf(FILE *file, const char *fmt, ...);
963
965
{ "SUBSTR", 0, 0, 0, ""},
964
966
{ "SUBSTRING", 0, 0, 0, ""},
965
967
{ "SUBSTRING_INDEX", 0, 0, 0, ""},
966
{ "SUBTIME", 0, 0, 0, ""},
967
968
{ "SUM", 0, 0, 0, ""},
968
969
{ "SYSDATE", 0, 0, 0, ""},
969
970
{ "SYSTEM_USER", 0, 0, 0, ""},
970
971
{ "TAN", 0, 0, 0, ""},
971
972
{ "TIME_FORMAT", 0, 0, 0, ""},
972
{ "TIME_TO_SEC", 0, 0, 0, ""},
973
{ "TIMEDIFF", 0, 0, 0, ""},
974
973
{ "TO_DAYS", 0, 0, 0, ""},
975
974
{ "TOUCHES", 0, 0, 0, ""},
976
975
{ "TRIM", 0, 0, 0, ""},
1224
1230
If query is in process, kill query
1225
1231
no query in process, terminate like previous behavior
1227
RETSIGTYPE handle_sigint(int sig)
1234
void handle_sigint(int sig)
1229
1236
char kill_buffer[40];
1230
DRIZZLE *kill_drizzle= NULL;
1237
drizzle_con_st kill_drizzle;
1238
drizzle_result_st res;
1239
drizzle_return_t ret;
1232
1241
/* terminate if no query being executed, or we already tried interrupting */
1233
1242
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))
1246
if (drizzle_con_add_tcp(&drizzle, &kill_drizzle, current_host,
1247
opt_drizzle_port, current_user, opt_password, NULL,
1248
DRIZZLE_CON_NONE) == NULL)
1244
1253
/* 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);
1254
sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1255
drizzle_con_thread_id(&con));
1257
if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
1258
drizzle_result_free(&res);
1260
drizzle_con_free(&kill_drizzle);
1248
1261
tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1250
1263
interrupted_query= 1;
1277
1290
(char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
1279
1292
{"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."),
1293
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
1294
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1282
1295
{"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT,
1283
1296
N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
1284
1297
(char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1286
1299
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
1300
{"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
1291
1301
(char**) &column_types_flag, (char**) &column_types_flag,
1292
1302
0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1360
1370
{"no-pager", OPT_NOPAGER,
1361
1371
N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
1362
1372
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1364
1374
N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1365
1375
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, ")
1376
{"port", 'p', N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, ")
1367
1377
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},
1378
0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1370
1379
{"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
1371
1380
(char**) ¤t_prompt, (char**) ¤t_prompt, 0, GET_STR_ALLOC,
1372
1381
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
1383
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
1384
(char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1411
1418
{"connect_timeout", OPT_CONNECT_TIMEOUT,
1412
1419
N_("Number of seconds before connection timeout."),
1413
1420
(char**) &opt_connect_timeout,
1414
(char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 0, 0, 3600*12, 0,
1421
(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,
1423
{"max_input_line", OPT_MAX_INPUT_LINE,
1424
N_("Max length of input line"),
1425
(char**) &opt_max_input_line, (char**) &opt_max_input_line, 0,
1426
GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1420
1427
(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
1428
{"select_limit", OPT_SELECT_LIMIT,
1426
1429
N_("Automatic limit for SELECT when using --safe-updates"),
1427
1430
(char**) &select_limit,
1428
(char**) &select_limit, 0, GET_ULONG, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1431
(char**) &select_limit, 0, GET_UINT32, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1430
1433
{"max_join_size", OPT_MAX_JOIN_SIZE,
1431
1434
N_("Automatic limit for rows in a join when using --safe-updates"),
1432
1435
(char**) &max_join_size,
1433
(char**) &max_join_size, 0, GET_ULONG, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1436
(char**) &max_join_size, 0, GET_UINT32, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1435
1438
{"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
1439
(char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1437
1440
{"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1438
1441
(char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1439
1442
0, 0, 0, 0, 0, 0},
1443
{"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1444
(char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
1440
1446
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1446
1452
const char* readline= "readline";
1448
1454
printf(_("%s Ver %s Distrib %s, for %s (%s) using %s %s\n"),
1449
my_progname, VER, drizzle_get_client_info(),
1455
my_progname, VER.c_str(), drizzle_version(),
1450
1456
SYSTEM_TYPE, MACHINE_TYPE,
1451
1457
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"));
1461
printf(_("Copyright (C) 2008 Sun Microsystems\n"
1462
"This software comes with ABSOLUTELY NO WARRANTY. "
1463
"This is free software,\n"
1464
"and you are welcome to modify and redistribute it "
1465
"under the GPL license\n"));
1459
1466
printf(_("Usage: %s [OPTIONS] [database]\n"), my_progname);
1460
1467
my_print_help(my_long_options);
1461
print_defaults("my", load_default_groups);
1468
print_defaults("drizzle", load_default_groups);
1462
1469
my_print_variables(my_long_options);
1467
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
1474
get_one_option(int optid, const struct my_option *, char *argument)
1476
char *endchar= NULL;
1477
uint64_t temp_drizzle_port= 0;
1470
1479
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
1480
case OPT_DEFAULT_CHARSET:
1476
1481
default_charset_used= 1;
1478
1483
case OPT_DELIMITER:
1479
1484
if (argument == disabled_my_option)
1481
my_stpcpy(delimiter, DEFAULT_DELIMITER);
1486
strcpy(delimiter, DEFAULT_DELIMITER);
1485
1490
/* Check that delimiter does not contain a backslash */
1486
1491
if (!strstr(argument, "\\"))
1488
strmake(delimiter, argument, sizeof(delimiter) - 1);
1493
strncpy(delimiter, argument, sizeof(delimiter) - 1);
1492
1497
put_info(_("DELIMITER cannot contain a backslash character"),
1493
1498
INFO_ERROR,0,0);
1497
delimiter_length= (uint)strlen(delimiter);
1502
delimiter_length= (uint32_t)strlen(delimiter);
1498
1503
delimiter_str= delimiter;
1500
case OPT_LOCAL_INFILE:
1501
using_opt_local_infile=1;
1504
1506
if (argument == disabled_my_option)
2194
2222
return (char**) 0;
2198
static char *new_command_generator(const char *text,int state)
2225
inline string lower_string(const string &from_string)
2227
string to_string= from_string;
2228
transform(to_string.begin(), to_string.end(),
2229
to_string.begin(), ::tolower);
2232
inline string lower_string(const char * from_string)
2234
string to_string= from_string;
2235
return lower_string(to_string);
2239
class CompletionMatch :
2240
public unary_function<const string&, bool>
2245
CompletionMatch(string text) : match_text(text) {}
2246
inline bool operator() (const pair<string,string> &match_against) const
2249
lower_string(match_against.first.substr(0,match_text.size()));
2250
return match_func(sub_match,match_text);
2257
char *new_command_generator(const char *text, int state)
2207
textlen=(uint) strlen(text);
2210
{ /* lookup in the hash */
2215
b = find_all_matches(&ht,text,(uint) strlen(text),&len);
2223
ptr= strdup(e->str);
2229
{ /* traverse the entire hash, ugly but works */
2233
/* find the first used bucket */
2234
for (i=0 ; i < ht.nTableSize ; i++)
2236
if (ht.arBuckets[i])
2238
b = ht.arBuckets[i];
2246
{ /* find valid entry in bucket */
2247
if ((uint) strlen(e->str) == b->nKeyLength)
2248
ptr = strdup(e->str);
2249
/* find the next used entry */
2252
{ /* find the next used bucket */
2256
for (i++ ; i<ht.nTableSize; i++)
2258
if (ht.arBuckets[i])
2260
b = ht.arBuckets[i];
2262
completion_string= lower_string(text);
2263
if (completion_string.size() == 0)
2265
completion_iter= completion_map.begin();
2266
completion_end= completion_map.end();
2270
completion_iter= find_if(completion_map.begin(), completion_map.end(),
2271
CompletionMatch<equal_to<string> >(completion_string));
2272
completion_end= find_if(completion_iter, completion_map.end(),
2273
CompletionMatch<not_equal_to<string> >(completion_string));
2276
if (completion_iter == completion_end || (size_t)state > completion_map.size())
2278
char *result= (char *)malloc((*completion_iter).second.size()+1);
2279
strcpy(result, (*completion_iter).second.c_str());
2277
2284
/* Build up the completion hash */
2279
2286
static void build_completion_hash(bool rehash, bool write_info)
2281
2288
COMMANDS *cmd=commands;
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
2289
drizzle_return_t ret;
2290
drizzle_result_st databases,tables,fields;
2291
drizzle_row_t database_row,table_row;
2292
drizzle_column_st *sql_field;
2293
string tmp_str, tmp_str_lower;
2291
2295
if (status.batch || quick || !current_db)
2292
2296
return; // We don't need completion in batches
2296
/* Free old used memory */
2299
completion_hash_clean(&ht);
2300
free_root(&hash_mem_root,MYF(0));
2300
completion_map.clear();
2302
2302
/* hash this file's known subset of SQL commands */
2303
2303
while (cmd->name) {
2304
add_word(&ht,(char*) cmd->name);
2305
tmp_str_lower= lower_string(tmp_str);
2306
completion_map[tmp_str_lower]= tmp_str;
2308
2310
/* hash Drizzle functions (to be implemented) */
2310
2312
/* hash all database names */
2311
if (drizzle_query(&drizzle,"show databases") == 0)
2313
if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
2313
if (!(databases = drizzle_store_result(&drizzle)))
2314
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2315
if (ret == DRIZZLE_RETURN_OK)
2317
while ((database_row=drizzle_fetch_row(databases)))
2317
if (drizzle_result_buffer(&databases) != DRIZZLE_RETURN_OK)
2318
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2319
char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
2321
add_word(&ht,(char*) str);
2321
while ((database_row=drizzle_row_next(&databases)))
2323
tmp_str= database_row[0];
2324
tmp_str_lower= lower_string(tmp_str);
2325
completion_map[tmp_str_lower]= tmp_str;
2323
drizzle_free_result(databases);
2330
drizzle_result_free(&databases);
2326
2333
/* hash all table names */
2327
if (drizzle_query(&drizzle,"show tables")==0)
2334
if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
2329
if (!(tables = drizzle_store_result(&drizzle)))
2336
if (ret != DRIZZLE_RETURN_OK)
2338
drizzle_result_free(&tables);
2342
if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
2330
2343
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2333
if (drizzle_num_rows(tables) > 0 && !opt_silent && write_info)
2346
if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
2335
2348
tee_fprintf(stdout, _("\
2336
2349
Reading table information for completion of table and column names\n \
2337
2350
You can turn off this feature to get a quicker startup with -A\n\n"));
2339
while ((table_row=drizzle_fetch_row(tables)))
2352
while ((table_row=drizzle_row_next(&tables)))
2341
char *str=strdup_root(&hash_mem_root, (char*) table_row[0]);
2343
!completion_hash_exists(&ht,(char*) str, (uint) strlen(str)))
2354
tmp_str= table_row[0];
2355
tmp_str_lower= lower_string(tmp_str);
2356
completion_map[tmp_str_lower]= tmp_str;
2349
2363
/* hash all field names, both with the table prefix and without it */
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)))
2364
if (drizzle_result_row_count(&tables) == 0)
2366
drizzle_result_free(&tables);
2370
drizzle_row_seek(&tables, 0);
2372
while ((table_row=drizzle_row_next(&tables)))
2376
query.append("show fields in '");
2377
query.append(table_row[0]);
2380
if (drizzle_query(&con, &fields, query.c_str(), query.length(),
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);
2383
if (ret == DRIZZLE_RETURN_OK &&
2384
drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2386
while ((sql_field=drizzle_column_next(&fields)))
2388
tmp_str=table_row[0];
2389
tmp_str.append(".");
2390
tmp_str.append(drizzle_column_name(sql_field));
2391
tmp_str_lower= lower_string(tmp_str);
2392
completion_map[tmp_str_lower]= tmp_str;
2394
tmp_str=drizzle_column_name(sql_field);
2395
tmp_str_lower= lower_string(tmp_str);
2396
completion_map[tmp_str_lower]= tmp_str;
2399
drizzle_result_free(&fields);
2395
drizzle_free_result(tables);
2396
field_names[i]=0; // End pointer
2402
drizzle_result_free(&tables);
2403
completion_iter= completion_map.begin();
2400
2406
/* for gnu readline */
2461
2471
The different commands
2462
2472
***************************************************************************/
2464
int drizzle_real_query_for_lazy(const char *buf, int length)
2474
int drizzleclient_real_query_for_lazy(const char *buf, int length,
2475
drizzle_result_st *result,
2476
uint32_t *error_code)
2466
for (uint retry=0;; retry++)
2478
drizzle_return_t ret;
2480
for (uint32_t retry=0;; retry++)
2469
if (!drizzle_real_query(&drizzle,buf,length))
2483
if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2484
ret == DRIZZLE_RETURN_OK)
2471
error= put_error(&drizzle);
2472
if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR || retry > 1 ||
2488
error= put_error(&con, result);
2490
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2492
*error_code= drizzle_result_error_code(result);
2493
drizzle_result_free(result);
2496
if (ret != DRIZZLE_RETURN_SERVER_GONE || retry > 1 ||
2473
2497
!opt_reconnect)
2475
2502
if (reconnect())
2480
int drizzle_store_result_for_lazy(DRIZZLE_RES **result)
2507
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
2482
if ((*result=drizzle_store_result(&drizzle)))
2509
if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
2485
if (drizzle_error(&drizzle)[0])
2486
return put_error(&drizzle);
2512
if (drizzle_con_error(&con)[0])
2513
return put_error(&con, result);
2490
static void print_help_item(DRIZZLE_ROW *cur, int num_name, int num_cat, char *last_char)
2492
char ccat= (*cur)[num_cat][0];
2493
if (*last_char != ccat)
2495
put_info(ccat == 'Y' ? _("categories:") : _("topics:"), INFO_INFO,0,0);
2498
tee_fprintf(PAGER, " %s\n", (*cur)[num_name]);
2502
static int com_server_help(string *buffer,
2503
const char *line __attribute__((unused)),
2507
const char *server_cmd= buffer->c_str();
2509
DRIZZLE_RES *result;
2512
if (help_arg[0] != '\'')
2514
char *end_arg= strchr(help_arg, '\0');
2517
while (my_isspace(charset_info,*end_arg))
2521
(void) strxnmov(cmd_buf, sizeof(cmd_buf), "help '", help_arg, "'", NULL);
2522
server_cmd= cmd_buf;
2525
if (!connected && reconnect())
2528
if ((error= drizzle_real_query_for_lazy(server_cmd,(int)strlen(server_cmd))) ||
2529
(error= drizzle_store_result_for_lazy(&result)))
2534
unsigned int num_fields= drizzle_num_fields(result);
2535
uint64_t num_rows= drizzle_num_rows(result);
2536
drizzle_fetch_fields(result);
2537
if (num_fields==3 && num_rows==1)
2539
if (!(cur= drizzle_fetch_row(result)))
2546
tee_fprintf(PAGER, _("Name: \'%s\'\n"), cur[0]);
2547
tee_fprintf(PAGER, _("Description:\n%s"), cur[1]);
2548
if (cur[2] && *((char*)cur[2]))
2549
tee_fprintf(PAGER, _("Examples:\n%s"), cur[2]);
2550
tee_fprintf(PAGER, "\n");
2553
else if (num_fields >= 2 && num_rows)
2558
int num_name= 0, num_cat= 0;
2560
if (num_fields == 2)
2562
put_info(_("Many help items for your request exist."), INFO_INFO,0,0);
2563
put_info(_("To make a more specific request, please type 'help <item>',\nwhere <item> is one of the following"), INFO_INFO,0,0);
2567
else if ((cur= drizzle_fetch_row(result)))
2569
tee_fprintf(PAGER, _("You asked for help about help category: '%s'\n"), cur[0]);
2570
put_info(_("For more information, type 'help <item>', where <item> is one of the following"), INFO_INFO,0,0);
2573
print_help_item(&cur,1,2,&last_char);
2576
while ((cur= drizzle_fetch_row(result)))
2577
print_help_item(&cur,num_name,num_cat,&last_char);
2578
tee_fprintf(PAGER, "\n");
2583
put_info(_("\nNothing found"), INFO_INFO,0,0);
2584
put_info(_("Please try to run 'help contents' for a list of all accessible topics\n"), INFO_INFO,0,0);
2589
drizzle_free_result(result);
2594
com_help(string *buffer __attribute__((unused)),
2595
const char *line __attribute__((unused)))
2518
com_help(string *buffer, const char *)
2597
2520
register int i, j;
2598
char * help_arg= strchr(line,' '), buff[32], *end;
2601
while (my_isspace(charset_info,*help_arg))
2603
if (*help_arg) return com_server_help(buffer,line,help_arg);
2521
char buff[32], *end;
2606
2523
put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
2607
2524
if (!named_cmds)
2608
2525
put_info(_("Note that all text commands must be first on line and end with ';'"),INFO_INFO,0,0);
2609
2526
for (i = 0; commands[i].name; i++)
2611
end= my_stpcpy(buff, commands[i].name);
2528
end= strcpy(buff, commands[i].name);
2529
end+= strlen(commands[i].name);
2612
2530
for (j= (int)strlen(commands[i].name); j < 10; j++)
2613
end= my_stpcpy(end, " ");
2531
end= strcpy(end, " ")+1;
2614
2532
if (commands[i].func)
2615
2533
tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
2616
2534
commands[i].cmd_char, _(commands[i].doc));
2618
if (connected && drizzle_get_server_version(&drizzle) >= 40100)
2619
put_info(_("\nFor server side help, type 'help contents'\n"), INFO_INFO,0,0);
2536
tee_fprintf(stdout, "\n");
2625
com_clear(string *buffer,
2626
const char *line __attribute__((unused)))
2543
com_clear(string *buffer, const char *)
2628
2545
if (status.add_to_history)
2629
2546
fix_history(buffer);
2719
2637
time_buff[0]= '\0';
2721
2639
/* Every branch must truncate buff . */
2640
if (drizzle_result_column_count(&result) > 0)
2724
if (!drizzle_num_rows(result) && ! quick && !column_types_flag)
2642
if (!quick && drizzle_result_row_count(&result) == 0 &&
2726
my_stpcpy(buff, _("Empty set"));
2645
strcpy(buff, _("Empty set"));
2731
2650
if (vertical || (auto_vertical_output &&
2732
(terminal_width < get_result_width(result))))
2733
print_table_data_vertically(result);
2651
(terminal_width < get_result_width(&result))))
2652
print_table_data_vertically(&result);
2734
2653
else if (opt_silent && verbose <= 2 && !output_tables)
2735
print_tab_data(result);
2654
print_tab_data(&result);
2737
print_table_data(result);
2656
print_table_data(&result);
2739
2658
ngettext("%ld row in set","%ld rows in set",
2740
(long) drizzle_num_rows(result)),
2741
(long) drizzle_num_rows(result));
2659
(long) drizzle_result_row_count(&result)),
2660
(long) drizzle_result_row_count(&result));
2743
if (drizzle_errno(&drizzle))
2744
error= put_error(&drizzle);
2662
if (drizzle_result_error_code(&result))
2663
error= put_error(&con, &result);
2747
else if (drizzle_affected_rows(&drizzle) == ~(uint64_t) 0)
2748
my_stpcpy(buff,_("Query OK"));
2666
else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2667
strcpy(buff,_("Query OK"));
2750
2669
sprintf(buff, ngettext("Query OK, %ld row affected",
2751
2670
"Query OK, %ld rows affected",
2752
(long) drizzle_affected_rows(&drizzle)),
2753
(long) drizzle_affected_rows(&drizzle));
2671
(long) drizzle_result_affected_rows(&result)),
2672
(long) drizzle_result_affected_rows(&result));
2755
2674
pos= strchr(buff, '\0');
2756
if ((warnings= drizzle_warning_count(&drizzle)))
2675
if ((warnings= drizzle_result_warning_count(&result)))
2760
pos=int10_to_str(warnings, pos, 10);
2761
pos=my_stpcpy(pos, " warning");
2679
pos= int10_to_str(warnings, pos, 10);
2680
pos= strcpy(pos, " warning")+8;
2762
2681
if (warnings != 1)
2765
my_stpcpy(pos, time_buff);
2684
strcpy(pos, time_buff);
2766
2685
put_info(buff,INFO_RESULT,0,0);
2767
if (drizzle_info(&drizzle))
2768
put_info(drizzle_info(&drizzle),INFO_RESULT,0,0);
2686
if (strcmp(drizzle_result_info(&result), ""))
2687
put_info(drizzle_result_info(&result),INFO_RESULT,0,0);
2769
2688
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)
2774
2691
fflush(stdout);
2775
drizzle_free_result(result);
2776
} while (!(err= drizzle_next_result(&drizzle)));
2692
drizzle_result_free(&result);
2694
if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2696
if (drizzle_result_read(&con, &result, &ret) == NULL ||
2697
ret != DRIZZLE_RETURN_OK)
2699
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2701
error_code= drizzle_result_error_code(&result);
2702
drizzle_result_free(&result);
2705
error= put_error(&con, NULL);
2710
} while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2778
error= put_error(&drizzle);
2712
error= put_error(&con, NULL);
2782
2716
/* Show warnings if any or error occured */
2783
2717
if (show_warnings == 1 && (warnings >= 1 || error))
2718
print_warnings(error_code);
2786
2720
if (!error && !status.batch &&
2787
(drizzle.server_status & SERVER_STATUS_DB_DROPPED))
2721
drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2788
2723
get_current_db();
2790
2726
executing_query= 0;
2791
2727
return error; /* New command follows */
2855
static const char *fieldtype2str(enum enum_field_types type)
2792
static const char *fieldtype2str(drizzle_column_type_t type)
2857
2794
switch (type) {
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";
2795
case DRIZZLE_COLUMN_TYPE_BLOB: return "BLOB";
2796
case DRIZZLE_COLUMN_TYPE_DATE: return "DATE";
2797
case DRIZZLE_COLUMN_TYPE_DATETIME: return "DATETIME";
2798
case DRIZZLE_COLUMN_TYPE_NEWDECIMAL: return "DECIMAL";
2799
case DRIZZLE_COLUMN_TYPE_DOUBLE: return "DOUBLE";
2800
case DRIZZLE_COLUMN_TYPE_ENUM: return "ENUM";
2801
case DRIZZLE_COLUMN_TYPE_LONG: return "LONG";
2802
case DRIZZLE_COLUMN_TYPE_LONGLONG: return "LONGLONG";
2803
case DRIZZLE_COLUMN_TYPE_NULL: return "NULL";
2804
case DRIZZLE_COLUMN_TYPE_TIMESTAMP: return "TIMESTAMP";
2805
case DRIZZLE_COLUMN_TYPE_TINY: return "TINY";
2806
case DRIZZLE_COLUMN_TYPE_VIRTUAL: return "VIRTUAL";
2870
2807
default: return "?-unknown-?";
2874
static char *fieldflags2str(uint f) {
2811
static char *fieldflags2str(uint32_t f) {
2875
2812
static char buf[1024];
2878
2815
#define ff2s_check_flag(X) \
2879
if (f & X ## _FLAG) { s=my_stpcpy(s, # X " "); f &= ~ X ## _FLAG; }
2816
if (f & DRIZZLE_COLUMN_FLAGS_ ## X) { s=strcpy(s, # X " ")+strlen(# X " "); \
2817
f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
2880
2818
ff2s_check_flag(NOT_NULL);
2881
2819
ff2s_check_flag(PRI_KEY);
2882
2820
ff2s_check_flag(UNIQUE_KEY);
2921
2859
"Decimals: %u\n"
2922
2860
"Flags: %s\n\n",
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));
2862
drizzle_column_name(field), drizzle_column_catalog(field),
2863
drizzle_column_db(field), drizzle_column_table(field),
2864
drizzle_column_orig_table(field),
2865
fieldtype2str(drizzle_column_type(field)),
2866
get_charset_name(drizzle_column_charset(field)),
2867
drizzle_column_charset(field), drizzle_column_size(field),
2868
drizzle_column_max_size(field), drizzle_column_decimals(field),
2869
fieldflags2str(drizzle_column_flags(field)));
2930
2871
tee_puts("", PAGER);
2935
print_table_data(DRIZZLE_RES *result)
2876
print_table_data(drizzle_result_st *result)
2938
DRIZZLE_FIELD *field;
2879
drizzle_return_t ret;
2880
drizzle_column_st *field;
2940
2882
string separator;
2942
2884
separator.reserve(256);
2944
num_flag=(bool*) my_malloc(sizeof(bool)*drizzle_num_fields(result),
2886
num_flag=(bool*) malloc(sizeof(bool)*drizzle_result_column_count(result));
2946
2887
if (column_types_flag)
2948
2889
print_field_types(result);
2949
if (!drizzle_num_rows(result))
2890
if (!drizzle_result_row_count(result))
2951
drizzle_field_seek(result,0);
2892
drizzle_column_seek(result,0);
2953
2894
separator.append("+");
2954
while ((field = drizzle_fetch_field(result)))
2895
while ((field = drizzle_column_next(result)))
2956
uint32_t length= column_names ? field->name_length : 0;
2897
uint32_t x, length= 0;
2901
uint32_t name_length= strlen(drizzle_column_name(field));
2903
/* Check if the max_byte value is really the maximum in terms
2904
of visual length since multibyte characters can affect the
2905
length of the separator. */
2906
length= charset_info->cset->numcells(charset_info,
2907
drizzle_column_name(field),
2908
drizzle_column_name(field) +
2911
if (name_length == drizzle_column_max_size(field))
2913
if (length < drizzle_column_max_size(field))
2914
drizzle_column_set_max_size(field, length);
2918
length= name_length;
2958
length=max(length,field->length);
2923
length=max(length,drizzle_column_size(field));
2960
length=max(length,field->max_length);
2961
if (length < 4 && !(field->flags & NOT_NULL_FLAG))
2925
length=max(length,(uint32_t)drizzle_column_max_size(field));
2927
!(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
2962
2929
// Room for "NULL"
2964
field->max_length=length;
2932
drizzle_column_set_max_size(field, length);
2966
2934
for (x=0; x< (length+2); x++)
2967
2935
separator.append("-");
2968
2936
separator.append("+");
2971
2939
tee_puts((char*) separator.c_str(), PAGER);
2972
2940
if (column_names)
2974
drizzle_field_seek(result,0);
2942
drizzle_column_seek(result,0);
2975
2943
(void) tee_fputs("|", PAGER);
2976
for (uint off=0; (field = drizzle_fetch_field(result)) ; off++)
2944
for (uint32_t off=0; (field = drizzle_column_next(result)) ; off++)
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;
2946
uint32_t name_length= (uint32_t) strlen(drizzle_column_name(field));
2947
uint32_t numcells= charset_info->cset->numcells(charset_info,
2948
drizzle_column_name(field),
2949
drizzle_column_name(field) +
2951
uint32_t display_length= drizzle_column_max_size(field) + name_length -
2983
2953
tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
2984
2954
MAX_COLUMN_LENGTH),
2986
num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) ||
2987
(field->type == DRIZZLE_TYPE_NEWDECIMAL));
2955
drizzle_column_name(field));
2956
num_flag[off]= ((drizzle_column_type(field) <= DRIZZLE_COLUMN_TYPE_LONGLONG) ||
2957
(drizzle_column_type(field) == DRIZZLE_COLUMN_TYPE_NEWDECIMAL));
2989
2959
(void) tee_fputs("\n", PAGER);
2990
2960
tee_puts((char*) separator.c_str(), PAGER);
2993
while ((cur= drizzle_fetch_row(result)))
2995
if (interrupted_query)
2967
cur= drizzle_row_buffer(result, &ret);
2968
if (ret != DRIZZLE_RETURN_OK)
2970
(void)put_error(&con, result);
2975
cur= drizzle_row_next(result);
2977
if (cur == NULL || interrupted_query)
2997
uint32_t *lengths= drizzle_fetch_lengths(result);
2980
size_t *lengths= drizzle_row_field_sizes(result);
2998
2981
(void) tee_fputs("| ", PAGER);
2999
drizzle_field_seek(result, 0);
3000
for (uint off= 0; off < drizzle_num_fields(result); off++)
2982
drizzle_column_seek(result, 0);
2983
for (uint32_t off= 0; off < drizzle_result_column_count(result); off++)
3002
2985
const char *buffer;
3004
uint field_max_length;
3005
uint visible_length;
2986
uint32_t data_length;
2987
uint32_t field_max_length;
2988
uint32_t visible_length;
2989
uint32_t extra_padding;
3008
2991
if (cur[off] == NULL)
3138
print_table_data_vertically(DRIZZLE_RES *result)
3126
print_table_data_vertically(drizzle_result_st *result)
3142
DRIZZLE_FIELD *field;
3129
drizzle_return_t ret;
3130
uint32_t max_length=0;
3131
drizzle_column_st *field;
3144
while ((field = drizzle_fetch_field(result)))
3133
while ((field = drizzle_column_next(result)))
3146
uint length= field->name_length;
3135
uint32_t length= strlen(drizzle_column_name(field));
3147
3136
if (length > max_length)
3148
3137
max_length= length;
3149
field->max_length=length;
3138
drizzle_column_set_max_size(field, length);
3152
drizzle_field_seek(result,0);
3153
for (uint row_count=1; (cur= drizzle_fetch_row(result)); row_count++)
3141
for (uint32_t row_count=1;; row_count++)
3155
if (interrupted_query)
3145
cur= drizzle_row_buffer(result, &ret);
3146
if (ret != DRIZZLE_RETURN_OK)
3148
(void)put_error(&con, result);
3153
cur= drizzle_row_next(result);
3155
if (cur == NULL || interrupted_query)
3157
drizzle_field_seek(result,0);
3157
drizzle_column_seek(result,0);
3158
3158
tee_fprintf(PAGER,
3159
3159
"*************************** %d. row ***************************\n", row_count);
3160
for (uint off=0; off < drizzle_num_fields(result); off++)
3160
for (uint32_t off=0; off < drizzle_result_column_count(result); off++)
3162
field= drizzle_fetch_field(result);
3163
tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
3162
field= drizzle_column_next(result);
3163
tee_fprintf(PAGER, "%*s: ",(int) max_length,drizzle_column_name(field));
3164
3164
tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3167
drizzle_row_free(result, cur);
3170
3172
/* print_warnings should be called right after executing a statement */
3172
static void print_warnings()
3174
static void print_warnings(uint32_t error_code)
3175
DRIZZLE_RES *result;
3177
drizzle_result_st result;
3177
3179
uint64_t num_rows;
3179
/* Save current error before calling "show warnings" */
3180
uint error= drizzle_errno(&drizzle);
3180
uint32_t new_code= 0;
3182
3182
/* Get the warnings */
3183
3183
query= "show warnings";
3184
drizzle_real_query_for_lazy(query, strlen(query));
3185
drizzle_store_result_for_lazy(&result);
3184
drizzleclient_real_query_for_lazy(query, strlen(query),&result,&new_code);
3185
drizzleclient_store_result_for_lazy(&result);
3187
3187
/* Bail out when no warnings */
3188
if (!(num_rows= drizzle_num_rows(result)))
3188
if (!(num_rows= drizzle_result_row_count(&result)))
3191
cur= drizzle_fetch_row(result);
3191
cur= drizzle_row_next(&result);
3194
3194
Don't print a duplicate of the current error. It is possible for SHOW
3253
print_tab_data(DRIZZLE_RES *result)
3256
print_tab_data(drizzle_result_st *result)
3256
DRIZZLE_FIELD *field;
3259
drizzle_return_t ret;
3260
drizzle_column_st *field;
3259
3263
if (opt_silent < 2 && column_names)
3262
while ((field = drizzle_fetch_field(result)))
3266
while ((field = drizzle_column_next(result)))
3265
3269
(void) tee_fputs("\t", PAGER);
3266
(void) tee_fputs(field->name, PAGER);
3270
(void) tee_fputs(drizzle_column_name(field), PAGER);
3268
3272
(void) tee_fputs("\n", PAGER);
3270
while ((cur = drizzle_fetch_row(result)))
3272
lengths= drizzle_fetch_lengths(result);
3278
cur= drizzle_row_buffer(result, &ret);
3279
if (ret != DRIZZLE_RETURN_OK)
3281
(void)put_error(&con, result);
3286
cur= drizzle_row_next(result);
3291
lengths= drizzle_row_field_sizes(result);
3273
3292
safe_put_field(cur[0],lengths[0]);
3274
for (uint off=1 ; off < drizzle_num_fields(result); off++)
3293
for (uint32_t off=1 ; off < drizzle_result_column_count(result); off++)
3276
3295
(void) tee_fputs("\t", PAGER);
3277
3296
safe_put_field(cur[off], lengths[off]);
3279
3298
(void) tee_fputs("\n", PAGER);
3300
drizzle_row_free(result, cur);
3284
com_tee(string *buffer __attribute__((unused)), const char *line )
3305
com_tee(string *, const char *line )
3286
char file_name[FN_REFLEN], *end, *param;
3307
char file_name[FN_REFLEN], *end;
3288
3310
if (status.batch)
3727
3760
sql_connect(char *host,char *database,char *user,char *password,
3763
drizzle_return_t ret;
3733
drizzle_close(&drizzle);
3768
drizzle_con_free(&con);
3769
drizzle_free(&drizzle);
3735
3771
drizzle_create(&drizzle);
3772
if (drizzle_con_add_tcp(&drizzle, &con, host, opt_drizzle_port, user,
3773
password, database, DRIZZLE_CON_NONE) == NULL)
3775
(void) put_error(&con, NULL);
3776
(void) fflush(stdout);
3780
/* XXX add this back in
3736
3781
if (opt_connect_timeout)
3738
uint timeout=opt_connect_timeout;
3739
drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3783
uint32_t timeout=opt_connect_timeout;
3784
drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3740
3785
(char*) &timeout);
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);
3789
/* XXX Do we need this?
3748
3790
if (safe_updates)
3750
3792
char init_command[100];
3751
3793
sprintf(init_command,
3752
3794
"SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3753
",SQL_MAX_JOIN_SIZE=%"PRIu32,
3795
",MAX_JOIN_SIZE=%"PRIu32,
3754
3796
select_limit, max_join_size);
3755
drizzle_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3797
drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3757
if (!drizzle_connect(&drizzle, host, user, password,
3758
database, opt_drizzle_port, opt_drizzle_unix_port,
3759
connect_flag | CLIENT_MULTI_STATEMENTS))
3800
if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
3762
(drizzle_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3763
drizzle_errno(&drizzle) != CR_CONNECTION_ERROR))
3802
if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
3803
ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
3765
(void) put_error(&drizzle);
3805
(void) put_error(&con, NULL);
3766
3806
(void) fflush(stdout);
3767
3807
return ignore_errors ? -1 : 1; // Abort
3769
3809
return -1; // Retryable
3772
3813
drizzle.reconnect= debug_info_flag; // We want to know if this happens
3773
3815
build_completion_hash(opt_rehash, 1);
3779
com_status(string *buffer __attribute__((unused)),
3780
const char *line __attribute__((unused)))
3821
com_status(string *, const char *)
3784
DRIZZLE_RES *result;
3827
drizzle_result_st result;
3828
drizzle_return_t ret;
3786
3830
tee_puts("--------------", stdout);
3787
3831
usage(1); /* Print version */
3790
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_thread_id(&drizzle));
3834
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_con_thread_id(&con));
3792
3836
Don't remove "limit 1",
3793
3837
it is protection againts SQL_SELECT_LIMIT=0
3795
if (!drizzle_query(&drizzle,"select DATABASE(), USER() limit 1") &&
3796
(result=drizzle_use_result(&drizzle)))
3839
if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
3840
&ret) != NULL && ret == DRIZZLE_RETURN_OK &&
3841
drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
3798
DRIZZLE_ROW cur=drizzle_fetch_row(result);
3843
drizzle_row_t cur=drizzle_row_next(&result);
3801
3846
tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3802
3847
tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3804
drizzle_free_result(result);
3849
drizzle_result_free(&result);
3851
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
3852
drizzle_result_free(&result);
3806
3853
tee_puts("SSL:\t\t\tNot in use", stdout);
3821
3868
tee_fprintf(stdout, "Current pager:\t\t%s\n", pager);
3822
3869
tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
3823
3870
tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
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)))
3871
tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&con));
3872
tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_con_protocol_version(&con));
3873
tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_con_host(&con));
3874
/* XXX need to save this from result
3875
if ((id= drizzleclient_insert_id(&drizzle)))
3828
3876
tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
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);
3879
if (strcmp(drizzle_con_uds(&con), ""))
3880
tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle_con_uds(&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");
3882
tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle_con_port(&con));
3852
3884
if (safe_updates)
3868
3900
static const char *
3869
server_version_string(DRIZZLE *con)
3901
server_version_string(drizzle_con_st *local_con)
3871
static char buf[MAX_SERVER_VERSION_LENGTH] = "";
3903
static string buf("");
3904
static bool server_version_string_reserved= false;
3906
if (!server_version_string_reserved)
3908
buf.reserve(MAX_SERVER_VERSION_LENGTH);
3909
server_version_string_reserved= true;
3873
3911
/* Only one thread calls this, so no synchronization is needed */
3874
3912
if (buf[0] == '\0')
3877
DRIZZLE_RES *result;
3914
drizzle_result_st result;
3915
drizzle_return_t ret;
3879
bufp= my_stpncpy(buf, drizzle_get_server_info(con), sizeof buf);
3917
buf.append(drizzle_con_server_version(local_con));
3881
3919
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3882
if (!drizzle_query(con, "select @@version_comment limit 1") &&
3883
(result = drizzle_use_result(con)))
3920
(void)drizzle_query_str(local_con, &result,
3921
"select @@version_comment limit 1", &ret);
3922
if (ret == DRIZZLE_RETURN_OK &&
3923
drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
3885
DRIZZLE_ROW cur = drizzle_fetch_row(result);
3925
drizzle_row_t cur = drizzle_row_next(&result);
3886
3926
if (cur && cur[0])
3888
bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NULL);
3890
drizzle_free_result(result);
3931
drizzle_result_free(&result);
3893
/* str*nmov doesn't guarantee NUL-termination */
3894
if (bufp == buf + sizeof buf)
3895
buf[sizeof buf - 1] = '\0';
3933
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
3934
drizzle_result_free(&result);
3902
put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
3941
put_info(const char *str,INFO_TYPE info_type, uint32_t error, const char *sqlstate)
3904
3943
FILE *file= (info_type == INFO_ERROR ? stderr : stdout);
3905
3944
static int inited=0;