Resource.hh

00001 //********************************************************************
00002 //*** ResourcePool/mysql/Resource.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: Resource.hh,v 1.14 2009-05-10 16:09:25 mws Exp $
00005 //********************************************************************
00006 /*
00007 This file is part of ResourcePool.
00008 
00009 ResourcePool is free software; you can redistribute it
00010 and/or modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2 of
00012 the License, or (at your option) any later version.
00013 
00014 ResourcePool is distributed in the hope that it will be
00015 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00016 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with ResourcePool; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00022 02111-1307  USA */
00023 
00024 
00025 #ifndef RESOURCEPOOL_MYSQL_RESOURCE_HH
00026 #define RESOURCEPOOL_MYSQL_RESOURCE_HH
00027 
00028 #include "ResourcePool/ResourcePoolEventObserver.hh"
00029 
00030 #ifndef RESOURCEPOOL_MYSQL_MYSQLFWD_HH
00031 #include "mysqlFwd.hh"
00032 #endif
00033 
00034 #include <string>
00035 
00036 namespace fatalmind {
00037 namespace mysql {
00038 class FinallyTxDepth;
00039 } // namespace mysql
00040 
00041 class mysqlResource {
00042     public:
00043         mysqlResource(const std::string& host
00044             , const std::string& user
00045             , const std::string& pass
00046             , const std::string& db
00047             , gc_ptr<ResourcePoolEventObserver> o
00048             , unsigned int port = 3306
00049             , const std::string& sock = ""
00050         );
00051         ~mysqlResource();
00052         bool precheck();
00053         bool postcheck();
00054         void close();
00055         void fail_close();
00056         MYSQL& get_plain_resource();
00057 
00058         int getTxDepth() {
00059             return _txDepth;
00060         }
00061 
00062         const std::string& getSessionID() const;
00063         inline const std::string& getResourceID() const {
00064             return getSessionID();
00065         }
00066 
00067         struct Events {
00068             ResourcePoolEventObserver::event_t PREPARE;
00069             ResourcePoolEventObserver::event_t EXECUTE;
00070             ResourcePoolEventObserver::event_t FETCH;
00071             ResourcePoolEventObserver::event_t COMMIT;
00072             ResourcePoolEventObserver::event_t ROLLBACK;
00073         } Event;
00074 
00075         gc_ptr<EventReader> getReader(const ResourcePoolEventObserver::event_t eventid) {
00076             return observer->getReader(eventid, getResourceID());
00077         }
00078     private:
00079         MYSQL*   _dbh;
00080     // transaction managment helper function
00081         int _txDepth;
00082         int incTxDepth() {
00083             return _txDepth++;
00084         }
00085         void decTxDepth() {
00086             --_txDepth;
00087         }
00088         mutable std::string sessionID;
00089     
00090         gc_ptr<ResourcePoolEventObserver> observer;
00091 
00092     friend class mysql::FinallyTxDepth;
00093 };
00094 
00095 namespace mysql {
00096 // TODO, use a more generic approach
00097 class FinallyTxDepth {
00098     public:
00099         FinallyTxDepth(mysqlResource& a_hand)
00100         : hand(a_hand)
00101         {
00102             hand.incTxDepth();
00103         }
00104 
00105         ~FinallyTxDepth() {
00106             hand.decTxDepth();
00107         }
00108     private:
00109         mysqlResource& hand;
00110 };
00111 } // namespace mysql
00112 
00113 } // namespace fatalmind
00114 #endif

Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3