Web Wispers 1.2.2 C++ Unit Test Coverage |
|
|
|
|
Branch data Line data Source code
1 : : /*
2 : : * Copyright 2006-2009 Savarese Software Research Corporation
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * https://www.savarese.com/software/ApacheLicense-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : /**
18 : : * @file
19 : : * This header defines the DatabaseTransaction class.
20 : : */
21 : :
22 : : #ifndef __SSRC_WSPR_DATABASE_DATABASE_TRANSACTION_H
23 : : #define __SSRC_WSPR_DATABASE_DATABASE_TRANSACTION_H
24 : :
25 : : #include <ssrc/wispers/database/Database.h>
26 : :
27 : : #include <boost/noncopyable.hpp>
28 : :
29 : : __BEGIN_NS_SSRC_WSPR_DATABASE
30 : :
31 : : // This is a convenience macro intended only for internal use by the
32 : : // library and is therefore left undocumented.
33 : : #define WSPR_DB_INIT_QUERY(key) \
34 : : _ ## key(_database.prepare(*properties.get_ptr<string>(#key)))
35 : :
36 : : class DatabaseWrapper {
37 : : protected:
38 : : friend class DatabaseTransaction;
39 : : Database _database;
40 : :
41 : : public:
42 : :
43 : 16 : explicit DatabaseWrapper(const string & db_file) SSRC_DECL_THROW(DatabaseException) :
44 : 16 : _database(db_file)
45 : 16 : { }
46 : :
47 [ - + ]: 16 : virtual ~DatabaseWrapper() { }
48 : :
49 : : void begin_transaction() SSRC_DECL_THROW(DatabaseException) {
50 : : _database.begin_transaction();
51 : : }
52 : :
53 : : void rollback_transaction() SSRC_DECL_THROW(DatabaseException) {
54 : : _database.rollback_transaction();
55 : : }
56 : :
57 : : void end_transaction() SSRC_DECL_THROW(DatabaseException) {
58 : : _database.end_transaction();
59 : : }
60 : : };
61 : :
62 : :
63 : : /**
64 : : * Use DatabaseTransaction to ensure a transaction is rolled
65 : : * back if an exception is thrown. DatabaseTransaction begins a
66 : : * database transaction in its constructor. If DatabaseTransaction::end()
67 : : * is called before the instance is destroyed, the transaction is
68 : : * committed. Otherwise, the destructor rolls back the transaction.
69 : : */
70 : : class DatabaseTransaction : private boost::noncopyable {
71 : : enum State { Undefined, Begin, End };
72 : : State _state;
73 : : Database & _db;
74 : :
75 : : public:
76 : :
77 : : DatabaseTransaction(DatabaseWrapper & dbw)
78 : : SSRC_DECL_THROW(DatabaseException) :
79 : : _state(Undefined), _db(dbw._database)
80 : : {
81 : : _db.begin_transaction();
82 : : _state = Begin;
83 : : }
84 : :
85 : : DatabaseTransaction(Database & db) SSRC_DECL_THROW(DatabaseException) :
86 : : _state(Undefined), _db(db)
87 : : {
88 : : _db.begin_transaction();
89 : : _state = Begin;
90 : : }
91 : :
92 : : void end() SSRC_DECL_THROW(DatabaseException) {
93 : : if(_state == Begin) {
94 : : _db.end_transaction();
95 : : _state = End;
96 : : }
97 : : }
98 : :
99 : : void rollback() SSRC_DECL_THROW(DatabaseException) {
100 : : if(_state == Begin) {
101 : : _state = Undefined;
102 : : _db.rollback_transaction();
103 : : }
104 : : }
105 : :
106 : : ~DatabaseTransaction() {
107 : : try {
108 : : rollback();
109 : : } catch(const DatabaseException & de) {
110 : : // TODO: add some kind of error reporting
111 : : }
112 : : }
113 : : };
114 : :
115 : : __END_NS_SSRC_WSPR_DATABASE
116 : :
117 : : #endif
|
Copyright © 2017 Savarese Software Research Corporation. All rights reserved