/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 09:16:26 UTC
  • Revision ID: edam@waxworlds.org-20100729091626-h8fmg0r74eyfo5ae
- fixed error caused by finialising in-progress queries during rollback that were later finaliased by RAII.

Show diffs side-by-side

added added

removed removed

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