/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-01-28 12:21:03 UTC
  • Revision ID: edam@waxworlds.org-20100128122103-v3gs6hro5h7ffnhb
- further initial development
- basic support for: queries, rows (query results)
- filename, comment header and project name consistency

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