/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

Lines of Context:
24
24
#define SQLITE3CC_QUERY_H_
25
25
 
26
26
 
 
27
#include <boost/iterator/iterator_facade.hpp>
27
28
#include <sqlite3cc/basic_statement.h>
 
29
#include <sqlite3cc/row.h>
28
30
 
29
31
 
30
32
namespace sqlite
31
33
{
32
34
 
33
35
 
34
 
class row;
35
 
 
36
 
 
37
36
class query
38
37
        :
39
38
        public basic_statement
74
73
                const std::string &sql );
75
74
 
76
75
        /**
77
 
         * Peform a step() and return row object that can be used to retrieve the
 
76
         * Perform a step() and return row object that can be used to retrieve the
78
77
         * results.
79
78
         * @return a row object
80
79
         */
81
80
        row step();
82
81
 
83
82
        /**
84
 
         * Get the number of columns in the result of
 
83
         * Get the number of columns in the results
 
84
         * @see sqlite3_column_count()
85
85
         */
86
86
        unsigned int column_count();
87
87
 
 
88
        /**
 
89
         * Get the name of a column in the results
 
90
         * @param index column index
 
91
         * @see sqlite3_column_name()
 
92
         */
 
93
        const std::string column_name(
 
94
                unsigned int index );
 
95
 
 
96
        /**
 
97
         * Query iterator which can be used to obtain rows
 
98
         */
 
99
        class iterator
 
100
                :
 
101
                public boost::iterator_facade< iterator, row,
 
102
                        boost::single_pass_traversal_tag, row >
 
103
        {
 
104
        public:
 
105
                iterator();
 
106
                explicit iterator( query &query, bool valid );
 
107
 
 
108
        private:
 
109
                friend class boost::iterator_core_access;
 
110
 
 
111
                row dereference() const;
 
112
                void increment();
 
113
                bool equal( iterator const &other ) const;
 
114
 
 
115
                /** is this iterator still pointing to a valid row? */
 
116
                bool _valid;
 
117
 
 
118
                /** the query */
 
119
                query &_query;
 
120
        };
 
121
 
 
122
        /**
 
123
         * Return an iterator to the initial row. Note that creating an iterator
 
124
         * causes step() to be called, so it should only be called to begin
 
125
         * iterating over the rows and not for comparison.
 
126
         * @return a query iterator
 
127
         */
 
128
        iterator begin();
 
129
 
 
130
        /**
 
131
         * Return an iterator to after the last row (i.e., an invalid row).
 
132
         */
 
133
        iterator end();
 
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
 
 
151
//______________________________________________________________________________
 
152
//                                                                implementation
 
153
private:
 
154
 
 
155
        /** next row index */
 
156
        unsigned long long _next_row;
 
157
 
88
158
};
89
159
 
90
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
 
91
170
} // namespace sqlite
92
171
 
93
172