/sqlite3cc

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

« back to all changes in this revision

Viewing changes to src/transaction.cc

  • Committer: edam
  • Date: 2012-01-23 13:47:08 UTC
  • Revision ID: edam@waxworlds.org-20120123134708-ol4tilkotsm3han0
updated build system

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 */
22
22
 
23
23
#include <sqlite3cc/transaction.h>
24
 
#include <sqlite3cc/database.h>
 
24
#include <sqlite3cc/connection.h>
25
25
#include <sqlite3cc/exception.h>
26
26
#include <boost/thread.hpp>
27
27
#include <boost/lexical_cast.hpp>
28
28
 
29
29
 
30
 
sqlite::basic_transaction::basic_transaction(
31
 
        database &database )
 
30
sqlite::detail::basic_transaction::basic_transaction(
 
31
        connection &connection )
32
32
        :
33
 
        _database( database )
34
 
{
35
 
}
36
 
 
37
 
 
38
 
void sqlite::basic_transaction::begin()
39
 
{
40
 
        _database.exec( "BEGIN" );
41
 
}
42
 
 
43
 
 
44
 
void sqlite::basic_transaction::commit()
45
 
{
46
 
        _database.exec( "COMMIT" );
47
 
}
48
 
 
49
 
 
50
 
void sqlite::basic_transaction::rollback()
51
 
{
52
 
        _database.exec( "ROLLBACK" );
 
33
        _connection( connection )
 
34
{
 
35
}
 
36
 
 
37
 
 
38
void sqlite::detail::basic_transaction::begin()
 
39
{
 
40
        _connection.exec( "BEGIN" );
 
41
}
 
42
 
 
43
 
 
44
void sqlite::detail::basic_transaction::commit()
 
45
{
 
46
        // we must reset any active queries when committing
 
47
        reset_active_queries();
 
48
        _connection.exec( "COMMIT" );
 
49
}
 
50
 
 
51
 
 
52
void sqlite::detail::basic_transaction::reset_active_queries()
 
53
{
 
54
        sqlite3_stmt *old_handle = NULL;
 
55
        while( sqlite3_stmt *handle =
 
56
                sqlite3_next_stmt( _connection._handle, old_handle ) )
 
57
        {
 
58
                int code = sqlite3_reset( handle );
 
59
                if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
 
60
                old_handle = handle;
 
61
        }
 
62
}
 
63
 
 
64
 
 
65
void sqlite::detail::basic_transaction::rollback()
 
66
{
 
67
        // we must reset any active queries when rolling back
 
68
        reset_active_queries();
 
69
        _connection.exec( "ROLLBACK" );
53
70
}
54
71
 
55
72
 
56
73
sqlite::immediate_transaction::immediate_transaction(
57
 
        database &database )
 
74
        connection &connection )
58
75
        :
59
 
        basic_transaction( database )
 
76
        basic_transaction( connection )
60
77
{
61
78
}
62
79
 
63
80
 
64
81
void sqlite::immediate_transaction::begin()
65
82
{
66
 
        _database.exec( "BEGIN IMMEDIATE" );
 
83
        _connection.exec( "BEGIN IMMEDIATE" );
67
84
}
68
85
 
69
86
 
70
87
sqlite::exclusive_transaction::exclusive_transaction(
71
 
        database &database )
 
88
        connection &connection )
72
89
        :
73
 
        basic_transaction( database )
 
90
        basic_transaction( connection )
74
91
{
75
92
}
76
93
 
77
94
 
78
95
void sqlite::exclusive_transaction::begin()
79
96
{
80
 
        _database.exec( "BEGIN EXCLUSIVE" );
 
97
        _connection.exec( "BEGIN EXCLUSIVE" );
81
98
}
82
99
 
83
100
 
84
101
sqlite::recursive_transaction::recursive_transaction(
85
 
        database &database )
 
102
        connection &connection )
86
103
        :
87
 
        basic_transaction( database )
 
104
        basic_transaction( connection )
88
105
{
89
106
        static unsigned long long i = 0;
 
107
        static boost::mutex mutex;
90
108
        unsigned long long my_i;
91
 
        static boost::mutex mutex;
92
109
        {
93
110
                boost::lock_guard< boost::mutex > lock( mutex );
94
111
                my_i = i++;
99
116
 
100
117
void sqlite::recursive_transaction::begin()
101
118
{
102
 
        _database.exec( "SAVEPOINT " + _sp_name );
 
119
        _connection.exec( "SAVEPOINT " + _sp_name );
103
120
}
104
121
 
105
122
 
106
123
void sqlite::recursive_transaction::commit()
107
124
{
108
 
        _database.exec( "RELEASE " + _sp_name );
 
125
        // we must reset any active queries when committing
 
126
        reset_active_queries();
 
127
        _connection.exec( "RELEASE " + _sp_name );
109
128
}
110
129
 
111
130
 
112
131
void sqlite::recursive_transaction::rollback()
113
132
{
114
 
        _database.exec( "ROLLBACK TO " + _sp_name );
 
133
        // we must reset any active queries when rolling back
 
134
        reset_active_queries();
 
135
        _connection.exec( "ROLLBACK TO " + _sp_name );
115
136
 
116
137
        // we have rolled back this transaction's savepoint, but the savepoint will
117
138
        // remain on the transaction stack unless we also release it
118
 
        _database.exec( "RELEASE " + _sp_name );
 
139
        _connection.exec( "RELEASE " + _sp_name );
119
140
}