| 2752 | */ |
| 2753 | |
| 2754 | bool Lex_prepared_stmt::get_dynamic_sql_string(THD *thd, |
| 2755 | LEX_CSTRING *dst, |
| 2756 | String *buffer) |
| 2757 | { |
| 2758 | if (m_code->fix_fields_if_needed_for_scalar(thd, NULL)) |
| 2759 | return true; |
| 2760 | |
| 2761 | const String *str= m_code->val_str(buffer); |
| 2762 | if (m_code->null_value) |
| 2763 | { |
| 2764 | /* |
| 2765 | Prepare source was NULL, so we need to set "str" to |
| 2766 | something reasonable to get a readable error message during parsing |
| 2767 | */ |
| 2768 | dst->str= "NULL"; |
| 2769 | dst->length= 4; |
| 2770 | return false; |
| 2771 | } |
| 2772 | |
| 2773 | /* |
| 2774 | Character set conversion notes: |
| 2775 | |
| 2776 | 1) When PREPARE or EXECUTE IMMEDIATE are used with string literals: |
| 2777 | PREPARE stmt FROM 'SELECT ''str'''; |
| 2778 | EXECUTE IMMEDIATE 'SELECT ''str'''; |
| 2779 | it's very unlikely that any conversion will happen below, because |
| 2780 | @@character_set_client and @@collation_connection are normally |
| 2781 | set to the same CHARSET_INFO pointer. |
| 2782 | |
| 2783 | In tricky environments when @@collation_connection is set to something |
| 2784 | different from @@character_set_client, double conversion may happen: |
| 2785 | - When the parser scans the string literal |
| 2786 | (sql_yacc.yy rules "prepare_src" -> "expr" -> ... -> "text_literal") |
| 2787 | it will convert 'str' from @@character_set_client to |
| 2788 | @@collation_connection. |
| 2789 | - Then in the code below will convert 'str' from @@collation_connection |
| 2790 | back to @@character_set_client. |
| 2791 | |
| 2792 | 2) When PREPARE or EXECUTE IMMEDIATE is used with a user variable, |
| 2793 | it should work about the same way, because user variables are usually |
| 2794 | assigned like this: |
| 2795 | SET @str='str'; |
| 2796 | and thus have the same character set with string literals. |
| 2797 | |
| 2798 | 3) When PREPARE or EXECUTE IMMEDIATE is used with some |
| 2799 | more complex expression, conversion will depend on this expression. |
| 2800 | For example, a concatenation of string literals: |
| 2801 | EXECUTE IMMEDIATE 'SELECT * FROM'||'t1'; |
| 2802 | should work the same way with just a single literal, |
| 2803 | so no conversion normally. |
| 2804 | */ |
| 2805 | CHARSET_INFO *to_cs= thd->variables.character_set_client; |
| 2806 | |
| 2807 | uint32 unused; |
| 2808 | if (String::needs_conversion(str->length(), str->charset(), to_cs, &unused)) |
| 2809 | { |
| 2810 | if (!(dst->str= sql_strmake_with_convert(thd, str->ptr(), str->length(), |
| 2811 | str->charset(), UINT_MAX32, |
no test coverage detected