Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

result.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 /***********************************************************************
00005  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
00006  MySQL AB, and (c) 2004, 2005 by Educational Technology Resources, Inc.
00007  Others may also hold copyrights on code in this file.  See the CREDITS
00008  file in the top directory of the distribution for details.
00009 
00010  This file is part of MySQL++.
00011 
00012  MySQL++ is free software; you can redistribute it and/or modify it
00013  under the terms of the GNU Lesser General Public License as published
00014  by the Free Software Foundation; either version 2.1 of the License, or
00015  (at your option) any later version.
00016 
00017  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
00018  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00019  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00020  License for more details.
00021 
00022  You should have received a copy of the GNU Lesser General Public
00023  License along with MySQL++; if not, write to the Free Software
00024  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00025  USA
00026 ***********************************************************************/
00027 
00028 #ifndef MYSQLPP_RESULT_H
00029 #define MYSQLPP_RESULT_H
00030 
00031 #include "common.h"
00032 
00033 #include "exceptions.h"
00034 #include "fields.h"
00035 #include "field_names.h"
00036 #include "field_types.h"
00037 #include "noexceptions.h"
00038 #include "resiter.h"
00039 #include "row.h"
00040 
00041 #include <map>
00042 #include <set>
00043 #include <string>
00044 
00045 namespace mysqlpp {
00046 
00047 #if !defined(DOXYGEN_IGNORE)
00048 // Make Doxygen ignore this
00049 class MYSQLPP_EXPORT Connection;
00050 #endif
00051 
00061 
00062 class MYSQLPP_EXPORT ResUse : public OptionalExceptions
00063 {
00064 public:
00066         ResUse() :
00067         OptionalExceptions(),
00068         conn_(0),
00069         result_(0),
00070         initialized_(false),
00071         names_(0),
00072         types_(0),
00073         fields_(this)
00074         {
00075         }
00076         
00078         ResUse(MYSQL_RES* result, Connection* c = 0, bool te = true);
00079         
00081         ResUse(const ResUse& other) :
00082         OptionalExceptions(),
00083         initialized_(false)
00084         {
00085                 copy(other);
00086                 other.result_ = 0;
00087         }
00088         
00090         virtual ~ResUse();
00091 
00093         ResUse& operator =(const ResUse& other);
00094 
00096         MYSQL_RES* raw_result()
00097         {
00098                 return result_;
00099         }
00100 
00105         Row fetch_row()
00106         {
00107                 if (!result_) {
00108                         if (throw_exceptions()) {
00109                                 throw BadQuery("Results not fetched");
00110                         }
00111                         else {
00112                                 return Row();
00113                         }
00114                 }
00115                 MYSQL_ROW row = mysql_fetch_row(result_);
00116                 unsigned long* length = mysql_fetch_lengths(result_);
00117                 if (!row || !length) {
00118                         if (throw_exceptions()) {
00119                                 throw EndOfResults();
00120                         }
00121                         else {
00122                                 return Row();
00123                         }
00124                 }
00125                 return Row(row, this, length, throw_exceptions());
00126         }
00127 
00129         unsigned long *fetch_lengths() const
00130         {
00131                 return mysql_fetch_lengths(result_);
00132         }
00133 
00135         Field& fetch_field() const
00136         {
00137                 return *mysql_fetch_field(result_);
00138         }
00139 
00141         void field_seek(int field)
00142         {
00143                 mysql_field_seek(result_, field);
00144         }
00145 
00147         int num_fields() const
00148         {
00149                 return mysql_num_fields(result_);
00150         }
00151         
00153         void parent_leaving()
00154         {
00155                 conn_ = 0;
00156         }
00157 
00163         void purge()
00164         {
00165                 if (result_) {
00166                         mysql_free_result(result_);
00167                         result_ = 0;
00168                 }
00169 
00170                 delete names_;
00171                 names_ = 0;
00172 
00173                 delete types_;
00174                 types_ = 0;
00175 
00176                 table_.erase();
00177         }
00178 
00192         operator bool() const
00193         {
00194                 return result_;
00195         }
00196         
00198         unsigned int columns() const
00199         {
00200                 return num_fields();
00201         }
00202 
00204         std::string& table()
00205         {
00206                 return table_;
00207         }
00208 
00212         const std::string& table() const
00213         {
00214                 return table_;
00215         }
00216 
00220         int field_num(const std::string&) const;
00221 
00225         std::string& field_name(int);
00226 
00228         const std::string& field_name(int) const;
00229 
00231         FieldNames& field_names();
00232 
00234         const FieldNames& field_names() const;
00235 
00238         void reset_field_names();
00239 
00241         mysql_type_info& field_type(int i);
00242 
00244         const mysql_type_info& field_type(int) const;
00245 
00248         FieldTypes& field_types();
00249 
00252         const FieldTypes& field_types() const;
00253 
00255         void reset_field_types();
00256 
00258         int names(const std::string & s) const { return field_num(s); }
00259 
00261         std::string& names(int i) { return field_name(i); }
00262 
00264         const std::string& names(int i) const { return field_name(i); }
00265 
00267         FieldNames& names() { return field_names(); }
00268 
00270         const FieldNames& names() const { return field_names(); }
00271 
00273         void reset_names() { reset_field_names(); }
00274 
00276         mysql_type_info& types(int i) { return field_type(i); }
00277 
00279         const mysql_type_info& types(int i) const { return field_type(i); }
00280 
00282         FieldTypes& types() { return field_types(); }
00283 
00285         const FieldTypes& types() const { return field_types(); }
00286 
00288         void reset_types() { reset_field_types(); }
00289 
00291         const Fields& fields() const { return fields_; }
00292 
00294         const Field& fields(unsigned int i) const { return fields_.at(i); }
00295         
00301         bool operator ==(const ResUse& other) const
00302         {
00303                 return result_ == other.result_;
00304         }
00305         
00308         bool operator !=(const ResUse& other) const
00309         {
00310                 return result_ != other.result_;
00311         }
00312 
00313 protected:
00314         Connection* conn_;                      
00315         mutable MYSQL_RES* result_;     
00316         bool initialized_;                      
00317         mutable FieldNames* names_;     
00318         mutable FieldTypes* types_;     
00319         Fields fields_;                         
00320         std::string table_;                     
00321 
00325         void copy(const ResUse& other);
00326 };
00327 
00328 
00340 
00341 class MYSQLPP_EXPORT Result : public ResUse,
00342                 public const_subscript_container<Result, Row, const Row>
00343 {
00344 public:
00346         Result()
00347         {
00348         }
00349         
00351         Result(MYSQL_RES* result, bool te = true) :
00352         ResUse(result, 0, te)
00353         {
00354         }
00355 
00357         Result(const Result& other) :
00358         ResUse(other),
00359         const_subscript_container<Result, Row, const Row>() // no copying here
00360         {
00361                 conn_ = 0;
00362         }
00363 
00365         virtual ~Result() { }
00366 
00372         const Row fetch_row() const
00373         {
00374                 if (!result_) {
00375                         if (throw_exceptions()) {
00376                                 throw BadQuery("Results not fetched");
00377                         }
00378                         else {
00379                                 return Row();
00380                         }
00381                 }
00382                 MYSQL_ROW row = mysql_fetch_row(result_);
00383                 unsigned long* length = mysql_fetch_lengths(result_);
00384                 if (!row || !length) {
00385                         if (throw_exceptions()) {
00386                                 throw EndOfResults();
00387                         }
00388                         else {
00389                                 return Row();
00390                         }
00391                 }
00392                 return Row(row, this, length, throw_exceptions());
00393         }
00394 
00396         my_ulonglong num_rows() const
00397         {
00398                 if (initialized_)
00399                         return mysql_num_rows(result_);
00400                 else
00401                         return 0;
00402         }
00403 
00405         void data_seek(uint offset) const
00406         {
00407                 mysql_data_seek(result_, offset);
00408         }
00409 
00411         size_type size() const
00412         {
00413                 return size_type(num_rows());
00414         }
00415 
00417         size_type rows() const
00418         {
00419                 return size_type(num_rows());
00420         }
00421 
00423         const Row at(size_type i) const
00424         {
00425                 data_seek(i);
00426                 return fetch_row();
00427         }
00428 };
00429 
00430 
00432 inline void swap(ResUse& x, ResUse& y)
00433 {
00434         ResUse tmp = x;
00435         x = y;
00436         y = tmp;
00437 }
00438 
00440 inline void swap(Result& x, Result& y)
00441 {
00442         Result tmp = x;
00443         x = y;
00444         y = tmp;
00445 }
00446 
00449 class MYSQLPP_EXPORT ResNSel
00450 {
00451 public:
00452         bool success;                   
00453         my_ulonglong insert_id; 
00454         my_ulonglong rows;              
00455         std::string info;               
00456 
00457         ResNSel() :
00458         success(false)
00459         {
00460         }
00461 
00463         ResNSel(Connection* q);
00464 
00466         operator bool() { return success; }
00467 };
00468 
00469 
00470 } // end namespace mysqlpp
00471 
00472 #endif

Generated on Wed Jul 11 15:34:34 2007 for MySQL++ by doxygen 1.3.5