| 1 | /* Copyright (c) 2002, 2015, Oracle and/or its affiliates. |
| 2 | Copyright (c) 2008, 2017, MariaDB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; version 2 of the License. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 16 | |
| 17 | /** |
| 18 | @file |
| 19 | |
| 20 | This file contains the implementation of prepared statements. |
| 21 | |
| 22 | When one prepares a statement: |
| 23 | |
| 24 | - Server gets the query from client with command 'COM_STMT_PREPARE'; |
| 25 | in the following format: |
| 26 | [COM_STMT_PREPARE:1] [query] |
| 27 | - Parse the query and recognize any parameter markers '?' and |
| 28 | store its information list in lex->param_list |
| 29 | - Allocate a new statement for this prepare; and keep this in |
| 30 | 'thd->stmt_map'. |
| 31 | - Without executing the query, return back to client the total |
| 32 | number of parameters along with result-set metadata information |
| 33 | (if any) in the following format: |
| 34 | @verbatim |
| 35 | [STMT_ID:4] |
| 36 | [Column_count:2] |
| 37 | [Param_count:2] |
| 38 | [Params meta info (stubs only for now)] (if Param_count > 0) |
| 39 | [Columns meta info] (if Column_count > 0) |
| 40 | @endverbatim |
| 41 | |
| 42 | During prepare the tables used in a statement are opened, but no |
| 43 | locks are acquired. Table opening will block any DDL during the |
| 44 | operation, and we do not need any locks as we neither read nor |
| 45 | modify any data during prepare. Tables are closed after prepare |
| 46 | finishes. |
| 47 | |
| 48 | When one executes a statement: |
| 49 | |
| 50 | - Server gets the command 'COM_STMT_EXECUTE' to execute the |
| 51 | previously prepared query. If there are any parameter markers, then the |
| 52 | client will send the data in the following format: |
| 53 | @verbatim |
| 54 | [COM_STMT_EXECUTE:1] |
| 55 | [STMT_ID:4] |
| 56 | [NULL_BITS:(param_count+7)/8)] |
| 57 | [TYPES_SUPPLIED_BY_CLIENT(0/1):1] |
| 58 | [[length]data] |
| 59 | [[length]data] .. [[length]data]. |
| 60 | @endverbatim |
| 61 | (Note: Except for string/binary types; all other types will not be |
| 62 | supplied with length field) |
| 63 | - If it is a first execute or types of parameters were altered by client, |
| 64 | then setup the conversion routines. |
| 65 | - Assign parameter items from the supplied data. |
| 66 | - Execute the query without re-parsing and send back the results |
| 67 | to client |
| 68 | |
| 69 | During execution of prepared statement tables are opened and locked |
| 70 | the same way they would for normal (non-prepared) statement |
| 71 | execution. Tables are unlocked and closed after the execution. |
| 72 | |
| 73 | When one supplies long data for a placeholder: |
| 74 | |
| 75 | - Server gets the long data in pieces with command type |
| 76 | 'COM_STMT_SEND_LONG_DATA'. |
| 77 | - The packet received will have the format as: |
| 78 | [COM_STMT_SEND_LONG_DATA:1][STMT_ID:4][parameter_number:2][data] |
| 79 | - data from the packet is appended to the long data value buffer for this |
| 80 | placeholder. |
| 81 | - It's up to the client to stop supplying data chunks at any point. The |
| 82 | server doesn't care; also, the server doesn't notify the client whether |
| 83 | it got the data or not; if there is any error, then it will be returned |
| 84 | at statement execute. |
| 85 | */ |
| 86 | |
| 87 | #include "mariadb.h" /* NO_EMBEDDED_ACCESS_CHECKS */ |
| 88 | #include "sql_priv.h" |
| 89 | #include "unireg.h" |
| 90 | #include "sql_class.h" // set_var.h: THD |
| 91 | #include "set_var.h" |
| 92 | #include "sql_prepare.h" |
| 93 | #include "sql_parse.h" // insert_precheck, update_precheck, delete_precheck |
| 94 | #include "sql_base.h" // open_normal_and_derived_tables |
| 95 | #include "sql_cache.h" // query_cache_* |
| 96 | #include "sql_view.h" // create_view_precheck |
| 97 | #include "sql_delete.h" // mysql_prepare_delete |
| 98 | #include "sql_select.h" // for JOIN |
| 99 | #include "sql_insert.h" // upgrade_lock_type_for_insert, mysql_prepare_insert |
| 100 | #include "sql_update.h" // mysql_prepare_update |
| 101 | #include "sql_db.h" // mysql_opt_change_db, mysql_change_db |
| 102 | #include "sql_acl.h" // *_ACL |
| 103 | #include "sql_derived.h" // mysql_derived_prepare, |
| 104 | // mysql_handle_derived |
| 105 | #include "sql_cte.h" |
| 106 | #include "sql_cursor.h" |
| 107 | #include "sql_show.h" |
| 108 | #include "sql_repl.h" |
| 109 | #include "slave.h" |
| 110 | #include "sp_head.h" |
| 111 | #include "sp.h" |
| 112 | #include "sp_cache.h" |
| 113 | #include "sql_handler.h" // mysql_ha_rm_tables |
| 114 | #include "probes_mysql.h" |
| 115 | #ifdef EMBEDDED_LIBRARY |
| 116 | /* include MYSQL_BIND headers */ |
| 117 | #include <mysql.h> |
| 118 | #else |
| 119 | #include <mysql_com.h> |
| 120 | #endif |
| 121 | #include "lock.h" // MYSQL_OPEN_FORCE_SHARED_MDL |
| 122 | #include "sql_handler.h" |
| 123 | #include "transaction.h" // trans_rollback_implicit |
| 124 | #include "wsrep_mysqld.h" |
| 125 | |
| 126 | /** |
| 127 | A result class used to send cursor rows using the binary protocol. |
| 128 | */ |
| 129 | |
| 130 | class Select_fetch_protocol_binary: public select_send |
| 131 | { |
| 132 | Protocol_binary protocol; |
| 133 | public: |
| 134 | Select_fetch_protocol_binary(THD *thd); |
| 135 | virtual bool send_result_set_metadata(List<Item> &list, uint flags); |
| 136 | virtual int send_data(List<Item> &items); |
| 137 | virtual bool send_eof(); |
| 138 | #ifdef EMBEDDED_LIBRARY |
| 139 | void begin_dataset() |
| 140 | { |
| 141 | protocol.begin_dataset(); |
| 142 | } |
| 143 | #endif |
| 144 | }; |
| 145 | |
| 146 | /****************************************************************************/ |
| 147 | |
| 148 | /** |
| 149 | Prepared_statement: a statement that can contain placeholders. |
| 150 | */ |
| 151 | |
| 152 | class Prepared_statement: public Statement |
| 153 | { |
| 154 | public: |
| 155 | enum flag_values |
| 156 | { |
| 157 | IS_IN_USE= 1, |
| 158 | IS_SQL_PREPARE= 2 |
| 159 | }; |
| 160 | |
| 161 | THD *thd; |
| 162 | Select_fetch_protocol_binary result; |
| 163 | Item_param **param_array; |
| 164 | Server_side_cursor *cursor; |
| 165 | uchar *packet; |
| 166 | uchar *packet_end; |
| 167 | uint param_count; |
| 168 | uint last_errno; |
| 169 | uint flags; |
| 170 | char last_error[MYSQL_ERRMSG_SIZE]; |
| 171 | my_bool iterations; |
| 172 | my_bool start_param; |
| 173 | my_bool read_types; |
| 174 | #ifndef EMBEDDED_LIBRARY |
| 175 | bool (*set_params)(Prepared_statement *st, uchar *data, uchar *data_end, |
| 176 | uchar *read_pos, String *expanded_query); |
| 177 | bool (*set_bulk_params)(Prepared_statement *st, |
| 178 | uchar **read_pos, uchar *data_end, bool reset); |
| 179 | #else |
| 180 | bool (*set_params_data)(Prepared_statement *st, String *expanded_query); |
| 181 | /*TODO: add bulk support for builtin server */ |
| 182 | #endif |
| 183 | bool (*set_params_from_actual_params)(Prepared_statement *stmt, |
| 184 | List<Item> &list, |
| 185 | String *expanded_query); |
| 186 | public: |
| 187 | Prepared_statement(THD *thd_arg); |
| 188 | virtual ~Prepared_statement(); |
| 189 | void setup_set_params(); |
| 190 | virtual Query_arena::Type type() const; |
| 191 | virtual void cleanup_stmt(); |
| 192 | bool set_name(LEX_CSTRING *name); |
| 193 | inline void close_cursor() { delete cursor; cursor= 0; } |
| 194 | inline bool is_in_use() { return flags & (uint) IS_IN_USE; } |
| 195 | inline bool is_sql_prepare() const { return flags & (uint) IS_SQL_PREPARE; } |
| 196 | void set_sql_prepare() { flags|= (uint) IS_SQL_PREPARE; } |
| 197 | bool prepare(const char *packet, uint packet_length); |
| 198 | bool execute_loop(String *expanded_query, |
| 199 | bool open_cursor, |
| 200 | uchar *packet_arg, uchar *packet_end_arg); |
| 201 | bool execute_bulk_loop(String *expanded_query, |
| 202 | bool open_cursor, |
| 203 | uchar *packet_arg, uchar *packet_end_arg); |
| 204 | bool execute_server_runnable(Server_runnable *server_runnable); |
| 205 | my_bool set_bulk_parameters(bool reset); |
| 206 | bool bulk_iterations() { return iterations; }; |
| 207 | /* Destroy this statement */ |
| 208 | void deallocate(); |
| 209 | bool execute_immediate(const char *query, uint query_length); |
| 210 | private: |
| 211 | /** |
| 212 | The memory root to allocate parsed tree elements (instances of Item, |
| 213 | SELECT_LEX and other classes). |
| 214 | */ |
| 215 | MEM_ROOT main_mem_root; |
| 216 | sql_mode_t m_sql_mode; |
| 217 | private: |
| 218 | bool set_db(const LEX_CSTRING *db); |
| 219 | bool set_parameters(String *expanded_query, |
| 220 | uchar *packet, uchar *packet_end); |
| 221 | bool execute(String *expanded_query, bool open_cursor); |
| 222 | void deallocate_immediate(); |
| 223 | bool reprepare(); |
| 224 | bool validate_metadata(Prepared_statement *copy); |
| 225 | void swap_prepared_statement(Prepared_statement *copy); |
| 226 | }; |
| 227 | |
| 228 | /** |
| 229 | Execute one SQL statement in an isolated context. |
| 230 | */ |
| 231 | |
| 232 | class Execute_sql_statement: public Server_runnable |
| 233 | { |
| 234 | public: |
| 235 | Execute_sql_statement(LEX_STRING sql_text); |
| 236 | virtual bool execute_server_code(THD *thd); |
| 237 | private: |
| 238 | LEX_STRING m_sql_text; |
| 239 | }; |
| 240 | |
| 241 | |
| 242 | class Ed_connection; |
| 243 | |
| 244 | /** |
| 245 | Protocol_local: a helper class to intercept the result |
| 246 | of the data written to the network. |
| 247 | */ |
| 248 | |
| 249 | class Protocol_local :public Protocol |
| 250 | { |
| 251 | public: |
| 252 | Protocol_local(THD *thd, Ed_connection *ed_connection); |
| 253 | ~Protocol_local() { free_root(&m_rset_root, MYF(0)); } |
| 254 | protected: |
| 255 | virtual void prepare_for_resend(); |
| 256 | virtual bool write(); |
| 257 | virtual bool store_null(); |
| 258 | virtual bool store_tiny(longlong from); |
| 259 | virtual bool store_short(longlong from); |
| 260 | virtual bool store_long(longlong from); |
| 261 | virtual bool store_longlong(longlong from, bool unsigned_flag); |
| 262 | virtual bool store_decimal(const my_decimal *); |
| 263 | virtual bool store(const char *from, size_t length, CHARSET_INFO *cs); |
| 264 | virtual bool store(const char *from, size_t length, |
| 265 | CHARSET_INFO *fromcs, CHARSET_INFO *tocs); |
| 266 | virtual bool store(MYSQL_TIME *time, int decimals); |
| 267 | virtual bool store_date(MYSQL_TIME *time); |
| 268 | virtual bool store_time(MYSQL_TIME *time, int decimals); |
| 269 | virtual bool store(float value, uint32 decimals, String *buffer); |
| 270 | virtual bool store(double value, uint32 decimals, String *buffer); |
| 271 | virtual bool store(Field *field); |
| 272 | |
| 273 | virtual bool send_result_set_metadata(List<Item> *list, uint flags); |
| 274 | virtual bool send_out_parameters(List<Item_param> *sp_params); |
| 275 | #ifdef EMBEDDED_LIBRARY |
| 276 | void remove_last_row(); |
| 277 | #endif |
| 278 | virtual enum enum_protocol_type type() { return PROTOCOL_LOCAL; }; |
| 279 | |
| 280 | virtual bool send_ok(uint server_status, uint statement_warn_count, |
| 281 | ulonglong affected_rows, ulonglong last_insert_id, |
| 282 | const char *message, bool skip_flush); |
| 283 | |
| 284 | virtual bool send_eof(uint server_status, uint statement_warn_count); |
| 285 | virtual bool send_error(uint sql_errno, const char *err_msg, const char* sqlstate); |
| 286 | private: |
| 287 | bool store_string(const char *str, size_t length, |
| 288 | CHARSET_INFO *src_cs, CHARSET_INFO *dst_cs); |
| 289 | |
| 290 | bool store_column(const void *data, size_t length); |
| 291 | void opt_add_row_to_rset(); |
| 292 | private: |
| 293 | Ed_connection *m_connection; |
| 294 | MEM_ROOT m_rset_root; |
| 295 | List<Ed_row> *m_rset; |
| 296 | size_t m_column_count; |
| 297 | Ed_column *m_current_row; |
| 298 | Ed_column *m_current_column; |
| 299 | }; |
| 300 | |
| 301 | /****************************************************************************** |
| 302 | Implementation |
| 303 | ******************************************************************************/ |
| 304 | |
| 305 | |
| 306 | inline bool is_param_null(const uchar *pos, ulong param_no) |
| 307 | { |
| 308 | return pos[param_no/8] & (1 << (param_no & 7)); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | Find a prepared statement in the statement map by id. |
| 313 | |
| 314 | Try to find a prepared statement and set THD error if it's not found. |
| 315 | |
| 316 | @param thd thread handle |
| 317 | @param id statement id |
| 318 | @param where the place from which this function is called (for |
| 319 | error reporting). |
| 320 | |
| 321 | @return |
| 322 | 0 if the statement was not found, a pointer otherwise. |
| 323 | */ |
| 324 | |
| 325 | static Prepared_statement * |
| 326 | find_prepared_statement(THD *thd, ulong id) |
| 327 | { |
| 328 | /* |
| 329 | To strictly separate namespaces of SQL prepared statements and C API |
| 330 | prepared statements find() will return 0 if there is a named prepared |
| 331 | statement with such id. |
| 332 | |
| 333 | LAST_STMT_ID is special value which mean last prepared statement ID |
| 334 | (it was made for COM_MULTI to allow prepare and execute a statement |
| 335 | in the same command but usage is not limited by COM_MULTI only). |
| 336 | */ |
| 337 | Statement *stmt= ((id == LAST_STMT_ID) ? |
| 338 | thd->last_stmt : |
| 339 | thd->stmt_map.find(id)); |
| 340 | |
| 341 | if (stmt == 0 || stmt->type() != Query_arena::PREPARED_STATEMENT) |
| 342 | return NULL; |
| 343 | |
| 344 | return (Prepared_statement *) stmt; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /** |
| 349 | Send prepared statement id and metadata to the client after prepare. |
| 350 | |
| 351 | @todo |
| 352 | Fix this nasty upcast from List<Item_param> to List<Item> |
| 353 | |
| 354 | @return |
| 355 | 0 in case of success, 1 otherwise |
| 356 | */ |
| 357 | |
| 358 | #ifndef EMBEDDED_LIBRARY |
| 359 | static bool send_prep_stmt(Prepared_statement *stmt, uint columns) |
| 360 | { |
| 361 | NET *net= &stmt->thd->net; |
| 362 | uchar buff[12]; |
| 363 | uint tmp; |
| 364 | int error; |
| 365 | THD *thd= stmt->thd; |
| 366 | DBUG_ENTER("send_prep_stmt" ); |
| 367 | DBUG_PRINT("enter" ,("stmt->id: %lu columns: %d param_count: %d" , |
| 368 | stmt->id, columns, stmt->param_count)); |
| 369 | |
| 370 | buff[0]= 0; /* OK packet indicator */ |
| 371 | int4store(buff+1, stmt->id); |
| 372 | int2store(buff+5, columns); |
| 373 | int2store(buff+7, stmt->param_count); |
| 374 | buff[9]= 0; // Guard against a 4.1 client |
| 375 | tmp= MY_MIN(stmt->thd->get_stmt_da()->current_statement_warn_count(), 65535); |
| 376 | int2store(buff+10, tmp); |
| 377 | |
| 378 | /* |
| 379 | Send types and names of placeholders to the client |
| 380 | XXX: fix this nasty upcast from List<Item_param> to List<Item> |
| 381 | */ |
| 382 | error= my_net_write(net, buff, sizeof(buff)); |
| 383 | if (stmt->param_count && likely(!error)) |
| 384 | { |
| 385 | error= thd->protocol_text.send_result_set_metadata((List<Item> *) |
| 386 | &stmt->lex->param_list, |
| 387 | Protocol::SEND_EOF); |
| 388 | } |
| 389 | |
| 390 | if (likely(!error)) |
| 391 | { |
| 392 | /* Flag that a response has already been sent */ |
| 393 | thd->get_stmt_da()->disable_status(); |
| 394 | } |
| 395 | |
| 396 | DBUG_RETURN(error); |
| 397 | } |
| 398 | #else |
| 399 | static bool send_prep_stmt(Prepared_statement *stmt, |
| 400 | uint columns __attribute__((unused))) |
| 401 | { |
| 402 | THD *thd= stmt->thd; |
| 403 | |
| 404 | thd->client_stmt_id= stmt->id; |
| 405 | thd->client_param_count= stmt->param_count; |
| 406 | thd->clear_error(); |
| 407 | thd->get_stmt_da()->disable_status(); |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | #endif /*!EMBEDDED_LIBRARY*/ |
| 412 | |
| 413 | |
| 414 | #ifndef EMBEDDED_LIBRARY |
| 415 | |
| 416 | /** |
| 417 | Read the length of the parameter data and return it back to |
| 418 | the caller. |
| 419 | |
| 420 | Read data length, position the packet to the first byte after it, |
| 421 | and return the length to the caller. |
| 422 | |
| 423 | @param packet a pointer to the data |
| 424 | @param len remaining packet length |
| 425 | |
| 426 | @return |
| 427 | Length of data piece. |
| 428 | */ |
| 429 | |
| 430 | static ulong get_param_length(uchar **packet, ulong len) |
| 431 | { |
| 432 | uchar *pos= *packet; |
| 433 | if (len < 1) |
| 434 | return 0; |
| 435 | if (*pos < 251) |
| 436 | { |
| 437 | (*packet)++; |
| 438 | return (ulong) *pos; |
| 439 | } |
| 440 | if (len < 3) |
| 441 | return 0; |
| 442 | if (*pos == 252) |
| 443 | { |
| 444 | (*packet)+=3; |
| 445 | return (ulong) uint2korr(pos+1); |
| 446 | } |
| 447 | if (len < 4) |
| 448 | return 0; |
| 449 | if (*pos == 253) |
| 450 | { |
| 451 | (*packet)+=4; |
| 452 | return (ulong) uint3korr(pos+1); |
| 453 | } |
| 454 | if (len < 5) |
| 455 | return 0; |
| 456 | (*packet)+=9; // Must be 254 when here |
| 457 | /* |
| 458 | In our client-server protocol all numbers bigger than 2^24 |
| 459 | stored as 8 bytes with uint8korr. Here we always know that |
| 460 | parameter length is less than 2^4 so don't look at the second |
| 461 | 4 bytes. But still we need to obey the protocol hence 9 in the |
| 462 | assignment above. |
| 463 | */ |
| 464 | return (ulong) uint4korr(pos+1); |
| 465 | } |
| 466 | #else |
| 467 | #define get_param_length(packet, len) len |
| 468 | #endif /*!EMBEDDED_LIBRARY*/ |
| 469 | |
| 470 | /** |
| 471 | Data conversion routines. |
| 472 | |
| 473 | All these functions read the data from pos, convert it to requested |
| 474 | type and assign to param; pos is advanced to predefined length. |
| 475 | |
| 476 | Make a note that the NULL handling is examined at first execution |
| 477 | (i.e. when input types altered) and for all subsequent executions |
| 478 | we don't read any values for this. |
| 479 | |
| 480 | @param pos input data buffer |
| 481 | @param len length of data in the buffer |
| 482 | */ |
| 483 | |
| 484 | void Item_param::set_param_tiny(uchar **pos, ulong len) |
| 485 | { |
| 486 | #ifndef EMBEDDED_LIBRARY |
| 487 | if (len < 1) |
| 488 | return; |
| 489 | #endif |
| 490 | int8 value= (int8) **pos; |
| 491 | set_int(unsigned_flag ? (longlong) ((uint8) value) : |
| 492 | (longlong) value, 4); |
| 493 | *pos+= 1; |
| 494 | } |
| 495 | |
| 496 | void Item_param::set_param_short(uchar **pos, ulong len) |
| 497 | { |
| 498 | int16 value; |
| 499 | #ifndef EMBEDDED_LIBRARY |
| 500 | if (len < 2) |
| 501 | return; |
| 502 | value= sint2korr(*pos); |
| 503 | #else |
| 504 | shortget(value, *pos); |
| 505 | #endif |
| 506 | set_int(unsigned_flag ? (longlong) ((uint16) value) : |
| 507 | (longlong) value, 6); |
| 508 | *pos+= 2; |
| 509 | } |
| 510 | |
| 511 | void Item_param::set_param_int32(uchar **pos, ulong len) |
| 512 | { |
| 513 | int32 value; |
| 514 | #ifndef EMBEDDED_LIBRARY |
| 515 | if (len < 4) |
| 516 | return; |
| 517 | value= sint4korr(*pos); |
| 518 | #else |
| 519 | longget(value, *pos); |
| 520 | #endif |
| 521 | set_int(unsigned_flag ? (longlong) ((uint32) value) : |
| 522 | (longlong) value, 11); |
| 523 | *pos+= 4; |
| 524 | } |
| 525 | |
| 526 | void Item_param::set_param_int64(uchar **pos, ulong len) |
| 527 | { |
| 528 | longlong value; |
| 529 | #ifndef EMBEDDED_LIBRARY |
| 530 | if (len < 8) |
| 531 | return; |
| 532 | value= (longlong) sint8korr(*pos); |
| 533 | #else |
| 534 | longlongget(value, *pos); |
| 535 | #endif |
| 536 | set_int(value, 21); |
| 537 | *pos+= 8; |
| 538 | } |
| 539 | |
| 540 | void Item_param::set_param_float(uchar **pos, ulong len) |
| 541 | { |
| 542 | float data; |
| 543 | #ifndef EMBEDDED_LIBRARY |
| 544 | if (len < 4) |
| 545 | return; |
| 546 | float4get(data,*pos); |
| 547 | #else |
| 548 | floatget(data, *pos); |
| 549 | #endif |
| 550 | set_double((double) data); |
| 551 | *pos+= 4; |
| 552 | } |
| 553 | |
| 554 | void Item_param::set_param_double(uchar **pos, ulong len) |
| 555 | { |
| 556 | double data; |
| 557 | #ifndef EMBEDDED_LIBRARY |
| 558 | if (len < 8) |
| 559 | return; |
| 560 | float8get(data,*pos); |
| 561 | #else |
| 562 | doubleget(data, *pos); |
| 563 | #endif |
| 564 | set_double((double) data); |
| 565 | *pos+= 8; |
| 566 | } |
| 567 | |
| 568 | void Item_param::set_param_decimal(uchar **pos, ulong len) |
| 569 | { |
| 570 | ulong length= get_param_length(pos, len); |
| 571 | set_decimal((char*)*pos, length); |
| 572 | *pos+= length; |
| 573 | } |
| 574 | |
| 575 | #ifndef EMBEDDED_LIBRARY |
| 576 | |
| 577 | /* |
| 578 | Read date/time/datetime parameter values from network (binary |
| 579 | protocol). See writing counterparts of these functions in |
| 580 | libmysql.c (store_param_{time,date,datetime}). |
| 581 | */ |
| 582 | |
| 583 | /** |
| 584 | @todo |
| 585 | Add warning 'Data truncated' here |
| 586 | */ |
| 587 | void Item_param::set_param_time(uchar **pos, ulong len) |
| 588 | { |
| 589 | MYSQL_TIME tm; |
| 590 | ulong length= get_param_length(pos, len); |
| 591 | |
| 592 | if (length >= 8) |
| 593 | { |
| 594 | uchar *to= *pos; |
| 595 | uint day; |
| 596 | |
| 597 | tm.neg= (bool) to[0]; |
| 598 | day= (uint) sint4korr(to+1); |
| 599 | tm.hour= (uint) to[5] + day * 24; |
| 600 | tm.minute= (uint) to[6]; |
| 601 | tm.second= (uint) to[7]; |
| 602 | tm.second_part= (length > 8) ? (ulong) sint4korr(to+8) : 0; |
| 603 | if (tm.hour > 838) |
| 604 | { |
| 605 | /* TODO: add warning 'Data truncated' here */ |
| 606 | tm.hour= 838; |
| 607 | tm.minute= 59; |
| 608 | tm.second= 59; |
| 609 | } |
| 610 | tm.day= tm.year= tm.month= 0; |
| 611 | } |
| 612 | else |
| 613 | set_zero_time(&tm, MYSQL_TIMESTAMP_TIME); |
| 614 | set_time(&tm, MYSQL_TIMESTAMP_TIME, MAX_TIME_FULL_WIDTH); |
| 615 | *pos+= length; |
| 616 | } |
| 617 | |
| 618 | void Item_param::set_param_datetime(uchar **pos, ulong len) |
| 619 | { |
| 620 | MYSQL_TIME tm; |
| 621 | ulong length= get_param_length(pos, len); |
| 622 | |
| 623 | if (length >= 4) |
| 624 | { |
| 625 | uchar *to= *pos; |
| 626 | |
| 627 | tm.neg= 0; |
| 628 | tm.year= (uint) sint2korr(to); |
| 629 | tm.month= (uint) to[2]; |
| 630 | tm.day= (uint) to[3]; |
| 631 | if (length > 4) |
| 632 | { |
| 633 | tm.hour= (uint) to[4]; |
| 634 | tm.minute= (uint) to[5]; |
| 635 | tm.second= (uint) to[6]; |
| 636 | } |
| 637 | else |
| 638 | tm.hour= tm.minute= tm.second= 0; |
| 639 | |
| 640 | tm.second_part= (length > 7) ? (ulong) sint4korr(to+7) : 0; |
| 641 | } |
| 642 | else |
| 643 | set_zero_time(&tm, MYSQL_TIMESTAMP_DATETIME); |
| 644 | set_time(&tm, MYSQL_TIMESTAMP_DATETIME, MAX_DATETIME_WIDTH); |
| 645 | *pos+= length; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | void Item_param::set_param_date(uchar **pos, ulong len) |
| 650 | { |
| 651 | MYSQL_TIME tm; |
| 652 | ulong length= get_param_length(pos, len); |
| 653 | |
| 654 | if (length >= 4) |
| 655 | { |
| 656 | uchar *to= *pos; |
| 657 | |
| 658 | tm.year= (uint) sint2korr(to); |
| 659 | tm.month= (uint) to[2]; |
| 660 | tm.day= (uint) to[3]; |
| 661 | |
| 662 | tm.hour= tm.minute= tm.second= 0; |
| 663 | tm.second_part= 0; |
| 664 | tm.neg= 0; |
| 665 | } |
| 666 | else |
| 667 | set_zero_time(&tm, MYSQL_TIMESTAMP_DATE); |
| 668 | set_time(&tm, MYSQL_TIMESTAMP_DATE, MAX_DATE_WIDTH); |
| 669 | *pos+= length; |
| 670 | } |
| 671 | |
| 672 | #else/*!EMBEDDED_LIBRARY*/ |
| 673 | /** |
| 674 | @todo |
| 675 | Add warning 'Data truncated' here |
| 676 | */ |
| 677 | void Item_param::set_param_time(uchar **pos, ulong len) |
| 678 | { |
| 679 | MYSQL_TIME tm= *((MYSQL_TIME*)*pos); |
| 680 | tm.hour+= tm.day * 24; |
| 681 | tm.day= tm.year= tm.month= 0; |
| 682 | if (tm.hour > 838) |
| 683 | { |
| 684 | /* TODO: add warning 'Data truncated' here */ |
| 685 | tm.hour= 838; |
| 686 | tm.minute= 59; |
| 687 | tm.second= 59; |
| 688 | } |
| 689 | set_time(&tm, MYSQL_TIMESTAMP_TIME, MAX_TIME_WIDTH); |
| 690 | } |
| 691 | |
| 692 | void Item_param::set_param_datetime(uchar **pos, ulong len) |
| 693 | { |
| 694 | MYSQL_TIME tm= *((MYSQL_TIME*)*pos); |
| 695 | tm.neg= 0; |
| 696 | set_time(&tm, MYSQL_TIMESTAMP_DATETIME, MAX_DATETIME_WIDTH); |
| 697 | } |
| 698 | |
| 699 | void Item_param::set_param_date(uchar **pos, ulong len) |
| 700 | { |
| 701 | MYSQL_TIME *to= (MYSQL_TIME*)*pos; |
| 702 | set_time(to, MYSQL_TIMESTAMP_DATE, MAX_DATE_WIDTH); |
| 703 | } |
| 704 | #endif /*!EMBEDDED_LIBRARY*/ |
| 705 | |
| 706 | |
| 707 | void Item_param::set_param_str(uchar **pos, ulong len) |
| 708 | { |
| 709 | ulong length= get_param_length(pos, len); |
| 710 | if (length == 0 && m_empty_string_is_null) |
| 711 | set_null(); |
| 712 | else |
| 713 | { |
| 714 | if (length > len) |
| 715 | length= len; |
| 716 | /* |
| 717 | We use &my_charset_bin here. Conversion and setting real character |
| 718 | sets will be done in Item_param::convert_str_value(), after the |
| 719 | original value is appended to the query used for logging. |
| 720 | */ |
| 721 | set_str((const char *) *pos, length, &my_charset_bin, &my_charset_bin); |
| 722 | *pos+= length; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | |
| 727 | #undef get_param_length |
| 728 | |
| 729 | |
| 730 | void Item_param::setup_conversion(THD *thd, uchar param_type) |
| 731 | { |
| 732 | const Type_handler *h= |
| 733 | Type_handler::get_handler_by_field_type((enum_field_types) param_type); |
| 734 | /* |
| 735 | The client library ensures that we won't get any unexpected typecodes |
| 736 | in the bound parameter. Translating unknown typecodes to |
| 737 | &type_handler_string lets us to handle malformed packets as well. |
| 738 | */ |
| 739 | if (!h) |
| 740 | h= &type_handler_string; |
| 741 | set_handler(h); |
| 742 | h->Item_param_setup_conversion(thd, this); |
| 743 | } |
| 744 | |
| 745 | |
| 746 | void Item_param::setup_conversion_blob(THD *thd) |
| 747 | { |
| 748 | value.cs_info.character_set_of_placeholder= &my_charset_bin; |
| 749 | value.cs_info.character_set_client= thd->variables.character_set_client; |
| 750 | DBUG_ASSERT(thd->variables.character_set_client); |
| 751 | value.cs_info.final_character_set_of_str_value= &my_charset_bin; |
| 752 | m_empty_string_is_null= thd->variables.sql_mode & MODE_EMPTY_STRING_IS_NULL; |
| 753 | } |
| 754 | |
| 755 | |
| 756 | void Item_param::setup_conversion_string(THD *thd, CHARSET_INFO *fromcs) |
| 757 | { |
| 758 | value.cs_info.set(thd, fromcs); |
| 759 | m_empty_string_is_null= thd->variables.sql_mode & MODE_EMPTY_STRING_IS_NULL; |
| 760 | /* |
| 761 | Exact value of max_length is not known unless data is converted to |
| 762 | charset of connection, so we have to set it later. |
| 763 | */ |
| 764 | } |
| 765 | |
| 766 | #ifndef EMBEDDED_LIBRARY |
| 767 | |
| 768 | /** |
| 769 | Routines to assign parameters from data supplied by the client. |
| 770 | |
| 771 | Update the parameter markers by reading data from the packet and |
| 772 | and generate a valid query for logging. |
| 773 | |
| 774 | @note |
| 775 | This function, along with other _with_log functions is called when one of |
| 776 | binary, slow or general logs is open. Logging of prepared statements in |
| 777 | all cases is performed by means of conventional queries: if parameter |
| 778 | data was supplied from C API, each placeholder in the query is |
| 779 | replaced with its actual value; if we're logging a [Dynamic] SQL |
| 780 | prepared statement, parameter markers are replaced with variable names. |
| 781 | Example: |
| 782 | @verbatim |
| 783 | mysqld_stmt_prepare("UPDATE t1 SET a=a*1.25 WHERE a=?") |
| 784 | --> general logs gets [Prepare] UPDATE t1 SET a*1.25 WHERE a=?" |
| 785 | mysqld_stmt_execute(stmt); |
| 786 | --> general and binary logs get |
| 787 | [Execute] UPDATE t1 SET a*1.25 WHERE a=1" |
| 788 | @endverbatim |
| 789 | |
| 790 | If a statement has been prepared using SQL syntax: |
| 791 | @verbatim |
| 792 | PREPARE stmt FROM "UPDATE t1 SET a=a*1.25 WHERE a=?" |
| 793 | --> general log gets |
| 794 | [Query] PREPARE stmt FROM "UPDATE ..." |
| 795 | EXECUTE stmt USING @a |
| 796 | --> general log gets |
| 797 | [Query] EXECUTE stmt USING @a; |
| 798 | @endverbatim |
| 799 | |
| 800 | @retval |
| 801 | 0 if success |
| 802 | @retval |
| 803 | 1 otherwise |
| 804 | */ |
| 805 | |
| 806 | static bool insert_params_with_log(Prepared_statement *stmt, uchar *null_array, |
| 807 | uchar *read_pos, uchar *data_end, |
| 808 | String *query) |
| 809 | { |
| 810 | THD *thd= stmt->thd; |
| 811 | Item_param **begin= stmt->param_array; |
| 812 | Item_param **end= begin + stmt->param_count; |
| 813 | Copy_query_with_rewrite acc(thd, stmt->query(), stmt->query_length(), query); |
| 814 | DBUG_ENTER("insert_params_with_log" ); |
| 815 | |
| 816 | for (Item_param **it= begin; it < end; ++it) |
| 817 | { |
| 818 | Item_param *param= *it; |
| 819 | if (!param->has_long_data_value()) |
| 820 | { |
| 821 | if (is_param_null(null_array, (uint) (it - begin))) |
| 822 | param->set_null(); |
| 823 | else |
| 824 | { |
| 825 | if (read_pos >= data_end) |
| 826 | DBUG_RETURN(1); |
| 827 | param->set_param_func(&read_pos, (uint) (data_end - read_pos)); |
| 828 | if (param->has_no_value()) |
| 829 | DBUG_RETURN(1); |
| 830 | |
| 831 | if (param->limit_clause_param && !param->has_int_value()) |
| 832 | { |
| 833 | if (param->set_limit_clause_param(param->val_int())) |
| 834 | DBUG_RETURN(1); |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | /* |
| 839 | A long data stream was supplied for this parameter marker. |
| 840 | This was done after prepare, prior to providing a placeholder |
| 841 | type (the types are supplied at execute). Check that the |
| 842 | supplied type of placeholder can accept a data stream. |
| 843 | */ |
| 844 | else if (!param->type_handler()->is_param_long_data_type()) |
| 845 | DBUG_RETURN(1); |
| 846 | |
| 847 | if (acc.append(param)) |
| 848 | DBUG_RETURN(1); |
| 849 | |
| 850 | if (param->convert_str_value(thd)) |
| 851 | DBUG_RETURN(1); /* out of memory */ |
| 852 | } |
| 853 | if (acc.finalize()) |
| 854 | DBUG_RETURN(1); |
| 855 | |
| 856 | DBUG_RETURN(0); |
| 857 | } |
| 858 | |
| 859 | |
| 860 | static bool insert_params(Prepared_statement *stmt, uchar *null_array, |
| 861 | uchar *read_pos, uchar *data_end, |
| 862 | String *expanded_query) |
| 863 | { |
| 864 | Item_param **begin= stmt->param_array; |
| 865 | Item_param **end= begin + stmt->param_count; |
| 866 | |
| 867 | DBUG_ENTER("insert_params" ); |
| 868 | |
| 869 | for (Item_param **it= begin; it < end; ++it) |
| 870 | { |
| 871 | Item_param *param= *it; |
| 872 | param->indicator= STMT_INDICATOR_NONE; // only for bulk parameters |
| 873 | if (!param->has_long_data_value()) |
| 874 | { |
| 875 | if (is_param_null(null_array, (uint) (it - begin))) |
| 876 | param->set_null(); |
| 877 | else |
| 878 | { |
| 879 | if (read_pos >= data_end) |
| 880 | DBUG_RETURN(1); |
| 881 | param->set_param_func(&read_pos, (uint) (data_end - read_pos)); |
| 882 | if (param->has_no_value()) |
| 883 | DBUG_RETURN(1); |
| 884 | } |
| 885 | } |
| 886 | /* |
| 887 | A long data stream was supplied for this parameter marker. |
| 888 | This was done after prepare, prior to providing a placeholder |
| 889 | type (the types are supplied at execute). Check that the |
| 890 | supplied type of placeholder can accept a data stream. |
| 891 | */ |
| 892 | else if (!param->type_handler()->is_param_long_data_type()) |
| 893 | DBUG_RETURN(1); |
| 894 | if (param->convert_str_value(stmt->thd)) |
| 895 | DBUG_RETURN(1); /* out of memory */ |
| 896 | } |
| 897 | DBUG_RETURN(0); |
| 898 | } |
| 899 | |
| 900 | |
| 901 | static bool insert_bulk_params(Prepared_statement *stmt, |
| 902 | uchar **read_pos, uchar *data_end, |
| 903 | bool reset) |
| 904 | { |
| 905 | Item_param **begin= stmt->param_array; |
| 906 | Item_param **end= begin + stmt->param_count; |
| 907 | |
| 908 | DBUG_ENTER("insert_params" ); |
| 909 | |
| 910 | for (Item_param **it= begin; it < end; ++it) |
| 911 | { |
| 912 | Item_param *param= *it; |
| 913 | if (reset) |
| 914 | param->reset(); |
| 915 | if (!param->has_long_data_value()) |
| 916 | { |
| 917 | param->indicator= (enum_indicator_type) *((*read_pos)++); |
| 918 | if ((*read_pos) > data_end) |
| 919 | DBUG_RETURN(1); |
| 920 | switch (param->indicator) |
| 921 | { |
| 922 | case STMT_INDICATOR_NONE: |
| 923 | if ((*read_pos) >= data_end) |
| 924 | DBUG_RETURN(1); |
| 925 | param->set_param_func(read_pos, (uint) (data_end - (*read_pos))); |
| 926 | if (param->has_no_value()) |
| 927 | DBUG_RETURN(1); |
| 928 | if (param->convert_str_value(stmt->thd)) |
| 929 | DBUG_RETURN(1); /* out of memory */ |
| 930 | break; |
| 931 | case STMT_INDICATOR_NULL: |
| 932 | param->set_null(); |
| 933 | break; |
| 934 | case STMT_INDICATOR_DEFAULT: |
| 935 | param->set_default(); |
| 936 | break; |
| 937 | case STMT_INDICATOR_IGNORE: |
| 938 | param->set_ignore(); |
| 939 | break; |
| 940 | } |
| 941 | } |
| 942 | else |
| 943 | DBUG_RETURN(1); // long is not supported here |
| 944 | } |
| 945 | DBUG_RETURN(0); |
| 946 | } |
| 947 | |
| 948 | static bool set_conversion_functions(Prepared_statement *stmt, |
| 949 | uchar **data, uchar *data_end) |
| 950 | { |
| 951 | uchar *read_pos= *data; |
| 952 | const uint signed_bit= 1 << 15; |
| 953 | DBUG_ENTER("set_conversion_functions" ); |
| 954 | /* |
| 955 | First execute or types altered by the client, setup the |
| 956 | conversion routines for all parameters (one time) |
| 957 | */ |
| 958 | Item_param **it= stmt->param_array; |
| 959 | Item_param **end= it + stmt->param_count; |
| 960 | THD *thd= stmt->thd; |
| 961 | for (; it < end; ++it) |
| 962 | { |
| 963 | ushort typecode; |
| 964 | |
| 965 | if (read_pos >= data_end) |
| 966 | DBUG_RETURN(1); |
| 967 | |
| 968 | typecode= sint2korr(read_pos); |
| 969 | read_pos+= 2; |
| 970 | (**it).unsigned_flag= MY_TEST(typecode & signed_bit); |
| 971 | (*it)->setup_conversion(thd, (uchar) (typecode & 0xff)); |
| 972 | } |
| 973 | *data= read_pos; |
| 974 | DBUG_RETURN(0); |
| 975 | } |
| 976 | |
| 977 | |
| 978 | static bool setup_conversion_functions(Prepared_statement *stmt, |
| 979 | uchar **data, uchar *data_end, |
| 980 | bool bulk_protocol= 0) |
| 981 | { |
| 982 | /* skip null bits */ |
| 983 | uchar *read_pos= *data; |
| 984 | if (!bulk_protocol) |
| 985 | read_pos+= (stmt->param_count+7) / 8; |
| 986 | |
| 987 | DBUG_ENTER("setup_conversion_functions" ); |
| 988 | |
| 989 | if (*read_pos++) //types supplied / first execute |
| 990 | { |
| 991 | *data= read_pos; |
| 992 | bool res= set_conversion_functions(stmt, data, data_end); |
| 993 | DBUG_RETURN(res); |
| 994 | } |
| 995 | *data= read_pos; |
| 996 | DBUG_RETURN(0); |
| 997 | } |
| 998 | |
| 999 | #else |
| 1000 | |
| 1001 | //TODO: support bulk parameters |
| 1002 | |
| 1003 | /** |
| 1004 | Embedded counterparts of parameter assignment routines. |
| 1005 | |
| 1006 | The main difference between the embedded library and the server is |
| 1007 | that in embedded case we don't serialize/deserialize parameters data. |
| 1008 | |
| 1009 | Additionally, for unknown reason, the client-side flag raised for |
| 1010 | changed types of placeholders is ignored and we simply setup conversion |
| 1011 | functions at each execute (TODO: fix). |
| 1012 | */ |
| 1013 | |
| 1014 | static bool emb_insert_params(Prepared_statement *stmt, String *expanded_query) |
| 1015 | { |
| 1016 | THD *thd= stmt->thd; |
| 1017 | Item_param **it= stmt->param_array; |
| 1018 | Item_param **end= it + stmt->param_count; |
| 1019 | MYSQL_BIND *client_param= stmt->thd->client_params; |
| 1020 | |
| 1021 | DBUG_ENTER("emb_insert_params" ); |
| 1022 | |
| 1023 | for (; it < end; ++it, ++client_param) |
| 1024 | { |
| 1025 | Item_param *param= *it; |
| 1026 | param->setup_conversion(thd, client_param->buffer_type); |
| 1027 | if (!param->has_long_data_value()) |
| 1028 | { |
| 1029 | if (*client_param->is_null) |
| 1030 | param->set_null(); |
| 1031 | else |
| 1032 | { |
| 1033 | uchar *buff= (uchar*) client_param->buffer; |
| 1034 | param->unsigned_flag= client_param->is_unsigned; |
| 1035 | param->set_param_func(&buff, |
| 1036 | client_param->length ? |
| 1037 | *client_param->length : |
| 1038 | client_param->buffer_length); |
| 1039 | if (param->has_no_value()) |
| 1040 | DBUG_RETURN(1); |
| 1041 | } |
| 1042 | } |
| 1043 | if (param->convert_str_value(thd)) |
| 1044 | DBUG_RETURN(1); /* out of memory */ |
| 1045 | } |
| 1046 | DBUG_RETURN(0); |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | static bool emb_insert_params_with_log(Prepared_statement *stmt, String *query) |
| 1051 | { |
| 1052 | THD *thd= stmt->thd; |
| 1053 | Item_param **it= stmt->param_array; |
| 1054 | Item_param **end= it + stmt->param_count; |
| 1055 | MYSQL_BIND *client_param= thd->client_params; |
| 1056 | Copy_query_with_rewrite acc(thd, stmt->query(), stmt->query_length(), query); |
| 1057 | DBUG_ENTER("emb_insert_params_with_log" ); |
| 1058 | |
| 1059 | for (; it < end; ++it, ++client_param) |
| 1060 | { |
| 1061 | Item_param *param= *it; |
| 1062 | param->setup_conversion(thd, client_param->buffer_type); |
| 1063 | if (!param->has_long_data_value()) |
| 1064 | { |
| 1065 | if (*client_param->is_null) |
| 1066 | param->set_null(); |
| 1067 | else |
| 1068 | { |
| 1069 | uchar *buff= (uchar*)client_param->buffer; |
| 1070 | param->unsigned_flag= client_param->is_unsigned; |
| 1071 | param->set_param_func(&buff, |
| 1072 | client_param->length ? |
| 1073 | *client_param->length : |
| 1074 | client_param->buffer_length); |
| 1075 | if (param->has_no_value()) |
| 1076 | DBUG_RETURN(1); |
| 1077 | } |
| 1078 | } |
| 1079 | if (acc.append(param)) |
| 1080 | DBUG_RETURN(1); |
| 1081 | |
| 1082 | if (param->convert_str_value(thd)) |
| 1083 | DBUG_RETURN(1); /* out of memory */ |
| 1084 | } |
| 1085 | if (acc.finalize()) |
| 1086 | DBUG_RETURN(1); |
| 1087 | |
| 1088 | DBUG_RETURN(0); |
| 1089 | } |
| 1090 | |
| 1091 | #endif /*!EMBEDDED_LIBRARY*/ |
| 1092 | |
| 1093 | /** |
| 1094 | Setup data conversion routines using an array of parameter |
| 1095 | markers from the original prepared statement. |
| 1096 | Swap the parameter data of the original prepared |
| 1097 | statement to the new one. |
| 1098 | |
| 1099 | Used only when we re-prepare a prepared statement. |
| 1100 | There are two reasons for this function to exist: |
| 1101 | |
| 1102 | 1) In the binary client/server protocol, parameter metadata |
| 1103 | is sent only at first execute. Consequently, if we need to |
| 1104 | reprepare a prepared statement at a subsequent execution, |
| 1105 | we may not have metadata information in the packet. |
| 1106 | In that case we use the parameter array of the original |
| 1107 | prepared statement to setup parameter types of the new |
| 1108 | prepared statement. |
| 1109 | |
| 1110 | 2) In the binary client/server protocol, we may supply |
| 1111 | long data in pieces. When the last piece is supplied, |
| 1112 | we assemble the pieces and convert them from client |
| 1113 | character set to the connection character set. After |
| 1114 | that the parameter value is only available inside |
| 1115 | the parameter, the original pieces are lost, and thus |
| 1116 | we can only assign the corresponding parameter of the |
| 1117 | reprepared statement from the original value. |
| 1118 | |
| 1119 | @param[out] param_array_dst parameter markers of the new statement |
| 1120 | @param[in] param_array_src parameter markers of the original |
| 1121 | statement |
| 1122 | @param[in] param_count total number of parameters. Is the |
| 1123 | same in src and dst arrays, since |
| 1124 | the statement query is the same |
| 1125 | |
| 1126 | @return this function never fails |
| 1127 | */ |
| 1128 | |
| 1129 | static void |
| 1130 | swap_parameter_array(Item_param **param_array_dst, |
| 1131 | Item_param **param_array_src, |
| 1132 | uint param_count) |
| 1133 | { |
| 1134 | Item_param **dst= param_array_dst; |
| 1135 | Item_param **src= param_array_src; |
| 1136 | Item_param **end= param_array_dst + param_count; |
| 1137 | |
| 1138 | for (; dst < end; ++src, ++dst) |
| 1139 | (*dst)->set_param_type_and_swap_value(*src); |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | /** |
| 1144 | Assign prepared statement parameters from user variables. |
| 1145 | |
| 1146 | @param stmt Statement |
| 1147 | @param params A list of parameters. Caller must ensure that number |
| 1148 | of parameters in the list is equal to number of statement |
| 1149 | parameters |
| 1150 | @param query Ignored |
| 1151 | */ |
| 1152 | |
| 1153 | static bool |
| 1154 | insert_params_from_actual_params(Prepared_statement *stmt, |
| 1155 | List<Item> ¶ms, |
| 1156 | String *query __attribute__((unused))) |
| 1157 | { |
| 1158 | Item_param **begin= stmt->param_array; |
| 1159 | Item_param **end= begin + stmt->param_count; |
| 1160 | List_iterator<Item> param_it(params); |
| 1161 | DBUG_ENTER("insert_params_from_actual_params" ); |
| 1162 | |
| 1163 | for (Item_param **it= begin; it < end; ++it) |
| 1164 | { |
| 1165 | Item_param *param= *it; |
| 1166 | Item *ps_param= param_it++; |
| 1167 | if (ps_param->save_in_param(stmt->thd, param) || |
| 1168 | param->convert_str_value(stmt->thd)) |
| 1169 | DBUG_RETURN(1); |
| 1170 | } |
| 1171 | DBUG_RETURN(0); |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | /** |
| 1176 | Do the same as insert_params_from_actual_params |
| 1177 | but also construct query text for binary log. |
| 1178 | |
| 1179 | @param stmt Prepared statement |
| 1180 | @param params A list of parameters. Caller must ensure that number of |
| 1181 | parameters in the list is equal to number of statement |
| 1182 | parameters |
| 1183 | @param query The query with parameter markers replaced with corresponding |
| 1184 | user variables that were used to execute the query. |
| 1185 | */ |
| 1186 | |
| 1187 | static bool |
| 1188 | insert_params_from_actual_params_with_log(Prepared_statement *stmt, |
| 1189 | List<Item> ¶ms, |
| 1190 | String *query) |
| 1191 | { |
| 1192 | Item_param **begin= stmt->param_array; |
| 1193 | Item_param **end= begin + stmt->param_count; |
| 1194 | List_iterator<Item> param_it(params); |
| 1195 | THD *thd= stmt->thd; |
| 1196 | Copy_query_with_rewrite acc(thd, stmt->query(), stmt->query_length(), query); |
| 1197 | |
| 1198 | DBUG_ENTER("insert_params_from_actual_params_with_log" ); |
| 1199 | |
| 1200 | for (Item_param **it= begin; it < end; ++it) |
| 1201 | { |
| 1202 | Item_param *param= *it; |
| 1203 | Item *ps_param= param_it++; |
| 1204 | if (ps_param->save_in_param(thd, param)) |
| 1205 | DBUG_RETURN(1); |
| 1206 | |
| 1207 | if (acc.append(param)) |
| 1208 | DBUG_RETURN(1); |
| 1209 | |
| 1210 | if (param->convert_str_value(thd)) |
| 1211 | DBUG_RETURN(1); |
| 1212 | } |
| 1213 | if (acc.finalize()) |
| 1214 | DBUG_RETURN(1); |
| 1215 | |
| 1216 | DBUG_RETURN(0); |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | Validate INSERT statement. |
| 1221 | |
| 1222 | @param stmt prepared statement |
| 1223 | @param tables global/local table list |
| 1224 | |
| 1225 | @retval |
| 1226 | FALSE success |
| 1227 | @retval |
| 1228 | TRUE error, error message is set in THD |
| 1229 | */ |
| 1230 | |
| 1231 | static bool mysql_test_insert(Prepared_statement *stmt, |
| 1232 | TABLE_LIST *table_list, |
| 1233 | List<Item> &fields, |
| 1234 | List<List_item> &values_list, |
| 1235 | List<Item> &update_fields, |
| 1236 | List<Item> &update_values, |
| 1237 | enum_duplicates duplic) |
| 1238 | { |
| 1239 | THD *thd= stmt->thd; |
| 1240 | List_iterator_fast<List_item> its(values_list); |
| 1241 | List_item *values; |
| 1242 | DBUG_ENTER("mysql_test_insert" ); |
| 1243 | |
| 1244 | /* |
| 1245 | Since INSERT DELAYED doesn't support temporary tables, we could |
| 1246 | not pre-open temporary tables for SQLCOM_INSERT / SQLCOM_REPLACE. |
| 1247 | Open them here instead. |
| 1248 | */ |
| 1249 | if (table_list->lock_type != TL_WRITE_DELAYED) |
| 1250 | { |
| 1251 | if (thd->open_temporary_tables(table_list)) |
| 1252 | goto error; |
| 1253 | } |
| 1254 | |
| 1255 | if (insert_precheck(thd, table_list)) |
| 1256 | goto error; |
| 1257 | |
| 1258 | //upgrade_lock_type_for_insert(thd, &table_list->lock_type, duplic, |
| 1259 | // values_list.elements > 1); |
| 1260 | /* |
| 1261 | open temporary memory pool for temporary data allocated by derived |
| 1262 | tables & preparation procedure |
| 1263 | Note that this is done without locks (should not be needed as we will not |
| 1264 | access any data here) |
| 1265 | If we would use locks, then we have to ensure we are not using |
| 1266 | TL_WRITE_DELAYED as having two such locks can cause table corruption. |
| 1267 | */ |
| 1268 | if (open_normal_and_derived_tables(thd, table_list, |
| 1269 | MYSQL_OPEN_FORCE_SHARED_MDL, DT_INIT)) |
| 1270 | goto error; |
| 1271 | |
| 1272 | if ((values= its++)) |
| 1273 | { |
| 1274 | uint value_count; |
| 1275 | ulong counter= 0; |
| 1276 | Item *unused_conds= 0; |
| 1277 | |
| 1278 | if (table_list->table) |
| 1279 | { |
| 1280 | // don't allocate insert_values |
| 1281 | table_list->table->insert_values=(uchar *)1; |
| 1282 | } |
| 1283 | |
| 1284 | if (mysql_prepare_insert(thd, table_list, table_list->table, |
| 1285 | fields, values, update_fields, update_values, |
| 1286 | duplic, &unused_conds, FALSE)) |
| 1287 | goto error; |
| 1288 | |
| 1289 | value_count= values->elements; |
| 1290 | its.rewind(); |
| 1291 | |
| 1292 | if (table_list->lock_type == TL_WRITE_DELAYED && |
| 1293 | !(table_list->table->file->ha_table_flags() & HA_CAN_INSERT_DELAYED)) |
| 1294 | { |
| 1295 | my_error(ER_DELAYED_NOT_SUPPORTED, MYF(0), (table_list->view ? |
| 1296 | table_list->view_name.str : |
| 1297 | table_list->table_name.str)); |
| 1298 | goto error; |
| 1299 | } |
| 1300 | while ((values= its++)) |
| 1301 | { |
| 1302 | counter++; |
| 1303 | if (values->elements != value_count) |
| 1304 | { |
| 1305 | my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter); |
| 1306 | goto error; |
| 1307 | } |
| 1308 | if (setup_fields(thd, Ref_ptr_array(), |
| 1309 | *values, COLUMNS_READ, 0, NULL, 0)) |
| 1310 | goto error; |
| 1311 | } |
| 1312 | } |
| 1313 | DBUG_RETURN(FALSE); |
| 1314 | |
| 1315 | error: |
| 1316 | /* insert_values is cleared in open_table */ |
| 1317 | DBUG_RETURN(TRUE); |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | /** |
| 1322 | Validate UPDATE statement. |
| 1323 | |
| 1324 | @param stmt prepared statement |
| 1325 | @param tables list of tables used in this query |
| 1326 | |
| 1327 | @todo |
| 1328 | - here we should send types of placeholders to the client. |
| 1329 | |
| 1330 | @retval |
| 1331 | 0 success |
| 1332 | @retval |
| 1333 | 1 error, error message is set in THD |
| 1334 | @retval |
| 1335 | 2 convert to multi_update |
| 1336 | */ |
| 1337 | |
| 1338 | static int mysql_test_update(Prepared_statement *stmt, |
| 1339 | TABLE_LIST *table_list) |
| 1340 | { |
| 1341 | int res; |
| 1342 | THD *thd= stmt->thd; |
| 1343 | uint table_count= 0; |
| 1344 | TABLE_LIST *update_source_table; |
| 1345 | SELECT_LEX *select= &stmt->lex->select_lex; |
| 1346 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1347 | uint want_privilege; |
| 1348 | #endif |
| 1349 | DBUG_ENTER("mysql_test_update" ); |
| 1350 | |
| 1351 | if (update_precheck(thd, table_list) || |
| 1352 | open_tables(thd, &table_list, &table_count, MYSQL_OPEN_FORCE_SHARED_MDL)) |
| 1353 | goto error; |
| 1354 | |
| 1355 | if (mysql_handle_derived(thd->lex, DT_INIT)) |
| 1356 | goto error; |
| 1357 | |
| 1358 | if (((update_source_table= unique_table(thd, table_list, |
| 1359 | table_list->next_global, 0)) || |
| 1360 | table_list->is_multitable())) |
| 1361 | { |
| 1362 | DBUG_ASSERT(update_source_table || table_list->view != 0); |
| 1363 | DBUG_PRINT("info" , ("Switch to multi-update" )); |
| 1364 | /* pass counter value */ |
| 1365 | thd->lex->table_count= table_count; |
| 1366 | /* convert to multiupdate */ |
| 1367 | DBUG_RETURN(2); |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | thd->fill_derived_tables() is false here for sure (because it is |
| 1372 | preparation of PS, so we even do not check it). |
| 1373 | */ |
| 1374 | if (table_list->handle_derived(thd->lex, DT_MERGE_FOR_INSERT)) |
| 1375 | goto error; |
| 1376 | if (table_list->handle_derived(thd->lex, DT_PREPARE)) |
| 1377 | goto error; |
| 1378 | |
| 1379 | if (!table_list->single_table_updatable()) |
| 1380 | { |
| 1381 | my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "UPDATE" ); |
| 1382 | goto error; |
| 1383 | } |
| 1384 | |
| 1385 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1386 | /* Force privilege re-checking for views after they have been opened. */ |
| 1387 | want_privilege= (table_list->view ? UPDATE_ACL : |
| 1388 | table_list->grant.want_privilege); |
| 1389 | #endif |
| 1390 | |
| 1391 | if (mysql_prepare_update(thd, table_list, &select->where, |
| 1392 | select->order_list.elements, |
| 1393 | select->order_list.first)) |
| 1394 | goto error; |
| 1395 | |
| 1396 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1397 | table_list->grant.want_privilege= want_privilege; |
| 1398 | table_list->table->grant.want_privilege= want_privilege; |
| 1399 | table_list->register_want_access(want_privilege); |
| 1400 | #endif |
| 1401 | thd->lex->select_lex.no_wrap_view_item= TRUE; |
| 1402 | res= setup_fields(thd, Ref_ptr_array(), |
| 1403 | select->item_list, MARK_COLUMNS_READ, 0, NULL, 0); |
| 1404 | thd->lex->select_lex.no_wrap_view_item= FALSE; |
| 1405 | if (res) |
| 1406 | goto error; |
| 1407 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1408 | /* Check values */ |
| 1409 | table_list->grant.want_privilege= |
| 1410 | table_list->table->grant.want_privilege= |
| 1411 | (SELECT_ACL & ~table_list->table->grant.privilege); |
| 1412 | table_list->register_want_access(SELECT_ACL); |
| 1413 | #endif |
| 1414 | if (setup_fields(thd, Ref_ptr_array(), |
| 1415 | stmt->lex->value_list, COLUMNS_READ, 0, NULL, 0) || |
| 1416 | check_unique_table(thd, table_list)) |
| 1417 | goto error; |
| 1418 | /* TODO: here we should send types of placeholders to the client. */ |
| 1419 | DBUG_RETURN(0); |
| 1420 | error: |
| 1421 | DBUG_RETURN(1); |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | /** |
| 1426 | Validate DELETE statement. |
| 1427 | |
| 1428 | @param stmt prepared statement |
| 1429 | @param tables list of tables used in this query |
| 1430 | |
| 1431 | @retval |
| 1432 | FALSE success |
| 1433 | @retval |
| 1434 | TRUE error, error message is set in THD |
| 1435 | */ |
| 1436 | |
| 1437 | static bool mysql_test_delete(Prepared_statement *stmt, |
| 1438 | TABLE_LIST *table_list) |
| 1439 | { |
| 1440 | uint table_count= 0; |
| 1441 | THD *thd= stmt->thd; |
| 1442 | LEX *lex= stmt->lex; |
| 1443 | bool delete_while_scanning; |
| 1444 | DBUG_ENTER("mysql_test_delete" ); |
| 1445 | |
| 1446 | if (delete_precheck(thd, table_list) || |
| 1447 | open_tables(thd, &table_list, &table_count, MYSQL_OPEN_FORCE_SHARED_MDL)) |
| 1448 | goto error; |
| 1449 | |
| 1450 | if (mysql_handle_derived(thd->lex, DT_INIT)) |
| 1451 | goto error; |
| 1452 | if (mysql_handle_derived(thd->lex, DT_MERGE_FOR_INSERT)) |
| 1453 | goto error; |
| 1454 | if (mysql_handle_derived(thd->lex, DT_PREPARE)) |
| 1455 | goto error; |
| 1456 | |
| 1457 | if (!table_list->single_table_updatable()) |
| 1458 | { |
| 1459 | my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "DELETE" ); |
| 1460 | goto error; |
| 1461 | } |
| 1462 | if (!table_list->table || !table_list->table->is_created()) |
| 1463 | { |
| 1464 | my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0), |
| 1465 | table_list->view_db.str, table_list->view_name.str); |
| 1466 | goto error; |
| 1467 | } |
| 1468 | |
| 1469 | DBUG_RETURN(mysql_prepare_delete(thd, table_list, |
| 1470 | lex->select_lex.with_wild, |
| 1471 | lex->select_lex.item_list, |
| 1472 | &lex->select_lex.where, |
| 1473 | &delete_while_scanning)); |
| 1474 | error: |
| 1475 | DBUG_RETURN(TRUE); |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | /** |
| 1480 | Validate SELECT statement. |
| 1481 | |
| 1482 | In case of success, if this query is not EXPLAIN, send column list info |
| 1483 | back to the client. |
| 1484 | |
| 1485 | @param stmt prepared statement |
| 1486 | @param tables list of tables used in the query |
| 1487 | |
| 1488 | @retval |
| 1489 | 0 success |
| 1490 | @retval |
| 1491 | 1 error, error message is set in THD |
| 1492 | @retval |
| 1493 | 2 success, and statement metadata has been sent |
| 1494 | */ |
| 1495 | |
| 1496 | static int mysql_test_select(Prepared_statement *stmt, |
| 1497 | TABLE_LIST *tables) |
| 1498 | { |
| 1499 | THD *thd= stmt->thd; |
| 1500 | LEX *lex= stmt->lex; |
| 1501 | SELECT_LEX_UNIT *unit= &lex->unit; |
| 1502 | DBUG_ENTER("mysql_test_select" ); |
| 1503 | |
| 1504 | lex->select_lex.context.resolve_in_select_list= TRUE; |
| 1505 | |
| 1506 | ulong privilege= lex->exchange ? SELECT_ACL | FILE_ACL : SELECT_ACL; |
| 1507 | if (tables) |
| 1508 | { |
| 1509 | if (check_table_access(thd, privilege, tables, FALSE, UINT_MAX, FALSE)) |
| 1510 | goto error; |
| 1511 | } |
| 1512 | else if (check_access(thd, privilege, any_db, NULL, NULL, 0, 0)) |
| 1513 | goto error; |
| 1514 | |
| 1515 | if (!lex->result && !(lex->result= new (stmt->mem_root) select_send(thd))) |
| 1516 | { |
| 1517 | my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), |
| 1518 | static_cast<int>(sizeof(select_send))); |
| 1519 | goto error; |
| 1520 | } |
| 1521 | |
| 1522 | if (open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL, |
| 1523 | DT_INIT | DT_PREPARE | DT_CREATE)) |
| 1524 | goto error; |
| 1525 | |
| 1526 | thd->lex->used_tables= 0; // Updated by setup_fields |
| 1527 | |
| 1528 | /* |
| 1529 | JOIN::prepare calls |
| 1530 | It is not SELECT COMMAND for sure, so setup_tables will be called as |
| 1531 | usual, and we pass 0 as setup_tables_done_option |
| 1532 | */ |
| 1533 | if (unit->prepare(unit->derived, 0, 0)) |
| 1534 | goto error; |
| 1535 | if (!lex->describe && !thd->lex->analyze_stmt && !stmt->is_sql_prepare()) |
| 1536 | { |
| 1537 | /* Make copy of item list, as change_columns may change it */ |
| 1538 | List<Item> fields(lex->select_lex.item_list); |
| 1539 | |
| 1540 | /* Change columns if a procedure like analyse() */ |
| 1541 | if (unit->last_procedure && unit->last_procedure->change_columns(thd, fields)) |
| 1542 | goto error; |
| 1543 | |
| 1544 | /* |
| 1545 | We can use lex->result as it should've been prepared in |
| 1546 | unit->prepare call above. |
| 1547 | */ |
| 1548 | if (send_prep_stmt(stmt, lex->result->field_count(fields)) || |
| 1549 | lex->result->send_result_set_metadata(fields, Protocol::SEND_EOF) || |
| 1550 | thd->protocol->flush()) |
| 1551 | goto error; |
| 1552 | DBUG_RETURN(2); |
| 1553 | } |
| 1554 | DBUG_RETURN(0); |
| 1555 | error: |
| 1556 | DBUG_RETURN(1); |
| 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | /** |
| 1561 | Validate and prepare for execution DO statement expressions. |
| 1562 | |
| 1563 | @param stmt prepared statement |
| 1564 | @param tables list of tables used in this query |
| 1565 | @param values list of expressions |
| 1566 | |
| 1567 | @retval |
| 1568 | FALSE success |
| 1569 | @retval |
| 1570 | TRUE error, error message is set in THD |
| 1571 | */ |
| 1572 | |
| 1573 | static bool mysql_test_do_fields(Prepared_statement *stmt, |
| 1574 | TABLE_LIST *tables, |
| 1575 | List<Item> *values) |
| 1576 | { |
| 1577 | THD *thd= stmt->thd; |
| 1578 | |
| 1579 | DBUG_ENTER("mysql_test_do_fields" ); |
| 1580 | if (tables && check_table_access(thd, SELECT_ACL, tables, FALSE, |
| 1581 | UINT_MAX, FALSE)) |
| 1582 | DBUG_RETURN(TRUE); |
| 1583 | |
| 1584 | if (open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL, |
| 1585 | DT_PREPARE | DT_CREATE)) |
| 1586 | DBUG_RETURN(TRUE); |
| 1587 | DBUG_RETURN(setup_fields(thd, Ref_ptr_array(), |
| 1588 | *values, COLUMNS_READ, 0, NULL, 0)); |
| 1589 | } |
| 1590 | |
| 1591 | |
| 1592 | /** |
| 1593 | Validate and prepare for execution SET statement expressions. |
| 1594 | |
| 1595 | @param stmt prepared statement |
| 1596 | @param tables list of tables used in this query |
| 1597 | @param values list of expressions |
| 1598 | |
| 1599 | @retval |
| 1600 | FALSE success |
| 1601 | @retval |
| 1602 | TRUE error, error message is set in THD |
| 1603 | */ |
| 1604 | |
| 1605 | static bool mysql_test_set_fields(Prepared_statement *stmt, |
| 1606 | TABLE_LIST *tables, |
| 1607 | List<set_var_base> *var_list) |
| 1608 | { |
| 1609 | DBUG_ENTER("mysql_test_set_fields" ); |
| 1610 | List_iterator_fast<set_var_base> it(*var_list); |
| 1611 | THD *thd= stmt->thd; |
| 1612 | set_var_base *var; |
| 1613 | |
| 1614 | if ((tables && |
| 1615 | check_table_access(thd, SELECT_ACL, tables, FALSE, UINT_MAX, FALSE)) || |
| 1616 | open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL, |
| 1617 | DT_PREPARE | DT_CREATE)) |
| 1618 | goto error; |
| 1619 | |
| 1620 | while ((var= it++)) |
| 1621 | { |
| 1622 | if (var->light_check(thd)) |
| 1623 | goto error; |
| 1624 | } |
| 1625 | DBUG_RETURN(FALSE); |
| 1626 | error: |
| 1627 | DBUG_RETURN(TRUE); |
| 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | /** |
| 1632 | Validate and prepare for execution CALL statement expressions. |
| 1633 | |
| 1634 | @param stmt prepared statement |
| 1635 | @param tables list of tables used in this query |
| 1636 | @param value_list list of expressions |
| 1637 | |
| 1638 | @retval FALSE success |
| 1639 | @retval TRUE error, error message is set in THD |
| 1640 | */ |
| 1641 | |
| 1642 | static bool mysql_test_call_fields(Prepared_statement *stmt, |
| 1643 | TABLE_LIST *tables, |
| 1644 | List<Item> *value_list) |
| 1645 | { |
| 1646 | DBUG_ENTER("mysql_test_call_fields" ); |
| 1647 | |
| 1648 | List_iterator<Item> it(*value_list); |
| 1649 | THD *thd= stmt->thd; |
| 1650 | Item *item; |
| 1651 | |
| 1652 | if ((tables && |
| 1653 | check_table_access(thd, SELECT_ACL, tables, FALSE, UINT_MAX, FALSE)) || |
| 1654 | open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL, DT_PREPARE)) |
| 1655 | goto err; |
| 1656 | |
| 1657 | while ((item= it++)) |
| 1658 | { |
| 1659 | if ((!item->fixed && item->fix_fields(thd, it.ref())) || |
| 1660 | item->check_cols(1)) |
| 1661 | goto err; |
| 1662 | } |
| 1663 | DBUG_RETURN(FALSE); |
| 1664 | err: |
| 1665 | DBUG_RETURN(TRUE); |
| 1666 | } |
| 1667 | |
| 1668 | |
| 1669 | /** |
| 1670 | Check internal SELECT of the prepared command. |
| 1671 | |
| 1672 | @param stmt prepared statement |
| 1673 | @param specific_prepare function of command specific prepare |
| 1674 | @param setup_tables_done_option options to be passed to LEX::unit.prepare() |
| 1675 | |
| 1676 | @note |
| 1677 | This function won't directly open tables used in select. They should |
| 1678 | be opened either by calling function (and in this case you probably |
| 1679 | should use select_like_stmt_test_with_open()) or by |
| 1680 | "specific_prepare" call (like this happens in case of multi-update). |
| 1681 | |
| 1682 | @retval |
| 1683 | FALSE success |
| 1684 | @retval |
| 1685 | TRUE error, error message is set in THD |
| 1686 | */ |
| 1687 | |
| 1688 | static bool select_like_stmt_test(Prepared_statement *stmt, |
| 1689 | int (*specific_prepare)(THD *thd), |
| 1690 | ulong setup_tables_done_option) |
| 1691 | { |
| 1692 | DBUG_ENTER("select_like_stmt_test" ); |
| 1693 | THD *thd= stmt->thd; |
| 1694 | LEX *lex= stmt->lex; |
| 1695 | |
| 1696 | lex->select_lex.context.resolve_in_select_list= TRUE; |
| 1697 | |
| 1698 | if (specific_prepare && (*specific_prepare)(thd)) |
| 1699 | DBUG_RETURN(TRUE); |
| 1700 | |
| 1701 | thd->lex->used_tables= 0; // Updated by setup_fields |
| 1702 | |
| 1703 | /* Calls JOIN::prepare */ |
| 1704 | DBUG_RETURN(lex->unit.prepare(lex->unit.derived, 0, setup_tables_done_option)); |
| 1705 | } |
| 1706 | |
| 1707 | /** |
| 1708 | Check internal SELECT of the prepared command (with opening of used |
| 1709 | tables). |
| 1710 | |
| 1711 | @param stmt prepared statement |
| 1712 | @param tables list of tables to be opened |
| 1713 | before calling specific_prepare function |
| 1714 | @param specific_prepare function of command specific prepare |
| 1715 | @param setup_tables_done_option options to be passed to LEX::unit.prepare() |
| 1716 | |
| 1717 | @retval |
| 1718 | FALSE success |
| 1719 | @retval |
| 1720 | TRUE error |
| 1721 | */ |
| 1722 | |
| 1723 | static bool |
| 1724 | select_like_stmt_test_with_open(Prepared_statement *stmt, |
| 1725 | TABLE_LIST *tables, |
| 1726 | int (*specific_prepare)(THD *thd), |
| 1727 | ulong setup_tables_done_option) |
| 1728 | { |
| 1729 | uint table_count= 0; |
| 1730 | DBUG_ENTER("select_like_stmt_test_with_open" ); |
| 1731 | |
| 1732 | /* |
| 1733 | We should not call LEX::unit.cleanup() after this |
| 1734 | open_normal_and_derived_tables() call because we don't allow |
| 1735 | prepared EXPLAIN yet so derived tables will clean up after |
| 1736 | themself. |
| 1737 | */ |
| 1738 | THD *thd= stmt->thd; |
| 1739 | if (open_tables(thd, &tables, &table_count, MYSQL_OPEN_FORCE_SHARED_MDL)) |
| 1740 | DBUG_RETURN(TRUE); |
| 1741 | |
| 1742 | DBUG_RETURN(select_like_stmt_test(stmt, specific_prepare, |
| 1743 | setup_tables_done_option)); |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | /** |
| 1748 | Validate and prepare for execution CREATE TABLE statement. |
| 1749 | |
| 1750 | @param stmt prepared statement |
| 1751 | @param tables list of tables used in this query |
| 1752 | |
| 1753 | @retval |
| 1754 | FALSE success |
| 1755 | @retval |
| 1756 | TRUE error, error message is set in THD |
| 1757 | */ |
| 1758 | |
| 1759 | static bool mysql_test_create_table(Prepared_statement *stmt) |
| 1760 | { |
| 1761 | DBUG_ENTER("mysql_test_create_table" ); |
| 1762 | THD *thd= stmt->thd; |
| 1763 | LEX *lex= stmt->lex; |
| 1764 | SELECT_LEX *select_lex= &lex->select_lex; |
| 1765 | bool res= FALSE; |
| 1766 | bool link_to_local; |
| 1767 | TABLE_LIST *create_table= lex->query_tables; |
| 1768 | TABLE_LIST *tables= lex->create_last_non_select_table->next_global; |
| 1769 | |
| 1770 | if (create_table_precheck(thd, tables, create_table)) |
| 1771 | DBUG_RETURN(TRUE); |
| 1772 | |
| 1773 | if (select_lex->item_list.elements) |
| 1774 | { |
| 1775 | /* Base table and temporary table are not in the same name space. */ |
| 1776 | if (!lex->create_info.tmp_table()) |
| 1777 | create_table->open_type= OT_BASE_ONLY; |
| 1778 | |
| 1779 | if (open_normal_and_derived_tables(stmt->thd, lex->query_tables, |
| 1780 | MYSQL_OPEN_FORCE_SHARED_MDL, |
| 1781 | DT_PREPARE | DT_CREATE)) |
| 1782 | DBUG_RETURN(TRUE); |
| 1783 | |
| 1784 | select_lex->context.resolve_in_select_list= TRUE; |
| 1785 | |
| 1786 | lex->unlink_first_table(&link_to_local); |
| 1787 | |
| 1788 | res= select_like_stmt_test(stmt, 0, 0); |
| 1789 | |
| 1790 | lex->link_first_table_back(create_table, link_to_local); |
| 1791 | } |
| 1792 | else |
| 1793 | { |
| 1794 | /* |
| 1795 | Check that the source table exist, and also record |
| 1796 | its metadata version. Even though not strictly necessary, |
| 1797 | we validate metadata of all CREATE TABLE statements, |
| 1798 | which keeps metadata validation code simple. |
| 1799 | */ |
| 1800 | if (open_normal_and_derived_tables(stmt->thd, lex->query_tables, |
| 1801 | MYSQL_OPEN_FORCE_SHARED_MDL, |
| 1802 | DT_PREPARE)) |
| 1803 | DBUG_RETURN(TRUE); |
| 1804 | } |
| 1805 | |
| 1806 | DBUG_RETURN(res); |
| 1807 | } |
| 1808 | |
| 1809 | |
| 1810 | static int send_stmt_metadata(THD *thd, Prepared_statement *stmt, List<Item> *fields) |
| 1811 | { |
| 1812 | if (stmt->is_sql_prepare()) |
| 1813 | return 0; |
| 1814 | |
| 1815 | if (send_prep_stmt(stmt, fields->elements) || |
| 1816 | thd->protocol->send_result_set_metadata(fields, Protocol::SEND_EOF) || |
| 1817 | thd->protocol->flush()) |
| 1818 | return 1; |
| 1819 | |
| 1820 | return 2; |
| 1821 | } |
| 1822 | |
| 1823 | |
| 1824 | /** |
| 1825 | Validate and prepare for execution SHOW CREATE TABLE statement. |
| 1826 | |
| 1827 | @param stmt prepared statement |
| 1828 | @param tables list of tables used in this query |
| 1829 | |
| 1830 | @retval |
| 1831 | FALSE success |
| 1832 | @retval |
| 1833 | TRUE error, error message is set in THD |
| 1834 | */ |
| 1835 | |
| 1836 | static int mysql_test_show_create_table(Prepared_statement *stmt, |
| 1837 | TABLE_LIST *tables) |
| 1838 | { |
| 1839 | DBUG_ENTER("mysql_test_show_create_table" ); |
| 1840 | THD *thd= stmt->thd; |
| 1841 | List<Item> fields; |
| 1842 | char buff[2048]; |
| 1843 | String buffer(buff, sizeof(buff), system_charset_info); |
| 1844 | |
| 1845 | if (mysqld_show_create_get_fields(thd, tables, &fields, &buffer)) |
| 1846 | DBUG_RETURN(1); |
| 1847 | |
| 1848 | DBUG_RETURN(send_stmt_metadata(thd, stmt, &fields)); |
| 1849 | } |
| 1850 | |
| 1851 | |
| 1852 | /** |
| 1853 | Validate and prepare for execution SHOW CREATE DATABASE statement. |
| 1854 | |
| 1855 | @param stmt prepared statement |
| 1856 | |
| 1857 | @retval |
| 1858 | FALSE success |
| 1859 | @retval |
| 1860 | TRUE error, error message is set in THD |
| 1861 | */ |
| 1862 | |
| 1863 | static int mysql_test_show_create_db(Prepared_statement *stmt) |
| 1864 | { |
| 1865 | DBUG_ENTER("mysql_test_show_create_db" ); |
| 1866 | THD *thd= stmt->thd; |
| 1867 | List<Item> fields; |
| 1868 | |
| 1869 | mysqld_show_create_db_get_fields(thd, &fields); |
| 1870 | |
| 1871 | DBUG_RETURN(send_stmt_metadata(thd, stmt, &fields)); |
| 1872 | } |
| 1873 | |
| 1874 | |
| 1875 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1876 | /** |
| 1877 | Validate and prepare for execution SHOW GRANTS statement. |
| 1878 | |
| 1879 | @param stmt prepared statement |
| 1880 | |
| 1881 | @retval |
| 1882 | FALSE success |
| 1883 | @retval |
| 1884 | TRUE error, error message is set in THD |
| 1885 | */ |
| 1886 | |
| 1887 | static int mysql_test_show_grants(Prepared_statement *stmt) |
| 1888 | { |
| 1889 | DBUG_ENTER("mysql_test_show_grants" ); |
| 1890 | THD *thd= stmt->thd; |
| 1891 | List<Item> fields; |
| 1892 | |
| 1893 | mysql_show_grants_get_fields(thd, &fields, STRING_WITH_LEN("Grants for" )); |
| 1894 | DBUG_RETURN(send_stmt_metadata(thd, stmt, &fields)); |
| 1895 | } |
| 1896 | #endif /*NO_EMBEDDED_ACCESS_CHECKS*/ |
| 1897 | |
| 1898 | |
| 1899 | #ifndef EMBEDDED_LIBRARY |
| 1900 | /** |
| 1901 | Validate and prepare for execution SHOW SLAVE STATUS statement. |
| 1902 | |
| 1903 | @param stmt prepared statement |
| 1904 | |
| 1905 | @retval |
| 1906 | FALSE success |
| 1907 | @retval |
| 1908 | TRUE error, error message is set in THD |
| 1909 | */ |
| 1910 | |
| 1911 | static int mysql_test_show_slave_status(Prepared_statement *stmt) |
| 1912 | { |
| 1913 | DBUG_ENTER("mysql_test_show_slave_status" ); |
| 1914 | THD *thd= stmt->thd; |
| 1915 | List<Item> fields; |
| 1916 | |
| 1917 | show_master_info_get_fields(thd, &fields, 0, 0); |
| 1918 | |
| 1919 | DBUG_RETURN(send_stmt_metadata(thd, stmt, &fields)); |
| 1920 | } |
| 1921 | |
| 1922 | |
| 1923 | /** |
| 1924 | Validate and prepare for execution SHOW MASTER STATUS statement. |
| 1925 | |
| 1926 | @param stmt prepared statement |
| 1927 | |
| 1928 | @retval |
| 1929 | FALSE success |
| 1930 | @retval |
| 1931 | TRUE error, error message is set in THD |
| 1932 | */ |
| 1933 | |
| 1934 | static int mysql_test_show_master_status(Prepared_statement *stmt) |
| 1935 | { |
| 1936 | DBUG_ENTER("mysql_test_show_master_status" ); |
| 1937 | THD *thd= stmt->thd; |
| 1938 | List<Item> fields; |
| 1939 | |
| 1940 | show_binlog_info_get_fields(thd, &fields); |
| 1941 | |
| 1942 | DBUG_RETURN(send_stmt_metadata(thd, stmt, &fields)); |
| 1943 | } |
| 1944 | |
| 1945 | |
| 1946 | /** |
| 1947 | Validate and prepare for execution SHOW BINLOGS statement. |
| 1948 | |
| 1949 | @param stmt prepared statement |
| 1950 | |
| 1951 | @retval |
| 1952 | FALSE success |
| 1953 | @retval |
| 1954 | TRUE error, error message is set in THD |
| 1955 | */ |
| 1956 | |
| 1957 | static int mysql_test_show_binlogs(Prepared_statement *stmt) |
| 1958 | { |
| 1959 | DBUG_ENTER("mysql_test_show_binlogs" ); |
| 1960 | THD *thd= stmt->thd; |
| 1961 | List<Item> fields; |
| 1962 | |
| 1963 | show_binlogs_get_fields(thd, &fields); |
| 1964 | |
| 1965 | DBUG_RETURN(send_stmt_metadata(thd, stmt, &fields)); |
| 1966 | } |
| 1967 | |
| 1968 | #endif /* EMBEDDED_LIBRARY */ |
| 1969 | |
| 1970 | |
| 1971 | /** |
| 1972 | Validate and prepare for execution SHOW CREATE PROC/FUNC statement. |
| 1973 | |
| 1974 | @param stmt prepared statement |
| 1975 | |
| 1976 | @retval |
| 1977 | FALSE success |
| 1978 | @retval |
| 1979 | TRUE error, error message is set in THD |
| 1980 | */ |
| 1981 | |
| 1982 | static int mysql_test_show_create_routine(Prepared_statement *stmt, |
| 1983 | const Sp_handler *sph) |
| 1984 | { |
| 1985 | DBUG_ENTER("mysql_test_show_binlogs" ); |
| 1986 | THD *thd= stmt->thd; |
| 1987 | List<Item> fields; |
| 1988 | |
| 1989 | sp_head::show_create_routine_get_fields(thd, sph, &fields); |
| 1990 | |
| 1991 | DBUG_RETURN(send_stmt_metadata(thd, stmt, &fields)); |
| 1992 | } |
| 1993 | |
| 1994 | |
| 1995 | /** |
| 1996 | @brief Validate and prepare for execution CREATE VIEW statement |
| 1997 | |
| 1998 | @param stmt prepared statement |
| 1999 | |
| 2000 | @note This function handles create view commands. |
| 2001 | |
| 2002 | @retval FALSE Operation was a success. |
| 2003 | @retval TRUE An error occurred. |
| 2004 | */ |
| 2005 | |
| 2006 | static bool mysql_test_create_view(Prepared_statement *stmt) |
| 2007 | { |
| 2008 | DBUG_ENTER("mysql_test_create_view" ); |
| 2009 | THD *thd= stmt->thd; |
| 2010 | LEX *lex= stmt->lex; |
| 2011 | bool res= TRUE; |
| 2012 | /* Skip first table, which is the view we are creating */ |
| 2013 | bool link_to_local; |
| 2014 | TABLE_LIST *view= lex->unlink_first_table(&link_to_local); |
| 2015 | TABLE_LIST *tables= lex->query_tables; |
| 2016 | |
| 2017 | if (create_view_precheck(thd, tables, view, lex->create_view->mode)) |
| 2018 | goto err; |
| 2019 | |
| 2020 | /* |
| 2021 | Since we can't pre-open temporary tables for SQLCOM_CREATE_VIEW, |
| 2022 | (see mysql_create_view) we have to do it here instead. |
| 2023 | */ |
| 2024 | if (thd->open_temporary_tables(tables)) |
| 2025 | goto err; |
| 2026 | |
| 2027 | lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_VIEW; |
| 2028 | if (open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL, |
| 2029 | DT_PREPARE)) |
| 2030 | goto err; |
| 2031 | |
| 2032 | res= select_like_stmt_test(stmt, 0, 0); |
| 2033 | |
| 2034 | err: |
| 2035 | /* put view back for PS rexecuting */ |
| 2036 | lex->link_first_table_back(view, link_to_local); |
| 2037 | DBUG_RETURN(res); |
| 2038 | } |
| 2039 | |
| 2040 | |
| 2041 | /* |
| 2042 | Validate and prepare for execution a multi update statement. |
| 2043 | |
| 2044 | @param stmt prepared statement |
| 2045 | @param tables list of tables used in this query |
| 2046 | @param converted converted to multi-update from usual update |
| 2047 | |
| 2048 | @retval |
| 2049 | FALSE success |
| 2050 | @retval |
| 2051 | TRUE error, error message is set in THD |
| 2052 | */ |
| 2053 | |
| 2054 | static bool mysql_test_multiupdate(Prepared_statement *stmt, |
| 2055 | TABLE_LIST *tables, |
| 2056 | bool converted) |
| 2057 | { |
| 2058 | /* if we switched from normal update, rights are checked */ |
| 2059 | if (!converted && multi_update_precheck(stmt->thd, tables)) |
| 2060 | return TRUE; |
| 2061 | |
| 2062 | return select_like_stmt_test(stmt, &mysql_multi_update_prepare, |
| 2063 | OPTION_SETUP_TABLES_DONE); |
| 2064 | } |
| 2065 | |
| 2066 | |
| 2067 | /** |
| 2068 | Validate and prepare for execution a multi delete statement. |
| 2069 | |
| 2070 | @param stmt prepared statement |
| 2071 | @param tables list of tables used in this query |
| 2072 | |
| 2073 | @retval |
| 2074 | FALSE success |
| 2075 | @retval |
| 2076 | TRUE error, error message in THD is set. |
| 2077 | */ |
| 2078 | |
| 2079 | static bool mysql_test_multidelete(Prepared_statement *stmt, |
| 2080 | TABLE_LIST *tables) |
| 2081 | { |
| 2082 | THD *thd= stmt->thd; |
| 2083 | |
| 2084 | thd->lex->current_select= &thd->lex->select_lex; |
| 2085 | if (add_item_to_list(thd, new (thd->mem_root) |
| 2086 | Item_null(thd))) |
| 2087 | { |
| 2088 | my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 0); |
| 2089 | goto error; |
| 2090 | } |
| 2091 | |
| 2092 | if (multi_delete_precheck(thd, tables) || |
| 2093 | select_like_stmt_test_with_open(stmt, tables, |
| 2094 | &mysql_multi_delete_prepare, |
| 2095 | OPTION_SETUP_TABLES_DONE)) |
| 2096 | goto error; |
| 2097 | if (!tables->table) |
| 2098 | { |
| 2099 | my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0), |
| 2100 | tables->view_db.str, tables->view_name.str); |
| 2101 | goto error; |
| 2102 | } |
| 2103 | return FALSE; |
| 2104 | error: |
| 2105 | return TRUE; |
| 2106 | } |
| 2107 | |
| 2108 | |
| 2109 | /** |
| 2110 | Wrapper for mysql_insert_select_prepare, to make change of local tables |
| 2111 | after open_normal_and_derived_tables() call. |
| 2112 | |
| 2113 | @param thd thread handle |
| 2114 | |
| 2115 | @note |
| 2116 | We need to remove the first local table after |
| 2117 | open_normal_and_derived_tables(), because mysql_handle_derived |
| 2118 | uses local tables lists. |
| 2119 | */ |
| 2120 | |
| 2121 | static int mysql_insert_select_prepare_tester(THD *thd) |
| 2122 | { |
| 2123 | SELECT_LEX *first_select= &thd->lex->select_lex; |
| 2124 | TABLE_LIST *second_table= first_select->table_list.first->next_local; |
| 2125 | |
| 2126 | /* Skip first table, which is the table we are inserting in */ |
| 2127 | first_select->table_list.first= second_table; |
| 2128 | thd->lex->select_lex.context.table_list= |
| 2129 | thd->lex->select_lex.context.first_name_resolution_table= second_table; |
| 2130 | |
| 2131 | return mysql_insert_select_prepare(thd); |
| 2132 | } |
| 2133 | |
| 2134 | |
| 2135 | /** |
| 2136 | Validate and prepare for execution INSERT ... SELECT statement. |
| 2137 | |
| 2138 | @param stmt prepared statement |
| 2139 | @param tables list of tables used in this query |
| 2140 | |
| 2141 | @retval |
| 2142 | FALSE success |
| 2143 | @retval |
| 2144 | TRUE error, error message is set in THD |
| 2145 | */ |
| 2146 | |
| 2147 | static bool mysql_test_insert_select(Prepared_statement *stmt, |
| 2148 | TABLE_LIST *tables) |
| 2149 | { |
| 2150 | int res; |
| 2151 | LEX *lex= stmt->lex; |
| 2152 | TABLE_LIST *first_local_table; |
| 2153 | |
| 2154 | if (tables->table) |
| 2155 | { |
| 2156 | // don't allocate insert_values |
| 2157 | tables->table->insert_values=(uchar *)1; |
| 2158 | } |
| 2159 | |
| 2160 | if (insert_precheck(stmt->thd, tables)) |
| 2161 | return 1; |
| 2162 | |
| 2163 | /* store it, because mysql_insert_select_prepare_tester change it */ |
| 2164 | first_local_table= lex->select_lex.table_list.first; |
| 2165 | DBUG_ASSERT(first_local_table != 0); |
| 2166 | |
| 2167 | res= |
| 2168 | select_like_stmt_test_with_open(stmt, tables, |
| 2169 | &mysql_insert_select_prepare_tester, |
| 2170 | OPTION_SETUP_TABLES_DONE); |
| 2171 | /* revert changes made by mysql_insert_select_prepare_tester */ |
| 2172 | lex->select_lex.table_list.first= first_local_table; |
| 2173 | return res; |
| 2174 | } |
| 2175 | |
| 2176 | /** |
| 2177 | Validate SELECT statement. |
| 2178 | |
| 2179 | In case of success, if this query is not EXPLAIN, send column list info |
| 2180 | back to the client. |
| 2181 | |
| 2182 | @param stmt prepared statement |
| 2183 | @param tables list of tables used in the query |
| 2184 | |
| 2185 | @retval 0 success |
| 2186 | @retval 1 error, error message is set in THD |
| 2187 | @retval 2 success, and statement metadata has been sent |
| 2188 | */ |
| 2189 | |
| 2190 | static int mysql_test_handler_read(Prepared_statement *stmt, |
| 2191 | TABLE_LIST *tables) |
| 2192 | { |
| 2193 | THD *thd= stmt->thd; |
| 2194 | LEX *lex= stmt->lex; |
| 2195 | SQL_HANDLER *ha_table; |
| 2196 | DBUG_ENTER("mysql_test_handler_read" ); |
| 2197 | |
| 2198 | lex->select_lex.context.resolve_in_select_list= TRUE; |
| 2199 | |
| 2200 | /* |
| 2201 | We don't have to test for permissions as this is already done during |
| 2202 | HANDLER OPEN |
| 2203 | */ |
| 2204 | if (!(ha_table= mysql_ha_read_prepare(thd, tables, lex->ha_read_mode, |
| 2205 | lex->ident.str, |
| 2206 | lex->insert_list, |
| 2207 | lex->ha_rkey_mode, |
| 2208 | lex->select_lex.where))) |
| 2209 | DBUG_RETURN(1); |
| 2210 | |
| 2211 | if (!stmt->is_sql_prepare()) |
| 2212 | { |
| 2213 | if (!lex->result && !(lex->result= new (stmt->mem_root) select_send(thd))) |
| 2214 | DBUG_RETURN(1); |
| 2215 | |
| 2216 | if (send_prep_stmt(stmt, ha_table->fields.elements) || |
| 2217 | lex->result->send_result_set_metadata(ha_table->fields, Protocol::SEND_EOF) || |
| 2218 | thd->protocol->flush()) |
| 2219 | DBUG_RETURN(1); |
| 2220 | DBUG_RETURN(2); |
| 2221 | } |
| 2222 | DBUG_RETURN(0); |
| 2223 | } |
| 2224 | |
| 2225 | |
| 2226 | /** |
| 2227 | Perform semantic analysis of the parsed tree and send a response packet |
| 2228 | to the client. |
| 2229 | |
| 2230 | This function |
| 2231 | - opens all tables and checks access rights |
| 2232 | - validates semantics of statement columns and SQL functions |
| 2233 | by calling fix_fields. |
| 2234 | |
| 2235 | @param stmt prepared statement |
| 2236 | |
| 2237 | @retval |
| 2238 | FALSE success, statement metadata is sent to client |
| 2239 | @retval |
| 2240 | TRUE error, error message is set in THD (but not sent) |
| 2241 | */ |
| 2242 | |
| 2243 | static bool check_prepared_statement(Prepared_statement *stmt) |
| 2244 | { |
| 2245 | THD *thd= stmt->thd; |
| 2246 | LEX *lex= stmt->lex; |
| 2247 | SELECT_LEX *select_lex= &lex->select_lex; |
| 2248 | TABLE_LIST *tables; |
| 2249 | enum enum_sql_command sql_command= lex->sql_command; |
| 2250 | int res= 0; |
| 2251 | DBUG_ENTER("check_prepared_statement" ); |
| 2252 | DBUG_PRINT("enter" ,("command: %d param_count: %u" , |
| 2253 | sql_command, stmt->param_count)); |
| 2254 | |
| 2255 | lex->first_lists_tables_same(); |
| 2256 | tables= lex->query_tables; |
| 2257 | |
| 2258 | /* set context for commands which do not use setup_tables */ |
| 2259 | lex->select_lex.context.resolve_in_table_list_only(select_lex-> |
| 2260 | get_table_list()); |
| 2261 | |
| 2262 | /* Reset warning count for each query that uses tables */ |
| 2263 | if (tables) |
| 2264 | thd->get_stmt_da()->opt_clear_warning_info(thd->query_id); |
| 2265 | |
| 2266 | if (check_dependencies_in_with_clauses(thd->lex->with_clauses_list)) |
| 2267 | goto error; |
| 2268 | |
| 2269 | if (sql_command_flags[sql_command] & CF_HA_CLOSE) |
| 2270 | mysql_ha_rm_tables(thd, tables); |
| 2271 | |
| 2272 | /* |
| 2273 | Open temporary tables that are known now. Temporary tables added by |
| 2274 | prelocking will be opened afterwards (during open_tables()). |
| 2275 | */ |
| 2276 | if (sql_command_flags[sql_command] & CF_PREOPEN_TMP_TABLES) |
| 2277 | { |
| 2278 | if (thd->open_temporary_tables(tables)) |
| 2279 | goto error; |
| 2280 | } |
| 2281 | |
| 2282 | switch (sql_command) { |
| 2283 | case SQLCOM_REPLACE: |
| 2284 | case SQLCOM_INSERT: |
| 2285 | res= mysql_test_insert(stmt, tables, lex->field_list, |
| 2286 | lex->many_values, |
| 2287 | lex->update_list, lex->value_list, |
| 2288 | lex->duplicates); |
| 2289 | break; |
| 2290 | |
| 2291 | case SQLCOM_UPDATE: |
| 2292 | res= mysql_test_update(stmt, tables); |
| 2293 | /* mysql_test_update returns 2 if we need to switch to multi-update */ |
| 2294 | if (res != 2) |
| 2295 | break; |
| 2296 | /* fall through */ |
| 2297 | case SQLCOM_UPDATE_MULTI: |
| 2298 | res= mysql_test_multiupdate(stmt, tables, res == 2); |
| 2299 | break; |
| 2300 | |
| 2301 | case SQLCOM_DELETE: |
| 2302 | res= mysql_test_delete(stmt, tables); |
| 2303 | break; |
| 2304 | /* The following allow WHERE clause, so they must be tested like SELECT */ |
| 2305 | case SQLCOM_SHOW_DATABASES: |
| 2306 | case SQLCOM_SHOW_TABLES: |
| 2307 | case SQLCOM_SHOW_TRIGGERS: |
| 2308 | case SQLCOM_SHOW_EVENTS: |
| 2309 | case SQLCOM_SHOW_OPEN_TABLES: |
| 2310 | case SQLCOM_SHOW_FIELDS: |
| 2311 | case SQLCOM_SHOW_KEYS: |
| 2312 | case SQLCOM_SHOW_COLLATIONS: |
| 2313 | case SQLCOM_SHOW_CHARSETS: |
| 2314 | case SQLCOM_SHOW_VARIABLES: |
| 2315 | case SQLCOM_SHOW_STATUS: |
| 2316 | case SQLCOM_SHOW_TABLE_STATUS: |
| 2317 | case SQLCOM_SHOW_STATUS_PROC: |
| 2318 | case SQLCOM_SHOW_STATUS_FUNC: |
| 2319 | case SQLCOM_SHOW_STATUS_PACKAGE: |
| 2320 | case SQLCOM_SHOW_STATUS_PACKAGE_BODY: |
| 2321 | case SQLCOM_SELECT: |
| 2322 | res= mysql_test_select(stmt, tables); |
| 2323 | if (res == 2) |
| 2324 | { |
| 2325 | /* Statement and field info has already been sent */ |
| 2326 | DBUG_RETURN(FALSE); |
| 2327 | } |
| 2328 | break; |
| 2329 | case SQLCOM_CREATE_TABLE: |
| 2330 | case SQLCOM_CREATE_SEQUENCE: |
| 2331 | res= mysql_test_create_table(stmt); |
| 2332 | break; |
| 2333 | case SQLCOM_SHOW_CREATE: |
| 2334 | if ((res= mysql_test_show_create_table(stmt, tables)) == 2) |
| 2335 | { |
| 2336 | /* Statement and field info has already been sent */ |
| 2337 | DBUG_RETURN(FALSE); |
| 2338 | } |
| 2339 | break; |
| 2340 | case SQLCOM_SHOW_CREATE_DB: |
| 2341 | if ((res= mysql_test_show_create_db(stmt)) == 2) |
| 2342 | { |
| 2343 | /* Statement and field info has already been sent */ |
| 2344 | DBUG_RETURN(FALSE); |
| 2345 | } |
| 2346 | break; |
| 2347 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 2348 | case SQLCOM_SHOW_GRANTS: |
| 2349 | if ((res= mysql_test_show_grants(stmt)) == 2) |
| 2350 | { |
| 2351 | /* Statement and field info has already been sent */ |
| 2352 | DBUG_RETURN(FALSE); |
| 2353 | } |
| 2354 | break; |
| 2355 | #endif /* NO_EMBEDDED_ACCESS_CHECKS */ |
| 2356 | #ifndef EMBEDDED_LIBRARY |
| 2357 | case SQLCOM_SHOW_SLAVE_STAT: |
| 2358 | if ((res= mysql_test_show_slave_status(stmt)) == 2) |
| 2359 | { |
| 2360 | /* Statement and field info has already been sent */ |
| 2361 | DBUG_RETURN(FALSE); |
| 2362 | } |
| 2363 | break; |
| 2364 | case SQLCOM_SHOW_MASTER_STAT: |
| 2365 | if ((res= mysql_test_show_master_status(stmt)) == 2) |
| 2366 | { |
| 2367 | /* Statement and field info has already been sent */ |
| 2368 | DBUG_RETURN(FALSE); |
| 2369 | } |
| 2370 | break; |
| 2371 | case SQLCOM_SHOW_BINLOGS: |
| 2372 | if ((res= mysql_test_show_binlogs(stmt)) == 2) |
| 2373 | { |
| 2374 | /* Statement and field info has already been sent */ |
| 2375 | DBUG_RETURN(FALSE); |
| 2376 | } |
| 2377 | break; |
| 2378 | #endif /* EMBEDDED_LIBRARY */ |
| 2379 | case SQLCOM_SHOW_CREATE_PROC: |
| 2380 | if ((res= mysql_test_show_create_routine(stmt, &sp_handler_procedure)) == 2) |
| 2381 | { |
| 2382 | /* Statement and field info has already been sent */ |
| 2383 | DBUG_RETURN(FALSE); |
| 2384 | } |
| 2385 | break; |
| 2386 | case SQLCOM_SHOW_CREATE_FUNC: |
| 2387 | if ((res= mysql_test_show_create_routine(stmt, &sp_handler_function)) == 2) |
| 2388 | { |
| 2389 | /* Statement and field info has already been sent */ |
| 2390 | DBUG_RETURN(FALSE); |
| 2391 | } |
| 2392 | break; |
| 2393 | case SQLCOM_SHOW_CREATE_PACKAGE: |
| 2394 | if ((res= mysql_test_show_create_routine(stmt, &sp_handler_package_spec)) == 2) |
| 2395 | { |
| 2396 | /* Statement and field info has already been sent */ |
| 2397 | DBUG_RETURN(FALSE); |
| 2398 | } |
| 2399 | break; |
| 2400 | case SQLCOM_SHOW_CREATE_PACKAGE_BODY: |
| 2401 | if ((res= mysql_test_show_create_routine(stmt, |
| 2402 | &sp_handler_package_body)) == 2) |
| 2403 | { |
| 2404 | /* Statement and field info has already been sent */ |
| 2405 | DBUG_RETURN(FALSE); |
| 2406 | } |
| 2407 | break; |
| 2408 | case SQLCOM_CREATE_VIEW: |
| 2409 | if (lex->create_view->mode == VIEW_ALTER) |
| 2410 | { |
| 2411 | my_message(ER_UNSUPPORTED_PS, ER_THD(thd, ER_UNSUPPORTED_PS), MYF(0)); |
| 2412 | goto error; |
| 2413 | } |
| 2414 | res= mysql_test_create_view(stmt); |
| 2415 | break; |
| 2416 | case SQLCOM_DO: |
| 2417 | res= mysql_test_do_fields(stmt, tables, lex->insert_list); |
| 2418 | break; |
| 2419 | |
| 2420 | case SQLCOM_CALL: |
| 2421 | res= mysql_test_call_fields(stmt, tables, &lex->value_list); |
| 2422 | break; |
| 2423 | case SQLCOM_SET_OPTION: |
| 2424 | res= mysql_test_set_fields(stmt, tables, &lex->var_list); |
| 2425 | break; |
| 2426 | |
| 2427 | case SQLCOM_DELETE_MULTI: |
| 2428 | res= mysql_test_multidelete(stmt, tables); |
| 2429 | break; |
| 2430 | |
| 2431 | case SQLCOM_INSERT_SELECT: |
| 2432 | case SQLCOM_REPLACE_SELECT: |
| 2433 | res= mysql_test_insert_select(stmt, tables); |
| 2434 | break; |
| 2435 | |
| 2436 | case SQLCOM_HA_READ: |
| 2437 | res= mysql_test_handler_read(stmt, tables); |
| 2438 | /* Statement and field info has already been sent */ |
| 2439 | DBUG_RETURN(res == 1 ? TRUE : FALSE); |
| 2440 | |
| 2441 | /* |
| 2442 | Note that we don't need to have cases in this list if they are |
| 2443 | marked with CF_STATUS_COMMAND in sql_command_flags |
| 2444 | */ |
| 2445 | case SQLCOM_SHOW_EXPLAIN: |
| 2446 | case SQLCOM_DROP_TABLE: |
| 2447 | case SQLCOM_DROP_SEQUENCE: |
| 2448 | case SQLCOM_RENAME_TABLE: |
| 2449 | case SQLCOM_ALTER_TABLE: |
| 2450 | case SQLCOM_ALTER_SEQUENCE: |
| 2451 | case SQLCOM_COMMIT: |
| 2452 | case SQLCOM_CREATE_INDEX: |
| 2453 | case SQLCOM_DROP_INDEX: |
| 2454 | case SQLCOM_ROLLBACK: |
| 2455 | case SQLCOM_TRUNCATE: |
| 2456 | case SQLCOM_DROP_VIEW: |
| 2457 | case SQLCOM_REPAIR: |
| 2458 | case SQLCOM_ANALYZE: |
| 2459 | case SQLCOM_OPTIMIZE: |
| 2460 | case SQLCOM_CHANGE_MASTER: |
| 2461 | case SQLCOM_RESET: |
| 2462 | case SQLCOM_FLUSH: |
| 2463 | case SQLCOM_SLAVE_START: |
| 2464 | case SQLCOM_SLAVE_STOP: |
| 2465 | case SQLCOM_SLAVE_ALL_START: |
| 2466 | case SQLCOM_SLAVE_ALL_STOP: |
| 2467 | case SQLCOM_INSTALL_PLUGIN: |
| 2468 | case SQLCOM_UNINSTALL_PLUGIN: |
| 2469 | case SQLCOM_CREATE_DB: |
| 2470 | case SQLCOM_DROP_DB: |
| 2471 | case SQLCOM_ALTER_DB_UPGRADE: |
| 2472 | case SQLCOM_CHECKSUM: |
| 2473 | case SQLCOM_CREATE_USER: |
| 2474 | case SQLCOM_RENAME_USER: |
| 2475 | case SQLCOM_DROP_USER: |
| 2476 | case SQLCOM_CREATE_ROLE: |
| 2477 | case SQLCOM_DROP_ROLE: |
| 2478 | case SQLCOM_ASSIGN_TO_KEYCACHE: |
| 2479 | case SQLCOM_PRELOAD_KEYS: |
| 2480 | case SQLCOM_GRANT: |
| 2481 | case SQLCOM_GRANT_ROLE: |
| 2482 | case SQLCOM_REVOKE: |
| 2483 | case SQLCOM_REVOKE_ROLE: |
| 2484 | case SQLCOM_KILL: |
| 2485 | case SQLCOM_COMPOUND: |
| 2486 | case SQLCOM_SHUTDOWN: |
| 2487 | break; |
| 2488 | |
| 2489 | case SQLCOM_PREPARE: |
| 2490 | case SQLCOM_EXECUTE: |
| 2491 | case SQLCOM_DEALLOCATE_PREPARE: |
| 2492 | default: |
| 2493 | /* |
| 2494 | Trivial check of all status commands. This is easier than having |
| 2495 | things in the above case list, as it's less chance for mistakes. |
| 2496 | */ |
| 2497 | if (!(sql_command_flags[sql_command] & CF_STATUS_COMMAND)) |
| 2498 | { |
| 2499 | /* All other statements are not supported yet. */ |
| 2500 | my_message(ER_UNSUPPORTED_PS, ER_THD(thd, ER_UNSUPPORTED_PS), MYF(0)); |
| 2501 | goto error; |
| 2502 | } |
| 2503 | break; |
| 2504 | } |
| 2505 | if (res == 0) |
| 2506 | { |
| 2507 | if (!stmt->is_sql_prepare()) |
| 2508 | { |
| 2509 | if (lex->describe || lex->analyze_stmt) |
| 2510 | { |
| 2511 | if (!lex->result && |
| 2512 | !(lex->result= new (stmt->mem_root) select_send(thd))) |
| 2513 | DBUG_RETURN(TRUE); |
| 2514 | List<Item> field_list; |
| 2515 | thd->prepare_explain_fields(lex->result, &field_list, |
| 2516 | lex->describe, lex->analyze_stmt); |
| 2517 | res= send_prep_stmt(stmt, lex->result->field_count(field_list)) || |
| 2518 | lex->result->send_result_set_metadata(field_list, |
| 2519 | Protocol::SEND_EOF); |
| 2520 | } |
| 2521 | else |
| 2522 | res= send_prep_stmt(stmt, 0); |
| 2523 | if (!res) |
| 2524 | thd->protocol->flush(); |
| 2525 | } |
| 2526 | DBUG_RETURN(FALSE); |
| 2527 | } |
| 2528 | error: |
| 2529 | DBUG_RETURN(TRUE); |
| 2530 | } |
| 2531 | |
| 2532 | /** |
| 2533 | Initialize array of parameters in statement from LEX. |
| 2534 | (We need to have quick access to items by number in mysql_stmt_get_longdata). |
| 2535 | This is to avoid using malloc/realloc in the parser. |
| 2536 | */ |
| 2537 | |
| 2538 | static bool init_param_array(Prepared_statement *stmt) |
| 2539 | { |
| 2540 | LEX *lex= stmt->lex; |
| 2541 | if ((stmt->param_count= lex->param_list.elements)) |
| 2542 | { |
| 2543 | if (stmt->param_count > (uint) UINT_MAX16) |
| 2544 | { |
| 2545 | /* Error code to be defined in 5.0 */ |
| 2546 | my_message(ER_PS_MANY_PARAM, ER_THD(stmt->thd, ER_PS_MANY_PARAM), |
| 2547 | MYF(0)); |
| 2548 | return TRUE; |
| 2549 | } |
| 2550 | Item_param **to; |
| 2551 | List_iterator<Item_param> param_iterator(lex->param_list); |
| 2552 | /* Use thd->mem_root as it points at statement mem_root */ |
| 2553 | stmt->param_array= (Item_param **) |
| 2554 | alloc_root(stmt->thd->mem_root, |
| 2555 | sizeof(Item_param*) * stmt->param_count); |
| 2556 | if (!stmt->param_array) |
| 2557 | return TRUE; |
| 2558 | for (to= stmt->param_array; |
| 2559 | to < stmt->param_array + stmt->param_count; |
| 2560 | ++to) |
| 2561 | { |
| 2562 | *to= param_iterator++; |
| 2563 | } |
| 2564 | } |
| 2565 | return FALSE; |
| 2566 | } |
| 2567 | |
| 2568 | |
| 2569 | /** |
| 2570 | COM_STMT_PREPARE handler. |
| 2571 | |
| 2572 | Given a query string with parameter markers, create a prepared |
| 2573 | statement from it and send PS info back to the client. |
| 2574 | |
| 2575 | If parameter markers are found in the query, then store the information |
| 2576 | using Item_param along with maintaining a list in lex->param_array, so |
| 2577 | that a fast and direct retrieval can be made without going through all |
| 2578 | field items. |
| 2579 | |
| 2580 | @param packet query to be prepared |
| 2581 | @param packet_length query string length, including ignored |
| 2582 | trailing NULL or quote char. |
| 2583 | |
| 2584 | @note |
| 2585 | This function parses the query and sends the total number of parameters |
| 2586 | and resultset metadata information back to client (if any), without |
| 2587 | executing the query i.e. without any log/disk writes. This allows the |
| 2588 | queries to be re-executed without re-parsing during execute. |
| 2589 | |
| 2590 | @return |
| 2591 | none: in case of success a new statement id and metadata is sent |
| 2592 | to the client, otherwise an error message is set in THD. |
| 2593 | */ |
| 2594 | |
| 2595 | void mysqld_stmt_prepare(THD *thd, const char *packet, uint packet_length) |
| 2596 | { |
| 2597 | Protocol *save_protocol= thd->protocol; |
| 2598 | Prepared_statement *stmt; |
| 2599 | DBUG_ENTER("mysqld_stmt_prepare" ); |
| 2600 | DBUG_PRINT("prep_query" , ("%s" , packet)); |
| 2601 | |
| 2602 | /* First of all clear possible warnings from the previous command */ |
| 2603 | thd->reset_for_next_command(); |
| 2604 | |
| 2605 | if (! (stmt= new Prepared_statement(thd))) |
| 2606 | goto end; /* out of memory: error is set in Sql_alloc */ |
| 2607 | |
| 2608 | if (thd->stmt_map.insert(thd, stmt)) |
| 2609 | { |
| 2610 | /* |
| 2611 | The error is set in the insert. The statement itself |
| 2612 | will be also deleted there (this is how the hash works). |
| 2613 | */ |
| 2614 | goto end; |
| 2615 | } |
| 2616 | |
| 2617 | thd->protocol= &thd->protocol_binary; |
| 2618 | |
| 2619 | if (stmt->prepare(packet, packet_length)) |
| 2620 | { |
| 2621 | /* Statement map deletes statement on erase */ |
| 2622 | thd->stmt_map.erase(stmt); |
| 2623 | thd->clear_last_stmt(); |
| 2624 | } |
| 2625 | else |
| 2626 | thd->set_last_stmt(stmt); |
| 2627 | |
| 2628 | thd->protocol= save_protocol; |
| 2629 | |
| 2630 | sp_cache_enforce_limit(thd->sp_proc_cache, stored_program_cache_size); |
| 2631 | sp_cache_enforce_limit(thd->sp_func_cache, stored_program_cache_size); |
| 2632 | sp_cache_enforce_limit(thd->sp_package_spec_cache, stored_program_cache_size); |
| 2633 | sp_cache_enforce_limit(thd->sp_package_body_cache, stored_program_cache_size); |
| 2634 | |
| 2635 | /* check_prepared_statemnt sends the metadata packet in case of success */ |
| 2636 | end: |
| 2637 | DBUG_VOID_RETURN; |
| 2638 | } |
| 2639 | |
| 2640 | /** |
| 2641 | Get an SQL statement from an item in lex->prepared_stmt_code. |
| 2642 | |
| 2643 | This function can return pointers to very different memory classes: |
| 2644 | - a static string "NULL", if the item returned NULL |
| 2645 | - the result of prepare_stmt_code->val_str(), if no conversion was needed |
| 2646 | - a thd->mem_root allocated string with the result of |
| 2647 | prepare_stmt_code->val_str() converted to @@collation_connection, |
| 2648 | if conversion was needed |
| 2649 | |
| 2650 | The caller must dispose the result before the life cycle of "buffer" ends. |
| 2651 | As soon as buffer's destructor is called, the value is not valid any more! |
| 2652 | |
| 2653 | mysql_sql_stmt_prepare() and mysql_sql_stmt_execute_immediate() |
| 2654 | call get_dynamic_sql_string() and then call respectively |
| 2655 | Prepare_statement::prepare() and Prepare_statment::execute_immediate(), |
| 2656 | who store the returned result into its permanent location using |
| 2657 | alloc_query(). "buffer" is still not destructed at that time. |
| 2658 | |
| 2659 | @param[out] dst the result is stored here |
| 2660 | @param[inout] buffer |
| 2661 | |
| 2662 | @retval false on success |
| 2663 | @retval true on error (out of memory) |
| 2664 | */ |
| 2665 | |
| 2666 | bool LEX::get_dynamic_sql_string(LEX_CSTRING *dst, String *buffer) |
| 2667 | { |
| 2668 | if (prepared_stmt_code->fix_fields(thd, NULL) || |
| 2669 | prepared_stmt_code->check_cols(1)) |
| 2670 | return true; |
| 2671 | |
| 2672 | const String *str= prepared_stmt_code->val_str(buffer); |
| 2673 | if (prepared_stmt_code->null_value) |
| 2674 | { |
| 2675 | /* |
| 2676 | Prepare source was NULL, so we need to set "str" to |
| 2677 | something reasonable to get a readable error message during parsing |
| 2678 | */ |
| 2679 | dst->str= "NULL" ; |
| 2680 | dst->length= 4; |
| 2681 | return false; |
| 2682 | } |
| 2683 | |
| 2684 | /* |
| 2685 | Character set conversion notes: |
| 2686 | |
| 2687 | 1) When PREPARE or EXECUTE IMMEDIATE are used with string literals: |
| 2688 | PREPARE stmt FROM 'SELECT ''str'''; |
| 2689 | EXECUTE IMMEDIATE 'SELECT ''str'''; |
| 2690 | it's very unlikely that any conversion will happen below, because |
| 2691 | @@character_set_client and @@collation_connection are normally |
| 2692 | set to the same CHARSET_INFO pointer. |
| 2693 | |
| 2694 | In tricky environments when @@collation_connection is set to something |
| 2695 | different from @@character_set_client, double conversion may happen: |
| 2696 | - When the parser scans the string literal |
| 2697 | (sql_yacc.yy rules "prepare_src" -> "expr" -> ... -> "text_literal") |
| 2698 | it will convert 'str' from @@character_set_client to |
| 2699 | @@collation_connection. |
| 2700 | - Then in the code below will convert 'str' from @@collation_connection |
| 2701 | back to @@character_set_client. |
| 2702 | |
| 2703 | 2) When PREPARE or EXECUTE IMMEDIATE is used with a user variable, |
| 2704 | it should work about the same way, because user variables are usually |
| 2705 | assigned like this: |
| 2706 | SET @str='str'; |
| 2707 | and thus have the same character set with string literals. |
| 2708 | |
| 2709 | 3) When PREPARE or EXECUTE IMMEDIATE is used with some |
| 2710 | more complex expression, conversion will depend on this expression. |
| 2711 | For example, a concatenation of string literals: |
| 2712 | EXECUTE IMMEDIATE 'SELECT * FROM'||'t1'; |
| 2713 | should work the same way with just a single literal, |
| 2714 | so no conversion normally. |
| 2715 | */ |
| 2716 | CHARSET_INFO *to_cs= thd->variables.character_set_client; |
| 2717 | |
| 2718 | uint32 unused; |
| 2719 | if (String::needs_conversion(str->length(), str->charset(), to_cs, &unused)) |
| 2720 | { |
| 2721 | if (!(dst->str= sql_strmake_with_convert(thd, str->ptr(), str->length(), |
| 2722 | str->charset(), UINT_MAX32, |
| 2723 | to_cs, &dst->length))) |
| 2724 | { |
| 2725 | dst->length= 0; |
| 2726 | return true; |
| 2727 | } |
| 2728 | DBUG_ASSERT(dst->length <= UINT_MAX32); |
| 2729 | return false; |
| 2730 | } |
| 2731 | dst->str= str->ptr(); |
| 2732 | dst->length= str->length(); |
| 2733 | return false; |
| 2734 | } |
| 2735 | |
| 2736 | |
| 2737 | /** |
| 2738 | SQLCOM_PREPARE implementation. |
| 2739 | |
| 2740 | Prepare an SQL prepared statement. This is called from |
| 2741 | mysql_execute_command and should therefore behave like an |
| 2742 | ordinary query (e.g. should not reset any global THD data). |
| 2743 | |
| 2744 | @param thd thread handle |
| 2745 | |
| 2746 | @return |
| 2747 | none: in case of success, OK packet is sent to the client, |
| 2748 | otherwise an error message is set in THD |
| 2749 | */ |
| 2750 | |
| 2751 | void mysql_sql_stmt_prepare(THD *thd) |
| 2752 | { |
| 2753 | LEX *lex= thd->lex; |
| 2754 | LEX_CSTRING *name= &lex->prepared_stmt_name; |
| 2755 | Prepared_statement *stmt; |
| 2756 | LEX_CSTRING query; |
| 2757 | DBUG_ENTER("mysql_sql_stmt_prepare" ); |
| 2758 | |
| 2759 | if ((stmt= (Prepared_statement*) thd->stmt_map.find_by_name(name))) |
| 2760 | { |
| 2761 | /* |
| 2762 | If there is a statement with the same name, remove it. It is ok to |
| 2763 | remove old and fail to insert a new one at the same time. |
| 2764 | */ |
| 2765 | if (stmt->is_in_use()) |
| 2766 | { |
| 2767 | my_error(ER_PS_NO_RECURSION, MYF(0)); |
| 2768 | DBUG_VOID_RETURN; |
| 2769 | } |
| 2770 | |
| 2771 | stmt->deallocate(); |
| 2772 | } |
| 2773 | |
| 2774 | /* |
| 2775 | It's important for "buffer" not to be destructed before stmt->prepare()! |
| 2776 | See comments in get_dynamic_sql_string(). |
| 2777 | */ |
| 2778 | StringBuffer<256> buffer; |
| 2779 | if (lex->get_dynamic_sql_string(&query, &buffer) || |
| 2780 | ! (stmt= new Prepared_statement(thd))) |
| 2781 | { |
| 2782 | DBUG_VOID_RETURN; /* out of memory */ |
| 2783 | } |
| 2784 | |
| 2785 | stmt->set_sql_prepare(); |
| 2786 | |
| 2787 | /* Set the name first, insert should know that this statement has a name */ |
| 2788 | if (stmt->set_name(name)) |
| 2789 | { |
| 2790 | delete stmt; |
| 2791 | DBUG_VOID_RETURN; |
| 2792 | } |
| 2793 | |
| 2794 | if (thd->stmt_map.insert(thd, stmt)) |
| 2795 | { |
| 2796 | /* The statement is deleted and an error is set if insert fails */ |
| 2797 | DBUG_VOID_RETURN; |
| 2798 | } |
| 2799 | |
| 2800 | /* |
| 2801 | Make sure we call Prepared_statement::prepare() with an empty |
| 2802 | THD::change_list. It can be non-empty as LEX::get_dynamic_sql_string() |
| 2803 | calls fix_fields() for the Item containing the PS source, |
| 2804 | e.g. on character set conversion: |
| 2805 | |
| 2806 | SET NAMES utf8; |
| 2807 | DELIMITER $$ |
| 2808 | CREATE PROCEDURE p1() |
| 2809 | BEGIN |
| 2810 | PREPARE stmt FROM CONCAT('SELECT ',CONVERT(RAND() USING latin1)); |
| 2811 | EXECUTE stmt; |
| 2812 | END; |
| 2813 | $$ |
| 2814 | DELIMITER ; |
| 2815 | CALL p1(); |
| 2816 | */ |
| 2817 | Item_change_list_savepoint change_list_savepoint(thd); |
| 2818 | |
| 2819 | if (stmt->prepare(query.str, (uint) query.length)) |
| 2820 | { |
| 2821 | /* Statement map deletes the statement on erase */ |
| 2822 | thd->stmt_map.erase(stmt); |
| 2823 | } |
| 2824 | else |
| 2825 | { |
| 2826 | SESSION_TRACKER_CHANGED(thd, SESSION_STATE_CHANGE_TRACKER, NULL); |
| 2827 | my_ok(thd, 0L, 0L, "Statement prepared" ); |
| 2828 | } |
| 2829 | change_list_savepoint.rollback(thd); |
| 2830 | |
| 2831 | DBUG_VOID_RETURN; |
| 2832 | } |
| 2833 | |
| 2834 | |
| 2835 | void mysql_sql_stmt_execute_immediate(THD *thd) |
| 2836 | { |
| 2837 | LEX *lex= thd->lex; |
| 2838 | Prepared_statement *stmt; |
| 2839 | LEX_CSTRING query; |
| 2840 | DBUG_ENTER("mysql_sql_stmt_execute_immediate" ); |
| 2841 | |
| 2842 | if (lex->prepared_stmt_params_fix_fields(thd)) |
| 2843 | DBUG_VOID_RETURN; |
| 2844 | |
| 2845 | /* |
| 2846 | Prepared_statement is quite large, |
| 2847 | let's allocate it on the heap rather than on the stack. |
| 2848 | |
| 2849 | It's important for "buffer" not to be destructed |
| 2850 | before stmt->execute_immediate(). |
| 2851 | See comments in get_dynamic_sql_string(). |
| 2852 | */ |
| 2853 | StringBuffer<256> buffer; |
| 2854 | if (lex->get_dynamic_sql_string(&query, &buffer) || |
| 2855 | !(stmt= new Prepared_statement(thd))) |
| 2856 | DBUG_VOID_RETURN; // out of memory |
| 2857 | |
| 2858 | // See comments on thd->free_list in mysql_sql_stmt_execute() |
| 2859 | Item *free_list_backup= thd->free_list; |
| 2860 | thd->free_list= NULL; |
| 2861 | /* |
| 2862 | Make sure we call Prepared_statement::execute_immediate() |
| 2863 | with an empty THD::change_list. It can be non empty as the above |
| 2864 | LEX::prepared_stmt_params_fix_fields() and LEX::get_dynamic_str_string() |
| 2865 | call fix_fields() for the PS source and PS parameter Items and |
| 2866 | can do Item tree changes, e.g. on character set conversion: |
| 2867 | |
| 2868 | - Example #1: Item tree changes in get_dynamic_str_string() |
| 2869 | SET NAMES utf8; |
| 2870 | CREATE PROCEDURE p1() |
| 2871 | EXECUTE IMMEDIATE CONCAT('SELECT ',CONVERT(RAND() USING latin1)); |
| 2872 | CALL p1(); |
| 2873 | |
| 2874 | - Example #2: Item tree changes in prepared_stmt_param_fix_fields(): |
| 2875 | SET NAMES utf8; |
| 2876 | CREATE PROCEDURE p1(a VARCHAR(10) CHARACTER SET utf8) |
| 2877 | EXECUTE IMMEDIATE 'SELECT ?' USING CONCAT(a, CONVERT(RAND() USING latin1)); |
| 2878 | CALL p1('x'); |
| 2879 | */ |
| 2880 | Item_change_list_savepoint change_list_savepoint(thd); |
| 2881 | (void) stmt->execute_immediate(query.str, (uint) query.length); |
| 2882 | change_list_savepoint.rollback(thd); |
| 2883 | thd->free_items(); |
| 2884 | thd->free_list= free_list_backup; |
| 2885 | |
| 2886 | stmt->lex->restore_set_statement_var(); |
| 2887 | delete stmt; |
| 2888 | DBUG_VOID_RETURN; |
| 2889 | } |
| 2890 | |
| 2891 | |
| 2892 | /** |
| 2893 | Reinit prepared statement/stored procedure before execution. |
| 2894 | |
| 2895 | @todo |
| 2896 | When the new table structure is ready, then have a status bit |
| 2897 | to indicate the table is altered, and re-do the setup_* |
| 2898 | and open the tables back. |
| 2899 | */ |
| 2900 | |
| 2901 | void reinit_stmt_before_use(THD *thd, LEX *lex) |
| 2902 | { |
| 2903 | SELECT_LEX *sl= lex->all_selects_list; |
| 2904 | DBUG_ENTER("reinit_stmt_before_use" ); |
| 2905 | |
| 2906 | /* |
| 2907 | We have to update "thd" pointer in LEX, all its units and in LEX::result, |
| 2908 | since statements which belong to trigger body are associated with TABLE |
| 2909 | object and because of this can be used in different threads. |
| 2910 | */ |
| 2911 | lex->thd= thd; |
| 2912 | DBUG_ASSERT(!lex->explain); |
| 2913 | |
| 2914 | if (lex->empty_field_list_on_rset) |
| 2915 | { |
| 2916 | lex->empty_field_list_on_rset= 0; |
| 2917 | lex->field_list.empty(); |
| 2918 | } |
| 2919 | for (; sl; sl= sl->next_select_in_list()) |
| 2920 | { |
| 2921 | if (!sl->first_execution) |
| 2922 | { |
| 2923 | /* remove option which was put by mysql_explain_union() */ |
| 2924 | sl->options&= ~SELECT_DESCRIBE; |
| 2925 | |
| 2926 | /* see unique_table() */ |
| 2927 | sl->exclude_from_table_unique_test= FALSE; |
| 2928 | |
| 2929 | /* |
| 2930 | Copy WHERE, HAVING clause pointers to avoid damaging them |
| 2931 | by optimisation |
| 2932 | */ |
| 2933 | if (sl->prep_where) |
| 2934 | { |
| 2935 | /* |
| 2936 | We need this rollback because memory allocated in |
| 2937 | copy_andor_structure() will be freed |
| 2938 | */ |
| 2939 | thd->change_item_tree((Item**)&sl->where, |
| 2940 | sl->prep_where->copy_andor_structure(thd)); |
| 2941 | sl->where->cleanup(); |
| 2942 | } |
| 2943 | else |
| 2944 | sl->where= NULL; |
| 2945 | if (sl->prep_having) |
| 2946 | { |
| 2947 | /* |
| 2948 | We need this rollback because memory allocated in |
| 2949 | copy_andor_structure() will be freed |
| 2950 | */ |
| 2951 | thd->change_item_tree((Item**)&sl->having, |
| 2952 | sl->prep_having->copy_andor_structure(thd)); |
| 2953 | sl->having->cleanup(); |
| 2954 | } |
| 2955 | else |
| 2956 | sl->having= NULL; |
| 2957 | DBUG_ASSERT(sl->join == 0); |
| 2958 | ORDER *order; |
| 2959 | /* Fix GROUP list */ |
| 2960 | if (sl->group_list_ptrs && sl->group_list_ptrs->size() > 0) |
| 2961 | { |
| 2962 | for (uint ix= 0; ix < sl->group_list_ptrs->size() - 1; ++ix) |
| 2963 | { |
| 2964 | order= sl->group_list_ptrs->at(ix); |
| 2965 | order->next= sl->group_list_ptrs->at(ix+1); |
| 2966 | } |
| 2967 | } |
| 2968 | for (order= sl->group_list.first; order; order= order->next) |
| 2969 | order->item= &order->item_ptr; |
| 2970 | /* Fix ORDER list */ |
| 2971 | for (order= sl->order_list.first; order; order= order->next) |
| 2972 | order->item= &order->item_ptr; |
| 2973 | { |
| 2974 | #ifdef DBUG_ASSERT_EXISTS |
| 2975 | bool res= |
| 2976 | #endif |
| 2977 | sl->handle_derived(lex, DT_REINIT); |
| 2978 | DBUG_ASSERT(res == 0); |
| 2979 | } |
| 2980 | } |
| 2981 | { |
| 2982 | SELECT_LEX_UNIT *unit= sl->master_unit(); |
| 2983 | unit->unclean(); |
| 2984 | unit->types.empty(); |
| 2985 | /* for derived tables & PS (which can't be reset by Item_subselect) */ |
| 2986 | unit->reinit_exec_mechanism(); |
| 2987 | unit->set_thd(thd); |
| 2988 | } |
| 2989 | } |
| 2990 | |
| 2991 | /* |
| 2992 | TODO: When the new table structure is ready, then have a status bit |
| 2993 | to indicate the table is altered, and re-do the setup_* |
| 2994 | and open the tables back. |
| 2995 | */ |
| 2996 | /* |
| 2997 | NOTE: We should reset whole table list here including all tables added |
| 2998 | by prelocking algorithm (it is not a problem for substatements since |
| 2999 | they have their own table list). |
| 3000 | */ |
| 3001 | for (TABLE_LIST *tables= lex->query_tables; |
| 3002 | tables; |
| 3003 | tables= tables->next_global) |
| 3004 | { |
| 3005 | tables->reinit_before_use(thd); |
| 3006 | } |
| 3007 | |
| 3008 | /* Reset MDL tickets for procedures/functions */ |
| 3009 | for (Sroutine_hash_entry *rt= |
| 3010 | (Sroutine_hash_entry*)thd->lex->sroutines_list.first; |
| 3011 | rt; rt= rt->next) |
| 3012 | rt->mdl_request.ticket= NULL; |
| 3013 | |
| 3014 | /* |
| 3015 | Cleanup of the special case of DELETE t1, t2 FROM t1, t2, t3 ... |
| 3016 | (multi-delete). We do a full clean up, although at the moment all we |
| 3017 | need to clean in the tables of MULTI-DELETE list is 'table' member. |
| 3018 | */ |
| 3019 | for (TABLE_LIST *tables= lex->auxiliary_table_list.first; |
| 3020 | tables; |
| 3021 | tables= tables->next_global) |
| 3022 | { |
| 3023 | tables->reinit_before_use(thd); |
| 3024 | } |
| 3025 | lex->current_select= &lex->select_lex; |
| 3026 | |
| 3027 | |
| 3028 | if (lex->result) |
| 3029 | { |
| 3030 | lex->result->cleanup(); |
| 3031 | lex->result->set_thd(thd); |
| 3032 | } |
| 3033 | lex->allow_sum_func= 0; |
| 3034 | lex->in_sum_func= NULL; |
| 3035 | DBUG_VOID_RETURN; |
| 3036 | } |
| 3037 | |
| 3038 | |
| 3039 | /** |
| 3040 | Clears parameters from data left from previous execution or long data. |
| 3041 | |
| 3042 | @param stmt prepared statement for which parameters should |
| 3043 | be reset |
| 3044 | */ |
| 3045 | |
| 3046 | static void reset_stmt_params(Prepared_statement *stmt) |
| 3047 | { |
| 3048 | Item_param **item= stmt->param_array; |
| 3049 | Item_param **end= item + stmt->param_count; |
| 3050 | for (;item < end ; ++item) |
| 3051 | (**item).reset(); |
| 3052 | } |
| 3053 | |
| 3054 | |
| 3055 | static void mysql_stmt_execute_common(THD *thd, |
| 3056 | ulong stmt_id, |
| 3057 | uchar *packet, |
| 3058 | uchar *packet_end, |
| 3059 | ulong cursor_flags, |
| 3060 | bool iteration, |
| 3061 | bool types); |
| 3062 | |
| 3063 | /** |
| 3064 | COM_STMT_EXECUTE handler: execute a previously prepared statement. |
| 3065 | |
| 3066 | If there are any parameters, then replace parameter markers with the |
| 3067 | data supplied from the client, and then execute the statement. |
| 3068 | This function uses binary protocol to send a possible result set |
| 3069 | to the client. |
| 3070 | |
| 3071 | @param thd current thread |
| 3072 | @param packet_arg parameter types and data, if any |
| 3073 | @param packet_length packet length, including the terminator character. |
| 3074 | |
| 3075 | @return |
| 3076 | none: in case of success OK packet or a result set is sent to the |
| 3077 | client, otherwise an error message is set in THD. |
| 3078 | */ |
| 3079 | |
| 3080 | void mysqld_stmt_execute(THD *thd, char *packet_arg, uint packet_length) |
| 3081 | { |
| 3082 | uchar *packet= (uchar*)packet_arg; // GCC 4.0.1 workaround |
| 3083 | ulong stmt_id= uint4korr(packet); |
| 3084 | ulong flags= (ulong) packet[4]; |
| 3085 | uchar *packet_end= packet + packet_length; |
| 3086 | DBUG_ENTER("mysqld_stmt_execute" ); |
| 3087 | |
| 3088 | packet+= 9; /* stmt_id + 5 bytes of flags */ |
| 3089 | |
| 3090 | mysql_stmt_execute_common(thd, stmt_id, packet, packet_end, flags, FALSE, |
| 3091 | FALSE); |
| 3092 | DBUG_VOID_RETURN; |
| 3093 | } |
| 3094 | |
| 3095 | |
| 3096 | /** |
| 3097 | COM_STMT_BULK_EXECUTE handler: execute a previously prepared statement. |
| 3098 | |
| 3099 | If there are any parameters, then replace parameter markers with the |
| 3100 | data supplied from the client, and then execute the statement. |
| 3101 | This function uses binary protocol to send a possible result set |
| 3102 | to the client. |
| 3103 | |
| 3104 | @param thd current thread |
| 3105 | @param packet_arg parameter types and data, if any |
| 3106 | @param packet_length packet length, including the terminator character. |
| 3107 | |
| 3108 | @return |
| 3109 | none: in case of success OK packet or a result set is sent to the |
| 3110 | client, otherwise an error message is set in THD. |
| 3111 | */ |
| 3112 | |
| 3113 | void mysqld_stmt_bulk_execute(THD *thd, char *packet_arg, uint packet_length) |
| 3114 | { |
| 3115 | uchar *packet= (uchar*)packet_arg; // GCC 4.0.1 workaround |
| 3116 | ulong stmt_id= uint4korr(packet); |
| 3117 | uint flags= (uint) uint2korr(packet + 4); |
| 3118 | uchar *packet_end= packet + packet_length; |
| 3119 | DBUG_ENTER("mysqld_stmt_execute_bulk" ); |
| 3120 | |
| 3121 | if (!(thd->client_capabilities & |
| 3122 | MARIADB_CLIENT_STMT_BULK_OPERATIONS)) |
| 3123 | { |
| 3124 | DBUG_PRINT("error" , |
| 3125 | ("An attempt to execute bulk operation without support" )); |
| 3126 | my_error(ER_UNSUPPORTED_PS, MYF(0)); |
| 3127 | } |
| 3128 | /* Check for implemented parameters */ |
| 3129 | if (flags & (~STMT_BULK_FLAG_CLIENT_SEND_TYPES)) |
| 3130 | { |
| 3131 | DBUG_PRINT("error" , ("unsupported bulk execute flags %x" , flags)); |
| 3132 | my_error(ER_UNSUPPORTED_PS, MYF(0)); |
| 3133 | } |
| 3134 | |
| 3135 | /* stmt id and two bytes of flags */ |
| 3136 | packet+= 4 + 2; |
| 3137 | mysql_stmt_execute_common(thd, stmt_id, packet, packet_end, 0, TRUE, |
| 3138 | (flags & STMT_BULK_FLAG_CLIENT_SEND_TYPES)); |
| 3139 | DBUG_VOID_RETURN; |
| 3140 | } |
| 3141 | |
| 3142 | |
| 3143 | /** |
| 3144 | Common part of prepared statement execution |
| 3145 | |
| 3146 | @param thd THD handle |
| 3147 | @param stmt_id id of the prepared statement |
| 3148 | @param paket packet with parameters to bind |
| 3149 | @param packet_end pointer to the byte after parameters end |
| 3150 | @param cursor_flags cursor flags |
| 3151 | @param bulk_op id it bulk operation |
| 3152 | @param read_types flag say that types muast been read |
| 3153 | */ |
| 3154 | |
| 3155 | static void mysql_stmt_execute_common(THD *thd, |
| 3156 | ulong stmt_id, |
| 3157 | uchar *packet, |
| 3158 | uchar *packet_end, |
| 3159 | ulong cursor_flags, |
| 3160 | bool bulk_op, |
| 3161 | bool read_types) |
| 3162 | { |
| 3163 | /* Query text for binary, general or slow log, if any of them is open */ |
| 3164 | String expanded_query; |
| 3165 | Prepared_statement *stmt; |
| 3166 | Protocol *save_protocol= thd->protocol; |
| 3167 | bool open_cursor; |
| 3168 | DBUG_ENTER("mysqld_stmt_execute_common" ); |
| 3169 | DBUG_ASSERT((!read_types) || (read_types && bulk_op)); |
| 3170 | |
| 3171 | /* First of all clear possible warnings from the previous command */ |
| 3172 | thd->reset_for_next_command(); |
| 3173 | |
| 3174 | if (!(stmt= find_prepared_statement(thd, stmt_id))) |
| 3175 | { |
| 3176 | char llbuf[22]; |
| 3177 | my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), static_cast<int>(sizeof(llbuf)), |
| 3178 | llstr(stmt_id, llbuf), "mysqld_stmt_execute" ); |
| 3179 | DBUG_VOID_RETURN; |
| 3180 | } |
| 3181 | stmt->read_types= read_types; |
| 3182 | |
| 3183 | #if defined(ENABLED_PROFILING) |
| 3184 | thd->profiling.set_query_source(stmt->query(), stmt->query_length()); |
| 3185 | #endif |
| 3186 | DBUG_PRINT("exec_query" , ("%s" , stmt->query())); |
| 3187 | DBUG_PRINT("info" ,("stmt: %p bulk_op %d" , stmt, bulk_op)); |
| 3188 | |
| 3189 | open_cursor= MY_TEST(cursor_flags & (ulong) CURSOR_TYPE_READ_ONLY); |
| 3190 | |
| 3191 | thd->protocol= &thd->protocol_binary; |
| 3192 | if (!bulk_op) |
| 3193 | stmt->execute_loop(&expanded_query, open_cursor, packet, packet_end); |
| 3194 | else |
| 3195 | stmt->execute_bulk_loop(&expanded_query, open_cursor, packet, packet_end); |
| 3196 | thd->protocol= save_protocol; |
| 3197 | |
| 3198 | sp_cache_enforce_limit(thd->sp_proc_cache, stored_program_cache_size); |
| 3199 | sp_cache_enforce_limit(thd->sp_func_cache, stored_program_cache_size); |
| 3200 | sp_cache_enforce_limit(thd->sp_package_spec_cache, stored_program_cache_size); |
| 3201 | sp_cache_enforce_limit(thd->sp_package_body_cache, stored_program_cache_size); |
| 3202 | |
| 3203 | /* Close connection socket; for use with client testing (Bug#43560). */ |
| 3204 | DBUG_EXECUTE_IF("close_conn_after_stmt_execute" , vio_close(thd->net.vio);); |
| 3205 | |
| 3206 | DBUG_VOID_RETURN; |
| 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | /** |
| 3211 | SQLCOM_EXECUTE implementation. |
| 3212 | |
| 3213 | Execute prepared statement using parameter values from |
| 3214 | lex->prepared_stmt_params and send result to the client using |
| 3215 | text protocol. This is called from mysql_execute_command and |
| 3216 | therefore should behave like an ordinary query (e.g. not change |
| 3217 | global THD data, such as warning count, server status, etc). |
| 3218 | This function uses text protocol to send a possible result set. |
| 3219 | |
| 3220 | @param thd thread handle |
| 3221 | |
| 3222 | @return |
| 3223 | none: in case of success, OK (or result set) packet is sent to the |
| 3224 | client, otherwise an error is set in THD |
| 3225 | */ |
| 3226 | |
| 3227 | void mysql_sql_stmt_execute(THD *thd) |
| 3228 | { |
| 3229 | LEX *lex= thd->lex; |
| 3230 | Prepared_statement *stmt; |
| 3231 | LEX_CSTRING *name= &lex->prepared_stmt_name; |
| 3232 | /* Query text for binary, general or slow log, if any of them is open */ |
| 3233 | String expanded_query; |
| 3234 | DBUG_ENTER("mysql_sql_stmt_execute" ); |
| 3235 | DBUG_PRINT("info" , ("EXECUTE: %.*s\n" , (int) name->length, name->str)); |
| 3236 | |
| 3237 | if (!(stmt= (Prepared_statement*) thd->stmt_map.find_by_name(name))) |
| 3238 | { |
| 3239 | my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), |
| 3240 | static_cast<int>(name->length), name->str, "EXECUTE" ); |
| 3241 | DBUG_VOID_RETURN; |
| 3242 | } |
| 3243 | |
| 3244 | if (stmt->param_count != lex->prepared_stmt_params.elements) |
| 3245 | { |
| 3246 | my_error(ER_WRONG_ARGUMENTS, MYF(0), "EXECUTE" ); |
| 3247 | DBUG_VOID_RETURN; |
| 3248 | } |
| 3249 | |
| 3250 | DBUG_PRINT("info" ,("stmt: %p" , stmt)); |
| 3251 | |
| 3252 | if (lex->prepared_stmt_params_fix_fields(thd)) |
| 3253 | DBUG_VOID_RETURN; |
| 3254 | |
| 3255 | /* |
| 3256 | thd->free_list can already have some Items. |
| 3257 | |
| 3258 | Example queries: |
| 3259 | - SET STATEMENT var=expr FOR EXECUTE stmt; |
| 3260 | - EXECUTE stmt USING expr; |
| 3261 | |
| 3262 | E.g. for a query like this: |
| 3263 | PREPARE stmt FROM 'INSERT INTO t1 VALUES (@@max_sort_length)'; |
| 3264 | SET STATEMENT max_sort_length=2048 FOR EXECUTE stmt; |
| 3265 | thd->free_list contains a pointer to Item_int corresponding to 2048. |
| 3266 | |
| 3267 | If Prepared_statement::execute() notices that the table metadata for "t1" |
| 3268 | has changed since PREPARE, it returns an error asking the calling |
| 3269 | Prepared_statement::execute_loop() to re-prepare the statement. |
| 3270 | Before returning the error, Prepared_statement::execute() |
| 3271 | calls Prepared_statement::cleanup_stmt(), |
| 3272 | which calls thd->cleanup_after_query(), |
| 3273 | which calls Query_arena::free_items(). |
| 3274 | |
| 3275 | We hide "external" Items, e.g. those created while parsing the |
| 3276 | "SET STATEMENT" or "USING" parts of the query, |
| 3277 | so they don't get freed in case of re-prepare. |
| 3278 | See MDEV-10702 Crash in SET STATEMENT FOR EXECUTE |
| 3279 | */ |
| 3280 | Item *free_list_backup= thd->free_list; |
| 3281 | thd->free_list= NULL; // Hide the external (e.g. "SET STATEMENT") Items |
| 3282 | /* |
| 3283 | Make sure we call Prepared_statement::execute_loop() with an empty |
| 3284 | THD::change_list. It can be non-empty because the above |
| 3285 | LEX::prepared_stmt_params_fix_fields() calls fix_fields() for |
| 3286 | the PS parameter Items and can do some Item tree changes, |
| 3287 | e.g. on character set conversion: |
| 3288 | |
| 3289 | SET NAMES utf8; |
| 3290 | DELIMITER $$ |
| 3291 | CREATE PROCEDURE p1(a VARCHAR(10) CHARACTER SET utf8) |
| 3292 | BEGIN |
| 3293 | PREPARE stmt FROM 'SELECT ?'; |
| 3294 | EXECUTE stmt USING CONCAT(a, CONVERT(RAND() USING latin1)); |
| 3295 | END; |
| 3296 | $$ |
| 3297 | DELIMITER ; |
| 3298 | CALL p1('x'); |
| 3299 | */ |
| 3300 | Item_change_list_savepoint change_list_savepoint(thd); |
| 3301 | (void) stmt->execute_loop(&expanded_query, FALSE, NULL, NULL); |
| 3302 | change_list_savepoint.rollback(thd); |
| 3303 | thd->free_items(); // Free items created by execute_loop() |
| 3304 | /* |
| 3305 | Now restore the "external" (e.g. "SET STATEMENT") Item list. |
| 3306 | It will be freed normaly in THD::cleanup_after_query(). |
| 3307 | */ |
| 3308 | thd->free_list= free_list_backup; |
| 3309 | |
| 3310 | stmt->lex->restore_set_statement_var(); |
| 3311 | DBUG_VOID_RETURN; |
| 3312 | } |
| 3313 | |
| 3314 | |
| 3315 | /** |
| 3316 | COM_STMT_FETCH handler: fetches requested amount of rows from cursor. |
| 3317 | |
| 3318 | @param thd Thread handle |
| 3319 | @param packet Packet from client (with stmt_id & num_rows) |
| 3320 | @param packet_length Length of packet |
| 3321 | */ |
| 3322 | |
| 3323 | void mysqld_stmt_fetch(THD *thd, char *packet, uint packet_length) |
| 3324 | { |
| 3325 | /* assume there is always place for 8-16 bytes */ |
| 3326 | ulong stmt_id= uint4korr(packet); |
| 3327 | ulong num_rows= uint4korr(packet+4); |
| 3328 | Prepared_statement *stmt; |
| 3329 | Statement stmt_backup; |
| 3330 | Server_side_cursor *cursor; |
| 3331 | DBUG_ENTER("mysqld_stmt_fetch" ); |
| 3332 | |
| 3333 | /* First of all clear possible warnings from the previous command */ |
| 3334 | thd->reset_for_next_command(); |
| 3335 | |
| 3336 | status_var_increment(thd->status_var.com_stmt_fetch); |
| 3337 | if (!(stmt= find_prepared_statement(thd, stmt_id))) |
| 3338 | { |
| 3339 | char llbuf[22]; |
| 3340 | my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), static_cast<int>(sizeof(llbuf)), |
| 3341 | llstr(stmt_id, llbuf), "mysqld_stmt_fetch" ); |
| 3342 | DBUG_VOID_RETURN; |
| 3343 | } |
| 3344 | |
| 3345 | cursor= stmt->cursor; |
| 3346 | if (!cursor) |
| 3347 | { |
| 3348 | my_error(ER_STMT_HAS_NO_OPEN_CURSOR, MYF(0), stmt_id); |
| 3349 | DBUG_VOID_RETURN; |
| 3350 | } |
| 3351 | |
| 3352 | thd->stmt_arena= stmt; |
| 3353 | thd->set_n_backup_statement(stmt, &stmt_backup); |
| 3354 | |
| 3355 | cursor->fetch(num_rows); |
| 3356 | |
| 3357 | if (!cursor->is_open()) |
| 3358 | { |
| 3359 | stmt->close_cursor(); |
| 3360 | reset_stmt_params(stmt); |
| 3361 | } |
| 3362 | |
| 3363 | thd->restore_backup_statement(stmt, &stmt_backup); |
| 3364 | thd->stmt_arena= thd; |
| 3365 | |
| 3366 | DBUG_VOID_RETURN; |
| 3367 | } |
| 3368 | |
| 3369 | |
| 3370 | /** |
| 3371 | Reset a prepared statement in case there was a recoverable error. |
| 3372 | |
| 3373 | This function resets statement to the state it was right after prepare. |
| 3374 | It can be used to: |
| 3375 | - clear an error happened during mysqld_stmt_send_long_data |
| 3376 | - cancel long data stream for all placeholders without |
| 3377 | having to call mysqld_stmt_execute. |
| 3378 | - close an open cursor |
| 3379 | Sends 'OK' packet in case of success (statement was reset) |
| 3380 | or 'ERROR' packet (unrecoverable error/statement not found/etc). |
| 3381 | |
| 3382 | @param thd Thread handle |
| 3383 | @param packet Packet with stmt id |
| 3384 | */ |
| 3385 | |
| 3386 | void mysqld_stmt_reset(THD *thd, char *packet) |
| 3387 | { |
| 3388 | /* There is always space for 4 bytes in buffer */ |
| 3389 | ulong stmt_id= uint4korr(packet); |
| 3390 | Prepared_statement *stmt; |
| 3391 | DBUG_ENTER("mysqld_stmt_reset" ); |
| 3392 | |
| 3393 | /* First of all clear possible warnings from the previous command */ |
| 3394 | thd->reset_for_next_command(); |
| 3395 | |
| 3396 | status_var_increment(thd->status_var.com_stmt_reset); |
| 3397 | if (!(stmt= find_prepared_statement(thd, stmt_id))) |
| 3398 | { |
| 3399 | char llbuf[22]; |
| 3400 | my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), static_cast<int>(sizeof(llbuf)), |
| 3401 | llstr(stmt_id, llbuf), "mysqld_stmt_reset" ); |
| 3402 | DBUG_VOID_RETURN; |
| 3403 | } |
| 3404 | |
| 3405 | stmt->close_cursor(); |
| 3406 | |
| 3407 | /* |
| 3408 | Clear parameters from data which could be set by |
| 3409 | mysqld_stmt_send_long_data() call. |
| 3410 | */ |
| 3411 | reset_stmt_params(stmt); |
| 3412 | |
| 3413 | stmt->state= Query_arena::STMT_PREPARED; |
| 3414 | |
| 3415 | general_log_print(thd, thd->get_command(), NullS); |
| 3416 | |
| 3417 | my_ok(thd); |
| 3418 | |
| 3419 | DBUG_VOID_RETURN; |
| 3420 | } |
| 3421 | |
| 3422 | |
| 3423 | /** |
| 3424 | Delete a prepared statement from memory. |
| 3425 | |
| 3426 | @note |
| 3427 | we don't send any reply to this command. |
| 3428 | */ |
| 3429 | |
| 3430 | void mysqld_stmt_close(THD *thd, char *packet) |
| 3431 | { |
| 3432 | /* There is always space for 4 bytes in packet buffer */ |
| 3433 | ulong stmt_id= uint4korr(packet); |
| 3434 | Prepared_statement *stmt; |
| 3435 | DBUG_ENTER("mysqld_stmt_close" ); |
| 3436 | |
| 3437 | thd->get_stmt_da()->disable_status(); |
| 3438 | |
| 3439 | if (!(stmt= find_prepared_statement(thd, stmt_id))) |
| 3440 | DBUG_VOID_RETURN; |
| 3441 | |
| 3442 | /* |
| 3443 | The only way currently a statement can be deallocated when it's |
| 3444 | in use is from within Dynamic SQL. |
| 3445 | */ |
| 3446 | DBUG_ASSERT(! stmt->is_in_use()); |
| 3447 | stmt->deallocate(); |
| 3448 | general_log_print(thd, thd->get_command(), NullS); |
| 3449 | |
| 3450 | if (thd->last_stmt == stmt) |
| 3451 | thd->clear_last_stmt(); |
| 3452 | |
| 3453 | DBUG_VOID_RETURN; |
| 3454 | } |
| 3455 | |
| 3456 | |
| 3457 | /** |
| 3458 | SQLCOM_DEALLOCATE implementation. |
| 3459 | |
| 3460 | Close an SQL prepared statement. As this can be called from Dynamic |
| 3461 | SQL, we should be careful to not close a statement that is currently |
| 3462 | being executed. |
| 3463 | |
| 3464 | @return |
| 3465 | none: OK packet is sent in case of success, otherwise an error |
| 3466 | message is set in THD |
| 3467 | */ |
| 3468 | |
| 3469 | void mysql_sql_stmt_close(THD *thd) |
| 3470 | { |
| 3471 | Prepared_statement* stmt; |
| 3472 | LEX_CSTRING *name= &thd->lex->prepared_stmt_name; |
| 3473 | DBUG_PRINT("info" , ("DEALLOCATE PREPARE: %.*s\n" , (int) name->length, |
| 3474 | name->str)); |
| 3475 | |
| 3476 | if (! (stmt= (Prepared_statement*) thd->stmt_map.find_by_name(name))) |
| 3477 | my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), |
| 3478 | static_cast<int>(name->length), name->str, "DEALLOCATE PREPARE" ); |
| 3479 | else if (stmt->is_in_use()) |
| 3480 | my_error(ER_PS_NO_RECURSION, MYF(0)); |
| 3481 | else |
| 3482 | { |
| 3483 | stmt->deallocate(); |
| 3484 | SESSION_TRACKER_CHANGED(thd, SESSION_STATE_CHANGE_TRACKER, NULL); |
| 3485 | my_ok(thd); |
| 3486 | } |
| 3487 | } |
| 3488 | |
| 3489 | |
| 3490 | /** |
| 3491 | Handle long data in pieces from client. |
| 3492 | |
| 3493 | Get a part of a long data. To make the protocol efficient, we are |
| 3494 | not sending any return packets here. If something goes wrong, then |
| 3495 | we will send the error on 'execute' We assume that the client takes |
| 3496 | care of checking that all parts are sent to the server. (No checking |
| 3497 | that we get a 'end of column' in the server is performed). |
| 3498 | |
| 3499 | @param thd Thread handle |
| 3500 | @param packet String to append |
| 3501 | @param packet_length Length of string (including end \\0) |
| 3502 | */ |
| 3503 | |
| 3504 | void mysql_stmt_get_longdata(THD *thd, char *packet, ulong packet_length) |
| 3505 | { |
| 3506 | ulong stmt_id; |
| 3507 | uint param_number; |
| 3508 | Prepared_statement *stmt; |
| 3509 | Item_param *param; |
| 3510 | #ifndef EMBEDDED_LIBRARY |
| 3511 | char *packet_end= packet + packet_length; |
| 3512 | #endif |
| 3513 | DBUG_ENTER("mysql_stmt_get_longdata" ); |
| 3514 | |
| 3515 | status_var_increment(thd->status_var.com_stmt_send_long_data); |
| 3516 | |
| 3517 | thd->get_stmt_da()->disable_status(); |
| 3518 | #ifndef EMBEDDED_LIBRARY |
| 3519 | /* Minimal size of long data packet is 6 bytes */ |
| 3520 | if (packet_length < MYSQL_LONG_DATA_HEADER) |
| 3521 | DBUG_VOID_RETURN; |
| 3522 | #endif |
| 3523 | |
| 3524 | stmt_id= uint4korr(packet); |
| 3525 | packet+= 4; |
| 3526 | |
| 3527 | if (!(stmt=find_prepared_statement(thd, stmt_id))) |
| 3528 | DBUG_VOID_RETURN; |
| 3529 | |
| 3530 | param_number= uint2korr(packet); |
| 3531 | packet+= 2; |
| 3532 | #ifndef EMBEDDED_LIBRARY |
| 3533 | if (param_number >= stmt->param_count) |
| 3534 | { |
| 3535 | /* Error will be sent in execute call */ |
| 3536 | stmt->state= Query_arena::STMT_ERROR; |
| 3537 | stmt->last_errno= ER_WRONG_ARGUMENTS; |
| 3538 | sprintf(stmt->last_error, ER_THD(thd, ER_WRONG_ARGUMENTS), |
| 3539 | "mysqld_stmt_send_long_data" ); |
| 3540 | DBUG_VOID_RETURN; |
| 3541 | } |
| 3542 | #endif |
| 3543 | |
| 3544 | param= stmt->param_array[param_number]; |
| 3545 | |
| 3546 | Diagnostics_area new_stmt_da(thd->query_id, false, true); |
| 3547 | Diagnostics_area *save_stmt_da= thd->get_stmt_da(); |
| 3548 | |
| 3549 | thd->set_stmt_da(&new_stmt_da); |
| 3550 | |
| 3551 | #ifndef EMBEDDED_LIBRARY |
| 3552 | param->set_longdata(packet, (ulong) (packet_end - packet)); |
| 3553 | #else |
| 3554 | param->set_longdata(thd->extra_data, thd->extra_length); |
| 3555 | #endif |
| 3556 | if (unlikely(thd->get_stmt_da()->is_error())) |
| 3557 | { |
| 3558 | stmt->state= Query_arena::STMT_ERROR; |
| 3559 | stmt->last_errno= thd->get_stmt_da()->sql_errno(); |
| 3560 | strmake_buf(stmt->last_error, thd->get_stmt_da()->message()); |
| 3561 | } |
| 3562 | thd->set_stmt_da(save_stmt_da); |
| 3563 | |
| 3564 | general_log_print(thd, thd->get_command(), NullS); |
| 3565 | |
| 3566 | DBUG_VOID_RETURN; |
| 3567 | } |
| 3568 | |
| 3569 | |
| 3570 | /*************************************************************************** |
| 3571 | Select_fetch_protocol_binary |
| 3572 | ****************************************************************************/ |
| 3573 | |
| 3574 | Select_fetch_protocol_binary::Select_fetch_protocol_binary(THD *thd_arg): |
| 3575 | select_send(thd_arg), protocol(thd_arg) |
| 3576 | {} |
| 3577 | |
| 3578 | bool Select_fetch_protocol_binary::send_result_set_metadata(List<Item> &list, uint flags) |
| 3579 | { |
| 3580 | bool rc; |
| 3581 | Protocol *save_protocol= thd->protocol; |
| 3582 | |
| 3583 | /* |
| 3584 | Protocol::send_result_set_metadata caches the information about column types: |
| 3585 | this information is later used to send data. Therefore, the same |
| 3586 | dedicated Protocol object must be used for all operations with |
| 3587 | a cursor. |
| 3588 | */ |
| 3589 | thd->protocol= &protocol; |
| 3590 | rc= select_send::send_result_set_metadata(list, flags); |
| 3591 | thd->protocol= save_protocol; |
| 3592 | |
| 3593 | return rc; |
| 3594 | } |
| 3595 | |
| 3596 | bool Select_fetch_protocol_binary::send_eof() |
| 3597 | { |
| 3598 | /* |
| 3599 | Don't send EOF if we're in error condition (which implies we've already |
| 3600 | sent or are sending an error) |
| 3601 | */ |
| 3602 | if (unlikely(thd->is_error())) |
| 3603 | return true; |
| 3604 | |
| 3605 | ::my_eof(thd); |
| 3606 | return false; |
| 3607 | } |
| 3608 | |
| 3609 | |
| 3610 | int |
| 3611 | Select_fetch_protocol_binary::send_data(List<Item> &fields) |
| 3612 | { |
| 3613 | Protocol *save_protocol= thd->protocol; |
| 3614 | int rc; |
| 3615 | |
| 3616 | thd->protocol= &protocol; |
| 3617 | rc= select_send::send_data(fields); |
| 3618 | thd->protocol= save_protocol; |
| 3619 | return rc; |
| 3620 | } |
| 3621 | |
| 3622 | /******************************************************************* |
| 3623 | * Reprepare_observer |
| 3624 | *******************************************************************/ |
| 3625 | /** Push an error to the error stack and return TRUE for now. */ |
| 3626 | |
| 3627 | bool |
| 3628 | Reprepare_observer::report_error(THD *thd) |
| 3629 | { |
| 3630 | /* |
| 3631 | This 'error' is purely internal to the server: |
| 3632 | - No exception handler is invoked, |
| 3633 | - No condition is added in the condition area (warn_list). |
| 3634 | The diagnostics area is set to an error status to enforce |
| 3635 | that this thread execution stops and returns to the caller, |
| 3636 | backtracking all the way to Prepared_statement::execute_loop(). |
| 3637 | */ |
| 3638 | thd->get_stmt_da()->set_error_status(ER_NEED_REPREPARE); |
| 3639 | m_invalidated= TRUE; |
| 3640 | |
| 3641 | return TRUE; |
| 3642 | } |
| 3643 | |
| 3644 | |
| 3645 | /******************************************************************* |
| 3646 | * Server_runnable |
| 3647 | *******************************************************************/ |
| 3648 | |
| 3649 | Server_runnable::~Server_runnable() |
| 3650 | { |
| 3651 | } |
| 3652 | |
| 3653 | /////////////////////////////////////////////////////////////////////////// |
| 3654 | |
| 3655 | Execute_sql_statement:: |
| 3656 | Execute_sql_statement(LEX_STRING sql_text) |
| 3657 | :m_sql_text(sql_text) |
| 3658 | {} |
| 3659 | |
| 3660 | |
| 3661 | /** |
| 3662 | Parse and execute a statement. Does not prepare the query. |
| 3663 | |
| 3664 | Allows to execute a statement from within another statement. |
| 3665 | The main property of the implementation is that it does not |
| 3666 | affect the environment -- i.e. you can run many |
| 3667 | executions without having to cleanup/reset THD in between. |
| 3668 | */ |
| 3669 | |
| 3670 | bool |
| 3671 | Execute_sql_statement::execute_server_code(THD *thd) |
| 3672 | { |
| 3673 | PSI_statement_locker *parent_locker; |
| 3674 | bool error; |
| 3675 | |
| 3676 | if (alloc_query(thd, m_sql_text.str, m_sql_text.length)) |
| 3677 | return TRUE; |
| 3678 | |
| 3679 | Parser_state parser_state; |
| 3680 | if (parser_state.init(thd, thd->query(), thd->query_length())) |
| 3681 | return TRUE; |
| 3682 | |
| 3683 | parser_state.m_lip.multi_statements= FALSE; |
| 3684 | lex_start(thd); |
| 3685 | |
| 3686 | error= parse_sql(thd, &parser_state, NULL) || thd->is_error(); |
| 3687 | |
| 3688 | if (unlikely(error)) |
| 3689 | goto end; |
| 3690 | |
| 3691 | thd->lex->set_trg_event_type_for_tables(); |
| 3692 | |
| 3693 | parent_locker= thd->m_statement_psi; |
| 3694 | thd->m_statement_psi= NULL; |
| 3695 | error= mysql_execute_command(thd); |
| 3696 | thd->m_statement_psi= parent_locker; |
| 3697 | |
| 3698 | /* report error issued during command execution */ |
| 3699 | if (likely(error == 0) && thd->spcont == NULL) |
| 3700 | general_log_write(thd, COM_STMT_EXECUTE, |
| 3701 | thd->query(), thd->query_length()); |
| 3702 | |
| 3703 | end: |
| 3704 | thd->lex->restore_set_statement_var(); |
| 3705 | lex_end(thd->lex); |
| 3706 | |
| 3707 | return error; |
| 3708 | } |
| 3709 | |
| 3710 | /*************************************************************************** |
| 3711 | Prepared_statement |
| 3712 | ****************************************************************************/ |
| 3713 | |
| 3714 | Prepared_statement::Prepared_statement(THD *thd_arg) |
| 3715 | :Statement(NULL, &main_mem_root, |
| 3716 | STMT_INITIALIZED, |
| 3717 | ((++thd_arg->statement_id_counter) & STMT_ID_MASK)), |
| 3718 | thd(thd_arg), |
| 3719 | result(thd_arg), |
| 3720 | param_array(0), |
| 3721 | cursor(0), |
| 3722 | packet(0), |
| 3723 | packet_end(0), |
| 3724 | param_count(0), |
| 3725 | last_errno(0), |
| 3726 | flags((uint) IS_IN_USE), |
| 3727 | iterations(0), |
| 3728 | start_param(0), |
| 3729 | read_types(0), |
| 3730 | m_sql_mode(thd->variables.sql_mode) |
| 3731 | { |
| 3732 | init_sql_alloc(&main_mem_root, "Prepared_statement" , |
| 3733 | thd_arg->variables.query_alloc_block_size, |
| 3734 | thd_arg->variables.query_prealloc_size, |
| 3735 | MYF(MY_THREAD_SPECIFIC)); |
| 3736 | *last_error= '\0'; |
| 3737 | } |
| 3738 | |
| 3739 | |
| 3740 | void Prepared_statement::setup_set_params() |
| 3741 | { |
| 3742 | /* |
| 3743 | Note: BUG#25843 applies here too (query cache lookup uses thd->db, not |
| 3744 | db from "prepare" time). |
| 3745 | */ |
| 3746 | if (query_cache_maybe_disabled(thd)) // we won't expand the query |
| 3747 | lex->safe_to_cache_query= FALSE; // so don't cache it at Execution |
| 3748 | |
| 3749 | /* |
| 3750 | Decide if we have to expand the query (because we must write it to logs or |
| 3751 | because we want to look it up in the query cache) or not. |
| 3752 | */ |
| 3753 | bool replace_params_with_values= false; |
| 3754 | // binlog |
| 3755 | replace_params_with_values|= mysql_bin_log.is_open() && is_update_query(lex->sql_command); |
| 3756 | // general or slow log |
| 3757 | replace_params_with_values|= opt_log || thd->variables.sql_log_slow; |
| 3758 | // query cache |
| 3759 | replace_params_with_values|= query_cache_is_cacheable_query(lex); |
| 3760 | // but never for compound statements |
| 3761 | replace_params_with_values&= lex->sql_command != SQLCOM_COMPOUND; |
| 3762 | |
| 3763 | if (replace_params_with_values) |
| 3764 | { |
| 3765 | set_params_from_actual_params= insert_params_from_actual_params_with_log; |
| 3766 | #ifndef EMBEDDED_LIBRARY |
| 3767 | set_params= insert_params_with_log; |
| 3768 | set_bulk_params= insert_bulk_params; // RBR is on for bulk operation |
| 3769 | #else |
| 3770 | //TODO: add bulk support for bulk parameters |
| 3771 | set_params_data= emb_insert_params_with_log; |
| 3772 | #endif |
| 3773 | } |
| 3774 | else |
| 3775 | { |
| 3776 | set_params_from_actual_params= insert_params_from_actual_params; |
| 3777 | #ifndef EMBEDDED_LIBRARY |
| 3778 | set_params= insert_params; |
| 3779 | set_bulk_params= insert_bulk_params; |
| 3780 | #else |
| 3781 | //TODO: add bulk support for bulk parameters |
| 3782 | set_params_data= emb_insert_params; |
| 3783 | #endif |
| 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | |
| 3788 | /** |
| 3789 | Destroy this prepared statement, cleaning up all used memory |
| 3790 | and resources. |
| 3791 | |
| 3792 | This is called from ::deallocate() to handle COM_STMT_CLOSE and |
| 3793 | DEALLOCATE PREPARE or when THD ends and all prepared statements are freed. |
| 3794 | */ |
| 3795 | |
| 3796 | Prepared_statement::~Prepared_statement() |
| 3797 | { |
| 3798 | DBUG_ENTER("Prepared_statement::~Prepared_statement" ); |
| 3799 | DBUG_PRINT("enter" ,("stmt: %p cursor: %p" , |
| 3800 | this, cursor)); |
| 3801 | delete cursor; |
| 3802 | /* |
| 3803 | We have to call free on the items even if cleanup is called as some items, |
| 3804 | like Item_param, don't free everything until free_items() |
| 3805 | */ |
| 3806 | free_items(); |
| 3807 | if (lex) |
| 3808 | { |
| 3809 | delete lex->sphead; |
| 3810 | delete lex->result; |
| 3811 | delete (st_lex_local *) lex; |
| 3812 | } |
| 3813 | free_root(&main_mem_root, MYF(0)); |
| 3814 | DBUG_VOID_RETURN; |
| 3815 | } |
| 3816 | |
| 3817 | |
| 3818 | Query_arena::Type Prepared_statement::type() const |
| 3819 | { |
| 3820 | return PREPARED_STATEMENT; |
| 3821 | } |
| 3822 | |
| 3823 | |
| 3824 | void Prepared_statement::cleanup_stmt() |
| 3825 | { |
| 3826 | DBUG_ENTER("Prepared_statement::cleanup_stmt" ); |
| 3827 | DBUG_PRINT("enter" ,("stmt: %p" , this)); |
| 3828 | lex->restore_set_statement_var(); |
| 3829 | thd->rollback_item_tree_changes(); |
| 3830 | cleanup_items(free_list); |
| 3831 | thd->cleanup_after_query(); |
| 3832 | |
| 3833 | DBUG_VOID_RETURN; |
| 3834 | } |
| 3835 | |
| 3836 | |
| 3837 | bool Prepared_statement::set_name(LEX_CSTRING *name_arg) |
| 3838 | { |
| 3839 | name.length= name_arg->length; |
| 3840 | name.str= (char*) memdup_root(mem_root, name_arg->str, name_arg->length); |
| 3841 | return name.str == 0; |
| 3842 | } |
| 3843 | |
| 3844 | |
| 3845 | /** |
| 3846 | Remember the current database. |
| 3847 | |
| 3848 | We must reset/restore the current database during execution of |
| 3849 | a prepared statement since it affects execution environment: |
| 3850 | privileges, @@character_set_database, and other. |
| 3851 | |
| 3852 | @return 1 if out of memory. |
| 3853 | */ |
| 3854 | |
| 3855 | bool |
| 3856 | Prepared_statement::set_db(const LEX_CSTRING *db_arg) |
| 3857 | { |
| 3858 | /* Remember the current database. */ |
| 3859 | if (db_arg->length) |
| 3860 | { |
| 3861 | if (!(db.str= this->strmake(db_arg->str, db_arg->length))) |
| 3862 | return 1; |
| 3863 | db.length= db_arg->length; |
| 3864 | } |
| 3865 | else |
| 3866 | db= null_clex_str; |
| 3867 | return 0; |
| 3868 | } |
| 3869 | |
| 3870 | /************************************************************************** |
| 3871 | Common parts of mysql_[sql]_stmt_prepare, mysql_[sql]_stmt_execute. |
| 3872 | Essentially, these functions do all the magic of preparing/executing |
| 3873 | a statement, leaving network communication, input data handling and |
| 3874 | global THD state management to the caller. |
| 3875 | ***************************************************************************/ |
| 3876 | |
| 3877 | /** |
| 3878 | Parse statement text, validate the statement, and prepare it for execution. |
| 3879 | |
| 3880 | You should not change global THD state in this function, if at all |
| 3881 | possible: it may be called from any context, e.g. when executing |
| 3882 | a COM_* command, and SQLCOM_* command, or a stored procedure. |
| 3883 | |
| 3884 | @param packet statement text |
| 3885 | @param packet_len |
| 3886 | |
| 3887 | @note |
| 3888 | Precondition: |
| 3889 | The caller must ensure that thd->change_list and thd->free_list |
| 3890 | is empty: this function will not back them up but will free |
| 3891 | in the end of its execution. |
| 3892 | |
| 3893 | @note |
| 3894 | Postcondition: |
| 3895 | thd->mem_root contains unused memory allocated during validation. |
| 3896 | */ |
| 3897 | |
| 3898 | bool Prepared_statement::prepare(const char *packet, uint packet_len) |
| 3899 | { |
| 3900 | bool error; |
| 3901 | Statement stmt_backup; |
| 3902 | Query_arena *old_stmt_arena; |
| 3903 | DBUG_ENTER("Prepared_statement::prepare" ); |
| 3904 | DBUG_ASSERT(m_sql_mode == thd->variables.sql_mode); |
| 3905 | /* |
| 3906 | If this is an SQLCOM_PREPARE, we also increase Com_prepare_sql. |
| 3907 | However, it seems handy if com_stmt_prepare is increased always, |
| 3908 | no matter what kind of prepare is processed. |
| 3909 | */ |
| 3910 | status_var_increment(thd->status_var.com_stmt_prepare); |
| 3911 | |
| 3912 | if (! (lex= new (mem_root) st_lex_local)) |
| 3913 | DBUG_RETURN(TRUE); |
| 3914 | lex->stmt_lex= lex; |
| 3915 | |
| 3916 | if (set_db(&thd->db)) |
| 3917 | DBUG_RETURN(TRUE); |
| 3918 | |
| 3919 | /* |
| 3920 | alloc_query() uses thd->mem_root && thd->query, so we should call |
| 3921 | both of backup_statement() and backup_query_arena() here. |
| 3922 | */ |
| 3923 | thd->set_n_backup_statement(this, &stmt_backup); |
| 3924 | thd->set_n_backup_active_arena(this, &stmt_backup); |
| 3925 | |
| 3926 | if (alloc_query(thd, packet, packet_len)) |
| 3927 | { |
| 3928 | thd->restore_backup_statement(this, &stmt_backup); |
| 3929 | thd->restore_active_arena(this, &stmt_backup); |
| 3930 | DBUG_RETURN(TRUE); |
| 3931 | } |
| 3932 | |
| 3933 | old_stmt_arena= thd->stmt_arena; |
| 3934 | thd->stmt_arena= this; |
| 3935 | |
| 3936 | Parser_state parser_state; |
| 3937 | if (parser_state.init(thd, thd->query(), thd->query_length())) |
| 3938 | { |
| 3939 | thd->restore_backup_statement(this, &stmt_backup); |
| 3940 | thd->restore_active_arena(this, &stmt_backup); |
| 3941 | thd->stmt_arena= old_stmt_arena; |
| 3942 | DBUG_RETURN(TRUE); |
| 3943 | } |
| 3944 | |
| 3945 | parser_state.m_lip.stmt_prepare_mode= TRUE; |
| 3946 | parser_state.m_lip.multi_statements= FALSE; |
| 3947 | |
| 3948 | lex_start(thd); |
| 3949 | lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_PREPARE; |
| 3950 | |
| 3951 | error= (parse_sql(thd, & parser_state, NULL) || |
| 3952 | thd->is_error() || |
| 3953 | init_param_array(this)); |
| 3954 | |
| 3955 | lex->set_trg_event_type_for_tables(); |
| 3956 | |
| 3957 | /* |
| 3958 | While doing context analysis of the query (in check_prepared_statement) |
| 3959 | we allocate a lot of additional memory: for open tables, JOINs, derived |
| 3960 | tables, etc. Let's save a snapshot of current parse tree to the |
| 3961 | statement and restore original THD. In cases when some tree |
| 3962 | transformation can be reused on execute, we set again thd->mem_root from |
| 3963 | stmt->mem_root (see setup_wild for one place where we do that). |
| 3964 | */ |
| 3965 | thd->restore_active_arena(this, &stmt_backup); |
| 3966 | |
| 3967 | /* |
| 3968 | If called from a stored procedure, ensure that we won't rollback |
| 3969 | external changes when cleaning up after validation. |
| 3970 | */ |
| 3971 | DBUG_ASSERT(thd->Item_change_list::is_empty()); |
| 3972 | |
| 3973 | /* |
| 3974 | Marker used to release metadata locks acquired while the prepared |
| 3975 | statement is being checked. |
| 3976 | */ |
| 3977 | MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint(); |
| 3978 | |
| 3979 | /* |
| 3980 | The only case where we should have items in the thd->free_list is |
| 3981 | after stmt->set_params_from_vars(), which may in some cases create |
| 3982 | Item_null objects. |
| 3983 | */ |
| 3984 | |
| 3985 | if (likely(error == 0)) |
| 3986 | error= check_prepared_statement(this); |
| 3987 | |
| 3988 | if (unlikely(error)) |
| 3989 | { |
| 3990 | /* |
| 3991 | let the following code know we're not in PS anymore, |
| 3992 | the won't be any EXECUTE, so we need a full cleanup |
| 3993 | */ |
| 3994 | lex->context_analysis_only&= ~CONTEXT_ANALYSIS_ONLY_PREPARE; |
| 3995 | } |
| 3996 | |
| 3997 | /* The order is important */ |
| 3998 | lex->unit.cleanup(); |
| 3999 | |
| 4000 | /* No need to commit statement transaction, it's not started. */ |
| 4001 | DBUG_ASSERT(thd->transaction.stmt.is_empty()); |
| 4002 | |
| 4003 | close_thread_tables(thd); |
| 4004 | thd->mdl_context.rollback_to_savepoint(mdl_savepoint); |
| 4005 | |
| 4006 | /* |
| 4007 | Transaction rollback was requested since MDL deadlock was discovered |
| 4008 | while trying to open tables. Rollback transaction in all storage |
| 4009 | engines including binary log and release all locks. |
| 4010 | |
| 4011 | Once dynamic SQL is allowed as substatements the below if-statement |
| 4012 | has to be adjusted to not do rollback in substatement. |
| 4013 | */ |
| 4014 | DBUG_ASSERT(! thd->in_sub_stmt); |
| 4015 | if (thd->transaction_rollback_request) |
| 4016 | { |
| 4017 | trans_rollback_implicit(thd); |
| 4018 | thd->mdl_context.release_transactional_locks(); |
| 4019 | } |
| 4020 | |
| 4021 | /* Preserve CHANGE MASTER attributes */ |
| 4022 | lex_end_stage1(lex); |
| 4023 | cleanup_stmt(); |
| 4024 | thd->restore_backup_statement(this, &stmt_backup); |
| 4025 | thd->stmt_arena= old_stmt_arena; |
| 4026 | |
| 4027 | if (likely(error == 0)) |
| 4028 | { |
| 4029 | setup_set_params(); |
| 4030 | lex->context_analysis_only&= ~CONTEXT_ANALYSIS_ONLY_PREPARE; |
| 4031 | state= Query_arena::STMT_PREPARED; |
| 4032 | flags&= ~ (uint) IS_IN_USE; |
| 4033 | |
| 4034 | /* |
| 4035 | Log COM_EXECUTE to the general log. Note, that in case of SQL |
| 4036 | prepared statements this causes two records to be output: |
| 4037 | |
| 4038 | Query PREPARE stmt from @user_variable |
| 4039 | Prepare <statement SQL text> |
| 4040 | |
| 4041 | This is considered user-friendly, since in the |
| 4042 | second log entry we output the actual statement text. |
| 4043 | |
| 4044 | Do not print anything if this is an SQL prepared statement and |
| 4045 | we're inside a stored procedure (also called Dynamic SQL) -- |
| 4046 | sub-statements inside stored procedures are not logged into |
| 4047 | the general log. |
| 4048 | */ |
| 4049 | if (thd->spcont == NULL) |
| 4050 | general_log_write(thd, COM_STMT_PREPARE, query(), query_length()); |
| 4051 | } |
| 4052 | DBUG_RETURN(error); |
| 4053 | } |
| 4054 | |
| 4055 | |
| 4056 | /** |
| 4057 | Assign parameter values either from variables, in case of SQL PS |
| 4058 | or from the execute packet. |
| 4059 | |
| 4060 | @param expanded_query a container with the original SQL statement. |
| 4061 | '?' placeholders will be replaced with |
| 4062 | their values in case of success. |
| 4063 | The result is used for logging and replication |
| 4064 | @param packet pointer to execute packet. |
| 4065 | NULL in case of SQL PS |
| 4066 | @param packet_end end of the packet. NULL in case of SQL PS |
| 4067 | |
| 4068 | @todo Use a paremeter source class family instead of 'if's, and |
| 4069 | support stored procedure variables. |
| 4070 | |
| 4071 | @retval TRUE an error occurred when assigning a parameter (likely |
| 4072 | a conversion error or out of memory, or malformed packet) |
| 4073 | @retval FALSE success |
| 4074 | */ |
| 4075 | |
| 4076 | bool |
| 4077 | Prepared_statement::set_parameters(String *expanded_query, |
| 4078 | uchar *packet, uchar *packet_end) |
| 4079 | { |
| 4080 | bool is_sql_ps= packet == NULL; |
| 4081 | bool res= FALSE; |
| 4082 | |
| 4083 | if (is_sql_ps) |
| 4084 | { |
| 4085 | /* SQL prepared statement */ |
| 4086 | res= set_params_from_actual_params(this, thd->lex->prepared_stmt_params, |
| 4087 | expanded_query); |
| 4088 | } |
| 4089 | else if (param_count) |
| 4090 | { |
| 4091 | #ifndef EMBEDDED_LIBRARY |
| 4092 | uchar *null_array= packet; |
| 4093 | res= (setup_conversion_functions(this, &packet, packet_end) || |
| 4094 | set_params(this, null_array, packet, packet_end, expanded_query)); |
| 4095 | #else |
| 4096 | /* |
| 4097 | In embedded library we re-install conversion routines each time |
| 4098 | we set parameters, and also we don't need to parse packet. |
| 4099 | So we do it in one function. |
| 4100 | */ |
| 4101 | res= set_params_data(this, expanded_query); |
| 4102 | #endif |
| 4103 | } |
| 4104 | if (res) |
| 4105 | { |
| 4106 | my_error(ER_WRONG_ARGUMENTS, MYF(0), |
| 4107 | is_sql_ps ? "EXECUTE" : "mysqld_stmt_execute" ); |
| 4108 | reset_stmt_params(this); |
| 4109 | } |
| 4110 | return res; |
| 4111 | } |
| 4112 | |
| 4113 | |
| 4114 | /** |
| 4115 | Execute a prepared statement. Re-prepare it a limited number |
| 4116 | of times if necessary. |
| 4117 | |
| 4118 | Try to execute a prepared statement. If there is a metadata |
| 4119 | validation error, prepare a new copy of the prepared statement, |
| 4120 | swap the old and the new statements, and try again. |
| 4121 | If there is a validation error again, repeat the above, but |
| 4122 | perform no more than MAX_REPREPARE_ATTEMPTS. |
| 4123 | |
| 4124 | @note We have to try several times in a loop since we |
| 4125 | release metadata locks on tables after prepared statement |
| 4126 | prepare. Therefore, a DDL statement may sneak in between prepare |
| 4127 | and execute of a new statement. If this happens repeatedly |
| 4128 | more than MAX_REPREPARE_ATTEMPTS times, we give up. |
| 4129 | |
| 4130 | @return TRUE if an error, FALSE if success |
| 4131 | @retval TRUE either MAX_REPREPARE_ATTEMPTS has been reached, |
| 4132 | or some general error |
| 4133 | @retval FALSE successfully executed the statement, perhaps |
| 4134 | after having reprepared it a few times. |
| 4135 | */ |
| 4136 | const static int MAX_REPREPARE_ATTEMPTS= 3; |
| 4137 | |
| 4138 | bool |
| 4139 | Prepared_statement::execute_loop(String *expanded_query, |
| 4140 | bool open_cursor, |
| 4141 | uchar *packet, |
| 4142 | uchar *packet_end) |
| 4143 | { |
| 4144 | Reprepare_observer reprepare_observer; |
| 4145 | bool error; |
| 4146 | int reprepare_attempt= 0; |
| 4147 | iterations= FALSE; |
| 4148 | |
| 4149 | /* |
| 4150 | - In mysql_sql_stmt_execute() we hide all "external" Items |
| 4151 | e.g. those created in the "SET STATEMENT" part of the "EXECUTE" query. |
| 4152 | - In case of mysqld_stmt_execute() there should not be "external" Items. |
| 4153 | */ |
| 4154 | DBUG_ASSERT(thd->free_list == NULL); |
| 4155 | |
| 4156 | /* Check if we got an error when sending long data */ |
| 4157 | if (unlikely(state == Query_arena::STMT_ERROR)) |
| 4158 | { |
| 4159 | my_message(last_errno, last_error, MYF(0)); |
| 4160 | return TRUE; |
| 4161 | } |
| 4162 | |
| 4163 | if (set_parameters(expanded_query, packet, packet_end)) |
| 4164 | return TRUE; |
| 4165 | |
| 4166 | #ifdef NOT_YET_FROM_MYSQL_5_6 |
| 4167 | if (unlikely(thd->security_ctx->password_expired && |
| 4168 | !lex->is_change_password)) |
| 4169 | { |
| 4170 | my_error(ER_MUST_CHANGE_PASSWORD, MYF(0)); |
| 4171 | return true; |
| 4172 | } |
| 4173 | #endif |
| 4174 | |
| 4175 | reexecute: |
| 4176 | // Make sure that reprepare() did not create any new Items. |
| 4177 | DBUG_ASSERT(thd->free_list == NULL); |
| 4178 | |
| 4179 | /* |
| 4180 | Install the metadata observer. If some metadata version is |
| 4181 | different from prepare time and an observer is installed, |
| 4182 | the observer method will be invoked to push an error into |
| 4183 | the error stack. |
| 4184 | */ |
| 4185 | |
| 4186 | if (sql_command_flags[lex->sql_command] & CF_REEXECUTION_FRAGILE) |
| 4187 | { |
| 4188 | reprepare_observer.reset_reprepare_observer(); |
| 4189 | DBUG_ASSERT(thd->m_reprepare_observer == NULL); |
| 4190 | thd->m_reprepare_observer= &reprepare_observer; |
| 4191 | } |
| 4192 | |
| 4193 | error= execute(expanded_query, open_cursor) || thd->is_error(); |
| 4194 | |
| 4195 | thd->m_reprepare_observer= NULL; |
| 4196 | #ifdef WITH_WSREP |
| 4197 | |
| 4198 | if (WSREP_ON) |
| 4199 | { |
| 4200 | mysql_mutex_lock(&thd->LOCK_thd_data); |
| 4201 | switch (thd->wsrep_conflict_state) |
| 4202 | { |
| 4203 | case CERT_FAILURE: |
| 4204 | WSREP_DEBUG("PS execute fail for CERT_FAILURE: thd: %lld err: %d" , |
| 4205 | (longlong) thd->thread_id, |
| 4206 | thd->get_stmt_da()->sql_errno() ); |
| 4207 | thd->wsrep_conflict_state = NO_CONFLICT; |
| 4208 | break; |
| 4209 | |
| 4210 | case MUST_REPLAY: |
| 4211 | (void) wsrep_replay_transaction(thd); |
| 4212 | break; |
| 4213 | |
| 4214 | default: |
| 4215 | break; |
| 4216 | } |
| 4217 | mysql_mutex_unlock(&thd->LOCK_thd_data); |
| 4218 | } |
| 4219 | #endif /* WITH_WSREP */ |
| 4220 | |
| 4221 | if (unlikely(error) && |
| 4222 | (sql_command_flags[lex->sql_command] & CF_REEXECUTION_FRAGILE) && |
| 4223 | !thd->is_fatal_error && !thd->killed && |
| 4224 | reprepare_observer.is_invalidated() && |
| 4225 | reprepare_attempt++ < MAX_REPREPARE_ATTEMPTS) |
| 4226 | { |
| 4227 | DBUG_ASSERT(thd->get_stmt_da()->sql_errno() == ER_NEED_REPREPARE); |
| 4228 | thd->clear_error(); |
| 4229 | |
| 4230 | error= reprepare(); |
| 4231 | |
| 4232 | if (likely(!error)) /* Success */ |
| 4233 | goto reexecute; |
| 4234 | } |
| 4235 | reset_stmt_params(this); |
| 4236 | |
| 4237 | return error; |
| 4238 | } |
| 4239 | |
| 4240 | my_bool bulk_parameters_set(THD *thd) |
| 4241 | { |
| 4242 | DBUG_ENTER("bulk_parameters_set" ); |
| 4243 | Prepared_statement *stmt= (Prepared_statement *) thd->bulk_param; |
| 4244 | |
| 4245 | if (stmt && unlikely(stmt->set_bulk_parameters(FALSE))) |
| 4246 | DBUG_RETURN(TRUE); |
| 4247 | DBUG_RETURN(FALSE); |
| 4248 | } |
| 4249 | |
| 4250 | my_bool bulk_parameters_iterations(THD *thd) |
| 4251 | { |
| 4252 | Prepared_statement *stmt= (Prepared_statement *) thd->bulk_param; |
| 4253 | if (!stmt) |
| 4254 | return FALSE; |
| 4255 | return stmt->bulk_iterations(); |
| 4256 | } |
| 4257 | |
| 4258 | |
| 4259 | my_bool Prepared_statement::set_bulk_parameters(bool reset) |
| 4260 | { |
| 4261 | DBUG_ENTER("Prepared_statement::set_bulk_parameters" ); |
| 4262 | DBUG_PRINT("info" , ("iteration: %d" , iterations)); |
| 4263 | |
| 4264 | if (iterations) |
| 4265 | { |
| 4266 | #ifndef EMBEDDED_LIBRARY |
| 4267 | if ((*set_bulk_params)(this, &packet, packet_end, reset)) |
| 4268 | #else |
| 4269 | // bulk parameters are not supported for embedded, so it will an error |
| 4270 | #endif |
| 4271 | { |
| 4272 | my_error(ER_WRONG_ARGUMENTS, MYF(0), |
| 4273 | "mysqld_stmt_bulk_execute" ); |
| 4274 | reset_stmt_params(this); |
| 4275 | DBUG_RETURN(true); |
| 4276 | } |
| 4277 | if (packet >= packet_end) |
| 4278 | iterations= FALSE; |
| 4279 | } |
| 4280 | start_param= 0; |
| 4281 | DBUG_RETURN(false); |
| 4282 | } |
| 4283 | |
| 4284 | bool |
| 4285 | Prepared_statement::execute_bulk_loop(String *expanded_query, |
| 4286 | bool open_cursor, |
| 4287 | uchar *packet_arg, |
| 4288 | uchar *packet_end_arg) |
| 4289 | { |
| 4290 | Reprepare_observer reprepare_observer; |
| 4291 | bool error= 0; |
| 4292 | packet= packet_arg; |
| 4293 | packet_end= packet_end_arg; |
| 4294 | iterations= TRUE; |
| 4295 | start_param= true; |
| 4296 | #ifdef DBUG_ASSERT_EXISTS |
| 4297 | Item *free_list_state= thd->free_list; |
| 4298 | #endif |
| 4299 | thd->set_bulk_execution((void *)this); |
| 4300 | /* Check if we got an error when sending long data */ |
| 4301 | if (state == Query_arena::STMT_ERROR) |
| 4302 | { |
| 4303 | my_message(last_errno, last_error, MYF(0)); |
| 4304 | thd->set_bulk_execution(0); |
| 4305 | return TRUE; |
| 4306 | } |
| 4307 | /* Check for non zero parameter count*/ |
| 4308 | if (param_count == 0) |
| 4309 | { |
| 4310 | DBUG_PRINT("error" , ("Statement with no parameters for bulk execution." )); |
| 4311 | my_error(ER_UNSUPPORTED_PS, MYF(0)); |
| 4312 | thd->set_bulk_execution(0); |
| 4313 | return TRUE; |
| 4314 | } |
| 4315 | |
| 4316 | if (!(sql_command_flags[lex->sql_command] & CF_SP_BULK_SAFE)) |
| 4317 | { |
| 4318 | DBUG_PRINT("error" , ("Command is not supported in bulk execution." )); |
| 4319 | my_error(ER_UNSUPPORTED_PS, MYF(0)); |
| 4320 | thd->set_bulk_execution(0); |
| 4321 | return TRUE; |
| 4322 | } |
| 4323 | |
| 4324 | #ifndef EMBEDDED_LIBRARY |
| 4325 | if (read_types && |
| 4326 | set_conversion_functions(this, &packet, packet_end)) |
| 4327 | #else |
| 4328 | // bulk parameters are not supported for embedded, so it will an error |
| 4329 | #endif |
| 4330 | { |
| 4331 | my_error(ER_WRONG_ARGUMENTS, MYF(0), |
| 4332 | "mysqld_stmt_bulk_execute" ); |
| 4333 | reset_stmt_params(this); |
| 4334 | thd->set_bulk_execution(0); |
| 4335 | return true; |
| 4336 | } |
| 4337 | read_types= FALSE; |
| 4338 | |
| 4339 | #ifdef NOT_YET_FROM_MYSQL_5_6 |
| 4340 | if (unlikely(thd->security_ctx->password_expired && |
| 4341 | !lex->is_change_password)) |
| 4342 | { |
| 4343 | my_error(ER_MUST_CHANGE_PASSWORD, MYF(0)); |
| 4344 | thd->set_bulk_execution(0); |
| 4345 | return true; |
| 4346 | } |
| 4347 | #endif |
| 4348 | |
| 4349 | // iterations changed by set_bulk_parameters |
| 4350 | while ((iterations || start_param) && !error && !thd->is_error()) |
| 4351 | { |
| 4352 | int reprepare_attempt= 0; |
| 4353 | |
| 4354 | /* |
| 4355 | Here we set parameters for not optimized commands, |
| 4356 | optimized commands do it inside thier internal loop. |
| 4357 | */ |
| 4358 | if (!(sql_command_flags[lex->sql_command] & CF_SP_BULK_OPTIMIZED)) |
| 4359 | { |
| 4360 | if (set_bulk_parameters(TRUE)) |
| 4361 | { |
| 4362 | thd->set_bulk_execution(0); |
| 4363 | return true; |
| 4364 | } |
| 4365 | } |
| 4366 | |
| 4367 | reexecute: |
| 4368 | /* |
| 4369 | If the free_list is not empty, we'll wrongly free some externally |
| 4370 | allocated items when cleaning up after validation of the prepared |
| 4371 | statement. |
| 4372 | */ |
| 4373 | DBUG_ASSERT(thd->free_list == free_list_state); |
| 4374 | |
| 4375 | /* |
| 4376 | Install the metadata observer. If some metadata version is |
| 4377 | different from prepare time and an observer is installed, |
| 4378 | the observer method will be invoked to push an error into |
| 4379 | the error stack. |
| 4380 | */ |
| 4381 | |
| 4382 | if (sql_command_flags[lex->sql_command] & CF_REEXECUTION_FRAGILE) |
| 4383 | { |
| 4384 | reprepare_observer.reset_reprepare_observer(); |
| 4385 | DBUG_ASSERT(thd->m_reprepare_observer == NULL); |
| 4386 | thd->m_reprepare_observer= &reprepare_observer; |
| 4387 | } |
| 4388 | |
| 4389 | error= execute(expanded_query, open_cursor) || thd->is_error(); |
| 4390 | |
| 4391 | thd->m_reprepare_observer= NULL; |
| 4392 | #ifdef WITH_WSREP |
| 4393 | |
| 4394 | if (WSREP_ON) |
| 4395 | { |
| 4396 | mysql_mutex_lock(&thd->LOCK_thd_data); |
| 4397 | switch (thd->wsrep_conflict_state) |
| 4398 | { |
| 4399 | case CERT_FAILURE: |
| 4400 | WSREP_DEBUG("PS execute fail for CERT_FAILURE: thd: %lld err: %d" , |
| 4401 | (longlong) thd->thread_id, |
| 4402 | thd->get_stmt_da()->sql_errno() ); |
| 4403 | thd->wsrep_conflict_state = NO_CONFLICT; |
| 4404 | break; |
| 4405 | |
| 4406 | case MUST_REPLAY: |
| 4407 | (void) wsrep_replay_transaction(thd); |
| 4408 | break; |
| 4409 | |
| 4410 | default: |
| 4411 | break; |
| 4412 | } |
| 4413 | mysql_mutex_unlock(&thd->LOCK_thd_data); |
| 4414 | } |
| 4415 | #endif /* WITH_WSREP */ |
| 4416 | |
| 4417 | if (unlikely(error) && |
| 4418 | (sql_command_flags[lex->sql_command] & CF_REEXECUTION_FRAGILE) && |
| 4419 | !thd->is_fatal_error && !thd->killed && |
| 4420 | reprepare_observer.is_invalidated() && |
| 4421 | reprepare_attempt++ < MAX_REPREPARE_ATTEMPTS) |
| 4422 | { |
| 4423 | DBUG_ASSERT(thd->get_stmt_da()->sql_errno() == ER_NEED_REPREPARE); |
| 4424 | thd->clear_error(); |
| 4425 | |
| 4426 | error= reprepare(); |
| 4427 | |
| 4428 | if (likely(!error)) /* Success */ |
| 4429 | goto reexecute; |
| 4430 | } |
| 4431 | } |
| 4432 | reset_stmt_params(this); |
| 4433 | thd->set_bulk_execution(0); |
| 4434 | |
| 4435 | return error; |
| 4436 | } |
| 4437 | |
| 4438 | |
| 4439 | bool |
| 4440 | Prepared_statement::execute_server_runnable(Server_runnable *server_runnable) |
| 4441 | { |
| 4442 | Statement stmt_backup; |
| 4443 | bool error; |
| 4444 | Query_arena *save_stmt_arena= thd->stmt_arena; |
| 4445 | Item_change_list save_change_list; |
| 4446 | thd->Item_change_list::move_elements_to(&save_change_list); |
| 4447 | |
| 4448 | state= STMT_CONVENTIONAL_EXECUTION; |
| 4449 | |
| 4450 | if (!(lex= new (mem_root) st_lex_local)) |
| 4451 | return TRUE; |
| 4452 | |
| 4453 | thd->set_n_backup_statement(this, &stmt_backup); |
| 4454 | thd->set_n_backup_active_arena(this, &stmt_backup); |
| 4455 | thd->stmt_arena= this; |
| 4456 | |
| 4457 | error= server_runnable->execute_server_code(thd); |
| 4458 | |
| 4459 | thd->cleanup_after_query(); |
| 4460 | |
| 4461 | thd->restore_active_arena(this, &stmt_backup); |
| 4462 | thd->restore_backup_statement(this, &stmt_backup); |
| 4463 | thd->stmt_arena= save_stmt_arena; |
| 4464 | |
| 4465 | save_change_list.move_elements_to(thd); |
| 4466 | |
| 4467 | /* Items and memory will freed in destructor */ |
| 4468 | |
| 4469 | return error; |
| 4470 | } |
| 4471 | |
| 4472 | |
| 4473 | /** |
| 4474 | Reprepare this prepared statement. |
| 4475 | |
| 4476 | Currently this is implemented by creating a new prepared |
| 4477 | statement, preparing it with the original query and then |
| 4478 | swapping the new statement and the original one. |
| 4479 | |
| 4480 | @retval TRUE an error occurred. Possible errors include |
| 4481 | incompatibility of new and old result set |
| 4482 | metadata |
| 4483 | @retval FALSE success, the statement has been reprepared |
| 4484 | */ |
| 4485 | |
| 4486 | bool |
| 4487 | Prepared_statement::reprepare() |
| 4488 | { |
| 4489 | char saved_cur_db_name_buf[SAFE_NAME_LEN+1]; |
| 4490 | LEX_STRING saved_cur_db_name= |
| 4491 | { saved_cur_db_name_buf, sizeof(saved_cur_db_name_buf) }; |
| 4492 | LEX_CSTRING stmt_db_name= db; |
| 4493 | bool cur_db_changed; |
| 4494 | bool error; |
| 4495 | |
| 4496 | Prepared_statement copy(thd); |
| 4497 | copy.m_sql_mode= m_sql_mode; |
| 4498 | |
| 4499 | copy.set_sql_prepare(); /* To suppress sending metadata to the client. */ |
| 4500 | |
| 4501 | status_var_increment(thd->status_var.com_stmt_reprepare); |
| 4502 | |
| 4503 | if (unlikely(mysql_opt_change_db(thd, &stmt_db_name, &saved_cur_db_name, |
| 4504 | TRUE, &cur_db_changed))) |
| 4505 | return TRUE; |
| 4506 | |
| 4507 | sql_mode_t save_sql_mode= thd->variables.sql_mode; |
| 4508 | thd->variables.sql_mode= m_sql_mode; |
| 4509 | error= ((name.str && copy.set_name(&name)) || |
| 4510 | copy.prepare(query(), query_length()) || |
| 4511 | validate_metadata(©)); |
| 4512 | thd->variables.sql_mode= save_sql_mode; |
| 4513 | |
| 4514 | if (cur_db_changed) |
| 4515 | mysql_change_db(thd, (LEX_CSTRING*) &saved_cur_db_name, TRUE); |
| 4516 | |
| 4517 | if (likely(!error)) |
| 4518 | { |
| 4519 | swap_prepared_statement(©); |
| 4520 | swap_parameter_array(param_array, copy.param_array, param_count); |
| 4521 | #ifdef DBUG_ASSERT_EXISTS |
| 4522 | is_reprepared= TRUE; |
| 4523 | #endif |
| 4524 | /* |
| 4525 | Clear possible warnings during reprepare, it has to be completely |
| 4526 | transparent to the user. We use clear_warning_info() since |
| 4527 | there were no separate query id issued for re-prepare. |
| 4528 | Sic: we can't simply silence warnings during reprepare, because if |
| 4529 | it's failed, we need to return all the warnings to the user. |
| 4530 | */ |
| 4531 | thd->get_stmt_da()->clear_warning_info(thd->query_id); |
| 4532 | } |
| 4533 | return error; |
| 4534 | } |
| 4535 | |
| 4536 | |
| 4537 | /** |
| 4538 | Validate statement result set metadata (if the statement returns |
| 4539 | a result set). |
| 4540 | |
| 4541 | Currently we only check that the number of columns of the result |
| 4542 | set did not change. |
| 4543 | This is a helper method used during re-prepare. |
| 4544 | |
| 4545 | @param[in] copy the re-prepared prepared statement to verify |
| 4546 | the metadata of |
| 4547 | |
| 4548 | @retval TRUE error, ER_PS_REBIND is reported |
| 4549 | @retval FALSE statement return no or compatible metadata |
| 4550 | */ |
| 4551 | |
| 4552 | |
| 4553 | bool Prepared_statement::validate_metadata(Prepared_statement *copy) |
| 4554 | { |
| 4555 | /** |
| 4556 | If this is an SQL prepared statement or EXPLAIN, |
| 4557 | return FALSE -- the metadata of the original SELECT, |
| 4558 | if any, has not been sent to the client. |
| 4559 | */ |
| 4560 | if (is_sql_prepare() || lex->describe) |
| 4561 | return FALSE; |
| 4562 | |
| 4563 | if (lex->select_lex.item_list.elements != |
| 4564 | copy->lex->select_lex.item_list.elements) |
| 4565 | { |
| 4566 | /** Column counts mismatch, update the client */ |
| 4567 | thd->server_status|= SERVER_STATUS_METADATA_CHANGED; |
| 4568 | } |
| 4569 | |
| 4570 | return FALSE; |
| 4571 | } |
| 4572 | |
| 4573 | |
| 4574 | /** |
| 4575 | Replace the original prepared statement with a prepared copy. |
| 4576 | |
| 4577 | This is a private helper that is used as part of statement |
| 4578 | reprepare |
| 4579 | |
| 4580 | @return This function does not return any errors. |
| 4581 | */ |
| 4582 | |
| 4583 | void |
| 4584 | Prepared_statement::swap_prepared_statement(Prepared_statement *copy) |
| 4585 | { |
| 4586 | Statement tmp_stmt; |
| 4587 | |
| 4588 | /* Swap memory roots. */ |
| 4589 | swap_variables(MEM_ROOT, main_mem_root, copy->main_mem_root); |
| 4590 | |
| 4591 | /* Swap the arenas */ |
| 4592 | tmp_stmt.set_query_arena(this); |
| 4593 | set_query_arena(copy); |
| 4594 | copy->set_query_arena(&tmp_stmt); |
| 4595 | |
| 4596 | /* Swap the statement parent classes */ |
| 4597 | tmp_stmt.set_statement(this); |
| 4598 | set_statement(copy); |
| 4599 | copy->set_statement(&tmp_stmt); |
| 4600 | |
| 4601 | /* Swap ids back, we need the original id */ |
| 4602 | swap_variables(ulong, id, copy->id); |
| 4603 | /* Swap mem_roots back, they must continue pointing at the main_mem_roots */ |
| 4604 | swap_variables(MEM_ROOT *, mem_root, copy->mem_root); |
| 4605 | /* |
| 4606 | Swap the old and the new parameters array. The old array |
| 4607 | is allocated in the old arena. |
| 4608 | */ |
| 4609 | swap_variables(Item_param **, param_array, copy->param_array); |
| 4610 | /* Don't swap flags: the copy has IS_SQL_PREPARE always set. */ |
| 4611 | /* swap_variables(uint, flags, copy->flags); */ |
| 4612 | /* Swap names, the old name is allocated in the wrong memory root */ |
| 4613 | swap_variables(LEX_CSTRING, name, copy->name); |
| 4614 | /* Ditto */ |
| 4615 | swap_variables(LEX_CSTRING, db, copy->db); |
| 4616 | |
| 4617 | DBUG_ASSERT(param_count == copy->param_count); |
| 4618 | DBUG_ASSERT(thd == copy->thd); |
| 4619 | last_error[0]= '\0'; |
| 4620 | last_errno= 0; |
| 4621 | } |
| 4622 | |
| 4623 | |
| 4624 | /** |
| 4625 | Execute a prepared statement. |
| 4626 | |
| 4627 | You should not change global THD state in this function, if at all |
| 4628 | possible: it may be called from any context, e.g. when executing |
| 4629 | a COM_* command, and SQLCOM_* command, or a stored procedure. |
| 4630 | |
| 4631 | @param expanded_query A query for binlogging which has all parameter |
| 4632 | markers ('?') replaced with their actual values. |
| 4633 | @param open_cursor True if an attempt to open a cursor should be made. |
| 4634 | Currenlty used only in the binary protocol. |
| 4635 | |
| 4636 | @note |
| 4637 | Preconditions, postconditions. |
| 4638 | - See the comment for Prepared_statement::prepare(). |
| 4639 | |
| 4640 | @retval |
| 4641 | FALSE ok |
| 4642 | @retval |
| 4643 | TRUE Error |
| 4644 | */ |
| 4645 | |
| 4646 | bool Prepared_statement::execute(String *expanded_query, bool open_cursor) |
| 4647 | { |
| 4648 | Statement stmt_backup; |
| 4649 | Query_arena *old_stmt_arena; |
| 4650 | bool error= TRUE; |
| 4651 | bool qc_executed= FALSE; |
| 4652 | |
| 4653 | char saved_cur_db_name_buf[SAFE_NAME_LEN+1]; |
| 4654 | LEX_STRING saved_cur_db_name= |
| 4655 | { saved_cur_db_name_buf, sizeof(saved_cur_db_name_buf) }; |
| 4656 | bool cur_db_changed; |
| 4657 | |
| 4658 | LEX_CSTRING stmt_db_name= db; |
| 4659 | |
| 4660 | status_var_increment(thd->status_var.com_stmt_execute); |
| 4661 | |
| 4662 | if (flags & (uint) IS_IN_USE) |
| 4663 | { |
| 4664 | my_error(ER_PS_NO_RECURSION, MYF(0)); |
| 4665 | return TRUE; |
| 4666 | } |
| 4667 | |
| 4668 | /* |
| 4669 | For SHOW VARIABLES lex->result is NULL, as it's a non-SELECT |
| 4670 | command. For such queries we don't return an error and don't |
| 4671 | open a cursor -- the client library will recognize this case and |
| 4672 | materialize the result set. |
| 4673 | For SELECT statements lex->result is created in |
| 4674 | check_prepared_statement. lex->result->simple_select() is FALSE |
| 4675 | in INSERT ... SELECT and similar commands. |
| 4676 | */ |
| 4677 | |
| 4678 | if (open_cursor && lex->result && lex->result->check_simple_select()) |
| 4679 | { |
| 4680 | DBUG_PRINT("info" ,("Cursor asked for not SELECT stmt" )); |
| 4681 | return TRUE; |
| 4682 | } |
| 4683 | |
| 4684 | /* In case the command has a call to SP which re-uses this statement name */ |
| 4685 | flags|= IS_IN_USE; |
| 4686 | |
| 4687 | close_cursor(); |
| 4688 | |
| 4689 | /* |
| 4690 | If the free_list is not empty, we'll wrongly free some externally |
| 4691 | allocated items when cleaning up after execution of this statement. |
| 4692 | */ |
| 4693 | DBUG_ASSERT(thd->Item_change_list::is_empty()); |
| 4694 | |
| 4695 | /* |
| 4696 | The only case where we should have items in the thd->free_list is |
| 4697 | after stmt->set_params_from_vars(), which may in some cases create |
| 4698 | Item_null objects. |
| 4699 | */ |
| 4700 | |
| 4701 | thd->set_n_backup_statement(this, &stmt_backup); |
| 4702 | |
| 4703 | /* |
| 4704 | Change the current database (if needed). |
| 4705 | |
| 4706 | Force switching, because the database of the prepared statement may be |
| 4707 | NULL (prepared statements can be created while no current database |
| 4708 | selected). |
| 4709 | */ |
| 4710 | |
| 4711 | if (mysql_opt_change_db(thd, &stmt_db_name, &saved_cur_db_name, TRUE, |
| 4712 | &cur_db_changed)) |
| 4713 | goto error; |
| 4714 | |
| 4715 | /* Allocate query. */ |
| 4716 | |
| 4717 | if (expanded_query->length() && |
| 4718 | alloc_query(thd, (char*) expanded_query->ptr(), |
| 4719 | expanded_query->length())) |
| 4720 | { |
| 4721 | my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), expanded_query->length()); |
| 4722 | goto error; |
| 4723 | } |
| 4724 | /* |
| 4725 | Expanded query is needed for slow logging, so we want thd->query |
| 4726 | to point at it even after we restore from backup. This is ok, as |
| 4727 | expanded query was allocated in thd->mem_root. |
| 4728 | */ |
| 4729 | stmt_backup.set_query_inner(thd->query_string); |
| 4730 | |
| 4731 | /* |
| 4732 | At first execution of prepared statement we may perform logical |
| 4733 | transformations of the query tree. Such changes should be performed |
| 4734 | on the parse tree of current prepared statement and new items should |
| 4735 | be allocated in its memory root. Set the appropriate pointer in THD |
| 4736 | to the arena of the statement. |
| 4737 | */ |
| 4738 | old_stmt_arena= thd->stmt_arena; |
| 4739 | thd->stmt_arena= this; |
| 4740 | reinit_stmt_before_use(thd, lex); |
| 4741 | |
| 4742 | /* Go! */ |
| 4743 | |
| 4744 | if (open_cursor) |
| 4745 | error= mysql_open_cursor(thd, &result, &cursor); |
| 4746 | else |
| 4747 | { |
| 4748 | /* |
| 4749 | Try to find it in the query cache, if not, execute it. |
| 4750 | Note that multi-statements cannot exist here (they are not supported in |
| 4751 | prepared statements). |
| 4752 | */ |
| 4753 | if (query_cache_send_result_to_client(thd, thd->query(), |
| 4754 | thd->query_length()) <= 0) |
| 4755 | { |
| 4756 | PSI_statement_locker *parent_locker; |
| 4757 | MYSQL_QUERY_EXEC_START(thd->query(), |
| 4758 | thd->thread_id, |
| 4759 | thd->get_db(), |
| 4760 | &thd->security_ctx->priv_user[0], |
| 4761 | (char *) thd->security_ctx->host_or_ip, |
| 4762 | 1); |
| 4763 | parent_locker= thd->m_statement_psi; |
| 4764 | thd->m_statement_psi= NULL; |
| 4765 | error= mysql_execute_command(thd); |
| 4766 | thd->m_statement_psi= parent_locker; |
| 4767 | MYSQL_QUERY_EXEC_DONE(error); |
| 4768 | } |
| 4769 | else |
| 4770 | { |
| 4771 | thd->lex->sql_command= SQLCOM_SELECT; |
| 4772 | status_var_increment(thd->status_var.com_stat[SQLCOM_SELECT]); |
| 4773 | thd->update_stats(); |
| 4774 | qc_executed= TRUE; |
| 4775 | } |
| 4776 | } |
| 4777 | |
| 4778 | /* |
| 4779 | Restore the current database (if changed). |
| 4780 | |
| 4781 | Force switching back to the saved current database (if changed), |
| 4782 | because it may be NULL. In this case, mysql_change_db() would generate |
| 4783 | an error. |
| 4784 | */ |
| 4785 | |
| 4786 | if (cur_db_changed) |
| 4787 | mysql_change_db(thd, (LEX_CSTRING*) &saved_cur_db_name, TRUE); |
| 4788 | |
| 4789 | /* Assert that if an error, no cursor is open */ |
| 4790 | DBUG_ASSERT(! (error && cursor)); |
| 4791 | |
| 4792 | if (! cursor) |
| 4793 | cleanup_stmt(); |
| 4794 | |
| 4795 | /* |
| 4796 | EXECUTE command has its own dummy "explain data". We don't need it, |
| 4797 | instead, we want to keep the query plan of the statement that was |
| 4798 | executed. |
| 4799 | */ |
| 4800 | if (!stmt_backup.lex->explain || |
| 4801 | !stmt_backup.lex->explain->have_query_plan()) |
| 4802 | { |
| 4803 | delete_explain_query(stmt_backup.lex); |
| 4804 | stmt_backup.lex->explain = thd->lex->explain; |
| 4805 | thd->lex->explain= NULL; |
| 4806 | } |
| 4807 | else |
| 4808 | delete_explain_query(thd->lex); |
| 4809 | |
| 4810 | thd->set_statement(&stmt_backup); |
| 4811 | thd->stmt_arena= old_stmt_arena; |
| 4812 | |
| 4813 | if (state == Query_arena::STMT_PREPARED && !qc_executed) |
| 4814 | state= Query_arena::STMT_EXECUTED; |
| 4815 | |
| 4816 | if (likely(error == 0) && this->lex->sql_command == SQLCOM_CALL) |
| 4817 | { |
| 4818 | if (is_sql_prepare()) |
| 4819 | { |
| 4820 | /* |
| 4821 | Here we have the diagnostics area status already set to DA_OK. |
| 4822 | sent_out_parameters() can raise errors when assigning OUT parameters: |
| 4823 | DECLARE a DATETIME; |
| 4824 | EXECUTE IMMEDIATE 'CALL p1(?)' USING a; |
| 4825 | when the procedure p1 assigns a DATETIME-incompatible value (e.g. 10) |
| 4826 | to the out parameter. Allow to overwrite status (to DA_ERROR). |
| 4827 | */ |
| 4828 | thd->get_stmt_da()->set_overwrite_status(true); |
| 4829 | thd->protocol_text.send_out_parameters(&this->lex->param_list); |
| 4830 | thd->get_stmt_da()->set_overwrite_status(false); |
| 4831 | } |
| 4832 | else |
| 4833 | thd->protocol->send_out_parameters(&this->lex->param_list); |
| 4834 | } |
| 4835 | |
| 4836 | /* |
| 4837 | Log COM_EXECUTE to the general log. Note, that in case of SQL |
| 4838 | prepared statements this causes two records to be output: |
| 4839 | |
| 4840 | Query EXECUTE <statement name> |
| 4841 | Execute <statement SQL text> |
| 4842 | |
| 4843 | This is considered user-friendly, since in the |
| 4844 | second log entry we output values of parameter markers. |
| 4845 | |
| 4846 | Do not print anything if this is an SQL prepared statement and |
| 4847 | we're inside a stored procedure (also called Dynamic SQL) -- |
| 4848 | sub-statements inside stored procedures are not logged into |
| 4849 | the general log. |
| 4850 | */ |
| 4851 | if (likely(error == 0 && thd->spcont == NULL)) |
| 4852 | general_log_write(thd, COM_STMT_EXECUTE, thd->query(), thd->query_length()); |
| 4853 | |
| 4854 | error: |
| 4855 | thd->lex->restore_set_statement_var(); |
| 4856 | flags&= ~ (uint) IS_IN_USE; |
| 4857 | return error; |
| 4858 | } |
| 4859 | |
| 4860 | |
| 4861 | /** |
| 4862 | Prepare, execute and clean-up a statement. |
| 4863 | @param query - query text |
| 4864 | @param length - query text length |
| 4865 | @retval true - the query was not executed (parse error, wrong parameters) |
| 4866 | @retval false - the query was prepared and executed |
| 4867 | |
| 4868 | Note, if some error happened during execution, it still returns "false". |
| 4869 | */ |
| 4870 | bool Prepared_statement::execute_immediate(const char *query, uint query_len) |
| 4871 | { |
| 4872 | DBUG_ENTER("Prepared_statement::execute_immediate" ); |
| 4873 | String expanded_query; |
| 4874 | static LEX_CSTRING execute_immediate_stmt_name= |
| 4875 | {STRING_WITH_LEN("(immediate)" ) }; |
| 4876 | |
| 4877 | set_sql_prepare(); |
| 4878 | name= execute_immediate_stmt_name; // for DBUG_PRINT etc |
| 4879 | if (unlikely(prepare(query, query_len))) |
| 4880 | DBUG_RETURN(true); |
| 4881 | |
| 4882 | if (param_count != thd->lex->prepared_stmt_params.elements) |
| 4883 | { |
| 4884 | my_error(ER_WRONG_ARGUMENTS, MYF(0), "EXECUTE" ); |
| 4885 | deallocate_immediate(); |
| 4886 | DBUG_RETURN(true); |
| 4887 | } |
| 4888 | |
| 4889 | (void) execute_loop(&expanded_query, FALSE, NULL, NULL); |
| 4890 | deallocate_immediate(); |
| 4891 | DBUG_RETURN(false); |
| 4892 | } |
| 4893 | |
| 4894 | |
| 4895 | /** |
| 4896 | Common part of DEALLOCATE PREPARE, EXECUTE IMMEDIATE, mysqld_stmt_close. |
| 4897 | */ |
| 4898 | void Prepared_statement::deallocate_immediate() |
| 4899 | { |
| 4900 | /* We account deallocate in the same manner as mysqld_stmt_close */ |
| 4901 | status_var_increment(thd->status_var.com_stmt_close); |
| 4902 | |
| 4903 | /* It should now be safe to reset CHANGE MASTER parameters */ |
| 4904 | lex_end_stage2(lex); |
| 4905 | } |
| 4906 | |
| 4907 | |
| 4908 | /** Common part of DEALLOCATE PREPARE and mysqld_stmt_close. */ |
| 4909 | |
| 4910 | void Prepared_statement::deallocate() |
| 4911 | { |
| 4912 | deallocate_immediate(); |
| 4913 | /* Statement map calls delete stmt on erase */ |
| 4914 | thd->stmt_map.erase(this); |
| 4915 | } |
| 4916 | |
| 4917 | |
| 4918 | /*************************************************************************** |
| 4919 | * Ed_result_set |
| 4920 | ***************************************************************************/ |
| 4921 | /** |
| 4922 | Use operator delete to free memory of Ed_result_set. |
| 4923 | Accessing members of a class after the class has been destroyed |
| 4924 | is a violation of the C++ standard but is commonly used in the |
| 4925 | server code. |
| 4926 | */ |
| 4927 | |
| 4928 | void Ed_result_set::operator delete(void *ptr, size_t size) throw () |
| 4929 | { |
| 4930 | if (ptr) |
| 4931 | { |
| 4932 | /* |
| 4933 | Make a stack copy, otherwise free_root() will attempt to |
| 4934 | write to freed memory. |
| 4935 | */ |
| 4936 | MEM_ROOT own_root= ((Ed_result_set*) ptr)->m_mem_root; |
| 4937 | free_root(&own_root, MYF(0)); |
| 4938 | } |
| 4939 | } |
| 4940 | |
| 4941 | |
| 4942 | /** |
| 4943 | Initialize an instance of Ed_result_set. |
| 4944 | |
| 4945 | Instances of the class, as well as all result set rows, are |
| 4946 | always allocated in the memory root passed over as the second |
| 4947 | argument. In the constructor, we take over ownership of the |
| 4948 | memory root. It will be freed when the class is destroyed. |
| 4949 | |
| 4950 | sic: Ed_result_est is not designed to be allocated on stack. |
| 4951 | */ |
| 4952 | |
| 4953 | Ed_result_set::Ed_result_set(List<Ed_row> *rows_arg, |
| 4954 | size_t column_count_arg, |
| 4955 | MEM_ROOT *mem_root_arg) |
| 4956 | :m_mem_root(*mem_root_arg), |
| 4957 | m_column_count(column_count_arg), |
| 4958 | m_rows(rows_arg), |
| 4959 | m_next_rset(NULL) |
| 4960 | { |
| 4961 | /* Take over responsibility for the memory */ |
| 4962 | clear_alloc_root(mem_root_arg); |
| 4963 | } |
| 4964 | |
| 4965 | /*************************************************************************** |
| 4966 | * Ed_result_set |
| 4967 | ***************************************************************************/ |
| 4968 | |
| 4969 | /** |
| 4970 | Create a new "execute direct" connection. |
| 4971 | */ |
| 4972 | |
| 4973 | Ed_connection::Ed_connection(THD *thd) |
| 4974 | :m_diagnostics_area(thd->query_id, false, true), |
| 4975 | m_thd(thd), |
| 4976 | m_rsets(0), |
| 4977 | m_current_rset(0) |
| 4978 | { |
| 4979 | } |
| 4980 | |
| 4981 | |
| 4982 | /** |
| 4983 | Free all result sets of the previous statement, if any, |
| 4984 | and reset warnings and errors. |
| 4985 | |
| 4986 | Called before execution of the next query. |
| 4987 | */ |
| 4988 | |
| 4989 | void |
| 4990 | Ed_connection::free_old_result() |
| 4991 | { |
| 4992 | while (m_rsets) |
| 4993 | { |
| 4994 | Ed_result_set *rset= m_rsets->m_next_rset; |
| 4995 | delete m_rsets; |
| 4996 | m_rsets= rset; |
| 4997 | } |
| 4998 | m_current_rset= m_rsets; |
| 4999 | m_diagnostics_area.reset_diagnostics_area(); |
| 5000 | m_diagnostics_area.clear_warning_info(m_thd->query_id); |
| 5001 | } |
| 5002 | |
| 5003 | |
| 5004 | /** |
| 5005 | A simple wrapper that uses a helper class to execute SQL statements. |
| 5006 | */ |
| 5007 | |
| 5008 | bool |
| 5009 | Ed_connection::execute_direct(LEX_STRING sql_text) |
| 5010 | { |
| 5011 | Execute_sql_statement execute_sql_statement(sql_text); |
| 5012 | DBUG_PRINT("ed_query" , ("%s" , sql_text.str)); |
| 5013 | |
| 5014 | return execute_direct(&execute_sql_statement); |
| 5015 | } |
| 5016 | |
| 5017 | |
| 5018 | /** |
| 5019 | Execute a fragment of server functionality without an effect on |
| 5020 | thd, and store results in memory. |
| 5021 | |
| 5022 | Conventions: |
| 5023 | - the code fragment must finish with OK, EOF or ERROR. |
| 5024 | - the code fragment doesn't have to close thread tables, |
| 5025 | free memory, commit statement transaction or do any other |
| 5026 | cleanup that is normally done in the end of dispatch_command(). |
| 5027 | |
| 5028 | @param server_runnable A code fragment to execute. |
| 5029 | */ |
| 5030 | |
| 5031 | bool Ed_connection::execute_direct(Server_runnable *server_runnable) |
| 5032 | { |
| 5033 | bool rc= FALSE; |
| 5034 | Protocol_local protocol_local(m_thd, this); |
| 5035 | Prepared_statement stmt(m_thd); |
| 5036 | Protocol *save_protocol= m_thd->protocol; |
| 5037 | Diagnostics_area *save_diagnostics_area= m_thd->get_stmt_da(); |
| 5038 | |
| 5039 | DBUG_ENTER("Ed_connection::execute_direct" ); |
| 5040 | |
| 5041 | free_old_result(); /* Delete all data from previous execution, if any */ |
| 5042 | |
| 5043 | m_thd->protocol= &protocol_local; |
| 5044 | m_thd->set_stmt_da(&m_diagnostics_area); |
| 5045 | |
| 5046 | rc= stmt.execute_server_runnable(server_runnable); |
| 5047 | m_thd->protocol->end_statement(); |
| 5048 | |
| 5049 | m_thd->protocol= save_protocol; |
| 5050 | m_thd->set_stmt_da(save_diagnostics_area); |
| 5051 | /* |
| 5052 | Protocol_local makes use of m_current_rset to keep |
| 5053 | track of the last result set, while adding result sets to the end. |
| 5054 | Reset it to point to the first result set instead. |
| 5055 | */ |
| 5056 | m_current_rset= m_rsets; |
| 5057 | |
| 5058 | DBUG_RETURN(rc); |
| 5059 | } |
| 5060 | |
| 5061 | |
| 5062 | /** |
| 5063 | A helper method that is called only during execution. |
| 5064 | |
| 5065 | Although Ed_connection doesn't support multi-statements, |
| 5066 | a statement may generate many result sets. All subsequent |
| 5067 | result sets are appended to the end. |
| 5068 | |
| 5069 | @pre This is called only by Protocol_local. |
| 5070 | */ |
| 5071 | |
| 5072 | void |
| 5073 | Ed_connection::add_result_set(Ed_result_set *ed_result_set) |
| 5074 | { |
| 5075 | if (m_rsets) |
| 5076 | { |
| 5077 | m_current_rset->m_next_rset= ed_result_set; |
| 5078 | /* While appending, use m_current_rset as a pointer to the tail. */ |
| 5079 | m_current_rset= ed_result_set; |
| 5080 | } |
| 5081 | else |
| 5082 | m_current_rset= m_rsets= ed_result_set; |
| 5083 | } |
| 5084 | |
| 5085 | |
| 5086 | /** |
| 5087 | Release ownership of the current result set to the client. |
| 5088 | |
| 5089 | Since we use a simple linked list for result sets, |
| 5090 | this method uses a linear search of the previous result |
| 5091 | set to exclude the released instance from the list. |
| 5092 | |
| 5093 | @todo Use double-linked list, when this is really used. |
| 5094 | |
| 5095 | XXX: This has never been tested with more than one result set! |
| 5096 | |
| 5097 | @pre There must be a result set. |
| 5098 | */ |
| 5099 | |
| 5100 | Ed_result_set * |
| 5101 | Ed_connection::store_result_set() |
| 5102 | { |
| 5103 | Ed_result_set *ed_result_set; |
| 5104 | |
| 5105 | DBUG_ASSERT(m_current_rset); |
| 5106 | |
| 5107 | if (m_current_rset == m_rsets) |
| 5108 | { |
| 5109 | /* Assign the return value */ |
| 5110 | ed_result_set= m_current_rset; |
| 5111 | /* Exclude the return value from the list. */ |
| 5112 | m_current_rset= m_rsets= m_rsets->m_next_rset; |
| 5113 | } |
| 5114 | else |
| 5115 | { |
| 5116 | Ed_result_set *prev_rset= m_rsets; |
| 5117 | /* Assign the return value. */ |
| 5118 | ed_result_set= m_current_rset; |
| 5119 | |
| 5120 | /* Exclude the return value from the list */ |
| 5121 | while (prev_rset->m_next_rset != m_current_rset) |
| 5122 | prev_rset= ed_result_set->m_next_rset; |
| 5123 | m_current_rset= prev_rset->m_next_rset= m_current_rset->m_next_rset; |
| 5124 | } |
| 5125 | ed_result_set->m_next_rset= NULL; /* safety */ |
| 5126 | |
| 5127 | return ed_result_set; |
| 5128 | } |
| 5129 | |
| 5130 | /************************************************************************* |
| 5131 | * Protocol_local |
| 5132 | **************************************************************************/ |
| 5133 | |
| 5134 | Protocol_local::Protocol_local(THD *thd, Ed_connection *ed_connection) |
| 5135 | :Protocol(thd), |
| 5136 | m_connection(ed_connection), |
| 5137 | m_rset(NULL), |
| 5138 | m_column_count(0), |
| 5139 | m_current_row(NULL), |
| 5140 | m_current_column(NULL) |
| 5141 | { |
| 5142 | clear_alloc_root(&m_rset_root); |
| 5143 | } |
| 5144 | |
| 5145 | /** |
| 5146 | Called between two result set rows. |
| 5147 | |
| 5148 | Prepare structures to fill result set rows. |
| 5149 | Unfortunately, we can't return an error here. If memory allocation |
| 5150 | fails, we'll have to return an error later. And so is done |
| 5151 | in methods such as @sa store_column(). |
| 5152 | */ |
| 5153 | |
| 5154 | void Protocol_local::prepare_for_resend() |
| 5155 | { |
| 5156 | DBUG_ASSERT(alloc_root_inited(&m_rset_root)); |
| 5157 | |
| 5158 | opt_add_row_to_rset(); |
| 5159 | /* Start a new row. */ |
| 5160 | m_current_row= (Ed_column *) alloc_root(&m_rset_root, |
| 5161 | sizeof(Ed_column) * m_column_count); |
| 5162 | m_current_column= m_current_row; |
| 5163 | } |
| 5164 | |
| 5165 | |
| 5166 | /** |
| 5167 | In "real" protocols this is called to finish a result set row. |
| 5168 | Unused in the local implementation. |
| 5169 | */ |
| 5170 | |
| 5171 | bool Protocol_local::write() |
| 5172 | { |
| 5173 | return FALSE; |
| 5174 | } |
| 5175 | |
| 5176 | /** |
| 5177 | A helper function to add the current row to the current result |
| 5178 | set. Called in @sa prepare_for_resend(), when a new row is started, |
| 5179 | and in send_eof(), when the result set is finished. |
| 5180 | */ |
| 5181 | |
| 5182 | void Protocol_local::opt_add_row_to_rset() |
| 5183 | { |
| 5184 | if (m_current_row) |
| 5185 | { |
| 5186 | /* Add the old row to the result set */ |
| 5187 | Ed_row *ed_row= new (&m_rset_root) Ed_row(m_current_row, m_column_count); |
| 5188 | if (ed_row) |
| 5189 | m_rset->push_back(ed_row, &m_rset_root); |
| 5190 | } |
| 5191 | } |
| 5192 | |
| 5193 | |
| 5194 | /** |
| 5195 | Add a NULL column to the current row. |
| 5196 | */ |
| 5197 | |
| 5198 | bool Protocol_local::store_null() |
| 5199 | { |
| 5200 | if (m_current_column == NULL) |
| 5201 | return TRUE; /* prepare_for_resend() failed to allocate memory. */ |
| 5202 | |
| 5203 | bzero(m_current_column, sizeof(*m_current_column)); |
| 5204 | ++m_current_column; |
| 5205 | return FALSE; |
| 5206 | } |
| 5207 | |
| 5208 | |
| 5209 | /** |
| 5210 | A helper method to add any column to the current row |
| 5211 | in its binary form. |
| 5212 | |
| 5213 | Allocates memory for the data in the result set memory root. |
| 5214 | */ |
| 5215 | |
| 5216 | bool Protocol_local::store_column(const void *data, size_t length) |
| 5217 | { |
| 5218 | if (m_current_column == NULL) |
| 5219 | return TRUE; /* prepare_for_resend() failed to allocate memory. */ |
| 5220 | /* |
| 5221 | alloc_root() automatically aligns memory, so we don't need to |
| 5222 | do any extra alignment if we're pointing to, say, an integer. |
| 5223 | */ |
| 5224 | m_current_column->str= (char*) memdup_root(&m_rset_root, |
| 5225 | data, |
| 5226 | length + 1 /* Safety */); |
| 5227 | if (! m_current_column->str) |
| 5228 | return TRUE; |
| 5229 | m_current_column->str[length]= '\0'; /* Safety */ |
| 5230 | m_current_column->length= length; |
| 5231 | ++m_current_column; |
| 5232 | return FALSE; |
| 5233 | } |
| 5234 | |
| 5235 | |
| 5236 | /** |
| 5237 | Store a string value in a result set column, optionally |
| 5238 | having converted it to character_set_results. |
| 5239 | */ |
| 5240 | |
| 5241 | bool |
| 5242 | Protocol_local::store_string(const char *str, size_t length, |
| 5243 | CHARSET_INFO *src_cs, CHARSET_INFO *dst_cs) |
| 5244 | { |
| 5245 | /* Store with conversion */ |
| 5246 | uint error_unused; |
| 5247 | |
| 5248 | if (dst_cs && !my_charset_same(src_cs, dst_cs) && |
| 5249 | src_cs != &my_charset_bin && |
| 5250 | dst_cs != &my_charset_bin) |
| 5251 | { |
| 5252 | if (unlikely(convert->copy(str, length, src_cs, dst_cs, &error_unused))) |
| 5253 | return TRUE; |
| 5254 | str= convert->ptr(); |
| 5255 | length= convert->length(); |
| 5256 | } |
| 5257 | return store_column(str, length); |
| 5258 | } |
| 5259 | |
| 5260 | |
| 5261 | /** Store a tiny int as is (1 byte) in a result set column. */ |
| 5262 | |
| 5263 | bool Protocol_local::store_tiny(longlong value) |
| 5264 | { |
| 5265 | char v= (char) value; |
| 5266 | return store_column(&v, 1); |
| 5267 | } |
| 5268 | |
| 5269 | |
| 5270 | /** Store a short as is (2 bytes, host order) in a result set column. */ |
| 5271 | |
| 5272 | bool Protocol_local::store_short(longlong value) |
| 5273 | { |
| 5274 | int16 v= (int16) value; |
| 5275 | return store_column(&v, 2); |
| 5276 | } |
| 5277 | |
| 5278 | |
| 5279 | /** Store a "long" as is (4 bytes, host order) in a result set column. */ |
| 5280 | |
| 5281 | bool Protocol_local::store_long(longlong value) |
| 5282 | { |
| 5283 | int32 v= (int32) value; |
| 5284 | return store_column(&v, 4); |
| 5285 | } |
| 5286 | |
| 5287 | |
| 5288 | /** Store a "longlong" as is (8 bytes, host order) in a result set column. */ |
| 5289 | |
| 5290 | bool Protocol_local::store_longlong(longlong value, bool unsigned_flag) |
| 5291 | { |
| 5292 | int64 v= (int64) value; |
| 5293 | return store_column(&v, 8); |
| 5294 | } |
| 5295 | |
| 5296 | |
| 5297 | /** Store a decimal in string format in a result set column */ |
| 5298 | |
| 5299 | bool Protocol_local::store_decimal(const my_decimal *value) |
| 5300 | { |
| 5301 | char buf[DECIMAL_MAX_STR_LENGTH]; |
| 5302 | String str(buf, sizeof (buf), &my_charset_bin); |
| 5303 | int rc; |
| 5304 | |
| 5305 | rc= my_decimal2string(E_DEC_FATAL_ERROR, value, 0, 0, 0, &str); |
| 5306 | |
| 5307 | if (rc) |
| 5308 | return TRUE; |
| 5309 | |
| 5310 | return store_column(str.ptr(), str.length()); |
| 5311 | } |
| 5312 | |
| 5313 | |
| 5314 | /** Convert to cs_results and store a string. */ |
| 5315 | |
| 5316 | bool Protocol_local::store(const char *str, size_t length, |
| 5317 | CHARSET_INFO *src_cs) |
| 5318 | { |
| 5319 | CHARSET_INFO *dst_cs; |
| 5320 | |
| 5321 | dst_cs= m_connection->m_thd->variables.character_set_results; |
| 5322 | return store_string(str, length, src_cs, dst_cs); |
| 5323 | } |
| 5324 | |
| 5325 | |
| 5326 | /** Store a string. */ |
| 5327 | |
| 5328 | bool Protocol_local::store(const char *str, size_t length, |
| 5329 | CHARSET_INFO *src_cs, CHARSET_INFO *dst_cs) |
| 5330 | { |
| 5331 | return store_string(str, length, src_cs, dst_cs); |
| 5332 | } |
| 5333 | |
| 5334 | |
| 5335 | /* Store MYSQL_TIME (in binary format) */ |
| 5336 | |
| 5337 | bool Protocol_local::store(MYSQL_TIME *time, int decimals) |
| 5338 | { |
| 5339 | if (decimals != AUTO_SEC_PART_DIGITS) |
| 5340 | my_time_trunc(time, decimals); |
| 5341 | return store_column(time, sizeof(MYSQL_TIME)); |
| 5342 | } |
| 5343 | |
| 5344 | |
| 5345 | /** Store MYSQL_TIME (in binary format) */ |
| 5346 | |
| 5347 | bool Protocol_local::store_date(MYSQL_TIME *time) |
| 5348 | { |
| 5349 | return store_column(time, sizeof(MYSQL_TIME)); |
| 5350 | } |
| 5351 | |
| 5352 | |
| 5353 | /** Store MYSQL_TIME (in binary format) */ |
| 5354 | |
| 5355 | bool Protocol_local::store_time(MYSQL_TIME *time, int decimals) |
| 5356 | { |
| 5357 | if (decimals != AUTO_SEC_PART_DIGITS) |
| 5358 | my_time_trunc(time, decimals); |
| 5359 | return store_column(time, sizeof(MYSQL_TIME)); |
| 5360 | } |
| 5361 | |
| 5362 | |
| 5363 | /* Store a floating point number, as is. */ |
| 5364 | |
| 5365 | bool Protocol_local::store(float value, uint32 decimals, String *buffer) |
| 5366 | { |
| 5367 | return store_column(&value, sizeof(float)); |
| 5368 | } |
| 5369 | |
| 5370 | |
| 5371 | /* Store a double precision number, as is. */ |
| 5372 | |
| 5373 | bool Protocol_local::store(double value, uint32 decimals, String *buffer) |
| 5374 | { |
| 5375 | return store_column(&value, sizeof (double)); |
| 5376 | } |
| 5377 | |
| 5378 | |
| 5379 | /* Store a Field. */ |
| 5380 | |
| 5381 | bool Protocol_local::store(Field *field) |
| 5382 | { |
| 5383 | if (field->is_null()) |
| 5384 | return store_null(); |
| 5385 | return field->send_binary(this); |
| 5386 | } |
| 5387 | |
| 5388 | |
| 5389 | /** Called to start a new result set. */ |
| 5390 | |
| 5391 | bool Protocol_local::send_result_set_metadata(List<Item> *columns, uint) |
| 5392 | { |
| 5393 | DBUG_ASSERT(m_rset == 0 && !alloc_root_inited(&m_rset_root)); |
| 5394 | |
| 5395 | init_sql_alloc(&m_rset_root, "send_result_set_metadata" , |
| 5396 | MEM_ROOT_BLOCK_SIZE, 0, MYF(MY_THREAD_SPECIFIC)); |
| 5397 | |
| 5398 | if (! (m_rset= new (&m_rset_root) List<Ed_row>)) |
| 5399 | return TRUE; |
| 5400 | |
| 5401 | m_column_count= columns->elements; |
| 5402 | |
| 5403 | return FALSE; |
| 5404 | } |
| 5405 | |
| 5406 | |
| 5407 | /** |
| 5408 | Normally this is a separate result set with OUT parameters |
| 5409 | of stored procedures. Currently unsupported for the local |
| 5410 | version. |
| 5411 | */ |
| 5412 | |
| 5413 | bool Protocol_local::send_out_parameters(List<Item_param> *sp_params) |
| 5414 | { |
| 5415 | return FALSE; |
| 5416 | } |
| 5417 | |
| 5418 | |
| 5419 | /** Called for statements that don't have a result set, at statement end. */ |
| 5420 | |
| 5421 | bool |
| 5422 | Protocol_local::send_ok(uint server_status, uint statement_warn_count, |
| 5423 | ulonglong affected_rows, ulonglong last_insert_id, |
| 5424 | const char *message, bool skip_flush) |
| 5425 | { |
| 5426 | /* |
| 5427 | Just make sure nothing is sent to the client, we have grabbed |
| 5428 | the status information in the connection diagnostics area. |
| 5429 | */ |
| 5430 | return FALSE; |
| 5431 | } |
| 5432 | |
| 5433 | |
| 5434 | /** |
| 5435 | Called at the end of a result set. Append a complete |
| 5436 | result set to the list in Ed_connection. |
| 5437 | |
| 5438 | Don't send anything to the client, but instead finish |
| 5439 | building of the result set at hand. |
| 5440 | */ |
| 5441 | |
| 5442 | bool Protocol_local::send_eof(uint server_status, uint statement_warn_count) |
| 5443 | { |
| 5444 | Ed_result_set *ed_result_set; |
| 5445 | |
| 5446 | DBUG_ASSERT(m_rset); |
| 5447 | |
| 5448 | opt_add_row_to_rset(); |
| 5449 | m_current_row= 0; |
| 5450 | |
| 5451 | ed_result_set= new (&m_rset_root) Ed_result_set(m_rset, m_column_count, |
| 5452 | &m_rset_root); |
| 5453 | |
| 5454 | m_rset= NULL; |
| 5455 | |
| 5456 | if (! ed_result_set) |
| 5457 | return TRUE; |
| 5458 | |
| 5459 | /* In case of successful allocation memory ownership was transferred. */ |
| 5460 | DBUG_ASSERT(!alloc_root_inited(&m_rset_root)); |
| 5461 | |
| 5462 | /* |
| 5463 | Link the created Ed_result_set instance into the list of connection |
| 5464 | result sets. Never fails. |
| 5465 | */ |
| 5466 | m_connection->add_result_set(ed_result_set); |
| 5467 | return FALSE; |
| 5468 | } |
| 5469 | |
| 5470 | |
| 5471 | /** Called to send an error to the client at the end of a statement. */ |
| 5472 | |
| 5473 | bool |
| 5474 | Protocol_local::send_error(uint sql_errno, const char *err_msg, const char*) |
| 5475 | { |
| 5476 | /* |
| 5477 | Just make sure that nothing is sent to the client (default |
| 5478 | implementation). |
| 5479 | */ |
| 5480 | return FALSE; |
| 5481 | } |
| 5482 | |
| 5483 | |
| 5484 | #ifdef EMBEDDED_LIBRARY |
| 5485 | void Protocol_local::remove_last_row() |
| 5486 | { } |
| 5487 | #endif |
| 5488 | |