161
163
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
162
164
typedef enum enum_info_type INFO_TYPE;
164
static DRIZZLE drizzle; /* The connection */
166
static drizzle_st drizzle; /* The library handle */
167
static drizzle_con_st con; /* The connection */
165
168
static bool ignore_errors= false, quick= false,
166
169
connected= false, opt_raw_data= false, unbuffered= false,
167
170
output_tables= false, opt_rehash= true, skip_updates= false,
178
181
static bool debug_info_flag, debug_check_flag;
179
182
static bool column_types_flag;
180
183
static bool preserve_comments= false;
181
static uint32_t opt_max_allowed_packet, opt_net_buffer_length,
184
static uint32_t opt_max_input_line, opt_drizzle_port= 0;
183
185
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
184
186
static uint32_t my_end_arg;
185
187
static char * opt_drizzle_unix_port= NULL;
186
static int connect_flag= 0;
188
static drizzle_capabilities_t connect_flag= DRIZZLE_CAPABILITIES_NONE;
187
189
static char *current_host, *current_db, *current_user= NULL,
188
190
*opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
189
191
static char *histfile;
211
213
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
213
int drizzleclient_real_query_for_lazy(const char *buf, int length);
214
int drizzleclient_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);
217
221
void tee_fprintf(FILE *file, const char *fmt, ...);
238
242
static int read_and_execute(bool interactive);
239
243
static int sql_connect(char *host,char *database,char *user,char *password,
240
244
uint32_t silent);
241
static const char *server_version_string(DRIZZLE *drizzle);
245
static const char *server_version_string(drizzle_con_st *con);
242
246
static int put_info(const char *str,INFO_TYPE info,uint32_t error,
243
247
const char *sql_state);
244
static int put_error(DRIZZLE *drizzle);
248
static int put_error(drizzle_con_st *con, drizzle_result_st *res);
245
249
static void safe_put_field(const char *pos,uint32_t length);
246
250
static void init_pager(void);
247
251
static void end_pager(void);
251
255
static char *get_arg(char *line, bool get_next_arg);
252
256
static void init_username(void);
253
257
static void add_int_to_prompt(int toadd);
254
static int get_result_width(DRIZZLE_RES *res);
255
static int get_field_disp_length(DRIZZLE_FIELD * field);
258
static int get_result_width(drizzle_result_st *res);
259
static int get_field_disp_length(drizzle_column_st * field);
256
260
static const char * strcont(register const char *str, register const char *set);
258
262
/* A structure which contains information on the commands this program
1002
1006
static bool add_line(string *buffer,char *line,char *in_string,
1003
1007
bool *ml_comment);
1004
1008
static void remove_cntrl(string *buffer);
1005
static void print_table_data(DRIZZLE_RES *result);
1006
static void print_tab_data(DRIZZLE_RES *result);
1007
static void print_table_data_vertically(DRIZZLE_RES *result);
1008
static void print_warnings(void);
1009
static void print_table_data(drizzle_result_st *result);
1010
static void print_tab_data(drizzle_result_st *result);
1011
static void print_table_data_vertically(drizzle_result_st *result);
1012
static void print_warnings(uint32_t error_code);
1009
1013
static uint32_t start_timer(void);
1010
1014
static void end_timer(uint32_t start_time,char *buff);
1011
1015
static void drizzle_end_timer(uint32_t start_time,char *buff);
1136
1140
sprintf(output_buff,
1137
1141
_("Your Drizzle connection id is %u\nServer version: %s\n"),
1138
drizzleclient_thread_id(&drizzle),
1139
server_version_string(&drizzle));
1142
drizzle_con_thread_id(&con),
1143
server_version_string(&con));
1140
1144
put_info(output_buff, INFO_INFO, 0, 0);
1142
1146
initialize_readline(current_prompt);
1229
1234
void handle_sigint(int sig)
1231
1236
char kill_buffer[40];
1232
DRIZZLE *kill_drizzle= NULL;
1237
drizzle_con_st kill_drizzle;
1238
drizzle_result_st res;
1239
drizzle_return_t ret;
1234
1241
/* terminate if no query being executed, or we already tried interrupting */
1235
1242
if (!executing_query || interrupted_query) {
1239
kill_drizzle= drizzleclient_create(kill_drizzle);
1240
if (!drizzleclient_connect(kill_drizzle,current_host, current_user, opt_password,
1241
"", 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)
1246
1253
/* kill_buffer is always big enough because max length of %lu is 15 */
1247
sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u", drizzleclient_thread_id(&drizzle));
1248
drizzleclient_real_query(kill_drizzle, kill_buffer, strlen(kill_buffer));
1249
drizzleclient_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);
1250
1261
tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1252
1263
interrupted_query= 1;
1279
1290
(char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
1281
1292
{"no-auto-rehash", 'A',
1282
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."),
1283
1294
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1284
1295
{"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT,
1285
1296
N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
1409
1420
(char**) &opt_connect_timeout,
1410
1421
(char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 0, 0, 3600*12, 0,
1412
{"max_allowed_packet", OPT_MAX_ALLOWED_PACKET,
1413
N_("Max packet length to send to, or receive from server"),
1414
(char**) &opt_max_allowed_packet, (char**) &opt_max_allowed_packet, 0,
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,
1415
1426
GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1416
1427
(int64_t) 2*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
1417
{"net_buffer_length", OPT_NET_BUFFER_LENGTH,
1418
N_("Buffer for TCP/IP and socket communication"),
1419
(char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0, GET_UINT32,
1420
REQUIRED_ARG, 16384, 1024, 512*1024*1024L, MALLOC_OVERHEAD, 1024, 0},
1421
1428
{"select_limit", OPT_SELECT_LIMIT,
1422
1429
N_("Automatic limit for SELECT when using --safe-updates"),
1423
1430
(char**) &select_limit,
1445
1452
const char* readline= "readline";
1447
1454
printf(_("%s Ver %s Distrib %s, for %s (%s) using %s %s\n"),
1448
my_progname, VER.c_str(), drizzleclient_get_client_info(),
1455
my_progname, VER.c_str(), drizzle_version(),
1449
1456
SYSTEM_TYPE, MACHINE_TYPE,
1450
1457
readline, rl_library_version);
1651
1657
strcpy(pager, pagpoint);
1652
1658
strcpy(default_pager, pager);
1654
opt_max_allowed_packet= *drizzle_params->p_max_allowed_packet;
1655
opt_net_buffer_length= *drizzle_params->p_net_buffer_length;
1657
1660
if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
1658
1661
exit(ho_error);
1660
*drizzle_params->p_max_allowed_packet= opt_max_allowed_packet;
1661
*drizzle_params->p_net_buffer_length= opt_net_buffer_length;
1663
1663
if (status.batch) /* disable pager and outfile in this case */
1665
1665
strcpy(default_pager, "stdout");
2286
2286
static void build_completion_hash(bool rehash, bool write_info)
2288
2288
COMMANDS *cmd=commands;
2289
DRIZZLE_RES *databases=0,*tables=0;
2290
DRIZZLE_RES *fields;
2291
DRIZZLE_ROW database_row,table_row;
2292
DRIZZLE_FIELD *sql_field;
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
2293
string tmp_str, tmp_str_lower;
2296
2295
if (status.batch || quick || !current_db)
2297
2296
return; // We don't need completion in batches
2311
2310
/* hash Drizzle functions (to be implemented) */
2313
2312
/* hash all database names */
2314
if (drizzleclient_query(&drizzle,"show databases") == 0)
2313
if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
2316
if (!(databases = drizzleclient_store_result(&drizzle)))
2317
put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
2315
if (ret == DRIZZLE_RETURN_OK)
2320
while ((database_row=drizzleclient_fetch_row(databases)))
2317
if (drizzle_result_buffer(&databases) != DRIZZLE_RETURN_OK)
2318
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2322
tmp_str= database_row[0];
2323
tmp_str_lower= lower_string(tmp_str);
2324
completion_map[tmp_str_lower]= tmp_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;
2326
drizzleclient_free_result(databases);
2330
drizzle_result_free(&databases);
2329
2333
/* hash all table names */
2330
if (drizzleclient_query(&drizzle,"show tables")==0)
2334
if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
2332
if (!(tables = drizzleclient_store_result(&drizzle)))
2333
put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
2336
if (ret != DRIZZLE_RETURN_OK)
2338
drizzle_result_free(&tables);
2342
if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
2343
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2336
if (drizzleclient_num_rows(tables) > 0 && !opt_silent && write_info)
2346
if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
2338
2348
tee_fprintf(stdout, _("\
2339
2349
Reading table information for completion of table and column names\n \
2340
2350
You can turn off this feature to get a quicker startup with -A\n\n"));
2342
while ((table_row=drizzleclient_fetch_row(tables)))
2352
while ((table_row=drizzle_row_next(&tables)))
2344
2354
tmp_str= table_row[0];
2345
2355
tmp_str_lower= lower_string(tmp_str);
2351
2363
/* hash all field names, both with the table prefix and without it */
2352
if (!tables) /* no tables */
2364
if (drizzle_result_row_count(&tables) == 0)
2366
drizzle_result_free(&tables);
2356
drizzleclient_data_seek(tables,0);
2358
while ((table_row=drizzleclient_fetch_row(tables)))
2370
drizzle_row_seek(&tables, 0);
2372
while ((table_row=drizzle_row_next(&tables)))
2362
query.append("show fields in `");
2376
query.append("show fields in '");
2363
2377
query.append(table_row[0]);
2366
if (drizzleclient_query(&drizzle, query.c_str()) == 0)
2380
if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2368
fields= drizzleclient_store_result(&drizzle);
2383
if (ret == DRIZZLE_RETURN_OK &&
2384
drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2371
while ((sql_field=drizzleclient_fetch_field(fields)))
2386
while ((sql_field=drizzle_column_next(&fields)))
2373
2388
tmp_str=table_row[0];
2374
2389
tmp_str.append(".");
2375
tmp_str.append(sql_field->name);
2376
tmp_str_lower= lower_string(tmp_str);
2377
completion_map[tmp_str_lower]= tmp_str;
2379
tmp_str=sql_field->name;
2380
tmp_str_lower= lower_string(tmp_str);
2381
completion_map[tmp_str_lower]= tmp_str;
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;
2384
drizzleclient_free_result(fields);
2399
drizzle_result_free(&fields);
2388
drizzleclient_free_result(tables);
2402
drizzle_result_free(&tables);
2389
2403
completion_iter= completion_map.begin();
2435
2449
static void get_current_db(void)
2451
drizzle_return_t ret;
2452
drizzle_result_st res;
2439
2454
free(current_db);
2440
2455
current_db= NULL;
2441
2456
/* In case of error below current_db will be NULL */
2442
if (!drizzleclient_query(&drizzle, "SELECT DATABASE()") &&
2443
(res= drizzleclient_use_result(&drizzle)))
2457
if (drizzle_query_str(&con, &res, "SELECT DATABASE()", &ret) != NULL)
2445
DRIZZLE_ROW row= drizzleclient_fetch_row(res);
2447
current_db= strdup(row[0]);
2448
drizzleclient_free_result(res);
2459
if (ret == DRIZZLE_RETURN_OK &&
2460
drizzle_result_buffer(&res) == DRIZZLE_RETURN_OK)
2462
drizzle_row_t row= drizzle_row_next(&res);
2464
current_db= strdup(row[0]);
2466
drizzle_result_free(&res);
2453
2471
The different commands
2454
2472
***************************************************************************/
2456
int drizzleclient_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)
2478
drizzle_return_t ret;
2458
2480
for (uint32_t retry=0;; retry++)
2461
if (!drizzleclient_real_query(&drizzle,buf,length))
2483
if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2484
ret == DRIZZLE_RETURN_OK)
2463
error= put_error(&drizzle);
2464
if (drizzleclient_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 ||
2465
2497
!opt_reconnect)
2467
2502
if (reconnect())
2472
int drizzleclient_store_result_for_lazy(DRIZZLE_RES **result)
2507
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
2474
if ((*result=drizzleclient_store_result(&drizzle)))
2509
if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
2477
if (drizzleclient_error(&drizzle)[0])
2478
return put_error(&drizzle);
2512
if (drizzle_con_error(&con)[0])
2513
return put_error(&con, result);
2612
2650
if (vertical || (auto_vertical_output &&
2613
(terminal_width < get_result_width(result))))
2614
print_table_data_vertically(result);
2651
(terminal_width < get_result_width(&result))))
2652
print_table_data_vertically(&result);
2615
2653
else if (opt_silent && verbose <= 2 && !output_tables)
2616
print_tab_data(result);
2654
print_tab_data(&result);
2618
print_table_data(result);
2656
print_table_data(&result);
2620
2658
ngettext("%ld row in set","%ld rows in set",
2621
(long) drizzleclient_num_rows(result)),
2622
(long) drizzleclient_num_rows(result));
2659
(long) drizzle_result_row_count(&result)),
2660
(long) drizzle_result_row_count(&result));
2624
if (drizzleclient_errno(&drizzle))
2625
error= put_error(&drizzle);
2662
if (drizzle_result_error_code(&result))
2663
error= put_error(&con, &result);
2628
else if (drizzleclient_affected_rows(&drizzle) == ~(uint64_t) 0)
2666
else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2629
2667
strcpy(buff,_("Query OK"));
2631
2669
sprintf(buff, ngettext("Query OK, %ld row affected",
2632
2670
"Query OK, %ld rows affected",
2633
(long) drizzleclient_affected_rows(&drizzle)),
2634
(long) drizzleclient_affected_rows(&drizzle));
2671
(long) drizzle_result_affected_rows(&result)),
2672
(long) drizzle_result_affected_rows(&result));
2636
2674
pos= strchr(buff, '\0');
2637
if ((warnings= drizzleclient_warning_count(&drizzle)))
2675
if ((warnings= drizzle_result_warning_count(&result)))
2646
2684
strcpy(pos, time_buff);
2647
2685
put_info(buff,INFO_RESULT,0,0);
2648
if (drizzleclient_info(&drizzle))
2649
put_info(drizzleclient_info(&drizzle),INFO_RESULT,0,0);
2686
if (strcmp(drizzle_result_info(&result), ""))
2687
put_info(drizzle_result_info(&result),INFO_RESULT,0,0);
2650
2688
put_info("",INFO_RESULT,0,0); // Empty row
2652
if (result && !drizzleclient_eof(result)) /* Something wrong when using quick */
2653
error= put_error(&drizzle);
2654
else if (unbuffered)
2655
2691
fflush(stdout);
2656
drizzleclient_free_result(result);
2657
} while (!(err= drizzleclient_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);
2659
error= put_error(&drizzle);
2712
error= put_error(&con, NULL);
2663
2716
/* Show warnings if any or error occured */
2664
2717
if (show_warnings == 1 && (warnings >= 1 || error))
2718
print_warnings(error_code);
2667
2720
if (!error && !status.batch &&
2668
(drizzle.server_status & SERVER_STATUS_DB_DROPPED))
2721
drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2669
2723
get_current_db();
2671
2726
executing_query= 0;
2672
2727
return error; /* New command follows */
2737
static const char *fieldtype2str(enum enum_field_types type)
2792
static const char *fieldtype2str(drizzle_column_type_t type)
2739
2794
switch (type) {
2740
case DRIZZLE_TYPE_BLOB: return "BLOB";
2741
case DRIZZLE_TYPE_DATE: return "DATE";
2742
case DRIZZLE_TYPE_DATETIME: return "DATETIME";
2743
case DRIZZLE_TYPE_NEWDECIMAL: return "DECIMAL";
2744
case DRIZZLE_TYPE_DOUBLE: return "DOUBLE";
2745
case DRIZZLE_TYPE_ENUM: return "ENUM";
2746
case DRIZZLE_TYPE_LONG: return "LONG";
2747
case DRIZZLE_TYPE_LONGLONG: return "LONGLONG";
2748
case DRIZZLE_TYPE_NULL: return "NULL";
2749
case DRIZZLE_TYPE_TIMESTAMP: return "TIMESTAMP";
2750
case DRIZZLE_TYPE_TINY: return "TINY";
2751
case DRIZZLE_TYPE_VIRTUAL: return "VIRTUAL";
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";
2752
2807
default: return "?-unknown-?";
2804
2859
"Decimals: %u\n"
2805
2860
"Flags: %s\n\n",
2807
field->name, field->catalog, field->db, field->table,
2808
field->org_table, fieldtype2str(field->type),
2809
get_charset_name(field->charsetnr), field->charsetnr,
2810
field->length, field->max_length, field->decimals,
2811
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)));
2813
2871
tee_puts("", PAGER);
2818
print_table_data(DRIZZLE_RES *result)
2876
print_table_data(drizzle_result_st *result)
2821
DRIZZLE_FIELD *field;
2879
drizzle_return_t ret;
2880
drizzle_column_st *field;
2823
2882
string separator;
2825
2884
separator.reserve(256);
2827
num_flag=(bool*) malloc(sizeof(bool)*drizzleclient_num_fields(result));
2886
num_flag=(bool*) malloc(sizeof(bool)*drizzle_result_column_count(result));
2828
2887
if (column_types_flag)
2830
2889
print_field_types(result);
2831
if (!drizzleclient_num_rows(result))
2890
if (!drizzle_result_row_count(result))
2833
drizzleclient_field_seek(result,0);
2892
drizzle_column_seek(result,0);
2835
2894
separator.append("+");
2836
while ((field = drizzleclient_fetch_field(result)))
2895
while ((field = drizzle_column_next(result)))
2838
2897
uint32_t x, length= 0;
2840
2899
if (column_names)
2901
uint32_t name_length= strlen(drizzle_column_name(field));
2842
2903
/* Check if the max_byte value is really the maximum in terms
2843
2904
of visual length since multibyte characters can affect the
2844
2905
length of the separator. */
2845
2906
length= charset_info->cset->numcells(charset_info,
2847
field->name+field->name_length);
2907
drizzle_column_name(field),
2908
drizzle_column_name(field) +
2849
if (field->name_length == field->max_length)
2911
if (name_length == drizzle_column_max_size(field))
2851
if (length < field->max_length)
2852
field->max_length= length;
2913
if (length < drizzle_column_max_size(field))
2914
drizzle_column_set_max_size(field, length);
2856
length= field->name_length;
2918
length= name_length;
2861
length=max(length,field->length);
2923
length=max(length,drizzle_column_size(field));
2863
length=max(length,field->max_length);
2864
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))
2865
2929
// Room for "NULL"
2867
field->max_length=length;
2932
drizzle_column_set_max_size(field, length);
2869
2934
for (x=0; x< (length+2); x++)
2870
2935
separator.append("-");
2874
2939
tee_puts((char*) separator.c_str(), PAGER);
2875
2940
if (column_names)
2877
drizzleclient_field_seek(result,0);
2942
drizzle_column_seek(result,0);
2878
2943
(void) tee_fputs("|", PAGER);
2879
for (uint32_t off=0; (field = drizzleclient_fetch_field(result)) ; off++)
2944
for (uint32_t off=0; (field = drizzle_column_next(result)) ; off++)
2881
uint32_t name_length= (uint32_t) strlen(field->name);
2946
uint32_t name_length= (uint32_t) strlen(drizzle_column_name(field));
2882
2947
uint32_t numcells= charset_info->cset->numcells(charset_info,
2884
field->name + name_length);
2885
uint32_t display_length= field->max_length + name_length - numcells;
2948
drizzle_column_name(field),
2949
drizzle_column_name(field) +
2951
uint32_t display_length= drizzle_column_max_size(field) + name_length -
2886
2953
tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
2887
2954
MAX_COLUMN_LENGTH),
2889
num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) ||
2890
(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));
2892
2959
(void) tee_fputs("\n", PAGER);
2893
2960
tee_puts((char*) separator.c_str(), PAGER);
2896
while ((cur= drizzleclient_fetch_row(result)))
2898
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)
2900
uint32_t *lengths= drizzleclient_fetch_lengths(result);
2980
size_t *lengths= drizzle_row_field_sizes(result);
2901
2981
(void) tee_fputs("| ", PAGER);
2902
drizzleclient_field_seek(result, 0);
2903
for (uint32_t off= 0; off < drizzleclient_num_fields(result); off++)
2982
drizzle_column_seek(result, 0);
2983
for (uint32_t off= 0; off < drizzle_result_column_count(result); off++)
2905
2985
const char *buffer;
2906
2986
uint32_t data_length;
2968
3050
@returns number of character positions to be used, at most
2970
static int get_field_disp_length(DRIZZLE_FIELD *field)
3052
static int get_field_disp_length(drizzle_column_st *field)
2972
uint32_t length= column_names ? field->name_length : 0;
3054
uint32_t length= column_names ? strlen(drizzle_column_name(field)) : 0;
2975
length= max(length, field->length);
3057
length= max(length, drizzle_column_size(field));
2977
length= max(length, field->max_length);
3059
length= max(length, (uint32_t)drizzle_column_max_size(field));
2979
if (length < 4 && !(field->flags & NOT_NULL_FLAG))
3062
!(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
2980
3064
length= 4; /* Room for "NULL" */
2991
3076
@returns The max number of characters in any row of this result
2993
static int get_result_width(DRIZZLE_RES *result)
3078
static int get_result_width(drizzle_result_st *result)
2995
3080
unsigned int len= 0;
2996
DRIZZLE_FIELD *field;
2997
DRIZZLE_FIELD_OFFSET offset;
3081
drizzle_column_st *field;
2999
offset= drizzleclient_field_tell(result);
3084
offset= drizzle_column_current(result);
3000
3085
assert(offset == 0);
3002
while ((field= drizzleclient_fetch_field(result)) != NULL)
3087
while ((field= drizzle_column_next(result)) != NULL)
3003
3088
len+= get_field_disp_length(field) + 3; /* plus bar, space, & final space */
3005
(void) drizzleclient_field_seek(result, offset);
3090
(void) drizzle_column_seek(result, offset);
3007
3092
return len + 1; /* plus final bar. */
3041
print_table_data_vertically(DRIZZLE_RES *result)
3126
print_table_data_vertically(drizzle_result_st *result)
3044
uint32_t max_length=0;
3045
DRIZZLE_FIELD *field;
3129
drizzle_return_t ret;
3130
uint32_t max_length=0;
3131
drizzle_column_st *field;
3047
while ((field = drizzleclient_fetch_field(result)))
3133
while ((field = drizzle_column_next(result)))
3049
uint32_t length= field->name_length;
3135
uint32_t length= strlen(drizzle_column_name(field));
3050
3136
if (length > max_length)
3051
3137
max_length= length;
3052
field->max_length=length;
3138
drizzle_column_set_max_size(field, length);
3055
drizzleclient_field_seek(result,0);
3056
for (uint32_t row_count=1; (cur= drizzleclient_fetch_row(result)); row_count++)
3141
for (uint32_t row_count=1;; row_count++)
3058
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)
3060
drizzleclient_field_seek(result,0);
3157
drizzle_column_seek(result,0);
3061
3158
tee_fprintf(PAGER,
3062
3159
"*************************** %d. row ***************************\n", row_count);
3063
for (uint32_t off=0; off < drizzleclient_num_fields(result); off++)
3160
for (uint32_t off=0; off < drizzle_result_column_count(result); off++)
3065
field= drizzleclient_fetch_field(result);
3066
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));
3067
3164
tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3167
drizzle_row_free(result, cur);
3073
3172
/* print_warnings should be called right after executing a statement */
3075
static void print_warnings()
3174
static void print_warnings(uint32_t error_code)
3078
DRIZZLE_RES *result;
3177
drizzle_result_st result;
3080
3179
uint64_t num_rows;
3082
/* Save current error before calling "show warnings" */
3083
uint32_t error= drizzleclient_errno(&drizzle);
3180
uint32_t new_code= 0;
3085
3182
/* Get the warnings */
3086
3183
query= "show warnings";
3087
drizzleclient_real_query_for_lazy(query, strlen(query));
3184
drizzleclient_real_query_for_lazy(query, strlen(query),&result,&new_code);
3088
3185
drizzleclient_store_result_for_lazy(&result);
3090
3187
/* Bail out when no warnings */
3091
if (!(num_rows= drizzleclient_num_rows(result)))
3188
if (!(num_rows= drizzle_result_row_count(&result)))
3094
cur= drizzleclient_fetch_row(result);
3191
cur= drizzle_row_next(&result);
3097
3194
Don't print a duplicate of the current error. It is possible for SHOW
3099
3196
messages. To be safe, skip printing the duplicate only if it is the only
3102
if (!cur || (num_rows == 1 && error == (uint32_t) strtoul(cur[1], NULL, 10)))
3199
if (!cur || (num_rows == 1 &&
3200
error_code == (uint32_t) strtoul(cur[1], NULL, 10)))
3105
3205
/* Print the warnings */
3109
3209
tee_fprintf(PAGER, "%s (Code %s): %s\n", cur[0], cur[1], cur[2]);
3110
} while ((cur= drizzleclient_fetch_row(result)));
3210
} while ((cur= drizzle_row_next(&result)));
3114
drizzleclient_free_result(result);
3214
drizzle_result_free(&result);
3156
print_tab_data(DRIZZLE_RES *result)
3256
print_tab_data(drizzle_result_st *result)
3159
DRIZZLE_FIELD *field;
3259
drizzle_return_t ret;
3260
drizzle_column_st *field;
3162
3263
if (opt_silent < 2 && column_names)
3165
while ((field = drizzleclient_fetch_field(result)))
3266
while ((field = drizzle_column_next(result)))
3168
3269
(void) tee_fputs("\t", PAGER);
3169
(void) tee_fputs(field->name, PAGER);
3270
(void) tee_fputs(drizzle_column_name(field), PAGER);
3171
3272
(void) tee_fputs("\n", PAGER);
3173
while ((cur = drizzleclient_fetch_row(result)))
3175
lengths= drizzleclient_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);
3176
3292
safe_put_field(cur[0],lengths[0]);
3177
for (uint32_t off=1 ; off < drizzleclient_num_fields(result); off++)
3293
for (uint32_t off=1 ; off < drizzle_result_column_count(result); off++)
3179
3295
(void) tee_fputs("\t", PAGER);
3180
3296
safe_put_field(cur[off], lengths[off]);
3182
3298
(void) tee_fputs("\n", PAGER);
3300
drizzle_row_free(result, cur);
3528
3648
if (!connected && reconnect())
3529
3649
return opt_reconnect ? -1 : 1; // Fatal error
3530
if (drizzleclient_select_db(&drizzle,tmp))
3650
for (bool try_again= true; try_again; try_again= false)
3532
if (drizzleclient_errno(&drizzle) != CR_SERVER_GONE_ERROR)
3533
return put_error(&drizzle);
3536
return opt_reconnect ? -1 : 1; // Fatal error
3537
if (drizzleclient_select_db(&drizzle,tmp))
3538
return put_error(&drizzle);
3652
if (drizzle_select_db(&con,&result,tmp,&ret) == NULL ||
3653
ret != DRIZZLE_RETURN_OK)
3655
if (ret == DRIZZLE_RETURN_ERROR_CODE)
3657
int error= put_error(&con, &result);
3658
drizzle_result_free(&result);
3662
if (ret != DRIZZLE_RETURN_SERVER_GONE || !try_again)
3663
return put_error(&con, NULL);
3666
return opt_reconnect ? -1 : 1; // Fatal error
3669
drizzle_result_free(&result);
3540
3671
free(current_db);
3541
3672
current_db= strdup(tmp);
3629
3760
sql_connect(char *host,char *database,char *user,char *password,
3630
3761
uint32_t silent)
3763
drizzle_return_t ret;
3635
drizzleclient_close(&drizzle);
3637
drizzleclient_create(&drizzle);
3768
drizzle_con_free(&con);
3769
drizzle_free(&drizzle);
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
3638
3781
if (opt_connect_timeout)
3640
3783
uint32_t timeout=opt_connect_timeout;
3641
3784
drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3642
3785
(char*) &timeout);
3645
drizzleclient_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
3646
if (opt_secure_auth)
3647
drizzleclient_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
3789
/* XXX Do we need this?
3648
3790
if (safe_updates)
3650
3792
char init_command[100];
3654
3796
select_limit, max_join_size);
3655
3797
drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3657
if (!drizzleclient_connect(&drizzle, host, user, password,
3658
database, opt_drizzle_port, opt_drizzle_unix_port,
3659
connect_flag | CLIENT_MULTI_STATEMENTS))
3800
if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
3662
(drizzleclient_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3663
drizzleclient_errno(&drizzle) != CR_CONNECTION_ERROR))
3802
if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
3803
ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
3665
(void) put_error(&drizzle);
3805
(void) put_error(&con, NULL);
3666
3806
(void) fflush(stdout);
3667
3807
return ignore_errors ? -1 : 1; // Abort
3669
3809
return -1; // Retryable
3672
3813
drizzle.reconnect= debug_info_flag; // We want to know if this happens
3673
3815
build_completion_hash(opt_rehash, 1);
3679
3821
com_status(string *, const char *)
3683
DRIZZLE_RES *result;
3827
drizzle_result_st result;
3828
drizzle_return_t ret;
3685
3830
tee_puts("--------------", stdout);
3686
3831
usage(1); /* Print version */
3689
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzleclient_thread_id(&drizzle));
3834
tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_con_thread_id(&con));
3691
3836
Don't remove "limit 1",
3692
3837
it is protection againts SQL_SELECT_LIMIT=0
3694
if (!drizzleclient_query(&drizzle,"select DATABASE(), USER() limit 1") &&
3695
(result=drizzleclient_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)
3697
DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
3843
drizzle_row_t cur=drizzle_row_next(&result);
3700
3846
tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3701
3847
tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3703
drizzleclient_free_result(result);
3849
drizzle_result_free(&result);
3851
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
3852
drizzle_result_free(&result);
3705
3853
tee_puts("SSL:\t\t\tNot in use", stdout);
3720
3868
tee_fprintf(stdout, "Current pager:\t\t%s\n", pager);
3721
3869
tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
3722
3870
tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
3723
tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&drizzle));
3724
tee_fprintf(stdout, "Protocol version:\t%d\n", drizzleclient_get_proto_info(&drizzle));
3725
tee_fprintf(stdout, "Connection:\t\t%s\n", drizzleclient_get_host_info(&drizzle));
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
3726
3875
if ((id= drizzleclient_insert_id(&drizzle)))
3727
3876
tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
3729
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3730
if (!drizzleclient_query(&drizzle,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
3731
(result=drizzleclient_use_result(&drizzle)))
3733
DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
3736
tee_fprintf(stdout, "Server characterset:\t%s\n", cur[2] ? cur[2] : "");
3737
tee_fprintf(stdout, "Db characterset:\t%s\n", cur[3] ? cur[3] : "");
3738
tee_fprintf(stdout, "Client characterset:\t%s\n", cur[0] ? cur[0] : "");
3739
tee_fprintf(stdout, "Conn. characterset:\t%s\n", cur[1] ? cur[1] : "");
3741
drizzleclient_free_result(result);
3744
if (strstr(drizzleclient_get_host_info(&drizzle),"TCP/IP") || ! drizzle.unix_socket)
3745
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));
3747
tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle.unix_socket);
3748
if (drizzle.net.compress)
3749
tee_fprintf(stdout, "Protocol:\t\tCompressed\n");
3882
tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle_con_port(&con));
3751
3884
if (safe_updates)
3778
3911
/* Only one thread calls this, so no synchronization is needed */
3779
3912
if (buf[0] == '\0')
3781
DRIZZLE_RES *result;
3914
drizzle_result_st result;
3915
drizzle_return_t ret;
3783
buf.append(drizzleclient_get_server_info(con));
3917
buf.append(drizzle_con_server_version(local_con));
3785
3919
/* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3786
if (!drizzleclient_query(con, "select @@version_comment limit 1") &&
3787
(result = drizzleclient_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)
3789
DRIZZLE_ROW cur = drizzleclient_fetch_row(result);
3925
drizzle_row_t cur = drizzle_row_next(&result);
3790
3926
if (cur && cur[0])
3792
3928
buf.append(" ");
3793
3929
buf.append(cur[0]);
3795
drizzleclient_free_result(result);
3931
drizzle_result_free(&result);
3933
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
3934
drizzle_result_free(&result);
3799
3937
return buf.c_str();
3875
put_error(DRIZZLE *con)
4013
put_error(drizzle_con_st *local_con, drizzle_result_st *res)
3877
return put_info(drizzleclient_error(con), INFO_ERROR, drizzleclient_errno(con),
3878
drizzleclient_sqlstate(con));
4019
error= drizzle_result_error(res);
4020
if (!strcmp(error, ""))
4021
error= drizzle_con_error(local_con);
4024
error= drizzle_con_error(local_con);
4026
return put_info(error, INFO_ERROR,
4027
res == NULL ? drizzle_con_error_code(local_con) :
4028
drizzle_result_error_code(res),
4029
res == NULL ? drizzle_con_sqlstate(local_con) :
4030
drizzle_result_sqlstate(res));
4072
const char *host_info = drizzleclient_get_host_info(&drizzle);
4073
if (strstr(host_info, "memory"))
4224
if (strcmp(drizzle_con_uds(&con), ""))
4075
processed_prompt->append(drizzle.host);
4226
const char *pos=strrchr(drizzle_con_uds(&con),'/');
4227
processed_prompt->append(pos ? pos+1 : drizzle_con_uds(&con));
4077
else if (strstr(host_info,"TCP/IP") ||
4078
!drizzle.unix_socket)
4079
add_int_to_prompt(drizzle.port);
4082
char *pos=strrchr(drizzle.unix_socket,'/');
4083
processed_prompt->append(pos ? pos+1 : drizzle.unix_socket);
4230
add_int_to_prompt(drizzle_con_port(&con));
4194
4340
static void init_username()
4196
4343
free(full_username);
4197
4344
free(part_username);
4199
DRIZZLE_RES *result;
4346
drizzle_result_st *result;
4200
4347
if (!drizzleclient_query(&drizzle,"select USER()") &&
4201
4348
(result=drizzleclient_use_result(&drizzle)))
4203
DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
4350
drizzle_row_t cur=drizzleclient_fetch_row(result);
4204
4351
full_username= strdup(cur[0]);
4205
4352
part_username= strdup(strtok(cur[0],"@"));
4206
4353
(void) drizzleclient_fetch_row(result); // Read eof
4210
4358
static int com_prompt(string *, const char *line)