field.hxx
/*-------------------------------------------------------------------------
*
* FILE
* pqxx/field.hxx
*
* DESCRIPTION
* definitions for the pqxx::field class.
* pqxx::field refers to a field in a query result.
* DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/field instead.
*
* Copyright (c) 2001-2011, 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_FIELD
#define PQXX_H_FIELD
#include "pqxx/compiler-public.hxx"
#include "pqxx/compiler-internal-pre.hxx"
#include "pqxx/strconv"
/* Methods tested in eg. self-test program test001 are marked with "//[t1]"
*/
namespace pqxx
{
class result;
class tuple;
typedef unsigned int tuple_size_type;
typedef signed int tuple_difference_type;
class PQXX_LIBEXPORT field
{
public:
typedef size_t size_type;
field(const tuple &T, tuple_size_type C) throw (); //[t1]
bool operator==(const field &) const; //[t75]
bool operator!=(const field &rhs) const //[t82]
{return !operator==(rhs);}
const char *name() const; //[t11]
oid type() const; //[t7]
oid table() const; //[t2]
tuple_size_type num() const { return col(); } //[t82]
tuple_size_type table_column() const; //[t93]
const char *c_str() const; //[t2]
template<typename T> bool to(T &Obj) const //[t3]
{
const char *const bytes = c_str();
if (!bytes[0] && is_null()) return false;
from_string(bytes, Obj);
return true;
}
template<typename T> bool operator>>(T &Obj) const //[t7]
{ return to(Obj); }
#ifdef PQXX_NO_PARTIAL_CLASS_TEMPLATE_SPECIALISATION
template<> bool to<PGSTD::string>(PGSTD::string &Obj) const;
template<> bool to<const char *>(const char *&Obj) const;
#endif
template<typename T> bool to(T &Obj, const T &Default) const //[t12]
{
const bool NotNull = to(Obj);
if (!NotNull) Obj = Default;
return NotNull;
}
template<typename T> T as(const T &Default) const //[t1]
{
T Obj;
to(Obj, Default);
return Obj;
}
template<typename T> T as() const //[t45]
{
T Obj;
const bool NotNull = to(Obj);
if (!NotNull) Obj = string_traits<T>::null();
return Obj;
}
bool is_null() const throw (); //[t12]
size_type size() const throw (); //[t11]
protected:
const result *home() const throw () { return m_home; }
size_t idx() const throw () { return m_row; }
tuple_size_type col() const throw () { return m_col; }
tuple_size_type m_col;
private:
const result *m_home;
size_t m_row;
};
template<>
inline bool field::to<PGSTD::string>(PGSTD::string &Obj) const
{
const char *const bytes = c_str();
if (!bytes[0] && is_null()) return false;
Obj = PGSTD::string(bytes, size());
return true;
}
template<>
inline bool field::to<const char *>(const char *&Obj) const
{
if (is_null()) return false;
Obj = c_str();
return true;
}
template<typename CHAR=char, typename TRAITS=PGSTD::char_traits<CHAR> >
class field_streambuf :
#ifdef PQXX_HAVE_STREAMBUF
public PGSTD::basic_streambuf<CHAR, TRAITS>
#else
public PGSTD::streambuf
#endif
{
public:
typedef CHAR char_type;
typedef TRAITS traits_type;
typedef typename traits_type::int_type int_type;
#ifdef PQXX_HAVE_STREAMBUF
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
#else
typedef streamoff off_type;
typedef streampos pos_type;
#endif
typedef PGSTD::ios::openmode openmode;
typedef PGSTD::ios::seekdir seekdir;
explicit field_streambuf(const field &F) : //[t74]
m_Field(F)
{
initialize();
}
#ifdef PQXX_HAVE_STREAMBUF
protected:
#endif
virtual int sync() { return traits_type::eof(); }
protected:
virtual pos_type seekoff(off_type, seekdir, openmode)
{ return traits_type::eof(); }
virtual pos_type seekpos(pos_type, openmode) {return traits_type::eof();}
virtual int_type overflow(int_type) { return traits_type::eof(); }
virtual int_type underflow() { return traits_type::eof(); }
private:
const field &m_Field;
int_type initialize()
{
char_type *G =
reinterpret_cast<char_type *>(const_cast<char *>(m_Field.c_str()));
this->setg(G, G, G + m_Field.size());
return int_type(m_Field.size());
}
};
template<typename CHAR=char, typename TRAITS=PGSTD::char_traits<CHAR> >
class basic_fieldstream :
#ifdef PQXX_HAVE_STREAMBUF
public PGSTD::basic_istream<CHAR, TRAITS>
#else
public PGSTD::istream
#endif
{
#ifdef PQXX_HAVE_STREAMBUF
typedef PGSTD::basic_istream<CHAR, TRAITS> super;
#else
typedef PGSTD::istream super;
#endif
public:
typedef CHAR char_type;
typedef TRAITS traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
basic_fieldstream(const field &F) : super(0), m_Buf(F)
{ super::init(&m_Buf); }
private:
field_streambuf<CHAR, TRAITS> m_Buf;
};
typedef basic_fieldstream<char> fieldstream;
} // namespace pqxx
#include "pqxx/compiler-internal-post.hxx"
#endif
pqxx::basic_fieldstream::traits_type
TRAITS traits_type
Definition: field.hxx:289
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::field::operator!=
bool operator!=(const field &rhs) const
Byte-by-byte comparison (all nulls are considered equal)
Definition: field.hxx:81
pqxx::field
Reference to a field in a result set.
Definition: field.hxx:43
pqxx::basic_fieldstream::basic_fieldstream
basic_fieldstream(const field &F)
Definition: field.hxx:294
pqxx::fieldstream
basic_fieldstream< char > fieldstream
Definition: field.hxx:301
pqxx::field_streambuf::seekoff
virtual pos_type seekoff(off_type, seekdir, openmode)
Definition: field.hxx:245
pqxx::basic_fieldstream::off_type
traits_type::off_type off_type
Definition: field.hxx:292
pqxx::field_streambuf::pos_type
streampos pos_type
Definition: field.hxx:228
pqxx::field::operator>>
bool operator>>(T &Obj) const
Read value into Obj; or leave Obj untouched and return false if null.
Definition: field.hxx:126
pqxx::field_streambuf::field_streambuf
field_streambuf(const field &F)
Definition: field.hxx:233
pqxx::field::home
const result * home() const
Definition: field.hxx:174
pqxx::field_streambuf::openmode
std::ios::openmode openmode
Definition: field.hxx:230
pqxx::field::to
bool to(T &Obj) const
Read value into Obj; or leave Obj untouched and return false if null.
Definition: field.hxx:117
pqxx::field_streambuf::seekdir
std::ios::seekdir seekdir
Definition: field.hxx:231
pqxx::field_streambuf::sync
virtual int sync()
Definition: field.hxx:242
pqxx::field::size_type
size_t size_type
Definition: field.hxx:46
pqxx::field::col
tuple_size_type col() const
Definition: field.hxx:176
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::field_streambuf::int_type
traits_type::int_type int_type
Definition: field.hxx:222
pqxx::field::m_col
tuple_size_type m_col
Definition: field.hxx:178
pqxx::field::to
bool to(T &Obj, const T &Default) const
Read value into Obj; or use Default & return false if null.
Definition: field.hxx:141
pqxx::field_streambuf::char_type
CHAR char_type
Definition: field.hxx:220
pqxx::basic_fieldstream::pos_type
traits_type::pos_type pos_type
Definition: field.hxx:291
pqxx::field_streambuf::underflow
virtual int_type underflow()
Definition: field.hxx:249
pqxx::field::as
T as() const
Return value as object of given type, or throw exception if null.
Definition: field.hxx:160
pqxx::basic_fieldstream
Input stream that gets its data from a result field.
Definition: field.hxx:274
pqxx::string_traits
Traits class for use in string conversions.
Definition: strconv.hxx:46
pqxx::field::idx
size_t idx() const
Definition: field.hxx:175
pqxx::field::size
size_type size() const
Definition: field.cxx:86
pqxx::field_streambuf::off_type
streamoff off_type
Definition: field.hxx:227
pqxx::field_streambuf::overflow
virtual int_type overflow(int_type)
Definition: field.hxx:248
pqxx::tuple_size_type
unsigned int tuple_size_type
Definition: field.hxx:34
pqxx::basic_fieldstream::char_type
CHAR char_type
Definition: field.hxx:288
pqxx::field::as
T as(const T &Default) const
Return value as object of given type, or Default if null.
Definition: field.hxx:152
pqxx::field::c_str
const char * c_str() const
Read as plain C string.
Definition: field.cxx:74
pqxx::basic_fieldstream::int_type
traits_type::int_type int_type
Definition: field.hxx:290
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: basic_connection.hxx:35
pqxx::field_streambuf
Definition: field.hxx:212
pqxx::field_streambuf::seekpos
virtual pos_type seekpos(pos_type, openmode)
Definition: field.hxx:247
pqxx::tuple_difference_type
signed int tuple_difference_type
Definition: field.hxx:37
pqxx::field_streambuf::traits_type
TRAITS traits_type
Definition: field.hxx:221
pqxx::field::num
tuple_size_type num() const
Definition: field.hxx:98