/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 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

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 the handle of the statement (query) that created this row
 
64
         * @oaram the result index that is this row
61
65
         */
62
66
        explicit row(
63
 
                query &query,
64
 
                bool valid = true );
 
67
                sqlite3_stmt *handle,
 
68
                unsigned long long index );
 
69
 
 
70
        /**
 
71
         * Constructor that produces an invalid row.
 
72
         * @param the handle of the statement (query) that created this row
 
73
         */
 
74
        explicit row();
65
75
 
66
76
//______________________________________________________________________________
67
77
//                                                              public interface
71
81
         * Determine if this row is valid or not. If it is not valid, there are no
72
82
         * more rows in the results of the query.
73
83
         */
74
 
        inline operator bool()
75
 
        {
76
 
                return _valid;
 
84
        inline operator bool() const
 
85
        {
 
86
                return _index != std::numeric_limits< unsigned long long >::max();
 
87
        }
 
88
 
 
89
        /**
 
90
         * get the index in to the results that is this row
 
91
         * @return index
 
92
         */
 
93
        inline unsigned long long index()
 
94
        {
 
95
                return _index;
77
96
        }
78
97
 
79
98
        /**
95
114
                unsigned int index );
96
115
 
97
116
        /**
98
 
         * Get a value from the row
 
117
         * Get a value from the row.
99
118
         * @param index column index
100
119
         * @param value reference to object to set with the value
101
120
         * @see sqlite3_column_*()
105
124
                unsigned int index,
106
125
                T &value )
107
126
        {
108
 
                assert( index < _query.column_count() );
 
127
                assert( index <
 
128
                        static_cast< unsigned int >( sqlite3_column_count( _handle ) ) );
109
129
                const char *text = reinterpret_cast< const char * >(
110
 
                        sqlite3_column_text( _query._handle, index ) );
 
130
                        sqlite3_column_text( _handle, index ) );
111
131
                if( text )
112
132
                        value = boost::lexical_cast< T >( text );
113
133
                else
115
135
        }
116
136
 
117
137
        /**
 
138
         * Get a value from the row and return it.
 
139
         * @param index column index
 
140
         * @return the value
 
141
         * @see sqlite3_column_*()
 
142
         */
 
143
        template< class T >
 
144
        T column(
 
145
                unsigned int index )
 
146
        {
 
147
                T value;
 
148
                column( index, value );
 
149
                return value;
 
150
        }
 
151
 
 
152
        /**
118
153
         * Stream operator is used to obtain values from a result row, fetching from
119
154
         * each column in turn. In addition, the null and set_index() auto-column-
120
155
         * getting manipulators can be used.
133
168
         * Stream operator for use with set_index().
134
169
         */
135
170
        row &operator >>(
136
 
                _set_index_t t );
137
 
 
138
 
        /**
139
 
         * Check of this row is valid
140
 
         * @return true if it is
141
 
         */
142
 
        operator bool() const
143
 
        {
144
 
                return _valid;
145
 
        }
 
171
                detail::set_index_t t );
146
172
 
147
173
//______________________________________________________________________________
148
174
//                                                                implementation
149
175
protected:
150
176
 
151
 
        friend class query;
152
 
 
153
 
        /** the parent query */
154
 
        query &_query;
 
177
        /** the query's handle */
 
178
        sqlite3_stmt *_handle;
155
179
 
156
180
private:
157
181
 
158
182
        /** index used when auto-column-getting */
159
183
        unsigned int _column_index;
160
184
 
161
 
        /** is this row valid? */
162
 
        bool _valid;
 
185
        /** the index of this row */
 
186
        unsigned long long _index;
163
187
 
164
188
};
165
189
 
166
190
 
167
191
// template specialisations
168
192
template< >
169
 
row &row::operator >> < _null_t >(
170
 
        _null_t & );
171
 
 
172
 
 
173
 
} // namespace row
 
193
row &row::operator >> < detail::null_t >(
 
194
        detail::null_t & );
 
195
 
 
196
 
 
197
} // namespace sqlite
174
198
 
175
199
 
176
200
#endif /* SQLITE3CC_ROW_H_ */