result.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 /***********************************************************************
00006  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
00007  (c) 2004-2007 by Educational Technology Resources, Inc.  Others may
00008  also hold copyrights on code in this file.  See the CREDITS file in
00009  the top directory of the distribution for details.
00010 
00011  This file is part of MySQL++.
00012 
00013  MySQL++ is free software; you can redistribute it and/or modify it
00014  under the terms of the GNU Lesser General Public License as published
00015  by the Free Software Foundation; either version 2.1 of the License, or
00016  (at your option) any later version.
00017 
00018  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
00019  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00020  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00021  License for more details.
00022 
00023  You should have received a copy of the GNU Lesser General Public
00024  License along with MySQL++; if not, write to the Free Software
00025  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00026  USA
00027 ***********************************************************************/
00028 
00029 #if !defined(MYSQLPP_RESULT_H)
00030 #define MYSQLPP_RESULT_H
00031 
00032 #include "common.h"
00033 
00034 #include "exceptions.h"
00035 #include "field.h"
00036 #include "field_names.h"
00037 #include "field_types.h"
00038 #include "noexceptions.h"
00039 #include "refcounted.h"
00040 #include "row.h"
00041 
00042 namespace mysqlpp {
00043 
00044 
00047 
00048 class MYSQLPP_EXPORT SimpleResult
00049 {
00050 private:
00055     typedef bool SimpleResult::*private_bool_type;
00056 
00057 public:
00059         SimpleResult() :
00060         copacetic_(false),
00061         insert_id_(0),
00062         rows_(0)
00063         {
00064         }
00065 
00067         SimpleResult(bool copacetic, ulonglong insert_id,
00068                         ulonglong rows, const std::string& info) :
00069         copacetic_(copacetic),
00070         insert_id_(insert_id),
00071         rows_(rows),
00072         info_(info)
00073         {
00074         }
00075 
00082         operator private_bool_type() const
00083         {
00084                 return copacetic_ ? &SimpleResult::copacetic_ : 0;
00085         }
00086 
00088         ulonglong insert_id() const { return insert_id_; }
00089 
00091         ulonglong rows() const { return rows_; }
00092 
00095         const char* info() const { return info_.c_str(); }
00096 
00097 private:
00098         bool copacetic_;
00099         ulonglong insert_id_;
00100         ulonglong rows_;
00101         std::string info_;
00102 };
00103 
00104 
00109 
00110 class MYSQLPP_EXPORT ResultBase : public OptionalExceptions
00111 {
00112 public:
00114         virtual ~ResultBase() { }
00115 
00117         const Field& fetch_field() const
00118                         { return fields_.at(current_field_++); }
00119 
00121         const Field& fetch_field(Fields::size_type i) const
00122                         { return fields_.at(i); }
00123 
00125         const Field& field(unsigned int i) const { return fields_.at(i); }
00126 
00128         const Fields& fields() const { return fields_; }
00129 
00131         const std::string& field_name(int i) const
00132                         { return names_->at(i); }
00133 
00135         const RefCountedPointer<FieldNames>& field_names() const
00136                         { return names_; }
00137 
00141         int field_num(const std::string&) const;
00142 
00144         const FieldTypes::value_type& field_type(int i) const
00145                         { return types_->at(i); }
00146 
00149         const RefCountedPointer<FieldTypes>& field_types() const
00150                         { return types_; }
00151 
00153         size_t num_fields() const { return fields_.size(); }
00154 
00156         const char* table() const
00157                         { return fields_.empty() ? "" : fields_[0].table(); }
00158 
00159 protected:
00161         ResultBase() :
00162         driver_(0),
00163         current_field_(0)
00164         {
00165         }
00166         
00168         ResultBase(MYSQL_RES* result, DBDriver* dbd, bool te = true);
00169         
00171         ResultBase(const ResultBase& other) :
00172         OptionalExceptions()
00173         {
00174                 copy(other);
00175         }
00176 
00178         ResultBase& copy(const ResultBase& other);
00179 
00180         DBDriver* driver_;      
00181         Fields fields_;         
00182 
00184         RefCountedPointer<FieldNames> names_;
00185 
00187         RefCountedPointer<FieldTypes> types_;
00188 
00196         mutable Fields::size_type current_field_;
00197 };
00198 
00199 
00208 
00209 class MYSQLPP_EXPORT StoreQueryResult :
00210                 public ResultBase,
00211                 public std::vector<Row>
00212 {
00213 private:
00218     typedef bool StoreQueryResult::*private_bool_type;
00219 
00220 public:
00221         typedef std::vector<Row> list_type;     
00222 
00224         StoreQueryResult() :
00225         ResultBase(),
00226         copacetic_(false)
00227         {
00228         }
00229         
00231         StoreQueryResult(MYSQL_RES* result, DBDriver* dbd, bool te = true);
00232 
00235         StoreQueryResult(const StoreQueryResult& other) :
00236         ResultBase(),
00237         std::vector<Row>(),
00238         copacetic_(false)
00239         {
00240                 copy(other);
00241         }
00242 
00244         ~StoreQueryResult() { }
00245 
00247         list_type::size_type num_rows() const { return size(); }
00248 
00251         StoreQueryResult& operator =(const StoreQueryResult& rhs)
00252                         { return this != &rhs ? copy(rhs) : *this; }
00253 
00260         operator private_bool_type() const
00261         {
00262                 return copacetic_ ? &StoreQueryResult::copacetic_ : 0;
00263         }
00264 
00265 private:
00268         StoreQueryResult& copy(const StoreQueryResult& other);
00269 
00270         bool copacetic_;        
00271 };
00272 
00273 
00280 template <>
00281 struct RefCountedPointerDestroyer<MYSQL_RES>
00282 {
00284         void operator()(MYSQL_RES* doomed) const
00285         {
00286                 if (doomed) {
00287                         mysql_free_result(doomed);
00288                 }
00289         }
00290 };
00291 
00292 
00297 
00298 class MYSQLPP_EXPORT UseQueryResult : public ResultBase
00299 {
00300 public:
00302         UseQueryResult() :
00303         ResultBase()
00304         {
00305         }
00306         
00308         UseQueryResult(MYSQL_RES* result, DBDriver* dbd, bool te = true);
00309         
00311         UseQueryResult(const UseQueryResult& other) :
00312         ResultBase()
00313         {
00314                 copy(other);
00315         }
00316         
00318         ~UseQueryResult() { }
00319 
00321         UseQueryResult& operator =(const UseQueryResult& rhs)
00322                         { return this != &rhs ? copy(rhs) : *this; }
00323 
00325         const Field& fetch_field() const
00326                         { return fields_.at(current_field_++); }
00327 
00329         const Field& fetch_field(Fields::size_type i) const
00330                         { return fields_.at(i); }
00331 
00337         const unsigned long* fetch_lengths() const;
00338 
00346         Row fetch_row() const;
00347 
00354         MYSQL_ROW fetch_raw_row() const;
00355 
00360         void field_seek(Fields::size_type field) const
00361                         { current_field_ = field; }
00362 
00374         // it was successful:
00385         operator MYSQL_RES*() const { return result_.raw(); }
00386         
00387 private:
00389         UseQueryResult& copy(const UseQueryResult& other);
00390 
00401         mutable RefCountedPointer<MYSQL_RES> result_;
00402 };
00403 
00404 
00406 inline void
00407 swap(StoreQueryResult& x, StoreQueryResult& y)
00408 {
00409         StoreQueryResult tmp = x;
00410         x = y;
00411         y = tmp;
00412 }
00413 
00415 inline void
00416 swap(UseQueryResult& x, UseQueryResult& y)
00417 {
00418         UseQueryResult tmp = x;
00419         x = y;
00420         y = tmp;
00421 }
00422 
00423 } // end namespace mysqlpp
00424 
00425 #endif // !defined(MYSQLPP_RESULT_H)

Generated on Fri Feb 29 16:26:00 2008 for MySQL++ by  doxygen 1.4.7