281
281
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
282
282
typedef enum enum_info_type INFO_TYPE;
284
static drizzle_st drizzle; /* The library handle */
285
static drizzle_con_st con; /* The connection */
284
static drizzle_st *drizzle= NULL; /* The library handle */
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_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)
2629
2628
if (drizzle_result_buffer(&databases) != DRIZZLE_RETURN_OK)
2630
put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2630
put_info(drizzle_error(drizzle),INFO_INFO,0,0);
2633
2634
while ((database_row=drizzle_row_next(&databases)))
2646
2647
query.append(current_db);
2647
2648
query.append("' order by table_name");
2649
if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2650
if (drizzle_query(con, &fields, query.c_str(), query.length(), &ret) != NULL)
2652
2652
if (ret == DRIZZLE_RETURN_OK &&
2653
2653
drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2712
2712
current_db.erase();
2713
2713
current_db= "";
2714
2714
/* In case of error below current_db will be NULL */
2715
if (drizzle_query_str(&con, &res, "SELECT DATABASE()", &ret) != NULL)
2715
if (drizzle_query_str(con, &res, "SELECT DATABASE()", &ret) != NULL)
2717
2717
if (ret == DRIZZLE_RETURN_OK &&
2718
2718
drizzle_result_buffer(&res) == DRIZZLE_RETURN_OK)
2738
2738
for (uint32_t retry=0;; retry++)
2741
if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2741
if (drizzle_query(con, result, buf, length, &ret) != NULL and
2742
2742
ret == DRIZZLE_RETURN_OK)
2746
error= put_error(&con, result);
2746
error= put_error(con, result);
2748
2748
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2765
2765
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
2767
2767
if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
2770
if (drizzle_con_error(&con)[0])
2772
if (drizzle_con_error(con)[0])
2772
int ret= put_error(&con, result);
2774
int ret= put_error(con, result);
2773
2775
drizzle_result_free(result);
2928
2931
(long) drizzle_result_row_count(&result));
2930
2933
if (drizzle_result_error_code(&result))
2931
error= put_error(&con, &result);
2934
error= put_error(con, &result);
2934
2937
else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2963
2966
fflush(stdout);
2964
2967
drizzle_result_free(&result);
2966
if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2969
if (drizzle_con_status(con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2968
if (drizzle_result_read(&con, &result, &ret) == NULL ||
2971
if (drizzle_result_read(con, &result, &ret) == NULL ||
2969
2972
ret != DRIZZLE_RETURN_OK)
2971
2974
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2974
2977
drizzle_result_free(&result);
2977
error= put_error(&con, NULL);
2980
error= put_error(con, NULL);
2982
} while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2985
} while (drizzle_con_status(con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2984
error= put_error(&con, NULL);
2988
error= put_error(con, NULL);
2989
2994
if (show_warnings == 1 && (warnings >= 1 || error))
2990
2995
print_warnings(error_code);
2992
if (!error && !status.getBatch() &&
2993
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)
2995
3000
get_current_db();
3003
executing_query= false;
2999
3004
return error; /* New command follows */
3003
3008
static void init_pager()
3010
if (opt_nopager == false)
3007
3012
if (!(PAGER= popen(pager.c_str(), "w")))
3026
3033
FILE* new_outfile;
3027
3034
if (opt_outfile)
3029
if (!(new_outfile= fopen(file_name, "a")))
3039
if ((new_outfile= fopen(file_name, "a")) == NULL)
3031
3041
tee_fprintf(stdout, _("Error logging to file '%s'\n"), file_name);
3259
3269
cur= drizzle_row_buffer(result, &ret);
3260
3270
if (ret != DRIZZLE_RETURN_OK)
3262
(void)put_error(&con, result);
3272
(void)put_error(con, result);
3465
3475
cur= drizzle_row_buffer(result, &ret);
3466
3476
if (ret != DRIZZLE_RETURN_OK)
3468
(void)put_error(&con, result);
3478
(void)put_error(con, result);
3631
3641
cur= drizzle_row_buffer(result, &ret);
3632
3642
if (ret != DRIZZLE_RETURN_OK)
3634
(void)put_error(&con, result);
3644
(void)put_error(con, result);
3878
sprintf(buff, _("Connection id: %u"), drizzle_con_thread_id(&con));
3888
sprintf(buff, _("Connection id: %u"), drizzle_con_thread_id(con));
3879
3889
put_info(buff,INFO_INFO,0,0);
3880
3890
sprintf(buff, _("Current schema: %.128s\n"),
3881
3891
!current_db.empty() ? current_db.c_str() : _("*** NONE ***"));
4026
4036
return opt_reconnect ? -1 : 1; // Fatal error
4027
4037
for (bool try_again= true; try_again; try_again= false)
4029
if (drizzle_select_db(&con,&result,tmp,&ret) == NULL ||
4039
if (drizzle_select_db(con, &result, tmp, &ret) == NULL ||
4030
4040
ret != DRIZZLE_RETURN_OK)
4032
4042
if (ret == DRIZZLE_RETURN_ERROR_CODE)
4034
int error= put_error(&con, &result);
4044
int error= put_error(con, &result);
4035
4045
drizzle_result_free(&result);
4039
4049
if (ret != DRIZZLE_RETURN_LOST_CONNECTION || !try_again)
4040
return put_error(&con, NULL);
4051
return put_error(con, NULL);
4042
4054
if (reconnect())
4043
4056
return opt_reconnect ? -1 : 1; // Fatal error
4046
4060
drizzle_result_free(&result);
4067
4081
printf("... ");
4070
if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
4084
if (drizzle_shutdown(con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
4071
4085
&ret) == NULL || ret != DRIZZLE_RETURN_OK)
4073
4087
if (ret == DRIZZLE_RETURN_ERROR_CODE)
4075
fprintf(stderr, _("shutdown failed; error: '%s'"),
4076
drizzle_result_error(&result));
4089
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_result_error(&result));
4077
4090
drizzle_result_free(&result);
4081
fprintf(stderr, _("shutdown failed; error: '%s'"),
4082
drizzle_con_error(&con));
4094
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_con_error(con));
4180
drizzle_con_free(&con);
4181
drizzle_free(&drizzle);
4183
drizzle_create(&drizzle);
4185
if (drizzle_con_add_tcp(&drizzle, &con, (char *)host.c_str(),
4186
opt_drizzle_port, (char *)user.c_str(),
4187
(char *)password.c_str(), (char *)database.c_str(),
4188
global_con_options) == NULL)
4190
(void) put_error(&con, NULL);
4192
drizzle_con_free(con);
4193
drizzle_free(drizzle);
4196
drizzle= drizzle_create();
4197
if (drizzle == 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)
4207
(void) put_error(con, NULL);
4191
4208
(void) fflush(stdout);
4195
if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
4212
if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
4198
4215
if (opt_silent < 2)
4200
(void) put_error(&con, NULL);
4217
(void) put_error(con, NULL);
4201
4218
(void) fflush(stdout);
4202
4219
return ignore_errors ? -1 : 1; // Abort
4234
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));
4236
4253
Don't remove "limit 1",
4237
4254
it is protection againts SQL_SELECT_LIMIT=0
4239
if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
4256
if (drizzle_query_str(con, &result, "select DATABASE(), USER() limit 1",
4240
4257
&ret) != NULL && ret == DRIZZLE_RETURN_OK &&
4241
4258
drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
4268
4285
tee_fprintf(stdout, _("Current pager:\t\t%s\n"), pager.c_str());
4269
4286
tee_fprintf(stdout, _("Using outfile:\t\t'%s'\n"), opt_outfile ? outfile.c_str() : "");
4270
4287
tee_fprintf(stdout, _("Using delimiter:\t%s\n"), delimiter);
4271
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));
4272
4289
tee_fprintf(stdout, _("Protocol:\t\t%s\n"), opt_protocol.c_str());
4273
tee_fprintf(stdout, _("Protocol version:\t%d\n"), drizzle_con_protocol_version(&con));
4274
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));
4275
4292
/* XXX need to save this from result
4276
4293
if ((id= drizzleclient_insert_id(&drizzle)))
4277
4294
tee_fprintf(stdout, "Insert id:\t\t%s\n", internal::llstr(id, buff));
4280
if (drizzle_con_uds(&con))
4281
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));
4283
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));
4285
4306
if (safe_updates)
4420
4441
error= drizzle_result_error(res);
4421
4442
if (!strcmp(error, ""))
4422
4444
error= drizzle_con_error(local_con);
4425
4449
error= drizzle_con_error(local_con);
4427
4452
return put_info(error, INFO_ERROR,
4428
4453
res == NULL ? drizzle_con_error_code(local_con) :
4585
const char *prompt= connected ? drizzle_con_host(&con) : "not_connected";
4610
const char *prompt= connected ? drizzle_con_host(con) : "not_connected";
4586
4611
if (strstr(prompt, "Localhost"))
4587
4612
processed_prompt->append("localhost");
4604
if (drizzle_con_uds(&con))
4629
if (drizzle_con_uds(con))
4606
const char *pos=strrchr(drizzle_con_uds(&con),'/');
4607
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));
4610
add_int_to_prompt(drizzle_con_port(&con));
4635
add_int_to_prompt(drizzle_con_port(con));