result.h
Go to the documentation of this file.
/***********************************************************************
Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
(c) 2004-2007 by Educational Technology Resources, Inc. Others may
also hold copyrights on code in this file. See the CREDITS.txt file
in the top directory of the distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ 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 MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#if !defined(MYSQLPP_RESULT_H)
#define MYSQLPP_RESULT_H
#include "common.h"
#include "exceptions.h"
#include "field.h"
#include "field_names.h"
#include "field_types.h"
#include "noexceptions.h"
#include "refcounted.h"
#include "row.h"
namespace mysqlpp {
class MYSQLPP_EXPORT SimpleResult
{
private:
typedef bool SimpleResult::*private_bool_type;
public:
SimpleResult() :
copacetic_(false),
insert_id_(0),
rows_(0)
{
}
SimpleResult(bool copacetic, ulonglong insert_id,
ulonglong rows, const std::string& info) :
copacetic_(copacetic),
insert_id_(insert_id),
rows_(rows),
info_(info)
{
}
operator private_bool_type() const
{
return copacetic_ ? &SimpleResult::copacetic_ : 0;
}
ulonglong insert_id() const { return insert_id_; }
ulonglong rows() const { return rows_; }
const char* info() const { return info_.c_str(); }
private:
bool copacetic_;
ulonglong insert_id_;
ulonglong rows_;
std::string info_;
};
class MYSQLPP_EXPORT ResultBase : public OptionalExceptions
{
public:
virtual ~ResultBase() { }
const Field& fetch_field() const
{ return fields_.at(current_field_++); }
const Field& fetch_field(Fields::size_type i) const
{ return fields_.at(i); }
const Field& field(unsigned int i) const { return fields_.at(i); }
const Fields& fields() const { return fields_; }
const std::string& field_name(int i) const
{ return names_->at(i); }
const RefCountedPointer<FieldNames>& field_names() const
{ return names_; }
int field_num(const std::string&) const;
const FieldTypes::value_type& field_type(int i) const
{ return types_->at(i); }
const RefCountedPointer<FieldTypes>& field_types() const
{ return types_; }
size_t num_fields() const { return fields_.size(); }
const char* table() const
{ return fields_.empty() ? "" : fields_[0].table(); }
protected:
ResultBase() :
driver_(0),
current_field_(0)
{
}
ResultBase(MYSQL_RES* result, DBDriver* dbd, bool te = true);
ResultBase(const ResultBase& other) :
OptionalExceptions()
{
copy(other);
}
ResultBase& copy(const ResultBase& other);
DBDriver* driver_;
Fields fields_;
RefCountedPointer<FieldNames> names_;
RefCountedPointer<FieldTypes> types_;
mutable Fields::size_type current_field_;
};
class MYSQLPP_EXPORT StoreQueryResult :
public ResultBase,
public std::vector<Row>
{
private:
typedef bool StoreQueryResult::*private_bool_type;
public:
typedef std::vector<Row> list_type;
StoreQueryResult() :
ResultBase(),
copacetic_(false)
{
}
StoreQueryResult(MYSQL_RES* result, DBDriver* dbd, bool te = true);
StoreQueryResult(const StoreQueryResult& other) :
ResultBase(),
std::vector<Row>(),
copacetic_(false)
{
copy(other);
}
~StoreQueryResult() { }
list_type::size_type num_rows() const { return size(); }
StoreQueryResult& operator =(const StoreQueryResult& rhs)
{ return this != &rhs ? copy(rhs) : *this; }
operator private_bool_type() const
{
return copacetic_ ? &StoreQueryResult::copacetic_ : 0;
}
private:
StoreQueryResult& copy(const StoreQueryResult& other);
bool copacetic_;
};
template <>
struct RefCountedPointerDestroyer<MYSQL_RES>
{
void operator()(MYSQL_RES* doomed) const
{
if (doomed) {
mysql_free_result(doomed);
}
}
};
class MYSQLPP_EXPORT UseQueryResult : public ResultBase
{
public:
UseQueryResult() :
ResultBase()
{
}
UseQueryResult(MYSQL_RES* result, DBDriver* dbd, bool te = true);
UseQueryResult(const UseQueryResult& other) :
ResultBase()
{
copy(other);
}
~UseQueryResult() { }
UseQueryResult& operator =(const UseQueryResult& rhs)
{ return this != &rhs ? copy(rhs) : *this; }
const Field& fetch_field() const
{ return fields_.at(current_field_++); }
const Field& fetch_field(Fields::size_type i) const
{ return fields_.at(i); }
const unsigned long* fetch_lengths() const;
Row fetch_row() const;
MYSQL_ROW fetch_raw_row() const;
void field_seek(Fields::size_type field) const
{ current_field_ = field; }
// it was successful:
operator MYSQL_RES*() const { return result_.raw(); }
private:
UseQueryResult& copy(const UseQueryResult& other);
mutable RefCountedPointer<MYSQL_RES> result_;
};
inline void
swap(StoreQueryResult& x, StoreQueryResult& y)
{
StoreQueryResult tmp = x;
x = y;
y = tmp;
}
inline void
swap(UseQueryResult& x, UseQueryResult& y)
{
UseQueryResult tmp = x;
x = y;
y = tmp;
}
} // end namespace mysqlpp
#endif // !defined(MYSQLPP_RESULT_H)
mysqlpp::ResultBase::ResultBase
ResultBase()
Create empty object.
Definition: result.h:161
mysqlpp::StoreQueryResult
StoreQueryResult set type for &quot;store&quot; queries.
Definition: result.h:209
mysqlpp::SimpleResult::SimpleResult
SimpleResult(bool copacetic, ulonglong insert_id, ulonglong rows, const std::string &info)
Initialize object.
Definition: result.h:67
field_types.h
Declares a class to hold a list of SQL field type info.
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::SimpleResult::info
const char * info() const
Get any additional information about the query returned by the server.
Definition: result.h:95
mysqlpp::ResultBase::num_fields
size_t num_fields() const
Returns the number of fields in this result set.
Definition: result.h:153
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::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::ResultBase::~ResultBase
virtual ~ResultBase()
Destroy object.
Definition: result.h:114
mysqlpp::RefCountedPointerDestroyer< MYSQL_RES >::operator()
void operator()(MYSQL_RES *doomed) const
Functor implementation.
Definition: result.h:284
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
const Fields & fields() const
Get the underlying Fields structure.
Definition: result.h:128
mysqlpp::SimpleResult
Holds information about the result of queries that don&#39;t return rows.
Definition: result.h:48
mysqlpp::Field
Class to hold information about a SQL field.
Definition: field.h:46
mysqlpp::RefCountedPointerDestroyer
Functor to call delete on the pointer you pass to it.
Definition: refcounted.h:45
mysqlpp::ResultBase::fields_
Fields fields_
list of fields in result
Definition: result.h:181
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::SimpleResult::insert_id
ulonglong insert_id() const
Get the last value used for an AUTO_INCREMENT field.
Definition: result.h:88
refcounted.h
Declares the RefCountedPointer template.
mysqlpp::ResultBase::fetch_field
const Field & fetch_field() const
Returns the next field in this result set.
Definition: result.h:117
mysqlpp::UseQueryResult
StoreQueryResult set type for &quot;use&quot; queries.
Definition: result.h:298
mysqlpp::SimpleResult::SimpleResult
SimpleResult()
Default ctor.
Definition: result.h:59
mysqlpp::ResultBase::ResultBase
ResultBase(const ResultBase &other)
Create object as a copy of another ResultBase.
Definition: result.h:171
mysqlpp::ResultBase::current_field_
Fields::size_type current_field_
Default field index used by fetch_field()
Definition: result.h:196
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::UseQueryResult::fetch_field
const Field & fetch_field() const
Returns the next field in this result set.
Definition: result.h:325
field_names.h
Declares a class to hold a list of field names.
mysqlpp::StoreQueryResult::StoreQueryResult
StoreQueryResult()
Default constructor.
Definition: result.h:224
mysqlpp::Row
Manages rows from a result set.
Definition: row.h:63
mysqlpp::ResultBase::field_names
const RefCountedPointer< FieldNames > & field_names() const
Get the names of the fields within this result set.
Definition: result.h:135
exceptions.h
Declares the MySQL++-specific exception classes.
mysqlpp::ResultBase::names_
RefCountedPointer< FieldNames > names_
list of field names in result
Definition: result.h:184
mysqlpp::OptionalExceptions
Interface allowing a class to have optional exceptions.
Definition: noexceptions.h:71
mysqlpp::SimpleResult::rows
ulonglong rows() const
Get the number of rows affected by the query.
Definition: result.h:91
mysqlpp::StoreQueryResult::StoreQueryResult
StoreQueryResult(const StoreQueryResult &other)
Initialize object as a copy of another StoreQueryResult object.
Definition: result.h:235
mysqlpp::UseQueryResult::~UseQueryResult
~UseQueryResult()
Destroy object.
Definition: result.h:318
mysqlpp::ResultBase
Base class for StoreQueryResult and UseQueryResult.
Definition: result.h:110
mysqlpp::ResultBase::field
const Field & field(unsigned int i) const
Get the underlying Field structure given its index.
Definition: result.h:125
mysqlpp::StoreQueryResult::~StoreQueryResult
~StoreQueryResult()
Destroy result set.
Definition: result.h:244
mysqlpp::RefCountedPointer
Creates an object that acts as a reference-counted pointer to another object.
Definition: refcounted.h:80
field.h
Declares the Field and Fields classes.
row.h
Declares the classes for holding row data from a result set.
mysqlpp::UseQueryResult::UseQueryResult
UseQueryResult(const UseQueryResult &other)
Create a copy of another UseQueryResult object.
Definition: result.h:311
common.h
This file includes top-level definitions for use both internal to the library, and outside it...
mysqlpp::StoreQueryResult::list_type
std::vector< Row > list_type
type of vector base class
Definition: result.h:221
noexceptions.h
Declares interface that allows exceptions to be optional.
mysqlpp::DBDriver
Provides a thin abstraction layer over the underlying database client library.
Definition: dbdriver.h:57
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::UseQueryResult::UseQueryResult
UseQueryResult()
Default constructor.
Definition: result.h:302
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