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= NULL; /* The library handle */
285
static drizzle_con_st *con= NULL; /* The connection */
284
static drizzle_st drizzle; /* The library handle */
285
static drizzle_con_st con; /* 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;
1194
1195
printf(_("shutting down drizzled"));
1195
1196
if (opt_drizzle_port > 0)
1197
1197
printf(_(" on port %d"), opt_drizzle_port);
1199
1198
printf("... ");
1202
drizzle_return_t ret;
1203
if (drizzle_shutdown(con, &result, DRIZZLE_SHUTDOWN_DEFAULT, &ret) == NULL or
1204
ret != DRIZZLE_RETURN_OK)
1201
if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
1202
&ret) == NULL || ret != DRIZZLE_RETURN_OK)
1206
1204
if (ret == DRIZZLE_RETURN_ERROR_CODE)
1208
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_result_error(&result));
1206
fprintf(stderr, _("shutdown failed; error: '%s'"),
1207
drizzle_result_error(&result));
1209
1208
drizzle_result_free(&result);
1213
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_con_error(con));
1212
fprintf(stderr, _("shutdown failed; error: '%s'"),
1213
drizzle_con_error(&con));
1236
1234
printf("... ");
1239
if (drizzle_kill(con, &result, query_id,
1237
if (drizzle_kill(&con, &result, query_id,
1240
1238
&ret) == NULL || ret != DRIZZLE_RETURN_OK)
1242
1240
if (ret == DRIZZLE_RETURN_ERROR_CODE)
1250
fprintf(stderr, _("kill failed; error: '%s'"), drizzle_con_error(con));
1248
fprintf(stderr, _("kill failed; error: '%s'"),
1249
drizzle_con_error(&con));
1273
1272
drizzle_result_st result;
1274
1273
drizzle_return_t ret;
1276
if (drizzle_ping(con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
1275
if (drizzle_ping(&con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
1278
1277
if (opt_silent < 2)
1279
1278
printf(_("drizzled is alive\n"));
1291
fprintf(stderr, _("drizzled won't answer to ping, error: '%s'"), drizzle_con_error(con));
1290
fprintf(stderr, _("drizzled won't answer to ping, error: '%s'"),
1291
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;
1940
1945
/* terminate if no query being executed, or we already tried interrupting */
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;
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;
1972
1973
drizzle_end(sig);
2621
2622
/* hash Drizzle functions (to be implemented) */
2623
2624
/* hash all database names */
2624
if (drizzle_query_str(con, &databases, "select schema_name from information_schema.schemata", &ret) != NULL)
2625
if (drizzle_query_str(&con, &databases, "select schema_name from information_schema.schemata", &ret) != NULL)
2626
2627
if (ret == DRIZZLE_RETURN_OK)
2628
2629
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);
2634
2633
while ((database_row=drizzle_row_next(&databases)))
2647
2646
query.append(current_db);
2648
2647
query.append("' order by table_name");
2650
if (drizzle_query(con, &fields, query.c_str(), query.length(), &ret) != NULL)
2649
if (drizzle_query(&con, &fields, query.c_str(), query.length(),
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 and
2741
if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
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)
2772
if (drizzle_con_error(con)[0])
2770
if (drizzle_con_error(&con)[0])
2774
int ret= put_error(con, result);
2772
int ret= put_error(&con, result);
2775
2773
drizzle_result_free(result);
2931
2928
(long) drizzle_result_row_count(&result));
2933
2930
if (drizzle_result_error_code(&result))
2934
error= put_error(con, &result);
2931
error= put_error(&con, &result);
2937
2934
else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2966
2963
fflush(stdout);
2967
2964
drizzle_result_free(&result);
2969
if (drizzle_con_status(con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2966
if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2971
if (drizzle_result_read(con, &result, &ret) == NULL ||
2968
if (drizzle_result_read(&con, &result, &ret) == NULL ||
2972
2969
ret != DRIZZLE_RETURN_OK)
2974
2971
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2977
2974
drizzle_result_free(&result);
2980
error= put_error(con, NULL);
2977
error= put_error(&con, NULL);
2985
} while (drizzle_con_status(con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2982
} while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2988
error= put_error(con, NULL);
2984
error= put_error(&con, NULL);
2994
2989
if (show_warnings == 1 && (warnings >= 1 || error))
2995
2990
print_warnings(error_code);
2997
if (!error && !status.getBatch() and
2998
drizzle_con_status(con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2992
if (!error && !status.getBatch() &&
2993
drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
3000
2995
get_current_db();
3003
executing_query= false;
3004
2999
return error; /* New command follows */
3008
3003
static void init_pager()
3010
if (opt_nopager == false)
3012
3007
if (!(PAGER= popen(pager.c_str(), "w")))
3033
3026
FILE* new_outfile;
3034
3027
if (opt_outfile)
3039
if ((new_outfile= fopen(file_name, "a")) == NULL)
3029
if (!(new_outfile= fopen(file_name, "a")))
3041
3031
tee_fprintf(stdout, _("Error logging to file '%s'\n"), file_name);
3269
3259
cur= drizzle_row_buffer(result, &ret);
3270
3260
if (ret != DRIZZLE_RETURN_OK)
3272
(void)put_error(con, result);
3262
(void)put_error(&con, result);
3475
3465
cur= drizzle_row_buffer(result, &ret);
3476
3466
if (ret != DRIZZLE_RETURN_OK)
3478
(void)put_error(con, result);
3468
(void)put_error(&con, result);
3641
3631
cur= drizzle_row_buffer(result, &ret);
3642
3632
if (ret != DRIZZLE_RETURN_OK)
3644
(void)put_error(con, result);
3634
(void)put_error(&con, result);
3888
sprintf(buff, _("Connection id: %u"), drizzle_con_thread_id(con));
3878
sprintf(buff, _("Connection id: %u"), drizzle_con_thread_id(&con));
3889
3879
put_info(buff,INFO_INFO,0,0);
3890
3880
sprintf(buff, _("Current schema: %.128s\n"),
3891
3881
!current_db.empty() ? current_db.c_str() : _("*** NONE ***"));
4036
4026
return opt_reconnect ? -1 : 1; // Fatal error
4037
4027
for (bool try_again= true; try_again; try_again= false)
4039
if (drizzle_select_db(con, &result, tmp, &ret) == NULL ||
4029
if (drizzle_select_db(&con,&result,tmp,&ret) == NULL ||
4040
4030
ret != DRIZZLE_RETURN_OK)
4042
4032
if (ret == DRIZZLE_RETURN_ERROR_CODE)
4044
int error= put_error(con, &result);
4034
int error= put_error(&con, &result);
4045
4035
drizzle_result_free(&result);
4049
4039
if (ret != DRIZZLE_RETURN_LOST_CONNECTION || !try_again)
4051
return put_error(con, NULL);
4040
return put_error(&con, NULL);
4054
4042
if (reconnect())
4056
4043
return opt_reconnect ? -1 : 1; // Fatal error
4060
4046
drizzle_result_free(&result);
4081
4067
printf("... ");
4084
if (drizzle_shutdown(con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
4070
if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
4085
4071
&ret) == NULL || ret != DRIZZLE_RETURN_OK)
4087
4073
if (ret == DRIZZLE_RETURN_ERROR_CODE)
4089
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_result_error(&result));
4075
fprintf(stderr, _("shutdown failed; error: '%s'"),
4076
drizzle_result_error(&result));
4090
4077
drizzle_result_free(&result);
4094
fprintf(stderr, _("shutdown failed; error: '%s'"), drizzle_con_error(con));
4081
fprintf(stderr, _("shutdown failed; error: '%s'"),
4082
drizzle_con_error(&con));
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);
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);
4208
4191
(void) fflush(stdout);
4212
if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
4195
if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
4215
4198
if (opt_silent < 2)
4217
(void) put_error(con, NULL);
4200
(void) put_error(&con, NULL);
4218
4201
(void) fflush(stdout);
4219
4202
return ignore_errors ? -1 : 1; // Abort
4251
tee_fprintf(stdout, _("\nConnection id:\t\t%lu\n"),drizzle_con_thread_id(con));
4234
tee_fprintf(stdout, _("\nConnection id:\t\t%lu\n"),drizzle_con_thread_id(&con));
4253
4236
Don't remove "limit 1",
4254
4237
it is protection againts SQL_SELECT_LIMIT=0
4256
if (drizzle_query_str(con, &result, "select DATABASE(), USER() limit 1",
4239
if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
4257
4240
&ret) != NULL && ret == DRIZZLE_RETURN_OK &&
4258
4241
drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
4285
4268
tee_fprintf(stdout, _("Current pager:\t\t%s\n"), pager.c_str());
4286
4269
tee_fprintf(stdout, _("Using outfile:\t\t'%s'\n"), opt_outfile ? outfile.c_str() : "");
4287
4270
tee_fprintf(stdout, _("Using delimiter:\t%s\n"), delimiter);
4288
tee_fprintf(stdout, _("Server version:\t\t%s\n"), server_version_string(con));
4271
tee_fprintf(stdout, _("Server version:\t\t%s\n"), server_version_string(&con));
4289
4272
tee_fprintf(stdout, _("Protocol:\t\t%s\n"), opt_protocol.c_str());
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));
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));
4292
4275
/* XXX need to save this from result
4293
4276
if ((id= drizzleclient_insert_id(&drizzle)))
4294
4277
tee_fprintf(stdout, "Insert id:\t\t%s\n", internal::llstr(id, buff));
4297
if (drizzle_con_uds(con))
4299
tee_fprintf(stdout, _("UNIX socket:\t\t%s\n"), drizzle_con_uds(con));
4280
if (drizzle_con_uds(&con))
4281
tee_fprintf(stdout, _("UNIX socket:\t\t%s\n"), drizzle_con_uds(&con));
4303
tee_fprintf(stdout, _("TCP port:\t\t%d\n"), drizzle_con_port(con));
4283
tee_fprintf(stdout, _("TCP port:\t\t%d\n"), drizzle_con_port(&con));
4306
4285
if (safe_updates)
4441
4420
error= drizzle_result_error(res);
4442
4421
if (!strcmp(error, ""))
4444
4422
error= drizzle_con_error(local_con);
4449
4425
error= drizzle_con_error(local_con);
4452
4427
return put_info(error, INFO_ERROR,
4453
4428
res == NULL ? drizzle_con_error_code(local_con) :
4610
const char *prompt= connected ? drizzle_con_host(con) : "not_connected";
4585
const char *prompt= connected ? drizzle_con_host(&con) : "not_connected";
4611
4586
if (strstr(prompt, "Localhost"))
4612
4587
processed_prompt->append("localhost");
4629
if (drizzle_con_uds(con))
4604
if (drizzle_con_uds(&con))
4631
const char *pos=strrchr(drizzle_con_uds(con),'/');
4632
processed_prompt->append(pos ? pos+1 : drizzle_con_uds(con));
4606
const char *pos=strrchr(drizzle_con_uds(&con),'/');
4607
processed_prompt->append(pos ? pos+1 : drizzle_con_uds(&con));
4635
add_int_to_prompt(drizzle_con_port(con));
4610
add_int_to_prompt(drizzle_con_port(&con));