DatabaseTransaction.h
Go to the documentation of this file.
00001 /* 00002 * Copyright 2006-2009 Savarese Software Research Corporation 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * https://www.savarese.com/software/ApacheLicense-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00022 #ifndef __SSRC_WSPR_DATABASE_DATABASE_TRANSACTION_H 00023 #define __SSRC_WSPR_DATABASE_DATABASE_TRANSACTION_H 00024 00025 #include <ssrc/wispers/database/Database.h> 00026 00027 #include <boost/noncopyable.hpp> 00028 00029 __BEGIN_NS_SSRC_WSPR_DATABASE 00030 00031 // This is a convenience macro intended only for internal use by the 00032 // library and is therefore left undocumented. 00033 #define WSPR_DB_INIT_QUERY(key) \ 00034 _ ## key(_database.prepare(*properties.get_ptr<string>(#key))) 00035 00036 class DatabaseWrapper { 00037 protected: 00038 friend class DatabaseTransaction; 00039 Database _database; 00040 00041 public: 00042 00043 explicit DatabaseWrapper(const string & db_file) SSRC_DECL_THROW(DatabaseException) : 00044 _database(db_file) 00045 { } 00046 00047 virtual ~DatabaseWrapper() { } 00048 00049 void begin_transaction() SSRC_DECL_THROW(DatabaseException) { 00050 _database.begin_transaction(); 00051 } 00052 00053 void rollback_transaction() SSRC_DECL_THROW(DatabaseException) { 00054 _database.rollback_transaction(); 00055 } 00056 00057 void end_transaction() SSRC_DECL_THROW(DatabaseException) { 00058 _database.end_transaction(); 00059 } 00060 }; 00061 00062 00070 class DatabaseTransaction : private boost::noncopyable { 00071 enum State { Undefined, Begin, End }; 00072 State _state; 00073 Database & _db; 00074 00075 public: 00076 00077 DatabaseTransaction(DatabaseWrapper & dbw) 00078 SSRC_DECL_THROW(DatabaseException) : 00079 _state(Undefined), _db(dbw._database) 00080 { 00081 _db.begin_transaction(); 00082 _state = Begin; 00083 } 00084 00085 DatabaseTransaction(Database & db) SSRC_DECL_THROW(DatabaseException) : 00086 _state(Undefined), _db(db) 00087 { 00088 _db.begin_transaction(); 00089 _state = Begin; 00090 } 00091 00092 void end() SSRC_DECL_THROW(DatabaseException) { 00093 if(_state == Begin) { 00094 _db.end_transaction(); 00095 _state = End; 00096 } 00097 } 00098 00099 void rollback() SSRC_DECL_THROW(DatabaseException) { 00100 if(_state == Begin) { 00101 _state = Undefined; 00102 _db.rollback_transaction(); 00103 } 00104 } 00105 00106 ~DatabaseTransaction() { 00107 try { 00108 rollback(); 00109 } catch(const DatabaseException & de) { 00110 // TODO: add some kind of error reporting 00111 } 00112 } 00113 }; 00114 00115 __END_NS_SSRC_WSPR_DATABASE 00116 00117 #endif
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.