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