# tablereader.hxx

    00001 /*-------------------------------------------------------------------------
    00002  *
    00003  *   FILE
    00004  *      pqxx/tablereader.hxx
    00005  *
    00006  *   DESCRIPTION
    00007  *      definition of the pqxx::tablereader class.
    00008  *   pqxx::tablereader enables optimized batch reads from a database table
    00009  *   DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/tablereader instead.
    00010  *
    00011  * Copyright (c) 2001-2008, 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_TABLEREADER
    00020 #define PQXX_H_TABLEREADER
    00021
    00022 #include "pqxx/compiler-public.hxx"
    00023 #include "pqxx/compiler-internal-pre.hxx"
    00024
    00025 #include "pqxx/result"
    00026 #include "pqxx/tablestream"
    00027
    00028 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
    00029  */
    00030
    00031 namespace pqxx
    00032 {
    00033
    00035
    00049 class PQXX_LIBEXPORT tablereader : public tablestream
    00050 {
    00051 public:
    00052   tablereader(transaction_base &,
    00053       const PGSTD::string &Name,
    00054       const PGSTD::string &Null=PGSTD::string());                       //[t6]
    00055
    00057
    00059   template<typename ITER>
    00060   tablereader(transaction_base &,
    00061       const PGSTD::string &Name,
    00062       ITER begincolumns,
    00063       ITER endcolumns);                                                 //[t80]
    00064
    00065   template<typename ITER> tablereader(transaction_base &,
    00066       const PGSTD::string &Name,
    00067       ITER begincolumns,
    00068       ITER endcolumns,
    00069       const PGSTD::string &Null);                                       //[t80]
    00070
    00071   ~tablereader() throw ();                                              //[t6]
    00072
    00073   template<typename TUPLE> tablereader &operator>>(TUPLE &);            //[t8]
    00074
    00075   operator bool() const throw () { return !m_Done; }                    //[t6]
    00076   bool operator!() const throw () { return m_Done; }                    //[t6]
    00077
    00079
    00083   bool get_raw_line(PGSTD::string &Line);                               //[t8]
    00084
    00085   template<typename TUPLE>
    00086   void tokenize(PGSTD::string, TUPLE &) const;                          //[t8]
    00087
    00089
    00096   virtual void complete();                                              //[t8]
    00097
    00098 private:
    00099   void setup(transaction_base &T,
    00100       const PGSTD::string &RName,
    00101       const PGSTD::string &Columns=PGSTD::string());
    00102   void PQXX_PRIVATE reader_close();
    00103   PGSTD::string extract_field(const PGSTD::string &,
    00104       PGSTD::string::size_type &) const;
    00105
    00106   bool m_Done;
    00107 };
    00108
    00109
    00110 // TODO: Find meaningful definition of input iterator
    00111
    00112
    00113 template<typename ITER> inline
    00114 tablereader::tablereader(transaction_base &T,
    00115     const PGSTD::string &Name,
    00116     ITER begincolumns,
    00117     ITER endcolumns) :
    00118   namedclass(Name, "tablereader"),
    00119   tablestream(T, PGSTD::string()),
    00120   m_Done(true)
    00121 {
    00122   setup(T, Name, columnlist(begincolumns, endcolumns));
    00123 }
    00124
    00125 template<typename ITER> inline
    00126 tablereader::tablereader(transaction_base &T,
    00127     const PGSTD::string &Name,
    00128     ITER begincolumns,
    00129     ITER endcolumns,
    00130     const PGSTD::string &Null) :
    00131   namedclass(Name, "tablereader"),
    00132   tablestream(T, Null),
    00133   m_Done(true)
    00134 {
    00135   setup(T, Name, columnlist(begincolumns, endcolumns));
    00136 }
    00137
    00138
    00139 template<typename TUPLE>
    00140 inline void tablereader::tokenize(PGSTD::string Line, TUPLE &T) const
    00141 {
    00142   PGSTD::back_insert_iterator<TUPLE> ins = PGSTD::back_inserter(T);
    00143
    00144   // Filter and tokenize line, inserting tokens at end of T
    00145   PGSTD::string::size_type here=0;
    00146   while (here < Line.size()) *ins++ = extract_field(Line, here);
    00147 }
    00148
    00149
    00150 template<typename TUPLE>
    00151 inline tablereader &pqxx::tablereader::operator>>(TUPLE &T)
    00152 {
    00153   PGSTD::string Line;
    00154   if (get_raw_line(Line)) tokenize(Line, T);
    00155   return *this;
    00156 }
    00157
    00158
    00159 } // namespace pqxx
    00160
    00161 #include "pqxx/compiler-internal-post.hxx"
    00162
    00163 #endif
    00164