bzr branch
http://bzr.ed.am/sqlite3cc
2
by edam
- further initial development |
1 |
/* |
2 |
* row.h |
|
3 |
* |
|
4 |
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org> |
|
5 |
* |
|
6 |
* This file is part of sqlite3cc (hereafter referred to as "this program"). |
|
7 |
* See http://www.waxworlds.org/edam/software/sqlite3cc for more information. |
|
8 |
* |
|
9 |
* This program is free software: you can redistribute it and/or modify |
|
10 |
* it under the terms of the GNU Lesser General Public License as published |
|
11 |
* by the Free Software Foundation, either version 3 of the License, or |
|
12 |
* (at your option) any later version. |
|
13 |
* |
|
14 |
* This program is distributed in the hope that it will be useful, |
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU Lesser General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU Lesser General Public License |
|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
*/ |
|
22 |
||
23 |
#ifndef SQLITE3CC_ROW_H_ |
|
24 |
#define SQLITE3CC_ROW_H_ |
|
25 |
||
26 |
||
27 |
#include <sqlite3.h> |
|
28 |
#include <boost/utility.hpp> |
|
29 |
#include <boost/lexical_cast.hpp> |
|
30 |
#include <boost/utility/value_init.hpp> |
|
31 |
#include <cassert> |
|
32 |
#include <iostream> |
|
33 |
||
34 |
||
35 |
namespace sqlite |
|
36 |
{ |
|
37 |
||
38 |
||
9
by edam
- added NEWS |
39 |
class query; |
12
by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace |
40 |
namespace detail { |
41 |
struct null_t; |
|
42 |
struct set_index_t; |
|
43 |
} |
|
2
by edam
- further initial development |
44 |
|
45 |
||
46 |
/** |
|
47 |
* A result row from a query. |
|
48 |
* |
|
49 |
* The row is only valid until the next call to step() or reset() on the parent |
|
50 |
* query object, or until the parent query object is destructed. This may change |
|
51 |
* in future versions. |
|
52 |
*/ |
|
53 |
class row |
|
54 |
{ |
|
55 |
//______________________________________________________________________________ |
|
56 |
// instantiation |
|
57 |
protected: |
|
58 |
||
9
by edam
- added NEWS |
59 |
friend class query; |
60 |
||
2
by edam
- further initial development |
61 |
/** |
9
by edam
- added NEWS |
62 |
* Constructor that produces a valid row. |
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
63 |
* @param handle of the statement (query) that created this row |
64 |
* @oaram row_number the index of this row |
|
2
by edam
- further initial development |
65 |
*/ |
66 |
explicit row( |
|
9
by edam
- added NEWS |
67 |
sqlite3_stmt *handle, |
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
68 |
unsigned long long row_number ); |
9
by edam
- added NEWS |
69 |
|
70 |
/** |
|
71 |
* Constructor that produces an invalid row. |
|
72 |
*/ |
|
73 |
explicit row(); |
|
2
by edam
- further initial development |
74 |
|
75 |
//______________________________________________________________________________ |
|
76 |
// public interface |
|
77 |
public: |
|
78 |
||
79 |
/** |
|
80 |
* Determine if this row is valid or not. If it is not valid, there are no |
|
81 |
* more rows in the results of the query. |
|
82 |
*/ |
|
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
83 |
operator bool() const; |
9
by edam
- added NEWS |
84 |
|
85 |
/** |
|
86 |
* get the index in to the results that is this row |
|
87 |
* @return index |
|
88 |
*/ |
|
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
89 |
inline unsigned long long row_number() |
9
by edam
- added NEWS |
90 |
{ |
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
91 |
return _row_number; |
2
by edam
- further initial development |
92 |
} |
93 |
||
94 |
/** |
|
95 |
* Ascertain a column's type. |
|
96 |
* @param index column index |
|
97 |
* @return sqlite datatype code |
|
98 |
* @see sqlite3_column_type() |
|
99 |
*/ |
|
100 |
int column_type( |
|
101 |
unsigned int index ); |
|
102 |
||
103 |
/** |
|
104 |
* get the number of bytes in the result for a given column. |
|
105 |
* @param index column index |
|
106 |
* @return number of bytes in result |
|
107 |
* @see sqlite3_column_bytes() |
|
108 |
*/ |
|
109 |
unsigned int column_bytes( |
|
110 |
unsigned int index ); |
|
111 |
||
112 |
/** |
|
9
by edam
- added NEWS |
113 |
* Get a value from the row. |
2
by edam
- further initial development |
114 |
* @param index column index |
115 |
* @param value reference to object to set with the value |
|
116 |
* @see sqlite3_column_*() |
|
117 |
*/ |
|
118 |
template< class T > |
|
119 |
void column( |
|
120 |
unsigned int index, |
|
121 |
T &value ) |
|
122 |
{ |
|
9
by edam
- added NEWS |
123 |
assert( index < |
124 |
static_cast< unsigned int >( sqlite3_column_count( _handle ) ) ); |
|
2
by edam
- further initial development |
125 |
const char *text = reinterpret_cast< const char * >( |
9
by edam
- added NEWS |
126 |
sqlite3_column_text( _handle, index ) ); |
2
by edam
- further initial development |
127 |
if( text ) |
128 |
value = boost::lexical_cast< T >( text ); |
|
129 |
else |
|
130 |
value = boost::get( boost::value_initialized< T >() ); |
|
131 |
} |
|
132 |
||
133 |
/** |
|
9
by edam
- added NEWS |
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 |
/** |
|
2
by edam
- further initial development |
149 |
* Stream operator is used to obtain values from a result row, fetching from |
150 |
* each column in turn. In addition, the null and set_index() auto-column- |
|
151 |
* getting manipulators can be used. |
|
152 |
* @param value is a variable to store the retrieved data in |
|
153 |
*/ |
|
154 |
template< class T > |
|
155 |
row &operator >>( |
|
156 |
T &value ) |
|
157 |
{ |
|
158 |
column( _column_index, value ); |
|
159 |
_column_index++; |
|
160 |
return *this; |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Stream operator for use with set_index(). |
|
165 |
*/ |
|
166 |
row &operator >>( |
|
12
by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace |
167 |
detail::set_index_t t ); |
2
by edam
- further initial development |
168 |
|
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
169 |
/** |
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 |
|
173 |
*/ |
|
174 |
bool operator ==( |
|
175 |
const row &other ) |
|
176 |
const; |
|
177 |
||
2
by edam
- further initial development |
178 |
//______________________________________________________________________________ |
179 |
// implementation |
|
180 |
protected: |
|
181 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
182 |
/** the query's handle, or NULL */ |
9
by edam
- added NEWS |
183 |
sqlite3_stmt *_handle; |
2
by edam
- further initial development |
184 |
|
185 |
private: |
|
186 |
||
187 |
/** index used when auto-column-getting */ |
|
188 |
unsigned int _column_index; |
|
189 |
||
10
by edam
- cleaned up test-main |
190 |
/** the index of this row */ |
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
191 |
unsigned long long _row_number; |
2
by edam
- further initial development |
192 |
|
193 |
}; |
|
194 |
||
195 |
||
196 |
// template specialisations |
|
197 |
template< > |
|
12
by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace |
198 |
row &row::operator >> < detail::null_t >( |
199 |
detail::null_t & ); |
|
2
by edam
- further initial development |
200 |
|
201 |
||
9
by edam
- added NEWS |
202 |
} // namespace sqlite |
2
by edam
- further initial development |
203 |
|
204 |
||
205 |
#endif /* SQLITE3CC_ROW_H_ */ |