/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc

« back to all changes in this revision

Viewing changes to src/connection.cc

  • Committer: edam
  • Date: 2012-01-23 14:34:45 UTC
  • Revision ID: edam@waxworlds.org-20120123143445-s2v4v90nycmfm6bv
fixed up tests

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * database.cpp
3
 
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
 
 *
6
 
 * This file is part of sqlitepp (hereafter referred to as "this program").
7
 
 * See http://www.waxworlds.org/edam/software/sqlitepp 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.
 
2
 * connection.cc
 
3
 *
 
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 
5
 *
 
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.
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
 
#include <sqlitepp/database.hpp>
24
 
#include <sqlitepp/exception.hpp>
25
 
 
26
 
 
27
 
sqlite::database::database(
 
23
#include <sqlite3cc/connection.h>
 
24
#include <sqlite3cc/exception.h>
 
25
 
 
26
 
 
27
sqlite::connection::mutex_guard::mutex_guard(
 
28
        connection &connection )
 
29
        :
 
30
        _mutex( sqlite3_db_mutex( connection._handle ) )
 
31
{
 
32
        if( _mutex ) sqlite3_mutex_enter( _mutex );
 
33
}
 
34
 
 
35
 
 
36
sqlite::connection::mutex_guard::~mutex_guard()
 
37
{
 
38
        leave();
 
39
}
 
40
 
 
41
void sqlite::connection::mutex_guard::leave()
 
42
{
 
43
        if( _mutex ) {
 
44
                sqlite3_mutex_leave( _mutex );
 
45
                _mutex = NULL;
 
46
        }
 
47
}
 
48
 
 
49
 
 
50
sqlite::connection::connection(
28
51
        const std::string &filename )
29
52
        :
30
53
        _handle( NULL )
31
54
{
32
 
        int error_code = open( filename );
33
 
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
55
        int code = open( filename );
 
56
        if( code != SQLITE_OK ) throw sqlite_error( *this, code );
34
57
}
35
58
 
36
59
 
37
 
sqlite::database::database()
 
60
sqlite::connection::connection()
38
61
        :
39
62
        _handle( NULL )
40
63
{
41
64
}
42
65
 
43
66
 
44
 
sqlite::database::~database() throw( )
 
67
sqlite::connection::~connection()
45
68
{
46
69
        close();
47
70
}
48
71
 
49
72
 
50
 
int sqlite::database::open(
 
73
int sqlite::connection::open(
51
74
        const std::string &filename,
52
75
        int flags )
53
76
{
56
79
}
57
80
 
58
81
 
59
 
void sqlite::database::close()
 
82
void sqlite::connection::close()
60
83
{
61
84
        if( _handle ) {
62
85
                sqlite3_close( _handle );
65
88
}
66
89
 
67
90
 
68
 
int sqlite::database::exec(
 
91
void sqlite::connection::exec(
69
92
        const std::string &sql )
70
93
{
71
 
        return sqlite3_exec( _handle, sql.c_str(), NULL, NULL, NULL );
 
94
        int code = sqlite3_exec( _handle, sql.c_str(), NULL, NULL, NULL );
 
95
        if( code != SQLITE_OK ) throw sqlite_error( *this, code );
72
96
}
73
97
 
74
98
 
75
 
int sqlite::database::busy_timeout(
 
99
int sqlite::connection::busy_timeout(
76
100
        int duration )
77
101
{
78
102
        return sqlite3_busy_timeout( _handle, duration );