/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc
1 by edam
- initial commit
1
/*
2 by edam
- further initial development
2
 * transaction.cc
1 by edam
- initial commit
3
 *
22 by edam
updated email and web addresses
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
1 by edam
- initial commit
5
 *
2 by edam
- further initial development
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
22 by edam
updated email and web addresses
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.
1 by edam
- initial commit
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
2 by edam
- further initial development
23
#include <sqlite3cc/transaction.h>
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
24
#include <sqlite3cc/connection.h>
2 by edam
- further initial development
25
#include <sqlite3cc/exception.h>
3 by edam
- rewrote transaction classes and added transaction_guard
26
#include <boost/thread.hpp>
9 by edam
- added NEWS
27
#include <boost/lexical_cast.hpp>
3 by edam
- rewrote transaction classes and added transaction_guard
28
29
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
30
sqlite::detail::basic_transaction::basic_transaction(
31
	connection &connection )
1 by edam
- initial commit
32
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
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" );
1 by edam
- initial commit
70
}
71
72
12 by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
73
sqlite::immediate_transaction::immediate_transaction(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
74
	connection &connection )
12 by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
75
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
76
	basic_transaction( connection )
12 by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
77
{
78
}
79
80
81
void sqlite::immediate_transaction::begin()
82
{
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
83
	_connection.exec( "BEGIN IMMEDIATE" );
12 by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
84
}
85
86
1 by edam
- initial commit
87
sqlite::exclusive_transaction::exclusive_transaction(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
88
	connection &connection )
1 by edam
- initial commit
89
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
90
	basic_transaction( connection )
3 by edam
- rewrote transaction classes and added transaction_guard
91
{
92
}
93
94
95
void sqlite::exclusive_transaction::begin()
96
{
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
97
	_connection.exec( "BEGIN EXCLUSIVE" );
3 by edam
- rewrote transaction classes and added transaction_guard
98
}
99
100
101
sqlite::recursive_transaction::recursive_transaction(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
102
	connection &connection )
3 by edam
- rewrote transaction classes and added transaction_guard
103
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
104
	basic_transaction( connection )
3 by edam
- rewrote transaction classes and added transaction_guard
105
{
106
	static unsigned long long i = 0;
13 by edam
- made basic_statement::step() protected, for use by query and command only
107
	static boost::mutex mutex;
3 by edam
- rewrote transaction classes and added transaction_guard
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
{
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
119
	_connection.exec( "SAVEPOINT " + _sp_name );
3 by edam
- rewrote transaction classes and added transaction_guard
120
}
121
122
123
void sqlite::recursive_transaction::commit()
124
{
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
125
	// we must reset any active queries when committing
126
	reset_active_queries();
127
	_connection.exec( "RELEASE " + _sp_name );
3 by edam
- rewrote transaction classes and added transaction_guard
128
}
129
130
131
void sqlite::recursive_transaction::rollback()
132
{
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
133
	// we must reset any active queries when rolling back
134
	reset_active_queries();
135
	_connection.exec( "ROLLBACK TO " + _sp_name );
3 by edam
- rewrote transaction classes and added transaction_guard
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
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
139
	_connection.exec( "RELEASE " + _sp_name );
1 by edam
- initial commit
140
}