946
951
#endif /*!EMBEDDED_LIBRARY*/
954
Setup data conversion routines using an array of parameter
955
markers from the original prepared statement.
956
Swap the parameter data of the original prepared
957
statement to the new one.
959
Used only when we re-prepare a prepared statement.
960
There are two reasons for this function to exist:
962
1) In the binary client/server protocol, parameter metadata
963
is sent only at first execute. Consequently, if we need to
964
reprepare a prepared statement at a subsequent execution,
965
we may not have metadata information in the packet.
966
In that case we use the parameter array of the original
967
prepared statement to setup parameter types of the new
970
2) In the binary client/server protocol, we may supply
971
long data in pieces. When the last piece is supplied,
972
we assemble the pieces and convert them from client
973
character set to the connection character set. After
974
that the parameter value is only available inside
975
the parameter, the original pieces are lost, and thus
976
we can only assign the corresponding parameter of the
977
reprepared statement from the original value.
979
@param[out] param_array_dst parameter markers of the new statement
980
@param[in] param_array_src parameter markers of the original
982
@param[in] param_count total number of parameters. Is the
983
same in src and dst arrays, since
984
the statement query is the same
986
@return this function never fails
990
swap_parameter_array(Item_param **param_array_dst,
991
Item_param **param_array_src,
994
Item_param **dst= param_array_dst;
995
Item_param **src= param_array_src;
996
Item_param **end= param_array_dst + param_count;
998
for (; dst < end; ++src, ++dst)
999
(*dst)->set_param_type_and_swap_value(*src);
950
1004
Assign prepared statement parameters from user variables.
2321
2443
/* First of all clear possible warnings from the previous command */
2322
2444
mysql_reset_thd_for_next_command(thd);
2324
if (!(stmt= find_prepared_statement(thd, stmt_id, "mysql_stmt_execute")))
2446
if (!(stmt= find_prepared_statement(thd, stmt_id)))
2449
my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), sizeof(llbuf),
2450
llstr(stmt_id, llbuf), "mysql_stmt_execute");
2325
2451
DBUG_VOID_RETURN;
2327
2454
#if defined(ENABLED_PROFILING)
2328
2455
thd->profiling.set_query_source(stmt->query, stmt->query_length);
2330
2457
DBUG_PRINT("exec_query", ("%s", stmt->query));
2331
DBUG_PRINT("info",("stmt: 0x%lx", (long) stmt));
2458
DBUG_PRINT("info",("stmt: %p", stmt));
2333
2460
sp_cache_flush_obsolete(&thd->sp_proc_cache);
2334
2461
sp_cache_flush_obsolete(&thd->sp_func_cache);
2336
#ifndef EMBEDDED_LIBRARY
2337
if (stmt->param_count)
2339
uchar *null_array= packet;
2340
if (setup_conversion_functions(stmt, &packet, packet_end) ||
2341
stmt->set_params(stmt, null_array, packet, packet_end,
2343
goto set_params_data_err;
2347
In embedded library we re-install conversion routines each time
2348
we set params, and also we don't need to parse packet.
2349
So we do it in one function.
2351
if (stmt->param_count && stmt->set_params_data(stmt, &expanded_query))
2352
goto set_params_data_err;
2354
if (!(specialflag & SPECIAL_NO_PRIOR))
2355
my_pthread_setprio(pthread_self(),QUERY_PRIOR);
2358
If the free_list is not empty, we'll wrongly free some externally
2359
allocated items when cleaning up after validation of the prepared
2362
DBUG_ASSERT(thd->free_list == NULL);
2364
error= stmt->execute(&expanded_query,
2365
test(flags & (ulong) CURSOR_TYPE_READ_ONLY));
2366
if (!(specialflag & SPECIAL_NO_PRIOR))
2367
my_pthread_setprio(pthread_self(), WAIT_PRIOR);
2370
set_params_data_err:
2371
my_error(ER_WRONG_ARGUMENTS, MYF(0), "mysql_stmt_execute");
2372
reset_stmt_params(stmt);
2463
open_cursor= test(flags & (ulong) CURSOR_TYPE_READ_ONLY);
2465
stmt->execute_loop(&expanded_query, open_cursor, packet, packet_end);
2988
3114
DBUG_RETURN(error);
3119
Assign parameter values either from variables, in case of SQL PS
3120
or from the execute packet.
3122
@param expanded_query a container with the original SQL statement.
3123
'?' placeholders will be replaced with
3124
their values in case of success.
3125
The result is used for logging and replication
3126
@param packet pointer to execute packet.
3127
NULL in case of SQL PS
3128
@param packet_end end of the packet. NULL in case of SQL PS
3130
@todo Use a paremeter source class family instead of 'if's, and
3131
support stored procedure variables.
3133
@retval TRUE an error occurred when assigning a parameter (likely
3134
a conversion error or out of memory, or malformed packet)
3135
@retval FALSE success
3139
Prepared_statement::set_parameters(String *expanded_query,
3140
uchar *packet, uchar *packet_end)
3142
bool is_sql_ps= packet == NULL;
3147
/* SQL prepared statement */
3148
res= set_params_from_vars(this, thd->lex->prepared_stmt_params,
3151
else if (param_count)
3153
#ifndef EMBEDDED_LIBRARY
3154
uchar *null_array= packet;
3155
res= (setup_conversion_functions(this, &packet, packet_end) ||
3156
set_params(this, null_array, packet, packet_end, expanded_query));
3159
In embedded library we re-install conversion routines each time
3160
we set parameters, and also we don't need to parse packet.
3161
So we do it in one function.
3163
res= set_params_data(this, expanded_query);
3168
my_error(ER_WRONG_ARGUMENTS, MYF(0),
3169
is_sql_ps ? "EXECUTE" : "mysql_stmt_execute");
3170
reset_stmt_params(this);
3177
Execute a prepared statement. Re-prepare it a limited number
3178
of times if necessary.
3180
Try to execute a prepared statement. If there is a metadata
3181
validation error, prepare a new copy of the prepared statement,
3182
swap the old and the new statements, and try again.
3183
If there is a validation error again, repeat the above, but
3184
perform no more than MAX_REPREPARE_ATTEMPTS.
3186
@note We have to try several times in a loop since we
3187
release metadata locks on tables after prepared statement
3188
prepare. Therefore, a DDL statement may sneak in between prepare
3189
and execute of a new statement. If this happens repeatedly
3190
more than MAX_REPREPARE_ATTEMPTS times, we give up.
3192
In future we need to be able to keep the metadata locks between
3193
prepare and execute, but right now open_and_lock_tables(), as
3194
well as close_thread_tables() are buried deep inside
3195
execution code (mysql_execute_command()).
3197
@return TRUE if an error, FALSE if success
3198
@retval TRUE either MAX_REPREPARE_ATTEMPTS has been reached,
3199
or some general error
3200
@retval FALSE successfully executed the statement, perhaps
3201
after having reprepared it a few times.
3205
Prepared_statement::execute_loop(String *expanded_query,
3210
const int MAX_REPREPARE_ATTEMPTS= 3;
3211
Reprepare_observer reprepare_observer;
3213
int reprepare_attempt= 0;
3215
if (set_parameters(expanded_query, packet, packet_end))
3219
reprepare_observer.reset_reprepare_observer();
3222
If the free_list is not empty, we'll wrongly free some externally
3223
allocated items when cleaning up after validation of the prepared
3226
DBUG_ASSERT(thd->free_list == NULL);
3229
Install the metadata observer. If some metadata version is
3230
different from prepare time and an observer is installed,
3231
the observer method will be invoked to push an error into
3234
if (sql_command_flags[lex->sql_command] &
3235
CF_REEXECUTION_FRAGILE)
3237
DBUG_ASSERT(thd->m_reprepare_observer == NULL);
3238
thd->m_reprepare_observer = &reprepare_observer;
3241
if (!(specialflag & SPECIAL_NO_PRIOR))
3242
my_pthread_setprio(pthread_self(),QUERY_PRIOR);
3244
error= execute(expanded_query, open_cursor) || thd->is_error();
3246
if (!(specialflag & SPECIAL_NO_PRIOR))
3247
my_pthread_setprio(pthread_self(), WAIT_PRIOR);
3249
thd->m_reprepare_observer= NULL;
3251
if (error && !thd->is_fatal_error && !thd->killed &&
3252
reprepare_observer.is_invalidated() &&
3253
reprepare_attempt++ < MAX_REPREPARE_ATTEMPTS)
3255
DBUG_ASSERT(thd->main_da.sql_errno() == ER_NEED_REPREPARE);
3260
if (! error) /* Success */
3263
reset_stmt_params(this);
3270
Reprepare this prepared statement.
3272
Currently this is implemented by creating a new prepared
3273
statement, preparing it with the original query and then
3274
swapping the new statement and the original one.
3276
@retval TRUE an error occurred. Possible errors include
3277
incompatibility of new and old result set
3279
@retval FALSE success, the statement has been reprepared
3283
Prepared_statement::reprepare()
3285
char saved_cur_db_name_buf[NAME_LEN+1];
3286
LEX_STRING saved_cur_db_name=
3287
{ saved_cur_db_name_buf, sizeof(saved_cur_db_name_buf) };
3288
LEX_STRING stmt_db_name= { db, db_length };
3289
bool cur_db_changed;
3292
Prepared_statement copy(thd, &thd->protocol_text);
3294
status_var_increment(thd->status_var.com_stmt_reprepare);
3296
if (mysql_opt_change_db(thd, &stmt_db_name, &saved_cur_db_name, TRUE,
3300
error= (name.str && copy.set_name(&name) ||
3301
copy.prepare(query, query_length) ||
3302
validate_metadata(©));
3305
mysql_change_db(thd, &saved_cur_db_name, TRUE);
3309
swap_prepared_statement(©);
3310
swap_parameter_array(param_array, copy.param_array, param_count);
3312
is_reprepared= TRUE;
3315
Clear possible warnings during reprepare, it has to be completely
3316
transparent to the user. We use mysql_reset_errors() since
3317
there were no separate query id issued for re-prepare.
3318
Sic: we can't simply silence warnings during reprepare, because if
3319
it's failed, we need to return all the warnings to the user.
3321
mysql_reset_errors(thd, TRUE);
3328
Validate statement result set metadata (if the statement returns
3331
Currently we only check that the number of columns of the result
3333
This is a helper method used during re-prepare.
3335
@param[in] copy the re-prepared prepared statement to verify
3338
@retval TRUE error, ER_PS_REBIND is reported
3339
@retval FALSE statement return no or compatible metadata
3343
bool Prepared_statement::validate_metadata(Prepared_statement *copy)
3346
If this is an SQL prepared statement or EXPLAIN,
3347
return FALSE -- the metadata of the original SELECT,
3348
if any, has not been sent to the client.
3350
if (is_protocol_text() || lex->describe)
3353
if (lex->select_lex.item_list.elements !=
3354
copy->lex->select_lex.item_list.elements)
3356
/** Column counts mismatch, update the client */
3357
thd->server_status|= SERVER_STATUS_METADATA_CHANGED;
3365
Replace the original prepared statement with a prepared copy.
3367
This is a private helper that is used as part of statement
3370
@return This function does not return any errors.
3374
Prepared_statement::swap_prepared_statement(Prepared_statement *copy)
3378
/* Swap memory roots. */
3379
swap_variables(MEM_ROOT, main_mem_root, copy->main_mem_root);
3381
/* Swap the arenas */
3382
tmp_stmt.set_query_arena(this);
3383
set_query_arena(copy);
3384
copy->set_query_arena(&tmp_stmt);
3386
/* Swap the statement parent classes */
3387
tmp_stmt.set_statement(this);
3388
set_statement(copy);
3389
copy->set_statement(&tmp_stmt);
3391
/* Swap ids back, we need the original id */
3392
swap_variables(ulong, id, copy->id);
3393
/* Swap mem_roots back, they must continue pointing at the main_mem_roots */
3394
swap_variables(MEM_ROOT *, mem_root, copy->mem_root);
3396
Swap the old and the new parameters array. The old array
3397
is allocated in the old arena.
3399
swap_variables(Item_param **, param_array, copy->param_array);
3400
/* Swap flags: this is perhaps unnecessary */
3401
swap_variables(uint, flags, copy->flags);
3402
/* Swap names, the old name is allocated in the wrong memory root */
3403
swap_variables(LEX_STRING, name, copy->name);
3405
swap_variables(char *, db, copy->db);
3406
swap_variables(ulong, m_sp_cache_version, copy->m_sp_cache_version);
3408
DBUG_ASSERT(db_length == copy->db_length);
3409
DBUG_ASSERT(param_count == copy->param_count);
3410
DBUG_ASSERT(thd == copy->thd);
3411
last_error[0]= '\0';
3413
/* Do not swap protocols, the copy always has protocol_text */
2992
3418
Execute a prepared statement.