summaryrefslogtreecommitdiff
path: root/include/sqlite3xx/result.hpp
blob: 77d1938e01f9247803acae4c1620790e785745bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 *  sqlite3xx - sqlite3 C++ layer, following the ideas of libpqxx
 *  Copyright (C) 2009  Andreas Baumann
 *
 *  This copyrighted material is made available to anyone wishing to use,
 *  modify, copy, or redistribute it subject to the terms and conditions of
 *  the GNU Lesser General Public License, as published by the Free Software
 *  Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 *  License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this distribution; if not, write to:
 *  Free Software Foundation, Inc.
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02110-1301  USA
 *
 */

#ifndef SQLITE3XX_RESULT_H
#define SQLITE3XX_RESULT_H

#include "sqlite3.h"

#include <iostream>
#include <map>
#include <vector>
#include <stdexcept>
#include <sstream>

#include "port/iterator.hpp"
#include "port/dllexport.h"

using namespace std;

namespace sqlite3xx {

class SQLITEXX_LIBEXPORT result {
	public:
		class field;
		class const_iterator;

		typedef unsigned long size_type;
		typedef signed long difference_type;

	class SQLITEXX_LIBEXPORT tuple {
		public:
			typedef unsigned int size_type;

		private:
			const result *_r;
			result::size_type _i;

		public:
			tuple( const result *r, size_type i ) :
				_r( r ), _i( i ) { }
			~tuple( ) { }

			size_type size( ) const {
				return _r->columns( );
			}

			field operator[]( size_type i ) const {
				return field( *this, i );
			}
			field operator[]( int i ) const {
				return operator[]( (size_type)i );
			}
			field operator[]( string s ) const {
				return field( *this, _r->column_number( s ) );
			}

			int column_type( size_type i ) const {
				return _r->column_type( i );
			}

			int GetValueInt( size_type i ) const {
				return _r->GetValueInt( i );
			}

			const unsigned char* GetValueText( size_type i ) const {
				return _r->GetValueText( i );
			}

			double GetValueDouble( size_type i ) const {
				return _r->GetValueDouble( i );
			}

			size_type rownumber( ) const throw( ) {
				return _i;
			}

		protected:
			tuple( );
	};

	class SQLITEXX_LIBEXPORT field {
		private:
			tuple _t;
			tuple::size_type _c;

			SQLITEXX_PRIVATE string type_to_str( const int type ) const {
				switch( type ) {
					case SQLITE_INTEGER: 	return "SQLITE_INTEGER";
					case SQLITE_FLOAT:	return "SQLITE_FLOAT";
					case SQLITE_BLOB:	return "SQLITE_BLOB";
					case SQLITE_NULL:	return "SQLITE_NULL";
					case SQLITE3_TEXT:	return "SQLITE_TEXT";
					default:		return "<unknown type>";
				}
			}

		public:
			field( const tuple& t, tuple::size_type c ) :
				_t( t ), _c( c ) { }

			SQLITEXX_LIBEXPORT friend ostream& operator<<( ostream& o, const field& f );

			void GetValueOfType( int type, int& obj ) const {
				if( type == SQLITE_INTEGER ) {
					obj = _t.GetValueInt( _c );
				} else if( type == SQLITE_NULL ) {
					obj = 0;
				} else {
					ostringstream oss;
					oss 	<< "Invalid type conversion requested for integer type, type is "
						<< type_to_str( type ) << "(" << type << ")";
					throw invalid_argument( oss.str( ) );
				}
			}

			void GetValueOfType( int type, string& obj ) const {
				if( type == SQLITE3_TEXT ) {
					obj = string( (const char *)_t.GetValueText( _c ) );
				} else if( type == SQLITE_NULL ) {
					obj = string( );
				} else {
					ostringstream oss;
					oss 	<< "Invalid type conversion requested for string type, type is "
						<< type_to_str( type ) << "(" << type << ")";
					throw invalid_argument( oss.str( ) );
				}
			}

			void GetValueOfType( int type, double& obj ) const {
				if( type == SQLITE_FLOAT ) {
					obj = _t.GetValueDouble( _c );
				} else if( type == SQLITE_NULL ) {
					obj = 0.0;
				} else {
					ostringstream oss;
					oss 	<< "Invalid type conversion requested for double type, type is "
						<< type_to_str( type ) << "(" << type << ")";
					throw invalid_argument( oss.str( ) );
				}
			}

			template<typename T> bool to( T& obj ) const {
				int type = _t.column_type( _c );
				GetValueOfType( type, obj );
				return true;
			}
		};

	typedef sqlite3xxstd::iterator< std::random_access_iterator_tag,
		const tuple,
		result::difference_type,
		const_iterator,
		tuple>
	const_iterator_base;

	class SQLITEXX_LIBEXPORT const_iterator :
		public const_iterator_base,
		public tuple {
		
		public:
			typedef const tuple *pointer;

			const_iterator( ) throw( ) : tuple( 0, 0 ) { }
			const_iterator( const tuple& t ) throw( )
				: tuple( t ) { }

			pointer operator->( ) const { return this; }
			const_iterator operator++( int incr );
			bool operator<( const const_iterator& i ) const;

		private:
			int dummy;
			friend class result;
			SQLITEXX_PRIVATE const_iterator( const result* r, size_type i ) throw( ) :
				tuple( r, i ) {
			};
	};

	private:
		sqlite3_stmt* _stmt;
		size_type _row;		/* the currently visible row from the cache */
		size_type _crow;	/* the row of the sqlite step cursor */
		typedef map<string, size_type> ColMap;
		ColMap _colmap;
		typedef vector<int> ColType;
		ColType _coltype;

		enum Status {
			st_nascent,	/* created, we must call step */
			st_nodata,	/* a command without result data */
			st_lastrow,	/* a query, on the last row */
                        st_hasdata,	/* a query, we must clean up in the end */
			st_nomoredata   /* a query, with no data left */
		};
		
		class CachedValue {
			public:
				int type;
				union {
					int i;
					double d;
					unsigned char *s;
				} value;
			
			public:
				CachedValue( ) { type = 0; };
				CachedValue( const CachedValue& v );
				~CachedValue( );
				CachedValue& operator= ( const CachedValue& v );
		};
		
		typedef vector<CachedValue> ValueCache;
		ValueCache _cache;

		Status _status;
		
	public:
		result( sqlite3_stmt* stmt );
		result( const result& r );
		~result( );
		result& operator= ( const result& r );

		size_type affected_rows( ) const;
		size_type size( ) const;
		size_type columns( ) const;

		const tuple operator[]( size_type i ) throw( );

		size_type column_number( string name ) const;
		int column_type( size_type i ) const;
		int column_type( string name ) const {
			return column_type( column_number( name ) );
		}

		int GetValueInt( size_type i ) const {
			return _cache[i].value.i;
		}

		const unsigned char* GetValueText( size_type i ) const {
			return _cache[i].value.s;
		}

		double GetValueDouble( size_type i ) const {
			return _cache[i].value.d;
		}

		const_iterator begin( ) const throw( ) {
			return const_iterator( this, 0 );
		}

		/* as size( ) is not reliable, we can't create the correct
		 * tuple with the correct index here
		 */
		const_iterator end( ) const throw( ) {
			return const_iterator( this, size( ) );
		}

	private:
		SQLITEXX_PRIVATE void Step( );
		SQLITEXX_PRIVATE void BufferData( );
		SQLITEXX_PRIVATE void FillColNameMap( );
		SQLITEXX_PRIVATE void FillColTypeMap( );
};

}

#endif /* SQLITE3XX_RESULT_H */