/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.cpp

  • Committer: edam
  • Date: 2009-11-25 20:32:34 UTC
  • Revision ID: edam@waxworlds.org-20091125203234-24i7mzd60c7o46py
- initial commit
- basic support for: database, statements, commands, transactions, exceptions
- initial test app
- initial liscence and readme

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * database.cc
 
2
 * database.cpp
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
 
 * This file is part of sqlite3cc (hereafter referred to as "this program").
7
 
 * See http://www.waxworlds.org/edam/software/sqlite3cc for more information.
 
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
8
 *
9
9
 * This program is free software: you can redistribute it and/or modify
10
10
 * it under the terms of the GNU Lesser General Public License as published
20
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
 */
22
22
 
23
 
#include <sqlite3cc/database.h>
24
 
#include <sqlite3cc/exception.h>
 
23
#include <sqlitepp/database.hpp>
 
24
#include <sqlitepp/exception.hpp>
25
25
 
26
26
 
27
27
sqlite::database::database(
29
29
        :
30
30
        _handle( NULL )
31
31
{
32
 
        int code = open( filename );
33
 
        if( code != SQLITE_OK ) throw sqlite_error( *this, code );
 
32
        int error_code = open( filename );
 
33
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
34
34
}
35
35
 
36
36
 
41
41
}
42
42
 
43
43
 
44
 
sqlite::database::~database()
 
44
sqlite::database::~database() throw( )
45
45
{
46
46
        close();
47
47
}
65
65
}
66
66
 
67
67
 
68
 
void sqlite::database::exec(
 
68
int sqlite::database::exec(
69
69
        const std::string &sql )
70
70
{
71
 
        int code = sqlite3_exec( _handle, sql.c_str(), NULL, NULL, NULL );
72
 
        if( code ) throw sqlite_error( *this, code );
 
71
        return sqlite3_exec( _handle, sql.c_str(), NULL, NULL, NULL );
73
72
}
74
73
 
75
74
 
78
77
{
79
78
        return sqlite3_busy_timeout( _handle, duration );
80
79
}
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
 
}