List:Commits« Previous MessageNext Message »
From:marc.alff Date:February 21 2008 3:57am
Subject:bk commit into 5.1 tree (malff:1.2563) BUG#12093
View as plain text  
Below is the list of changes that have just been committed into a local
5.1 repository of malff.  When malff does a push these changes
will be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet@stripped, 2008-02-20 20:56:57-07:00, malff@stripped. +28 -0
  WL#4166 (Prepared statements: automatic re-prepare)
  Bug#12093 (SP not found on second PS execution if another thread drops other
  SP in between)
  
  Full solution implementing:
  - Prepared statements revalidation
  - automatic re-prepare
  Temporary patch, for internal review.

  mysql-test/r/ps_1general.result@stripped, 2008-02-20 20:55:29-07:00, malff@stripped. +5 -9
    WL#4166 (Prepared statements: automatic re-prepare)

  mysql-test/r/query_cache_ps_no_prot.result@stripped, 2008-02-20 20:55:29-07:00, malff@stripped. +10 -10
    WL#4166 (Prepared statements: automatic re-prepare)

  mysql-test/r/query_cache_ps_ps_prot.result@stripped, 2008-02-20 20:55:29-07:00, malff@stripped. +10 -10
    WL#4166 (Prepared statements: automatic re-prepare)

  mysql-test/r/sp-error.result@stripped, 2008-02-20 20:55:29-07:00, malff@stripped. +3 -2
    WL#4166 (Prepared statements: automatic re-prepare)

  mysql-test/r/trigger.result@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +0 -1
    WL#4166 (Prepared statements: automatic re-prepare)

  mysql-test/t/disabled.def@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +0 -1
    WL#4166 (Prepared statements: automatic re-prepare)

  mysql-test/t/ps_1general.test@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +5 -1
    WL#4166 (Prepared statements: automatic re-prepare)

  mysql-test/t/sp-error.test@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +0 -8
    WL#4166 (Prepared statements: automatic re-prepare)

  mysql-test/t/trigger.test@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +3 -4
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/mysql_priv.h@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +56 -1
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/mysqld.cc@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +4 -2
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/parse_file.cc@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +1 -1
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/share/errmsg.txt@stripped, 2008-02-20 20:55:32-07:00, malff@stripped. +7 -0
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sp.cc@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +157 -52
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sp.h@stripped, 2008-02-20 20:55:30-07:00, malff@stripped. +1 -0
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sp_head.cc@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +66 -0
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sp_head.h@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +8 -0
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sql_base.cc@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +162 -36
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sql_class.cc@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +304 -10
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sql_class.h@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +213 -2
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sql_insert.cc@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +4 -1
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sql_prepare.cc@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +1131 -124
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sql_table.cc@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +4 -1
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sql_view.cc@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +76 -2
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/sql_view.h@stripped, 2008-02-20 20:55:31-07:00, malff@stripped. +2 -0
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/table.cc@stripped, 2008-02-20 20:55:32-07:00, malff@stripped. +46 -0
    WL#4166 (Prepared statements: automatic re-prepare)

  sql/table.h@stripped, 2008-02-20 20:55:32-07:00, malff@stripped. +8 -0
    WL#4166 (Prepared statements: automatic re-prepare)

  tests/mysql_client_test.c@stripped, 2008-02-20 20:55:32-07:00, malff@stripped. +122 -5
    WL#4166 (Prepared statements: automatic re-prepare)

diff -Nrup a/mysql-test/r/ps_1general.result b/mysql-test/r/ps_1general.result
--- a/mysql-test/r/ps_1general.result	2007-09-20 02:56:25 -06:00
+++ b/mysql-test/r/ps_1general.result	2008-02-20 20:55:29 -07:00
@@ -143,8 +143,7 @@ b char(30)
 );
 insert into t5( a, b, c) values( 9, 'recreated table', 9);
 execute stmt2 ;
-a	b	c
-9	recreated table	9
+ERROR HY000: Prepared statement result set has changed, rebind needed
 drop table t5 ;
 create table t5
 (
@@ -155,8 +154,7 @@ d timestamp default current_timestamp
 );
 insert into t5( a, b, c) values( 9, 'recreated table', 9);
 execute stmt2 ;
-a	b	c
-9	recreated table	9
+ERROR HY000: Prepared statement result set has changed, rebind needed
 drop table t5 ;
 create table t5
 (
@@ -167,8 +165,7 @@ c int
 );
 insert into t5( a, b, c) values( 9, 'recreated table', 9);
 execute stmt2 ;
-a	b	c
-9	recreated table	9
+ERROR HY000: Prepared statement result set has changed, rebind needed
 drop table t5 ;
 create table t5
 (
@@ -178,8 +175,7 @@ c int
 );
 insert into t5( b, c) values( 'recreated table', 9);
 execute stmt2 ;
-a	b	c
-2004-02-29 18:01:59	recreated table	9
+ERROR HY000: Prepared statement result set has changed, rebind needed
 drop table t5 ;
 create table t5
 (
@@ -189,7 +185,7 @@ f3 int
 );
 insert into t5( f1, f2, f3) values( 9, 'recreated table', 9);
 execute stmt2 ;
-ERROR 42S22: Unknown column 'test.t5.a' in 'field list'
+ERROR HY000: Prepared statement result set has changed, rebind needed
 drop table t5 ;
 prepare stmt1 from ' select * from t1 where a <= 2 ' ;
 execute stmt1 ;
diff -Nrup a/mysql-test/r/query_cache_ps_no_prot.result b/mysql-test/r/query_cache_ps_no_prot.result
--- a/mysql-test/r/query_cache_ps_no_prot.result	2007-08-31 10:42:12 -06:00
+++ b/mysql-test/r/query_cache_ps_no_prot.result	2008-02-20 20:55:29 -07:00
@@ -312,63 +312,63 @@ c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	22
 execute stmt1;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	23
 ---- switch to connection con1 ----
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	23
 execute stmt3;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	24
 execute stmt3;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	25
 execute stmt3;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	26
 ---- switch to connection default ----
 set global query_cache_size=0;
 prepare stmt1 from "select * from t1 where c1=?";
 set global query_cache_size=100000;
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	26
 set @a=1;
 execute stmt1 using @a;
 c1
 1
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	26
 set @a=100;
 execute stmt1 using @a;
 c1
 100
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	26
 set @a=10;
 execute stmt1 using @a;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	21
+Qcache_hits	26
 drop table t1;
 ---- disconnect connection con1 ----
 ########################################################################
diff -Nrup a/mysql-test/r/query_cache_ps_ps_prot.result b/mysql-test/r/query_cache_ps_ps_prot.result
--- a/mysql-test/r/query_cache_ps_ps_prot.result	2007-08-31 10:42:12 -06:00
+++ b/mysql-test/r/query_cache_ps_ps_prot.result	2008-02-20 20:55:29 -07:00
@@ -312,63 +312,63 @@ c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	20
 execute stmt1;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	21
 ---- switch to connection con1 ----
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	21
 execute stmt3;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	22
 execute stmt3;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	23
 execute stmt3;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	24
 ---- switch to connection default ----
 set global query_cache_size=0;
 prepare stmt1 from "select * from t1 where c1=?";
 set global query_cache_size=100000;
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	24
 set @a=1;
 execute stmt1 using @a;
 c1
 1
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	24
 set @a=100;
 execute stmt1 using @a;
 c1
 100
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	24
 set @a=10;
 execute stmt1 using @a;
 c1
 10
 show status like 'Qcache_hits';
 Variable_name	Value
-Qcache_hits	19
+Qcache_hits	24
 drop table t1;
 ---- disconnect connection con1 ----
 ########################################################################
diff -Nrup a/mysql-test/r/sp-error.result b/mysql-test/r/sp-error.result
--- a/mysql-test/r/sp-error.result	2008-02-04 11:39:50 -07:00
+++ b/mysql-test/r/sp-error.result	2008-02-20 20:55:29 -07:00
@@ -796,7 +796,7 @@ bug11834_2()
 10
 drop function bug11834_1;
 execute stmt;
-ERROR 42000: FUNCTION test.bug11834_2 does not exist
+ERROR 42000: FUNCTION test.bug11834_1 does not exist
 deallocate prepare stmt;
 drop function bug11834_2;
 DROP FUNCTION IF EXISTS bug12953|
@@ -1045,7 +1045,8 @@ select bug12329();
 bug12329()
 101
 execute stmt1;
-ERROR 42S02: Table 'test.t2' doesn't exist
+bug12329()
+101
 deallocate prepare stmt1;
 drop function bug12329;
 drop table t1, t2;
diff -Nrup a/mysql-test/r/trigger.result b/mysql-test/r/trigger.result
--- a/mysql-test/r/trigger.result	2008-02-12 04:44:24 -07:00
+++ b/mysql-test/r/trigger.result	2008-02-20 20:55:30 -07:00
@@ -820,7 +820,6 @@ call p1();
 drop trigger t1_bi;
 create trigger t1_bi after insert on t1 for each row insert into t3 values (new.id);
 execute stmt1;
-ERROR 42S02: Table 'test.t3' doesn't exist
 call p1();
 ERROR 42S02: Table 'test.t3' doesn't exist
 deallocate prepare stmt1;
diff -Nrup a/mysql-test/t/disabled.def b/mysql-test/t/disabled.def
--- a/mysql-test/t/disabled.def	2008-02-08 10:07:38 -07:00
+++ b/mysql-test/t/disabled.def	2008-02-20 20:55:30 -07:00
@@ -22,5 +22,4 @@ innodb_mysql         : Bug#32724: innodb
 wait_timeout         : Bug#32801 wait_timeout.test fails randomly
 ctype_create         : Bug#32965 main.ctype_create fails
 status               : Bug#32966 main.status fails
-ps_ddl               : Bug#12093 2007-12-14 pending WL#4165 / WL#4166
 query_cache_debug    : Bug#34424: query_cache_debug.test leads to valgrind warnings
diff -Nrup a/mysql-test/t/ps_1general.test b/mysql-test/t/ps_1general.test
--- a/mysql-test/t/ps_1general.test	2007-08-15 02:18:40 -06:00
+++ b/mysql-test/t/ps_1general.test	2008-02-20 20:55:30 -07:00
@@ -172,6 +172,7 @@ create table t5
   b char(30)
 );
 insert into t5( a, b, c) values( 9, 'recreated table', 9);
+--error ER_PS_REBIND
 execute stmt2 ;
 drop table t5 ;
 # 3. drop + create table (same column names/types/order+extra column) 
@@ -184,6 +185,7 @@ create table t5
   d timestamp default current_timestamp
 );
 insert into t5( a, b, c) values( 9, 'recreated table', 9);
+--error ER_PS_REBIND
 execute stmt2 ;
 drop table t5 ;
 # 4. drop + create table (same column names/types, different order +
@@ -196,6 +198,7 @@ create table t5
   c int
 );
 insert into t5( a, b, c) values( 9, 'recreated table', 9);
+--error ER_PS_REBIND
 execute stmt2 ;
 drop table t5 ;
 # 5. drop + create table (same column names/order, different types)
@@ -207,6 +210,7 @@ create table t5
   c int
 );
 insert into t5( b, c) values( 'recreated table', 9);
+--error ER_PS_REBIND
 execute stmt2 ;
 drop table t5 ;
 # 6. drop + create table (same column types/order, different names) 
@@ -218,7 +222,7 @@ create table t5
   f3 int
 );
 insert into t5( f1, f2, f3) values( 9, 'recreated table', 9);
---error 1054
+--error ER_PS_REBIND
 execute stmt2 ;
 drop table t5 ;
 
diff -Nrup a/mysql-test/t/sp-error.test b/mysql-test/t/sp-error.test
--- a/mysql-test/t/sp-error.test	2008-02-04 11:39:50 -07:00
+++ b/mysql-test/t/sp-error.test	2008-02-20 20:55:30 -07:00
@@ -1114,10 +1114,6 @@ execute stmt;
 drop function bug11834_1;
 # Attempt to execute statement should return proper error and 
 # should not crash server.
-
-# NOTE! The error we get from the below query indicates that the sp bug11834_2
-# does not exist(this is wrong but can be accepted)
-# This behaviour has been reported as bug#21294
 --error ER_SP_DOES_NOT_EXIST
 execute stmt;
 deallocate prepare stmt;
@@ -1470,10 +1466,6 @@ execute stmt1;
 drop function bug12329;
 create function bug12329() returns int return (select a+100 from t2);
 select bug12329();
-# Until we implement proper mechanism for invalidation of PS/SP when table
-# or SP's are changed the following statement will fail with 'Table ... was
-# not locked' error (this mechanism should be based on the new TDC).
---error ER_NO_SUCH_TABLE 
 execute stmt1;
 deallocate prepare stmt1;
 drop function bug12329; 
diff -Nrup a/mysql-test/t/trigger.test b/mysql-test/t/trigger.test
--- a/mysql-test/t/trigger.test	2008-02-12 04:44:24 -07:00
+++ b/mysql-test/t/trigger.test	2008-02-20 20:55:30 -07:00
@@ -997,11 +997,10 @@ call p1();
 # Altering trigger forcing it use different set of tables
 drop trigger t1_bi;
 create trigger t1_bi after insert on t1 for each row insert into t3 values (new.id);
-# Until we implement proper mechanism for invalidation of PS/SP when table
-# or SP's are changed these two statements will fail with 'Table ... was
-# not locked' error (this mechanism should be based on the new TDC).
---error ER_NO_SUCH_TABLE 
 execute stmt1;
+# Until we implement proper mechanism for invalidation of SP when table
+# or SP's are changed this statements will fail with 'Table ... was
+# not locked' error (this mechanism should be based on the new TDC).
 --error ER_NO_SUCH_TABLE 
 call p1();
 deallocate prepare stmt1;
diff -Nrup a/sql/mysql_priv.h b/sql/mysql_priv.h
--- a/sql/mysql_priv.h	2008-02-08 08:55:02 -07:00
+++ b/sql/mysql_priv.h	2008-02-20 20:55:30 -07:00
@@ -657,6 +657,61 @@ const char *set_thd_proc_info(THD *thd, 
                               const char *calling_file, 
                               const unsigned int calling_line);
 
+typedef enum version_kind {
+  NULL_VERSION,
+  TABLE_VERSION,
+  TEMPORARY_TABLE_VERSION,
+  VIEW_VERSION,
+  PROCEDURE_VERSION,
+  FUNCTION_VERSION
+} VERSION_KIND;
+
+#define MD5_CHAR_LENGTH 32
+
+/**
+  A Version_id identifies a version of a given object.
+  Different objects (TABLE, VIEW, FUNCTION, PROCEDURE, ...)
+  can use version ids of different spaces (MD5, timestamp, counter, ...),
+  so that a Version_id is an heterogeneous type.
+  Versions can be compared for equality, but don't define any order.
+*/
+
+class Version_id
+{
+public:
+  Version_id();
+  Version_id(const Version_id& vid);
+  ~Version_id();
+
+  VERSION_KIND get_kind() const
+  { return m_kind; }
+
+  bool is_null() const;
+  /**
+    Test for equality.
+    Equality is defined by:
+    - having the same namespace (kind)
+    - having the same value within that namespace
+  */
+  bool is_equal(const Version_id *vid) const;
+  bool is_null_or_equal(const Version_id *vid) const;
+
+  void set_null();
+  void set(const Version_id *vid);
+  void set_table(longlong value);
+  void set_temporary_table(longlong value);
+  void set_view(const char* md5);
+  void set_procedure(const char* md5);
+  void set_function(const char* md5);
+
+  Version_id& operator = (const Version_id& vid);
+
+private:
+  VERSION_KIND m_kind;
+  longlong m_long_value;
+  char m_md5_value[MD5_CHAR_LENGTH];
+};
+
 /*
   External variables
 */
@@ -1196,7 +1251,7 @@ TABLE_SHARE *get_cached_table_share(cons
 TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type update,
                    uint lock_flags);
 TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT* mem,
-		  bool *refresh, uint flags);
+		  bool *refresh, bool *invalidated, uint flags);
 bool name_lock_locked_table(THD *thd, TABLE_LIST *tables);
 bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in);
 TABLE *table_cache_insert_placeholder(THD *thd, const char *key,
diff -Nrup a/sql/mysqld.cc b/sql/mysqld.cc
--- a/sql/mysqld.cc	2008-02-14 01:52:52 -07:00
+++ b/sql/mysqld.cc	2008-02-20 20:55:30 -07:00
@@ -2919,6 +2919,7 @@ SHOW_VAR com_status_vars[]= {
   {"stmt_execute",         (char*) offsetof(STATUS_VAR, com_stmt_execute), SHOW_LONG_STATUS},
   {"stmt_fetch",           (char*) offsetof(STATUS_VAR, com_stmt_fetch), SHOW_LONG_STATUS},
   {"stmt_prepare",         (char*) offsetof(STATUS_VAR, com_stmt_prepare), SHOW_LONG_STATUS},
+  {"stmt_reprepare",       (char*) offsetof(STATUS_VAR, com_stmt_reprepare), SHOW_LONG_STATUS},
   {"stmt_reset",           (char*) offsetof(STATUS_VAR, com_stmt_reset), SHOW_LONG_STATUS},
   {"stmt_send_long_data",  (char*) offsetof(STATUS_VAR, com_stmt_send_long_data), SHOW_LONG_STATUS},
   {"truncate",             (char*) offsetof(STATUS_VAR, com_stat[(uint) SQLCOM_TRUNCATE]), SHOW_LONG_STATUS},
@@ -3007,7 +3008,7 @@ static int init_common_variables(const c
     We have few debug-only commands in com_status_vars, only visible in debug
     builds. for simplicity we enable the assert only in debug builds
 
-    There are 7 Com_ variables which don't have corresponding SQLCOM_ values:
+    There are 8 Com_ variables which don't have corresponding SQLCOM_ values:
     (TODO strictly speaking they shouldn't be here, should not have Com_ prefix
     that is. Perhaps Stmt_ ? Comstmt_ ? Prepstmt_ ?)
 
@@ -3016,6 +3017,7 @@ static int init_common_variables(const c
       Com_stmt_execute         => com_stmt_execute
       Com_stmt_fetch           => com_stmt_fetch
       Com_stmt_prepare         => com_stmt_prepare
+      Com_stmt_reprepare       => com_stmt_reprepare
       Com_stmt_reset           => com_stmt_reset
       Com_stmt_send_long_data  => com_stmt_send_long_data
 
@@ -3024,7 +3026,7 @@ static int init_common_variables(const c
     of SQLCOM_ constants.
   */
   compile_time_assert(sizeof(com_status_vars)/sizeof(com_status_vars[0]) - 1 ==
-                     SQLCOM_END + 7);
+                     SQLCOM_END + 8);
 #endif
 
   load_defaults(conf_file_name, groups, &argc, &argv);
diff -Nrup a/sql/parse_file.cc b/sql/parse_file.cc
--- a/sql/parse_file.cc	2007-10-11 12:37:42 -06:00
+++ b/sql/parse_file.cc	2008-02-20 20:55:30 -07:00
@@ -430,7 +430,7 @@ sql_parse_prepare(const LEX_STRING *file
   File file;
   DBUG_ENTER("sql_parse_prepare");
 
-  if (!my_stat(file_name->str, &stat_info, MYF(MY_WME)))
+  if (!my_stat(file_name->str, &stat_info, MYF(0)))
   {
     DBUG_RETURN(0);
   }
diff -Nrup a/sql/share/errmsg.txt b/sql/share/errmsg.txt
--- a/sql/share/errmsg.txt	2008-01-31 05:54:00 -07:00
+++ b/sql/share/errmsg.txt	2008-02-20 20:55:32 -07:00
@@ -6121,3 +6121,10 @@ ER_NO_FORMAT_DESCRIPTION_EVENT_BEFORE_BI
   eng "The BINLOG statement of type `%s` was not preceded by a format description BINLOG statement."
 ER_SLAVE_CORRUPT_EVENT
   eng "Corrupted replication event was detected"
+
+ER_NEED_REPREPARE
+  eng "(Internal) Prepared statement needs to be re-prepared"
+
+ER_PS_REBIND
+  eng "Prepared statement result set has changed, rebind needed"
+
diff -Nrup a/sql/sp.cc b/sql/sp.cc
--- a/sql/sp.cc	2008-02-19 05:57:58 -07:00
+++ b/sql/sp.cc	2008-02-20 20:55:30 -07:00
@@ -1837,6 +1837,158 @@ static void sp_update_stmt_used_routines
 
 
 /**
+  This helper ensures that a given Stored Function or Stored Procedure
+  is present in the corresponding per session cache,
+  and loads the Stored Function/Procedure from storage into the cache,
+  if necessary.
+  @param [in] thd Current thread
+  @param [in] type Type of stored object to lookup/load
+  @param [in] name Name of the stored object
+  @param [out] sphp Stored object refreshed in the cache, if found
+  @return An error status
+    @retval 0 Success, sphp will contain the stored object fetched, if any
+*/
+
+static int
+sp_cache_load(THD *thd, Sroutine_hash_entry *entry, sp_head **sphp)
+{
+  sp_cache **cp;
+  sp_head *sp;
+  int ret;
+  Object_version sp_version;
+  sp_name name(thd, entry->key.str, entry->key.length);
+  int type= entry->key.str[0];
+
+  DBUG_ENTER("sp_cache_load");
+
+  DBUG_ASSERT(sphp);
+  DBUG_ASSERT(type == TYPE_ENUM_PROCEDURE ||
+              type == TYPE_ENUM_TRIGGER ||
+              type == TYPE_ENUM_FUNCTION);
+
+  /* Triggers are cached in the stored procedure cache. */
+  cp= (type == TYPE_ENUM_FUNCTION ? &thd->sp_func_cache : &thd->sp_proc_cache);
+
+  sp= sp_cache_lookup(cp, &name);
+
+  if (! sp)
+  {
+    switch ((ret= db_find_routine(thd, type, &name, &sp)))
+    {
+    case SP_OK:
+      sp_cache_insert(cp, sp);
+
+      if (thd->m_object_observer && type != TYPE_ENUM_TRIGGER)
+      {
+        sp->make_object_version(& sp_version);
+
+        if (thd->m_object_observer->notify(thd, & sp_version))
+          DBUG_RETURN(1);
+      }
+
+      *sphp= sp;
+      ret= 0;
+      break;
+    case SP_KEY_NOT_FOUND:
+      if (thd->m_object_observer && type != TYPE_ENUM_TRIGGER)
+      {
+        sp_head::make_object_null_version(& sp_version,
+                                          type,
+                                          & name.m_db,
+                                          & name.m_name);
+
+        if (thd->m_object_observer->notify(thd, & sp_version))
+          DBUG_RETURN(1);
+      }
+
+      *sphp= NULL;
+      ret= 0;
+      break;
+    default:
+      /*
+        Any error when loading an existing routine is either some problem
+        with the mysql.proc table, or a parse error because the contents
+        has been tampered with (in which case we clear that error).
+      */
+      if (ret == SP_PARSE_ERROR)
+        thd->clear_error();
+      /*
+        If we cleared the parse error, or when db_find_routine() flagged
+        an error with it's return value without calling my_error(), we
+        set the generic "mysql.proc table corrupt" error here.
+       */
+      if (! thd->is_error())
+      {
+        /*
+          SP allows full NAME_LEN chars thus he have to allocate enough
+          size in bytes. Otherwise there is stack overrun could happen
+          if multibyte sequence is `name`. `db` is still safe because the
+          rest of the server checks against NAME_LEN bytes and not chars.
+          Hence, the overrun happens only if the name is in length > 32 and
+          uses multibyte (cyrillic, greek, etc.)
+        */
+        char n[NAME_LEN*2+2];
+
+        /* m_qname.str is not always \0 terminated */
+        memcpy(n, name.m_qname.str, name.m_qname.length);
+        n[name.m_qname.length]= '\0';
+        my_error(ER_SP_PROC_TABLE_CORRUPT, MYF(0), n, ret);
+        ret= 1;
+      }
+      break;
+    }
+  }
+  else
+  {
+    if (thd->m_object_observer && type != TYPE_ENUM_TRIGGER)
+    {
+      sp->make_object_version(& sp_version);
+
+      if (thd->m_object_observer->notify(thd, & sp_version))
+        DBUG_RETURN(1);
+    }
+
+    ret= 0;
+    *sphp= sp;
+  }
+
+  DBUG_RETURN(ret);
+}
+
+
+/**
+  Refresh all the routines used by a statement in the session cache.
+  Note that routines that can be found are reloaded,
+  while routines that can not be found are ignored.
+  @param [in] thd Current thread
+  @param [in] lex Statement semantic tree
+  @return An error status
+    @retval 0 Success
+*/
+int
+sp_refresh_routines(THD *thd, LEX *lex)
+{
+  Sroutine_hash_entry *start;
+  int ret= 0;
+
+  DBUG_ENTER("sp_refresh_routines");
+
+  start= (Sroutine_hash_entry *)lex->sroutines_list.first;
+
+  for (Sroutine_hash_entry *rt= start; rt; rt= rt->next)
+  {
+    sp_head *sp;
+
+    ret= sp_cache_load(thd, rt, &sp);
+
+    if (ret)
+      break;
+  }
+  DBUG_RETURN(ret);
+}
+
+
+/**
   Cache sub-set of routines used by statement, add tables used by these
   routines to statement table list. Do the same for all routines used
   by these routines.
@@ -1871,60 +2023,13 @@ sp_cache_routines_and_add_tables_aux(THD
 
   for (Sroutine_hash_entry *rt= start; rt; rt= rt->next)
   {
-    sp_name name(thd, rt->key.str, rt->key.length);
-    int type= rt->key.str[0];
     sp_head *sp;
 
-    if (!(sp= sp_cache_lookup((type == TYPE_ENUM_FUNCTION ?
-                              &thd->sp_func_cache : &thd->sp_proc_cache),
-                              &name)))
-    {
-      switch ((ret= db_find_routine(thd, type, &name, &sp)))
-      {
-      case SP_OK:
-        {
-          if (type == TYPE_ENUM_FUNCTION)
-            sp_cache_insert(&thd->sp_func_cache, sp);
-          else
-            sp_cache_insert(&thd->sp_proc_cache, sp);
-        }
-        break;
-      case SP_KEY_NOT_FOUND:
-        ret= SP_OK;
-        break;
-      default:
-        /*
-          Any error when loading an existing routine is either some problem
-          with the mysql.proc table, or a parse error because the contents
-          has been tampered with (in which case we clear that error).
-        */
-        if (ret == SP_PARSE_ERROR)
-          thd->clear_error();
-        /*
-          If we cleared the parse error, or when db_find_routine() flagged
-          an error with it's return value without calling my_error(), we
-          set the generic "mysql.proc table corrupt" error here.
-         */
-        if (! thd->is_error())
-        {
-          /*
-            SP allows full NAME_LEN chars thus he have to allocate enough
-            size in bytes. Otherwise there is stack overrun could happen
-            if multibyte sequence is `name`. `db` is still safe because the
-            rest of the server checks agains NAME_LEN bytes and not chars.
-            Hence, the overrun happens only if the name is in length > 32 and
-            uses multibyte (cyrillic, greek, etc.)
-          */
-          char n[NAME_LEN*2+2];
-
-          /* m_qname.str is not always \0 terminated */
-          memcpy(n, name.m_qname.str, name.m_qname.length);
-          n[name.m_qname.length]= '\0';
-          my_error(ER_SP_PROC_TABLE_CORRUPT, MYF(0), n, ret);
-        }
-        break;
-      }
-    }
+    ret= sp_cache_load(thd, rt, &sp);
+
+    if (ret)
+      break;
+
     if (sp)
     {
       if (!(first && first_no_prelock))
diff -Nrup a/sql/sp.h b/sql/sp.h
--- a/sql/sp.h	2007-10-17 02:13:53 -06:00
+++ b/sql/sp.h	2008-02-20 20:55:30 -07:00
@@ -70,6 +70,7 @@ void sp_add_used_routine(LEX *lex, Query
                          sp_name *rt, char rt_type);
 void sp_remove_not_own_routines(LEX *lex);
 void sp_update_sp_used_routines(HASH *dst, HASH *src);
+int sp_refresh_routines(THD *thd, LEX *lex);
 int sp_cache_routines_and_add_tables(THD *thd, LEX *lex,
                                      bool first_no_prelock);
 int sp_cache_routines_and_add_tables_for_view(THD *thd, LEX *lex,
diff -Nrup a/sql/sp_head.cc b/sql/sp_head.cc
--- a/sql/sp_head.cc	2008-02-19 05:57:58 -07:00
+++ b/sql/sp_head.cc	2008-02-20 20:55:31 -07:00
@@ -22,6 +22,7 @@
 #include "sp_pcontext.h"
 #include "sp_rcontext.h"
 #include "sp_cache.h"
+#include "my_md5.h"
 
 /*
   Sufficient max length of printed destinations and frame offsets (all uints).
@@ -535,6 +536,55 @@ sp_head::sp_head()
   DBUG_VOID_RETURN;
 }
 
+int sp_head::make_object_version(Object_version *version)
+{
+  int rc;
+
+  switch(m_type)
+  {
+  case TYPE_ENUM_FUNCTION:
+    rc= version->m_oid.set_function(& m_db, & m_name);
+  version->m_vid.set_function(m_md5_value);
+    break;
+  case TYPE_ENUM_PROCEDURE:
+    rc= version->m_oid.set_procedure(& m_db, & m_name);
+  version->m_vid.set_procedure(m_md5_value);
+    break;
+  case TYPE_ENUM_TRIGGER:
+  default:
+    DBUG_ASSERT(FALSE);
+    rc= 1;
+  }
+
+  return rc;
+}
+
+int sp_head::make_object_null_version(
+  Object_version *version,
+  int type,
+  const LEX_STRING *db,
+  const LEX_STRING *name)
+{
+  int rc;
+
+  switch(type)
+  {
+  case TYPE_ENUM_FUNCTION:
+    rc= version->m_oid.set_function(db, name);
+    break;
+  case TYPE_ENUM_PROCEDURE:
+    rc= version->m_oid.set_procedure(db, name);
+    break;
+  case TYPE_ENUM_TRIGGER:
+  default:
+    DBUG_ASSERT(false);
+    rc= 1;
+  }
+
+  version->m_vid.set_null();
+
+  return rc;
+}
 
 void
 sp_head::init(LEX *lex)
@@ -632,6 +682,8 @@ sp_head::set_stmt_end(THD *thd)
 {
   Lex_input_stream *lip= thd->m_lip; /* shortcut */
   const char *end_ptr= lip->get_cpp_ptr(); /* shortcut */
+  my_MD5_CTX context;
+  uchar digest[16];
 
   /* Make the string of parameters. */
 
@@ -667,6 +719,20 @@ sp_head::set_stmt_end(THD *thd)
   m_defstr.length= end_ptr - lip->get_cpp_buf();
   m_defstr.str= thd->strmake(lip->get_cpp_buf(), m_defstr.length);
   trim_whitespace(thd->charset(), & m_defstr);
+
+  /*
+    Compute MD5(definition string), store the result in sp_head::m_md5_value.
+  */
+  my_MD5Init(&context);
+  my_MD5Update(&context,(uchar *) m_defstr.str, m_defstr.length);
+  my_MD5Final(digest, &context);
+
+  sprintf(m_md5_value,
+          "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
+          digest[0], digest[1], digest[2], digest[3],
+          digest[4], digest[5], digest[6], digest[7],
+          digest[8], digest[9], digest[10], digest[11],
+          digest[12], digest[13], digest[14], digest[15]);
 }
 
 
diff -Nrup a/sql/sp_head.h b/sql/sp_head.h
--- a/sql/sp_head.h	2008-01-25 10:14:58 -07:00
+++ b/sql/sp_head.h	2008-02-20 20:55:31 -07:00
@@ -194,6 +194,14 @@ public:
   LEX_STRING m_defstr;
   LEX_STRING m_definer_user;
   LEX_STRING m_definer_host;
+  char m_md5_value[MD5_CHAR_LENGTH + 1];
+
+  int make_object_version(Object_version * version);
+  static int make_object_null_version(
+    Object_version * version,
+    int type,
+    const LEX_STRING *db,
+    const LEX_STRING *name);
 
 private:
   Stored_program_creation_ctx *m_creation_ctx;
diff -Nrup a/sql/sql_base.cc b/sql/sql_base.cc
--- a/sql/sql_base.cc	2008-02-19 04:42:58 -07:00
+++ b/sql/sql_base.cc	2008-02-20 20:55:31 -07:00
@@ -2569,7 +2569,7 @@ bool check_if_table_exists(THD *thd, TAB
 
 
 TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
-		  bool *refresh, uint flags)
+                  bool *refresh, bool *invalidated, uint flags)
 {
   reg1	TABLE *table;
   char	key[MAX_DBKEY_LENGTH];
@@ -2581,9 +2581,12 @@ TABLE *open_table(THD *thd, TABLE_LIST *
   /* Parsing of partitioning information from .frm needs thd->lex set up. */
   DBUG_ASSERT(thd->lex->is_lex_started);
 
+  DBUG_ASSERT(refresh);
+  DBUG_ASSERT(invalidated);
+  *invalidated= FALSE;
+
   /* find a unused table in the open table cache */
-  if (refresh)
-    *refresh=0;
+  *refresh=0;
 
   /* an open table operation needs a lot of the stack space */
   if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (uchar *)&alias))
@@ -2624,7 +2627,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
                       (ulong) table->query_id, (uint) thd->server_id,
                       (ulong) thd->variables.pseudo_thread_id));
 	  my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
-	  DBUG_RETURN(0);
+          goto end_null;
 	}
 	table->query_id= thd->query_id;
 	thd->thread_specific_used= TRUE;
@@ -2637,7 +2640,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
   if (flags & MYSQL_OPEN_TEMPORARY_ONLY)
   {
     my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
-    DBUG_RETURN(0);
+    goto end_null;
   }
 
   /*
@@ -2670,7 +2673,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
           */
           my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
                    table->s->table_name.str);
-          DBUG_RETURN(0);
+          goto end_null;
         }
         /*
           When looking for a usable TABLE, ignore MERGE children, as they
@@ -2745,7 +2748,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
         {
           DBUG_ASSERT(table_list->view != 0);
           VOID(pthread_mutex_unlock(&LOCK_open));
-          DBUG_RETURN(0); // VIEW
+          goto end_null; // VIEW
         }
         VOID(pthread_mutex_unlock(&LOCK_open));
       }
@@ -2761,7 +2764,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
       my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->alias);
     else
       my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
-    DBUG_RETURN(0);
+    goto end_null;
   }
 
   /*
@@ -2796,10 +2799,9 @@ TABLE *open_table(THD *thd, TABLE_LIST *
            ! (flags & MYSQL_LOCK_IGNORE_FLUSH))
   {
     /* Someone did a refresh while thread was opening tables */
-    if (refresh)
-      *refresh=1;
+    *refresh=1;
     VOID(pthread_mutex_unlock(&LOCK_open));
-    DBUG_RETURN(0);
+    goto end_null;
   }
 
   /*
@@ -2866,7 +2868,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
       {
 	VOID(pthread_mutex_unlock(&LOCK_open));
         my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
-        DBUG_RETURN(0);
+        goto end_null;
       }
 
       /*
@@ -2911,9 +2913,8 @@ TABLE *open_table(THD *thd, TABLE_LIST *
         There is a refresh in progress for this table.
         Signal the caller that it has to try again.
       */
-      if (refresh)
-	*refresh=1;
-      DBUG_RETURN(0);
+      *refresh=1;
+      goto end_null;
     }
   }
   if (table)
@@ -2947,7 +2948,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
       if (check_if_table_exists(thd, table_list, &exists))
       {
         VOID(pthread_mutex_unlock(&LOCK_open));
-        DBUG_RETURN(NULL);
+        goto end_null;
       }
 
       if (!exists)
@@ -2958,7 +2959,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
         if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
         {
           VOID(pthread_mutex_unlock(&LOCK_open));
-          DBUG_RETURN(NULL);
+          goto end_null;
         }
         /*
           Link placeholder to the open tables list so it will be automatically
@@ -2969,7 +2970,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
         table->next= thd->open_tables;
         thd->open_tables= table;
         VOID(pthread_mutex_unlock(&LOCK_open));
-        DBUG_RETURN(table);
+        goto end_table;
       }
       /* Table exists. Let us try to open it. */
     }
@@ -2978,7 +2979,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
     if (!(table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME))))
     {
       VOID(pthread_mutex_unlock(&LOCK_open));
-      DBUG_RETURN(NULL);
+      goto end_null;
     }
 
     error= open_unireg_entry(thd, table, table_list, alias, key, key_length,
@@ -2987,7 +2988,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
     {
       my_free((uchar*)table, MYF(0));
       VOID(pthread_mutex_unlock(&LOCK_open));
-      DBUG_RETURN(NULL);
+      goto end_null;
     }
     if (table_list->view || error < 0)
     {
@@ -3000,7 +3001,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *
 
       my_free((uchar*)table, MYF(0));
       VOID(pthread_mutex_unlock(&LOCK_open));
-      DBUG_RETURN(0); // VIEW
+      goto end_null; // VIEW
     }
     DBUG_PRINT("info", ("inserting table '%s'.'%s' 0x%lx into the cache",
                         table->s->db.str, table->s->table_name.str,
@@ -3011,11 +3012,16 @@ TABLE *open_table(THD *thd, TABLE_LIST *
   check_unused();				// Debugging call
 
   VOID(pthread_mutex_unlock(&LOCK_open));
-  if (refresh)
-  {
-    table->next=thd->open_tables;		/* Link into simple list */
-    thd->open_tables=table;
-  }
+
+  /*
+    Link into simple list
+    Note that the table is added to the THD open tables,
+    even if it's rejected by the Object_observer later.
+    This is to avoid table leaks.
+  */
+  table->next=thd->open_tables;
+  thd->open_tables=table;
+
   table->reginfo.lock_type=TL_READ;		/* Assume read */
 
  reset:
@@ -3050,7 +3056,33 @@ TABLE *open_table(THD *thd, TABLE_LIST *
   table_list->updatable= 1; // It is not derived table nor non-updatable VIEW
   table->clear_column_bitmaps();
   DBUG_ASSERT(table->key_read == 0);
+
+end_table:
+  if (thd->m_object_observer && ! thd->in_sub_stmt)
+  {
+    Object_version table_version;
+    table->s->make_object_version(& table_version);
+    if (thd->m_object_observer->notify(thd, & table_version))
+    {
+      *refresh= FALSE;
+      *invalidated= TRUE;
+      DBUG_RETURN(NULL);
+    }
+  }
   DBUG_RETURN(table);
+
+end_null:
+  if (thd->m_object_observer && ! thd->in_sub_stmt &&
+      ! *refresh && ! table_list->view)
+  {
+    Object_version table_version;
+    TABLE_SHARE::make_object_null_version(& table_version,
+                                          table_list->db,
+                                          table_list->table_name);
+    if (thd->m_object_observer->notify(thd, & table_version))
+      *invalidated= TRUE;
+  }
+  DBUG_RETURN(NULL);
 }
 
 
@@ -4343,6 +4375,68 @@ bool fix_merge_after_open(TABLE_LIST *ol
 }
 
 
+int recheck_all_views(THD *thd, TABLE_LIST *start)
+{
+  Object_observer *observer;
+  TABLE_LIST *tables;
+  TABLE_LIST dummy;
+  int rc;
+  Object_version view_version;
+
+  observer= thd->m_object_observer;
+
+  if (observer == NULL)
+    return 0;
+
+  /*
+    FIXME: There is no equivalent of sroutines_list for views,
+    so we have to iterate in TABLE_LIST.
+  */
+  for (tables= start; tables; tables= tables->next_global)
+  {
+    if (tables->view)
+    {
+      /*
+        WARNING: [1] and [2] are due to the implementation of views,
+        where the view logical name is found:
+        - sometime in (db, table_name) if the view has not be processed,
+        - sometime in (view_db, view_name) if the view has been processed.
+        See the comments in mysql_make_view()
+      */
+
+      /* [1] mysql_load_view_md5 uses (db, table_name) */
+      dummy.db= tables->view_db.str;
+      dummy.table_name= tables->view_name.str;
+      rc= mysql_load_view_md5(thd, &dummy);
+
+      if (rc == 0)
+      {
+        /* [2] TABLE_LIST::make_object_version uses (view_db, view_name) */
+        LEX fake_lex;
+        dummy.view_db= tables->view_db;
+        dummy.view_name= tables->view_name;
+        dummy.view= & fake_lex;
+        rc= dummy.make_object_version(& view_version);
+      }
+      else
+      {
+        rc= TABLE_SHARE::make_object_null_version(& view_version,
+                                                  tables->db,
+                                                  tables->table_name);
+      }
+
+      if (rc != 0)
+        return 1;
+
+      if (observer->notify(thd, & view_version))
+        return 1;
+    }
+  }
+
+  return 0;
+}
+
+
 /*
   Open all tables in list
 
@@ -4379,6 +4473,7 @@ int open_tables(THD *thd, TABLE_LIST **s
   MEM_ROOT new_frm_mem;
   /* Also used for indicating that prelocking is need */
   TABLE_LIST **query_tables_last_own;
+  bool invalidated;
   bool safe_to_ignore_table;
 
   DBUG_ENTER("open_tables");
@@ -4394,6 +4489,15 @@ int open_tables(THD *thd, TABLE_LIST **s
   query_tables_last_own= 0;
   thd_proc_info(thd, "Opening tables");
 
+  if (! thd->in_sub_stmt)
+  {
+    if (recheck_all_views(thd, *start))
+    {
+      result= -1;
+      goto err;
+    }
+  }
+
   /*
     If we are not already executing prelocked statement and don't have
     statement for which table list for prelocking is already built, let
@@ -4426,6 +4530,17 @@ int open_tables(THD *thd, TABLE_LIST **s
       *start= thd->lex->query_tables;
     }
   }
+  else
+  {
+    if (! thd->in_sub_stmt)
+    {
+      if (sp_refresh_routines(thd, thd->lex))
+      {
+        result= -1;
+        goto err;
+      }
+    }
+  }
 
   /*
     For every table in the list of tables to open, try to find or open
@@ -4436,6 +4551,7 @@ int open_tables(THD *thd, TABLE_LIST **s
     DBUG_PRINT("tcache", ("opening table: '%s'.'%s'  item: 0x%lx",
                           tables->db, tables->table_name, (long) tables));
 
+    invalidated= FALSE;
     safe_to_ignore_table= FALSE;
 
     /*
@@ -4480,12 +4596,14 @@ int open_tables(THD *thd, TABLE_LIST **s
         */
         Prelock_error_handler prelock_handler;
         thd->push_internal_handler(& prelock_handler);
-        tables->table= open_table(thd, tables, &new_frm_mem, &refresh, flags);
+        tables->table= open_table(thd, tables, &new_frm_mem,
+                                  &refresh, &invalidated, flags);
         thd->pop_internal_handler();
         safe_to_ignore_table= prelock_handler.safely_trapped_errors();
       }
       else
-        tables->table= open_table(thd, tables, &new_frm_mem, &refresh, flags);
+        tables->table= open_table(thd, tables, &new_frm_mem,
+                                  &refresh, &invalidated, flags);
     }
     else
       DBUG_PRINT("tcache", ("referenced table: '%s'.'%s' 0x%lx",
@@ -4496,10 +4614,13 @@ int open_tables(THD *thd, TABLE_LIST **s
     {
       free_root(&new_frm_mem, MYF(MY_KEEP_PREALLOC));
 
+      if (! invalidated)
+      {
+/* TODO: tabulate the code after review */
       if (tables->view)
       {
         /* VIEW placeholder */
-	(*counter)--;
+        (*counter)--;
 
         /*
           tables->next_global list consists of two parts:
@@ -4519,7 +4640,7 @@ int open_tables(THD *thd, TABLE_LIST **s
           call destructor for this LEX.
         */
         hash_free(&tables->view->sroutines);
-	goto process_view_routines;
+        goto process_view_routines;
       }
 
       /*
@@ -4535,7 +4656,7 @@ int open_tables(THD *thd, TABLE_LIST **s
         parent_l->next_global= *parent_l->table->child_last_l;
       }
 
-      if (refresh)				// Refresh in progress
+      if (refresh)                            // Refresh in progress
       {
         /*
           We have met name-locked or old version of table. Now we have
@@ -4554,7 +4675,7 @@ int open_tables(THD *thd, TABLE_LIST **s
         if (query_tables_last_own)
           thd->lex->mark_as_requiring_prelocking(query_tables_last_own);
         close_tables_for_reopen(thd, start);
-	goto restart;
+        goto restart;
       }
 
       if (safe_to_ignore_table)
@@ -4563,6 +4684,7 @@ int open_tables(THD *thd, TABLE_LIST **s
                             tables->db, tables->alias));
         continue;
       }
+      }
 
       result= -1;				// Fatal error
       break;
@@ -4790,13 +4912,15 @@ TABLE *open_ltable(THD *thd, TABLE_LIST 
 {
   TABLE *table;
   bool refresh;
+  bool invalidated;
   DBUG_ENTER("open_ltable");
 
   thd_proc_info(thd, "Opening table");
   thd->current_tablenr= 0;
   /* open_ltable can be used only for BASIC TABLEs */
   table_list->required_type= FRMTYPE_TABLE;
-  while (!(table= open_table(thd, table_list, thd->mem_root, &refresh, 0)) &&
+  while (!(table= open_table(thd, table_list, thd->mem_root,
+                             &refresh, &invalidated, 0)) &&
          refresh)
     ;
 
@@ -8674,10 +8798,12 @@ open_system_tables_for_read(THD *thd, TA
   thd->reset_n_backup_open_tables_state(backup);
 
   uint count= 0;
-  bool not_used;
+  bool refresh_not_used;
+  bool invalidated_not_used;
   for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
   {
-    TABLE *table= open_table(thd, tables, thd->mem_root, &not_used,
+    TABLE *table= open_table(thd, tables, thd->mem_root,
+                             &refresh_not_used, &invalidated_not_used,
                              MYSQL_LOCK_IGNORE_FLUSH);
     if (!table)
       goto error;
@@ -8697,7 +8823,7 @@ open_system_tables_for_read(THD *thd, TA
       *(ptr++)= tables->table;
 
     thd->lock= mysql_lock_tables(thd, list, count,
-                                 MYSQL_LOCK_IGNORE_FLUSH, &not_used);
+                                 MYSQL_LOCK_IGNORE_FLUSH, &refresh_not_used);
   }
   if (thd->lock)
     DBUG_RETURN(FALSE);
diff -Nrup a/sql/sql_class.cc b/sql/sql_class.cc
--- a/sql/sql_class.cc	2008-02-19 05:57:59 -07:00
+++ b/sql/sql_class.cc	2008-02-20 20:55:31 -07:00
@@ -50,6 +50,298 @@ char empty_c_string[1]= {0};    /* used 
 
 const char * const THD::DEFAULT_WHERE= "field list";
 
+Object_id::Object_id()
+{
+  m_type= NO_OBJECT_TYPE;
+  m_key[0]= '\0';
+  m_key_length= 0;
+}
+
+Object_id::Object_id(const Object_id& oid)
+{
+  m_type= oid.m_type;
+  memcpy(m_key, oid.m_key, sizeof(m_key));
+  m_key_length= oid.m_key_length;
+}
+
+bool Object_id::is_equal(const Object_id& oid) const
+{
+  return ((m_type == oid.m_type) &&
+         (m_key_length == oid.m_key_length) &&
+         (memcmp(m_key, oid.m_key, m_key_length) == 0));
+}
+
+int
+Object_id::set(const Object_id *oid)
+{
+  m_type= oid->m_type;
+  m_key_length= oid->m_key_length;
+  memcpy(m_key, oid->m_key, m_key_length);
+  return 0;
+}
+
+int
+Object_id::set_table(const char* db, const char* name)
+{
+  /* FIXME: Fix TABLE_SHARE::db, TABLE_SHARE::table_name */
+  LEX_STRING lex_db;
+  LEX_STRING lex_name;
+  lex_db.str= (char*) db;
+  lex_db.length= strlen(lex_db.str);
+  lex_name.str= (char*) name;
+  lex_name.length= strlen(lex_name.str);
+
+  return set_table(& lex_db, & lex_name);
+}
+
+int
+Object_id::set_key(
+  char kind, const LEX_STRING *db, const LEX_STRING *name)
+{
+  DBUG_ASSERT(db->length <= NAME_LEN);
+  DBUG_ASSERT(name->length <= NAME_LEN);
+
+  /* format: <type> <db> . <name> \0 */
+  m_key_length= sprintf(m_key, "%c%.*s.%.*s",
+                        kind,
+                        (int) db->length, db->str,
+                        (int) name->length, name->str);
+  DBUG_ASSERT(m_key_length <= sizeof(m_key));
+
+  return 0;
+}
+
+Version_id::Version_id()
+  : m_kind(NULL_VERSION),
+    m_long_value(0)
+{
+  m_md5_value[0]= 0;
+}
+
+Version_id::Version_id(const Version_id& vid)
+{
+  m_kind= vid.m_kind;
+  switch(m_kind)
+  {
+  case NULL_VERSION:
+    break;
+  case TABLE_VERSION:
+  case TEMPORARY_TABLE_VERSION:
+    m_long_value= vid.m_long_value;
+    break;
+  case VIEW_VERSION:
+  case PROCEDURE_VERSION:
+  case FUNCTION_VERSION:
+    memcpy(m_md5_value, vid.m_md5_value, sizeof(m_md5_value));
+    break;
+  }
+}
+
+Version_id::~Version_id()
+{}
+
+bool
+Version_id::is_equal(const Version_id *vid) const
+{
+  if (m_kind != vid->m_kind)
+    return FALSE;
+
+  switch(m_kind)
+  {
+    case NULL_VERSION:
+      return TRUE;
+    case TABLE_VERSION:
+    case TEMPORARY_TABLE_VERSION:
+      return (m_long_value == vid->m_long_value);
+    case VIEW_VERSION:
+    case PROCEDURE_VERSION:
+    case FUNCTION_VERSION:
+      return (memcmp(m_md5_value, vid->m_md5_value, sizeof(m_md5_value)) == 0);
+    default:
+      DBUG_ASSERT(FALSE);
+  }
+}
+
+bool
+Version_id::is_null_or_equal(const Version_id *vid) const
+{
+  return is_null() || vid->is_null() || is_equal(vid);
+}
+
+bool
+Version_id::is_null() const
+{
+  return (m_kind == NULL_VERSION);
+}
+
+void
+Version_id::set_null()
+{
+  m_kind= NULL_VERSION;
+}
+
+void
+Version_id::set(const Version_id *vid)
+{
+  m_kind= vid->m_kind;
+
+  switch(m_kind)
+  {
+    case NULL_VERSION:
+      return;
+    case TABLE_VERSION:
+    case TEMPORARY_TABLE_VERSION:
+      m_long_value= vid->m_long_value;
+      return;
+    case VIEW_VERSION:
+    case PROCEDURE_VERSION:
+    case FUNCTION_VERSION:
+      memcpy(m_md5_value, vid->m_md5_value, sizeof(m_md5_value));
+      return;
+    default:
+      DBUG_ASSERT(FALSE);
+  }
+}
+
+Version_id& Version_id::operator = (const Version_id& vid)
+{
+  set(& vid);
+  return *this;
+}
+
+void
+Version_id::set_table(longlong value)
+{
+  m_kind= TABLE_VERSION;
+  m_long_value= value;
+}
+
+void
+Version_id::set_temporary_table(longlong value)
+{
+  m_kind= TEMPORARY_TABLE_VERSION;
+  m_long_value= value;
+}
+
+void
+Version_id::set_view(const char* md5)
+{
+  m_kind= VIEW_VERSION;
+  memcpy(m_md5_value, md5, sizeof(m_md5_value));
+}
+
+void
+Version_id::set_procedure(const char* md5)
+{
+  m_kind= PROCEDURE_VERSION;
+  memcpy(m_md5_value, md5, sizeof(m_md5_value));
+}
+
+void
+Version_id::set_function(const char* md5)
+{
+  m_kind= FUNCTION_VERSION;
+  memcpy(m_md5_value, md5, sizeof(m_md5_value));
+}
+
+extern "C" uchar*
+get_obj_version_hash_key(const uchar *buff, size_t *length,
+                         my_bool /* unused */)
+{
+  Object_version *over= (Object_version*) buff;
+  return over->m_oid.get_hash_key(length);
+}
+
+void* Object_version::operator new(size_t size, MEM_ROOT* mem_root) throw()
+{
+  void *ptr;
+  DBUG_ENTER("Object_version::operator new");
+  ptr= alloc_root(mem_root, size);
+  DBUG_RETURN(ptr);
+}
+
+void
+Object_version::operator delete(void *ptr, MEM_ROOT* mem_root) throw()
+{
+}
+
+int
+Object_version_collection::init(MEM_ROOT *mem_root)
+{
+  DBUG_ENTER("Object_version_collection::init");
+  int rc;
+
+  DBUG_ASSERT(m_mem_root == NULL);
+  m_mem_root= mem_root;
+
+  rc= hash_init(& m_hash,
+                & my_charset_bin,
+                0,                             /* initial size */
+                0,
+                0,
+                (hash_get_key) get_obj_version_hash_key,
+                NULL,                          /* Nothing to free */
+                MYF(0));
+
+  DBUG_RETURN(rc);
+}
+
+Object_version_collection::~Object_version_collection()
+{
+  if (m_mem_root)
+  {
+    (void) hash_free(& m_hash);
+    m_mem_root= NULL;
+  }
+}
+
+int
+Object_version_collection::add(
+  const Object_id& oid,
+  const Version_id& vid)
+{
+  Object_version *over;
+  size_t hash_key_length;
+  uchar* hash_key;
+
+  DBUG_ENTER("Object_version_collection::add()");
+
+  hash_key= oid.get_hash_key(& hash_key_length);
+  over= (Object_version*) hash_search(& m_hash, hash_key, hash_key_length);
+
+  if (over)
+  {
+    if (! over->m_vid.is_equal(& vid))
+      DBUG_RETURN(1);
+  }
+  else
+  {
+    over= new (m_mem_root) Object_version(oid, vid);
+    if (my_hash_insert(& m_hash, (uchar*) over))
+      DBUG_RETURN(2);
+  }
+
+  DBUG_PRINT("info",("key: %s  length: %d",
+                    hash_key, (int) hash_key_length));
+  DBUG_RETURN(0);
+}
+
+int
+Object_version_collection::find(
+  const Object_id& oid,
+  const Object_version ** over) const
+{
+  Object_version *v;
+  size_t hash_key_length;
+  uchar* hash_key;
+
+  hash_key= oid.get_hash_key(& hash_key_length);
+  v= (Object_version*) hash_search(& m_hash, hash_key, hash_key_length);
+
+  *over= v;
+  return 0;
+}
+
 
 /*****************************************************************************
 ** Instansiate templates
@@ -500,7 +792,8 @@ THD::THD()
    bootstrap(0),
    derived_tables_processing(FALSE),
    spcont(NULL),
-   m_lip(NULL)
+   m_lip(NULL),
+   m_object_observer(NULL)
 {
   ulong tmp;
 
@@ -616,11 +909,7 @@ THD::THD()
 
 void THD::push_internal_handler(Internal_error_handler *handler)
 {
-  /*
-    TODO: The current implementation is limited to 1 handler at a time only.
-    THD and sp_rcontext need to be modified to use a common handler stack.
-  */
-  DBUG_ASSERT(m_internal_handler == NULL);
+  handler->m_next= m_internal_handler;
   m_internal_handler= handler;
 }
 
@@ -628,19 +917,24 @@ void THD::push_internal_handler(Internal
 bool THD::handle_error(uint sql_errno, const char *message,
                        MYSQL_ERROR::enum_warning_level level)
 {
-  if (m_internal_handler)
+  Internal_error_handler *current= m_internal_handler;
+
+  while (current != NULL)
   {
-    return m_internal_handler->handle_error(sql_errno, message, level, this);
+    if (current->handle_error(sql_errno, message, level, this))
+      return TRUE;
+
+    current= current->m_next;
   }
 
-  return FALSE;                                 // 'FALSE', as per coding style
+  return FALSE;
 }
 
 
 void THD::pop_internal_handler()
 {
   DBUG_ASSERT(m_internal_handler != NULL);
-  m_internal_handler= NULL;
+  m_internal_handler= m_internal_handler->m_next;
 }
 
 extern "C"
diff -Nrup a/sql/sql_class.h b/sql/sql_class.h
--- a/sql/sql_class.h	2008-02-19 05:57:59 -07:00
+++ b/sql/sql_class.h	2008-02-20 20:55:31 -07:00
@@ -23,6 +23,207 @@
 #include "log.h"
 #include "rpl_tblmap.h"
 
+/**
+  The type of a MySQL object.
+*/
+enum Object_type
+{
+  NO_OBJECT_TYPE= 0,
+  /**
+    A table in the general sense: a temporary table,
+    a base table, or a view.
+  */
+  OBJECT_TYPE_TABLE= 1,
+  /** A stored procedure. */
+  OBJECT_TYPE_PROCEDURE= 2,
+  /** A stored function. */
+  OBJECT_TYPE_FUNCTION= 3
+
+  /*
+    TODO: To be extended:
+    - OBJECT_TYPE_TRIGGER,
+    - OBJECT_TYPE_UDF,
+    - OBJECT_TYPE_EVENT,
+    - OBJECT_TYPE_USER,
+    - OBJECT_TYPE_TABLESPACE,
+    - OBJECT_TYPE_PARTITION,
+    etc, as needed.
+  */
+};
+
+/*
+  Format: <type> <NAME> <.> <NAME> <\0>
+*/
+#define MAX_OBJ_ID_KEY_LEN (2*NAME_LEN + 3)
+
+/**
+  An Object Identifier.
+  An Object_id can represent "TABLE db.t1", "PROCEDURE db.proc" or
+  "FUNCTION db.func", and contain both a type and a fully qualified name.
+  This class is mainly used to provide a HASH friendly key representation.
+*/
+class Object_id
+{
+public:
+  Object_id();
+  Object_id(const Object_id& oid);
+  ~Object_id() {}
+
+  bool is_equal(const Object_id& oid) const;
+
+  Object_type get_type() const
+  { return m_type; }
+
+  int set(const Object_id *oid);
+
+  int set_table(const char* db, const char* name);
+
+  int set_table(const LEX_STRING *db, const LEX_STRING *name)
+  {
+    m_type= OBJECT_TYPE_TABLE;
+    return set_key('T', db, name);
+  }
+
+  int set_procedure(const LEX_STRING *db, const LEX_STRING *name)
+  {
+    m_type= OBJECT_TYPE_PROCEDURE;
+    return set_key('P', db, name);
+  }
+
+  int set_function(const LEX_STRING *db, const LEX_STRING *name)
+  {
+    m_type= OBJECT_TYPE_FUNCTION;
+    return set_key('F', db, name);
+  }
+
+  uchar* get_hash_key(size_t *length) const
+  {
+    *length= m_key_length;
+    return (uchar*) & m_key[0];
+  }
+
+  const char* get_printable_key() const
+  {
+    return & m_key[0];
+  }
+
+private:
+  int set_key(char kind, const LEX_STRING *db, const LEX_STRING *name);
+
+private:
+  /** Type of this object. */
+  Object_type m_type;
+  /**
+    Object key, expressed as a HASH friendly string.
+    The key consist of the type, followed by a fully qualified name.
+  */
+  char m_key[MAX_OBJ_ID_KEY_LEN];
+  /** Length of m_key. */
+  uint m_key_length;
+
+private: // Not implemented, use set()
+  Object_id& operator = (const Object_id&);
+};
+
+/**
+  An Object Version.
+  An object version represent a specific version of a given object,
+  like "TABLE db.t1, version 1".
+  Object_version is primarily used to build collections,
+  see Object_version_collection.
+*/
+class Object_version
+{
+public:
+  static void* operator new(size_t size, MEM_ROOT* mem_root) throw();
+  static void operator delete(void *ptr, MEM_ROOT* mem_root) throw();
+
+  Object_version()
+  {}
+
+  Object_version(const Object_id& oid, const Version_id& vid)
+   : m_oid(oid), m_vid(vid)
+  {}
+
+  ~Object_version() {}
+
+  Object_id m_oid;
+  Version_id m_vid;
+
+private: // Non implemented
+  Object_version(const Object_version&);
+  Object_version& operator = (const Object_version&);
+};
+
+
+/**
+  A collection of object versions.
+  This class is a basic container of Object_version,
+  and enforces that at most one version of a given object can exist in the
+  collection.
+  For example, this collection can contain:
+  - "TABLE db.t1, version 1"
+  - "TABLE db.t2, version 2",
+  - "PROCEDURE db.proc, version AA",
+  - "TABLE db.t3, version 1"
+  but can not contain both:
+  - "TABLE db.t1, version 1"
+  - "TABLE db.t1, version 2"
+  The collection itself is implemented by a HASH.
+*/
+
+class Object_version_collection
+{
+public:
+  Object_version_collection()
+    : m_mem_root(NULL)
+  {}
+
+  ~Object_version_collection();
+
+  int init(MEM_ROOT *mem_root);
+  int add(const Object_id& oid, const Version_id & vid);
+  int find(const Object_id& oid, const Object_version ** over) const;
+
+private:
+  HASH m_hash;
+  MEM_ROOT *m_mem_root;
+
+private: // Non implemented
+  Object_version_collection(const Object_version_collection&);
+  Object_version_collection& operator=(const Object_version_collection&);
+};
+
+/**
+  Interface class for observers, used to inspect objects referenced
+  by a statement.
+  An Object_observer can inspect existing objects,
+  as well as get notified that some objects used in a statement are missing.
+  Observers can cause the current operation to fail, by returning an error
+  from the notification methods.
+*/
+
+class Object_observer
+{
+protected:
+  Object_observer() {}
+  virtual ~Object_observer() {}
+
+public:
+  /**
+    Notify that an object is used in a statement.
+    @param [in] thd Current thread
+    @param [in] found object version found during open
+    @return An error status
+      @retval 0 Success
+  */
+  virtual int notify(THD *thd, const Object_version *found) = 0;
+
+private: // Non implemented
+  Object_observer(const Object_observer&);
+  Object_observer& operator = (const Object_observer&);
+};
+
 class Relay_log_info;
 
 class Query_log_event;
@@ -406,6 +607,7 @@ typedef struct system_status_var
   ulong filesort_scan_count;
   /* Prepared statements and binary protocol */
   ulong com_stmt_prepare;
+  ulong com_stmt_reprepare;
   ulong com_stmt_execute;
   ulong com_stmt_send_long_data;
   ulong com_stmt_fetch;
@@ -454,7 +656,7 @@ public:
   bool is_backup_arena; /* True if this arena is used for backup. */
 #endif
   /*
-    The states relfects three diffrent life cycles for three
+    The states reflects three different life cycles for three
     different types of statements:
     Prepared statement: INITIALIZED -> PREPARED -> EXECUTED.
     Stored procedure:   INITIALIZED_FOR_SP -> EXECUTED.
@@ -943,7 +1145,10 @@ enum enum_thread_type
 class Internal_error_handler
 {
 protected:
-  Internal_error_handler() {}
+  Internal_error_handler()
+   : m_next(NULL)
+  {}
+
   virtual ~Internal_error_handler() {}
 
 public:
@@ -976,6 +1181,10 @@ public:
                             const char *message,
                             MYSQL_ERROR::enum_warning_level level,
                             THD *thd) = 0;
+
+private:
+  friend class THD;
+  Internal_error_handler *m_next;
 };
 
 
@@ -1716,6 +1925,8 @@ public:
     and may point to invalid memory after that.
   */
   Lex_input_stream *m_lip;
+
+  Object_observer *m_object_observer;
 
 #ifdef WITH_PARTITION_STORAGE_ENGINE
   partition_info *work_part_info;
diff -Nrup a/sql/sql_insert.cc b/sql/sql_insert.cc
--- a/sql/sql_insert.cc	2008-02-19 05:45:16 -07:00
+++ b/sql/sql_insert.cc	2008-02-20 20:55:31 -07:00
@@ -3392,7 +3392,10 @@ static TABLE *create_table_from_items(TH
       }
       else
       {
-        if (!(table= open_table(thd, create_table, thd->mem_root, (bool*) 0,
+        bool refresh_not_used;
+        bool invalidated_not_used;
+        if (!(table= open_table(thd, create_table, thd->mem_root,
+                                &refresh_not_used, &invalidated_not_used,
                                 MYSQL_OPEN_TEMPORARY_ONLY)) &&
             !create_info->table_existed)
         {
diff -Nrup a/sql/sql_prepare.cc b/sql/sql_prepare.cc
--- a/sql/sql_prepare.cc	2008-02-19 05:58:00 -07:00
+++ b/sql/sql_prepare.cc	2008-02-20 20:55:31 -07:00
@@ -96,6 +96,16 @@ When one supplies long data for a placeh
 #include <mysql_com.h>
 #endif
 
+/*
+  FIXME: PROTOTYPING
+  #undef to make sure refactoring alone does not cause regressions
+  #define to test re-prepare itself
+*/
+#define HAVE_VALIDATION
+#define HAVE_RE_PREPARE
+#define HAVE_RSP
+#define HAVE_REBIND
+
 /**
   A result class used to send cursor rows using the binary protocol.
 */
@@ -116,8 +126,446 @@ public:
 #endif
 };
 
+/**
+  Collect the version number of all the objects seen during PREPARE.
+  The list of object versions recorded is stored with a prepared statement,
+  and is used during EXECUTE to re-validate the statement.
+*/
+class Prepare_observer : public Object_observer
+{
+public:
+  Prepare_observer(Object_version_collection* coll)
+    : m_coll(coll)
+  {}
+
+  virtual ~Prepare_observer() {}
+
+  virtual int notify(THD *thd, const Object_version* found);
+
+private:
+  Object_version_collection *m_coll;
+};
+
+/**
+  Enforce that the objects used during EXECUTE are valid.
+  This class enforces integrity constraints, and guarantees that the content
+  of a prepared statement is still valid and can be used to EXECUTE the
+  statement.
+*/
+class Execute_observer : public Object_observer
+{
+public:
+  Execute_observer(Object_version_collection* coll)
+    : m_coll(coll)
+  {}
+
+  virtual ~Execute_observer() {}
+
+  virtual int notify(THD *thd, const Object_version* found);
+
+private:
+  bool is_safe_table_operation(LEX *lex) const;
+  bool is_safe_sp_operation(LEX *lex) const;
+  int notify_table_or_view(THD *thd, const Object_version *found);
+  int notify_function_or_procedure(THD *thd, const Object_version *found);
+
+private:
+  Object_version_collection *m_coll;
+};
+
+class Reprepare_handler: public Internal_error_handler
+{
+public:
+  Reprepare_handler()
+    : m_recoverable_errors(0), m_fatal_errors(0)
+  {}
+
+  virtual ~Reprepare_handler() {}
+
+  virtual bool handle_error(uint sql_errno, const char *message,
+                            MYSQL_ERROR::enum_warning_level level,
+                            THD *thd);
+
+  bool need_reprepare();
+
+private:
+  int m_recoverable_errors;
+  int m_fatal_errors;
+};
+
+bool
+Reprepare_handler::handle_error(uint sql_errno,
+                                const char * /* message */,
+                                MYSQL_ERROR::enum_warning_level /* level */,
+                                THD * /* thd */)
+{
+  if (sql_errno == ER_NEED_REPREPARE)
+  {
+    m_recoverable_errors++;
+  }
+
+  if (m_recoverable_errors)
+#ifdef HAVE_RE_PREPARE
+    return TRUE;
+#else
+    return FALSE;
+#endif
+
+  m_fatal_errors++;
+  return FALSE;
+}
+
+bool
+Reprepare_handler::need_reprepare()
+{
+  return ((m_recoverable_errors > 0) && (m_fatal_errors == 0));
+}
+
+int
+Prepare_observer::notify(THD *thd, const Object_version *found)
+{
+  int rc;
+
+  DBUG_ENTER("Prepare_observer::notify");
+
+  DBUG_PRINT("info", ("key= %s",
+                      found->m_oid.get_printable_key()));
+
+  switch(found->m_oid.get_type())
+  {
+  case OBJECT_TYPE_TABLE:
+  case OBJECT_TYPE_PROCEDURE:
+  case OBJECT_TYPE_FUNCTION:
+    rc= m_coll->add(found->m_oid, found->m_vid);
+  default:
+    rc= 0;
+  }
+
+  DBUG_RETURN(rc);
+}
+
+/**
+  Indicate if DDL performed on a table is safe for a prepared statement.
+  Some statements can be applied to different tables at each execution,
+  and the re-validation code should allow them.
+*/
+bool
+Execute_observer::is_safe_table_operation(LEX *lex) const
+{
+  switch(lex->sql_command)
+  {
+  case SQLCOM_ALTER_TABLE:
+  case SQLCOM_REPAIR:
+  case SQLCOM_ANALYZE:
+  case SQLCOM_OPTIMIZE:
+    return TRUE;
+
+  case SQLCOM_SHOW_CREATE: // safe for tables, not views
+  default:
+    return FALSE;
+  }
+}
+
+/**
+  Indicate if DDL performed on a procedure or function is safe for a
+  prepared statement.
+  Some statements can be applied to different SP/SF at each execution,
+  and the re-validation code should allow them.
+*/
+bool
+Execute_observer::is_safe_sp_operation(LEX *lex) const
+{
+  switch(lex->sql_command)
+  {
+  case SQLCOM_SHOW_CREATE_PROC:
+  case SQLCOM_SHOW_CREATE_FUNC:
+    return TRUE;
+
+  default:
+    return FALSE;
+  }
+}
+
+int
+Execute_observer::notify(THD *thd, const Object_version *found)
+{
+  int rc;
+
+  DBUG_ENTER("Execute_observer::notify()");
+
+  DBUG_PRINT("info", ("key= %s",
+                      found->m_oid.get_printable_key()));
+
+  DBUG_ASSERT(! thd->in_sub_stmt);
+
+#ifdef HAVE_VALIDATION
+  switch(found->m_oid.get_type())
+  {
+  case OBJECT_TYPE_TABLE:
+    rc= notify_table_or_view(thd, found);
+    break;
+  case OBJECT_TYPE_PROCEDURE:
+  case OBJECT_TYPE_FUNCTION:
+    rc= notify_function_or_procedure(thd, found);
+    break;
+  case NO_OBJECT_TYPE:
+  default:
+    rc= 0;
+    break;
+  }
+#else
+  rc= 0;
+#endif
+
+  DBUG_RETURN(rc);
+}
+
+int
+Execute_observer::notify_table_or_view(THD *thd, const Object_version *found)
+{
+  const Object_version *cached;
+  int rc;
+
+  DBUG_ENTER("Execute_observer::notify_table_or_view");
+
+  rc= m_coll->find(found->m_oid, & cached);
+  if (rc != 0)
+    goto err;
+
+  if (cached)
+  {
+    if (! cached->m_vid.is_equal(& found->m_vid))
+    {
+      if (! is_safe_table_operation(thd->lex))
+      {
+        my_error(ER_NEED_REPREPARE, MYF(0));
+        rc= 1;
+      }
+    }
+  }
+  else
+  {
+    /*
+      Here, the Prepared statement execute code is discovering
+      an object that was never found during the prepare phase.
+      For some statements, the prepare phase does not open and lock
+      tables (which is ok, performances), so that this is a normal case.
+      Example of such statements are:
+      - SHOW COLUMNS FROM <table>
+        --> open the table <table>
+      - INSTALL PLUGIN ... SONAME ...
+        --> open the table mysql.plugin
+      - SHOW CREATE TRIGGER t1_bi
+        --> open the table t1 (that the trigger relates to)
+      - SHOW COLUMNS FROM <view>
+        --> open the view <view>
+      Once a real object (version not null) is found by the first
+      execute(), it's added to the prepared statement internal state.
+      This is to properly detect further DDL changes for these late found
+      dependencies.
+      This is critical, since until Bug#27011 is fixed, the internal
+      state of the statement can contain a copy of view DDL,
+      which needs to be checked for changes later.
+    */
+    if (! found->m_vid.is_null())
+      rc= m_coll->add(found->m_oid, found->m_vid);
+  }
+
+err:
+  DBUG_RETURN(rc);
+}
+
+int
+Execute_observer::notify_function_or_procedure(THD *thd,
+                                               const Object_version *found)
+{
+  const Object_version *cached;
+  int rc;
+
+  DBUG_ENTER("Execute_observer::notify_function_or_procedure");
+
+  rc= m_coll->find(found->m_oid, & cached);
+  if (rc != 0)
+    goto err;
+
+  if (cached)
+  {
+    if (! cached->m_vid.is_equal(& found->m_vid))
+    {
+      if (! is_safe_sp_operation(thd->lex))
+      {
+        /*
+          For these objects, we do require an exact match:
+          - Version_id::is_equal() means that:
+            - either the object did not exist, and still does not exist
+            - or the object existed and is unchanged.
+        */
+        my_error(ER_NEED_REPREPARE, MYF(0));
+        rc= 1;
+      }
+    }
+  }
+  else
+  {
+    rc= m_coll->add(found->m_oid, found->m_vid);
+  }
+
+err:
+  DBUG_RETURN(rc);
+}
+
 /****************************************************************************/
 
+class Prepared_statement;
+
+class Parameter_setter
+{
+public:
+  int set(Prepared_statement *stmt, String *expanded_query);
+
+protected:
+  virtual int set_with_log(Prepared_statement *stmt,
+                           String *expanded_query) = 0;
+  virtual int set_no_log(Prepared_statement *stmt) = 0;
+
+  Parameter_setter()
+  {}
+
+  virtual ~Parameter_setter()
+  {}
+};
+
+static void reset_stmt_params(Prepared_statement *stmt);
+
+class User_vars_parameter_setter: public Parameter_setter
+{
+public:
+  User_vars_parameter_setter(List<LEX_STRING>& varnames)
+    : m_varnames(varnames)
+  {}
+
+  ~User_vars_parameter_setter()
+  {}
+
+protected:
+  virtual int set_with_log(Prepared_statement *stmt,
+                           String *expanded_query);
+  virtual int set_no_log(Prepared_statement *stmt);
+
+private:
+  List<LEX_STRING>& m_varnames;
+};
+
+#ifndef EMBEDDED_LIBRARY
+static bool setup_conversion_functions(Prepared_statement *stmt,
+                                       uchar **data, uchar *data_end);
+
+class Packet_parameter_setter: public Parameter_setter
+{
+public:
+  Packet_parameter_setter(uchar* packet_null_array, uchar* packet_end)
+  : m_packet_null_array(packet_null_array),
+    m_packet_end(packet_end)
+  {}
+
+  ~Packet_parameter_setter()
+  {}
+
+protected:
+  virtual int set_with_log(Prepared_statement *stmt,
+                           String *expanded_query);
+  virtual int set_no_log(Prepared_statement *stmt);
+
+private:
+  uchar* m_packet_null_array;
+  uchar* m_packet_end;
+};
+#endif
+
+#ifdef EMBEDDED_LIBRARY
+class Embedded_parameter_setter: public Parameter_setter
+{
+public:
+  Embedded_parameter_setter()
+  {}
+
+  ~Embedded_parameter_setter()
+  {}
+
+protected:
+  virtual int set_with_log(Prepared_statement *stmt,
+                           String *expanded_query);
+  virtual int set_no_log(Prepared_statement *stmt);
+};
+#endif
+
+/* TODO: Sp_head_parameter_setter, to use sp_head local variables */
+
+class Result_set_processor
+{
+public:
+  virtual bool process(Prepared_statement *stmt,
+                       select_result *result, List<Item> *fields) = 0;
+
+protected:
+  Result_set_processor()
+  {}
+
+  virtual ~Result_set_processor()
+  {}
+};
+
+class Prepare_RS_processor: public Result_set_processor
+{
+protected:
+  Prepare_RS_processor()
+  {}
+
+  ~Prepare_RS_processor()
+  {}
+
+  bool record_metadata(Prepared_statement *stmt, List<Item> *fields);
+};
+
+class Text_RS_processor : public Prepare_RS_processor
+{
+public:
+  Text_RS_processor()
+  {}
+
+  ~Text_RS_processor()
+  {}
+
+  virtual bool process(Prepared_statement *stmt,
+                       select_result *result, List<Item> *fields);
+};
+
+class Binary_RS_processor : public Prepare_RS_processor
+{
+public:
+  Binary_RS_processor()
+  {}
+
+  ~Binary_RS_processor()
+  {}
+
+  virtual bool process(Prepared_statement *stmt,
+                       select_result *result, List<Item> *fields);
+};
+
+class Reprepare_RS_processor : public Result_set_processor
+{
+public:
+  Reprepare_RS_processor()
+  {}
+
+  ~Reprepare_RS_processor()
+  {}
+
+  virtual bool process(Prepared_statement *stmt,
+                       select_result *result, List<Item> *fields);
+};
+
 /**
   Prepared_statement: a statement that can contain placeholders.
 */
@@ -134,32 +582,42 @@ public:
   Select_fetch_protocol_binary result;
   Protocol *protocol;
   Item_param **param_array;
+  ushort *param_typecode_array;
   uint param_count;
   uint last_errno;
   uint flags;
   char last_error[MYSQL_ERRMSG_SIZE];
-#ifndef EMBEDDED_LIBRARY
-  bool (*set_params)(Prepared_statement *st, uchar *data, uchar *data_end,
-                     uchar *read_pos, String *expanded_query);
-#else
-  bool (*set_params_data)(Prepared_statement *st, String *expanded_query);
-#endif
-  bool (*set_params_from_vars)(Prepared_statement *stmt,
-                               List<LEX_STRING>& varnames,
-                               String *expanded_query);
+
 public:
   Prepared_statement(THD *thd_arg, Protocol *protocol_arg);
+  Prepared_statement(Prepared_statement *model);
   virtual ~Prepared_statement();
-  void setup_set_params();
   virtual Query_arena::Type type() const;
   virtual void cleanup_stmt();
   bool set_name(LEX_STRING *name);
   inline void close_cursor() { delete cursor; cursor= 0; }
 
-  bool prepare(const char *packet, uint packet_length);
-  bool execute(String *expanded_query, bool open_cursor);
+  static bool execute_loop(THD *thd, Prepared_statement **stmt_ptr,
+                           Parameter_setter *setter, bool open_cursor);
+
+  bool prepare(Result_set_processor *rsp,
+               const char *packet, uint packet_length);
+  bool reprepare(const char *packet, uint packet_length);
+  bool prep(Result_set_processor *rsp, const char *packet, uint packet_length);
+  bool execute(Parameter_setter *setter, bool open_cursor,
+               bool *need_reprepare);
   /* Destroy this statement */
   bool deallocate();
+
+  bool set_column_count(uint count);
+  bool check_column_count(uint count);
+
+  bool set_column_metadata(uint col, Item* metadata);
+  bool check_column_metadata(uint col, Item* metadata);
+
+  bool clone_state(Prepared_statement *model);
+  bool clone_column_metadata(Send_field *col, Send_field *model);
+
 private:
   /**
     Store the parsed tree of a prepared statement here.
@@ -170,6 +628,20 @@ private:
     SELECT_LEX and other classes).
   */
   MEM_ROOT main_mem_root;
+
+  /**
+    Collection of all objects versions this prepared statement depends on.
+    The prepared statement internal state contains optimizations based on
+    the objects seen during prepare. Should any of the objects listed change
+    between prepare() and execute(), the prepared statement will be declared
+    invalidated (with some exceptions, see Execute_observer).
+  */
+  Object_version_collection m_versions;
+
+  ulong m_sql_mode;
+
+  uint m_column_count;
+  Send_field *m_columns_metadata;
 };
 
 
@@ -777,6 +1249,23 @@ static bool insert_params_with_log(Prepa
   DBUG_RETURN(0);
 }
 
+int Packet_parameter_setter::set_with_log(Prepared_statement *stmt,
+                                          String *expanded_query)
+{
+  uchar *parsed_packet= m_packet_null_array;
+
+  DBUG_ENTER("Packet_parameter_setter::set_with_log");
+
+  if (setup_conversion_functions(stmt, &parsed_packet, m_packet_end) ||
+      insert_params_with_log(stmt, m_packet_null_array, parsed_packet,
+                             m_packet_end, expanded_query))
+  {
+    DBUG_RETURN(1);
+  }
+
+  DBUG_RETURN(0);
+}
+
 
 static bool insert_params(Prepared_statement *stmt, uchar *null_array,
                           uchar *read_pos, uchar *data_end,
@@ -809,6 +1298,20 @@ static bool insert_params(Prepared_state
   DBUG_RETURN(0);
 }
 
+int Packet_parameter_setter::set_no_log(Prepared_statement *stmt)
+{
+  DBUG_ENTER("Packet_parameter_setter::set_no_log");
+  uchar *parsed_packet= m_packet_null_array;
+
+  if (setup_conversion_functions(stmt, &parsed_packet, m_packet_end) ||
+      insert_params(stmt, m_packet_null_array, parsed_packet, m_packet_end, NULL))
+  {
+    DBUG_RETURN(1);
+  }
+
+  DBUG_RETURN(0);
+}
+
 
 static bool setup_conversion_functions(Prepared_statement *stmt,
                                        uchar **data, uchar *data_end)
@@ -816,31 +1319,52 @@ static bool setup_conversion_functions(P
   /* skip null bits */
   uchar *read_pos= *data + (stmt->param_count+7) / 8;
 
+  /*
+    First execute or types altered by the client, setup the
+    conversion routines for all parameters (one time)
+  */
+  Item_param **it= stmt->param_array;
+  Item_param **end= it + stmt->param_count;
+  THD *thd= stmt->thd;
+  ushort typecode;
+  const uint signed_bit= 1 << 15;
+  uint param_index= 0;
+
   DBUG_ENTER("setup_conversion_functions");
 
   if (*read_pos++) //types supplied / first execute
   {
-    /*
-      First execute or types altered by the client, setup the
-      conversion routines for all parameters (one time)
-    */
-    Item_param **it= stmt->param_array;
-    Item_param **end= it + stmt->param_count;
-    THD *thd= stmt->thd;
-    for (; it < end; ++it)
+    for (; it < end; ++it, param_index++)
     {
-      ushort typecode;
-      const uint signed_bit= 1 << 15;
-
       if (read_pos >= data_end)
         DBUG_RETURN(1);
 
       typecode= sint2korr(read_pos);
+
+      /* Save the typecode for future use */
+      stmt->param_typecode_array[param_index]= typecode;
+
       read_pos+= 2;
       (**it).unsigned_flag= test(typecode & signed_bit);
       setup_one_conversion_function(thd, *it, (uchar) (typecode & ~signed_bit));
     }
   }
+  else
+  {
+    /*
+      Types are not supplied with this message,
+      but have been supplied before and saved in param_typecode_array[]
+    */
+    for (; it < end; ++it, param_index++)
+    {
+      /* Retrieve a typecode previously saved */
+      typecode= stmt->param_typecode_array[param_index];
+
+      (**it).unsigned_flag= test(typecode & signed_bit);
+      setup_one_conversion_function(thd, *it, (uchar) (typecode & ~signed_bit));
+    }
+  }
+
   *data= read_pos;
   DBUG_RETURN(0);
 }
@@ -893,6 +1417,13 @@ static bool emb_insert_params(Prepared_s
   DBUG_RETURN(0);
 }
 
+int Embedded_parameter_setter::set_no_log(Prepared_statement *stmt)
+{
+  int result;
+  DBUG_ENTER("Embedded_parameter_setter::set_no_log");
+  result= emb_insert_params(stmt, NULL);
+  DBUG_RETURN(result);
+}
 
 static bool emb_insert_params_with_log(Prepared_statement *stmt,
                                        String *query)
@@ -943,6 +1474,15 @@ static bool emb_insert_params_with_log(P
   DBUG_RETURN(0);
 }
 
+int Embedded_parameter_setter::set_with_log(Prepared_statement *stmt,
+                                            String *expanded_query)
+{
+  int result;
+  DBUG_ENTER("Embedded_parameter_setter::set_with_log");
+  result= emb_insert_params_with_log(stmt, expanded_query);
+  DBUG_RETURN(result);
+}
+
 #endif /*!EMBEDDED_LIBRARY*/
 
 
@@ -981,6 +1521,13 @@ static bool insert_params_from_vars(Prep
   DBUG_RETURN(0);
 }
 
+int User_vars_parameter_setter::set_no_log(Prepared_statement *stmt)
+{
+  int result;
+  DBUG_ENTER("User_vars_parameter_setter::set_no_log");
+  result= insert_params_from_vars(stmt, m_varnames, NULL);
+  DBUG_RETURN(result);
+}
 
 /**
   Do the same as insert_params_from_vars but also construct query text for
@@ -1040,6 +1587,15 @@ static bool insert_params_from_vars_with
   DBUG_RETURN(0);
 }
 
+int User_vars_parameter_setter::set_with_log(Prepared_statement *stmt,
+                                             String *expanded_query)
+{
+  int result;
+  DBUG_ENTER("User_vars_parameter_setter::set_with_log");
+  result= insert_params_from_vars_with_log(stmt, m_varnames, expanded_query);
+  DBUG_RETURN(result);
+}
+
 /**
   Validate INSERT statement.
 
@@ -1268,6 +1824,7 @@ error:
 */
 
 static int mysql_test_select(Prepared_statement *stmt,
+                             Result_set_processor *rsp,
                              TABLE_LIST *tables, bool text_protocol)
 {
   THD *thd= stmt->thd;
@@ -1304,6 +1861,8 @@ static int mysql_test_select(Prepared_st
   */
   if (unit->prepare(thd, 0, 0))
     goto error;
+
+#ifndef HAVE_RSP
   if (!lex->describe && !text_protocol)
   {
     /* Make copy of item list, as change_columns may change it */
@@ -1314,7 +1873,7 @@ static int mysql_test_select(Prepared_st
       goto error;
 
     /*
-      We can use lex->result as it should've been prepared in
+      We can use lex->result as it should have been prepared in
       unit->prepare call above.
     */
     if (send_prep_stmt(stmt, lex->result->field_count(fields)) ||
@@ -1323,6 +1882,28 @@ static int mysql_test_select(Prepared_st
       goto error;
     DBUG_RETURN(2);
   }
+#endif
+
+#ifdef HAVE_RSP
+  if (!lex->describe)
+  {
+    /* Make copy of item list, as change_columns may change it */
+    List<Item> fields(lex->select_lex.item_list);
+
+    /* Change columns if a procedure like analyse() */
+    if (unit->last_procedure && unit->last_procedure->change_columns(fields))
+      goto error;
+
+    /*
+      We can use lex->result as it should have been prepared in
+      unit->prepare call above.
+    */
+    if (rsp->process(stmt, lex->result, &fields))
+      goto error;
+    DBUG_RETURN(2);
+  }
+#endif
+
   DBUG_RETURN(0);
 error:
   DBUG_RETURN(1);
@@ -1517,7 +2098,7 @@ static bool mysql_test_create_table(Prep
     res= select_like_stmt_test(stmt, 0, 0);
   }
 
-  /* put tables back for PS rexecuting */
+  /* put tables back for PS re-executing */
   lex->link_first_table_back(create_table, link_to_local);
   DBUG_RETURN(res);
 }
@@ -1675,9 +2256,12 @@ static bool mysql_test_insert_select(Pre
 */
 
 static bool check_prepared_statement(Prepared_statement *stmt,
+                                     Result_set_processor *rsp,
                                      bool text_protocol)
 {
+#ifndef HAVE_RSP
   THD *thd= stmt->thd;
+#endif
   LEX *lex= stmt->lex;
   SELECT_LEX *select_lex= &lex->select_lex;
   TABLE_LIST *tables;
@@ -1718,7 +2302,7 @@ static bool check_prepared_statement(Pre
     break;
 
   case SQLCOM_SELECT:
-    res= mysql_test_select(stmt, tables, text_protocol);
+    res= mysql_test_select(stmt, rsp, tables, text_protocol);
     if (res == 2)
     {
       /* Statement and field info has already been sent */
@@ -1831,9 +2415,18 @@ static bool check_prepared_statement(Pre
     }
     break;
   }
+
+#ifndef HAVE_RSP
   if (res == 0)
     DBUG_RETURN(text_protocol? FALSE : (send_prep_stmt(stmt, 0) ||
                                         thd->protocol->flush()));
+#endif
+
+#ifdef HAVE_RSP
+  if (res == 0)
+    DBUG_RETURN(rsp->process(stmt, NULL, NULL));
+#endif
+
 error:
   DBUG_RETURN(TRUE);
 }
@@ -1855,6 +2448,7 @@ static bool init_param_array(Prepared_st
       my_message(ER_PS_MANY_PARAM, ER(ER_PS_MANY_PARAM), MYF(0));
       return TRUE;
     }
+    uint i;
     Item_param **to;
     List_iterator<Item_param> param_iterator(lex->param_list);
     /* Use thd->mem_root as it points at statement mem_root */
@@ -1863,12 +2457,27 @@ static bool init_param_array(Prepared_st
                                   sizeof(Item_param*) * stmt->param_count);
     if (!stmt->param_array)
       return TRUE;
+
+    if (stmt->param_typecode_array == NULL)
+    {
+      /* First PREPARE */
+      stmt->param_typecode_array= (ushort*)
+                                  alloc_root(stmt->thd->mem_root,
+                                           sizeof(ushort) * stmt->param_count);
+      if (!stmt->param_typecode_array)
+        return TRUE;
+
+      for (i= 0; i < stmt->param_count; i++)
+        stmt->param_typecode_array[i]= 0;
+    }
+
     for (to= stmt->param_array;
          to < stmt->param_array + stmt->param_count;
          ++to)
     {
       *to= param_iterator++;
     }
+
   }
   return FALSE;
 }
@@ -1902,6 +2511,7 @@ static bool init_param_array(Prepared_st
 
 void mysql_stmt_prepare(THD *thd, const char *packet, uint packet_length)
 {
+  Binary_RS_processor rsp;
   Prepared_statement *stmt;
   bool error;
   DBUG_ENTER("mysql_stmt_prepare");
@@ -1931,7 +2541,7 @@ void mysql_stmt_prepare(THD *thd, const 
   if (!(specialflag & SPECIAL_NO_PRIOR))
     my_pthread_setprio(pthread_self(),QUERY_PRIOR);
 
-  error= stmt->prepare(packet, packet_length);
+  error= stmt->prepare(&rsp, packet, packet_length);
 
   if (!(specialflag & SPECIAL_NO_PRIOR))
     my_pthread_setprio(pthread_self(),WAIT_PRIOR);
@@ -2067,6 +2677,7 @@ static void init_stmt_after_parse(LEX *l
 
 void mysql_sql_stmt_prepare(THD *thd)
 {
+  Text_RS_processor rsp;
   LEX *lex= thd->lex;
   LEX_STRING *name= &lex->prepared_stmt_name;
   Prepared_statement *stmt;
@@ -2105,7 +2716,7 @@ void mysql_sql_stmt_prepare(THD *thd)
     DBUG_VOID_RETURN;
   }
 
-  if (stmt->prepare(query, query_len))
+  if (stmt->prepare(&rsp, query, query_len))
   {
     /* Statement map deletes the statement on erase */
     thd->stmt_map.erase(stmt);
@@ -2267,8 +2878,6 @@ void mysql_stmt_execute(THD *thd, char *
   uchar *packet= (uchar*)packet_arg; // GCC 4.0.1 workaround
   ulong stmt_id= uint4korr(packet);
   ulong flags= (ulong) packet[4];
-  /* Query text for binary, general or slow log, if any of them is open */
-  String expanded_query;
 #ifndef EMBEDDED_LIBRARY
   uchar *packet_end= packet + packet_length;
 #endif
@@ -2293,24 +2902,6 @@ void mysql_stmt_execute(THD *thd, char *
   sp_cache_flush_obsolete(&thd->sp_proc_cache);
   sp_cache_flush_obsolete(&thd->sp_func_cache);
 
-#ifndef EMBEDDED_LIBRARY
-  if (stmt->param_count)
-  {
-    uchar *null_array= packet;
-    if (setup_conversion_functions(stmt, &packet, packet_end) ||
-        stmt->set_params(stmt, null_array, packet, packet_end,
-                         &expanded_query))
-      goto set_params_data_err;
-  }
-#else
-  /*
-    In embedded library we re-install conversion routines each time
-    we set params, and also we don't need to parse packet.
-    So we do it in one function.
-  */
-  if (stmt->param_count && stmt->set_params_data(stmt, &expanded_query))
-    goto set_params_data_err;
-#endif
   if (!(specialflag & SPECIAL_NO_PRIOR))
     my_pthread_setprio(pthread_self(),QUERY_PRIOR);
 
@@ -2321,16 +2912,18 @@ void mysql_stmt_execute(THD *thd, char *
   */
   DBUG_ASSERT(thd->free_list == NULL);
 
-  error= stmt->execute(&expanded_query,
-                       test(flags & (ulong) CURSOR_TYPE_READ_ONLY));
+#ifndef EMBEDDED_LIBRARY
+  Packet_parameter_setter setter(packet, packet_end);
+#else
+  Embedded_parameter_setter setter;
+#endif
+
+  bool open_cursor= test(flags & (ulong) CURSOR_TYPE_READ_ONLY);
+  error= Prepared_statement::execute_loop(thd, &stmt, &setter, open_cursor);
+
   if (!(specialflag & SPECIAL_NO_PRIOR))
     my_pthread_setprio(pthread_self(), WAIT_PRIOR);
   DBUG_VOID_RETURN;
-
-set_params_data_err:
-  my_error(ER_WRONG_ARGUMENTS, MYF(0), "mysql_stmt_execute");
-  reset_stmt_params(stmt);
-  DBUG_VOID_RETURN;
 }
 
 
@@ -2356,8 +2949,6 @@ void mysql_sql_stmt_execute(THD *thd)
   LEX *lex= thd->lex;
   Prepared_statement *stmt;
   LEX_STRING *name= &lex->prepared_stmt_name;
-  /* Query text for binary, general or slow log, if any of them is open */
-  String expanded_query;
   DBUG_ENTER("mysql_sql_stmt_execute");
   DBUG_PRINT("info", ("EXECUTE: %.*s\n", (int) name->length, name->str));
 
@@ -2383,18 +2974,10 @@ void mysql_sql_stmt_execute(THD *thd)
   */
   DBUG_ASSERT(thd->free_list == NULL);
 
-  if (stmt->set_params_from_vars(stmt, lex->prepared_stmt_params,
-                                 &expanded_query))
-    goto set_params_data_err;
-
-  (void) stmt->execute(&expanded_query, FALSE);
+  User_vars_parameter_setter setter(lex->prepared_stmt_params);
+  (void) Prepared_statement::execute_loop(thd, &stmt, &setter, FALSE);
 
   DBUG_VOID_RETURN;
-
-set_params_data_err:
-  my_error(ER_WRONG_ARGUMENTS, MYF(0), "EXECUTE");
-  reset_stmt_params(stmt);
-  DBUG_VOID_RETURN;
 }
 
 
@@ -2690,49 +3273,59 @@ Prepared_statement::Prepared_statement(T
   result(thd_arg),
   protocol(protocol_arg),
   param_array(0),
+  param_typecode_array(NULL),
   param_count(0),
   last_errno(0),
-  flags((uint) IS_IN_USE)
+  flags((uint) IS_IN_USE),
+  m_versions(),
+  m_column_count(0),
+  m_columns_metadata(NULL)
 {
   init_sql_alloc(&main_mem_root, thd_arg->variables.query_alloc_block_size,
                   thd_arg->variables.query_prealloc_size);
   *last_error= '\0';
 }
 
+Prepared_statement::Prepared_statement(Prepared_statement *model)
+  :Statement(&main_lex, &main_mem_root,
+             INITIALIZED, model->id),
+  thd(model->thd),
+  result(model->thd),
+  protocol(model->protocol),
+  param_array(0),
+  param_typecode_array(NULL),
+  param_count(0),
+  last_errno(0),
+  flags((uint) IS_IN_USE),
+  m_versions(),
+  m_column_count(0),
+  m_columns_metadata(NULL)
+{
+  init_sql_alloc(&main_mem_root, thd->variables.query_alloc_block_size,
+                  thd->variables.query_prealloc_size);
+  *last_error= '\0';
+}
 
-void Prepared_statement::setup_set_params()
+int
+Parameter_setter::set(Prepared_statement *stmt, String *expanded_query)
 {
-  /*
-    Note: BUG#25843 applies here too (query cache lookup uses thd->db, not
-    db from "prepare" time).
-  */
-  if (query_cache_maybe_disabled(thd)) // we won't expand the query
-    lex->safe_to_cache_query= FALSE;   // so don't cache it at Execution
+  LEX *lex= stmt->lex;
+  THD *thd= stmt->thd;
+
+  if (stmt->param_count == 0)
+    return 0;
 
   /*
     Decide if we have to expand the query (because we must write it to logs or
     because we want to look it up in the query cache) or not.
   */
-  if (mysql_bin_log.is_open() && is_update_query(lex->sql_command) ||
+  if ((mysql_bin_log.is_open() && is_update_query(lex->sql_command)) ||
       opt_log || opt_slow_log ||
-      query_cache_is_cacheable_query(lex))
-  {
-    set_params_from_vars= insert_params_from_vars_with_log;
-#ifndef EMBEDDED_LIBRARY
-    set_params= insert_params_with_log;
-#else
-    set_params_data= emb_insert_params_with_log;
-#endif
-  }
-  else
-  {
-    set_params_from_vars= insert_params_from_vars;
-#ifndef EMBEDDED_LIBRARY
-    set_params= insert_params;
-#else
-    set_params_data= emb_insert_params;
-#endif
-  }
+      (query_cache_is_cacheable_query(lex) &&
+       ! query_cache_maybe_disabled(thd)))
+    return set_with_log(stmt, expanded_query);
+
+  return set_no_log(stmt);
 }
 
 
@@ -2798,6 +3391,110 @@ bool Prepared_statement::set_name(LEX_ST
   global THD state management to the caller.
 ***************************************************************************/
 
+bool Prepared_statement::prepare(Result_set_processor *rsp,
+                                 const char *packet, uint packet_len)
+{
+  bool error;
+  DBUG_ENTER("Prepared_statement::prepare");
+
+  /*
+    If this is an SQLCOM_PREPARE, we also increase Com_prepare_sql.
+    However, it seems handy if com_stmt_prepare is increased always,
+    no matter what kind of prepare is processed.
+  */
+  status_var_increment(thd->status_var.com_stmt_prepare);
+
+  /* Remember the current database. */
+
+  if (thd->db && thd->db_length)
+  {
+    db= this->strmake(thd->db, thd->db_length);
+    db_length= thd->db_length;
+  }
+  else
+  {
+    db= NULL;
+    db_length= 0;
+  }
+
+  /* Remember the SQL_MODE */
+
+  m_sql_mode= thd->variables.sql_mode;
+
+  error= prep(rsp, packet, packet_len);
+
+  if (error == 0)
+  {
+    /*
+      Log COM_PREPARE to the general log. Note, that in case of SQL
+      prepared statements this causes two records to be output:
+
+      Query       PREPARE stmt from @user_variable
+      Prepare     <statement SQL text>
+
+      This is considered user-friendly, since in the
+      second log entry we output the actual statement text.
+
+      Do not print anything if this is an SQL prepared statement and
+      we're inside a stored procedure (also called Dynamic SQL) --
+      sub-statements inside stored procedures are not logged into
+      the general log.
+    */
+    if (thd->spcont == NULL)
+      general_log_write(thd, COM_STMT_PREPARE, query, query_length);
+  }
+
+  DBUG_RETURN(error);
+}
+
+bool Prepared_statement::reprepare(const char *packet, uint packet_len)
+{
+  Reprepare_RS_processor rsp;
+  bool error;
+  char saved_cur_db_name_buf[NAME_LEN+1];
+  LEX_STRING saved_cur_db_name=
+    { saved_cur_db_name_buf, sizeof(saved_cur_db_name_buf) };
+  bool cur_db_changed;
+  LEX_STRING stmt_db_name= { db, db_length };
+  ulong save_sql_mode;
+
+  DBUG_ENTER("Prepared_statement::reprepare");
+
+  status_var_increment(thd->status_var.com_stmt_reprepare);
+
+  /*
+    Save the current database,
+    set the database active during PREPARE as current.
+  */
+
+  if (mysql_opt_change_db(thd, &stmt_db_name, &saved_cur_db_name, TRUE,
+                          &cur_db_changed))
+    DBUG_RETURN(TRUE);
+
+  /*
+    Save the current SQL_MODE,
+    set the SQL_MODE active during PREPARE as current.
+  */
+  save_sql_mode= thd->variables.sql_mode;
+  thd->variables.sql_mode= m_sql_mode;
+
+  error= prep(& rsp, packet, packet_len);
+
+  /*
+    Restore the current database.
+  */
+
+  if (cur_db_changed)
+    mysql_change_db(thd, &saved_cur_db_name, TRUE);
+
+  /*
+    Restore the current SQL_MODE.
+  */
+  thd->variables.sql_mode= save_sql_mode;
+
+  DBUG_RETURN(error);
+}
+
 /**
   Parse statement text, validate the statement, and prepare it for execution.
 
@@ -2819,18 +3516,16 @@ bool Prepared_statement::set_name(LEX_ST
     thd->mem_root contains unused memory allocated during validation.
 */
 
-bool Prepared_statement::prepare(const char *packet, uint packet_len)
+bool Prepared_statement::prep(Result_set_processor *rsp,
+                              const char *packet, uint packet_len)
 {
   bool error;
   Statement stmt_backup;
   Query_arena *old_stmt_arena;
-  DBUG_ENTER("Prepared_statement::prepare");
-  /*
-    If this is an SQLCOM_PREPARE, we also increase Com_prepare_sql.
-    However, it seems handy if com_stmt_prepare is increased always,
-    no matter what kind of prepare is processed.
-  */
-  status_var_increment(thd->status_var.com_stmt_prepare);
+  DBUG_ENTER("Prepared_statement::prep");
+
+  if (m_versions.init(& main_mem_root))
+    DBUG_RETURN(TRUE);
 
   /*
     alloc_query() uses thd->memroot && thd->query, so we should call
@@ -2853,6 +3548,8 @@ bool Prepared_statement::prepare(const c
   lip.stmt_prepare_mode= TRUE;
   lex_start(thd);
 
+  DBUG_ASSERT(thd->lex == & main_lex);
+
   error= parse_sql(thd, &lip, NULL) ||
          thd->is_error() ||
          init_param_array(this);
@@ -2888,6 +3585,10 @@ bool Prepared_statement::prepare(const c
   */
   DBUG_ASSERT(thd->change_list.is_empty());
 
+  Prepare_observer observer(& m_versions);
+  Object_observer *save_observer= thd->m_object_observer;
+  thd->m_object_observer= & observer;
+
   /* 
    The only case where we should have items in the thd->free_list is
    after stmt->set_params_from_vars(), which may in some cases create
@@ -2895,7 +3596,9 @@ bool Prepared_statement::prepare(const c
   */
 
   if (error == 0)
-    error= check_prepared_statement(this, name.str != 0);
+    error= check_prepared_statement(this, rsp, name.str != 0);
+
+  thd->m_object_observer= save_observer;
 
   /*
     Currently CREATE PROCEDURE/TRIGGER/EVENT are prohibited in prepared
@@ -2916,30 +3619,63 @@ bool Prepared_statement::prepare(const c
 
   if (error == 0)
   {
-    setup_set_params();
     init_stmt_after_parse(lex);
     state= Query_arena::PREPARED;
     flags&= ~ (uint) IS_IN_USE;
+  }
+  DBUG_RETURN(error);
+}
 
-    /* 
-      Log COM_EXECUTE to the general log. Note, that in case of SQL
-      prepared statements this causes two records to be output:
+bool Prepared_statement::execute_loop(THD *thd,
+                                      Prepared_statement **stmt_ptr,
+                                      Parameter_setter *setter,
+                                      bool open_cursor)
+{
+  int count= 0;
+  Prepared_statement *stmt1= *stmt_ptr;
+#ifndef HAVE_RE_PREPARE
+  bool dummy;
+  return stmt1->execute(setter, open_cursor, & dummy);
+#else
+  bool need_reprepare;
+  bool error;
+  Prepared_statement *stmt2;
 
-      Query       PREPARE stmt from @user_variable
-      Prepare     <statement SQL text>
+  error= stmt1->execute(setter, open_cursor, & need_reprepare);
 
-      This is considered user-friendly, since in the
-      second log entry we output the actual statement text.
+  while (error && need_reprepare && thd->killed == THD::NOT_KILLED)
+  {
+    count++;
+    DBUG_ASSERT(count <= 100); // FIXME, debugging
 
-      Do not print anything if this is an SQL prepared statement and
-      we're inside a stored procedure (also called Dynamic SQL) --
-      sub-statements inside stored procedures are not logged into
-      the general log.
-    */
-    if (thd->spcont == NULL)
-      general_log_write(thd, COM_STMT_PREPARE, query, query_length);
+    thd->clear_error();
+    need_reprepare= FALSE;
+
+    stmt2= new Prepared_statement(stmt1);
+    if (stmt2)
+    {
+      error= stmt2->clone_state(stmt1);
+      if (! error)
+      {
+        error= stmt2->reprepare(stmt1->query, stmt1->query_length);
+        if (! error)
+        {
+          error= stmt2->execute(setter, open_cursor, & need_reprepare);
+          if (! error)
+          {
+            /* Destroys stmt1 */
+            thd->stmt_map.insert(thd, stmt2);
+            *stmt_ptr= stmt2;
+          }
+        }
+      }
+    }
+    else
+      error= TRUE;
   }
-  DBUG_RETURN(error);
+
+  return error;
+#endif
 }
 
 /**
@@ -2964,11 +3700,15 @@ bool Prepared_statement::prepare(const c
     TRUE		Error
 */
 
-bool Prepared_statement::execute(String *expanded_query, bool open_cursor)
+bool Prepared_statement::execute(Parameter_setter *setter, bool open_cursor,
+                                 bool *need_reprepare)
 {
   Statement stmt_backup;
   Query_arena *old_stmt_arena;
   bool error= TRUE;
+  /* Query text for binary, general or slow log, if any of them is open */
+  String expanded_query;
+  Reprepare_handler reprepare;
 
   char saved_cur_db_name_buf[NAME_LEN+1];
   LEX_STRING saved_cur_db_name=
@@ -2979,6 +3719,15 @@ bool Prepared_statement::execute(String 
 
   status_var_increment(thd->status_var.com_stmt_execute);
 
+  *need_reprepare= FALSE;
+
+  if (setter->set(this, &expanded_query))
+  {
+    my_error(ER_WRONG_ARGUMENTS, MYF(0), "EXECUTE");
+    reset_stmt_params(this);
+    goto error;
+  }
+
   /* Check if we got an error when sending long data */
   if (state == Query_arena::ERROR)
   {
@@ -3040,11 +3789,11 @@ bool Prepared_statement::execute(String 
 
   /* Allocate query. */
 
-  if (expanded_query->length() &&
-      alloc_query(thd, (char*) expanded_query->ptr(),
-                  expanded_query->length()))
+  if (expanded_query.length() &&
+      alloc_query(thd, (char*) expanded_query.ptr(),
+                  expanded_query.length()))
   {
-    my_error(ER_OUTOFMEMORY, 0, expanded_query->length());
+    my_error(ER_OUTOFMEMORY, 0, expanded_query.length());
     goto error;
   }
   /*
@@ -3070,11 +3819,16 @@ bool Prepared_statement::execute(String 
 
   /* Go! */
 
+  DBUG_ASSERT(thd->lex == & main_lex);
+
   if (open_cursor)
     error= mysql_open_cursor(thd, (uint) ALWAYS_MATERIALIZED_CURSOR,
                              &result, &cursor);
   else
   {
+    /* Assumption in query_cache_send_result_to_client() */
+    DBUG_ASSERT(thd->lex == this->lex);
+
     /*
       Try to find it in the query cache, if not, execute it.
       Note that multi-statements cannot exist here (they are not supported in
@@ -3083,7 +3837,16 @@ bool Prepared_statement::execute(String 
     if (query_cache_send_result_to_client(thd, thd->query,
                                           thd->query_length) <= 0)
     {
+      Execute_observer observer(& m_versions);
+      Object_observer *save_observer= thd->m_object_observer;
+      thd->m_object_observer= & observer;
+      thd->push_internal_handler(& reprepare);
+
       error= mysql_execute_command(thd);
+
+      thd->pop_internal_handler();
+      thd->m_object_observer= save_observer;
+      *need_reprepare= reprepare.need_reprepare();
     }
   }
 
@@ -3135,6 +3898,9 @@ bool Prepared_statement::execute(String 
 
 error:
   flags&= ~ (uint) IS_IN_USE;
+
+  /* If we need re prepare, an error should be returned. */
+  DBUG_ASSERT(! *need_reprepare || error);
   return error;
 }
 
@@ -3154,3 +3920,244 @@ bool Prepared_statement::deallocate()
   thd->stmt_map.erase(this);
   return FALSE;
 }
+
+bool Prepared_statement::set_column_count(uint count)
+{
+  DBUG_ASSERT(m_column_count == 0);
+  DBUG_ASSERT(m_columns_metadata == NULL);
+
+  m_column_count= count;
+  if (count)
+  {
+    size_t size= m_column_count * sizeof(Send_field);
+    m_columns_metadata= (Send_field *)
+                        alloc_root(&main_mem_root, size);
+
+    if (m_columns_metadata == NULL)
+      return TRUE;
+
+    memset(m_columns_metadata, 0, size);
+  }
+
+  return FALSE;
+}
+
+bool Prepared_statement::set_column_metadata(uint col, Item* item)
+{
+  DBUG_ASSERT(col < m_column_count);
+
+  Send_field *column= &m_columns_metadata[col];
+
+  Send_field dummy;
+  item->make_field(& dummy);
+
+  return clone_column_metadata(column, &dummy);
+}
+
+bool Prepared_statement::clone_state(Prepared_statement *model)
+{
+  /* Copy the name */
+
+  if (model->name.str)
+  {
+    name.str= (char*) memdup_root(&main_mem_root,
+                                  model->name.str,
+                                  model->name.length);
+    if (name.str == NULL)
+      return TRUE;
+    name.length= model->name.length;
+  }
+
+  /* Copy the current database */
+
+  db_length= model->db_length;
+  if (db_length)
+  {
+    db= (char*) memdup_root(&main_mem_root, model->db, db_length);
+    if (db == NULL)
+      return TRUE;
+  }
+  else
+    db= NULL;
+
+  /* Copy the SQL_MODE */
+
+  m_sql_mode= model->m_sql_mode;
+
+  /* Copy the parameters typecode */
+
+  param_count= model->param_count;
+
+  if (param_count)
+  {
+    uint i;
+    param_typecode_array= (ushort*) alloc_root(&main_mem_root,
+                                               sizeof(ushort) * param_count);
+    if (!param_typecode_array)
+      return TRUE;
+
+    for (i= 0; i < param_count; i++)
+      param_typecode_array[i]= model->param_typecode_array[i];
+  }
+
+  /* Copy the result set metadata */
+
+  m_column_count= model->m_column_count;
+
+  if (m_column_count)
+  {
+    uint col;
+    size_t size= m_column_count * sizeof(Send_field);
+    m_columns_metadata= (Send_field *)
+                        alloc_root(&main_mem_root, size);
+
+    if (m_columns_metadata == NULL)
+      return TRUE;
+
+    for (col=0; col < m_column_count; col++)
+    {
+      if (clone_column_metadata(& m_columns_metadata[col],
+                                & model->m_columns_metadata[col]))
+        return TRUE;
+    }
+  }
+
+  return FALSE;
+}
+
+bool Prepared_statement::clone_column_metadata(Send_field *col,
+                                               Send_field *model)
+{
+  col->db_name= strdup_root(&main_mem_root, model->db_name);
+  col->table_name= strdup_root(&main_mem_root, model->table_name);
+  col->org_table_name= strdup_root(&main_mem_root, model->org_table_name);
+  col->col_name= strdup_root(&main_mem_root, model->col_name);
+  col->org_col_name= strdup_root(&main_mem_root, model->org_col_name);
+  col->length= model->length;
+  col->charsetnr= model->charsetnr;
+  col->flags= model->flags;
+  col->decimals= model->decimals;
+  col->type= model->type;
+
+  return FALSE;
+}
+
+bool Prepare_RS_processor::record_metadata(Prepared_statement *stmt,
+                                           List<Item> *fields)
+{
+  if (fields == NULL)
+    return stmt->set_column_count(0);
+
+  uint col;
+  uint columns= fields->elements;
+  List_iterator<Item> iter(*fields);
+
+  if (stmt->set_column_count(columns))
+    return TRUE;
+
+  for (col= 0; col < columns; col++)
+  {
+    if (stmt->set_column_metadata(col, iter++))
+      return TRUE;
+  }
+
+  return FALSE;
+}
+
+bool Text_RS_processor::process(Prepared_statement *stmt,
+                                select_result *result, List<Item> *fields)
+{
+  return record_metadata(stmt, fields);
+}
+
+bool Binary_RS_processor::process(Prepared_statement *stmt,
+                                  select_result *result, List<Item> *fields)
+{
+  if (record_metadata(stmt, fields))
+    return TRUE;
+
+  if (result && fields)
+  {
+    uint columns= result->field_count(*fields);
+    return (send_prep_stmt(stmt, columns) ||
+            result->send_fields(*fields, Protocol::SEND_EOF) ||
+            stmt->thd->protocol->flush());
+  }
+
+  return (send_prep_stmt(stmt, 0) ||
+          stmt->thd->protocol->flush());
+}
+
+bool Prepared_statement::check_column_count(uint count)
+{
+  if (m_column_count != count)
+  {
+    my_error(ER_PS_REBIND, MYF(0));
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+bool Prepared_statement::check_column_metadata(uint col, Item* item)
+{
+  const uint FLAGS_TO_CHECK= NOT_NULL_FLAG |
+    BLOB_FLAG |
+    UNSIGNED_FLAG |
+    ZEROFILL_FLAG |
+    BINARY_FLAG |
+    ENUM_FLAG |
+    TIMESTAMP_FLAG |
+    SET_FLAG |
+    NUM_FLAG
+    ;
+
+  DBUG_ASSERT(col < m_column_count);
+
+  Send_field *column= &m_columns_metadata[col];
+
+  Send_field dummy;
+  item->make_field(& dummy);
+
+  if ((strcmp(column->db_name, dummy.db_name) != 0) ||
+      (strcmp(column->table_name, dummy.table_name) != 0) ||
+      /* (strcmp(column->org_table_name, dummy.org_table_name) != 0) || */
+      (strcmp(column->col_name, dummy.col_name) != 0) ||
+      /* (strcmp(column->org_col_name, dummy.org_col_name) != 0) || */
+      (column->length != dummy.length) ||
+      (column->charsetnr != dummy.charsetnr) ||
+      ((column->flags & FLAGS_TO_CHECK) != (dummy.flags & FLAGS_TO_CHECK)) ||
+      (column->decimals != dummy.decimals) ||
+      (column->type != dummy.type))
+  {
+    my_error(ER_PS_REBIND, MYF(0));
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+bool Reprepare_RS_processor::process(Prepared_statement *stmt,
+                                     select_result *result, List<Item> *fields)
+{
+#ifdef HAVE_REBIND
+  if (fields == NULL)
+    return stmt->check_column_count(0);
+
+  uint col;
+  uint columns= fields->elements;
+  List_iterator<Item> iter(*fields);
+
+  if (stmt->check_column_count(columns))
+    return TRUE;
+
+  for (col= 0; col < columns; col++)
+  {
+    if (stmt->check_column_metadata(col, iter++))
+      return TRUE;
+  }
+#endif
+
+  return FALSE;
+}
+
diff -Nrup a/sql/sql_table.cc b/sql/sql_table.cc
--- a/sql/sql_table.cc	2008-02-19 05:58:01 -07:00
+++ b/sql/sql_table.cc	2008-02-20 20:55:31 -07:00
@@ -6508,12 +6508,15 @@ view_err:
   {
     if (table->s->tmp_table)
     {
+      bool refresh_not_used;
+      bool invalidated_not_used;
       TABLE_LIST tbl;
       bzero((void*) &tbl, sizeof(tbl));
       tbl.db= new_db;
       tbl.table_name= tbl.alias= tmp_name;
       /* Table is in thd->temporary_tables */
-      new_table= open_table(thd, &tbl, thd->mem_root, (bool*) 0,
+      new_table= open_table(thd, &tbl, thd->mem_root,
+                            &refresh_not_used, &invalidated_not_used,
                             MYSQL_LOCK_IGNORE_FLUSH);
     }
     else
diff -Nrup a/sql/sql_view.cc b/sql/sql_view.cc
--- a/sql/sql_view.cc	2008-02-20 13:30:10 -07:00
+++ b/sql/sql_view.cc	2008-02-20 20:55:31 -07:00
@@ -178,7 +178,8 @@ static bool
 fill_defined_view_parts (THD *thd, TABLE_LIST *view)
 {
   LEX *lex= thd->lex;
-  bool not_used;
+  bool refresh_not_used;
+  bool invalidated_not_used;
   TABLE_LIST decoy;
 
   memcpy (&decoy, view, sizeof (TABLE_LIST));
@@ -203,7 +204,8 @@ fill_defined_view_parts (THD *thd, TABLE
     since the return value itself does not mean anything.
   */
 
-  open_table(thd, &decoy, thd->mem_root, &not_used, OPEN_VIEW_NO_PARSE);
+  open_table(thd, &decoy, thd->mem_root,
+             &refresh_not_used, &invalidated_not_used, OPEN_VIEW_NO_PARSE);
 
   if (!decoy.view)
   {
@@ -697,6 +699,19 @@ static File_option view_parameters[]=
   FILE_OPTIONS_STRING}
 };
 
+/* Short parsing, only to read MD5 */
+static File_option view_md5_parameters[]=
+{{{ C_STRING_WITH_LEN("query")},
+  my_offsetof(TABLE_LIST, select_stmt),
+  FILE_OPTIONS_ESTRING},
+ {{ C_STRING_WITH_LEN("md5")},
+  my_offsetof(TABLE_LIST, md5),
+  FILE_OPTIONS_STRING},
+ {{NullS, 0},			0,
+  FILE_OPTIONS_STRING}
+};
+
+
 static LEX_STRING view_file_type[]= {{(char*) STRING_WITH_LEN("VIEW") }};
 
 
@@ -963,6 +978,7 @@ bool mysql_make_view(THD *thd, File_pars
     {
       DBUG_RETURN(1);
     }
+
     DBUG_PRINT("info",
                ("VIEW %s.%s is already processed on previous PS/SP execution",
                 table->view_db.str, table->view_name.str));
@@ -1020,6 +1036,26 @@ bool mysql_make_view(THD *thd, File_pars
                     required_view_parameters, &file_parser_dummy_hook))
     goto err;
 
+
+  if (thd->m_object_observer && ! thd->in_sub_stmt)
+  {
+    Object_version version;
+
+    if (version.m_oid.set_table(table->db, table->table_name))
+    {
+      result= TRUE;
+      goto end;
+    }
+    version.m_vid.set_view(table->md5.str);
+
+    if (thd->m_object_observer->notify(thd, & version))
+    {
+      result= TRUE;
+      goto end;
+    }
+  }
+
+
   /*
     check old format view .frm
   */
@@ -1459,6 +1495,44 @@ err:
   table->view= 0;	// now it is not VIEW placeholder
   result= 1;
   goto end;
+}
+
+int mysql_load_view_md5(THD *thd, TABLE_LIST *view)
+{
+  int rc;
+  LEX_STRING pathstr;
+  File_parser *parser;
+  char path_buff[FN_REFLEN];
+  TABLE_LIST dummy;
+
+  DBUG_ENTER("mysql_load_view_md5");
+  DBUG_PRINT("info", ("view: (%s.%s)", view->db, view->table_name));
+
+  bzero(& dummy, sizeof(dummy));
+
+  pathstr.str= (char *) path_buff;
+  pathstr.length= build_table_filename(path_buff, sizeof(path_buff) - 1,
+                                       view->db, view->table_name,
+                                       reg_ext, 0);
+
+  parser= sql_parse_prepare(&pathstr, thd->mem_root, 0);
+  if (parser &&
+      parser->ok() &&
+      is_equal(&view_type, parser->type()) &&
+      parser->parse((uchar*) &dummy,
+                    thd->mem_root,
+                    view_md5_parameters,
+                    array_elements(view_md5_parameters)-1,
+                    &file_parser_dummy_hook) == 0)
+  {
+    view->md5.str= dummy.md5.str;
+    view->md5.length= dummy.md5.length;
+    rc= 0;
+  }
+  else
+    rc= 1;
+
+  DBUG_RETURN(rc);
 }
 
 
diff -Nrup a/sql/sql_view.h b/sql/sql_view.h
--- a/sql/sql_view.h	2006-12-23 12:19:56 -07:00
+++ b/sql/sql_view.h	2008-02-20 20:55:31 -07:00
@@ -31,6 +31,8 @@ frm_type_enum mysql_frm_type(THD *thd, c
 
 int view_checksum(THD *thd, TABLE_LIST *view);
 
+int mysql_load_view_md5(THD *thd, TABLE_LIST *view);
+
 extern TYPELIB updatable_views_with_limit_typelib;
 
 bool check_duplicate_names(List<Item>& item_list, bool gen_unique_view_names);
diff -Nrup a/sql/table.cc b/sql/table.cc
--- a/sql/table.cc	2007-12-17 06:28:05 -07:00
+++ b/sql/table.cc	2008-02-20 20:55:32 -07:00
@@ -250,6 +250,41 @@ TABLE_CATEGORY get_table_category(const 
   return TABLE_CATEGORY_USER;
 }
 
+int TABLE_SHARE::make_object_version(Object_version *version)
+{
+  int rc;
+
+  rc= version->m_oid.set_table(& db, & table_name);
+
+  if ((tmp_table == INTERNAL_TMP_TABLE) ||
+      (tmp_table == NON_TRANSACTIONAL_TMP_TABLE))
+  {
+    /*
+      table_map_id is THD::query_id.
+      The version number is not persistent,
+      but the table is not persistent either,
+      so using table_map_id is ok here.
+    */
+    version->m_vid.set_temporary_table(table_map_id);
+  }
+  else
+  {
+    version->m_vid.set_table(table_map_id);
+  }
+
+  return rc;
+}
+
+int TABLE_SHARE::make_object_null_version(
+  Object_version * version, const char* db, const char* name)
+{
+  int rc;
+
+  rc= version->m_oid.set_table(db, name);
+  version->m_vid.set_null();
+
+  return rc;
+}
 
 /*
   Allocate a setup TABLE_SHARE structure
@@ -2934,6 +2969,17 @@ void st_table::reset_item_list(List<Item
     DBUG_ASSERT(item_field != 0);
     item_field->reset_field(*ptr);
   }
+}
+
+int TABLE_LIST::make_object_version(Object_version *version)
+{
+  int rc;
+
+  DBUG_ASSERT(view);
+  rc= version->m_oid.set_table(& view_db, & view_name);
+  version->m_vid.set_view(md5.str);
+
+  return rc;
 }
 
 /*
diff -Nrup a/sql/table.h b/sql/table.h
--- a/sql/table.h	2007-12-20 13:24:07 -07:00
+++ b/sql/table.h	2008-02-20 20:55:32 -07:00
@@ -24,6 +24,7 @@ class st_select_lex;
 class partition_info;
 class COND_EQUAL;
 class Security_context;
+class Object_version;
 
 /*************************************************************************/
 
@@ -437,6 +438,11 @@ typedef struct st_table_share
     return table_map_id;
   }
 
+  int make_object_version(Object_version *version);
+  static int make_object_null_version(
+    Object_version * version, const char* db, const char* name);
+
+  int make_object_version(Version_id *version);
 } TABLE_SHARE;
 
 
@@ -893,6 +899,8 @@ class Index_hint;
 struct TABLE_LIST
 {
   TABLE_LIST() {}                          /* Remove gcc warning */
+
+  int make_object_version(Object_version *version);
 
   /**
     Prepare TABLE_LIST that consists of one table instance to use in
diff -Nrup a/tests/mysql_client_test.c b/tests/mysql_client_test.c
--- a/tests/mysql_client_test.c	2008-01-10 18:04:30 -07:00
+++ b/tests/mysql_client_test.c	2008-02-20 20:55:32 -07:00
@@ -2475,6 +2475,7 @@ static void test_ps_query_cache()
 
   for (iteration= TEST_QCACHE_ON; iteration <= TEST_QCACHE_ON_OFF; iteration++)
   {
+    fprintf(stdout, "\n Iteration %d", iteration);
 
     switch (iteration)
     {
@@ -2567,10 +2568,10 @@ static void test_ps_query_cache()
     hits2= query_cache_hits(mysql);
     switch(iteration) {
     case TEST_QCACHE_ON_WITH_OTHER_CONN:
+    case TEST_QCACHE_OFF_ON:
     case TEST_QCACHE_ON:                 /* should have hit */
       DIE_UNLESS(hits2-hits1 == 1);
       break;
-    case TEST_QCACHE_OFF_ON:
     case TEST_QCACHE_ON_OFF:             /* should not have hit */
       DIE_UNLESS(hits2-hits1 == 0);
       break;
@@ -2604,16 +2605,14 @@ static void test_ps_query_cache()
     mysql_stmt_close(stmt);
 
     switch(iteration) {
+    case TEST_QCACHE_OFF_ON:
+    case TEST_QCACHE_ON_WITH_OTHER_CONN:
     case TEST_QCACHE_ON:                 /* should have hit */
       DIE_UNLESS(hits2-hits1 == 1);
       break;
-    case TEST_QCACHE_OFF_ON:
     case TEST_QCACHE_ON_OFF:             /* should not have hit */
       DIE_UNLESS(hits2-hits1 == 0);
       break;
-    case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
-      DIE_UNLESS(hits2-hits1 == 1);
-      break;
     }
 
   } /* for(iteration=...) */
@@ -17231,6 +17230,123 @@ static void test_bug31669()
   DBUG_VOID_RETURN;
 }
 
+static void test_wl4166()
+{
+  MYSQL_STMT *stmt;
+  int        int_data;
+  char       str_data[50];
+  char       tiny_data;
+  short      small_data;
+  longlong   big_data;
+  float      real_data;
+  double     double_data;
+  ulong      length[7];
+  my_bool    is_null[7];
+  MYSQL_BIND my_bind[7];
+  int rc;
+  int i;
+
+  myheader("test_wl4166");
+
+  rc= mysql_query(mysql, "DROP TABLE IF EXISTS table_4166");
+  myquery(rc);
+
+  rc= mysql_query(mysql, "CREATE TABLE table_4166(col1 tinyint NOT NULL, "
+                         "col2 varchar(15), col3 int, "
+                         "col4 smallint, col5 bigint, "
+                         "col6 float, col7 double, "
+                         "colX varchar(10) default NULL)");
+  myquery(rc);
+
+  stmt= mysql_simple_prepare(mysql,
+    "INSERT INTO table_4166(col1, col2, col3, col4, col5, col6, col7) "
+    "VALUES(?, ?, ?, ?, ?, ?, ?)");
+  check_stmt(stmt);
+
+  verify_param_count(stmt, 7);
+
+  /* tinyint */
+  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
+  my_bind[0].buffer= (void *)&tiny_data;
+  /* string */
+  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
+  my_bind[1].buffer= (void *)str_data;
+  my_bind[1].buffer_length= 1000;                  /* Max string length */
+  /* integer */
+  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
+  my_bind[2].buffer= (void *)&int_data;
+  /* short */
+  my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
+  my_bind[3].buffer= (void *)&small_data;
+  /* bigint */
+  my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
+  my_bind[4].buffer= (void *)&big_data;
+  /* float */
+  my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
+  my_bind[5].buffer= (void *)&real_data;
+  /* double */
+  my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
+  my_bind[6].buffer= (void *)&double_data;
+
+  for (i= 0; i < (int) array_elements(my_bind); i++)
+  {
+    my_bind[i].length= &length[i];
+    my_bind[i].is_null= &is_null[i];
+    is_null[i]= 0;
+  }
+
+  rc= mysql_stmt_bind_param(stmt, my_bind);
+  check_execute(stmt, rc);
+
+  int_data= 320;
+  small_data= 1867;
+  big_data= 1000;
+  real_data= 2;
+  double_data= 6578.001;
+
+  /* now, execute the prepared statement to insert 10 records.. */
+  for (tiny_data= 0; tiny_data < 10; tiny_data++)
+  {
+    length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
+    rc= mysql_stmt_execute(stmt);
+    check_execute(stmt, rc);
+    int_data += 25;
+    small_data += 10;
+    big_data += 100;
+    real_data += 1;
+    double_data += 10.09;
+  }
+
+  /* force a re-prepare with some DDL */
+
+  rc= mysql_query(mysql,
+    "ALTER TABLE table_4166 change colX colX varchar(20) default NULL");
+  myquery(rc);
+
+  /*
+    execute the prepared statement again,
+    without changing the types of parameters already bound.
+  */
+
+  for (tiny_data= 50; tiny_data < 60; tiny_data++)
+  {
+    length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
+    rc= mysql_stmt_execute(stmt);
+    check_execute(stmt, rc);
+    int_data += 25;
+    small_data += 10;
+    big_data += 100;
+    real_data += 1;
+    double_data += 10.09;
+  }
+
+  mysql_stmt_close(stmt);
+
+  rc= mysql_query(mysql, "DROP TABLE table_4166");
+  myquery(rc);
+
+}
+
 /*
   Read and parse arguments and MySQL options from my.cnf
 */
@@ -17535,6 +17651,7 @@ static struct my_tests_st my_tests[]= {
   { "test_bug20023", test_bug20023 },
   { "test_bug31418", test_bug31418 },
   { "test_bug31669", test_bug31669 },
+  { "test_wl4166", test_wl4166 },
   { 0, 0 }
 };
 
Thread
bk commit into 5.1 tree (malff:1.2563) BUG#12093marc.alff21 Feb