/sqlite3cc

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

« back to all changes in this revision

Viewing changes to include/sqlite3cc/query.h

  • Committer: Tim Marston
  • Date: 2015-02-26 08:59:36 UTC
  • Revision ID: tim@ed.am-20150226085936-xe43in8bfz6f6nxb
fixed some missing/incorrect includes

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * query.h
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
5
5
 *
6
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
 
 *
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.
 
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.
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/>.
27
27
#include <boost/iterator/iterator_facade.hpp>
28
28
#include <sqlite3cc/basic_statement.h>
29
29
#include <sqlite3cc/row.h>
 
30
#include <string>
30
31
 
31
32
 
32
33
namespace sqlite
35
36
 
36
37
class query
37
38
        :
38
 
        public basic_statement
 
39
        public detail::basic_statement
39
40
{
40
41
//______________________________________________________________________________
41
42
//                                                                 instantiation
42
43
public:
43
44
 
44
45
        /**
45
 
         * Constructor that provides a database upon which to act and the SQL
 
46
         * Constructor that provides a connection upon which to act and the SQL
46
47
         * query to execute.
47
 
         * @param database a reference to a database
 
48
         *
 
49
         * @param connection a reference to a connection
48
50
         * @param sql an SQL statement in UTF-8
49
51
         */
50
52
        explicit query(
51
 
                database &database,
 
53
                connection &connection,
52
54
                const std::string &sql );
53
55
 
54
56
        /**
55
 
         * Constructor that provides a database upon which to act.
56
 
         * @param database a reference to a database
57
 
         * @param sql an SQL statement in UTF-8
 
57
         * Constructor that provides a connection upon which to act.
 
58
         *
 
59
         * @param connection a reference to a connection
58
60
         */
59
61
        explicit query(
60
 
                database &database );
 
62
                connection &connection );
61
63
 
62
64
//______________________________________________________________________________
63
65
//                                                              public interface
65
67
 
66
68
        /**
67
69
         * Prepare an SQL statement.
 
70
         *
68
71
         * @param sql an SQL statement in UTF-8
69
72
         * @returns an sqlite error code
70
73
         * @see sqlite3_prepare_v2()
73
76
                const std::string &sql );
74
77
 
75
78
        /**
 
79
         * Reset the statement, ready to re-execute it.  This does not clear any of
 
80
         * the values bound to the statement.
 
81
         *
 
82
         * @returns an sqlite error code
 
83
         * @see sqlite3_reset()
 
84
         */
 
85
        virtual int reset();
 
86
 
 
87
        /**
76
88
         * Perform a step() and return row object that can be used to retrieve the
77
89
         * results.
 
90
         *
78
91
         * @return a row object
79
92
         */
80
93
        row step();
81
94
 
82
95
        /**
83
 
         * Get the number of columns in the results
 
96
         * Get the number of columns in the results.
 
97
         *
84
98
         * @see sqlite3_column_count()
85
99
         */
86
100
        unsigned int column_count();
87
101
 
88
102
        /**
89
 
         * Get the name of a column in the results
 
103
         * Get the name of a column in the results.
 
104
         *
90
105
         * @param index column index
91
106
         * @see sqlite3_column_name()
92
107
         */
94
109
                unsigned int index );
95
110
 
96
111
        /**
97
 
         * Query iterator which can be used to obtain rows
 
112
         * Gets the number of results.  Be aware that this merely step()s over the
 
113
         * results and counts them, which is something you could do yourself.  This
 
114
         * method is intended to be used for convenience, where you only need know
 
115
         * the number of results and not what they are.  You could also execute some
 
116
         * SQL like this: "SELECT COUNT(*) ...", but this only causes SQLite to
 
117
         * internally do the same counting as this method does.  Also note that this
 
118
         * method reset()s the query.
 
119
         *
 
120
         * @returns the number of results
 
121
         */
 
122
        unsigned long long num_results();
 
123
 
 
124
        /**
 
125
         * Query iterator which can be used to obtain rows.
98
126
         */
99
127
        class iterator
100
128
                :
102
130
                        boost::single_pass_traversal_tag, row >
103
131
        {
104
132
        public:
105
 
                iterator();
106
 
                explicit iterator( query &query, bool valid );
 
133
                explicit iterator( query &query, bool step );
107
134
 
108
135
        private:
109
136
                friend class boost::iterator_core_access;
112
139
                void increment();
113
140
                bool equal( iterator const &other ) const;
114
141
 
115
 
                /** is this iterator still pointing to a valid row? */
116
 
                bool _valid;
 
142
                /** the current row */
 
143
                row _row;
117
144
 
118
145
                /** the query */
119
146
                query &_query;
120
147
        };
121
148
 
122
149
        /**
123
 
         * Return an iterator to the initial row. Note that creating an iterator
 
150
         * Get an iterator to the initial row.  Note that creating an iterator
124
151
         * causes step() to be called, so it should only be called to begin
125
152
         * iterating over the rows and not for comparison.
 
153
         *
126
154
         * @return a query iterator
127
155
         */
128
156
        iterator begin();
129
157
 
130
158
        /**
131
 
         * Return an iterator to after the last row (i.e., an invalid row).
 
159
         * Get an iterator to after the last row (i.e., an invalid row).
 
160
         *
 
161
         * @return an invalid query iterator
132
162
         */
133
163
        iterator end();
134
164
 
136
166
//                                                                implementation
137
167
private:
138
168
 
139
 
        /** next row index */
140
 
        unsigned long long _next_row;
 
169
        /** next row number */
 
170
        unsigned long long _next_row_number;
141
171
 
142
172
};
143
173