/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: edam
  • Date: 2010-07-29 06:39:13 UTC
  • Revision ID: edam@waxworlds.org-20100729063913-wvcvkogsa2alwkhr
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec

Show diffs side-by-side

added added

removed removed

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