86 #include "my_global.h"
89 #include "sql_class.h"
91 #include "sql_prepare.h"
92 #include "sql_parse.h"
94 #include "sql_cache.h"
96 #include "sql_delete.h"
98 #include "sql_insert.h"
99 #include "sql_update.h"
105 #include "sp_cache.h"
106 #include "sql_handler.h"
107 #include "probes_mysql.h"
108 #ifdef EMBEDDED_LIBRARY
112 #include <mysql_com.h>
116 #include "sql_analyse.h"
117 #include "sql_rewrite.h"
118 #include "transaction.h"
135 virtual bool send_eof();
136 #ifdef EMBEDDED_LIBRARY
139 protocol.begin_dataset();
166 char last_error[MYSQL_ERRMSG_SIZE];
167 #ifndef EMBEDDED_LIBRARY
169 uchar *read_pos,
String *expanded_query);
179 void setup_set_params();
180 virtual Query_arena::Type type()
const;
181 virtual void cleanup_stmt();
183 inline void close_cursor() {
delete cursor; cursor= 0; }
184 inline bool is_in_use() {
return flags & (uint) IS_IN_USE; }
185 inline bool is_sql_prepare()
const {
return flags & (uint) IS_SQL_PREPARE; }
186 void set_sql_prepare() { flags|= (uint) IS_SQL_PREPARE; }
187 bool prepare(
const char *packet, uint packet_length);
190 uchar *packet_arg, uchar *packet_end_arg);
201 bool set_db(
const char *db, uint db_length);
202 bool set_parameters(
String *expanded_query,
203 uchar *packet, uchar *packet_end);
204 bool execute(
String *expanded_query,
bool open_cursor);
238 virtual bool write();
246 virtual bool store(
const char *from,
size_t length,
251 virtual bool store(
float value, uint32 decimals,
String *buffer);
252 virtual bool store(
double value, uint32 decimals,
String *buffer);
257 #ifdef EMBEDDED_LIBRARY
258 void remove_last_row();
260 virtual enum enum_protocol_type type() {
return PROTOCOL_LOCAL; };
262 virtual bool send_ok(uint server_status, uint statement_warn_count,
263 ulonglong affected_rows, ulonglong last_insert_id,
266 virtual bool send_eof(uint server_status, uint statement_warn_count);
267 virtual bool send_error(uint sql_errno,
const char *err_msg,
const char* sqlstate);
269 bool store_string(
const char *str,
size_t length,
272 bool store_column(
const void *data,
size_t length);
273 void opt_add_row_to_rset();
278 size_t m_column_count;
287 static inline void rewrite_query_if_needed(THD *thd)
289 bool general= (opt_log &&
290 !(opt_log_raw || thd->slave_thread));
292 if ((thd->sp_runtime_ctx == NULL) &&
293 (general || opt_slow_log || opt_bin_log))
294 mysql_rewrite_query(thd);
297 static inline void log_execute_line(THD *thd)
305 if (thd->sp_runtime_ctx != NULL)
308 if (thd->rewritten_query.length())
309 logger.general_log_write(thd, COM_STMT_EXECUTE,
310 thd->rewritten_query.c_ptr_safe(),
311 thd->rewritten_query.length());
313 logger.general_log_write(thd, COM_STMT_EXECUTE,
314 thd->query(), thd->query_length());
318 inline bool is_param_null(
const uchar *pos, ulong param_no)
320 return pos[param_no/8] & (1 << (param_no & 7));
338 find_prepared_statement(THD *thd, ulong
id)
345 Statement *stmt= thd->stmt_map.find(
id);
347 if (stmt == 0 || stmt->type() != Query_arena::PREPARED_STATEMENT)
364 #ifndef EMBEDDED_LIBRARY
367 NET *net= &stmt->thd->net;
372 DBUG_ENTER(
"send_prep_stmt");
375 int4store(buff+1, stmt->id);
376 int2store(buff+5, columns);
377 int2store(buff+7, stmt->param_count);
379 tmp= min(stmt->thd->get_stmt_da()->current_statement_warn_count(), 65535UL);
380 int2store(buff+10, tmp);
387 if (stmt->param_count && ! error)
389 error= thd->protocol_text.send_result_set_metadata((
List<Item> *)
390 &stmt->lex->param_list,
396 thd->get_stmt_da()->disable_status();
402 uint columns __attribute__((unused)))
406 thd->client_stmt_id= stmt->id;
407 thd->client_param_count= stmt->param_count;
409 thd->get_stmt_da()->disable_status();
416 #ifndef EMBEDDED_LIBRARY
432 static ulong get_param_length(uchar **packet, ulong len)
434 reg1 uchar *pos= *packet;
447 return (ulong) uint2korr(pos+1);
454 return (ulong) uint3korr(pos+1);
466 return (ulong) uint4korr(pos+1);
469 #define get_param_length(packet, len) len
487 static void set_param_tiny(Item_param *param, uchar **pos, ulong len)
489 #ifndef EMBEDDED_LIBRARY
493 int8 value= (int8) **pos;
494 param->set_int(param->unsigned_flag ? (longlong) ((uint8) value) :
495 (longlong) value, 4);
499 static void set_param_short(
Item_param *param, uchar **pos, ulong len)
502 #ifndef EMBEDDED_LIBRARY
505 value= sint2korr(*pos);
507 shortget(value, *pos);
509 param->set_int(param->unsigned_flag ? (longlong) ((uint16) value) :
510 (longlong) value, 6);
514 static void set_param_int32(
Item_param *param, uchar **pos, ulong len)
517 #ifndef EMBEDDED_LIBRARY
520 value= sint4korr(*pos);
522 longget(value, *pos);
524 param->set_int(param->unsigned_flag ? (longlong) ((uint32) value) :
525 (longlong) value, 11);
529 static void set_param_int64(
Item_param *param, uchar **pos, ulong len)
532 #ifndef EMBEDDED_LIBRARY
535 value= (longlong) sint8korr(*pos);
537 longlongget(value, *pos);
539 param->set_int(value, 21);
543 static void set_param_float(
Item_param *param, uchar **pos, ulong len)
546 #ifndef EMBEDDED_LIBRARY
549 float4get(data,*pos);
551 floatget(data, *pos);
553 param->set_double((
double) data);
557 static void set_param_double(
Item_param *param, uchar **pos, ulong len)
560 #ifndef EMBEDDED_LIBRARY
563 float8get(data,*pos);
565 doubleget(data, *pos);
567 param->set_double((
double) data);
571 static void set_param_decimal(
Item_param *param, uchar **pos, ulong len)
573 ulong length= get_param_length(pos, len);
578 #ifndef EMBEDDED_LIBRARY
590 static void set_param_time(
Item_param *param, uchar **pos, ulong len)
593 ulong length= get_param_length(pos, len);
600 tm.neg= (bool) to[0];
601 day= (uint) sint4korr(to+1);
602 tm.hour= (uint) to[5] + day * 24;
603 tm.minute= (uint) to[6];
604 tm.second= (uint) to[7];
605 tm.
second_part= (length > 8) ? (ulong) sint4korr(to+8) : 0;
613 tm.day= tm.year= tm.month= 0;
616 set_zero_time(&tm, MYSQL_TIMESTAMP_TIME);
617 param->
set_time(&tm, MYSQL_TIMESTAMP_TIME,
618 MAX_TIME_FULL_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
622 static void set_param_datetime(
Item_param *param, uchar **pos, ulong len)
625 ulong length= get_param_length(pos, len);
632 tm.year= (uint) sint2korr(to);
633 tm.month= (uint) to[2];
634 tm.day= (uint) to[3];
637 tm.hour= (uint) to[4];
638 tm.minute= (uint) to[5];
639 tm.second= (uint) to[6];
642 tm.hour= tm.minute= tm.second= 0;
644 tm.
second_part= (length > 7) ? (ulong) sint4korr(to+7) : 0;
647 set_zero_time(&tm, MYSQL_TIMESTAMP_DATETIME);
648 param->
set_time(&tm, MYSQL_TIMESTAMP_DATETIME,
649 MAX_DATETIME_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
654 static void set_param_date(
Item_param *param, uchar **pos, ulong len)
657 ulong length= get_param_length(pos, len);
663 tm.year= (uint) sint2korr(to);
664 tm.month= (uint) to[2];
665 tm.day= (uint) to[3];
667 tm.hour= tm.minute= tm.second= 0;
672 set_zero_time(&tm, MYSQL_TIMESTAMP_DATE);
673 param->
set_time(&tm, MYSQL_TIMESTAMP_DATE,
674 MAX_DATE_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
683 void set_param_time(Item_param *param, uchar **pos, ulong len)
686 tm.hour+= tm.day * 24;
687 tm.day= tm.year= tm.month= 0;
695 param->
set_time(&tm, MYSQL_TIMESTAMP_TIME,
696 MAX_TIME_FULL_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
700 void set_param_datetime(
Item_param *param, uchar **pos, ulong len)
705 param->
set_time(&tm, MYSQL_TIMESTAMP_DATETIME,
706 MAX_DATETIME_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
709 void set_param_date(
Item_param *param, uchar **pos, ulong len)
713 param->
set_time(to, MYSQL_TIMESTAMP_DATE,
714 MAX_DATE_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
719 static void set_param_str(Item_param *param, uchar **pos, ulong len)
721 ulong length= get_param_length(pos, len);
724 param->set_str((
const char *)*pos, length);
729 #undef get_param_length
731 static void setup_one_conversion_function(THD *thd,
Item_param *param,
734 switch (param_type) {
735 case MYSQL_TYPE_TINY:
736 param->set_param_func= set_param_tiny;
737 param->item_type= Item::INT_ITEM;
738 param->item_result_type= INT_RESULT;
740 case MYSQL_TYPE_SHORT:
741 param->set_param_func= set_param_short;
742 param->item_type= Item::INT_ITEM;
743 param->item_result_type= INT_RESULT;
745 case MYSQL_TYPE_LONG:
746 param->set_param_func= set_param_int32;
747 param->item_type= Item::INT_ITEM;
748 param->item_result_type= INT_RESULT;
750 case MYSQL_TYPE_LONGLONG:
751 param->set_param_func= set_param_int64;
752 param->item_type= Item::INT_ITEM;
753 param->item_result_type= INT_RESULT;
755 case MYSQL_TYPE_FLOAT:
756 param->set_param_func= set_param_float;
757 param->item_type= Item::REAL_ITEM;
758 param->item_result_type= REAL_RESULT;
760 case MYSQL_TYPE_DOUBLE:
761 param->set_param_func= set_param_double;
762 param->item_type= Item::REAL_ITEM;
763 param->item_result_type= REAL_RESULT;
765 case MYSQL_TYPE_DECIMAL:
766 case MYSQL_TYPE_NEWDECIMAL:
767 param->set_param_func= set_param_decimal;
768 param->item_type= Item::DECIMAL_ITEM;
769 param->item_result_type= DECIMAL_RESULT;
771 case MYSQL_TYPE_TIME:
772 param->set_param_func= set_param_time;
773 param->item_type= Item::STRING_ITEM;
774 param->item_result_type= STRING_RESULT;
776 case MYSQL_TYPE_DATE:
777 param->set_param_func= set_param_date;
778 param->item_type= Item::STRING_ITEM;
779 param->item_result_type= STRING_RESULT;
781 case MYSQL_TYPE_DATETIME:
782 case MYSQL_TYPE_TIMESTAMP:
783 param->set_param_func= set_param_datetime;
784 param->item_type= Item::STRING_ITEM;
785 param->item_result_type= STRING_RESULT;
787 case MYSQL_TYPE_TINY_BLOB:
788 case MYSQL_TYPE_MEDIUM_BLOB:
789 case MYSQL_TYPE_LONG_BLOB:
790 case MYSQL_TYPE_BLOB:
791 param->set_param_func= set_param_str;
792 param->value.cs_info.character_set_of_placeholder= &my_charset_bin;
793 param->value.cs_info.character_set_client=
794 thd->variables.character_set_client;
795 DBUG_ASSERT(thd->variables.character_set_client);
796 param->value.cs_info.final_character_set_of_str_value= &my_charset_bin;
797 param->item_type= Item::STRING_ITEM;
798 param->item_result_type= STRING_RESULT;
807 const CHARSET_INFO *fromcs= thd->variables.character_set_client;
808 const CHARSET_INFO *tocs= thd->variables.collation_connection;
811 param->value.cs_info.character_set_of_placeholder= fromcs;
812 param->value.cs_info.character_set_client= fromcs;
819 param->value.cs_info.final_character_set_of_str_value=
820 String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
822 param->set_param_func= set_param_str;
827 param->item_type= Item::STRING_ITEM;
828 param->item_result_type= STRING_RESULT;
831 param->param_type= (
enum enum_field_types) param_type;
834 #ifndef EMBEDDED_LIBRARY
843 return ((param->param_type >= MYSQL_TYPE_TINY_BLOB) &&
844 (param->param_type <= MYSQL_TYPE_STRING));
887 uchar *read_pos, uchar *data_end,
896 DBUG_ENTER(
"insert_params_with_log");
898 if (query->copy(stmt->query(), stmt->query_length(), default_charset_info))
904 if (param->state != Item_param::LONG_DATA_VALUE)
906 if (is_param_null(null_array, (uint) (it - begin)))
910 if (read_pos >= data_end)
912 param->set_param_func(param, &read_pos, (uint) (data_end - read_pos));
913 if (param->state == Item_param::NO_VALUE)
918 param->set_int(param->val_int(), MY_INT64_NUM_DECIMAL_DIGITS);
919 param->item_type= Item::INT_ITEM;
920 if (!param->unsigned_flag && param->value.integer < 0)
937 if (query->replace(param->pos_in_query+length, 1, *res))
940 length+= res->length()-1;
947 uchar *read_pos, uchar *data_end,
953 DBUG_ENTER(
"insert_params");
958 if (param->state != Item_param::LONG_DATA_VALUE)
960 if (is_param_null(null_array, (uint) (it - begin)))
964 if (read_pos >= data_end)
966 param->set_param_func(param, &read_pos, (uint) (data_end - read_pos));
967 if (param->state == Item_param::NO_VALUE)
987 uchar **data, uchar *data_end)
990 uchar *read_pos= *data + (stmt->param_count+7) / 8;
992 DBUG_ENTER(
"setup_conversion_functions");
994 if (read_pos >= data_end)
1005 THD *thd= stmt->thd;
1006 for (; it < end; ++it)
1009 const uint signed_bit= 1 << 15;
1011 if (read_pos >= data_end)
1014 typecode= sint2korr(read_pos);
1016 (**it).unsigned_flag=
test(typecode & signed_bit);
1017 setup_one_conversion_function(thd, *it, (uchar) (typecode & ~signed_bit));
1039 THD *thd= stmt->thd;
1042 MYSQL_BIND *client_param= stmt->thd->client_params;
1044 DBUG_ENTER(
"emb_insert_params");
1046 for (; it < end; ++it, ++client_param)
1049 setup_one_conversion_function(thd, param, client_param->buffer_type);
1050 if (param->state != Item_param::LONG_DATA_VALUE)
1052 if (*client_param->is_null)
1056 uchar *buff= (uchar*) client_param->buffer;
1057 param->unsigned_flag= client_param->is_unsigned;
1058 param->set_param_func(param, &buff,
1059 client_param->length ?
1060 *client_param->length :
1061 client_param->buffer_length);
1062 if (param->state == Item_param::NO_VALUE)
1076 THD *thd= stmt->thd;
1079 MYSQL_BIND *client_param= thd->client_params;
1085 DBUG_ENTER(
"emb_insert_params_with_log");
1087 if (query->copy(stmt->query(), stmt->query_length(), default_charset_info))
1090 for (; it < end; ++it, ++client_param)
1093 setup_one_conversion_function(thd, param, client_param->buffer_type);
1094 if (param->state != Item_param::LONG_DATA_VALUE)
1096 if (*client_param->is_null)
1100 uchar *buff= (uchar*)client_param->buffer;
1101 param->unsigned_flag= client_param->is_unsigned;
1102 param->set_param_func(param, &buff,
1103 client_param->length ?
1104 *client_param->length :
1105 client_param->buffer_length);
1106 if (param->state == Item_param::NO_VALUE)
1114 if (query->replace(param->pos_in_query+length, 1, *res))
1117 length+= res->length()-1;
1161 swap_parameter_array(
Item_param **param_array_dst,
1167 Item_param **end= param_array_dst + param_count;
1169 for (; dst < end; ++src, ++dst)
1186 String *query __attribute__((unused)))
1190 user_var_entry *
entry;
1193 DBUG_ENTER(
"insert_params_from_vars");
1199 entry= (user_var_entry*)my_hash_search(&stmt->thd->user_vars,
1200 (uchar*) varname->str,
1228 user_var_entry *
entry;
1234 THD *thd= stmt->thd;
1236 DBUG_ENTER(
"insert_params_from_vars_with_log");
1238 if (query->copy(stmt->query(), stmt->query_length(), default_charset_info))
1246 entry= (user_var_entry *) my_hash_search(&thd->user_vars, (uchar*)
1247 varname->str, varname->length);
1253 setup_one_conversion_function(thd, param, param->param_type);
1261 if (query->replace(param->pos_in_query+length, 1, *val))
1263 length+= val->length()-1;
1286 enum_duplicates duplic)
1288 THD *thd= stmt->thd;
1291 DBUG_ENTER(
"mysql_test_insert");
1292 DBUG_ASSERT(stmt->is_stmt_prepare());
1299 if (table_list->lock_type != TL_WRITE_DELAYED)
1317 MYSQL_OPEN_FORCE_SHARED_MDL))
1320 if ((values= its++))
1324 Item *unused_conds= 0;
1326 if (table_list->table)
1329 table_list->table->insert_values=(uchar *)1;
1332 if (mysql_prepare_insert(thd, table_list, table_list->table,
1333 fields, values, update_fields, update_values,
1334 duplic, &unused_conds, FALSE, FALSE, FALSE))
1337 value_count= values->elements;
1340 if (table_list->lock_type == TL_WRITE_DELAYED &&
1341 !(table_list->table->file->
ha_table_flags() & HA_CAN_INSERT_DELAYED))
1343 my_error(ER_DELAYED_NOT_SUPPORTED, MYF(0), (table_list->view ?
1344 table_list->view_name.str :
1345 table_list->table_name));
1348 while ((values= its++))
1351 if (values->elements != value_count)
1353 my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
1356 if (setup_fields(thd,
Ref_ptr_array(), *values, MARK_COLUMNS_NONE, 0, 0))
1389 THD *thd= stmt->thd;
1390 SELECT_LEX *select= &stmt->lex->select_lex;
1391 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1392 uint want_privilege;
1394 DBUG_ENTER(
"mysql_test_update");
1398 MYSQL_OPEN_FORCE_SHARED_MDL))
1401 if (table_list->multitable_view)
1403 DBUG_ASSERT(table_list->view != 0);
1404 DBUG_PRINT(
"info", (
"Switch to multi-update"));
1409 if (!table_list->updatable)
1411 my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias,
"UPDATE");
1415 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1417 want_privilege= (table_list->view ? UPDATE_ACL :
1421 if (mysql_prepare_update(thd, table_list, &select->where,
1422 select->order_list.elements,
1423 select->order_list.first))
1426 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1429 table_list->register_want_access(want_privilege);
1431 thd->lex->select_lex.no_wrap_view_item= TRUE;
1433 select->item_list, MARK_COLUMNS_READ, 0, 0);
1434 thd->lex->select_lex.no_wrap_view_item= FALSE;
1437 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1441 (SELECT_ACL & ~table_list->table->grant.
privilege);
1442 table_list->register_want_access(SELECT_ACL);
1445 stmt->lex->value_list, MARK_COLUMNS_NONE, 0, 0))
1469 THD *thd= stmt->thd;
1470 LEX *lex= stmt->lex;
1471 DBUG_ENTER(
"mysql_test_delete");
1472 DBUG_ASSERT(stmt->is_stmt_prepare());
1476 MYSQL_OPEN_FORCE_SHARED_MDL))
1479 if (!table_list->table)
1481 my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
1482 table_list->view_db.str, table_list->view_name.str);
1486 DBUG_RETURN(mysql_prepare_delete(thd, table_list, &lex->select_lex.where));
1512 THD *thd= stmt->thd;
1513 LEX *lex= stmt->lex;
1514 SELECT_LEX_UNIT *unit= &lex->unit;
1515 DBUG_ENTER(
"mysql_test_select");
1517 lex->select_lex.context.resolve_in_select_list= TRUE;
1519 if (
select_precheck(thd, lex, tables, lex->select_lex.table_list.first))
1522 if (!lex->result && !(lex->result= new (stmt->mem_root) select_send))
1524 my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR),
1525 static_cast<int>(
sizeof(select_send)));
1532 thd->lex->used_tables= 0;
1539 if (unit->prepare(thd, 0, 0))
1541 if (!lex->describe && !stmt->is_sql_prepare())
1543 select_result *result= lex->result;
1544 select_result *analyse_result= NULL;
1545 if (lex->proc_analyse)
1550 if ((result= analyse_result=
1559 bool rc= (send_prep_stmt(stmt, result->field_count(unit->types)) ||
1560 result->send_result_set_metadata(unit->types,
1561 Protocol::SEND_EOF) ||
1562 thd->protocol->flush());
1563 delete analyse_result;
1591 THD *thd= stmt->thd;
1593 DBUG_ENTER(
"mysql_test_do_fields");
1594 DBUG_ASSERT(stmt->is_stmt_prepare());
1602 *values, MARK_COLUMNS_NONE, 0, 0));
1624 THD *thd= stmt->thd;
1626 DBUG_ENTER(
"mysql_test_set_fields");
1627 DBUG_ASSERT(stmt->is_stmt_prepare());
1630 UINT_MAX, FALSE)) ||
1636 if (var->light_check(thd))
1661 THD *thd= stmt->thd;
1663 DBUG_ENTER(
"mysql_test_call_fields");
1664 DBUG_ASSERT(stmt->is_stmt_prepare());
1667 UINT_MAX, FALSE)) ||
1671 while ((item= it++))
1673 if ((!item->fixed && item->fix_fields(thd, it.ref())) ||
1674 item->check_cols(1))
1703 int (*specific_prepare)(THD *thd),
1704 ulong setup_tables_done_option)
1706 DBUG_ENTER(
"select_like_stmt_test");
1707 THD *thd= stmt->thd;
1708 LEX *lex= stmt->lex;
1710 lex->select_lex.context.resolve_in_select_list= TRUE;
1712 if (specific_prepare && (*specific_prepare)(thd))
1715 thd->lex->used_tables= 0;
1718 DBUG_RETURN(lex->unit.prepare(thd, 0, setup_tables_done_option));
1740 int (*specific_prepare)(THD *thd),
1741 ulong setup_tables_done_option)
1743 DBUG_ENTER(
"select_like_stmt_test_with_open");
1744 DBUG_ASSERT(stmt->is_stmt_prepare());
1753 MYSQL_OPEN_FORCE_SHARED_MDL))
1756 DBUG_RETURN(select_like_stmt_test(stmt, specific_prepare,
1757 setup_tables_done_option));
1775 THD *thd= stmt->thd;
1776 LEX *lex= stmt->lex;
1777 SELECT_LEX *select_lex= &lex->select_lex;
1781 TABLE_LIST *tables= lex->create_last_non_select_table->next_global;
1782 DBUG_ENTER(
"mysql_test_create_table");
1783 DBUG_ASSERT(stmt->is_stmt_prepare());
1788 if (select_lex->item_list.elements)
1791 if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1795 MYSQL_OPEN_FORCE_SHARED_MDL))
1798 select_lex->context.resolve_in_select_list= TRUE;
1800 lex->unlink_first_table(&link_to_local);
1802 res= select_like_stmt_test(stmt, 0, 0);
1804 lex->link_first_table_back(create_table, link_to_local);
1815 MYSQL_OPEN_FORCE_SHARED_MDL))
1836 THD *thd= stmt->thd;
1837 LEX *lex= stmt->lex;
1843 DBUG_ENTER(
"mysql_test_create_view");
1844 DBUG_ASSERT(stmt->is_stmt_prepare());
1846 if (create_view_precheck(thd, tables, view, lex->create_view_mode))
1859 lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_VIEW;
1860 res= select_like_stmt_test(stmt, 0, 0);
1864 lex->link_first_table_back(view, link_to_local);
1890 return select_like_stmt_test(stmt, &mysql_multi_update_prepare,
1900 static int mysql_multi_delete_prepare_tester(THD *thd)
1903 return mysql_multi_delete_prepare(thd, &table_count);
1922 stmt->thd->lex->current_select= &stmt->thd->lex->select_lex;
1923 if (add_item_to_list(stmt->thd,
new Item_null()))
1925 my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 0);
1930 select_like_stmt_test_with_open(stmt, tables,
1931 &mysql_multi_delete_prepare_tester,
1936 my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
1937 tables->view_db.str, tables->view_name.str);
1958 static int mysql_insert_select_prepare_tester(THD *thd)
1960 SELECT_LEX *first_select= &thd->lex->select_lex;
1961 TABLE_LIST *second_table= first_select->table_list.first->next_local;
1964 first_select->table_list.first= second_table;
1965 thd->lex->select_lex.context.table_list=
1966 thd->lex->select_lex.context.first_name_resolution_table= second_table;
1968 return mysql_insert_select_prepare(thd);
1988 LEX *lex= stmt->lex;
1994 tables->table->insert_values=(uchar *)1;
2001 first_local_table= lex->select_lex.table_list.first;
2002 DBUG_ASSERT(first_local_table != 0);
2005 select_like_stmt_test_with_open(stmt, tables,
2006 &mysql_insert_select_prepare_tester,
2009 lex->select_lex.table_list.first= first_local_table;
2033 THD *thd= stmt->thd;
2034 LEX *lex= stmt->lex;
2035 SELECT_LEX *select_lex= &lex->select_lex;
2037 enum enum_sql_command sql_command= lex->sql_command;
2039 DBUG_ENTER(
"check_prepared_statement");
2040 DBUG_PRINT(
"enter",(
"command: %d param_count: %u",
2041 sql_command, stmt->param_count));
2043 lex->first_lists_tables_same();
2044 tables= lex->query_tables;
2047 lex->select_lex.context.resolve_in_table_list_only(select_lex->
2052 thd->get_stmt_da()->opt_clear_warning_info(thd->query_id);
2059 thd->query(), thd->query_length(), NULL,
2060 thd->variables.character_set_client);
2066 mysql_ha_rm_tables(thd, tables);
2078 switch (sql_command) {
2079 case SQLCOM_REPLACE:
2081 res= mysql_test_insert(stmt, tables, lex->field_list,
2083 lex->update_list, lex->value_list,
2088 res= mysql_test_update(stmt, tables);
2093 case SQLCOM_UPDATE_MULTI:
2094 res= mysql_test_multiupdate(stmt, tables, res == 2);
2098 res= mysql_test_delete(stmt, tables);
2101 case SQLCOM_SHOW_DATABASES:
2102 case SQLCOM_SHOW_TABLES:
2103 case SQLCOM_SHOW_TRIGGERS:
2104 case SQLCOM_SHOW_EVENTS:
2105 case SQLCOM_SHOW_OPEN_TABLES:
2106 case SQLCOM_SHOW_FIELDS:
2107 case SQLCOM_SHOW_KEYS:
2108 case SQLCOM_SHOW_COLLATIONS:
2109 case SQLCOM_SHOW_CHARSETS:
2110 case SQLCOM_SHOW_VARIABLES:
2111 case SQLCOM_SHOW_STATUS:
2112 case SQLCOM_SHOW_TABLE_STATUS:
2113 case SQLCOM_SHOW_STATUS_PROC:
2114 case SQLCOM_SHOW_STATUS_FUNC:
2116 res= mysql_test_select(stmt, tables);
2123 case SQLCOM_CREATE_TABLE:
2124 res= mysql_test_create_table(stmt);
2127 case SQLCOM_CREATE_VIEW:
2128 if (lex->create_view_mode == VIEW_ALTER)
2130 my_message(ER_UNSUPPORTED_PS, ER(ER_UNSUPPORTED_PS), MYF(0));
2133 res= mysql_test_create_view(stmt);
2136 res= mysql_test_do_fields(stmt, tables, lex->insert_list);
2140 res= mysql_test_call_fields(stmt, tables, &lex->value_list);
2142 case SQLCOM_SET_OPTION:
2143 res= mysql_test_set_fields(stmt, tables, &lex->var_list);
2146 case SQLCOM_DELETE_MULTI:
2147 res= mysql_test_multidelete(stmt, tables);
2150 case SQLCOM_INSERT_SELECT:
2151 case SQLCOM_REPLACE_SELECT:
2152 res= mysql_test_insert_select(stmt, tables);
2159 case SQLCOM_DROP_TABLE:
2160 case SQLCOM_RENAME_TABLE:
2161 case SQLCOM_ALTER_TABLE:
2163 case SQLCOM_CREATE_INDEX:
2164 case SQLCOM_DROP_INDEX:
2165 case SQLCOM_ROLLBACK:
2166 case SQLCOM_TRUNCATE:
2167 case SQLCOM_DROP_VIEW:
2169 case SQLCOM_ANALYZE:
2170 case SQLCOM_OPTIMIZE:
2171 case SQLCOM_CHANGE_MASTER:
2174 case SQLCOM_SLAVE_START:
2175 case SQLCOM_SLAVE_STOP:
2176 case SQLCOM_INSTALL_PLUGIN:
2177 case SQLCOM_UNINSTALL_PLUGIN:
2178 case SQLCOM_CREATE_DB:
2179 case SQLCOM_DROP_DB:
2180 case SQLCOM_ALTER_DB_UPGRADE:
2181 case SQLCOM_CHECKSUM:
2182 case SQLCOM_CREATE_USER:
2183 case SQLCOM_RENAME_USER:
2184 case SQLCOM_DROP_USER:
2185 case SQLCOM_ALTER_USER:
2186 case SQLCOM_ASSIGN_TO_KEYCACHE:
2187 case SQLCOM_PRELOAD_KEYS:
2193 case SQLCOM_PREPARE:
2194 case SQLCOM_EXECUTE:
2195 case SQLCOM_DEALLOCATE_PREPARE:
2204 my_message(ER_UNSUPPORTED_PS, ER(ER_UNSUPPORTED_PS), MYF(0));
2210 DBUG_RETURN(stmt->is_sql_prepare() ?
2211 FALSE : (send_prep_stmt(stmt, 0) || thd->protocol->flush()));
2224 LEX *lex= stmt->lex;
2225 if ((stmt->param_count= lex->param_list.elements))
2227 if (stmt->param_count > (uint) UINT_MAX16)
2230 my_message(ER_PS_MANY_PARAM, ER(ER_PS_MANY_PARAM), MYF(0));
2237 alloc_root(stmt->thd->mem_root,
2239 if (!stmt->param_array)
2241 for (to= stmt->param_array;
2242 to < stmt->param_array + stmt->param_count;
2245 *to= param_iterator++;
2280 Protocol *save_protocol= thd->protocol;
2282 DBUG_ENTER(
"mysqld_stmt_prepare");
2284 DBUG_PRINT(
"prep_query", (
"%s", packet));
2292 if (thd->stmt_map.insert(thd, stmt))
2301 thd->protocol= &thd->protocol_binary;
2303 if (stmt->
prepare(packet, packet_length))
2306 thd->stmt_map.erase(stmt);
2309 thd->protocol= save_protocol;
2311 sp_cache_enforce_limit(thd->sp_proc_cache, stored_program_cache_size);
2312 sp_cache_enforce_limit(thd->sp_func_cache, stored_program_cache_size);
2336 static const char *get_dynamic_sql_string(LEX *lex, uint *query_len)
2341 if (lex->prepared_stmt_code_is_varref)
2345 const CHARSET_INFO *to_cs= thd->variables.collation_connection;
2346 bool needs_conversion;
2347 user_var_entry *
entry;
2356 (user_var_entry*)my_hash_search(&thd->user_vars,
2357 (uchar*)lex->prepared_stmt_code.str,
2358 lex->prepared_stmt_code.length))
2361 my_bool is_var_null;
2362 var_value= entry->val_str(&is_var_null, &str, NOT_FIXED_DEC);
2367 DBUG_ASSERT(!is_var_null);
2377 str.set(STRING_WITH_LEN(
"NULL"), &my_charset_latin1);
2380 needs_conversion= String::needs_conversion(var_value->length(),
2381 var_value->charset(), to_cs,
2384 len= (needs_conversion ? var_value->length() * to_cs->mbmaxlen :
2385 var_value->length());
2386 if (!(query_str= (
char*) alloc_root(thd->mem_root, len+1)))
2389 if (needs_conversion)
2392 len= copy_and_convert(query_str, len, to_cs, var_value->ptr(),
2393 var_value->length(), var_value->charset(),
2397 memcpy(query_str, var_value->ptr(), var_value->length());
2398 query_str[len]=
'\0';
2403 query_str= lex->prepared_stmt_code.str;
2404 *query_len= lex->prepared_stmt_code.length;
2432 DBUG_ENTER(
"mysql_sql_stmt_prepare");
2440 if (stmt->is_in_use())
2442 my_error(ER_PS_NO_RECURSION, MYF(0));
2449 if (! (query= get_dynamic_sql_string(lex, &query_len)) ||
2455 stmt->set_sql_prepare();
2458 if (stmt->set_name(name))
2464 if (thd->stmt_map.insert(thd, stmt))
2470 if (stmt->
prepare(query, query_len))
2473 thd->stmt_map.erase(stmt);
2476 my_ok(thd, 0L, 0L,
"Statement prepared");
2492 SELECT_LEX *sl= lex->all_selects_list;
2493 DBUG_ENTER(
"reinit_stmt_before_use");
2502 if (lex->empty_field_list_on_rset)
2504 lex->empty_field_list_on_rset= 0;
2505 lex->field_list.empty();
2507 for (; sl; sl= sl->next_select_in_list())
2509 if (!sl->first_execution)
2512 sl->options&= ~SELECT_DESCRIBE;
2515 sl->exclude_from_table_unique_test= FALSE;
2523 sl->where= sl->prep_where->copy_andor_structure(thd);
2524 sl->where->cleanup();
2528 if (sl->prep_having)
2530 sl->having= sl->prep_having->copy_andor_structure(thd);
2531 sl->having->cleanup();
2535 DBUG_ASSERT(sl->join == 0);
2538 if (sl->group_list_ptrs && sl->group_list_ptrs->size() > 0)
2540 for (uint ix= 0; ix < sl->group_list_ptrs->size() - 1; ++ix)
2542 order= sl->group_list_ptrs->at(ix);
2543 order->next= sl->group_list_ptrs->at(ix+1);
2546 for (order= sl->group_list.first; order; order= order->next)
2547 order->item= &order->item_ptr;
2549 if (sl->order_list_ptrs && sl->order_list_ptrs->size() > 0)
2551 for (uint ix= 0; ix < sl->order_list_ptrs->size() - 1; ++ix)
2553 order= sl->order_list_ptrs->at(ix);
2554 order->next= sl->order_list_ptrs->at(ix+1);
2557 for (order= sl->order_list.first; order; order= order->next)
2558 order->item= &order->item_ptr;
2561 sl->no_error= FALSE;
2564 SELECT_LEX_UNIT *unit= sl->master_unit();
2566 unit->types.empty();
2568 unit->reinit_exec_mechanism();
2585 tables= tables->next_global)
2587 tables->reinit_before_use(thd);
2594 rt->mdl_request.ticket= NULL;
2601 for (
TABLE_LIST *tables= lex->auxiliary_table_list.first;
2603 tables= tables->next_global)
2605 tables->reinit_before_use(thd);
2607 lex->current_select= &lex->select_lex;
2610 if (lex->leaf_tables_insert)
2611 lex->select_lex.leaf_tables= lex->leaf_tables_insert;
2615 lex->result->cleanup();
2616 lex->result->set_thd(thd);
2618 lex->allow_sum_func= 0;
2619 lex->in_sum_func= NULL;
2635 for (;item < end ; ++item)
2659 uchar *packet= (uchar*)packet_arg;
2664 uchar *packet_end= packet + packet_length;
2666 Protocol *save_protocol= thd->protocol;
2668 DBUG_ENTER(
"mysqld_stmt_execute");
2670 if (packet + 9 > packet_end)
2672 my_error(ER_MALFORMED_PACKET, MYF(0));
2676 stmt_id= uint4korr(packet);
2677 flags= (ulong) packet[4];
2683 if (!(stmt= find_prepared_statement(thd, stmt_id)))
2686 my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), static_cast<int>(
sizeof(llbuf)),
2687 llstr(stmt_id, llbuf),
"mysqld_stmt_execute");
2691 #if defined(ENABLED_PROFILING)
2692 thd->profiling.set_query_source(stmt->query(), stmt->query_length());
2694 DBUG_PRINT(
"exec_query", (
"%s", stmt->query()));
2695 DBUG_PRINT(
"info",(
"stmt: 0x%lx", (
long) stmt));
2697 open_cursor=
test(flags & (ulong) CURSOR_TYPE_READ_ONLY);
2699 thd->protocol= &thd->protocol_binary;
2700 stmt->execute_loop(&expanded_query, open_cursor, packet, packet_end);
2701 thd->protocol= save_protocol;
2703 sp_cache_enforce_limit(thd->sp_proc_cache, stored_program_cache_size);
2704 sp_cache_enforce_limit(thd->sp_func_cache, stored_program_cache_size);
2707 DBUG_EXECUTE_IF(
"close_conn_after_stmt_execute", vio_shutdown(thd->net.vio););
2737 DBUG_ENTER(
"mysql_sql_stmt_execute");
2738 DBUG_PRINT(
"info", (
"EXECUTE: %.*s\n", (
int) name->length, name->str));
2742 my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0),
2743 static_cast<int>(name->length), name->str,
"EXECUTE");
2747 if (stmt->param_count != lex->prepared_stmt_params.elements)
2749 my_error(ER_WRONG_ARGUMENTS, MYF(0),
"EXECUTE");
2753 DBUG_PRINT(
"info",(
"stmt: 0x%lx", (
long) stmt));
2755 (void) stmt->execute_loop(&expanded_query, FALSE, NULL, NULL);
2775 Statement stmt_backup;
2777 DBUG_ENTER(
"mysqld_stmt_fetch");
2779 if (packet_length < 8)
2781 my_error(ER_MALFORMED_PACKET, MYF(0));
2784 stmt_id= uint4korr(packet);
2785 num_rows= uint4korr(packet+4);
2789 status_var_increment(thd->status_var.com_stmt_fetch);
2790 if (!(stmt= find_prepared_statement(thd, stmt_id)))
2793 my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), static_cast<int>(
sizeof(llbuf)),
2794 llstr(stmt_id, llbuf),
"mysqld_stmt_fetch");
2798 cursor= stmt->cursor;
2801 my_error(ER_STMT_HAS_NO_OPEN_CURSOR, MYF(0), stmt_id);
2805 thd->stmt_arena= stmt;
2806 thd->set_n_backup_statement(stmt, &stmt_backup);
2808 cursor->fetch(num_rows);
2810 if (!cursor->is_open())
2812 stmt->close_cursor();
2813 reset_stmt_params(stmt);
2816 thd->restore_backup_statement(stmt, &stmt_backup);
2817 thd->stmt_arena= thd;
2844 DBUG_ENTER(
"mysqld_stmt_reset");
2846 if (packet_length < 4)
2848 my_error(ER_MALFORMED_PACKET, MYF(0));
2852 stmt_id= uint4korr(packet);
2857 status_var_increment(thd->status_var.com_stmt_reset);
2858 if (!(stmt= find_prepared_statement(thd, stmt_id)))
2861 my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), static_cast<int>(
sizeof(llbuf)),
2862 llstr(stmt_id, llbuf),
"mysqld_stmt_reset");
2866 stmt->close_cursor();
2872 reset_stmt_params(stmt);
2874 stmt->state= Query_arena::STMT_PREPARED;
2876 general_log_print(thd, thd->get_command(), NullS);
2896 DBUG_ENTER(
"mysqld_stmt_close");
2898 if (packet_length < 4)
2900 my_error(ER_MALFORMED_PACKET, MYF(0));
2904 stmt_id= uint4korr(packet);
2906 thd->get_stmt_da()->disable_status();
2908 if (!(stmt= find_prepared_statement(thd, stmt_id)))
2915 DBUG_ASSERT(! stmt->is_in_use());
2917 general_log_print(thd, thd->get_command(), NullS);
2939 DBUG_PRINT(
"info", (
"DEALLOCATE PREPARE: %.*s\n", (
int) name->length,
2943 my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0),
2944 static_cast<int>(name->length), name->str,
"DEALLOCATE PREPARE");
2945 else if (stmt->is_in_use())
2946 my_error(ER_PS_NO_RECURSION, MYF(0));
2975 #ifndef EMBEDDED_LIBRARY
2976 char *packet_end= packet + packet_length;
2978 DBUG_ENTER(
"mysql_stmt_get_longdata");
2980 status_var_increment(thd->status_var.com_stmt_send_long_data);
2982 thd->get_stmt_da()->disable_status();
2983 #ifndef EMBEDDED_LIBRARY
2985 if (packet_length < MYSQL_LONG_DATA_HEADER)
2989 stmt_id= uint4korr(packet);
2992 if (!(stmt=find_prepared_statement(thd, stmt_id)))
2995 param_number= uint2korr(packet);
2997 #ifndef EMBEDDED_LIBRARY
2998 if (param_number >= stmt->param_count)
3001 stmt->state= Query_arena::STMT_ERROR;
3002 stmt->last_errno= ER_WRONG_ARGUMENTS;
3003 sprintf(stmt->last_error, ER(ER_WRONG_ARGUMENTS),
3004 "mysqld_stmt_send_long_data");
3009 param= stmt->param_array[param_number];
3014 thd->set_stmt_da(&new_stmt_da);
3016 #ifndef EMBEDDED_LIBRARY
3017 param->set_longdata(packet, (ulong) (packet_end - packet));
3019 param->set_longdata(thd->extra_data, thd->extra_length);
3021 if (thd->get_stmt_da()->is_error())
3023 stmt->state= Query_arena::STMT_ERROR;
3024 stmt->last_errno= thd->get_stmt_da()->sql_errno();
3025 strncpy(stmt->last_error, thd->get_stmt_da()->message(), MYSQL_ERRMSG_SIZE);
3027 thd->set_stmt_da(save_stmt_da);
3029 general_log_print(thd, thd->get_command(), NullS);
3039 Select_fetch_protocol_binary::Select_fetch_protocol_binary(THD *thd_arg)
3043 bool Select_fetch_protocol_binary::send_result_set_metadata(
List<Item> &list, uint
flags)
3046 Protocol *save_protocol= thd->protocol;
3054 thd->protocol= &protocol;
3055 rc= select_send::send_result_set_metadata(list, flags);
3056 thd->protocol= save_protocol;
3061 bool Select_fetch_protocol_binary::send_eof()
3067 if (thd->is_error())
3076 Select_fetch_protocol_binary::send_data(
List<Item> &fields)
3078 Protocol *save_protocol= thd->protocol;
3081 thd->protocol= &protocol;
3082 rc= select_send::send_data(fields);
3083 thd->protocol= save_protocol;
3103 thd->get_stmt_da()->set_error_status(ER_NEED_REPREPARE);
3104 m_invalidated= TRUE;
3114 Server_runnable::~Server_runnable()
3120 Execute_sql_statement::
3122 :m_sql_text(sql_text)
3138 PSI_statement_locker *parent_locker;
3141 if (
alloc_query(thd, m_sql_text.str, m_sql_text.length))
3144 Parser_state parser_state;
3145 if (parser_state.init(thd, thd->query(), thd->query_length()))
3148 parser_state.m_lip.multi_statements= FALSE;
3151 parent_locker= thd->m_statement_psi;
3152 thd->m_statement_psi= NULL;
3153 error=
parse_sql(thd, &parser_state, NULL) || thd->is_error();
3154 thd->m_statement_psi= parent_locker;
3159 thd->lex->set_trg_event_type_for_tables();
3161 parent_locker= thd->m_statement_psi;
3162 thd->m_statement_psi= NULL;
3168 rewrite_query_if_needed(thd);
3170 thd->m_statement_psi= parent_locker;
3174 log_execute_line(thd);
3186 Prepared_statement::Prepared_statement(THD *thd_arg)
3187 :Statement(NULL, &main_mem_root,
3188 STMT_INITIALIZED, ++thd_arg->statement_id_counter),
3195 flags((uint) IS_IN_USE)
3197 init_sql_alloc(&main_mem_root, thd_arg->variables.query_alloc_block_size,
3198 thd_arg->variables.query_prealloc_size);
3203 void Prepared_statement::setup_set_params()
3209 if (query_cache_maybe_disabled(thd))
3210 lex->safe_to_cache_query= FALSE;
3216 if ((mysql_bin_log.is_open() && is_update_query(lex->sql_command)) ||
3217 opt_log || opt_slow_log ||
3218 query_cache_is_cacheable_query(lex))
3220 set_params_from_vars= insert_params_from_vars_with_log;
3221 #ifndef EMBEDDED_LIBRARY
3222 set_params= insert_params_with_log;
3224 set_params_data= emb_insert_params_with_log;
3229 set_params_from_vars= insert_params_from_vars;
3230 #ifndef EMBEDDED_LIBRARY
3231 set_params= insert_params;
3233 set_params_data= emb_insert_params;
3249 DBUG_ENTER(
"Prepared_statement::~Prepared_statement");
3250 DBUG_PRINT(
"enter",(
"stmt: 0x%lx cursor: 0x%lx",
3251 (
long)
this, (
long) cursor));
3261 delete (st_lex_local *) lex;
3263 free_root(&main_mem_root, MYF(0));
3268 Query_arena::Type Prepared_statement::type()
const
3270 return PREPARED_STATEMENT;
3274 void Prepared_statement::cleanup_stmt()
3276 DBUG_ENTER(
"Prepared_statement::cleanup_stmt");
3277 DBUG_PRINT(
"enter",(
"stmt: 0x%lx", (
long)
this));
3280 thd->cleanup_after_query();
3281 thd->rollback_item_tree_changes();
3287 bool Prepared_statement::set_name(
LEX_STRING *name_arg)
3289 name.length= name_arg->length;
3290 name.str= (
char*) memdup_root(mem_root, name_arg->str, name_arg->length);
3291 return name.str == 0;
3306 Prepared_statement::set_db(
const char *db_arg, uint db_length_arg)
3309 if (db_arg && db_length_arg)
3311 db= this->strmake(db_arg, db_length_arg);
3312 db_length= db_length_arg;
3319 return db_arg != NULL && db == NULL;
3353 Statement stmt_backup;
3354 Query_arena *old_stmt_arena;
3355 PSI_statement_locker *parent_locker= thd->m_statement_psi;
3356 DBUG_ENTER(
"Prepared_statement::prepare");
3362 status_var_increment(thd->status_var.com_stmt_prepare);
3364 if (! (lex=
new (mem_root) st_lex_local))
3367 if (set_db(thd->db, thd->db_length))
3374 thd->set_n_backup_statement(
this, &stmt_backup);
3375 thd->set_n_backup_active_arena(
this, &stmt_backup);
3379 thd->restore_backup_statement(
this, &stmt_backup);
3380 thd->restore_active_arena(
this, &stmt_backup);
3384 old_stmt_arena= thd->stmt_arena;
3385 thd->stmt_arena=
this;
3387 Parser_state parser_state;
3388 if (parser_state.init(thd, thd->query(), thd->query_length()))
3390 thd->restore_backup_statement(
this, &stmt_backup);
3391 thd->restore_active_arena(
this, &stmt_backup);
3392 thd->stmt_arena= old_stmt_arena;
3396 parser_state.m_lip.stmt_prepare_mode= TRUE;
3397 parser_state.m_lip.multi_statements= FALSE;
3400 lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_PREPARE;
3402 thd->m_statement_psi= NULL;
3403 error=
parse_sql(thd, & parser_state, NULL) ||
3405 init_param_array(
this);
3406 thd->m_statement_psi= parent_locker;
3408 lex->set_trg_event_type_for_tables();
3418 thd->restore_active_arena(
this, &stmt_backup);
3424 DBUG_ASSERT(thd->change_list.is_empty());
3430 MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
3439 error= check_prepared_statement(
this);
3446 DBUG_ASSERT(lex->sphead == NULL || error != 0);
3448 lex->unit.cleanup();
3451 DBUG_ASSERT(thd->transaction.stmt.is_empty());
3453 close_thread_tables(thd);
3454 thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
3464 DBUG_ASSERT(! thd->in_sub_stmt);
3465 if (thd->transaction_rollback_request)
3467 trans_rollback_implicit(thd);
3468 thd->mdl_context.release_transactional_locks();
3473 rewrite_query_if_needed(thd);
3476 thd->restore_backup_statement(
this, &stmt_backup);
3477 thd->stmt_arena= old_stmt_arena;
3482 lex->context_analysis_only&= ~CONTEXT_ANALYSIS_ONLY_PREPARE;
3483 state= Query_arena::STMT_PREPARED;
3484 flags&= ~ (uint) IS_IN_USE;
3513 if (thd->sp_runtime_ctx == NULL)
3515 if (thd->rewritten_query.length())
3516 logger.general_log_write(thd, COM_STMT_PREPARE,
3517 thd->rewritten_query.c_ptr_safe(),
3518 thd->rewritten_query.length());
3520 logger.general_log_write(thd, COM_STMT_PREPARE,
3521 query(), query_length());
3549 Prepared_statement::set_parameters(
String *expanded_query,
3550 uchar *packet, uchar *packet_end)
3552 bool is_sql_ps= packet == NULL;
3558 res= set_params_from_vars(
this, thd->lex->prepared_stmt_params,
3561 else if (param_count)
3563 #ifndef EMBEDDED_LIBRARY
3564 uchar *null_array= packet;
3565 res= (setup_conversion_functions(
this, &packet, packet_end) ||
3566 set_params(
this, null_array, packet, packet_end, expanded_query));
3573 res= set_params_data(
this, expanded_query);
3578 my_error(ER_WRONG_ARGUMENTS, MYF(0),
3579 is_sql_ps ?
"EXECUTE" :
"mysqld_stmt_execute");
3580 reset_stmt_params(
this);
3615 const int MAX_REPREPARE_ATTEMPTS= 3;
3618 int reprepare_attempt= 0;
3621 if (state == Query_arena::STMT_ERROR)
3623 my_message(last_errno, last_error, MYF(0));
3627 if (set_parameters(expanded_query, packet, packet_end))
3630 if (unlikely(thd->security_ctx->password_expired &&
3631 !lex->is_change_password))
3633 my_error(ER_MUST_CHANGE_PASSWORD, MYF(0));
3643 DBUG_ASSERT(thd->free_list == NULL);
3655 reprepare_observer.reset_reprepare_observer();
3656 stmt_reprepare_observer = &reprepare_observer;
3659 thd->push_reprepare_observer(stmt_reprepare_observer);
3661 error= execute(expanded_query, open_cursor) || thd->is_error();
3663 thd->pop_reprepare_observer();
3666 error && !thd->is_fatal_error && !thd->killed &&
3667 reprepare_observer.is_invalidated() &&
3668 reprepare_attempt++ < MAX_REPREPARE_ATTEMPTS)
3670 DBUG_ASSERT(thd->get_stmt_da()->sql_errno() == ER_NEED_REPREPARE);
3678 reset_stmt_params(
this);
3685 Prepared_statement::execute_server_runnable(
Server_runnable *server_runnable)
3687 Statement stmt_backup;
3689 Query_arena *save_stmt_arena= thd->stmt_arena;
3690 Item_change_list save_change_list;
3691 thd->change_list.move_elements_to(&save_change_list);
3693 state= STMT_CONVENTIONAL_EXECUTION;
3695 if (!(lex=
new (mem_root) st_lex_local))
3698 thd->set_n_backup_statement(
this, &stmt_backup);
3699 thd->set_n_backup_active_arena(
this, &stmt_backup);
3700 thd->stmt_arena=
this;
3702 error= server_runnable->execute_server_code(thd);
3704 thd->cleanup_after_query();
3706 thd->restore_active_arena(
this, &stmt_backup);
3707 thd->restore_backup_statement(
this, &stmt_backup);
3708 thd->stmt_arena= save_stmt_arena;
3710 save_change_list.move_elements_to(&thd->change_list);
3732 Prepared_statement::reprepare()
3734 char saved_cur_db_name_buf[NAME_LEN+1];
3736 { saved_cur_db_name_buf,
sizeof(saved_cur_db_name_buf) };
3738 bool cur_db_changed;
3743 copy.set_sql_prepare();
3745 status_var_increment(thd->status_var.com_stmt_reprepare);
3747 if (mysql_opt_change_db(thd, &stmt_db_name, &saved_cur_db_name, TRUE,
3751 error= ((
name.str && copy.set_name(&
name)) ||
3752 copy.prepare(query(), query_length()) ||
3753 validate_metadata(©));
3756 mysql_change_db(thd, &saved_cur_db_name, TRUE);
3760 swap_prepared_statement(©);
3761 swap_parameter_array(param_array, copy.param_array, param_count);
3763 is_reprepared= TRUE;
3772 thd->get_stmt_da()->clear_warning_info(thd->query_id);
3801 if (is_sql_prepare() || lex->describe)
3804 if (lex->select_lex.item_list.elements !=
3805 copy->lex->select_lex.item_list.elements)
3808 thd->server_status|= SERVER_STATUS_METADATA_CHANGED;
3830 swap_variables(
MEM_ROOT, main_mem_root, copy->main_mem_root);
3833 tmp_stmt.set_query_arena(
this);
3834 set_query_arena(copy);
3835 copy->set_query_arena(&tmp_stmt);
3838 tmp_stmt.set_statement(
this);
3839 set_statement(copy);
3840 copy->set_statement(&tmp_stmt);
3843 swap_variables(ulong,
id, copy->id);
3845 swap_variables(
MEM_ROOT *, mem_root, copy->mem_root);
3850 swap_variables(
Item_param **, param_array, copy->param_array);
3856 swap_variables(
char *, db, copy->db);
3858 DBUG_ASSERT(db_length == copy->db_length);
3859 DBUG_ASSERT(param_count == copy->param_count);
3860 DBUG_ASSERT(thd == copy->thd);
3861 last_error[0]=
'\0';
3888 bool Prepared_statement::execute(
String *expanded_query,
bool open_cursor)
3890 Statement stmt_backup;
3891 Query_arena *old_stmt_arena;
3894 char saved_cur_db_name_buf[NAME_LEN+1];
3896 { saved_cur_db_name_buf,
sizeof(saved_cur_db_name_buf) };
3897 bool cur_db_changed;
3901 status_var_increment(thd->status_var.com_stmt_execute);
3903 if (flags & (uint) IS_IN_USE)
3905 my_error(ER_PS_NO_RECURSION, MYF(0));
3919 if (open_cursor && lex->result && lex->result->check_simple_select())
3921 DBUG_PRINT(
"info",(
"Cursor asked for not SELECT stmt"));
3934 DBUG_ASSERT(thd->change_list.is_empty());
3942 thd->set_n_backup_statement(
this, &stmt_backup);
3952 if (mysql_opt_change_db(thd, &stmt_db_name, &saved_cur_db_name, TRUE,
3958 if (expanded_query->length() &&
3960 expanded_query->length()))
3962 my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), expanded_query->length());
3970 stmt_backup.set_query_inner(thd->query_string);
3979 old_stmt_arena= thd->stmt_arena;
3980 thd->stmt_arena=
this;
3986 error= mysql_open_cursor(thd, &result, &cursor);
3994 if (query_cache_send_result_to_client(thd, thd->query(),
3995 thd->query_length()) <= 0)
3997 PSI_statement_locker *parent_locker;
3998 MYSQL_QUERY_EXEC_START(thd->query(),
4000 (
char *) (thd->db ? thd->db :
""),
4001 &thd->security_ctx->priv_user[0],
4002 (
char *) thd->security_ctx->host_or_ip,
4004 parent_locker= thd->m_statement_psi;
4005 thd->m_statement_psi= NULL;
4011 rewrite_query_if_needed(thd);
4014 thd->m_statement_psi= parent_locker;
4015 MYSQL_QUERY_EXEC_DONE(error);
4028 mysql_change_db(thd, &saved_cur_db_name, TRUE);
4031 DBUG_ASSERT(! (error && cursor));
4036 thd->set_statement(&stmt_backup);
4037 thd->stmt_arena= old_stmt_arena;
4039 if (state == Query_arena::STMT_PREPARED)
4040 state= Query_arena::STMT_EXECUTED;
4042 if (error == 0 && this->lex->sql_command == SQLCOM_CALL)
4044 if (is_sql_prepare())
4045 thd->protocol_text.send_out_parameters(&this->lex->param_list);
4047 thd->protocol->send_out_parameters(&this->lex->param_list);
4067 log_execute_line(thd);
4070 flags&= ~ (uint) IS_IN_USE;
4080 status_var_increment(thd->status_var.com_stmt_close);
4082 thd->stmt_map.erase(
this);
4096 void Ed_result_set::operator
delete(
void *ptr,
size_t size)
throw ()
4105 free_root(&own_root, MYF(0));
4122 size_t column_count_arg,
4124 :m_mem_root(*mem_root_arg),
4125 m_column_count(column_count_arg),
4130 clear_alloc_root(mem_root_arg);
4142 :m_diagnostics_area(thd->query_id, false),
4158 Ed_connection::free_old_result()
4166 m_current_rset= m_rsets;
4168 m_diagnostics_area.clear_warning_info(m_thd->query_id);
4180 DBUG_PRINT(
"ed_query", (
"%s", sql_text.str));
4204 Protocol *save_protocol= m_thd->protocol;
4207 DBUG_ENTER(
"Ed_connection::execute_direct");
4211 m_thd->protocol= &protocol_local;
4212 m_thd->set_stmt_da(&m_diagnostics_area);
4214 rc= stmt.execute_server_runnable(server_runnable);
4215 m_thd->protocol->end_statement();
4217 m_thd->protocol= save_protocol;
4218 m_thd->set_stmt_da(save_diagnostics_area);
4224 m_current_rset= m_rsets;
4245 m_current_rset->m_next_rset= ed_result_set;
4247 m_current_rset= ed_result_set;
4250 m_current_rset= m_rsets= ed_result_set;
4273 DBUG_ASSERT(m_current_rset);
4275 if (m_current_rset == m_rsets)
4278 ed_result_set= m_current_rset;
4280 m_current_rset= m_rsets= m_rsets->m_next_rset;
4286 ed_result_set= m_current_rset;
4289 while (prev_rset->m_next_rset != m_current_rset)
4290 prev_rset= ed_result_set->m_next_rset;
4291 m_current_rset= prev_rset->m_next_rset= m_current_rset->m_next_rset;
4293 ed_result_set->m_next_rset= NULL;
4295 return ed_result_set;
4302 Protocol_local::Protocol_local(THD *thd,
Ed_connection *ed_connection)
4304 m_connection(ed_connection),
4307 m_current_row(NULL),
4308 m_current_column(NULL)
4310 clear_alloc_root(&m_rset_root);
4324 DBUG_ASSERT(alloc_root_inited(&m_rset_root));
4326 opt_add_row_to_rset();
4328 m_current_row= (
Ed_column *) alloc_root(&m_rset_root,
4330 m_current_column= m_current_row;
4350 void Protocol_local::opt_add_row_to_rset()
4355 Ed_row *ed_row=
new (&m_rset_root)
Ed_row(m_current_row, m_column_count);
4357 m_rset->push_back(ed_row, &m_rset_root);
4368 if (m_current_column == NULL)
4371 memset(m_current_column, 0,
sizeof(*m_current_column));
4384 bool Protocol_local::store_column(
const void *data,
size_t length)
4386 if (m_current_column == NULL)
4392 m_current_column->str= (
char*) memdup_root(&m_rset_root,
4395 if (! m_current_column->str)
4397 m_current_column->str[length]=
'\0';
4398 m_current_column->length= length;
4410 Protocol_local::store_string(
const char *str,
size_t length,
4417 if (dst_cs && !my_charset_same(src_cs, dst_cs) &&
4418 src_cs != &my_charset_bin &&
4419 dst_cs != &my_charset_bin)
4421 if (convert->copy(str, length, src_cs, dst_cs, &error_unused))
4423 str= convert->ptr();
4424 length= convert->length();
4426 return store_column(str, length);
4434 char v= (char) value;
4435 return store_column(&v, 1);
4443 int16 v= (int16) value;
4444 return store_column(&v, 2);
4452 int32 v= (int32) value;
4453 return store_column(&v, 4);
4461 int64 v= (int64) value;
4462 return store_column(&v, 8);
4471 String str(buf,
sizeof (buf), &my_charset_bin);
4474 rc= my_decimal2string(E_DEC_FATAL_ERROR, value, 0, 0, 0, &str);
4479 return store_column(str.ptr(), str.length());
4490 dst_cs= m_connection->m_thd->variables.character_set_results;
4491 return store_string(str, length, src_cs, dst_cs);
4501 return store_string(str, length, src_cs, dst_cs);
4508 uint precision __attribute__((unused)))
4510 return store_column(time,
sizeof(
MYSQL_TIME));
4518 return store_column(time,
sizeof(
MYSQL_TIME));
4525 uint precision __attribute__((unused)))
4527 return store_column(time,
sizeof(
MYSQL_TIME));
4535 return store_column(&value,
sizeof(
float));
4543 return store_column(&value,
sizeof (
double));
4551 if (field->is_null())
4553 return field->send_binary(
this);
4561 DBUG_ASSERT(m_rset == 0 && !alloc_root_inited(&m_rset_root));
4563 init_sql_alloc(&m_rset_root, MEM_ROOT_BLOCK_SIZE, 0);
4568 m_column_count= columns->elements;
4590 ulonglong affected_rows, ulonglong last_insert_id,
4613 DBUG_ASSERT(m_rset);
4615 opt_add_row_to_rset();
4618 ed_result_set=
new (&m_rset_root)
Ed_result_set(m_rset, m_column_count,
4623 if (! ed_result_set)
4627 DBUG_ASSERT(!alloc_root_inited(&m_rset_root));
4633 m_connection->add_result_set(ed_result_set);
4651 #ifdef EMBEDDED_LIBRARY
4652 void Protocol_local::remove_last_row()