23
23
#include <sqlite3cc/transaction.h>
24
#include <sqlite3cc/connection.h>
24
#include <sqlite3cc/database.h>
25
25
#include <sqlite3cc/exception.h>
26
26
#include <boost/thread.hpp>
27
27
#include <boost/lexical_cast.hpp>
30
sqlite::detail::basic_transaction::basic_transaction(
31
connection &connection )
30
sqlite::basic_transaction::basic_transaction(
33
_connection( connection )
38
void sqlite::detail::basic_transaction::begin()
40
_connection.exec( "BEGIN" );
44
void sqlite::detail::basic_transaction::commit()
46
// we must reset any active queries when committing
47
reset_active_queries();
48
_connection.exec( "COMMIT" );
52
void sqlite::detail::basic_transaction::reset_active_queries()
54
sqlite3_stmt *old_handle = NULL;
55
while( sqlite3_stmt *handle =
56
sqlite3_next_stmt( _connection._handle, old_handle ) )
58
int code = sqlite3_reset( handle );
59
if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
65
void sqlite::detail::basic_transaction::rollback()
67
// we must reset any active queries when rolling back
68
reset_active_queries();
69
_connection.exec( "ROLLBACK" );
38
void sqlite::basic_transaction::begin()
40
_database.exec( "BEGIN" );
44
void sqlite::basic_transaction::commit()
46
_database.exec( "COMMIT" );
50
void sqlite::basic_transaction::invalidate_queries()
52
while( sqlite3_stmt *handle = sqlite3_next_stmt( _database._handle, NULL ) )
53
if( int code = sqlite3_finalize( handle ) )
54
throw sqlite_error( _database, code );
58
void sqlite::basic_transaction::rollback()
60
// we must invalidate any active queries when rolling back
62
_database.exec( "ROLLBACK" );
73
66
sqlite::immediate_transaction::immediate_transaction(
74
connection &connection )
76
basic_transaction( connection )
69
basic_transaction( database )
81
74
void sqlite::immediate_transaction::begin()
83
_connection.exec( "BEGIN IMMEDIATE" );
76
_database.exec( "BEGIN IMMEDIATE" );
87
80
sqlite::exclusive_transaction::exclusive_transaction(
88
connection &connection )
90
basic_transaction( connection )
83
basic_transaction( database )
95
88
void sqlite::exclusive_transaction::begin()
97
_connection.exec( "BEGIN EXCLUSIVE" );
90
_database.exec( "BEGIN EXCLUSIVE" );
101
94
sqlite::recursive_transaction::recursive_transaction(
102
connection &connection )
104
basic_transaction( connection )
97
basic_transaction( database )
106
99
static unsigned long long i = 0;
107
100
static boost::mutex mutex;
117
110
void sqlite::recursive_transaction::begin()
119
_connection.exec( "SAVEPOINT " + _sp_name );
112
_database.exec( "SAVEPOINT " + _sp_name );
123
116
void sqlite::recursive_transaction::commit()
125
// we must reset any active queries when committing
126
reset_active_queries();
127
_connection.exec( "RELEASE " + _sp_name );
118
_database.exec( "RELEASE " + _sp_name );
131
122
void sqlite::recursive_transaction::rollback()
133
// we must reset any active queries when rolling back
134
reset_active_queries();
135
_connection.exec( "ROLLBACK TO " + _sp_name );
124
// we must invalidate any active queries when rolling back
125
invalidate_queries();
126
_database.exec( "ROLLBACK TO " + _sp_name );
137
128
// we have rolled back this transaction's savepoint, but the savepoint will
138
129
// remain on the transaction stack unless we also release it
139
_connection.exec( "RELEASE " + _sp_name );
130
_database.exec( "RELEASE " + _sp_name );