From 97a5fe8e651eb0f2d089731ed7d55ab366aaf5ec Mon Sep 17 00:00:00 2001
From: "konstantin@mysql.com" <>
Date: Fri, 5 Nov 2004 22:39:19 +0300
Subject: [PATCH] Fix the client_test failure (due to failing cursors tests
 after the merge).

---
 sql/sql_prepare.cc | 16 ++++-------
 sql/sql_select.cc  | 70 ++++++++++++++++++----------------------------
 2 files changed, 33 insertions(+), 53 deletions(-)

diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index ed8a28b8b0..bd7f70c588 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -1898,7 +1898,7 @@ void mysql_stmt_execute(THD *thd, char *packet, uint packet_length)
   {
     if (stmt->cursor->is_open())
       stmt->cursor->init_from_thd(thd);
-    thd->set_item_arena(&thd->stmt_backup);
+    stmt->cursor->state= stmt->state;
   }
   else
   {
@@ -2044,6 +2044,7 @@ void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length)
 
   DBUG_ENTER("mysql_stmt_fetch");
 
+  thd->current_arena= stmt;
   if (!(stmt= thd->stmt_map.find(stmt_id)) ||
       !stmt->cursor ||
       !stmt->cursor->is_open())
@@ -2053,9 +2054,7 @@ void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length)
     DBUG_VOID_RETURN;
   }
 
-  thd->stmt_backup.set_statement(thd);
-  thd->stmt_backup.set_item_arena(thd);
-  thd->set_statement(stmt);
+  thd->set_n_backup_statement(stmt, &thd->stmt_backup);
   stmt->cursor->init_thd(thd);
 
   if (!(specialflag & SPECIAL_NO_PRIOR))
@@ -2070,11 +2069,8 @@ void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length)
 
   /* Restore THD state */
   stmt->cursor->reset_thd(thd);
-  thd->set_statement(&thd->stmt_backup);
-  thd->set_item_arena(&thd->stmt_backup);
-
-  if (error && error != -4)
-    send_error(thd, ER_OUT_OF_RESOURCES);
+  thd->restore_backup_statement(stmt, &thd->stmt_backup);
+  thd->current_arena= thd;
 
   DBUG_VOID_RETURN;
 }
@@ -2085,7 +2081,7 @@ void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length)
   SYNOPSIS
     mysql_stmt_reset()
       thd       Thread handle
-      packet	Packet with stmt id 
+      packet	Packet with stmt id
 
   DESCRIPTION
     This function resets statement to the state it was right after prepare.
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 2e1429feb3..18ffb7079c 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -1683,8 +1683,6 @@ Cursor::init_from_thd(THD *thd)
 void
 Cursor::init_thd(THD *thd)
 {
-  thd->mem_root= mem_root;
-
   DBUG_ASSERT(thd->derived_tables == 0);
   thd->derived_tables= derived_tables;
 
@@ -1694,7 +1692,6 @@ Cursor::init_thd(THD *thd)
   DBUG_ASSERT(thd->lock== 0);
   thd->lock= lock;
   thd->query_id= query_id;
-  thd->free_list= free_list;
 }
 
 
@@ -1779,6 +1776,8 @@ Cursor::fetch(ulong num_rows)
 
   int error= 0;
 
+  /* save references to memory, allocated during fetch */
+  thd->set_n_backup_item_arena(this, &thd->stmt_backup);
   join->fetch_limit+= num_rows;
 
   /*
@@ -1842,54 +1841,37 @@ Cursor::fetch(ulong num_rows)
 
   if (thd->net.report_error)
     error= -1;
+  if (error == -3)                              /* LIMIT clause worked */
+    error= 0;
 
-  switch (error) {
-    /* Fetch limit worked, possibly more rows are there */
-  case -4:
+#ifdef USING_TRANSACTIONS
     if (thd->transaction.all.innobase_tid)
       ha_release_temporary_latches(thd);
+#endif
+
+  thd->restore_backup_item_arena(this, &thd->stmt_backup);
+  if (error == -4)
+  {
+    /* Fetch limit worked, possibly more rows are there */
     thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
     ::send_eof(thd);
     thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
-    /* save references to memory, allocated during fetch */
-    mem_root= thd->mem_root;
-    free_list= thd->free_list;
-    break;
-    /* Limit clause worked: this is the same as 'no more rows' */
-  case -3:                                      /* LIMIT clause worked */
-    error= 0;
-    /* fallthrough */
-  case 0:                                       /* No more rows */
-    if (thd->transaction.all.innobase_tid)
-      ha_release_temporary_latches(thd);
-    close();
-    thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
-    ::send_eof(thd);
-    thd->server_status&= ~SERVER_STATUS_LAST_ROW_SENT;
-    join= 0;
-    unit= 0;
-    free_items(thd->free_list);
-    thd->free_list= free_list= 0;
-    /*
-      Must be last, as some memory might be allocated for free purposes,
-      like in free_tmp_table() (TODO: fix this issue)
-    */
-    mem_root= thd->mem_root;
-    free_root(&mem_root, MYF(0));
-    break;
-  default:
+  }
+  else
+  {
     close();
-    join= 0;
-    unit= 0;
-    free_items(thd->free_list);
-    thd->free_list= free_list= 0;
-    /*
-      Must be last, as some memory might be allocated for free purposes,
-      like in free_tmp_table() (TODO: fix this issue)
-    */
-    mem_root= thd->mem_root;
+    if (error == 0)
+    {
+      thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
+      ::send_eof(thd);
+      thd->server_status&= ~SERVER_STATUS_LAST_ROW_SENT;
+    }
+    else
+      send_error(thd, ER_OUT_OF_RESOURCES);
+    /* free cursor memory */
+    free_items(free_list);
+    free_list= 0;
     free_root(&mem_root, MYF(0));
-    break;
   }
   return error;
 }
@@ -1927,6 +1909,8 @@ Cursor::close()
     thd->derived_tables= tmp_derived_tables;
     thd->lock= tmp_lock;
   }
+  join= 0;
+  unit= 0;
 }
 
 
-- 
2.30.9