/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: 2010-07-29 21:00:36 UTC
  • Revision ID: edam@waxworlds.org-20100729210036-7me19mqc1iqo34pm
- failed to update TODO in last commit

Show diffs side-by-side

added added

removed removed

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