/sqlite3cc

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

« back to all changes in this revision

Viewing changes to src/database.cc

  • Committer: edam
  • Date: 2010-07-29 06:28:53 UTC
  • Revision ID: edam@waxworlds.org-20100729062853-4i2fec52m86mh724
- made basic_statement::step() protected, for use by query and command only
- moved basic_statement::operator<<() to command and query instead; one needs to accept sqlite::exec, the other doesn't
- added tests for query::operator<<()
- added code to invlaidate in-progress queries during any transaction rollbacks (currently segfaults in basic_statement::finalize())
- added new sqlite_error constructor that obtains a full error message
- implemented database::database_mutex_guard class
- swapped command's step mutex in favour of the database mutex

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * connection.cc
 
2
 * database.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
 
#include <sqlite3cc/connection.h>
 
23
#include <sqlite3cc/database.h>
24
24
#include <sqlite3cc/exception.h>
25
25
 
26
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(
 
27
sqlite::database::database(
51
28
        const std::string &filename )
52
29
        :
53
30
        _handle( NULL )
57
34
}
58
35
 
59
36
 
60
 
sqlite::connection::connection()
 
37
sqlite::database::database()
61
38
        :
62
39
        _handle( NULL )
63
40
{
64
41
}
65
42
 
66
43
 
67
 
sqlite::connection::~connection()
 
44
sqlite::database::~database()
68
45
{
69
46
        close();
70
47
}
71
48
 
72
49
 
73
 
int sqlite::connection::open(
 
50
int sqlite::database::open(
74
51
        const std::string &filename,
75
52
        int flags )
76
53
{
79
56
}
80
57
 
81
58
 
82
 
void sqlite::connection::close()
 
59
void sqlite::database::close()
83
60
{
84
61
        if( _handle ) {
85
62
                sqlite3_close( _handle );
88
65
}
89
66
 
90
67
 
91
 
void sqlite::connection::exec(
 
68
void sqlite::database::exec(
92
69
        const std::string &sql )
93
70
{
94
71
        int code = sqlite3_exec( _handle, sql.c_str(), NULL, NULL, NULL );
95
 
        if( code != SQLITE_OK ) throw sqlite_error( *this, code );
 
72
        if( code ) throw sqlite_error( *this, code );
96
73
}
97
74
 
98
75
 
99
 
int sqlite::connection::busy_timeout(
 
76
int sqlite::database::busy_timeout(
100
77
        int duration )
101
78
{
102
79
        return sqlite3_busy_timeout( _handle, duration );
103
80
}
 
81
 
 
82
 
 
83
sqlite::database::database_mutex_guard::database_mutex_guard(
 
84
        database &database )
 
85
        :
 
86
        _mutex( sqlite3_db_mutex( database._handle ) )
 
87
{
 
88
        if( _mutex ) sqlite3_mutex_enter( _mutex );
 
89
}
 
90
 
 
91
 
 
92
sqlite::database::database_mutex_guard::~database_mutex_guard()
 
93
{
 
94
        leave();
 
95
}
 
96
 
 
97
void sqlite::database::database_mutex_guard::leave()
 
98
{
 
99
        if( _mutex ) {
 
100
                sqlite3_mutex_leave( _mutex );
 
101
                _mutex = NULL;
 
102
        }
 
103
}