/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 06:28:53 UTC
  • Revision ID: edam@waxworlds.org-20100729062853-4i2fec52m86mh724
- made basic_statement::step() protected, for use by query and command only
- moved basic_statement::operator<<() to command and query instead; one needs to accept sqlite::exec, the other doesn't
- added tests for query::operator<<()
- added code to invlaidate in-progress queries during any transaction rollbacks (currently segfaults in basic_statement::finalize())
- added new sqlite_error constructor that obtains a full error message
- implemented database::database_mutex_guard class
- swapped command's step mutex in favour of the database mutex

Show diffs side-by-side

added added

removed removed

37
37
 
38
38
void sqlite::basic_transaction::begin()
39
39
{
40
 
        int error_code = _database.exec( "BEGIN" );
41
 
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
40
        _database.exec( "BEGIN" );
42
41
}
43
42
 
44
43
 
45
44
void sqlite::basic_transaction::commit()
46
45
{
47
 
        int error_code = _database.exec( "COMMIT" );
48
 
        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_finalize( handle ) )
 
54
                        throw sqlite_error( _database, code );
49
55
}
50
56
 
51
57
 
52
58
void sqlite::basic_transaction::rollback()
53
59
{
54
 
        int error_code = _database.exec( "ROLLBACK" );
55
 
        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" );
56
77
}
57
78
 
58
79
 
66
87
 
67
88
void sqlite::exclusive_transaction::begin()
68
89
{
69
 
        int error_code = _database.exec( "BEGIN EXCLUSIVE" );
70
 
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
90
        _database.exec( "BEGIN EXCLUSIVE" );
71
91
}
72
92
 
73
93
 
77
97
        basic_transaction( database )
78
98
{
79
99
        static unsigned long long i = 0;
 
100
        static boost::mutex mutex;
80
101
        unsigned long long my_i;
81
 
        static boost::mutex mutex;
82
102
        {
83
103
                boost::lock_guard< boost::mutex > lock( mutex );
84
104
                my_i = i++;
89
109
 
90
110
void sqlite::recursive_transaction::begin()
91
111
{
92
 
        int error_code = _database.exec( "SAVEPOINT " + _sp_name );
93
 
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
112
        _database.exec( "SAVEPOINT " + _sp_name );
94
113
}
95
114
 
96
115
 
97
116
void sqlite::recursive_transaction::commit()
98
117
{
99
 
        int error_code = _database.exec( "RELEASE " + _sp_name );
100
 
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
118
        _database.exec( "RELEASE " + _sp_name );
101
119
}
102
120
 
103
121
 
104
122
void sqlite::recursive_transaction::rollback()
105
123
{
106
 
        int error_code = _database.exec( "ROLLBACK TO " + _sp_name );
107
 
        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 );
108
127
 
109
128
        // we have rolled back this transaction's savepoint, but the savepoint will
110
129
        // remain on the transaction stack unless we also release it
111
 
        error_code = _database.exec( "RELEASE " + _sp_name );
112
 
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
130
        _database.exec( "RELEASE " + _sp_name );
113
131
}