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>
28
#include <drizzled/session/transactions.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 ( (session().options & OPTION_NOT_AUTOCOMMIT) &&
45
(transaction().all.getResourceContexts().empty() == true) )
47
if (session().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= transaction().savepoints;
69
/* Short-circuit for no savepoints */
70
if (savepoints.empty())
72
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex().ident.data());
77
/* Short-circuit for first savepoint */
78
NamedSavepoint &first_savepoint= savepoints.front();
79
const string &first_savepoint_name= first_savepoint.getName();
80
if (my_strnncoll(system_charset_info,
81
(unsigned char *) lex().ident.data(),
83
(unsigned char *) first_savepoint_name.c_str(),
84
first_savepoint_name.size()) == 0)
86
/* Found the named savepoint we want to rollback to */
87
(void) TransactionServices::rollbackToSavepoint(session(), first_savepoint);
89
if (transaction().all.hasModifiedNonTransData())
91
push_warning(&session(),
92
DRIZZLE_ERROR::WARN_LEVEL_WARN,
93
ER_WARNING_NOT_COMPLETE_ROLLBACK,
94
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
102
* OK, from here on out it means that we have savepoints
103
* but that the first savepoint isn't the one we're looking
104
* for. We need to search through the savepoints and find
105
* the one we're looking for, removing all savepoints "above"
109
deque<NamedSavepoint> copy_savepoints(savepoints); /* used to restore if not found */
110
deque<NamedSavepoint> new_savepoints;
111
while (savepoints.empty() == false)
113
NamedSavepoint &sv= savepoints.front();
114
const string &sv_name= sv.getName();
116
my_strnncoll(system_charset_info,
117
(unsigned char *) lex().ident.data(),
119
(unsigned char *) sv_name.c_str(),
120
sv_name.size()) == 0)
122
/* Found the named savepoint we want to rollback to */
125
(void) TransactionServices::rollbackToSavepoint(session(), sv);
130
* We push all savepoints "below" the found savepoint
131
* to our new savepoint list, in reverse order to keep
132
* the stack order correct.
134
new_savepoints.push_back(sv);
136
savepoints.pop_front();
140
if (transaction().all.hasModifiedNonTransData())
142
push_warning(&session(),
143
DRIZZLE_ERROR::WARN_LEVEL_WARN,
144
ER_WARNING_NOT_COMPLETE_ROLLBACK,
145
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
147
/* Store new savepoints list */
148
transaction().savepoints= new_savepoints;
153
/* restore the original savepoint list */
154
transaction().savepoints= copy_savepoints;
155
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex().ident.data());
160
} /* namespace drizzled */