/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: 2010-07-29 20:56:19 UTC
  • Revision ID: edam@waxworlds.org-20100729205619-a9yo4uzu647nvnsj
- renamed database to connection to better identify what it is (would database_connection be better though?)
- moved basic_statement and basic_transaction to sqlite::detail
- made sqlite::threadsafe() return the threading mode int, not a bool
- renamed row::index() to row_number() so it isn't confused with column index
- added typedef for deferred_transaction
- added early rollback method to transaction_guard
- allowed transaction_guard::~transaction_guard() to throw exceptions, since when it needs to, we're really screwed anyway
- bugfix: query::reset() didn't reset the internal row counter
- added query::num_results()
- added docs/design-notes
- reimplemented query::iterator so that increment() (which performs a step() on the query) now caches the returned row to be returned during dereference() (previously it stashed details and returned new row!?)
- bugfix: resetting active queries during rollbacks would hang!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * database.cc
 
2
 * connection.cc
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
20
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
 */
22
22
 
23
 
#include <sqlite3cc/database.h>
 
23
#include <sqlite3cc/connection.h>
24
24
#include <sqlite3cc/exception.h>
25
25
 
26
26
 
27
 
sqlite::database::database(
 
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 )
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()
 
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
 
void sqlite::database::exec(
 
91
void sqlite::connection::exec(
69
92
        const std::string &sql )
70
93
{
71
94
        int code = sqlite3_exec( _handle, sql.c_str(), NULL, NULL, NULL );
72
 
        if( code ) throw sqlite_error( *this, code );
 
95
        if( code != SQLITE_OK ) throw sqlite_error( *this, code );
73
96
}
74
97
 
75
98
 
76
 
int sqlite::database::busy_timeout(
 
99
int sqlite::connection::busy_timeout(
77
100
        int duration )
78
101
{
79
102
        return sqlite3_busy_timeout( _handle, duration );
80
103
}
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
 
}