/sqlite3cc

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

« back to all changes in this revision

Viewing changes to src/query.cc

  • Committer: edam
  • Date: 2010-07-29 09:16:26 UTC
  • Revision ID: edam@waxworlds.org-20100729091626-h8fmg0r74eyfo5ae
- fixed error caused by finialising in-progress queries during rollback that were later finaliased by RAII.

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * query.cpp
 
2
 * query.cc
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
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.
 
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.
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 <sqlitepp/query.hpp>
24
 
 
 
23
#include <sqlite3cc/query.h>
 
24
#include <sqlite3cc/row.h>
 
25
#include <sqlite3cc/database.h>
 
26
#include <assert.h>
 
27
 
 
28
 
 
29
sqlite::query::query(
 
30
        database &database,
 
31
        const std::string &sql )
 
32
        :
 
33
        basic_statement( database, sql ),
 
34
        _next_row( 0 )
 
35
{
 
36
        assert( sqlite3_column_count( _handle ) > 0 );
 
37
}
 
38
 
 
39
 
 
40
sqlite::query::query(
 
41
        database &database )
 
42
        :
 
43
        basic_statement( database ),
 
44
        _next_row( 0 )
 
45
{
 
46
}
 
47
 
 
48
 
 
49
int sqlite::query::prepare(
 
50
        const std::string &sql )
 
51
{
 
52
        _next_row = 0;
 
53
        int code = basic_statement::prepare( sql );
 
54
        assert( code != SQLITE_OK || sqlite3_column_count( _handle ) > 0 );
 
55
        return code;
 
56
}
 
57
 
 
58
 
 
59
sqlite::row sqlite::query::step()
 
60
{
 
61
        database::database_mutex_guard lock( _database );
 
62
 
 
63
        int code = basic_statement::step();
 
64
        if( code == SQLITE_DONE ) return row();
 
65
        if( code == SQLITE_ROW ) return row( _handle, _next_row++ );
 
66
 
 
67
        throw sqlite_error( _database, code );
 
68
}
 
69
 
 
70
 
 
71
unsigned int sqlite::query::column_count()
 
72
{
 
73
        return sqlite3_column_count( _handle );
 
74
}
 
75
 
 
76
 
 
77
const std::string sqlite::query::column_name(
 
78
        unsigned int index )
 
79
{
 
80
        assert( index <
 
81
                static_cast< unsigned int >( sqlite3_column_count( _handle ) ) );
 
82
        return sqlite3_column_name( _handle, index );
 
83
}
 
84
 
 
85
 
 
86
sqlite::query::iterator::iterator(
 
87
        sqlite::query &query,
 
88
        bool valid )
 
89
        :
 
90
        _query( query )
 
91
{
 
92
        if( valid )
 
93
                increment();
 
94
        else
 
95
                _valid = false;
 
96
}
 
97
 
 
98
 
 
99
sqlite::row sqlite::query::iterator::dereference() const
 
100
{
 
101
        return sqlite::row( _query._handle, _query._next_row++ );
 
102
}
 
103
 
 
104
 
 
105
void sqlite::query::iterator::increment()
 
106
{
 
107
        _valid = _query.step();
 
108
}
 
109
 
 
110
 
 
111
bool sqlite::query::iterator::equal(
 
112
        sqlite::query::iterator const &other )
 
113
        const
 
114
{
 
115
        return _valid == other._valid;
 
116
}
 
117
 
 
118
 
 
119
sqlite::query::iterator sqlite::query::begin()
 
120
{
 
121
        return sqlite::query::iterator( *this, true );
 
122
}
 
123
 
 
124
 
 
125
sqlite::query::iterator sqlite::query::end()
 
126
{
 
127
        return sqlite::query::iterator( *this, false );
 
128
}
25
129