~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rollback_to_savepoint.cc

Merge Nathan

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
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
 
 
21
 
#include "config.h"
22
 
#include "drizzled/show.h"
23
 
#include "drizzled/session.h"
24
 
#include "drizzled/statement/rollback_to_savepoint.h"
25
 
#include "drizzled/transaction_services.h"
26
 
#include "drizzled/named_savepoint.h"
27
 
#include "drizzled/util/functors.h"
28
 
 
29
 
#include <string>
30
 
 
31
 
using namespace std;
32
 
 
33
 
namespace drizzled
34
 
{
35
 
 
36
 
bool statement::RollbackToSavepoint::execute()
37
 
{
38
 
  /*
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 ( (getSession()->options & OPTION_NOT_AUTOCOMMIT) &&
45
 
       (getSession()->transaction.all.getResourceContexts().empty() == true) )
46
 
  {
47
 
    if (getSession()->startTransaction() == false)
48
 
    {
49
 
      return false;
50
 
    }
51
 
  }
52
 
 
53
 
  /*
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= getSession()->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
 
             getSession()->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();
84
 
    if (my_strnncoll(system_charset_info,
85
 
                     (unsigned char *) getSession()->lex->ident.str, 
86
 
                     getSession()->lex->ident.length,
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 */
91
 
      (void) transaction_services.rollbackToSavepoint(*getSession(), first_savepoint);
92
 
 
93
 
      if (getSession()->transaction.all.hasModifiedNonTransData())
94
 
      {
95
 
        push_warning(getSession(), 
96
 
                     DRIZZLE_ERROR::WARN_LEVEL_WARN,
97
 
                     ER_WARNING_NOT_COMPLETE_ROLLBACK,
98
 
                     ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
99
 
      }
100
 
      getSession()->my_ok();
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 *) getSession()->lex->ident.str, 
122
 
                     getSession()->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
 
 
129
 
      (void) transaction_services.rollbackToSavepoint(*getSession(), sv);
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
 
  {
144
 
    if (getSession()->transaction.all.hasModifiedNonTransData())
145
 
    {
146
 
      push_warning(getSession(), 
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
 
    getSession()->transaction.savepoints= new_savepoints;
153
 
    getSession()->my_ok();
154
 
  }
155
 
  else
156
 
  {
157
 
    /* restore the original savepoint list */
158
 
    getSession()->transaction.savepoints= copy_savepoints;
159
 
    my_error(ER_SP_DOES_NOT_EXIST, 
160
 
             MYF(0), 
161
 
             "SAVEPOINT", 
162
 
             getSession()->lex->ident.str);
163
 
  }
164
 
  return false;
165
 
}
166
 
 
167
 
} /* namespace drizzled */