Drizzled Public API Documentation

rollback_to_savepoint.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <drizzled/show.h>
00023 #include <drizzled/session.h>
00024 #include <drizzled/statement/rollback_to_savepoint.h>
00025 #include <drizzled/transaction_services.h>
00026 #include <drizzled/named_savepoint.h>
00027 #include <drizzled/util/functors.h>
00028 #include <drizzled/session/transactions.h>
00029 
00030 #include <string>
00031 
00032 using namespace std;
00033 
00034 namespace drizzled {
00035 
00036 bool statement::RollbackToSavepoint::execute()
00037 {
00038   /*
00039    * If AUTOCOMMIT is off and resource contexts are empty then we need
00040    * to start a transaction. It will be empty when ROLLBACK TO SAVEPOINT
00041    * starts the transaction. Table affecting statements do this work in
00042    * lockTables() by calling startStatement().
00043    */
00044   if ( (session().options & OPTION_NOT_AUTOCOMMIT) &&
00045        (transaction().all.getResourceContexts().empty() == true) )
00046   {
00047     if (session().startTransaction() == false)
00048     {
00049       return false;
00050     }
00051   }
00052 
00053   /*
00054    * Handle these situations:
00055    *
00056    * If the first savepoint on the deck matches the
00057    * name to find, simply call rollback_to_savepoint
00058    * for the resource managers.
00059    *
00060    * If the first savepoint on the deck does NOT match
00061    * the name to find, then we need to search through
00062    * the deque to find the savepoint we need.  If we
00063    * don't find it, we return an error.  If we do
00064    * find it, we must restructure the deque by removing
00065    * all savepoints "above" the one we find.
00066    */
00067   deque<NamedSavepoint> &savepoints= transaction().savepoints;
00068 
00069   /* Short-circuit for no savepoints */
00070   if (savepoints.empty())
00071   {
00072     my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex().ident.data());
00073     return false;
00074   }
00075 
00076   {
00077     /* Short-circuit for first savepoint */
00078     NamedSavepoint &first_savepoint= savepoints.front();
00079     const string &first_savepoint_name= first_savepoint.getName();
00080     if (my_strnncoll(system_charset_info,
00081                      (unsigned char *) lex().ident.data(), 
00082                      lex().ident.size(),
00083                      (unsigned char *) first_savepoint_name.c_str(), 
00084                      first_savepoint_name.size()) == 0)
00085     {
00086       /* Found the named savepoint we want to rollback to */
00087       (void) TransactionServices::rollbackToSavepoint(session(), first_savepoint);
00088 
00089       if (transaction().all.hasModifiedNonTransData())
00090       {
00091         push_warning(&session(), 
00092                      DRIZZLE_ERROR::WARN_LEVEL_WARN,
00093                      ER_WARNING_NOT_COMPLETE_ROLLBACK,
00094                      ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
00095       }
00096       session().my_ok();
00097       return false;
00098     }
00099   }
00100 
00101   /* 
00102    * OK, from here on out it means that we have savepoints
00103    * but that the first savepoint isn't the one we're looking
00104    * for.  We need to search through the savepoints and find
00105    * the one we're looking for, removing all savepoints "above"
00106    * the one we need.
00107    */
00108   bool found= false;
00109   deque<NamedSavepoint> copy_savepoints(savepoints); /* used to restore if not found */
00110   deque<NamedSavepoint> new_savepoints;
00111   while (savepoints.empty() == false)
00112   {
00113     NamedSavepoint &sv= savepoints.front();
00114     const string &sv_name= sv.getName();
00115     if (! found && 
00116         my_strnncoll(system_charset_info,
00117                      (unsigned char *) lex().ident.data(), 
00118                      lex().ident.size(),
00119                      (unsigned char *) sv_name.c_str(), 
00120                      sv_name.size()) == 0)
00121     {
00122       /* Found the named savepoint we want to rollback to */
00123       found= true;
00124 
00125       (void) TransactionServices::rollbackToSavepoint(session(), sv);
00126     }
00127     if (found)
00128     {
00129       /* 
00130        * We push all savepoints "below" the found savepoint
00131        * to our new savepoint list, in reverse order to keep
00132        * the stack order correct. 
00133        */
00134       new_savepoints.push_back(sv);
00135     }
00136     savepoints.pop_front();
00137   }
00138   if (found)
00139   {
00140     if (transaction().all.hasModifiedNonTransData())
00141     {
00142       push_warning(&session(), 
00143                    DRIZZLE_ERROR::WARN_LEVEL_WARN,
00144                    ER_WARNING_NOT_COMPLETE_ROLLBACK,
00145                    ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
00146     }
00147     /* Store new savepoints list */
00148     transaction().savepoints= new_savepoints;
00149     session().my_ok();
00150   }
00151   else
00152   {
00153     /* restore the original savepoint list */
00154     transaction().savepoints= copy_savepoints;
00155     my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex().ident.data());
00156   }
00157   return false;
00158 }
00159 
00160 } /* namespace drizzled */