The world's most popular open source database
#include "mysql_priv.h"#include "sql_select.h"#include "sql_cursor.h"#include "sp_head.h"#include "sp.h"#include "sp_cache.h"#include <mysql_com.h>Include dependency graph for sql_prepare.cc:

Go to the source code of this file.
| static bool check_prepared_statement | ( | Prepared_statement * | stmt, | |
| bool | text_protocol | |||
| ) | [static] |
Definition at line 1658 of file sql_prepare.cc.
References DBUG_ENTER, DBUG_PRINT, DBUG_RETURN, ER, ER_UNSUPPORTED_PS, error, FALSE, my_message(), MYF, mysql_test_create_table(), mysql_test_delete(), mysql_test_do_fields(), mysql_test_insert(), mysql_test_insert_select(), mysql_test_multidelete(), mysql_test_multiupdate(), mysql_test_select(), mysql_test_set_fields(), mysql_test_update(), Prepared_statement::param_count, st_table_list::select_lex, send_prep_stmt(), sql_command_flags, SQLCOM_ALTER_TABLE, SQLCOM_ANALYZE, SQLCOM_ASSIGN_TO_KEYCACHE, SQLCOM_CALL, SQLCOM_CHANGE_MASTER, SQLCOM_CHECKSUM, SQLCOM_COMMIT, SQLCOM_CREATE_DB, SQLCOM_CREATE_INDEX, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_USER, SQLCOM_CREATE_VIEW, SQLCOM_DELETE, SQLCOM_DELETE_MULTI, SQLCOM_DO, SQLCOM_DROP_DB, SQLCOM_DROP_INDEX, SQLCOM_DROP_TABLE, SQLCOM_DROP_USER, SQLCOM_DROP_VIEW, SQLCOM_FLUSH, SQLCOM_GRANT, SQLCOM_INSERT, SQLCOM_INSERT_SELECT, SQLCOM_INSTALL_PLUGIN, SQLCOM_KILL, SQLCOM_OPTIMIZE, SQLCOM_PRELOAD_KEYS, SQLCOM_RENAME_DB, SQLCOM_RENAME_TABLE, SQLCOM_RENAME_USER, SQLCOM_REPAIR, SQLCOM_REPLACE, SQLCOM_REPLACE_SELECT, SQLCOM_RESET, SQLCOM_REVOKE, SQLCOM_ROLLBACK, SQLCOM_SELECT, SQLCOM_SET_OPTION, SQLCOM_SHOW_AUTHORS, SQLCOM_SHOW_BINLOG_EVENTS, SQLCOM_SHOW_BINLOGS, SQLCOM_SHOW_COLUMN_TYPES, SQLCOM_SHOW_CONTRIBUTORS, SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CREATE_DB, SQLCOM_SHOW_CREATE_EVENT, SQLCOM_SHOW_CREATE_FUNC, SQLCOM_SHOW_CREATE_PROC, SQLCOM_SHOW_ENGINE_LOGS, SQLCOM_SHOW_ENGINE_MUTEX, SQLCOM_SHOW_ENGINE_STATUS, SQLCOM_SHOW_ERRORS, SQLCOM_SHOW_FUNC_CODE, SQLCOM_SHOW_GRANTS, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_PRIVILEGES, SQLCOM_SHOW_PROC_CODE, SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_SLAVE_STAT, SQLCOM_SHOW_STORAGE_ENGINES, SQLCOM_SHOW_WARNS, SQLCOM_SLAVE_START, SQLCOM_SLAVE_STOP, SQLCOM_TRUNCATE, SQLCOM_UNINSTALL_PLUGIN, SQLCOM_UPDATE, SQLCOM_UPDATE_MULTI, Prepared_statement::thd, and TRUE.
Referenced by Prepared_statement::prepare().
01660 { 01661 THD *thd= stmt->thd; 01662 LEX *lex= stmt->lex; 01663 SELECT_LEX *select_lex= &lex->select_lex; 01664 TABLE_LIST *tables; 01665 enum enum_sql_command sql_command= lex->sql_command; 01666 int res= 0; 01667 DBUG_ENTER("check_prepared_statement"); 01668 DBUG_PRINT("enter",("command: %d, param_count: %ld", 01669 sql_command, stmt->param_count)); 01670 01671 lex->first_lists_tables_same(); 01672 tables= lex->query_tables; 01673 01674 /* set context for commands which do not use setup_tables */ 01675 lex->select_lex.context.resolve_in_table_list_only(select_lex-> 01676 get_table_list()); 01677 01678 switch (sql_command) { 01679 case SQLCOM_REPLACE: 01680 case SQLCOM_INSERT: 01681 res= mysql_test_insert(stmt, tables, lex->field_list, 01682 lex->many_values, 01683 select_lex->item_list, lex->value_list, 01684 lex->duplicates); 01685 break; 01686 01687 case SQLCOM_UPDATE: 01688 res= mysql_test_update(stmt, tables); 01689 /* mysql_test_update returns 2 if we need to switch to multi-update */ 01690 if (res != 2) 01691 break; 01692 01693 case SQLCOM_UPDATE_MULTI: 01694 res= mysql_test_multiupdate(stmt, tables, res == 2); 01695 break; 01696 01697 case SQLCOM_DELETE: 01698 res= mysql_test_delete(stmt, tables); 01699 break; 01700 01701 case SQLCOM_SELECT: 01702 res= mysql_test_select(stmt, tables, text_protocol); 01703 if (res == 2) 01704 { 01705 /* Statement and field info has already been sent */ 01706 DBUG_RETURN(FALSE); 01707 } 01708 break; 01709 case SQLCOM_CREATE_TABLE: 01710 res= mysql_test_create_table(stmt); 01711 break; 01712 01713 case SQLCOM_DO: 01714 res= mysql_test_do_fields(stmt, tables, lex->insert_list); 01715 break; 01716 01717 case SQLCOM_SET_OPTION: 01718 res= mysql_test_set_fields(stmt, tables, &lex->var_list); 01719 break; 01720 01721 case SQLCOM_DELETE_MULTI: 01722 res= mysql_test_multidelete(stmt, tables); 01723 break; 01724 01725 case SQLCOM_INSERT_SELECT: 01726 case SQLCOM_REPLACE_SELECT: 01727 res= mysql_test_insert_select(stmt, tables); 01728 break; 01729 01730 /* 01731 Note that we don't need to have cases in this list if they are 01732 marked with CF_STATUS_COMMAND in sql_command_flags 01733 */ 01734 case SQLCOM_SHOW_PROCESSLIST: 01735 case SQLCOM_SHOW_STORAGE_ENGINES: 01736 case SQLCOM_SHOW_PRIVILEGES: 01737 case SQLCOM_SHOW_COLUMN_TYPES: 01738 case SQLCOM_SHOW_ENGINE_LOGS: 01739 case SQLCOM_SHOW_ENGINE_STATUS: 01740 case SQLCOM_SHOW_ENGINE_MUTEX: 01741 case SQLCOM_SHOW_CREATE_DB: 01742 case SQLCOM_SHOW_GRANTS: 01743 case SQLCOM_SHOW_BINLOG_EVENTS: 01744 case SQLCOM_SHOW_MASTER_STAT: 01745 case SQLCOM_SHOW_SLAVE_STAT: 01746 case SQLCOM_SHOW_CREATE_PROC: 01747 case SQLCOM_SHOW_CREATE_FUNC: 01748 case SQLCOM_SHOW_CREATE_EVENT: 01749 case SQLCOM_SHOW_CREATE: 01750 case SQLCOM_SHOW_PROC_CODE: 01751 case SQLCOM_SHOW_FUNC_CODE: 01752 case SQLCOM_SHOW_AUTHORS: 01753 case SQLCOM_SHOW_CONTRIBUTORS: 01754 case SQLCOM_SHOW_WARNS: 01755 case SQLCOM_SHOW_ERRORS: 01756 case SQLCOM_SHOW_BINLOGS: 01757 case SQLCOM_DROP_TABLE: 01758 case SQLCOM_RENAME_TABLE: 01759 case SQLCOM_ALTER_TABLE: 01760 case SQLCOM_COMMIT: 01761 case SQLCOM_CREATE_INDEX: 01762 case SQLCOM_DROP_INDEX: 01763 case SQLCOM_ROLLBACK: 01764 case SQLCOM_TRUNCATE: 01765 case SQLCOM_CALL: 01766 case SQLCOM_CREATE_VIEW: 01767 case SQLCOM_DROP_VIEW: 01768 case SQLCOM_REPAIR: 01769 case SQLCOM_ANALYZE: 01770 case SQLCOM_OPTIMIZE: 01771 case SQLCOM_CHANGE_MASTER: 01772 case SQLCOM_RESET: 01773 case SQLCOM_FLUSH: 01774 case SQLCOM_SLAVE_START: 01775 case SQLCOM_SLAVE_STOP: 01776 case SQLCOM_INSTALL_PLUGIN: 01777 case SQLCOM_UNINSTALL_PLUGIN: 01778 case SQLCOM_CREATE_DB: 01779 case SQLCOM_DROP_DB: 01780 case SQLCOM_RENAME_DB: 01781 case SQLCOM_CHECKSUM: 01782 case SQLCOM_CREATE_USER: 01783 case SQLCOM_RENAME_USER: 01784 case SQLCOM_DROP_USER: 01785 case SQLCOM_ASSIGN_TO_KEYCACHE: 01786 case SQLCOM_PRELOAD_KEYS: 01787 case SQLCOM_GRANT: 01788 case SQLCOM_REVOKE: 01789 case SQLCOM_KILL: 01790 break; 01791 01792 default: 01793 /* 01794 Trivial check of all status commands. This is easier than having 01795 things in the above case list, as it's less chance for mistakes. 01796 */ 01797 if (!(sql_command_flags[sql_command] & CF_STATUS_COMMAND)) 01798 { 01799 /* All other statements are not supported yet. */ 01800 my_message(ER_UNSUPPORTED_PS, ER(ER_UNSUPPORTED_PS), MYF(0)); 01801 goto error; 01802 } 01803 break; 01804 } 01805 if (res == 0) 01806 DBUG_RETURN(text_protocol? FALSE : (send_prep_stmt(stmt, 0) || 01807 thd->protocol->flush())); 01808 error: 01809 DBUG_RETURN(TRUE); 01810 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static Prepared_statement* find_prepared_statement | ( | THD * | thd, | |
| ulong | id, | |||
| const char * | where | |||
| ) | [static] |
Definition at line 176 of file sql_prepare.cc.
References ER_UNKNOWN_STMT_HANDLER, llstr(), my_error(), and MYF.
Referenced by mysql_stmt_close(), mysql_stmt_execute(), mysql_stmt_fetch(), mysql_stmt_get_longdata(), and mysql_stmt_reset().
00177 { 00178 /* 00179 To strictly separate namespaces of SQL prepared statements and C API 00180 prepared statements find() will return 0 if there is a named prepared 00181 statement with such id. 00182 */ 00183 Statement *stmt= thd->stmt_map.find(id); 00184 00185 if (stmt == 0 || stmt->type() != Query_arena::PREPARED_STATEMENT) 00186 { 00187 char llbuf[22]; 00188 my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), sizeof(llbuf), llstr(id, llbuf), 00189 where); 00190 return 0; 00191 } 00192 return (Prepared_statement *) stmt; 00193 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static const char* get_dynamic_sql_string | ( | LEX * | lex, | |
| uint * | query_len | |||
| ) | [static] |
Definition at line 1943 of file sql_prepare.cc.
References alloc_root(), String::charset(), copy_and_convert(), DBUG_ASSERT, hash_search(), String::length(), charset_info_st::mbmaxlen, memcpy, my_charset_latin1, String::needs_conversion(), NOT_FIXED_DEC, String::ptr(), String::set(), and STRING_WITH_LEN.
Referenced by mysql_sql_stmt_prepare().
01944 { 01945 THD *thd= lex->thd; 01946 char *query_str= 0; 01947 01948 if (lex->prepared_stmt_code_is_varref) 01949 { 01950 /* This is PREPARE stmt FROM or EXECUTE IMMEDIATE @var. */ 01951 String str; 01952 CHARSET_INFO *to_cs= thd->variables.collation_connection; 01953 bool needs_conversion; 01954 user_var_entry *entry; 01955 String *var_value= &str; 01956 uint32 unused, len; 01957 /* 01958 Convert @var contents to string in connection character set. Although 01959 it is known that int/real/NULL value cannot be a valid query we still 01960 convert it for error messages to be uniform. 01961 */ 01962 if ((entry= 01963 (user_var_entry*)hash_search(&thd->user_vars, 01964 (byte*)lex->prepared_stmt_code.str, 01965 lex->prepared_stmt_code.length)) 01966 && entry->value) 01967 { 01968 my_bool is_var_null; 01969 var_value= entry->val_str(&is_var_null, &str, NOT_FIXED_DEC); 01970 /* 01971 NULL value of variable checked early as entry->value so here 01972 we can't get NULL in normal conditions 01973 */ 01974 DBUG_ASSERT(!is_var_null); 01975 if (!var_value) 01976 goto end; 01977 } 01978 else 01979 { 01980 /* 01981 variable absent or equal to NULL, so we need to set variable to 01982 something reasonable to get a readable error message during parsing 01983 */ 01984 str.set(STRING_WITH_LEN("NULL"), &my_charset_latin1); 01985 } 01986 01987 needs_conversion= String::needs_conversion(var_value->length(), 01988 var_value->charset(), to_cs, 01989 &unused); 01990 01991 len= (needs_conversion ? var_value->length() * to_cs->mbmaxlen : 01992 var_value->length()); 01993 if (!(query_str= alloc_root(thd->mem_root, len+1))) 01994 goto end; 01995 01996 if (needs_conversion) 01997 { 01998 uint dummy_errors; 01999 len= copy_and_convert(query_str, len, to_cs, var_value->ptr(), 02000 var_value->length(), var_value->charset(), 02001 &dummy_errors); 02002 } 02003 else 02004 memcpy(query_str, var_value->ptr(), var_value->length()); 02005 query_str[len]= '\0'; // Safety (mostly for debug) 02006 *query_len= len; 02007 } 02008 else 02009 { 02010 query_str= lex->prepared_stmt_code.str; 02011 *query_len= lex->prepared_stmt_code.length; 02012 } 02013 end: 02014 return query_str; 02015 }
Here is the call graph for this function:

Here is the caller graph for this function:

EMBEDDED_LIBRARY
Definition at line 265 of file sql_prepare.cc.
References pos(), reg1, uint2korr, uint3korr, and uint4korr.
Referenced by set_param_date(), set_param_datetime(), set_param_decimal(), set_param_str(), and set_param_time().
00266 { 00267 reg1 uchar *pos= *packet; 00268 if (len < 1) 00269 return 0; 00270 if (*pos < 251) 00271 { 00272 (*packet)++; 00273 return (ulong) *pos; 00274 } 00275 if (len < 3) 00276 return 0; 00277 if (*pos == 252) 00278 { 00279 (*packet)+=3; 00280 return (ulong) uint2korr(pos+1); 00281 } 00282 if (len < 4) 00283 return 0; 00284 if (*pos == 253) 00285 { 00286 (*packet)+=4; 00287 return (ulong) uint3korr(pos+1); 00288 } 00289 if (len < 5) 00290 return 0; 00291 (*packet)+=9; // Must be 254 when here 00292 /* 00293 In our client-server protocol all numbers bigger than 2^24 00294 stored as 8 bytes with uint8korr. Here we always know that 00295 parameter length is less than 2^4 so don't look at the second 00296 4 bytes. But still we need to obey the protocol hence 9 in the 00297 assignment above. 00298 */ 00299 return (ulong) uint4korr(pos+1); 00300 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static bool init_param_array | ( | Prepared_statement * | stmt | ) | [static] |
Definition at line 1818 of file sql_prepare.cc.
References alloc_root(), ER, ER_PS_MANY_PARAM, FALSE, my_message(), MYF, Prepared_statement::param_array, Prepared_statement::param_count, Prepared_statement::thd, to, TRUE, and UINT_MAX16.
Referenced by Prepared_statement::prepare().
01819 { 01820 LEX *lex= stmt->lex; 01821 if ((stmt->param_count= lex->param_list.elements)) 01822 { 01823 if (stmt->param_count > (uint) UINT_MAX16) 01824 { 01825 /* Error code to be defined in 5.0 */ 01826 my_message(ER_PS_MANY_PARAM, ER(ER_PS_MANY_PARAM), MYF(0)); 01827 return TRUE; 01828 } 01829 Item_param **to; 01830 List_iterator<Item_param> param_iterator(lex->param_list); 01831 /* Use thd->mem_root as it points at statement mem_root */ 01832 stmt->param_array= (Item_param **) 01833 alloc_root(stmt->thd->mem_root, 01834 sizeof(Item_param*) * stmt->param_count); 01835 if (!stmt->param_array) 01836 return TRUE; 01837 for (to= stmt->param_array; 01838 to < stmt->param_array + stmt->param_count; 01839 ++to) 01840 { 01841 *to= param_iterator++; 01842 } 01843 } 01844 return FALSE; 01845 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static void init_stmt_after_parse | ( | LEX * | lex | ) | [static] |
Definition at line 2020 of file sql_prepare.cc.
References UNCACHEABLE_PREPARE.
Referenced by Prepared_statement::prepare().
02021 { 02022 SELECT_LEX *sl= lex->all_selects_list; 02023 /* 02024 Switch off a temporary flag that prevents evaluation of 02025 subqueries in statement prepare. 02026 */ 02027 for (; sl; sl= sl->next_select_in_list()) 02028 sl->uncacheable&= ~UNCACHEABLE_PREPARE; 02029 }
Here is the caller graph for this function:

| static bool insert_params | ( | Prepared_statement * | stmt, | |
| uchar * | null_array, | |||
| uchar * | read_pos, | |||
| uchar * | data_end, | |||
| String * | expanded_query | |||
| ) | [static] |
Definition at line 738 of file sql_prepare.cc.
References Item_param::convert_str_value(), DBUG_ENTER, DBUG_RETURN, is_param_null(), Item_param::LONG_DATA_VALUE, Prepared_statement::param_array, Prepared_statement::param_count, Item_param::set_null(), Item_param::set_param_func, Item_param::state, and Prepared_statement::thd.
Referenced by Prepared_statement::setup_set_params().
00741 { 00742 Item_param **begin= stmt->param_array; 00743 Item_param **end= begin + stmt->param_count; 00744 00745 DBUG_ENTER("insert_params"); 00746 00747 for (Item_param **it= begin; it < end; ++it) 00748 { 00749 Item_param *param= *it; 00750 if (param->state != Item_param::LONG_DATA_VALUE) 00751 { 00752 if (is_param_null(null_array, it - begin)) 00753 param->set_null(); 00754 else 00755 { 00756 if (read_pos >= data_end) 00757 DBUG_RETURN(1); 00758 param->set_param_func(param, &read_pos, data_end - read_pos); 00759 } 00760 } 00761 if (param->convert_str_value(stmt->thd)) 00762 DBUG_RETURN(1); /* out of memory */ 00763 } 00764 DBUG_RETURN(0); 00765 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static bool insert_params_from_vars | ( | Prepared_statement * | stmt, | |
| List< LEX_STRING > & | varnames, | |||
| String *query | __attribute__((unused)) | |||
| ) | [static] |
EMBEDDED_LIBRARY
Definition at line 910 of file sql_prepare.cc.
References DBUG_ENTER, DBUG_RETURN, hash_search(), LEX_STRING::length, Prepared_statement::param_array, Prepared_statement::param_count, LEX_STRING::str, and Prepared_statement::thd.
Referenced by Prepared_statement::setup_set_params().
00913 { 00914 Item_param **begin= stmt->param_array; 00915 Item_param **end= begin + stmt->param_count; 00916 user_var_entry *entry; 00917 LEX_STRING *varname; 00918 List_iterator<LEX_STRING> var_it(varnames); 00919 DBUG_ENTER("insert_params_from_vars"); 00920 00921 for (Item_param **it= begin; it < end; ++it) 00922 { 00923 Item_param *param= *it; 00924 varname= var_it++; 00925 entry= (user_var_entry*)hash_search(&stmt->thd->user_vars, 00926 (byte*) varname->str, 00927 varname->length); 00928 if (param->set_from_user_var(stmt->thd, entry) || 00929 param->convert_str_value(stmt->thd)) 00930 DBUG_RETURN(1); 00931 } 00932 DBUG_RETURN(0); 00933 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static bool insert_params_from_vars_with_log | ( | Prepared_statement * | stmt, | |
| List< LEX_STRING > & | varnames, | |||
| String * | query | |||
| ) | [static] |
Definition at line 949 of file sql_prepare.cc.
References buf, DBUG_ENTER, DBUG_RETURN, default_charset_info, escape_string_for_mysql(), get_var_with_binlog(), String::length(), my_charset_utf8_general_ci, my_null_string, charset_info_st::name, Prepared_statement::param_array, Prepared_statement::param_count, query, and Prepared_statement::thd.
Referenced by Prepared_statement::setup_set_params().
00952 { 00953 Item_param **begin= stmt->param_array; 00954 Item_param **end= begin + stmt->param_count; 00955 user_var_entry *entry; 00956 LEX_STRING *varname; 00957 List_iterator<LEX_STRING> var_it(varnames); 00958 String buf; 00959 const String *val; 00960 uint32 length= 0; 00961 00962 DBUG_ENTER("insert_params_from_vars"); 00963 00964 if (query->copy(stmt->query, stmt->query_length, default_charset_info)) 00965 DBUG_RETURN(1); 00966 00967 for (Item_param **it= begin; it < end; ++it) 00968 { 00969 Item_param *param= *it; 00970 varname= var_it++; 00971 if (get_var_with_binlog(stmt->thd, stmt->lex->sql_command, 00972 *varname, &entry)) 00973 DBUG_RETURN(1); 00974 00975 if (param->set_from_user_var(stmt->thd, entry)) 00976 DBUG_RETURN(1); 00977 /* Insert @'escaped-varname' instead of parameter in the query */ 00978 if (entry) 00979 { 00980 char *begin, *ptr; 00981 buf.length(0); 00982 if (buf.reserve(entry->name.length*2+3)) 00983 DBUG_RETURN(1); 00984 00985 begin= ptr= buf.c_ptr_quick(); 00986 *ptr++= '@'; 00987 *ptr++= '\''; 00988 ptr+= escape_string_for_mysql(&my_charset_utf8_general_ci, 00989 ptr, 0, entry->name.str, 00990 entry->name.length); 00991 *ptr++= '\''; 00992 buf.length(ptr - begin); 00993 val= &buf; 00994 } 00995 else 00996 val= &my_null_string; 00997 00998 if (param->convert_str_value(stmt->thd)) 00999 DBUG_RETURN(1);

