/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc
1 by edam
- initial commit
1
/*
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
2
 * connection.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
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
23
#include <sqlite3cc/connection.h>
2 by edam
- further initial development
24
#include <sqlite3cc/exception.h>
24 by edam
added command and query factory methods to connection
25
#include <sqlite3cc/command.h>
26
#include <sqlite3cc/query.h>
1 by edam
- initial commit
27
28
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
29
sqlite::connection::mutex_guard::mutex_guard(
30
	connection &connection )
31
	:
32
	_mutex( sqlite3_db_mutex( connection._handle ) )
33
{
34
	if( _mutex ) sqlite3_mutex_enter( _mutex );
35
}
36
37
38
sqlite::connection::mutex_guard::~mutex_guard()
39
{
40
	leave();
41
}
42
43
void sqlite::connection::mutex_guard::leave()
44
{
45
	if( _mutex ) {
46
		sqlite3_mutex_leave( _mutex );
47
		_mutex = NULL;
48
	}
49
}
50
51
52
sqlite::connection::connection(
1 by edam
- initial commit
53
	const std::string &filename )
54
	:
55
	_handle( NULL )
56
{
13 by edam
- made basic_statement::step() protected, for use by query and command only
57
	int code = open( filename );
58
	if( code != SQLITE_OK ) throw sqlite_error( *this, code );
1 by edam
- initial commit
59
}
60
61
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
62
sqlite::connection::connection()
1 by edam
- initial commit
63
	:
64
	_handle( NULL )
65
{
66
}
67
68
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
69
sqlite::connection::~connection()
1 by edam
- initial commit
70
{
71
	close();
72
}
73
74
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
75
int sqlite::connection::open(
1 by edam
- initial commit
76
	const std::string &filename,
77
	int flags )
78
{
79
	close();
80
	return sqlite3_open_v2( filename.c_str(), &_handle, flags, NULL );
81
}
82
83
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
84
void sqlite::connection::close()
1 by edam
- initial commit
85
{
86
	if( _handle ) {
87
		sqlite3_close( _handle );
88
		_handle = NULL;
89
	}
90
}
91
92
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
93
void sqlite::connection::exec(
1 by edam
- initial commit
94
	const std::string &sql )
95
{
10 by edam
- cleaned up test-main
96
	int code = sqlite3_exec( _handle, sql.c_str(), NULL, NULL, NULL );
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
97
	if( code != SQLITE_OK ) throw sqlite_error( *this, code );
1 by edam
- initial commit
98
}
99
100
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
101
int sqlite::connection::busy_timeout(
1 by edam
- initial commit
102
	int duration )
103
{
104
	return sqlite3_busy_timeout( _handle, duration );
105
}
24 by edam
added command and query factory methods to connection
106
107
108
boost::shared_ptr< sqlite::command > sqlite::connection::make_command(
109
	std::string sql )
110
{
111
	return boost::shared_ptr< sqlite::command >(
112
		new sqlite::command( *this, sql ) );
113
}
114
115
116
boost::shared_ptr< sqlite::query > sqlite::connection::make_query(
117
	std::string sql )
118
{
119
	return boost::shared_ptr< sqlite::query >(
120
		new sqlite::query( *this, sql ) );
121
}