00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef PQXX_RESULT_H
00015 #define PQXX_RESULT_H
00016
00017 #include <stdexcept>
00018
00019 #include "pqxx/util.h"
00020
00021
00022
00023
00024
00025
00026
00027 namespace pqxx
00028 {
00029
00031
00038 class PQXX_LIBEXPORT Result
00039 {
00040 public:
00041 Result() : m_Result(0), m_Refcount(0) {}
00042 Result(const Result &rhs) :
00043 m_Result(0), m_Refcount(0) { MakeRef(rhs); }
00044 ~Result() { LoseRef(); }
00045
00046 Result &operator=(const Result &);
00047
00048 typedef Result_size_type size_type;
00049 class Field;
00050
00051
00052
00054
00062 class PQXX_LIBEXPORT Tuple
00063 {
00064 public:
00065 typedef Tuple_size_type size_type;
00066 Tuple(const Result *r, Result::size_type i) : m_Home(r), m_Index(i) {}
00067 ~Tuple() {}
00068
00069 inline Field operator[](size_type) const;
00070 Field operator[](const char[]) const;
00071 Field operator[](const PGSTD::string &s) const
00072 { return operator[](s.c_str()); }
00073 Field at(size_type) const;
00074 Field at(const char[]) const;
00075 Field at(const PGSTD::string &s) const { return at(s.c_str()); }
00076
00077 inline size_type size() const;
00078
00079 Result::size_type Row() const { return m_Index; }
00080
00081 protected:
00082 const Result *m_Home;
00083 Result::size_type m_Index;
00084
00085
00086 Tuple();
00087 };
00088
00089
00091
00094 class PQXX_LIBEXPORT Field : private Tuple
00095 {
00096 public:
00097 typedef size_t size_type;
00098
00100
00104 Field(const Tuple &R, Tuple::size_type C) : Tuple(R), m_Col(C) {}
00105
00107
00112 const char *c_str() const {return m_Home->GetValue(m_Index,m_Col);}
00113
00115 inline const char *Name() const;
00116
00118 template<typename T> bool to(T &Obj) const
00119 {
00120 if (is_null())
00121 return false;
00122
00123 try
00124 {
00125 FromString(c_str(), Obj);
00126 }
00127 catch (const PGSTD::exception &e)
00128 {
00129 throw PGSTD::runtime_error("Error reading field " +
00130 PGSTD::string(Name()) +
00131 ": " +
00132 e.what());
00133 }
00134 return true;
00135 }
00136
00137 #ifdef NO_PARTIAL_CLASS_TEMPLATE_SPECIALISATION
00138
00139 template<> bool to(PGSTD::string &Obj) const
00140 {
00141 if (is_null())
00142 return false;
00143 Obj = c_str();
00144 return true;
00145 }
00146
00148
00151 template<> bool to(const char *&Obj) const
00152 {
00153 if (is_null())
00154 return false;
00155 Obj = c_str();
00156 return true;
00157 }
00158 #endif
00159
00160
00162 template<typename T> bool to(T &Obj, const T &Default) const
00163 {
00164 const bool NotNull = to(Obj);
00165 if (!NotNull)
00166 Obj = Default;
00167 return NotNull;
00168 }
00169
00170 bool is_null() const { return m_Home->GetIsNull(m_Index,m_Col); }
00171
00172 int size() const { return m_Home->GetLength(m_Index,m_Col); }
00173
00174 private:
00175
00176 Tuple::size_type m_Col;
00177 };
00178
00179
00181
00185 class PQXX_LIBEXPORT const_iterator :
00186 public PGSTD::iterator<PGSTD::random_access_iterator_tag,
00187 const Tuple,
00188 Result::size_type>,
00189 public Tuple
00190 {
00191 public:
00192
00199 pointer operator->() const { return this; }
00200 reference operator*() const { return *operator->(); }
00201
00202 const_iterator operator++(int);
00203 const_iterator &operator++() { ++m_Index; return *this; }
00204 const_iterator operator--(int);
00205 const_iterator &operator--() { --m_Index; return *this; }
00206
00207 const_iterator &operator+=(difference_type i)
00208 { m_Index+=i; return *this; }
00209 const_iterator &operator-=(difference_type i)
00210 { m_Index-=i; return *this; }
00211
00212 bool operator==(const const_iterator &i) const
00213 {return m_Index==i.m_Index;}
00214 bool operator!=(const const_iterator &i) const
00215 {return m_Index!=i.m_Index;}
00216 bool operator<(const const_iterator &i) const
00217 {return m_Index<i.m_Index;}
00218 bool operator<=(const const_iterator &i) const
00219 {return m_Index<=i.m_Index;}
00220 bool operator>(const const_iterator &i) const
00221 {return m_Index>i.m_Index;}
00222 bool operator>=(const const_iterator &i) const
00223 {return m_Index>=i.m_Index;}
00224
00225 inline const_iterator operator+(difference_type o) const;
00226
00227 friend const_iterator operator+(difference_type o,
00228 const_iterator i);
00229
00230 inline const_iterator operator-(difference_type o) const;
00231
00232 inline difference_type operator-(const_iterator i) const;
00233
00234 Result::size_type num() const { return Row(); }
00235
00236 private:
00237 friend class Result;
00238 const_iterator(const Result *r, Result::size_type i) : Tuple(r, i) {}
00239 };
00240
00241 const_iterator begin() const { return const_iterator(this, 0); }
00242 inline const_iterator end() const;
00243
00244
00245 size_type size() const { return m_Result ? PQntuples(m_Result) : 0; }
00246 bool empty() const { return !m_Result || !PQntuples(m_Result); }
00247 size_type capacity() const { return size(); }
00248
00249 const Tuple operator[](size_type i) const { return Tuple(this, i); }
00250 const Tuple at(size_type) const;
00251
00252 void clear() { LoseRef(); }
00253
00254 Tuple::size_type Columns() const { return PQnfields(m_Result); }
00255
00257 Tuple::size_type ColumnNumber(const char Name[]) const
00258 {return PQfnumber(m_Result,Name);}
00260 Tuple::size_type ColumnNumber(const std::string &Name) const
00261 {return ColumnNumber(Name.c_str());}
00262 const char *ColumnName(Tuple::size_type Number) const
00263 {return PQfname(m_Result,Number);}
00264
00266
00267 Oid InsertedOid() const { return PQoidValue(m_Result); }
00268
00270
00271 size_type AffectedRows() const;
00272
00273 private:
00274 PGresult *m_Result;
00275 mutable int *m_Refcount;
00276
00277 friend class Result::Field;
00278 const char *GetValue(size_type Row, Tuple::size_type Col) const;
00279 bool GetIsNull(size_type Row, Tuple::size_type Col) const;
00280 Field::size_type GetLength(size_type Row, Tuple::size_type Col) const;
00281
00282 friend class Connection;
00283 explicit Result(PGresult *rhs) : m_Result(rhs), m_Refcount(0) {MakeRef(rhs);}
00284 Result &operator=(PGresult *);
00285 bool operator!() const throw () { return !m_Result; }
00286 operator bool() const throw () { return m_Result != 0; }
00287 void CheckStatus() const;
00288
00289 friend class Cursor;
00290 const char *CmdStatus() const throw () { return PQcmdStatus(m_Result); }
00291
00292
00293 void MakeRef(PGresult *);
00294 void MakeRef(const Result &);
00295 void LoseRef() throw ();
00296 };
00297
00298
00299 inline Result::Field
00300 Result::Tuple::operator[](Result::Tuple::size_type i) const
00301 {
00302 return Field(*this, i);
00303 }
00304
00305 inline Result::Tuple::size_type Result::Tuple::size() const
00306 {
00307 return m_Home->Columns();
00308 }
00309
00310 inline const char *Result::Field::Name() const
00311 {
00312 return m_Home->ColumnName(m_Col);
00313 }
00314
00315
00316 #ifndef NO_PARTIAL_CLASS_TEMPLATE_SPECIALISATION
00317
00318 template<> inline bool Result::Field::to(PGSTD::string &Obj) const
00319 {
00320 if (is_null())
00321 return false;
00322 Obj = c_str();
00323 return true;
00324 }
00325
00327
00330 template<> inline bool Result::Field::to(const char *&Obj) const
00331 {
00332 if (is_null())
00333 return false;
00334 Obj = c_str();
00335 return true;
00336 }
00337 #endif
00338
00339
00340
00341 inline Result::const_iterator
00342 Result::const_iterator::operator+(difference_type o) const
00343 {
00344 return const_iterator(m_Home, m_Index + o);
00345 }
00346
00347 inline Result::const_iterator
00348 operator+(Result::const_iterator::difference_type o,
00349 Result::const_iterator i)
00350 {
00351 return i + o;
00352 }
00353
00354 inline Result::const_iterator
00355 Result::const_iterator::operator-(difference_type o) const
00356 {
00357 return const_iterator(m_Home, m_Index - o);
00358 }
00359
00360 inline Result::const_iterator::difference_type
00361 Result::const_iterator::operator-(const_iterator i) const
00362 {
00363 return num()-i.num();
00364 }
00365
00366 inline Result::const_iterator Result::end() const
00367 {
00368 return const_iterator(this, size());
00369 }
00370
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 #endif
00393