
Public Types | |
| IS_IN_USE = 1 | |
| enum | flag_values { IS_IN_USE = 1 } |
Public Member Functions | |
| Prepared_statement (THD *thd_arg, Protocol *protocol_arg) | |
| virtual | ~Prepared_statement () |
| void | setup_set_params () |
| virtual Query_arena::Type | type () const |
| virtual void | cleanup_stmt () |
| bool | set_name (LEX_STRING *name) |
| void | close_cursor () |
| bool | prepare (const char *packet, uint packet_length) |
| bool | execute (String *expanded_query, bool open_cursor) |
| bool | deallocate () |
Public Attributes | |
| THD * | thd |
| Select_fetch_protocol_prep | result |
| Protocol * | protocol |
| Item_param ** | param_array |
| uint | param_count |
| uint | last_errno |
| uint | flags |
| char | last_error [MYSQL_ERRMSG_SIZE] |
| bool(* | set_params )(Prepared_statement *st, uchar *data, uchar *data_end, uchar *read_pos, String *expanded_query) |
| bool(* | set_params_from_vars )(Prepared_statement *stmt, List< LEX_STRING > &varnames, String *expanded_query) |
Definition at line 107 of file sql_prepare.cc.
| Prepared_statement::Prepared_statement | ( | THD * | thd_arg, | |
| Protocol * | protocol_arg | |||
| ) |
Definition at line 2653 of file sql_prepare.cc.
References last_error.
02654 :Statement(INITIALIZED, ++thd_arg->statement_id_counter, 02655 thd_arg->variables.query_alloc_block_size, 02656 thd_arg->variables.query_prealloc_size), 02657 thd(thd_arg), 02658 result(thd_arg), 02659 protocol(protocol_arg), 02660 param_array(0), 02661 param_count(0), 02662 last_errno(0), 02663 flags((uint) IS_IN_USE) 02664 { 02665 *last_error= '\0'; 02666 }
| Prepared_statement::~Prepared_statement | ( | ) | [virtual] |
Definition at line 2702 of file sql_prepare.cc.
References DBUG_ENTER, DBUG_PRINT, DBUG_VOID_RETURN, and free_items().
02703 { 02704 DBUG_ENTER("Prepared_statement::~Prepared_statement"); 02705 DBUG_PRINT("enter",("stmt: %p cursor: %p", this, cursor)); 02706 delete cursor; 02707 /* 02708 We have to call free on the items even if cleanup is called as some items, 02709 like Item_param, don't free everything until free_items() 02710 */ 02711 free_items(); 02712 delete lex->result; 02713 DBUG_VOID_RETURN; 02714 }
Here is the call graph for this function:

| void Prepared_statement::cleanup_stmt | ( | ) | [virtual] |
Definition at line 2723 of file sql_prepare.cc.
References cleanup_items(), close_thread_tables(), DBUG_ENTER, DBUG_PRINT, DBUG_VOID_RETURN, and free_list().
Referenced by execute(), and prepare().
02724 { 02725 DBUG_ENTER("Prepared_statement::cleanup_stmt"); 02726 DBUG_PRINT("enter",("stmt: %p", this)); 02727 02728 /* The order is important */ 02729 lex->unit.cleanup(); 02730 cleanup_items(free_list); 02731 thd->cleanup_after_query(); 02732 close_thread_tables(thd); 02733 thd->rollback_item_tree_changes(); 02734 02735 DBUG_VOID_RETURN; 02736 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void Prepared_statement::close_cursor | ( | ) | [inline] |
Definition at line 139 of file sql_prepare.cc.
Referenced by execute(), mysql_stmt_fetch(), and mysql_stmt_reset().
Here is the caller graph for this function:

| bool Prepared_statement::deallocate | ( | ) |
Definition at line 2989 of file sql_prepare.cc.
References ER_PS_NO_RECURSION, FALSE, flags, IS_IN_USE, LOCK_status, my_error(), MYF, statistic_increment, and TRUE.
Referenced by mysql_sql_stmt_close(), mysql_sql_stmt_prepare(), and mysql_stmt_close().
02990 { 02991 /* We account deallocate in the same manner as mysql_stmt_close */ 02992 statistic_increment(thd->status_var.com_stmt_close, &LOCK_status); 02993 if (flags & (uint) IS_IN_USE) 02994 { 02995 my_error(ER_PS_NO_RECURSION, MYF(0)); 02996 return TRUE; 02997 } 02998 /* Statement map calls delete stmt on erase */ 02999 thd->stmt_map.erase(this); 03000 return FALSE; 03001 }
Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2883 of file sql_prepare.cc.
References alloc_query(), ALWAYS_MATERIALIZED_CURSOR, cleanup_stmt(), close_cursor(), DBUG_ASSERT, DBUG_PRINT, ER_OUTOFMEMORY, ER_PS_NO_RECURSION, ER_SP_BAD_CURSOR_QUERY, error, flags, IS_IN_USE, last_errno, last_error, String::length(), LOCK_status, my_error(), my_message(), MYF, mysql_execute_command(), mysql_open_cursor(), NULL, protocol, String::ptr(), reinit_stmt_before_use(), reset_stmt_params(), result, statistic_increment, and TRUE.
Referenced by mysql_sql_stmt_execute(), and mysql_stmt_execute().
02884 { 02885 Statement stmt_backup; 02886 Query_arena *old_stmt_arena; 02887 Item *old_free_list; 02888 bool error= TRUE; 02889 02890 statistic_increment(thd->status_var.com_stmt_execute, &LOCK_status); 02891 02892 /* Check if we got an error when sending long data */ 02893 if (state == Query_arena::ERROR) 02894 { 02895 my_message(last_errno, last_error, MYF(0)); 02896 return TRUE; 02897 } 02898 if (flags & (uint) IS_IN_USE) 02899 { 02900 my_error(ER_PS_NO_RECURSION, MYF(0)); 02901 return TRUE; 02902 } 02903 02904 /* 02905 For SHOW VARIABLES lex->result is NULL, as it's a non-SELECT 02906 command. For such queries we don't return an error and don't 02907 open a cursor -- the client library will recognize this case and 02908 materialize the result set. 02909 For SELECT statements lex->result is created in 02910 check_prepared_statement. lex->result->simple_select() is FALSE 02911 in INSERT ... SELECT and similar commands. 02912 */ 02913 02914 if (open_cursor && lex->result && !lex->result->simple_select()) 02915 { 02916 DBUG_PRINT("info",("Cursor asked for not SELECT stmt")); 02917 my_error(ER_SP_BAD_CURSOR_QUERY, MYF(0)); 02918 return TRUE; 02919 } 02920 02921 /* In case the command has a call to SP which re-uses this statement name */ 02922 flags|= IS_IN_USE; 02923 02924 close_cursor(); 02925 02926 /* 02927 If the free_list is not empty, we'll wrongly free some externally 02928 allocated items when cleaning up after execution of this statement. 02929 */ 02930 DBUG_ASSERT(thd->change_list.is_empty()); 02931 DBUG_ASSERT(thd->free_list == NULL); 02932 thd->set_n_backup_statement(this, &stmt_backup); 02933 if (expanded_query->length() && 02934 alloc_query(thd, (char*) expanded_query->ptr(), 02935 expanded_query->length()+1)) 02936 { 02937 my_error(ER_OUTOFMEMORY, 0, expanded_query->length()); 02938 goto error; 02939 } 02940 /* 02941 Expanded query is needed for slow logging, so we want thd->query 02942 to point at it even after we restore from backup. This is ok, as 02943 expanded query was allocated in thd->mem_root. 02944 */ 02945 stmt_backup.query= thd->query; 02946 stmt_backup.query_length= thd->query_length; 02947 02948 /* 02949 At first execution of prepared statement we may perform logical 02950 transformations of the query tree. Such changes should be performed 02951 on the parse tree of current prepared statement and new items should 02952 be allocated in its memory root. Set the appropriate pointer in THD 02953 to the arena of the statement. 02954 */ 02955 old_stmt_arena= thd->stmt_arena; 02956 thd->stmt_arena= this; 02957 reinit_stmt_before_use(thd, lex); 02958 02959 thd->protocol= protocol; /* activate stmt protocol */ 02960 error= (open_cursor ? 02961 mysql_open_cursor(thd, (uint) ALWAYS_MATERIALIZED_CURSOR, 02962 &result, &cursor) : 02963 mysql_execute_command(thd)); 02964 thd->protocol= &thd->protocol_simple; /* use normal protocol */ 02965 02966 /* Assert that if an error, no cursor is open */ 02967 DBUG_ASSERT(! (error && cursor)); 02968 02969 if (! cursor) 02970 { 02971 cleanup_stmt(); 02972 reset_stmt_params(this); 02973 } 02974 02975 thd->set_statement(&stmt_backup); 02976 thd->stmt_arena= old_stmt_arena; 02977 02978 if (state == Query_arena::PREPARED) 02979 state= Query_arena::EXECUTED; 02980 02981 error: 02982 flags&= ~ (uint) IS_IN_USE; 02983 return error; 02984 }
Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2778 of file sql_prepare.cc.
References alloc_query(), check_prepared_statement(), cleanup_stmt(), DBUG_ASSERT, DBUG_ENTER, DBUG_RETURN, error, FALSE, flags, init_param_array(), init_stmt_after_parse(), IS_IN_USE, lex_end(), lex_start(), LOCK_status, name, NULL, setup_set_params(), statistic_increment, and TRUE.
Referenced by mysql_sql_stmt_prepare(), and mysql_stmt_prepare().
02779 { 02780 bool error; 02781 Statement stmt_backup; 02782 Query_arena *old_stmt_arena; 02783 DBUG_ENTER("Prepared_statement::prepare"); 02784 /* 02785 If this is an SQLCOM_PREPARE, we also increase Com_prepare_sql. 02786 However, it seems handy if com_stmt_prepare is increased always, 02787 no matter what kind of prepare is processed. 02788 */ 02789 statistic_increment(thd->status_var.com_stmt_prepare, &LOCK_status); 02790 02791 /* 02792 alloc_query() uses thd->memroot && thd->query, so we should call 02793 both of backup_statement() and backup_query_arena() here. 02794 */ 02795 thd->set_n_backup_statement(this, &stmt_backup); 02796 thd->set_n_backup_active_arena(this, &stmt_backup); 02797 02798 if (alloc_query(thd, packet, packet_len)) 02799 { 02800 thd->restore_backup_statement(this, &stmt_backup); 02801 thd->restore_active_arena(this, &stmt_backup); 02802 DBUG_RETURN(TRUE); 02803 } 02804 02805 old_stmt_arena= thd->stmt_arena; 02806 thd->stmt_arena= this; 02807 lex_start(thd, (uchar*) thd->query, thd->query_length); 02808 lex->stmt_prepare_mode= TRUE; 02809 02810 error= MYSQLparse((void *)thd) || thd->is_fatal_error || 02811 thd->net.report_error || init_param_array(this); 02812 lex->safe_to_cache_query= FALSE; 02813 /* 02814 While doing context analysis of the query (in check_prepared_statement) 02815 we allocate a lot of additional memory: for open tables, JOINs, derived 02816 tables, etc. Let's save a snapshot of current parse tree to the 02817 statement and restore original THD. In cases when some tree 02818 transformation can be reused on execute, we set again thd->mem_root from 02819 stmt->mem_root (see setup_wild for one place where we do that). 02820 */ 02821 thd->restore_active_arena(this, &stmt_backup); 02822 02823 /* 02824 If called from a stored procedure, ensure that we won't rollback 02825 external changes when cleaning up after validation. 02826 */ 02827 DBUG_ASSERT(thd->change_list.is_empty()); 02828 /* 02829 If the free_list is not empty, we'll wrongly free some externally 02830 allocated items when cleaning up after validation of the prepared 02831 statement. 02832 */ 02833 DBUG_ASSERT(thd->free_list == NULL); 02834 02835 if (error == 0) 02836 error= check_prepared_statement(this, name.str != 0); 02837 02838 if (error && lex->sphead) 02839 { 02840 delete lex->sphead; 02841 lex->sphead= NULL; 02842 } 02843 lex_end(lex); 02844 cleanup_stmt(); 02845 thd->restore_backup_statement(this, &stmt_backup); 02846 thd->stmt_arena= old_stmt_arena; 02847 02848 if (error == 0) 02849 { 02850 setup_set_params(); 02851 init_stmt_after_parse(lex); 02852 state= Query_arena::PREPARED; 02853 flags&= ~ (uint) IS_IN_USE; 02854 } 02855 DBUG_RETURN(error); 02856 }
Here is the call graph for this function:

Here is the caller graph for this function:

| bool Prepared_statement::set_name | ( | LEX_STRING * | name | ) |
Definition at line 2739 of file sql_prepare.cc.
References LEX_STRING::length, memdup_root(), name, and LEX_STRING::str.
Referenced by mysql_sql_stmt_prepare().
02740 { 02741 name.length= name_arg->length; 02742 name.str= memdup_root(mem_root, (char*) name_arg->str, name_arg->length); 02743 return name.str == 0; 02744 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void Prepared_statement::setup_set_params | ( | ) |
Definition at line 2669 of file sql_prepare.cc.
References insert_params(), insert_params_from_vars(), insert_params_from_vars_with_log(), insert_params_withlog(), MYSQL_LOG::is_open(), is_update_query(), mysql_bin_log, opt_log, opt_slow_log, set_params, and set_params_from_vars.
Referenced by prepare().
02670 { 02671 /* Setup binary logging */ 02672 if (mysql_bin_log.is_open() && is_update_query(lex->sql_command) || 02673 opt_log || opt_slow_log) 02674 { 02675 set_params_from_vars= insert_params_from_vars_with_log; 02676 #ifndef EMBEDDED_LIBRARY 02677 set_params= insert_params_withlog; 02678 #else 02679 set_params_data= emb_insert_params_withlog; 02680 #endif 02681 } 02682 else 02683 { 02684 set_params_from_vars= insert_params_from_vars; 02685 #ifndef EMBEDDED_LIBRARY 02686 set_params= insert_params; 02687 #else 02688 set_params_data= emb_insert_params; 02689 #endif 02690 } 02691 }
Here is the call graph for this function:

Here is the caller graph for this function:

| Query_arena::Type Prepared_statement::type | ( | ) | const [virtual] |
Definition at line 121 of file sql_prepare.cc.
Referenced by deallocate(), execute(), mysql_stmt_close(), and prepare().
Definition at line 120 of file sql_prepare.cc.
Referenced by execute(), and mysql_stmt_get_longdata().
| char Prepared_statement::last_error[MYSQL_ERRMSG_SIZE] |
Definition at line 122 of file sql_prepare.cc.
Referenced by execute(), mysql_stmt_get_longdata(), and Prepared_statement().
Definition at line 118 of file sql_prepare.cc.
Referenced by init_param_array(), insert_params(), insert_params_from_vars(), insert_params_from_vars_with_log(), insert_params_withlog(), mysql_stmt_get_longdata(), reset_stmt_params(), and setup_conversion_functions().
Definition at line 119 of file sql_prepare.cc.
Referenced by check_prepared_statement(), init_param_array(), insert_params(), insert_params_from_vars(), insert_params_from_vars_with_log(), insert_params_withlog(), mysql_sql_stmt_execute(), mysql_stmt_execute(), mysql_stmt_get_longdata(), reset_stmt_params(), send_prep_stmt(), and setup_conversion_functions().
| bool(* Prepared_statement::set_params)(Prepared_statement *st, uchar *data, uchar *data_end, uchar *read_pos, String *expanded_query) |
Referenced by setup_set_params().
| bool(* Prepared_statement::set_params_from_vars)(Prepared_statement *stmt, List< LEX_STRING > &varnames, String *expanded_query) |
Referenced by mysql_sql_stmt_execute(), and setup_set_params().
Definition at line 115 of file sql_prepare.cc.
Referenced by check_prepared_statement(), init_param_array(), insert_params(), insert_params_from_vars(), insert_params_from_vars_with_log(), insert_params_withlog(), 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(), select_like_stmt_test(), select_like_stmt_test_with_open_n_lock(), send_prep_stmt(), and setup_conversion_functions().
1.4.7

