Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

tablereader.hxx

Go to the documentation of this file.
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-2003, 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 #include <string>
00020 
00021 #include "pqxx/result"
00022 #include "pqxx/tablestream"
00023 
00024 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00025  */
00026 
00027 
00028 namespace pqxx
00029 {
00030 
00032 
00044 class PQXX_LIBEXPORT tablereader : public tablestream
00045 {
00046 public:
00047   tablereader(transaction_base &, const PGSTD::string &RName);          //[t6]
00048   ~tablereader();                                                       //[t6]
00049 
00050   template<typename TUPLE> tablereader &operator>>(TUPLE &);            //[t8]
00051 
00052   operator bool() const throw () { return !m_Done; }                    //[t6]
00053   bool operator!() const throw () { return m_Done; }                    //[t6]
00054 
00056 
00059   bool get_raw_line(PGSTD::string &Line);                               //[t8]
00060 
00061   template<typename TUPLE> 
00062   void tokenize(PGSTD::string, TUPLE &) const;                          //[t8]
00063 
00064 
00065 #ifdef PQXX_DEPRECATED_HEADERS
00066 
00067   bool GetRawLine(PGSTD::string &L) { return get_raw_line(L); }
00069   template<typename TUPLE> void Tokenize(PGSTD::string L, TUPLE &T) const
00070         { tokenize(L, T); }
00071 #endif
00072 
00073 private:
00074   bool m_Done;
00075 };
00076 
00077 
00078 }
00079 
00080 // TODO: Find meaningful definition of input iterator
00081 
00082 
00083 template<typename TUPLE> 
00084 inline void pqxx::tablereader::tokenize(PGSTD::string Line, 
00085                                         TUPLE &T) const
00086 {
00087   PGSTD::back_insert_iterator<TUPLE> ins = PGSTD::back_inserter(T);
00088 
00089   // Filter and tokenize line, inserting tokens at end of T
00090   PGSTD::string::size_type token = 0;
00091   for (PGSTD::string::size_type i=0; i < Line.size(); ++i)
00092   {
00093     switch (Line[i])
00094     {
00095     case '\t': // End of token
00096       *ins++ = PGSTD::string(Line, token, i-token);
00097       token = i+1;
00098       break;
00099 
00100     case '\\':
00101       // Ignore the backslash and accept literally whatever comes after it 
00102       if ((i+1) >= Line.size()) 
00103         throw PGSTD::runtime_error("Row ends in backslash");
00104 
00105       switch (Line[i+1])
00106       {
00107       case 'N':
00108         // This is a \N, signifying a NULL value.
00109         Line.replace(i, 2, NullStr());
00110         i += NullStr().size() - 1;
00111         break;
00112       
00113       case 't':
00114         Line.replace(i++, 2, "\t");
00115         break;
00116 
00117       case 'n':
00118         Line.replace(i++, 2, "\n");
00119         break;
00120 
00121       case 'r':
00122         Line.replace(i++, 2, "\r");
00123         break;
00124 
00125       // TODO: Octal values
00126       // TODO: Backspace, form feed, vertical tab
00127       default:
00128         Line.erase(i, 1);
00129       }
00130       break;
00131     }
00132   }
00133 
00134   *ins++ = PGSTD::string(Line, token);
00135 }
00136 
00137 
00138 template<typename TUPLE> 
00139 inline pqxx::tablereader &pqxx::tablereader::operator>>(TUPLE &T)
00140 {
00141   PGSTD::string Line;
00142   if (get_raw_line(Line)) tokenize(Line, T);
00143   return *this;
00144 }
00145 
00146 

Generated on Fri Nov 21 19:50:08 2003 for libpqxx by doxygen 1.3.4