5337
Append all results from ps execution to the dynamic string separated
5338
with '\t'. Values may be converted with 'replace_column'
5341
void append_stmt_result(DYNAMIC_STRING *ds, MYSQL_STMT *stmt,
5342
MYSQL_FIELD *fields, uint num_fields)
5344
MYSQL_BIND *my_bind;
5349
/* Allocate array with bind structs, lengths and NULL flags */
5350
my_bind= (MYSQL_BIND*) my_malloc(num_fields * sizeof(MYSQL_BIND),
5351
MYF(MY_WME | MY_FAE | MY_ZEROFILL));
5352
length= (ulong*) my_malloc(num_fields * sizeof(ulong),
5353
MYF(MY_WME | MY_FAE));
5354
is_null= (my_bool*) my_malloc(num_fields * sizeof(my_bool),
5355
MYF(MY_WME | MY_FAE));
5357
/* Allocate data for the result of each field */
5358
for (i= 0; i < num_fields; i++)
5360
uint max_length= fields[i].max_length + 1;
5361
my_bind[i].buffer_type= MYSQL_TYPE_STRING;
5362
my_bind[i].buffer= (char *)my_malloc(max_length, MYF(MY_WME | MY_FAE));
5363
my_bind[i].buffer_length= max_length;
5364
my_bind[i].is_null= &is_null[i];
5365
my_bind[i].length= &length[i];
5367
DBUG_PRINT("bind", ("col[%d]: buffer_type: %d, buffer_length: %lu",
5368
i, my_bind[i].buffer_type, my_bind[i].buffer_length));
5371
if (mysql_stmt_bind_result(stmt, my_bind))
5372
die("mysql_stmt_bind_result failed: %d: %s",
5373
mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
5375
while (mysql_stmt_fetch(stmt) == 0)
5377
for (i= 0; i < num_fields; i++)
5378
append_field(ds, i, &fields[i], (const char *) my_bind[i].buffer,
5379
*my_bind[i].length, *my_bind[i].is_null);
5380
if (!display_result_vertically)
5381
dynstr_append_mem(ds, "\n", 1);
5384
if (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
5385
die("fetch didn't end with MYSQL_NO_DATA from statement: %d %s",
5386
mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
5388
for (i= 0; i < num_fields; i++)
5390
/* Free data for output */
5391
my_free(my_bind[i].buffer, MYF(MY_WME | MY_FAE));
5393
/* Free array with bind structs, lengths and NULL flags */
5394
my_free(my_bind , MYF(MY_WME | MY_FAE));
5395
my_free(length , MYF(MY_WME | MY_FAE));
5396
my_free(is_null , MYF(MY_WME | MY_FAE));
5293
5401
Append metadata for fields to output
5812
Run query using prepared statement C API
5816
mysql - mysql handle
5817
command - currrent command pointer
5818
query - query string to execute
5819
query_len - length query string to execute
5820
ds - output buffer where to store result form query
5823
error - function will not return
5826
void run_query_stmt(MYSQL *mysql, struct st_command *command,
5827
char *query, int query_len, DYNAMIC_STRING *ds,
5828
DYNAMIC_STRING *ds_warnings)
5830
MYSQL_RES *res= NULL; /* Note that here 'res' is meta data result set */
5832
DYNAMIC_STRING ds_prepare_warnings;
5833
DYNAMIC_STRING ds_execute_warnings;
5834
DBUG_ENTER("run_query_stmt");
5835
DBUG_PRINT("query", ("'%-.60s'", query));
5838
Init a new stmt if it's not already one created for this connection
5840
if(!(stmt= cur_con->stmt))
5842
if (!(stmt= mysql_stmt_init(mysql)))
5843
die("unable to init stmt structure");
5844
cur_con->stmt= stmt;
5847
/* Init dynamic strings for warnings */
5848
if (!disable_warnings)
5850
init_dynamic_string(&ds_prepare_warnings, NULL, 0, 256);
5851
init_dynamic_string(&ds_execute_warnings, NULL, 0, 256);
5857
if (mysql_stmt_prepare(stmt, query, query_len))
5859
handle_error(command, mysql_stmt_errno(stmt),
5860
mysql_stmt_error(stmt), mysql_stmt_sqlstate(stmt), ds);
5865
Get the warnings from mysql_stmt_prepare and keep them in a
5868
if (!disable_warnings)
5869
append_warnings(&ds_prepare_warnings, mysql);
5872
No need to call mysql_stmt_bind_param() because we have no
5876
#if MYSQL_VERSION_ID >= 50000
5877
if (cursor_protocol_enabled)
5880
Use cursor when retrieving result
5882
ulong type= CURSOR_TYPE_READ_ONLY;
5883
if (mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type))
5884
die("mysql_stmt_attr_set(STMT_ATTR_CURSOR_TYPE) failed': %d %s",
5885
mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
5892
if (mysql_stmt_execute(stmt))
5894
handle_error(command, mysql_stmt_errno(stmt),
5895
mysql_stmt_error(stmt), mysql_stmt_sqlstate(stmt), ds);
5900
When running in cursor_protocol get the warnings from execute here
5901
and keep them in a separate string for later.
5903
if (cursor_protocol_enabled && !disable_warnings)
5904
append_warnings(&ds_execute_warnings, mysql);
5907
We instruct that we want to update the "max_length" field in
5908
mysql_stmt_store_result(), this is our only way to know how much
5909
buffer to allocate for result data
5913
if (mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &one))
5914
die("mysql_stmt_attr_set(STMT_ATTR_UPDATE_MAX_LENGTH) failed': %d %s",
5915
mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
5919
If we got here the statement succeeded and was expected to do so,
5920
get data. Note that this can still give errors found during execution!
5921
Store the result of the query if if will return any fields
5923
if (mysql_stmt_field_count(stmt) && mysql_stmt_store_result(stmt))
5925
handle_error(command, mysql_stmt_errno(stmt),
5926
mysql_stmt_error(stmt), mysql_stmt_sqlstate(stmt), ds);
5930
/* If we got here the statement was both executed and read successfully */
5931
handle_no_error(command);
5932
if (!disable_result_log)
5935
Not all statements creates a result set. If there is one we can
5936
now create another normal result set that contains the meta
5937
data. This set can be handled almost like any other non prepared
5938
statement result set.
5940
if ((res= mysql_stmt_result_metadata(stmt)) != NULL)
5942
/* Take the column count from meta info */
5943
MYSQL_FIELD *fields= mysql_fetch_fields(res);
5944
uint num_fields= mysql_num_fields(res);
5946
if (display_metadata)
5947
append_metadata(ds, fields, num_fields);
5949
if (!display_result_vertically)
5950
append_table_headings(ds, fields, num_fields);
5952
append_stmt_result(ds, stmt, fields, num_fields);
5954
mysql_free_result(res); /* Free normal result set with meta data */
5956
/* Clear prepare warnings */
5957
dynstr_set(&ds_prepare_warnings, NULL);
5962
This is a query without resultset
5966
if (!disable_warnings)
5968
/* Get the warnings from execute */
5970
/* Append warnings to ds - if there are any */
5971
if (append_warnings(&ds_execute_warnings, mysql) ||
5972
ds_execute_warnings.length ||
5973
ds_prepare_warnings.length ||
5974
ds_warnings->length)
5976
dynstr_append_mem(ds, "Warnings:\n", 10);
5977
if (ds_warnings->length)
5978
dynstr_append_mem(ds, ds_warnings->str,
5979
ds_warnings->length);
5980
if (ds_prepare_warnings.length)
5981
dynstr_append_mem(ds, ds_prepare_warnings.str,
5982
ds_prepare_warnings.length);
5983
if (ds_execute_warnings.length)
5984
dynstr_append_mem(ds, ds_execute_warnings.str,
5985
ds_execute_warnings.length);
5990
append_info(ds, mysql_affected_rows(mysql), mysql_info(mysql));
5995
if (!disable_warnings)
5997
dynstr_free(&ds_prepare_warnings);
5998
dynstr_free(&ds_execute_warnings);
6002
/* Close the statement if - no reconnect, need new prepare */
6003
if (mysql->reconnect)
6005
mysql_stmt_close(stmt);
6006
cur_con->stmt= NULL;
6010
We save the return code (mysql_stmt_errno(stmt)) from the last call sent
6011
to the server into the mysqltest builtin variable $mysql_errno. This
6012
variable then can be used from the test case itself.
6015
var_set_errno(mysql_stmt_errno(stmt));
5705
6023
Create a util connection if one does not already exists