~drizzle-trunk/drizzle/development

1100.3.52 by Padraig O'Sullivan
Extracted the ROLLBACK_TO_SAVEPOINT command into its own class and
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
1100.3.52 by Padraig O'Sullivan
Extracted the ROLLBACK_TO_SAVEPOINT command into its own class and
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
21
#include "config.h"
2005.1.1 by David Shrewsbury
Fix for when SAVEPOINT or ROLLBACK TO SAVEPOINT starts a transaction with AUTOCOMMIT off and no BEGIN; also fix temp_tables test so it runs correctly independent of other tests
22
#include "drizzled/show.h"
23
#include "drizzled/session.h"
24
#include "drizzled/statement/rollback_to_savepoint.h"
1273.1.2 by Jay Pipes
This patch does not change any algorithms or code paths,
25
#include "drizzled/transaction_services.h"
1273.1.4 by Jay Pipes
This patch significantly reworks the way that
26
#include "drizzled/named_savepoint.h"
27
#include "drizzled/util/functors.h"
28
29
#include <string>
30
31
using namespace std;
1100.3.52 by Padraig O'Sullivan
Extracted the ROLLBACK_TO_SAVEPOINT command into its own class and
32
1130.3.12 by Monty Taylor
using namespace drizzled; to namespace drizzled { in statement/
33
namespace drizzled
34
{
1100.3.52 by Padraig O'Sullivan
Extracted the ROLLBACK_TO_SAVEPOINT command into its own class and
35
36
bool statement::RollbackToSavepoint::execute()
37
{
1273.1.4 by Jay Pipes
This patch significantly reworks the way that
38
  /*
2005.1.1 by David Shrewsbury
Fix for when SAVEPOINT or ROLLBACK TO SAVEPOINT starts a transaction with AUTOCOMMIT off and no BEGIN; also fix temp_tables test so it runs correctly independent of other tests
39
   * If AUTOCOMMIT is off and resource contexts are empty then we need
40
   * to start a transaction. It will be empty when ROLLBACK TO SAVEPOINT
41
   * starts the transaction. Table affecting statements do this work in
42
   * lockTables() by calling startStatement().
43
   */
44
  if ( (session->options & OPTION_NOT_AUTOCOMMIT) &&
45
       (session->transaction.all.getResourceContexts().empty() == true) )
46
  {
47
    if (session->startTransaction() == false)
48
    {
49
      return false;
50
    }
51
  }
52
53
  /*
1273.1.4 by Jay Pipes
This patch significantly reworks the way that
54
   * Handle these situations:
55
   *
56
   * If the first savepoint on the deck matches the
57
   * name to find, simply call rollback_to_savepoint
58
   * for the resource managers.
59
   *
60
   * If the first savepoint on the deck does NOT match
61
   * the name to find, then we need to search through
62
   * the deque to find the savepoint we need.  If we
63
   * don't find it, we return an error.  If we do
64
   * find it, we must restructure the deque by removing
65
   * all savepoints "above" the one we find.
66
   */
67
  deque<NamedSavepoint> &savepoints= session->transaction.savepoints;
68
  TransactionServices &transaction_services= TransactionServices::singleton();
69
70
  /* Short-circuit for no savepoints */
71
  if (savepoints.empty())
72
  {
73
    my_error(ER_SP_DOES_NOT_EXIST, 
74
             MYF(0), 
75
             "SAVEPOINT", 
76
             session->lex->ident.str);
77
    return false;
78
  }
79
80
  {
81
    /* Short-circuit for first savepoint */
82
    NamedSavepoint &first_savepoint= savepoints.front();
83
    const string &first_savepoint_name= first_savepoint.getName();
1100.3.52 by Padraig O'Sullivan
Extracted the ROLLBACK_TO_SAVEPOINT command into its own class and
84
    if (my_strnncoll(system_charset_info,
85
                     (unsigned char *) session->lex->ident.str, 
86
                     session->lex->ident.length,
1273.1.4 by Jay Pipes
This patch significantly reworks the way that
87
                     (unsigned char *) first_savepoint_name.c_str(), 
88
                     first_savepoint_name.size()) == 0)
89
    {
90
      /* Found the named savepoint we want to rollback to */
1405.3.5 by Jay Pipes
TransactionServices method names now meet code style guidelines.
91
      (void) transaction_services.rollbackToSavepoint(session, first_savepoint);
1273.1.4 by Jay Pipes
This patch significantly reworks the way that
92
1273.1.13 by Jay Pipes
Style cleanup around TransactionContext::modified_non_trans_table and dead code removal
93
      if (session->transaction.all.hasModifiedNonTransData())
1100.3.52 by Padraig O'Sullivan
Extracted the ROLLBACK_TO_SAVEPOINT command into its own class and
94
      {
95
        push_warning(session, 
96
                     DRIZZLE_ERROR::WARN_LEVEL_WARN,
97
                     ER_WARNING_NOT_COMPLETE_ROLLBACK,
98
                     ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
99
      }
100
      session->my_ok();
1273.1.4 by Jay Pipes
This patch significantly reworks the way that
101
      return false;
102
    }
103
  }
104
105
  /* 
106
   * OK, from here on out it means that we have savepoints
107
   * but that the first savepoint isn't the one we're looking
108
   * for.  We need to search through the savepoints and find
109
   * the one we're looking for, removing all savepoints "above"
110
   * the one we need.
111
   */
112
  bool found= false;
113
  deque<NamedSavepoint> copy_savepoints(savepoints); /* used to restore if not found */
114
  deque<NamedSavepoint> new_savepoints;
115
  while (savepoints.empty() == false)
116
  {
117
    NamedSavepoint &sv= savepoints.front();
118
    const string &sv_name= sv.getName();
119
    if (! found && 
120
        my_strnncoll(system_charset_info,
121
                     (unsigned char *) session->lex->ident.str, 
122
                     session->lex->ident.length,
123
                     (unsigned char *) sv_name.c_str(), 
124
                     sv_name.size()) == 0)
125
    {
126
      /* Found the named savepoint we want to rollback to */
127
      found= true;
128
1405.3.5 by Jay Pipes
TransactionServices method names now meet code style guidelines.
129
      (void) transaction_services.rollbackToSavepoint(session, sv);
1273.1.4 by Jay Pipes
This patch significantly reworks the way that
130
    }
131
    if (found)
132
    {
133
      /* 
134
       * We push all savepoints "below" the found savepoint
135
       * to our new savepoint list, in reverse order to keep
136
       * the stack order correct. 
137
       */
138
      new_savepoints.push_back(sv);
139
    }
140
    savepoints.pop_front();
141
  }
142
  if (found)
143
  {
1273.1.13 by Jay Pipes
Style cleanup around TransactionContext::modified_non_trans_table and dead code removal
144
    if (session->transaction.all.hasModifiedNonTransData())
1273.1.4 by Jay Pipes
This patch significantly reworks the way that
145
    {
146
      push_warning(session, 
147
                   DRIZZLE_ERROR::WARN_LEVEL_WARN,
148
                   ER_WARNING_NOT_COMPLETE_ROLLBACK,
149
                   ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
150
    }
151
    /* Store new savepoints list */
152
    session->transaction.savepoints= new_savepoints;
153
    session->my_ok();
1100.3.52 by Padraig O'Sullivan
Extracted the ROLLBACK_TO_SAVEPOINT command into its own class and
154
  }
155
  else
156
  {
1273.1.4 by Jay Pipes
This patch significantly reworks the way that
157
    /* restore the original savepoint list */
158
    session->transaction.savepoints= copy_savepoints;
1100.3.52 by Padraig O'Sullivan
Extracted the ROLLBACK_TO_SAVEPOINT command into its own class and
159
    my_error(ER_SP_DOES_NOT_EXIST, 
160
             MYF(0), 
161
             "SAVEPOINT", 
162
             session->lex->ident.str);
163
  }
164
  return false;
165
}
1130.3.12 by Monty Taylor
using namespace drizzled; to namespace drizzled { in statement/
166
167
} /* namespace drizzled */