/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/row.h

  • Committer: edam
  • Date: 2010-07-29 20:56:19 UTC
  • Revision ID: edam@waxworlds.org-20100729205619-a9yo4uzu647nvnsj
- renamed database to connection to better identify what it is (would database_connection be better though?)
- moved basic_statement and basic_transaction to sqlite::detail
- made sqlite::threadsafe() return the threading mode int, not a bool
- renamed row::index() to row_number() so it isn't confused with column index
- added typedef for deferred_transaction
- added early rollback method to transaction_guard
- allowed transaction_guard::~transaction_guard() to throw exceptions, since when it needs to, we're really screwed anyway
- bugfix: query::reset() didn't reset the internal row counter
- added query::num_results()
- added docs/design-notes
- reimplemented query::iterator so that increment() (which performs a step() on the query) now caches the returned row to be returned during dereference() (previously it stashed details and returned new row!?)
- bugfix: resetting active queries during rollbacks would hang!

Show diffs side-by-side

added added

removed removed

24
24
#define SQLITE3CC_ROW_H_
25
25
 
26
26
 
27
 
#include <sqlite3cc/query.h>
28
27
#include <sqlite3.h>
29
28
#include <boost/utility.hpp>
30
29
#include <boost/lexical_cast.hpp>
37
36
{
38
37
 
39
38
 
40
 
struct _null_t;
41
 
struct _set_index_t;
 
39
class query;
 
40
namespace detail {
 
41
        struct null_t;
 
42
        struct set_index_t;
 
43
}
42
44
 
43
45
 
44
46
/**
54
56
//                                                                 instantiation
55
57
protected:
56
58
 
 
59
        friend class query;
 
60
 
57
61
        /**
58
 
         * Constructor that provides the database that we were extracted from.
59
 
         * @param the query that this row belongs to
60
 
         * @oaram true if this row represents there being no more rows
 
62
         * Constructor that produces a valid row.
 
63
         * @param handle of the statement (query) that created this row
 
64
         * @oaram row_number the index of this row
61
65
         */
62
66
        explicit row(
63
 
                query &query,
64
 
                bool valid = true );
 
67
                sqlite3_stmt *handle,
 
68
                unsigned long long row_number );
 
69
 
 
70
        /**
 
71
         * Constructor that produces an invalid row.
 
72
         */
 
73
        explicit row();
65
74
 
66
75
//______________________________________________________________________________
67
76
//                                                              public interface
71
80
         * Determine if this row is valid or not. If it is not valid, there are no
72
81
         * more rows in the results of the query.
73
82
         */
74
 
        inline operator bool()
 
83
        operator bool() const;
 
84
 
 
85
        /**
 
86
         * get the index in to the results that is this row
 
87
         * @return index
 
88
         */
 
89
        inline unsigned long long row_number()
75
90
        {
76
 
                return _valid;
 
91
                return _row_number;
77
92
        }
78
93
 
79
94
        /**
95
110
                unsigned int index );
96
111
 
97
112
        /**
98
 
         * Get a value from the row
 
113
         * Get a value from the row.
99
114
         * @param index column index
100
115
         * @param value reference to object to set with the value
101
116
         * @see sqlite3_column_*()
105
120
                unsigned int index,
106
121
                T &value )
107
122
        {
108
 
                assert( index < _query.column_count() );
 
123
                assert( index <
 
124
                        static_cast< unsigned int >( sqlite3_column_count( _handle ) ) );
109
125
                const char *text = reinterpret_cast< const char * >(
110
 
                        sqlite3_column_text( _query._handle, index ) );
 
126
                        sqlite3_column_text( _handle, index ) );
111
127
                if( text )
112
128
                        value = boost::lexical_cast< T >( text );
113
129
                else
115
131
        }
116
132
 
117
133
        /**
 
134
         * Get a value from the row and return it.
 
135
         * @param index column index
 
136
         * @return the value
 
137
         * @see sqlite3_column_*()
 
138
         */
 
139
        template< class T >
 
140
        T column(
 
141
                unsigned int index )
 
142
        {
 
143
                T value;
 
144
                column( index, value );
 
145
                return value;
 
146
        }
 
147
 
 
148
        /**
118
149
         * Stream operator is used to obtain values from a result row, fetching from
119
150
         * each column in turn. In addition, the null and set_index() auto-column-
120
151
         * getting manipulators can be used.
133
164
         * Stream operator for use with set_index().
134
165
         */
135
166
        row &operator >>(
136
 
                _set_index_t t );
 
167
                detail::set_index_t t );
137
168
 
138
169
        /**
139
 
         * Check of this row is valid
140
 
         * @return true if it is
 
170
         * Test to see if two rows are the same
 
171
         * @param other the row to compare this one to
 
172
         * @return true if they are
141
173
         */
142
 
        operator bool() const
143
 
        {
144
 
                return _valid;
145
 
        }
 
174
        bool operator ==(
 
175
                const row &other )
 
176
                const;
146
177
 
147
178
//______________________________________________________________________________
148
179
//                                                                implementation
149
180
protected:
150
181
 
151
 
        friend class query;
152
 
 
153
 
        /** the parent query */
154
 
        query &_query;
 
182
        /** the query's handle, or NULL */
 
183
        sqlite3_stmt *_handle;
155
184
 
156
185
private:
157
186
 
158
187
        /** index used when auto-column-getting */
159
188
        unsigned int _column_index;
160
189
 
161
 
        /** is this row valid? */
162
 
        bool _valid;
 
190
        /** the index of this row */
 
191
        unsigned long long _row_number;
163
192
 
164
193
};
165
194
 
166
195
 
167
196
// template specialisations
168
197
template< >
169
 
row &row::operator >> < _null_t >(
170
 
        _null_t & );
171
 
 
172
 
 
173
 
} // namespace row
 
198
row &row::operator >> < detail::null_t >(
 
199
        detail::null_t & );
 
200
 
 
201
 
 
202
} // namespace sqlite
174
203
 
175
204
 
176
205
#endif /* SQLITE3CC_ROW_H_ */