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 #include <drizzled/session/transactions.h>
00028 
00029 #include <string>
00030 #include <deque>
00031 
00032 using namespace std;
00033 
00034 namespace drizzled {
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     deque<NamedSavepoint> &savepoints= transaction().savepoints;
00065     deque<NamedSavepoint>::iterator iter;
00066 
00067     for (iter= savepoints.begin();
00068          iter != savepoints.end();
00069          ++iter)
00070     {
00071       NamedSavepoint &sv= *iter;
00072       const string &sv_name= sv.getName();
00073       if (my_strnncoll(system_charset_info,
00074                        (unsigned char *) lex().ident.data(),
00075                        lex().ident.size(),
00076                        (unsigned char *) sv_name.c_str(),
00077                        sv_name.size()) == 0)
00078         break;
00079     }
00080     if (iter != savepoints.end())
00081     {
00082       NamedSavepoint &sv= *iter;
00083       (void) TransactionServices::releaseSavepoint(session(), sv);
00084       savepoints.erase(iter);
00085     }
00086     
00087     NamedSavepoint newsv(lex().ident.data(), lex().ident.size());
00088 
00089     if (TransactionServices::setSavepoint(session(), newsv))
00090     {
00091       return true;
00092     }
00093     else
00094     {
00095       savepoints.push_front(newsv);
00096       session().my_ok();
00097     }
00098   }
00099   return false;
00100 }
00101 
00102 } /* namespace drizzled */