282
282
typedef enum enum_info_type INFO_TYPE;
284
284
static drizzle_st *drizzle= NULL; /* The library handle */
285
static drizzle_con_st con; /* The connection */
285
static drizzle_con_st *con= NULL; /* The connection */
286
286
static bool ignore_errors= false, quick= false,
287
287
connected= false, opt_raw_data= false, unbuffered= false,
288
288
output_tables= false, opt_rehash= true, skip_updates= false,
1188
1188
static bool server_shutdown(void)
1190
1190
drizzle_result_st result;
1191
drizzle_return_t ret;
1195
1194
printf(_("shutting down drizzled"));
1196
1195
if (opt_drizzle_port > 0)
1197
1197
printf(_(" on port %d"), opt_drizzle_port);
1198
1199
printf("... ");
1201
if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
1202
&ret) == NULL || ret != DRIZZLE_RETURN_OK)
1202
drizzle_return_t ret;
1203
if (drizzle_shutdown(con, &result, DRIZZLE_SHUTDOWN_DEFAULT, &ret) == NULL or
1204
ret != DRIZZLE_RETURN_OK)
1204
1206
if (ret == DRIZZLE_RETURN_ERROR_CODE)
1206
fprintf(stderr, _("shutdown failed; error: '%s'"),
1207
drizzle_result_error(&result));
1208
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_result_error(&result));
1208
1209
drizzle_result_free(&result);
1212
fprintf(stderr, _("shutdown failed; error: '%s'"),
1213
drizzle_con_error(&con));
1213
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_con_error(con));
1234
1236
printf("... ");
1237
if (drizzle_kill(&con, &result, query_id,
1239
if (drizzle_kill(con, &result, query_id,
1238
1240
&ret) == NULL || ret != DRIZZLE_RETURN_OK)
1240
1242
if (ret == DRIZZLE_RETURN_ERROR_CODE)
1248
fprintf(stderr, _("kill failed; error: '%s'"),
1249
drizzle_con_error(&con));
1250
fprintf(stderr, _("kill failed; error: '%s'"), drizzle_con_error(con));
1272
1273
drizzle_result_st result;
1273
1274
drizzle_return_t ret;
1275
if (drizzle_ping(&con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
1276
if (drizzle_ping(con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
1277
1278
if (opt_silent < 2)
1278
1279
printf(_("drizzled is alive\n"));
1290
fprintf(stderr, _("drizzled won't answer to ping, error: '%s'"),
1291
drizzle_con_error(&con));
1291
fprintf(stderr, _("drizzled won't answer to ping, error: '%s'"), drizzle_con_error(con));
1840
1840
glob_buffer->reserve(512);
1842
1842
snprintf(&output_buff[0], output_buff.size(),
1843
_("Your Drizzle connection id is %u\nConnection protocol: %s\nServer version: %s\n"),
1844
drizzle_con_thread_id(&con),
1845
opt_protocol.c_str(),
1846
server_version_string(&con));
1843
_("Your Drizzle connection id is %u\nConnection protocol: %s\nServer version: %s\n"),
1844
drizzle_con_thread_id(con),
1845
opt_protocol.c_str(),
1846
server_version_string(con));
1847
1847
put_info(&output_buff[0], INFO_INFO, 0, 0);
1938
1938
void handle_sigint(int sig)
1940
char kill_buffer[40];
1941
boost::scoped_ptr<drizzle_con_st> kill_drizzle(new drizzle_con_st);
1942
drizzle_result_st res;
1943
drizzle_return_t ret;
1945
1940
/* terminate if no query being executed, or we already tried interrupting */
1946
if (!executing_query || interrupted_query)
1951
if (drizzle and drizzle_con_add_tcp(drizzle, kill_drizzle.get(), current_host.c_str(),
1952
opt_drizzle_port, current_user.c_str(), opt_password.c_str(), NULL,
1953
use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL) == NULL)
1958
/* kill_buffer is always big enough because max length of %lu is 15 */
1959
sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1960
drizzle_con_thread_id(&con));
1962
if (drizzle_query_str(kill_drizzle.get(), &res, kill_buffer, &ret) != NULL)
1963
drizzle_result_free(&res);
1965
drizzle_con_free(kill_drizzle.get());
1966
tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1968
interrupted_query= 1;
1941
if (executing_query == false or interrupted_query)
1945
drizzle_con_st *kill_drizzle_con;
1946
if ((kill_drizzle_con= drizzle_con_add_tcp(drizzle,
1947
current_host.c_str(), opt_drizzle_port,
1948
current_user.c_str(), opt_password.c_str(), NULL,
1949
use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL)))
1951
/* kill_buffer is always big enough because max length of %lu is 15 */
1952
char kill_buffer[40];
1953
snprintf(kill_buffer, sizeof(kill_buffer), "KILL /*!50000 QUERY */ %u", drizzle_con_thread_id(kill_drizzle_con));
1955
drizzle_return_t ret;
1956
drizzle_result_st res;
1957
if ((drizzle_query_str(kill_drizzle_con, &res, kill_buffer, &ret)))
1959
drizzle_result_free(&res);
1962
drizzle_con_free(kill_drizzle_con);
1964
tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1966
interrupted_query= true;
1973
1972
drizzle_end(sig);
2622
2621
/* hash Drizzle functions (to be implemented) */
2624
2623
/* hash all database names */
2625
if (drizzle_query_str(&con, &databases, "select schema_name from information_schema.schemata", &ret) != NULL)
2624
if (drizzle_query_str(con, &databases, "select schema_name from information_schema.schemata", &ret) != NULL)
2627
2626
if (ret == DRIZZLE_RETURN_OK)
2648
2647
query.append(current_db);
2649
2648
query.append("' order by table_name");
2651
if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2650
if (drizzle_query(con, &fields, query.c_str(), query.length(), &ret) != NULL)
2654
2652
if (ret == DRIZZLE_RETURN_OK &&
2655
2653
drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2714
2712
current_db.erase();
2715
2713
current_db= "";
2716
2714
/* In case of error below current_db will be NULL */
2717
if (drizzle_query_str(&con, &res, "SELECT DATABASE()", &ret) != NULL)
2715
if (drizzle_query_str(con, &res, "SELECT DATABASE()", &ret) != NULL)
2719
2717
if (ret == DRIZZLE_RETURN_OK &&
2720
2718
drizzle_result_buffer(&res) == DRIZZLE_RETURN_OK)
2740
2738
for (uint32_t retry=0;; retry++)
2743
if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2741
if (drizzle_query(con, result, buf, length, &ret) != NULL and
2744
2742
ret == DRIZZLE_RETURN_OK)
2748
error= put_error(&con, result);
2746
error= put_error(con, result);
2750
2748
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2767
2765
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
2769
2767
if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
2772
if (drizzle_con_error(&con)[0])
2772
if (drizzle_con_error(con)[0])
2774
int ret= put_error(&con, result);
2774
int ret= put_error(con, result);
2775
2775
drizzle_result_free(result);
2930
2931
(long) drizzle_result_row_count(&result));
2932
2933
if (drizzle_result_error_code(&result))
2933
error= put_error(&con, &result);
2934
error= put_error(con, &result);
2936
2937
else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2965
2966
fflush(stdout);
2966
2967
drizzle_result_free(&result);
2968
if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2969
if (drizzle_con_status(con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2970
if (drizzle_result_read(&con, &result, &ret) == NULL ||
2971
if (drizzle_result_read(con, &result, &ret) == NULL ||
2971
2972
ret != DRIZZLE_RETURN_OK)
2973
2974
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2976
2977
drizzle_result_free(&result);
2979
error= put_error(&con, NULL);
2980
error= put_error(con, NULL);
2984
} while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2985
} while (drizzle_con_status(con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2986
error= put_error(&con, NULL);
2988
error= put_error(con, NULL);
2991
2994
if (show_warnings == 1 && (warnings >= 1 || error))
2992
2995
print_warnings(error_code);
2994
if (!error && !status.getBatch() &&
2995
drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2997
if (!error && !status.getBatch() and
2998
drizzle_con_status(con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2997
3000
get_current_db();
3003
executing_query= false;
3001
3004
return error; /* New command follows */
3005
3008
static void init_pager()
3010
if (opt_nopager == false)
3009
3012
if (!(PAGER= popen(pager.c_str(), "w")))
3028
3033
FILE* new_outfile;
3029
3034
if (opt_outfile)
3031
if (!(new_outfile= fopen(file_name, "a")))
3039
if ((new_outfile= fopen(file_name, "a")) == NULL)
3033
3041
tee_fprintf(stdout, _("Error logging to file '%s'\n"), file_name);
3261
3269
cur= drizzle_row_buffer(result, &ret);
3262
3270
if (ret != DRIZZLE_RETURN_OK)
3264
(void)put_error(&con, result);
3272
(void)put_error(con, result);
3467
3475
cur= drizzle_row_buffer(result, &ret);
3468
3476
if (ret != DRIZZLE_RETURN_OK)
3470
(void)put_error(&con, result);
3478
(void)put_error(con, result);
3633
3641
cur= drizzle_row_buffer(result, &ret);
3634
3642
if (ret != DRIZZLE_RETURN_OK)
3636
(void)put_error(&con, result);
3644
(void)put_error(con, result);
3880
sprintf(buff, _("Connection id: %u"), drizzle_con_thread_id(&con));
3888
sprintf(buff, _("Connection id: %u"), drizzle_con_thread_id(con));
3881
3889
put_info(buff,INFO_INFO,0,0);
3882
3890
sprintf(buff, _("Current schema: %.128s\n"),
3883
3891
!current_db.empty() ? current_db.c_str() : _("*** NONE ***"));
4028
4036
return opt_reconnect ? -1 : 1; // Fatal error
4029
4037
for (bool try_again= true; try_again; try_again= false)
4031
if (drizzle_select_db(&con,&result,tmp,&ret) == NULL ||
4039
if (drizzle_select_db(con, &result, tmp, &ret) == NULL ||
4032
4040
ret != DRIZZLE_RETURN_OK)
4034
4042
if (ret == DRIZZLE_RETURN_ERROR_CODE)
4036
int error= put_error(&con, &result);
4044
int error= put_error(con, &result);
4037
4045
drizzle_result_free(&result);
4041
4049
if (ret != DRIZZLE_RETURN_LOST_CONNECTION || !try_again)
4042
return put_error(&con, NULL);
4051
return put_error(con, NULL);
4044
4054
if (reconnect())
4045
4056
return opt_reconnect ? -1 : 1; // Fatal error
4048
4060
drizzle_result_free(&result);
4069
4081
printf("... ");
4072
if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
4084
if (drizzle_shutdown(con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
4073
4085
&ret) == NULL || ret != DRIZZLE_RETURN_OK)
4075
4087
if (ret == DRIZZLE_RETURN_ERROR_CODE)
4077
fprintf(stderr, _("shutdown failed; error: '%s'"),
4078
drizzle_result_error(&result));
4089
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_result_error(&result));
4079
4090
drizzle_result_free(&result);
4083
fprintf(stderr, _("shutdown failed; error: '%s'"),
4084
drizzle_con_error(&con));
4094
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_con_error(con));
4192
if (drizzle_con_add_tcp(drizzle, &con, (char *)host.c_str(),
4193
opt_drizzle_port, (char *)user.c_str(),
4194
(char *)password.c_str(), (char *)database.c_str(),
4195
global_con_options) == NULL)
4202
if ((con= drizzle_con_add_tcp(drizzle, host.c_str(),
4203
opt_drizzle_port, user.c_str(),
4204
password.c_str(), database.c_str(),
4205
global_con_options)) == NULL)
4197
(void) put_error(&con, NULL);
4207
(void) put_error(con, NULL);
4198
4208
(void) fflush(stdout);
4202
if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
4212
if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
4205
4215
if (opt_silent < 2)
4207
(void) put_error(&con, NULL);
4217
(void) put_error(con, NULL);
4208
4218
(void) fflush(stdout);
4209
4219
return ignore_errors ? -1 : 1; // Abort
4241
tee_fprintf(stdout, _("\nConnection id:\t\t%lu\n"),drizzle_con_thread_id(&con));
4251
tee_fprintf(stdout, _("\nConnection id:\t\t%lu\n"),drizzle_con_thread_id(con));
4243
4253
Don't remove "limit 1",
4244
4254
it is protection againts SQL_SELECT_LIMIT=0
4246
if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
4256
if (drizzle_query_str(con, &result, "select DATABASE(), USER() limit 1",
4247
4257
&ret) != NULL && ret == DRIZZLE_RETURN_OK &&
4248
4258
drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
4275
4285
tee_fprintf(stdout, _("Current pager:\t\t%s\n"), pager.c_str());
4276
4286
tee_fprintf(stdout, _("Using outfile:\t\t'%s'\n"), opt_outfile ? outfile.c_str() : "");
4277
4287
tee_fprintf(stdout, _("Using delimiter:\t%s\n"), delimiter);
4278
tee_fprintf(stdout, _("Server version:\t\t%s\n"), server_version_string(&con));
4288
tee_fprintf(stdout, _("Server version:\t\t%s\n"), server_version_string(con));
4279
4289
tee_fprintf(stdout, _("Protocol:\t\t%s\n"), opt_protocol.c_str());
4280
tee_fprintf(stdout, _("Protocol version:\t%d\n"), drizzle_con_protocol_version(&con));
4281
tee_fprintf(stdout, _("Connection:\t\t%s\n"), drizzle_con_host(&con));
4290
tee_fprintf(stdout, _("Protocol version:\t%d\n"), drizzle_con_protocol_version(con));
4291
tee_fprintf(stdout, _("Connection:\t\t%s\n"), drizzle_con_host(con));
4282
4292
/* XXX need to save this from result
4283
4293
if ((id= drizzleclient_insert_id(&drizzle)))
4284
4294
tee_fprintf(stdout, "Insert id:\t\t%s\n", internal::llstr(id, buff));
4287
if (drizzle_con_uds(&con))
4288
tee_fprintf(stdout, _("UNIX socket:\t\t%s\n"), drizzle_con_uds(&con));
4297
if (drizzle_con_uds(con))
4299
tee_fprintf(stdout, _("UNIX socket:\t\t%s\n"), drizzle_con_uds(con));
4290
tee_fprintf(stdout, _("TCP port:\t\t%d\n"), drizzle_con_port(&con));
4303
tee_fprintf(stdout, _("TCP port:\t\t%d\n"), drizzle_con_port(con));
4292
4306
if (safe_updates)
4427
4441
error= drizzle_result_error(res);
4428
4442
if (!strcmp(error, ""))
4429
4444
error= drizzle_con_error(local_con);
4432
4449
error= drizzle_con_error(local_con);
4434
4452
return put_info(error, INFO_ERROR,
4435
4453
res == NULL ? drizzle_con_error_code(local_con) :
4592
const char *prompt= connected ? drizzle_con_host(&con) : "not_connected";
4610
const char *prompt= connected ? drizzle_con_host(con) : "not_connected";
4593
4611
if (strstr(prompt, "Localhost"))
4594
4612
processed_prompt->append("localhost");
4611
if (drizzle_con_uds(&con))
4629
if (drizzle_con_uds(con))
4613
const char *pos=strrchr(drizzle_con_uds(&con),'/');
4614
processed_prompt->append(pos ? pos+1 : drizzle_con_uds(&con));
4631
const char *pos=strrchr(drizzle_con_uds(con),'/');
4632
processed_prompt->append(pos ? pos+1 : drizzle_con_uds(con));
4617
add_int_to_prompt(drizzle_con_port(&con));
4635
add_int_to_prompt(drizzle_con_port(con));