1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems, Inc.
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.
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.
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
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"
36
bool statement::RollbackToSavepoint::execute()
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().
44
if ( (getSession()->options & OPTION_NOT_AUTOCOMMIT) &&
45
(getSession()->transaction.all.getResourceContexts().empty() == true) )
47
if (getSession()->startTransaction() == false)
54
* Handle these situations:
56
* If the first savepoint on the deck matches the
57
* name to find, simply call rollback_to_savepoint
58
* for the resource managers.
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.
67
deque<NamedSavepoint> &savepoints= getSession()->transaction.savepoints;
68
TransactionServices &transaction_services= TransactionServices::singleton();
70
/* Short-circuit for no savepoints */
71
if (savepoints.empty())
73
my_error(ER_SP_DOES_NOT_EXIST,
76
getSession()->lex->ident.str);
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)
90
/* Found the named savepoint we want to rollback to */
91
(void) transaction_services.rollbackToSavepoint(*getSession(), first_savepoint);
93
if (getSession()->transaction.all.hasModifiedNonTransData())
95
push_warning(getSession(),
96
DRIZZLE_ERROR::WARN_LEVEL_WARN,
97
ER_WARNING_NOT_COMPLETE_ROLLBACK,
98
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
100
getSession()->my_ok();
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"
113
deque<NamedSavepoint> copy_savepoints(savepoints); /* used to restore if not found */
114
deque<NamedSavepoint> new_savepoints;
115
while (savepoints.empty() == false)
117
NamedSavepoint &sv= savepoints.front();
118
const string &sv_name= sv.getName();
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)
126
/* Found the named savepoint we want to rollback to */
129
(void) transaction_services.rollbackToSavepoint(*getSession(), sv);
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.
138
new_savepoints.push_back(sv);
140
savepoints.pop_front();
144
if (getSession()->transaction.all.hasModifiedNonTransData())
146
push_warning(getSession(),
147
DRIZZLE_ERROR::WARN_LEVEL_WARN,
148
ER_WARNING_NOT_COMPLETE_ROLLBACK,
149
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
151
/* Store new savepoints list */
152
getSession()->transaction.savepoints= new_savepoints;
153
getSession()->my_ok();
157
/* restore the original savepoint list */
158
getSession()->transaction.savepoints= copy_savepoints;
159
my_error(ER_SP_DOES_NOT_EXIST,
162
getSession()->lex->ident.str);
167
} /* namespace drizzled */