/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 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
2
 * query.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/>.
22
22
 
23
23
#include <sqlite3cc/query.h>
24
24
#include <sqlite3cc/row.h>
25
 
#include <sqlite3cc/connection.h>
26
 
#include <cassert>
 
25
#include <sqlite3cc/manipulator.h>
 
26
#include <sqlite3cc/database.h>
 
27
#include <assert.h>
27
28
 
28
29
 
29
30
sqlite::query::query(
30
 
        connection &connection,
 
31
        database &database,
31
32
        const std::string &sql )
32
33
        :
33
 
        basic_statement( connection, sql ),
34
 
        _next_row_number( 0 )
 
34
        basic_statement( database, sql ),
 
35
        _next_row( 0 )
35
36
{
36
37
        assert( sqlite3_column_count( _handle ) > 0 );
37
38
}
38
39
 
39
40
 
40
41
sqlite::query::query(
41
 
        connection &connection )
 
42
        database &database )
42
43
        :
43
 
        basic_statement( connection ),
44
 
        _next_row_number( 0 )
 
44
        basic_statement( database ),
 
45
        _next_row( 0 )
45
46
{
46
47
}
47
48
 
49
50
int sqlite::query::prepare(
50
51
        const std::string &sql )
51
52
{
52
 
        _next_row_number = 0;
 
53
        _next_row = 0;
53
54
        int code = basic_statement::prepare( sql );
54
55
        assert( code != SQLITE_OK || sqlite3_column_count( _handle ) > 0 );
55
56
        return code;
56
57
}
57
58
 
58
59
 
59
 
int sqlite::query::reset()
60
 
{
61
 
        _next_row_number = 0;
62
 
        return basic_statement::reset();
63
 
}
64
 
 
65
 
 
66
60
sqlite::row sqlite::query::step()
67
61
{
68
 
        connection::mutex_guard lock( _connection );
 
62
        database::database_mutex_guard lock( _database );
69
63
 
70
64
        int code = basic_statement::step();
71
65
        if( code == SQLITE_DONE ) return row();
72
 
        if( code == SQLITE_ROW ) return row( _handle, _next_row_number++ );
 
66
        if( code == SQLITE_ROW ) return row( _handle, _next_row++ );
73
67
 
74
 
        throw sqlite_error( _connection, code );
 
68
        throw sqlite_error( _database, code );
75
69
}
76
70
 
77
71
 
90
84
}
91
85
 
92
86
 
93
 
unsigned long long sqlite::query::num_results()
94
 
{
95
 
        reset();
96
 
        unsigned long long count = 0;
97
 
        while( step() ) count++;
98
 
        reset();
99
 
        return count;
100
 
}
101
 
 
102
 
 
103
87
sqlite::query::iterator::iterator(
104
88
        sqlite::query &query,
105
 
        bool step )
 
89
        bool valid )
106
90
        :
107
91
        _query( query )
108
92
{
109
 
        if( step ) increment();
 
93
        if( valid )
 
94
                increment();
 
95
        else
 
96
                _valid = false;
110
97
}
111
98
 
112
99
 
113
100
sqlite::row sqlite::query::iterator::dereference() const
114
101
{
115
 
        return _row;
 
102
        return sqlite::row( _query._handle, _query._next_row++ );
116
103
}
117
104
 
118
105
 
119
106
void sqlite::query::iterator::increment()
120
107
{
121
 
        _row = _query.step();
 
108
        _valid = _query.step();
122
109
}
123
110
 
124
111
 
126
113
        sqlite::query::iterator const &other )
127
114
        const
128
115
{
129
 
        return _row == other._row;
 
116
        return _valid == other._valid;
130
117
}
131
118
 
132
119
 
133
120
sqlite::query::iterator sqlite::query::begin()
134
121
{
135
 
        reset();
136
122
        return sqlite::query::iterator( *this, true );
137
123
}
138
124
 
142
128
        return sqlite::query::iterator( *this, false );
143
129
}
144
130
 
 
131
 
 
132
template< >
 
133
sqlite::query &sqlite::query::operator <<
 
134
        < sqlite::detail::null_t >(
 
135
        const sqlite::detail::null_t & )
 
136
{
 
137
        int code = bind_null( _bind_index );
 
138
        if( code != SQLITE_OK ) throw sqlite_error( _database, code );
 
139
        _bind_index++;
 
140
        return *this;
 
141
}
 
142
 
 
143
 
 
144
template< >
 
145
sqlite::query &sqlite::query::operator <<
 
146
        < sqlite::detail::set_index_t >(
 
147
        const sqlite::detail::set_index_t &t )
 
148
{
 
149
        _bind_index = t._index;
 
150
        return *this;
 
151
}