result.hxx
/*-------------------------------------------------------------------------
*
* FILE
* pqxx/result.hxx
*
* DESCRIPTION
* definitions for the pqxx::result class and support classes.
* pqxx::result represents the set of result tuples from a database query
* DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/result instead.
*
* Copyright (c) 2001-2012, Jeroen T. Vermeulen <jtv@xs4all.nl>
*
* See COPYING for copyright license. If you did not receive a file called
* COPYING with this source code, please notify the distributor of this mistake,
* or contact the author.
*
*-------------------------------------------------------------------------
*/
#ifndef PQXX_H_RESULT
#define PQXX_H_RESULT
#include "pqxx/compiler-public.hxx"
#include "pqxx/compiler-internal-pre.hxx"
#ifdef PQXX_HAVE_IOS
#include <ios>
#endif
#include <stdexcept>
#include "pqxx/internal/result_data.hxx"
#include "pqxx/except"
#include "pqxx/field"
#include "pqxx/tuple"
#include "pqxx/util"
/* Methods tested in eg. self-test program test001 are marked with "//[t1]"
*/
// TODO: Support SQL arrays
namespace pqxx
{
namespace internal
{
namespace gate
{
class result_connection;
class result_creation;
class result_sql_cursor;
} // namespace internal::gate
} // namespace internal
class const_result_iterator;
class const_reverse_result_iterator;
class PQXX_LIBEXPORT result :
private internal::PQAlloc<
const internal::result_data, internal::freemem_result_data>
{
typedef internal::PQAlloc<
const internal::result_data, internal::freemem_result_data> super;
public:
typedef unsigned long size_type;
typedef signed long difference_type;
typedef pqxx::tuple reference;
typedef const_result_iterator const_iterator;
typedef const_iterator pointer;
typedef const_iterator iterator;
typedef const_reverse_result_iterator const_reverse_iterator;
typedef const_reverse_iterator reverse_iterator;
typedef pqxx::tuple tuple;
typedef pqxx::field field;
result() throw () : super(), m_data(0) {} //[t3]
result(const result &rhs) throw () : //[t1]
super(rhs), m_data(rhs.m_data) {}
result &operator=(const result &rhs) throw () //[t10]
{ super::operator=(rhs); m_data=rhs.m_data; return *this; }
bool operator==(const result &) const throw (); //[t70]
bool operator!=(const result &rhs) const throw () //[t70]
{ return !operator==(rhs); }
const_reverse_iterator rbegin() const; //[t75]
const_reverse_iterator rend() const; //[t75]
const_iterator begin() const throw (); //[t1]
inline const_iterator end() const throw (); //[t1]
reference front() const throw () { return tuple(this,0); } //[t74]
reference back() const throw () {return tuple(this,size()-1);} //[t75]
size_type PQXX_PURE size() const throw (); //[t2]
bool PQXX_PURE empty() const throw (); //[t11]
size_type capacity() const throw () { return size(); } //[t20]
void swap(result &) throw (); //[t77]
const tuple operator[](size_type i) const throw () //[t2]
{ return tuple(this, i); }
const tuple at(size_type) const throw (range_error); //[t10]
void clear() throw () { super::reset(); m_data = 0; } //[t20]
tuple::size_type PQXX_PURE columns() const throw (); //[t11]
tuple::size_type column_number(const char ColName[]) const; //[t11]
tuple::size_type column_number(const PGSTD::string &Name) const //[t11]
{return column_number(Name.c_str());}
const char *column_name(tuple::size_type Number) const; //[t11]
oid column_type(tuple::size_type ColNum) const; //[t7]
oid column_type(int ColNum) const //[t7]
{ return column_type(tuple::size_type(ColNum)); }
oid column_type(const PGSTD::string &ColName) const //[t7]
{ return column_type(column_number(ColName)); }
oid column_type(const char ColName[]) const //[t7]
{ return column_type(column_number(ColName)); }
oid column_table(tuple::size_type ColNum) const; //[t2]
oid column_table(int ColNum) const //[t2]
{ return column_table(tuple::size_type(ColNum)); }
oid column_table(const PGSTD::string &ColName) const //[t2]
{ return column_table(column_number(ColName)); }
tuple::size_type table_column(tuple::size_type ColNum) const; //[t93]
tuple::size_type table_column(int ColNum) const //[t93]
{ return table_column(tuple::size_type(ColNum)); }
tuple::size_type table_column(const PGSTD::string &ColName) const //[t93]
{ return table_column(column_number(ColName)); }
const PGSTD::string & PQXX_PURE query() const throw (); //[t70]
oid PQXX_PURE inserted_oid() const; //[t13]
size_type PQXX_PURE affected_rows() const; //[t7]
private:
friend class pqxx::field;
const char * PQXX_PURE GetValue(size_type Row, tuple::size_type Col) const;
bool PQXX_PURE GetIsNull(size_type Row, tuple::size_type Col) const;
field::size_type PQXX_PURE GetLength(
size_type,
tuple::size_type) const throw ();
friend class pqxx::internal::gate::result_creation;
result(internal::pq::PGresult *rhs,
int protocol,
const PGSTD::string &Query,
int encoding_code);
void PQXX_PRIVATE CheckStatus() const;
friend class pqxx::internal::gate::result_connection;
bool operator!() const throw () { return !m_data; }
operator bool() const throw () { return m_data != 0; }
void PQXX_PRIVATE PQXX_NORETURN ThrowSQLError(
const PGSTD::string &Err,
const PGSTD::string &Query) const;
int PQXX_PRIVATE PQXX_PURE errorposition() const throw ();
PGSTD::string PQXX_PRIVATE StatusError() const;
friend class pqxx::internal::gate::result_sql_cursor;
const char * PQXX_PURE CmdStatus() const throw ();
pqxx::internal::pq::PGresult *m_data;
static const PGSTD::string PQXX_PRIVATE s_empty_string;
};
class PQXX_LIBEXPORT const_result_iterator :
public PGSTD::iterator<
PGSTD::random_access_iterator_tag,
const tuple,
result::difference_type,
const_result_iterator,
tuple>,
public tuple
{
public:
typedef const tuple *pointer;
typedef tuple reference;
typedef result::size_type size_type;
typedef result::difference_type difference_type;
const_result_iterator() throw () : tuple(0,0) {}
const_result_iterator(const tuple &t) throw () : tuple(t) {}
pointer operator->() const { return this; } //[t12]
reference operator*() const { return tuple(*this); } //[t12]
const_result_iterator operator++(int); //[t12]
const_result_iterator &operator++() { ++m_Index; return *this; } //[t1]
const_result_iterator operator--(int); //[t12]
const_result_iterator &operator--() { --m_Index; return *this; } //[t12]
const_result_iterator &operator+=(difference_type i) //[t12]
{ m_Index = size_type(difference_type(m_Index) + i); return *this; }
const_result_iterator &operator-=(difference_type i) //[t12]
{ m_Index = size_type(difference_type (m_Index) - i); return *this; }
bool operator==(const const_result_iterator &i) const //[t12]
{return m_Index==i.m_Index;}
bool operator!=(const const_result_iterator &i) const //[t12]
{return m_Index!=i.m_Index;}
bool operator<(const const_result_iterator &i) const //[t12]
{return m_Index<i.m_Index;}
bool operator<=(const const_result_iterator &i) const //[t12]
{return m_Index<=i.m_Index;}
bool operator>(const const_result_iterator &i) const //[t12]
{return m_Index>i.m_Index;}
bool operator>=(const const_result_iterator &i) const //[t12]
{return m_Index>=i.m_Index;}
inline const_result_iterator operator+(difference_type) const; //[t12]
friend const_result_iterator operator+( //[t12]
difference_type,
const_result_iterator);
inline const_result_iterator operator-(difference_type) const; //[t12]
inline difference_type operator-(const_result_iterator) const; //[t12]
private:
friend class pqxx::result;
const_result_iterator(const pqxx::result *r, result::size_type i) throw () :
tuple(r, i) {}
};
class PQXX_LIBEXPORT const_reverse_result_iterator :
private const_result_iterator
{
public:
typedef const_result_iterator super;
typedef const_result_iterator iterator_type;
using iterator_type::iterator_category;
using iterator_type::difference_type;
using iterator_type::pointer;
#ifndef _MSC_VER
using iterator_type::value_type;
using iterator_type::reference;
#else
// Workaround for Visual C++.NET 2003, which has access problems
typedef const tuple &reference;
typedef tuple value_type;
#endif
const_reverse_result_iterator( //[t75]
const const_reverse_result_iterator &rhs) :
const_result_iterator(rhs) {}
explicit const_reverse_result_iterator( //[t75]
const const_result_iterator &rhs) :
const_result_iterator(rhs) { super::operator--(); }
const_result_iterator PQXX_PURE base() const throw (); //[t75]
using const_result_iterator::operator->; //[t75]
using const_result_iterator::operator*; //[t75]
const_reverse_result_iterator &operator=( //[t75]
const const_reverse_result_iterator &r)
{ iterator_type::operator=(r); return *this; }
const_reverse_result_iterator operator++() //[t75]
{ iterator_type::operator--(); return *this; }
const_reverse_result_iterator operator++(int); //[t75]
const_reverse_result_iterator &operator--() //[t75]
{ iterator_type::operator++(); return *this; }
const_reverse_result_iterator operator--(int); //[t75]
const_reverse_result_iterator &operator+=(difference_type i) //[t75]
{ iterator_type::operator-=(i); return *this; }
const_reverse_result_iterator &operator-=(difference_type i) //[t75]
{ iterator_type::operator+=(i); return *this; }
const_reverse_result_iterator operator+(difference_type i) const //[t75]
{ return const_reverse_result_iterator(base() - i); }
const_reverse_result_iterator operator-(difference_type i) //[t75]
{ return const_reverse_result_iterator(base() + i); }
difference_type operator-( //[t75]
const const_reverse_result_iterator &rhs) const
{ return rhs.const_result_iterator::operator-(*this); }
bool operator==( //[t75]
const const_reverse_result_iterator &rhs) const throw ()
{ return iterator_type::operator==(rhs); }
bool operator!=( //[t75]
const const_reverse_result_iterator &rhs) const throw ()
{ return !operator==(rhs); }
bool operator<(const const_reverse_result_iterator &rhs) const //[t75]
{ return iterator_type::operator>(rhs); }
bool operator<=(const const_reverse_result_iterator &rhs) const //[t75]
{ return iterator_type::operator>=(rhs); }
bool operator>(const const_reverse_result_iterator &rhs) const //[t75]
{ return iterator_type::operator<(rhs); }
bool operator>=(const const_reverse_result_iterator &rhs) const //[t75]
{ return iterator_type::operator<=(rhs); }
};
template<typename CHAR>
inline PGSTD::basic_ostream<CHAR> &operator<<(
PGSTD::basic_ostream<CHAR> &S, const pqxx::field &F) //[t46]
{
S.write(F.c_str(), PGSTD::streamsize(F.size()));
return S;
}
template<typename T>
inline void from_string(const field &F, T &Obj) //[t46]
{ from_string(F.c_str(), Obj, F.size()); }
template<>
inline PGSTD::string to_string(const field &Obj) //[t74]
{ return PGSTD::string(Obj.c_str(), Obj.size()); }
inline const_result_iterator
const_result_iterator::operator+(result::difference_type o) const
{
return const_result_iterator(
m_Home, size_type(result::difference_type(m_Index) + o));
}
inline const_result_iterator
operator+(result::difference_type o, const_result_iterator i)
{ return i + o; }
inline const_result_iterator
const_result_iterator::operator-(result::difference_type o) const
{
return const_result_iterator(
m_Home,
result::size_type(result::difference_type(m_Index) - o));
}
inline result::difference_type
const_result_iterator::operator-(const_result_iterator i) const
{ return result::difference_type(num() - i.num()); }
inline const_result_iterator result::end() const throw ()
{ return const_result_iterator(this, size()); }
inline const_reverse_result_iterator
operator+(result::difference_type n,
const const_reverse_result_iterator &i)
{ return const_reverse_result_iterator(i.base() - n); }
} // namespace pqxx
#include "pqxx/compiler-internal-post.hxx"
#endif
pqxx::result::column_type
oid column_type(const std::string &ColName) const
Type of given column.
Definition: result.hxx:163
pqxx::result
Result set containing data returned by a query or command.
Definition: result.hxx:81
pqxx::tuple
Reference to one row in a result.
Definition: tuple.hxx:52
pqxx::result::column_type
oid column_type(int ColNum) const
Type of given column.
Definition: result.hxx:159
pqxx::const_result_iterator::operator<=
bool operator<=(const const_result_iterator &i) const
Definition: result.hxx:311
pqxx::const_reverse_result_iterator::operator!=
bool operator!=(const const_reverse_result_iterator &rhs) const
Definition: result.hxx:413
pqxx::result::column_table
oid column_table(int ColNum) const
What table did this column come from?
Definition: result.hxx:174
pqxx::const_result_iterator::operator>=
bool operator>=(const const_result_iterator &i) const
Definition: result.hxx:315
pqxx::field
Reference to a field in a result set.
Definition: field.hxx:43
pqxx::const_result_iterator::operator-=
const_result_iterator & operator-=(difference_type i)
Definition: result.hxx:297
pqxx::const_result_iterator::operator--
const_result_iterator & operator--()
Definition: result.hxx:293
pqxx::const_result_iterator::operator-
const_result_iterator operator-(difference_type) const
Definition: result.hxx:483
pqxx::operator<<
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &S, const pqxx::field &F)
Write a result field to any type of stream.
Definition: result.hxx:452
pqxx::const_reverse_result_iterator::operator++
const_reverse_result_iterator operator++()
Definition: result.hxx:381
pqxx::result::const_iterator
const_result_iterator const_iterator
Definition: result.hxx:91
pqxx::const_result_iterator::pointer
const tuple * pointer
Definition: result.hxx:259
pqxx::result::field
pqxx::field field
For backward compatibility only.
Definition: result.hxx:100
pqxx::result::clear
void clear()
Definition: result.hxx:137
pqxx::const_reverse_result_iterator::iterator_type
const_result_iterator iterator_type
Definition: result.hxx:344
pqxx::result::result
result()
Definition: result.hxx:102
pqxx::const_result_iterator::operator+=
const_result_iterator & operator+=(difference_type i)
Definition: result.hxx:295
pqxx::result::reference
pqxx::tuple reference
Definition: result.hxx:90
pqxx::result::pointer
const_iterator pointer
Definition: result.hxx:92
pqxx::const_result_iterator::operator==
bool operator==(const const_result_iterator &i) const
Definition: result.hxx:305
pqxx::result::operator=
result & operator=(const result &rhs)
Definition: result.hxx:106
pqxx::field::size_type
size_t size_type
Definition: field.hxx:46
pqxx::const_reverse_result_iterator::operator-
difference_type operator-(const const_reverse_result_iterator &rhs) const
Definition: result.hxx:401
pqxx::const_reverse_result_iterator::operator+
const_reverse_result_iterator operator+(difference_type i) const
Definition: result.hxx:397
pqxx::const_result_iterator::difference_type
result::difference_type difference_type
Definition: result.hxx:262
pqxx::result::table_column
tuple::size_type table_column(const std::string &ColName) const
What column in its table did this column come from?
Definition: result.hxx:189
pqxx::const_reverse_result_iterator::operator-=
const_reverse_result_iterator & operator-=(difference_type i)
Definition: result.hxx:389
pqxx::const_reverse_result_iterator
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result.hxx:339
pqxx::from_string
void from_string(const field &F, T &Obj)
Convert a field&#39;s string contents to another type.
Definition: result.hxx:462
pqxx::const_reverse_result_iterator::operator+=
const_reverse_result_iterator & operator+=(difference_type i)
Definition: result.hxx:387
pqxx::const_reverse_result_iterator::operator>
bool operator>(const const_reverse_result_iterator &rhs) const
Definition: result.hxx:421
pqxx::const_reverse_result_iterator::const_reverse_result_iterator
const_reverse_result_iterator(const const_result_iterator &rhs)
Definition: result.hxx:360
pqxx::const_result_iterator::operator*
reference operator*() const
Definition: result.hxx:283
pqxx::result::capacity
size_type capacity() const
Definition: result.hxx:129
pqxx::const_result_iterator
Iterator for rows (tuples) in a result. Use as result::const_iterator.
Definition: result.hxx:249
pqxx::const_result_iterator::operator+
const_result_iterator operator+(difference_type) const
Definition: result.hxx:472
pqxx::result::column_table
oid column_table(const std::string &ColName) const
What table did this column come from?
Definition: result.hxx:178
pqxx::const_result_iterator::operator++
const_result_iterator & operator++()
Definition: result.hxx:291
pqxx::const_result_iterator::operator!=
bool operator!=(const const_result_iterator &i) const
Definition: result.hxx:307
pqxx::operator+
const_result_iterator operator+(result::difference_type o, const_result_iterator i)
Definition: result.hxx:479
pqxx::const_result_iterator::size_type
result::size_type size_type
Definition: result.hxx:261
pqxx::result::result
result(const result &rhs)
Definition: result.hxx:103
pqxx::tuple::size_type
tuple_size_type size_type
Definition: tuple.hxx:55
pqxx::result::table_column
tuple::size_type table_column(int ColNum) const
What column in its table did this column come from?
Definition: result.hxx:185
pqxx::const_reverse_result_iterator::operator<
bool operator<(const const_reverse_result_iterator &rhs) const
Definition: result.hxx:417
pqxx::result::end
const_iterator end() const
Definition: result.hxx:494
pqxx::internal::PQAlloc
Reference-counted smart pointer to libpq-allocated object.
Definition: util.hxx:559
pqxx::const_result_iterator::operator->
pointer operator->() const
Definition: result.hxx:282
pqxx::const_result_iterator::const_result_iterator
const_result_iterator(const tuple &t)
Definition: result.hxx:265
pqxx::result::tuple
pqxx::tuple tuple
For backward compatibility only.
Definition: result.hxx:98
pqxx::const_result_iterator::operator>
bool operator>(const const_result_iterator &i) const
Definition: result.hxx:313
pqxx::to_string
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.hxx:467
pqxx::const_reverse_result_iterator::super
const_result_iterator super
Definition: result.hxx:343
pqxx::const_result_iterator::operator<
bool operator<(const const_result_iterator &i) const
Definition: result.hxx:309
pqxx::tuple::num
size_t num() const
Definition: tuple.hxx:166
pqxx::const_reverse_result_iterator::operator==
bool operator==(const const_reverse_result_iterator &rhs) const
Definition: result.hxx:410
pqxx::field::size
size_type size() const
Definition: field.cxx:86
pqxx::const_reverse_result_iterator::const_reverse_result_iterator
const_reverse_result_iterator(const const_reverse_result_iterator &rhs)
Definition: result.hxx:357
pqxx::const_result_iterator::const_result_iterator
const_result_iterator()
Definition: result.hxx:264
pqxx::const_reverse_result_iterator::operator--
const_reverse_result_iterator & operator--()
Definition: result.hxx:384
pqxx::result::column_number
tuple::size_type column_number(const std::string &Name) const
Number of given column (throws exception if it doesn&#39;t exist)
Definition: result.hxx:150
pqxx::result::back
reference back() const
Definition: result.hxx:125
pqxx::const_reverse_result_iterator::operator<=
bool operator<=(const const_reverse_result_iterator &rhs) const
Definition: result.hxx:419
pqxx::result::operator!=
bool operator!=(const result &rhs) const
Definition: result.hxx:114
pqxx::result::operator[]
const tuple operator[](size_type i) const
Definition: result.hxx:133
pqxx::const_reverse_result_iterator::operator=
const_reverse_result_iterator & operator=(const const_reverse_result_iterator &r)
Definition: result.hxx:378
pqxx::result::column_type
oid column_type(const char ColName[]) const
Type of given column.
Definition: result.hxx:167
pqxx::result::difference_type
signed long difference_type
Definition: result.hxx:89
pqxx::result::front
reference front() const
Definition: result.hxx:124
pqxx::field::c_str
const char * c_str() const
Read as plain C string.
Definition: field.cxx:74
pqxx::result::size_type
unsigned long size_type
Definition: result.hxx:88
pqxx::const_result_iterator::reference
tuple reference
Definition: result.hxx:260
pqxx::const_reverse_result_iterator::operator>=
bool operator>=(const const_reverse_result_iterator &rhs) const
Definition: result.hxx:423
pqxx::result::reverse_iterator
const_reverse_iterator reverse_iterator
Definition: result.hxx:95
pqxx::const_reverse_result_iterator::operator-
const_reverse_result_iterator operator-(difference_type i)
Definition: result.hxx:399
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: basic_connection.hxx:35
pqxx::result::const_reverse_iterator
const_reverse_result_iterator const_reverse_iterator
Definition: result.hxx:94
pqxx::range_error
Something is out of range, similar to std::out_of_range.
Definition: except.hxx:203
pqxx::result::iterator
const_iterator iterator
Definition: result.hxx:93
pqxx::const_reverse_result_iterator::base
const_result_iterator PQXX_PURE base() const
Definition: result.cxx:386