summaryrefslogtreecommitdiff
path: root/src/transaction.cpp
blob: a06f223df334ad6b2d736df58731003a6117b79e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 *  sqlite3xx - sqlite3 C++ layer, following the ideas of libpqxx
 *  Copyright (C) 2009  Andreas Baumann
 *
 *  This copyrighted material is made available to anyone wishing to use,
 *  modify, copy, or redistribute it subject to the terms and conditions of
 *  the GNU Lesser General Public License, as published by the Free Software
 *  Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY  or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this distribution; if not, write to:
 *  Free Software Foundation, Inc.
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02110-1301  USA
 *
 */

#include "sqlite3xx/transaction.hpp"

#include <stdexcept>
#include <sstream>
#include <iostream>

using namespace std;

namespace sqlite3xx {

transaction::transaction( connection &c, string name ) :
	_c( c ),
	_name( name ),
	_status( st_nascent ) {
}

transaction::~transaction( ) {
}

void transaction::commit( ) {
	switch( _status ) {
		case st_nascent:
			// skip, nothing to do
			return;

		case st_active:
			// continue below, this is what we expect
			break;

		case st_committed:
			// already commited, skip
			// TODO: we should warn the user about double commits
			break;

		case st_aborted:
			throw logic_error( "Attempt to commit previously aborted transaction!" );

		default:
			throw logic_error( "Illegal state in sqlite3::transaction::commit( )!" );
	}

	ostringstream s;
	s << "COMMIT TRANSACTION trans_" << _name;
	_c.exec( s.str( ).c_str( ) );
	_status = st_committed;
}

void transaction::abort( ) {
	switch( _status ) {
		case st_nascent:
			// skip, nothing to do
			return;

		case st_active:
			// continue below, this is what we expect
			break;

		case st_committed:
			throw logic_error( "Attempt to abort previously committed transaction!" );

		case st_aborted:
			// already aborted, skip
			// TODO: we should warn the user about double commits

		default:
			throw logic_error( "Illegal state in sqlite3::transaction::abort( )!" );
	}

	ostringstream s;
	s << "ROLLBACK TRANSACTION trans_" << _name;
	_c.exec( s.str( ).c_str( ) );
	_status = st_aborted;
}

void transaction::Begin( ) {
	switch( _status ) {
		case st_nascent:
			break;

		case st_active:
		case st_committed:
		case st_aborted:
		default:
			throw logic_error( "Illegal state in sqlite3::transaction::Begin( )!" );
	}

	ostringstream s;
	s << "BEGIN TRANSACTION trans_" << _name;
	_c.exec( s.str( ).c_str( ) );
}

result transaction::exec( string sql ) {
	switch( _status ) {
		case st_nascent:
			Begin( );
			break;

		case st_active:
			break;

		case st_committed:
		case st_aborted:
			throw logic_error( "Attempt to execute query " + sql + " in a transaction " +
				"which is already closed" );

		default:
			throw logic_error( "Illegal state in sqlite3::transaction::exec( )!" );
	}

	result r = _c.exec( sql );
	_status = st_active;

	return r;
}

prepare::invocation transaction::prepared( const string& name ) {
	return prepare::invocation( _c, *this, name ); 
}

} /* namespace sqlite3xx */