/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:39:13 UTC
  • Revision ID: edam@waxworlds.org-20100729063913-wvcvkogsa2alwkhr
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec

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