/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-03-09 14:06:50 UTC
  • Revision ID: edam@waxworlds.org-20100309140650-oqwnsrbajh8d2p2m
- moved dependancy on boost_filesystem-mt from the library to test-main

Show diffs side-by-side

added added

removed removed

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