/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-02-08 22:08:57 UTC
  • Revision ID: edam@waxworlds.org-20100208220857-5up6fi4y6jh5bz4k
- removed "OK" from test-main when test is successful
- aranged for test/test-main to buld properly and be called from "make check"
- added include/Makefile.am to arrange for installation of library headers
- added project website to configure.ac
- added library interface configuration variable to configure.ac

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