field.hxx

00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/field.hxx
00005  *
00006  *   DESCRIPTION
00007  *      definitions for the pqxx::field class.
00008  *   pqxx::field refers to a field in a query result.
00009  *   DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/field instead.
00010  *
00011  * Copyright (c) 2001-2015, Jeroen T. Vermeulen <jtv@xs4all.nl>
00012  *
00013  * See COPYING for copyright license.  If you did not receive a file called
00014  * COPYING with this source code, please notify the distributor of this mistake,
00015  * or contact the author.
00016  *
00017  *-------------------------------------------------------------------------
00018  */
00019 #ifndef PQXX_H_FIELD
00020 #define PQXX_H_FIELD
00021 
00022 #include "pqxx/compiler-public.hxx"
00023 #include "pqxx/compiler-internal-pre.hxx"
00024 
00025 #if defined(PQXX_HAVE_OPTIONAL)
00026 #include <optional>
00027 #endif
00028 
00029 #if defined(PQXX_HAVE_EXP_OPTIONAL)
00030 #include <experimental/optional>
00031 #endif
00032 
00033 #include "pqxx/strconv"
00034 
00035 
00036 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
00037  */
00038 
00039 namespace pqxx
00040 {
00041 class result;
00042 class row;
00043 
00044 typedef unsigned int row_size_type;
00045 typedef signed int row_difference_type;
00046 
00048 
00051 class PQXX_LIBEXPORT field
00052 {
00053 public:
00054   typedef size_t size_type;
00055 
00057 
00061   field(const row &R, row_size_type C) PQXX_NOEXCEPT;           //[t1]
00062 
00067 
00068 
00084   bool operator==(const field &) const;                         //[t75]
00085 
00087 
00089   bool operator!=(const field &rhs) const                               //[t82]
00090                                                     {return !operator==(rhs);}
00092 
00097 
00098   const char *name() const;                                             //[t11]
00099 
00101   oid type() const;                                                     //[t7]
00102 
00104   oid table() const;                                                    //[t2]
00105 
00106   row_size_type num() const { return col(); }                           //[t82]
00107 
00109   row_size_type table_column() const;                                   //[t93]
00111 
00116 
00117 
00122   const char *c_str() const;                                            //[t2]
00123 
00125   template<typename T> bool to(T &Obj) const                            //[t3]
00126   {
00127     const char *const bytes = c_str();
00128     if (!bytes[0] && is_null()) return false;
00129     from_string(bytes, Obj);
00130     return true;
00131   }
00132 
00134   template<typename T> bool operator>>(T &Obj) const                    //[t7]
00135       { return to(Obj); }
00136 
00137 #ifdef PQXX_NO_PARTIAL_CLASS_TEMPLATE_SPECIALISATION
00139   template<> bool to<std::string>(std::string &Obj) const;
00140 
00142 
00145   template<> bool to<const char *>(const char *&Obj) const;
00146 #endif
00147 
00149   template<typename T> bool to(T &Obj, const T &Default) const  //[t12]
00150   {
00151     const bool NotNull = to(Obj);
00152     if (!NotNull) Obj = Default;
00153     return NotNull;
00154   }
00155 
00157 
00160   template<typename T> T as(const T &Default) const                     //[t1]
00161   {
00162     T Obj;
00163     to(Obj, Default);
00164     return Obj;
00165   }
00166 
00168   template<typename T> T as() const                                     //[t45]
00169   {
00170     T Obj;
00171     const bool NotNull = to(Obj);
00172     if (!NotNull) Obj = string_traits<T>::null();
00173     return Obj;
00174   }
00175 
00176 #if defined(PQXX_HAVE_OPTIONAL)
00178   template<typename T> std::optional<T> get() const
00179         { return get_opt<T, std::optional<T> >(); }
00180 #endif
00181 
00182 #if defined(PQXX_HAVE_EXP_OPTIONAL)
00184   template<typename T> std::experimental::optional<T> get() const
00185         { return get_opt<T, std::experimental::optional<T> >(); }
00186 #endif
00187 
00189   bool is_null() const PQXX_NOEXCEPT;                                   //[t12]
00190 
00192 
00195   size_type size() const PQXX_NOEXCEPT;                                 //[t11]
00197 
00198 
00199 protected:
00200   const result *home() const PQXX_NOEXCEPT { return m_home; }
00201   size_t idx() const PQXX_NOEXCEPT { return m_row; }
00202   row_size_type col() const PQXX_NOEXCEPT { return m_col; }
00203 
00204   row_size_type m_col;
00205 
00206 private:
00208 
00213   template<typename T, typename OPTIONAL_T> OPTIONAL_T get_opt() const
00214   {
00215     if (is_null()) return OPTIONAL_T();
00216     else return OPTIONAL_T(as<T>());
00217   }
00218 
00219   const result *m_home;
00220   size_t m_row;
00221 };
00222 
00223 
00225 template<>
00226 inline bool field::to<std::string>(std::string &Obj) const
00227 {
00228   const char *const bytes = c_str();
00229   if (!bytes[0] && is_null()) return false;
00230   Obj = std::string(bytes, size());
00231   return true;
00232 }
00233 
00235 
00240 template<>
00241 inline bool field::to<const char *>(const char *&Obj) const
00242 {
00243   if (is_null()) return false;
00244   Obj = c_str();
00245   return true;
00246 }
00247 
00248 
00249 template<typename CHAR=char, typename TRAITS=std::char_traits<CHAR> >
00250   class field_streambuf :
00251   public std::basic_streambuf<CHAR, TRAITS>
00252 {
00253 public:
00254   typedef CHAR char_type;
00255   typedef TRAITS traits_type;
00256   typedef typename traits_type::int_type int_type;
00257   typedef typename traits_type::pos_type pos_type;
00258   typedef typename traits_type::off_type off_type;
00259   typedef std::ios::openmode openmode;
00260   typedef std::ios::seekdir seekdir;
00261 
00262   explicit field_streambuf(const field &F) :                    //[t74]
00263     m_Field(F)
00264   {
00265     initialize();
00266   }
00267 
00268 protected:
00269   virtual int sync() PQXX_OVERRIDE { return traits_type::eof(); }
00270 
00271 protected:
00272   virtual pos_type seekoff(off_type, seekdir, openmode) PQXX_OVERRIDE
00273         { return traits_type::eof(); }
00274   virtual pos_type seekpos(pos_type, openmode) PQXX_OVERRIDE
00275         {return traits_type::eof();}
00276   virtual int_type overflow(int_type) PQXX_OVERRIDE
00277         { return traits_type::eof(); }
00278   virtual int_type underflow() PQXX_OVERRIDE
00279         { return traits_type::eof(); }
00280 
00281 private:
00282   const field &m_Field;
00283 
00284   int_type initialize()
00285   {
00286     char_type *G =
00287       reinterpret_cast<char_type *>(const_cast<char *>(m_Field.c_str()));
00288     this->setg(G, G, G + m_Field.size());
00289     return int_type(m_Field.size());
00290   }
00291 };
00292 
00293 
00295 
00303 template<typename CHAR=char, typename TRAITS=std::char_traits<CHAR> >
00304   class basic_fieldstream :
00305     public std::basic_istream<CHAR, TRAITS>
00306 {
00307   typedef std::basic_istream<CHAR, TRAITS> super;
00308 
00309 public:
00310   typedef CHAR char_type;
00311   typedef TRAITS traits_type;
00312   typedef typename traits_type::int_type int_type;
00313   typedef typename traits_type::pos_type pos_type;
00314   typedef typename traits_type::off_type off_type;
00315 
00316   basic_fieldstream(const field &F) : super(0), m_Buf(F)
00317         { super::init(&m_Buf); }
00318 
00319 private:
00320   field_streambuf<CHAR, TRAITS> m_Buf;
00321 };
00322 
00323 typedef basic_fieldstream<char> fieldstream;
00324 
00325 } // namespace pqxx
00326 
00327 
00328 #include "pqxx/compiler-internal-post.hxx"
00329 
00330 #endif

Generated on 17 Mar 2017 for libpqxx by  doxygen 1.6.1