MySQL++ Reference Manual

MySQL++: result.h Source File

MySQL++ 3.3.0

result.h
Go to the documentation of this file.
1
5 /***********************************************************************
6 Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
7 (c) 2004-2007 by Educational Technology Resources, Inc. Others may
8 also hold copyrights on code in this file. See the CREDITS.txt file
9 in the top directory of the distribution for details.
10
11 This file is part of MySQL++.
12
13 MySQL++ is free software; you can redistribute it and/or modify it
14 under the terms of the GNU Lesser General Public License as published
15 by the Free Software Foundation; either version 2.1 of the License, or
16 (at your option) any later version.
17
18 MySQL++ is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21 License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with MySQL++; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
26 USA
27 ***********************************************************************/
28
29 #if !defined(MYSQLPP_RESULT_H)
30 #define MYSQLPP_RESULT_H
31
32 #include "common.h"
33
34 #include "exceptions.h"
35 #include "field.h"
36 #include "field_names.h"
37 #include "field_types.h"
38 #include "noexceptions.h"
39 #include "refcounted.h"
40 #include "row.h"
41
42 namespace mysqlpp {
43
44
47
48 class MYSQLPP_EXPORT SimpleResult
49 {
50 private:
55 typedef bool SimpleResult::*private_bool_type;
56
57 public:
59 SimpleResult() :
60 copacetic_(false),
61 insert_id_(0),
62 rows_(0)
63 {
64 }
65
67 SimpleResult(bool copacetic, ulonglong insert_id,
68 ulonglong rows, const std::string& info) :
69 copacetic_(copacetic),
70 insert_id_(insert_id),
71 rows_(rows),
72 info_(info)
73 {
74 }
75
82 operator private_bool_type() const
83 {
84 return copacetic_ ? &SimpleResult::copacetic_ : 0;
85 }
86
88 ulonglong insert_id() const { return insert_id_; }
89
91 ulonglong rows() const { return rows_; }
92
95 const char* info() const { return info_.c_str(); }
96
97 private:
98 bool copacetic_;
99 ulonglong insert_id_;
100 ulonglong rows_;
101 std::string info_;
102 };
103
104
109
110 class MYSQLPP_EXPORT ResultBase : public OptionalExceptions
111 {
112 public:
114 virtual ~ResultBase() { }
115
117 const Field& fetch_field() const
118 { return fields_.at(current_field_++); }
119
121 const Field& fetch_field(Fields::size_type i) const
122 { return fields_.at(i); }
123
125 const Field& field(unsigned int i) const { return fields_.at(i); }
126
128 const Fields& fields() const { return fields_; }
129
131 const std::string& field_name(int i) const
132 { return names_->at(i); }
133
135 const RefCountedPointer<FieldNames>& field_names() const
136 { return names_; }
137
141 int field_num(const std::string&) const;
142
144 const FieldTypes::value_type& field_type(int i) const
145 { return types_->at(i); }
146
149 const RefCountedPointer<FieldTypes>& field_types() const
150 { return types_; }
151
153 size_t num_fields() const { return fields_.size(); }
154
156 const char* table() const
157 { return fields_.empty() ? "" : fields_[0].table(); }
158
159 protected:
161 ResultBase() :
162 driver_(0),
163 current_field_(0)
164 {
165 }
166
168 ResultBase(MYSQL_RES* result, DBDriver* dbd, bool te = true);
169
171 ResultBase(const ResultBase& other) :
172 OptionalExceptions()
173 {
174 copy(other);
175 }
176
178 ResultBase& copy(const ResultBase& other);
179
180 DBDriver* driver_;
181 Fields fields_;
182
184 RefCountedPointer<FieldNames> names_;
185
187 RefCountedPointer<FieldTypes> types_;
188
196 mutable Fields::size_type current_field_;
197 };
198
199
208
209 class MYSQLPP_EXPORT StoreQueryResult :
210 public ResultBase,
211 public std::vector<Row>
212 {
213 private:
218 typedef bool StoreQueryResult::*private_bool_type;
219
220 public:
221 typedef std::vector<Row> list_type;
222
224 StoreQueryResult() :
225 ResultBase(),
226 copacetic_(false)
227 {
228 }
229
231 StoreQueryResult(MYSQL_RES* result, DBDriver* dbd, bool te = true);
232
235 StoreQueryResult(const StoreQueryResult& other) :
236 ResultBase(),
237 std::vector<Row>(),
238 copacetic_(false)
239 {
240 copy(other);
241 }
242
244 ~StoreQueryResult() { }
245
247 list_type::size_type num_rows() const { return size(); }
248
251 StoreQueryResult& operator =(const StoreQueryResult& rhs)
252 { return this != &rhs ? copy(rhs) : *this; }
253
260 operator private_bool_type() const
261 {
262 return copacetic_ ? &StoreQueryResult::copacetic_ : 0;
263 }
264
265 private:
268 StoreQueryResult& copy(const StoreQueryResult& other);
269
270 bool copacetic_;
271 };
272
273
280 template <>
281 struct RefCountedPointerDestroyer<MYSQL_RES>
282 {
284 void operator()(MYSQL_RES* doomed) const
285 {
286 if (doomed) {
287 mysql_free_result(doomed);
288 }
289 }
290 };
291
292
297
298 class MYSQLPP_EXPORT UseQueryResult : public ResultBase
299 {
300 public:
302 UseQueryResult() :
303 ResultBase()
304 {
305 }
306
308 UseQueryResult(MYSQL_RES* result, DBDriver* dbd, bool te = true);
309
311 UseQueryResult(const UseQueryResult& other) :
312 ResultBase()
313 {
314 copy(other);
315 }
316
318 ~UseQueryResult() { }
319
321 UseQueryResult& operator =(const UseQueryResult& rhs)
322 { return this != &rhs ? copy(rhs) : *this; }
323
325 const Field& fetch_field() const
326 { return fields_.at(current_field_++); }
327
329 const Field& fetch_field(Fields::size_type i) const
330 { return fields_.at(i); }
331
337 const unsigned long* fetch_lengths() const;
338
346 Row fetch_row() const;
347
354 MYSQL_ROW fetch_raw_row() const;
355
360 void field_seek(Fields::size_type field) const
361 { current_field_ = field; }
362
374 // it was successful:
385 operator MYSQL_RES*() const { return result_.raw(); }
386
387 private:
389 UseQueryResult& copy(const UseQueryResult& other);
390
401 mutable RefCountedPointer<MYSQL_RES> result_;
402 };
403
404
406 inline void
407 swap(StoreQueryResult& x, StoreQueryResult& y)
408 {
409 StoreQueryResult tmp = x;
410 x = y;
411 y = tmp;
412 }
413
415 inline void
416 swap(UseQueryResult& x, UseQueryResult& y)
417 {
418 UseQueryResult tmp = x;
419 x = y;
420 y = tmp;
421 }
422
423 } // end namespace mysqlpp
424
425 #endif // !defined(MYSQLPP_RESULT_H)
mysqlpp::DBDriver
Provides a thin abstraction layer over the underlying database client library.
Definition: dbdriver.h:58
mysqlpp::Field
Class to hold information about a SQL field.
Definition: field.h:47
mysqlpp::OptionalExceptions
Interface allowing a class to have optional exceptions.
Definition: noexceptions.h:72
mysqlpp::RefCountedPointer
Creates an object that acts as a reference-counted pointer to another object.
Definition: refcounted.h:83
mysqlpp::ResultBase
Base class for StoreQueryResult and UseQueryResult.
Definition: result.h:111
mysqlpp::ResultBase::field_name
const std::string & field_name(int i) const
Get the name of the field at the given index.
Definition: result.h:131
mysqlpp::ResultBase::ResultBase
ResultBase()
Create empty object.
Definition: result.h:161
mysqlpp::ResultBase::field
const Field & field(unsigned int i) const
Get the underlying Field structure given its index.
Definition: result.h:125
mysqlpp::ResultBase::field_names
const RefCountedPointer< FieldNames > & field_names() const
Get the names of the fields within this result set.
Definition: result.h:135
mysqlpp::ResultBase::current_field_
Fields::size_type current_field_
Default field index used by fetch_field()
Definition: result.h:196
mysqlpp::ResultBase::fields
const Fields & fields() const
Get the underlying Fields structure.
Definition: result.h:128
mysqlpp::ResultBase::names_
RefCountedPointer< FieldNames > names_
list of field names in result
Definition: result.h:184
mysqlpp::ResultBase::field_type
const FieldTypes::value_type & field_type(int i) const
Get the type of a particular field within this result set.
Definition: result.h:144
mysqlpp::ResultBase::types_
RefCountedPointer< FieldTypes > types_
list of field types in result
Definition: result.h:187
mysqlpp::ResultBase::fields_
Fields fields_
list of fields in result
Definition: result.h:181
mysqlpp::ResultBase::~ResultBase
virtual ~ResultBase()
Destroy object.
Definition: result.h:114
mysqlpp::ResultBase::fetch_field
const Field & fetch_field() const
Returns the next field in this result set.
Definition: result.h:117
mysqlpp::ResultBase::fetch_field
const Field & fetch_field(Fields::size_type i) const
Returns the given field in this result set.
Definition: result.h:121
mysqlpp::ResultBase::field_types
const RefCountedPointer< FieldTypes > & field_types() const
Get a list of the types of the fields within this result set.
Definition: result.h:149
mysqlpp::ResultBase::ResultBase
ResultBase(const ResultBase &other)
Create object as a copy of another ResultBase.
Definition: result.h:171
mysqlpp::ResultBase::num_fields
size_t num_fields() const
Returns the number of fields in this result set.
Definition: result.h:153
mysqlpp::ResultBase::driver_
DBDriver * driver_
Access to DB driver; fully initted if nonzero.
Definition: result.h:180
mysqlpp::ResultBase::table
const char * table() const
Return the name of the table the result set comes from.
Definition: result.h:156
mysqlpp::Row
Manages rows from a result set.
Definition: row.h:64
mysqlpp::SimpleResult
Holds information about the result of queries that don't return rows.
Definition: result.h:49
mysqlpp::SimpleResult::insert_id
ulonglong insert_id() const
Get the last value used for an AUTO_INCREMENT field.
Definition: result.h:88
mysqlpp::SimpleResult::SimpleResult
SimpleResult(bool copacetic, ulonglong insert_id, ulonglong rows, const std::string &info)
Initialize object.
Definition: result.h:67
mysqlpp::SimpleResult::info
const char * info() const
Get any additional information about the query returned by the server.
Definition: result.h:95
mysqlpp::SimpleResult::SimpleResult
SimpleResult()
Default ctor.
Definition: result.h:59
mysqlpp::SimpleResult::rows
ulonglong rows() const
Get the number of rows affected by the query.
Definition: result.h:91
mysqlpp::StoreQueryResult
StoreQueryResult set type for "store" queries.
Definition: result.h:212
mysqlpp::StoreQueryResult::StoreQueryResult
StoreQueryResult()
Default constructor.
Definition: result.h:224
mysqlpp::StoreQueryResult::StoreQueryResult
StoreQueryResult(const StoreQueryResult &other)
Initialize object as a copy of another StoreQueryResult object.
Definition: result.h:235
mysqlpp::StoreQueryResult::num_rows
list_type::size_type num_rows() const
Returns the number of rows in this result set.
Definition: result.h:247
mysqlpp::StoreQueryResult::~StoreQueryResult
~StoreQueryResult()
Destroy result set.
Definition: result.h:244
mysqlpp::StoreQueryResult::list_type
std::vector< Row > list_type
type of vector base class
Definition: result.h:221
mysqlpp::UseQueryResult
StoreQueryResult set type for "use" queries.
Definition: result.h:299
mysqlpp::UseQueryResult::field_seek
void field_seek(Fields::size_type field) const
Jumps to the given field within the result set.
Definition: result.h:360
mysqlpp::UseQueryResult::~UseQueryResult
~UseQueryResult()
Destroy object.
Definition: result.h:318
mysqlpp::UseQueryResult::fetch_field
const Field & fetch_field(Fields::size_type i) const
Returns the given field in this result set.
Definition: result.h:329
mysqlpp::UseQueryResult::UseQueryResult
UseQueryResult(const UseQueryResult &other)
Create a copy of another UseQueryResult object.
Definition: result.h:311
mysqlpp::UseQueryResult::fetch_field
const Field & fetch_field() const
Returns the next field in this result set.
Definition: result.h:325
mysqlpp::UseQueryResult::UseQueryResult
UseQueryResult()
Default constructor.
Definition: result.h:302
common.h
This file includes top-level definitions for use both internal to the library, and outside it....
exceptions.h
Declares the MySQL++-specific exception classes.
field.h
Declares the Field and Fields classes.
mysqlpp::Fields
std::vector< Field > Fields
The list-of-Fields type.
Definition: field.h:153
field_names.h
Declares a class to hold a list of field names.
field_types.h
Declares a class to hold a list of SQL field type info.
noexceptions.h
Declares interface that allows exceptions to be optional.
refcounted.h
Declares the RefCountedPointer template.
mysqlpp::swap
void swap(StoreQueryResult &x, StoreQueryResult &y)
Swaps two StoreQueryResult objects.
Definition: result.h:407
row.h
Declares the classes for holding row data from a result set.
mysqlpp::RefCountedPointerDestroyer< MYSQL_RES >::operator()
void operator()(MYSQL_RES *doomed) const
Functor implementation.
Definition: result.h:284
mysqlpp::RefCountedPointerDestroyer
Functor to call delete on the pointer you pass to it.
Definition: refcounted.h:48