00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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 <mysql.h>
00042
00043 #include <map>
00044 #include <set>
00045 #include <string>
00046
00047 namespace mysqlpp {
00048
00049 #if !defined(DOXYGEN_IGNORE)
00050
00051 class MYSQLPP_EXPORT Connection;
00052 #endif
00053
00063
00064 class MYSQLPP_EXPORT ResUse : public OptionalExceptions
00065 {
00066 public:
00068 ResUse() :
00069 OptionalExceptions(),
00070 conn_(0),
00071 result_(0),
00072 initialized_(false),
00073 names_(0),
00074 types_(0),
00075 fields_(this)
00076 {
00077 }
00078
00080 ResUse(MYSQL_RES* result, Connection* c = 0, bool te = true);
00081
00083 ResUse(const ResUse& other) :
00084 OptionalExceptions(),
00085 initialized_(false)
00086 {
00087 copy(other);
00088 other.result_ = 0;
00089 }
00090
00092 virtual ~ResUse();
00093
00095 ResUse& operator =(const ResUse& other);
00096
00098 MYSQL_RES* raw_result()
00099 {
00100 return result_;
00101 }
00102
00107 Row fetch_row()
00108 {
00109 if (!result_) {
00110 if (throw_exceptions()) {
00111 throw BadQuery("Results not fetched");
00112 }
00113 else {
00114 return Row();
00115 }
00116 }
00117 MYSQL_ROW row = mysql_fetch_row(result_);
00118 unsigned long* length = mysql_fetch_lengths(result_);
00119 if (!row || !length) {
00120 if (throw_exceptions()) {
00121 throw EndOfResults();
00122 }
00123 else {
00124 return Row();
00125 }
00126 }
00127 return Row(row, this, length, throw_exceptions());
00128 }
00129
00131 unsigned long *fetch_lengths() const
00132 {
00133 return mysql_fetch_lengths(result_);
00134 }
00135
00137 Field& fetch_field() const
00138 {
00139 return *mysql_fetch_field(result_);
00140 }
00141
00143 void field_seek(int field)
00144 {
00145 mysql_field_seek(result_, field);
00146 }
00147
00149 int num_fields() const
00150 {
00151 return mysql_num_fields(result_);
00152 }
00153
00155 void parent_leaving()
00156 {
00157 conn_ = 0;
00158 }
00159
00165 void purge()
00166 {
00167 if (result_) {
00168 mysql_free_result(result_);
00169 result_ = 0;
00170 }
00171
00172 delete names_;
00173 names_ = 0;
00174
00175 delete types_;
00176 types_ = 0;
00177
00178 table_.erase();
00179 }
00180
00194 operator bool() const
00195 {
00196 return result_;
00197 }
00198
00200 unsigned int columns() const
00201 {
00202 return num_fields();
00203 }
00204
00206 std::string& table()
00207 {
00208 return table_;
00209 }
00210
00214 const std::string& table() const
00215 {
00216 return table_;
00217 }
00218
00222 int field_num(const std::string&) const;
00223
00227 std::string& field_name(int);
00228
00230 const std::string& field_name(int) const;
00231
00233 FieldNames& field_names();
00234
00236 const FieldNames& field_names() const;
00237
00240 void reset_field_names();
00241
00243 mysql_type_info& field_type(int i);
00244
00246 const mysql_type_info& field_type(int) const;
00247
00250 FieldTypes& field_types();
00251
00254 const FieldTypes& field_types() const;
00255
00257 void reset_field_types();
00258
00260 int names(const std::string & s) const { return field_num(s); }
00261
00263 std::string& names(int i) { return field_name(i); }
00264
00266 const std::string& names(int i) const { return field_name(i); }
00267
00269 FieldNames& names() { return field_names(); }
00270
00272 const FieldNames& names() const { return field_names(); }
00273
00275 void reset_names() { reset_field_names(); }
00276
00278 mysql_type_info& types(int i) { return field_type(i); }
00279
00281 const mysql_type_info& types(int i) const { return field_type(i); }
00282
00284 FieldTypes& types() { return field_types(); }
00285
00287 const FieldTypes& types() const { return field_types(); }
00288
00290 void reset_types() { reset_field_types(); }
00291
00293 const Fields& fields() const { return fields_; }
00294
00296 const Field& fields(unsigned int i) const { return fields_.at(i); }
00297
00303 bool operator ==(const ResUse& other) const
00304 {
00305 return result_ == other.result_;
00306 }
00307
00310 bool operator !=(const ResUse& other) const
00311 {
00312 return result_ != other.result_;
00313 }
00314
00315 protected:
00316 Connection* conn_;
00317 mutable MYSQL_RES* result_;
00318 bool initialized_;
00319 mutable FieldNames* names_;
00320 mutable FieldTypes* types_;
00321 Fields fields_;
00322 std::string table_;
00323
00327 void copy(const ResUse& other);
00328 };
00329
00330
00342
00343 class MYSQLPP_EXPORT Result : public ResUse,
00344 public const_subscript_container<Result, Row, const Row>
00345 {
00346 public:
00348 Result()
00349 {
00350 }
00351
00353 Result(MYSQL_RES* result, bool te = true) :
00354 ResUse(result, 0, te)
00355 {
00356 }
00357
00359 Result(const Result& other) :
00360 ResUse(other),
00361 const_subscript_container<Result, Row, const Row>()
00362 {
00363 conn_ = 0;
00364 }
00365
00367 virtual ~Result() { }
00368
00374 const Row fetch_row() const
00375 {
00376 if (!result_) {
00377 if (throw_exceptions()) {
00378 throw BadQuery("Results not fetched");
00379 }
00380 else {
00381 return Row();
00382 }
00383 }
00384 MYSQL_ROW row = mysql_fetch_row(result_);
00385 unsigned long* length = mysql_fetch_lengths(result_);
00386 if (!row || !length) {
00387 if (throw_exceptions()) {
00388 throw EndOfResults();
00389 }
00390 else {
00391 return Row();
00392 }
00393 }
00394 return Row(row, this, length, throw_exceptions());
00395 }
00396
00398 my_ulonglong num_rows() const
00399 {
00400 if (initialized_)
00401 return mysql_num_rows(result_);
00402 else
00403 return 0;
00404 }
00405
00407 void data_seek(uint offset) const
00408 {
00409 mysql_data_seek(result_, offset);
00410 }
00411
00413 size_type size() const
00414 {
00415 return size_type(num_rows());
00416 }
00417
00419 size_type rows() const
00420 {
00421 return size_type(num_rows());
00422 }
00423
00425 const Row at(size_type i) const
00426 {
00427 data_seek(i);
00428 return fetch_row();
00429 }
00430 };
00431
00432
00434 inline void swap(ResUse& x, ResUse& y)
00435 {
00436 ResUse tmp = x;
00437 x = y;
00438 y = tmp;
00439 }
00440
00442 inline void swap(Result& x, Result& y)
00443 {
00444 Result tmp = x;
00445 x = y;
00446 y = tmp;
00447 }
00448
00451 class MYSQLPP_EXPORT ResNSel
00452 {
00453 public:
00454 bool success;
00455 my_ulonglong insert_id;
00456 my_ulonglong rows;
00457 std::string info;
00458
00459 ResNSel() :
00460 success(false)
00461 {
00462 }
00463
00465 ResNSel(Connection* q);
00466
00468 operator bool() { return success; }
00469 };
00470
00471
00472 }
00473
00474 #endif