/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-23 12:57:07 UTC
  • Revision ID: edam@waxworlds.org-20100723125707-qu9jk9vvg2uewx7t
- cleaned up test-main
- fixed comment type
- made sqlite::exec() throw instead of returning sqlite error code

Show diffs side-by-side

added added

removed removed

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