Drizzled Public API Documentation

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/savepoint.h>
00025 #include <drizzled/transaction_services.h>
00026 #include <drizzled/named_savepoint.h>
00027 
00028 #include <string>
00029 #include <deque>
00030 
00031 using namespace std;
00032 
00033 namespace drizzled
00034 {
00035 
00036 bool statement::Savepoint::execute()
00037 {
00038   if (! (session().options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
00039   {
00040     /* AUTOCOMMIT is on and not in a BEGIN */
00041     session().my_ok();
00042   }
00043   else
00044   {
00045     /*
00046      * If AUTOCOMMIT is off and resource contexts are empty then we need
00047      * to start a transaction. It will be empty when SAVEPOINT starts the
00048      * transaction. Table affecting statements do this work in lockTables()
00049      * by calling startStatement().
00050      */
00051     if ( (session().options & OPTION_NOT_AUTOCOMMIT) &&
00052          (transaction().all.getResourceContexts().empty() == true) )
00053     {
00054       if (session().startTransaction() == false)
00055       {
00056         return false;
00057       }
00058     }
00059 
00060     /*
00061      * Look through the savepoints.  If we find one with
00062      * the same name, delete it.
00063      */
00064     TransactionServices &transaction_services= TransactionServices::singleton();
00065     deque<NamedSavepoint> &savepoints= transaction().savepoints;
00066     deque<NamedSavepoint>::iterator iter;
00067 
00068     for (iter= savepoints.begin();
00069          iter != savepoints.end();
00070          ++iter)
00071     {
00072       NamedSavepoint &sv= *iter;
00073       const string &sv_name= sv.getName();
00074       if (my_strnncoll(system_charset_info,
00075                        (unsigned char *) lex().ident.str,
00076                        lex().ident.length,
00077                        (unsigned char *) sv_name.c_str(),
00078                        sv_name.size()) == 0)
00079         break;
00080     }
00081     if (iter != savepoints.end())
00082     {
00083       NamedSavepoint &sv= *iter;
00084       (void) transaction_services.releaseSavepoint(session(), sv);
00085       savepoints.erase(iter);
00086     }
00087     
00088     NamedSavepoint newsv(lex().ident.str, lex().ident.length);
00089 
00090     if (transaction_services.setSavepoint(session(), newsv))
00091     {
00092       return true;
00093     }
00094     else
00095     {
00096       savepoints.push_front(newsv);
00097       session().my_ok();
00098     }
00099   }
00100   return false;
00101 }
00102 
00103 } /* namespace drizzled */