/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc

« back to all changes in this revision

Viewing changes to include/sqlitepp/transaction.hpp

  • Committer: edam
  • Date: 2009-11-25 20:32:34 UTC
  • Revision ID: edam@waxworlds.org-20091125203234-24i7mzd60c7o46py
- initial commit
- basic support for: database, statements, commands, transactions, exceptions
- initial test app
- initial liscence and readme

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * transaction.h
 
2
 * transaction.hpp
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
 
 * This file is part of sqlite3cc (hereafter referred to as "this program").
7
 
 * See http://www.waxworlds.org/edam/software/sqlite3cc for more information.
 
6
 * This file is part of sqlitepp (hereafter referred to as "this program").
 
7
 * See http://www.waxworlds.org/edam/software/sqlitepp for more information.
8
8
 *
9
9
 * This program is free software: you can redistribute it and/or modify
10
10
 * it under the terms of the GNU Lesser General Public License as published
20
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
 */
22
22
 
23
 
#ifndef SQLITE3CC_TRANSACTION_H_
24
 
#define SQLITE3CC_TRANSACTION_H_
 
23
#ifndef TRANSACTION_HPP_
 
24
#define TRANSACTION_HPP_
25
25
 
26
26
 
27
27
#include <boost/utility.hpp>
34
34
class database;
35
35
 
36
36
 
37
 
class basic_transaction
 
37
class transaction
38
38
        :
39
39
        private boost::noncopyable
40
40
{
46
46
         * Constructor that provides a database upon which to act
47
47
         * @param database a database
48
48
         */
49
 
        explicit basic_transaction(
 
49
        transaction(
50
50
                database &database );
51
51
 
 
52
        virtual ~transaction() throw( );
 
53
 
 
54
protected:
 
55
 
 
56
        /**
 
57
         * Constructor that provides a way for deriving classes to override the SQL
 
58
         * executed in beginning and rolling-back a transaction during construction
 
59
         * and destruction.
 
60
         * @param database a database
 
61
         * @param begin_sql the SQL statement used to begin the transaction
 
62
         * @param rollback_sql the SQL statement used to rollback the transaction,
 
63
         *              or an empty string if the default is to be used.
 
64
         */
 
65
        transaction(
 
66
                database &database,
 
67
                const std::string &begin_sql,
 
68
                const std::string &rollback_sql = "" );
 
69
 
52
70
//______________________________________________________________________________
53
71
//                                                              public interface
54
 
public:
55
 
 
56
 
        /**
57
 
         * Begin the transaction
58
 
         */
59
 
        virtual void begin();
60
72
 
61
73
        /**
62
74
         * Commit the transaction
66
78
        /**
67
79
         * Rollback the transaction
68
80
         */
69
 
        virtual void rollback();
 
81
        void rollback();
70
82
 
71
83
//______________________________________________________________________________
72
84
//                                                                implementation
73
85
protected:
74
86
 
75
 
        /* the database on which to act */
 
87
        /** the database */
76
88
        database &_database;
77
89
 
 
90
        /** the SQL used to rollback the transaction, or empty to use default */
 
91
        std::string _rollback_sql;
 
92
 
78
93
};
79
94
 
80
95
 
83
98
 
84
99
class exclusive_transaction
85
100
        :
86
 
        public basic_transaction
87
 
{
88
 
//______________________________________________________________________________
89
 
//                                                                 instantiation
90
 
public:
91
 
 
92
 
        /**
93
 
         * Constructor that provides a database upon which to act
94
 
         * @param database a database
95
 
         */
96
 
        explicit exclusive_transaction(
97
 
                database &database );
98
 
 
99
 
//______________________________________________________________________________
100
 
//                                                              public interface
101
 
public:
102
 
 
103
 
        /**
104
 
         * Begin the transaction
105
 
         */
106
 
        virtual void begin();
107
 
 
108
 
};
109
 
 
110
 
 
111
 
////////////////////////////////////////////////////////////////////////////////
112
 
 
113
 
 
114
 
class recursive_transaction
115
 
        :
116
 
        public basic_transaction
117
 
{
118
 
//______________________________________________________________________________
119
 
//                                                                 instantiation
120
 
public:
121
 
 
122
 
        /**
123
 
         * Constructor that provides a database upon which to act
124
 
         * @param database a database
125
 
         */
126
 
        explicit recursive_transaction(
127
 
                database &database );
128
 
 
129
 
//______________________________________________________________________________
130
 
//                                                              public interface
131
 
public:
132
 
 
133
 
        /**
134
 
         * Begin the transaction
135
 
         */
136
 
        virtual void begin();
137
 
 
138
 
        /**
139
 
         * Commit the transaction
140
 
         */
141
 
        virtual void commit();
142
 
 
143
 
        /**
144
 
         * Rollback the transaction
145
 
         */
146
 
        virtual void rollback();
147
 
 
148
 
//______________________________________________________________________________
149
 
//                                                                implementation
150
 
protected:
151
 
 
152
 
        /* this transaction's savepoint name */
153
 
        std::string _sp_name;
154
 
 
155
 
};
156
 
 
157
 
 
158
 
////////////////////////////////////////////////////////////////////////////////
159
 
 
160
 
 
161
 
template< class T = basic_transaction >
162
 
class transaction_guard
163
 
        :
164
 
        private boost::noncopyable
165
 
{
166
 
//______________________________________________________________________________
167
 
//                                                                 instantiation
168
 
public:
169
 
 
170
 
        /**
171
 
         * Constructor that provides a database upon which to act
172
 
         * @param database a database
173
 
         */
174
 
        explicit transaction_guard(
175
 
                database &database )
176
 
                :
177
 
                _transaction( database ),
178
 
                _released( false )
179
 
        {
180
 
                _transaction.begin();
181
 
        }
182
 
 
183
 
        ~transaction_guard()
184
 
        {
185
 
                if( !_released )
186
 
                        _transaction.rollback();
187
 
        }
188
 
 
189
 
//______________________________________________________________________________
190
 
//                                                              public interface
191
 
public:
192
 
 
193
 
        /**
194
 
         * Commit the transaction
195
 
         */
196
 
        void commit()
197
 
        {
198
 
                if( !_released ) {
199
 
                        _transaction.commit();
200
 
                        _released = true;
201
 
                }
202
 
        }
203
 
 
204
 
//______________________________________________________________________________
205
 
//                                                                implementation
206
 
protected:
207
 
 
208
 
        /* the transaction */
209
 
        T _transaction;
210
 
 
211
 
        /* have we released the transaction yet? */
212
 
        bool _released;
213
 
 
214
 
};
215
 
 
216
 
 
217
 
} // namespace sqlite
218
 
 
219
 
 
220
 
#endif /* SQLITE3CC_TRANSACTION_H_ */
 
101
        public transaction
 
102
{
 
103
//______________________________________________________________________________
 
104
//                                                                 instantiation
 
105
public:
 
106
 
 
107
        /**
 
108
         * Constructor that provides a database upon which to act
 
109
         * @param database a database
 
110
         */
 
111
        exclusive_transaction(
 
112
                database &database );
 
113
 
 
114
};
 
115
 
 
116
 
 
117
}
 
118
 
 
119
 
 
120
#endif /* TRANSACTION_HPP_ */