/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-03-09 14:06:50 UTC
  • Revision ID: edam@waxworlds.org-20100309140650-oqwnsrbajh8d2p2m
- moved dependancy on boost_filesystem-mt from the library to test-main

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * transaction.cc
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
7
 
 * See http://ed.am/dev/sqlite3cc for more information.
8
 
 *
9
 
 * This program is free software: you can redistribute it and/or modify it under
10
 
 * the terms of the GNU Lesser General Public License as published by the Free
11
 
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 
 * later version.
13
 
 *
14
 
 * This program is distributed in the hope that it will be useful, but WITHOUT
15
 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17
 
 * details.
 
7
 * See http://www.waxworlds.org/edam/software/sqlite3cc for more information.
 
8
 *
 
9
 * This program is free software: you can redistribute it and/or modify
 
10
 * it under the terms of the GNU Lesser General Public License as published
 
11
 * by the Free Software Foundation, either version 3 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU Lesser General Public License for more details.
18
18
 *
19
19
 * You should have received a copy of the GNU Lesser General Public License
20
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
 */
22
22
 
23
23
#include <sqlite3cc/transaction.h>
24
 
#include <sqlite3cc/connection.h>
 
24
#include <sqlite3cc/database.h>
25
25
#include <sqlite3cc/exception.h>
26
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" );
 
27
 
 
28
 
 
29
sqlite::basic_transaction::basic_transaction(
 
30
        database &database )
 
31
        :
 
32
        _database( database )
 
33
{
 
34
}
 
35
 
 
36
 
 
37
void sqlite::basic_transaction::begin()
 
38
{
 
39
        int error_code = _database.exec( "BEGIN" );
 
40
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
41
}
 
42
 
 
43
 
 
44
void sqlite::basic_transaction::commit()
 
45
{
 
46
        int error_code = _database.exec( "COMMIT" );
 
47
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
48
}
 
49
 
 
50
 
 
51
void sqlite::basic_transaction::rollback()
 
52
{
 
53
        int error_code = _database.exec( "ROLLBACK" );
 
54
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
84
55
}
85
56
 
86
57
 
87
58
sqlite::exclusive_transaction::exclusive_transaction(
88
 
        connection &connection )
 
59
        database &database )
89
60
        :
90
 
        basic_transaction( connection )
 
61
        basic_transaction( database )
91
62
{
92
63
}
93
64
 
94
65
 
95
66
void sqlite::exclusive_transaction::begin()
96
67
{
97
 
        _connection.exec( "BEGIN EXCLUSIVE" );
 
68
        int error_code = _database.exec( "BEGIN EXCLUSIVE" );
 
69
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
98
70
}
99
71
 
100
72
 
101
73
sqlite::recursive_transaction::recursive_transaction(
102
 
        connection &connection )
 
74
        database &database )
103
75
        :
104
 
        basic_transaction( connection )
 
76
        basic_transaction( database )
105
77
{
106
78
        static unsigned long long i = 0;
 
79
        unsigned long long my_i;
107
80
        static boost::mutex mutex;
108
 
        unsigned long long my_i;
109
81
        {
110
82
                boost::lock_guard< boost::mutex > lock( mutex );
111
83
                my_i = i++;
116
88
 
117
89
void sqlite::recursive_transaction::begin()
118
90
{
119
 
        _connection.exec( "SAVEPOINT " + _sp_name );
 
91
        int error_code = _database.exec( "SAVEPOINT " + _sp_name );
 
92
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
120
93
}
121
94
 
122
95
 
123
96
void sqlite::recursive_transaction::commit()
124
97
{
125
 
        // we must reset any active queries when committing
126
 
        reset_active_queries();
127
 
        _connection.exec( "RELEASE " + _sp_name );
 
98
        int error_code = _database.exec( "RELEASE " + _sp_name );
 
99
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
128
100
}
129
101
 
130
102
 
131
103
void sqlite::recursive_transaction::rollback()
132
104
{
133
 
        // we must reset any active queries when rolling back
134
 
        reset_active_queries();
135
 
        _connection.exec( "ROLLBACK TO " + _sp_name );
 
105
        int error_code = _database.exec( "ROLLBACK TO " + _sp_name );
 
106
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
136
107
 
137
108
        // we have rolled back this transaction's savepoint, but the savepoint will
138
109
        // remain on the transaction stack unless we also release it
139
 
        _connection.exec( "RELEASE " + _sp_name );
 
110
        error_code = _database.exec( "RELEASE " + _sp_name );
 
111
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
140
112
}