MySQL Lists are EOL. Please join:

List:Internals« Previous MessageNext Message »
From:konstantin Date:June 8 2005 6:26pm
Subject:bk commit into 5.0 tree (konstantin:1.1957) BUG#10729
View as plain text  
Below is the list of changes that have just been committed into a local
5.0 repository of kostja. When kostja 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
  1.1957 05/06/08 22:26:15 konstantin@stripped +4 -0
  A fix and test case for Bug#10729 "mysql_stmt_attr_set 
  CURSOR_TYPE_READ_ONLY". The bug was that we (me) don't perform proper
  cleanups of the prepared statement when done fetching from a cursor.

  tests/mysql_client_test.c
    1.122 05/06/08 22:26:09 konstantin@stripped +63 -0
    Add a test case for Bug#10729 "mysql_stmt_attr_set CURSOR_TYPE_READ_ONLY"
    (problem reusing a prepared statemnt if there was a cursor)

  sql/sql_prepare.cc
    1.120 05/06/08 22:26:09 konstantin@stripped +25 -15
    Move common cleanup code to a cleanup function, call it when we close
    a cursor.

  sql/sp_head.cc
    1.145 05/06/08 22:26:09 konstantin@stripped +1 -1
    Rename.

  sql/mysql_priv.h
    1.311 05/06/08 22:26:09 konstantin@stripped +1 -1
    Rename reset_stmt_for_execute to init_stmt_before_use (to correspond to
    cleanup_stmt_after_use).

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	konstantin
# Host:	dragonfly.local
# Root:	/opt/local/work/mysql-5.0-10729

--- 1.310/sql/mysql_priv.h	2005-06-08 00:34:45 +04:00
+++ 1.311/sql/mysql_priv.h	2005-06-08 22:26:09 +04:00
@@ -833,7 +833,7 @@
 void mysql_stmt_free(THD *thd, char *packet);
 void mysql_stmt_reset(THD *thd, char *packet);
 void mysql_stmt_get_longdata(THD *thd, char *pos, ulong packet_length);
-void reset_stmt_for_execute(THD *thd, LEX *lex);
+void init_stmt_before_use(THD *thd, LEX *lex);
 void init_stmt_after_parse(THD*, LEX*);
 
 /* sql_handler.cc */

--- 1.144/sql/sp_head.cc	2005-06-07 14:13:53 +04:00
+++ 1.145/sql/sp_head.cc	2005-06-08 22:26:09 +04:00
@@ -1355,7 +1355,7 @@
            implemented at the same time as ability not to store LEX for
            instruction if it is not really used.
   */
-  reset_stmt_for_execute(thd, m_lex);
+  init_stmt_before_use(thd, m_lex);
 
   /*
     If requested check whenever we have access to tables in LEX's table list

--- 1.119/sql/sql_prepare.cc	2005-06-08 19:57:22 +04:00
+++ 1.120/sql/sql_prepare.cc	2005-06-08 22:26:09 +04:00
@@ -1658,6 +1658,18 @@
   return FALSE;
 }
 
+
+/* Cleanup PS after execute/prepare and restore THD state */
+
+static void cleanup_stmt_after_use(Statement *stmt, THD *thd)
+{
+  stmt->lex->unit.cleanup();
+  close_thread_tables(thd);
+  cleanup_items(stmt->free_list);
+  thd->rollback_item_tree_changes();
+  thd->cleanup_after_query();
+}
+
 /*
   Given a query string with parameter markers, create a Prepared Statement
   from it and send PS info back to the client.
@@ -1762,12 +1774,8 @@
     thd->lex->sphead= NULL;
   }
   lex_end(lex);
-  lex->unit.cleanup();
-  close_thread_tables(thd);
+  cleanup_stmt_after_use(stmt, thd);
   thd->restore_backup_statement(stmt, &thd->stmt_backup);
-  cleanup_items(stmt->free_list);
-  thd->rollback_item_tree_changes();
-  thd->cleanup_after_query();
   thd->current_arena= thd;
 
   if (error)
@@ -1810,10 +1818,10 @@
 
 /* Reinit prepared statement/stored procedure before execution */
 
-void reset_stmt_for_execute(THD *thd, LEX *lex)
+void init_stmt_before_use(THD *thd, LEX *lex)
 {
   SELECT_LEX *sl= lex->all_selects_list;
-  DBUG_ENTER("reset_stmt_for_execute");
+  DBUG_ENTER("init_stmt_before_use");
 
   if (lex->empty_field_list_on_rset)
   {
@@ -2009,7 +2017,7 @@
   thd->stmt_backup.set_statement(thd);
   thd->set_statement(stmt);
   thd->current_arena= stmt;
-  reset_stmt_for_execute(thd, stmt->lex);
+  init_stmt_before_use(thd, stmt->lex);
   /* From now cursors assume that thd->mem_root is clean */
   if (expanded_query.length() &&
       alloc_query(thd, (char *)expanded_query.ptr(),
@@ -2038,12 +2046,8 @@
   }
   else
   {
-    thd->lex->unit.cleanup();
-    cleanup_items(stmt->free_list);
+    cleanup_stmt_after_use(stmt, thd);
     reset_stmt_params(stmt);
-    close_thread_tables(thd);                   /* to close derived tables */
-    thd->rollback_item_tree_changes();
-    thd->cleanup_after_query();
   }
 
   thd->set_statement(&thd->stmt_backup);
@@ -2121,7 +2125,7 @@
 {
   DBUG_ENTER("execute_stmt");
 
-  reset_stmt_for_execute(thd, stmt->lex);
+  init_stmt_before_use(thd, stmt->lex);
 
   if (expanded_query->length() &&
       alloc_query(thd, (char *)expanded_query->ptr(),
@@ -2173,7 +2177,7 @@
   /* assume there is always place for 8-16 bytes */
   ulong stmt_id= uint4korr(packet);
   ulong num_rows= uint4korr(packet+4);
-  Statement *stmt;
+  Prepared_statement *stmt;
   DBUG_ENTER("mysql_stmt_fetch");
 
   if (!(stmt= find_prepared_statement(thd, stmt_id, "mysql_stmt_fetch")))
@@ -2203,6 +2207,12 @@
   stmt->cursor->reset_thd(thd);
   thd->restore_backup_statement(stmt, &thd->stmt_backup);
   thd->current_arena= thd;
+
+  if (!stmt->cursor->is_open())      /* Fetch is done, cleanup the PS */
+  {
+    cleanup_stmt_after_use(stmt, thd);
+    reset_stmt_params(stmt);
+  }
 
   DBUG_VOID_RETURN;
 }

--- 1.121/tests/mysql_client_test.c	2005-06-01 20:33:06 +04:00
+++ 1.122/tests/mysql_client_test.c	2005-06-08 22:26:09 +04:00
@@ -13146,6 +13146,68 @@
 }
 
 /*
+  Check that proper cleanups are done for prepared statement when
+  fetching thorugh a cursor.
+*/
+
+static void test_bug10729()
+{
+  MYSQL_STMT *stmt;
+  MYSQL_BIND bind[1];
+  char a[21];
+  int rc;
+  const char *stmt_text;
+  int i= 0;
+  char *name_array[3]= { "aaa", "bbb", "ccc" };
+  ulong type;
+
+  myheader("test_bug10729");
+
+  mysql_query(mysql, "drop table if exists t1");
+  mysql_query(mysql, "create table t1 (id integer not null primary key,"
+                                      "name VARCHAR(20) NOT NULL)");
+  rc= mysql_query(mysql, "insert into t1 (id, name) values "
+                         "(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
+  myquery(rc);
+
+  stmt= mysql_stmt_init(mysql);
+  /* Not implemented in 5.0 */
+
+  type= (ulong) CURSOR_TYPE_READ_ONLY;
+  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
+  check_execute(stmt, rc);
+  stmt_text= "select name from t1";
+  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
+  check_execute(stmt, rc);
+
+  bzero(bind, sizeof(bind));
+  bind[0].buffer_type= MYSQL_TYPE_STRING;
+  bind[0].buffer= (void*) a;
+  bind[0].buffer_length= sizeof(a);
+  mysql_stmt_bind_result(stmt, bind);
+
+  for (i= 0; i < 3; i++)
+  {
+    int row_no= 0;
+    rc= mysql_stmt_execute(stmt);
+    check_execute(stmt, rc);
+    while ((rc= mysql_stmt_fetch(stmt)) == 0)
+    {
+      DIE_UNLESS(strcmp(a, name_array[row_no]) == 0);
+      if (!opt_silent)
+        printf("%d: %s\n", row_no, a);
+      ++row_no;
+    }
+    DIE_UNLESS(rc == MYSQL_NO_DATA);
+  }
+  rc= mysql_stmt_close(stmt);
+  DIE_UNLESS(rc == 0);
+
+  rc= mysql_query(mysql, "drop table t1");
+  myquery(rc);
+}
+
+/*
   Read and parse arguments and MySQL options from my.cnf
 */
 
@@ -13377,6 +13439,7 @@
   { "test_bug9520", test_bug9520 },
   { "test_bug9478", test_bug9478 },
   { "test_bug9643", test_bug9643 },
+  { "test_bug10729", test_bug10729 },
   { 0, 0 }
 };
 
Thread
bk commit into 5.0 tree (konstantin:1.1957) BUG#10729konstantin8 Jun