| 2937 | |
| 2938 | |
| 2939 | static |
| 2940 | bool mysql_sql_stmt_execute_immediate(THD *thd, |
| 2941 | bool dynamic_open_cursor, |
| 2942 | select_result *result_arg, |
| 2943 | Server_side_cursor **cursor_arg, |
| 2944 | const InstrSlice &instrs_set_placeholder) |
| 2945 | { |
| 2946 | LEX *lex= thd->lex; |
| 2947 | CSET_STRING orig_query= thd->query_string; |
| 2948 | Prepared_statement *stmt; |
| 2949 | LEX_CSTRING query; |
| 2950 | DBUG_ENTER("mysql_sql_stmt_execute_immediate"); |
| 2951 | |
| 2952 | /* |
| 2953 | Dynamic cursor placeholders are initialized from the USING clause |
| 2954 | with help of sp_instr_set_ps_placeholder instructions, |
| 2955 | so in case of a dynamic cursor lex->m_params should be empty. |
| 2956 | */ |
| 2957 | DBUG_ASSERT(!lex->prepared_stmt.param_count() || !dynamic_open_cursor); |
| 2958 | if (lex->prepared_stmt.params_fix_fields(thd)) |
| 2959 | DBUG_RETURN(true); |
| 2960 | |
| 2961 | /* |
| 2962 | Prepared_statement is quite large, |
| 2963 | let's allocate it on the heap rather than on the stack. |
| 2964 | |
| 2965 | It's important for "buffer" not to be destructed |
| 2966 | before stmt->execute_immediate(). |
| 2967 | See comments in get_dynamic_sql_string(). |
| 2968 | */ |
| 2969 | StringBuffer<256> buffer; |
| 2970 | if (lex->prepared_stmt.get_dynamic_sql_string(thd, &query, &buffer) || |
| 2971 | !(stmt= new Prepared_statement(thd))) |
| 2972 | DBUG_RETURN(true); // out of memory |
| 2973 | |
| 2974 | // See comments on thd->free_list in mysql_sql_stmt_execute() |
| 2975 | SCOPE_VALUE(thd->free_list, (Item *) NULL); |
| 2976 | SCOPE_EXIT([thd]() mutable { thd->free_items(); }); |
| 2977 | /* |
| 2978 | Make sure we call Prepared_statement::execute_immediate() |
| 2979 | with an empty THD::change_list. It can be non empty as the above |
| 2980 | LEX::prepared_stmt_params_fix_fields() and LEX::get_dynamic_str_string() |
| 2981 | call fix_fields() for the PS source and PS parameter Items and |
| 2982 | can do Item tree changes, e.g. on character set conversion: |
| 2983 | |
| 2984 | - Example #1: Item tree changes in get_dynamic_str_string() |
| 2985 | SET NAMES utf8; |
| 2986 | CREATE PROCEDURE p1() |
| 2987 | EXECUTE IMMEDIATE CONCAT('SELECT ',CONVERT(RAND() USING latin1)); |
| 2988 | CALL p1(); |
| 2989 | |
| 2990 | - Example #2: Item tree changes in prepared_stmt_param_fix_fields(): |
| 2991 | SET NAMES utf8; |
| 2992 | CREATE PROCEDURE p1(a VARCHAR(10) CHARACTER SET utf8) |
| 2993 | EXECUTE IMMEDIATE 'SELECT ?' USING CONCAT(a, CONVERT(RAND() USING latin1)); |
| 2994 | CALL p1('x'); |
| 2995 | */ |
| 2996 | Item_change_list_savepoint change_list_savepoint(thd); |
no test coverage detected