/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: 2012-01-23 17:12:43 UTC
  • Revision ID: edam@waxworlds.org-20120123171243-bgqedav41y2x8rlw
added command and query factory methods to connection

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * query.hpp
3
 
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
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.
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.
 
2
 * query.h
 
3
 *
 
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 
5
 *
 
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.
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/>.
21
21
 */
22
22
 
23
 
#ifndef QUERY_HPP_
24
 
#define QUERY_HPP_
25
 
 
26
 
 
27
 
#include <sqlitepp/statement.hpp>
 
23
#ifndef SQLITE3CC_QUERY_H_
 
24
#define SQLITE3CC_QUERY_H_
 
25
 
 
26
 
 
27
#include <boost/iterator/iterator_facade.hpp>
 
28
#include <sqlite3cc/basic_statement.h>
 
29
#include <sqlite3cc/row.h>
28
30
 
29
31
 
30
32
namespace sqlite
33
35
 
34
36
class query
35
37
        :
36
 
        public statement
 
38
        public detail::basic_statement
37
39
{
 
40
//______________________________________________________________________________
 
41
//                                                                 instantiation
 
42
public:
 
43
 
 
44
        /**
 
45
         * Constructor that provides a connection upon which to act and the SQL
 
46
         * query to execute.
 
47
         *
 
48
         * @param connection a reference to a connection
 
49
         * @param sql an SQL statement in UTF-8
 
50
         */
 
51
        explicit query(
 
52
                connection &connection,
 
53
                const std::string &sql );
 
54
 
 
55
        /**
 
56
         * Constructor that provides a connection upon which to act.
 
57
         *
 
58
         * @param connection a reference to a connection
 
59
         */
 
60
        explicit query(
 
61
                connection &connection );
 
62
 
 
63
//______________________________________________________________________________
 
64
//                                                              public interface
 
65
public:
 
66
 
 
67
        /**
 
68
         * Prepare an SQL statement.
 
69
         *
 
70
         * @param sql an SQL statement in UTF-8
 
71
         * @returns an sqlite error code
 
72
         * @see sqlite3_prepare_v2()
 
73
         */
 
74
        virtual int prepare(
 
75
                const std::string &sql );
 
76
 
 
77
        /**
 
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
 
88
         * results.
 
89
         *
 
90
         * @return a row object
 
91
         */
 
92
        row step();
 
93
 
 
94
        /**
 
95
         * Get the number of columns in the results.
 
96
         *
 
97
         * @see sqlite3_column_count()
 
98
         */
 
99
        unsigned int column_count();
 
100
 
 
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
        unsigned long long num_results();
 
120
 
 
121
        /**
 
122
         * Query iterator which can be used to obtain rows.
 
123
         */
 
124
        class iterator
 
125
                :
 
126
                public boost::iterator_facade< iterator, row,
 
127
                        boost::single_pass_traversal_tag, row >
 
128
        {
 
129
        public:
 
130
                explicit iterator( query &query, bool step );
 
131
 
 
132
        private:
 
133
                friend class boost::iterator_core_access;
 
134
 
 
135
                row dereference() const;
 
136
                void increment();
 
137
                bool equal( iterator const &other ) const;
 
138
 
 
139
                /** the current row */
 
140
                row _row;
 
141
 
 
142
                /** the query */
 
143
                query &_query;
 
144
        };
 
145
 
 
146
        /**
 
147
         * Get an iterator to the initial row.  Note that creating an iterator
 
148
         * causes step() to be called, so it should only be called to begin
 
149
         * iterating over the rows and not for comparison.
 
150
         *
 
151
         * @return a query iterator
 
152
         */
 
153
        iterator begin();
 
154
 
 
155
        /**
 
156
         * Get an iterator to after the last row (i.e., an invalid row).
 
157
         *
 
158
         * @return an invalid query iterator
 
159
         */
 
160
        iterator end();
 
161
 
 
162
//______________________________________________________________________________
 
163
//                                                                implementation
 
164
private:
 
165
 
 
166
        /** next row number */
 
167
        unsigned long long _next_row_number;
38
168
 
39
169
};
40
170
 
41
171
 
42
 
}
43
 
 
44
 
 
45
 
#endif /* QUERY_HPP_ */
 
172
} // namespace sqlite
 
173
 
 
174
 
 
175
#endif /* SQLITE3CC_QUERY_H_ */