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 
00055 #ifdef PQXX_DEPRECATED_HEADERS
00056 
00057   bool GetRawLine(PGSTD::string &L) { return get_raw_line(L); }
00059   template<typename TUPLE> void Tokenize(PGSTD::string L, TUPLE &T) const
00060         { tokenize(L, T); }
00061 #endif
00062 
00064 
00067   bool get_raw_line(PGSTD::string &Line);                               //[t8]
00068 
00069   template<typename TUPLE> 
00070   void tokenize(PGSTD::string, TUPLE &) const;                          //[t8]
00071 
00072 private:
00073   bool m_Done;
00074 };
00075 
00076 
00077 }
00078 
00079 // TODO: Find meaningful definition of input iterator
00080 
00081 
00082 template<typename TUPLE> 
00083 inline void pqxx::tablereader::tokenize(PGSTD::string Line, 
00084                                         TUPLE &T) const
00085 {
00086   PGSTD::back_insert_iterator<TUPLE> ins = PGSTD::back_inserter(T);
00087 
00088   // Filter and tokenize line, inserting tokens at end of T
00089   PGSTD::string::size_type token = 0;
00090   for (PGSTD::string::size_type i=0; i < Line.size(); ++i)
00091   {
00092     switch (Line[i])
00093     {
00094     case '\t': // End of token
00095       *ins++ = PGSTD::string(Line, token, i-token);
00096       token = i+1;
00097       break;
00098 
00099     case '\\':
00100       // Ignore the backslash and accept literally whatever comes after it 
00101       if ((i+1) >= Line.size()) 
00102         throw PGSTD::runtime_error("Row ends in backslash");
00103 
00104       switch (Line[i+1])
00105       {
00106       case 'N':
00107         // This is a \N, signifying a NULL value.
00108         Line.replace(i, 2, NullStr());
00109         i += NullStr().size() - 1;
00110         break;
00111       
00112       case 't':
00113         Line.replace(i++, 2, "\t");
00114         break;
00115 
00116       case 'n':
00117         Line.replace(i++, 2, "\n");
00118         break;
00119 
00120       default:
00121         Line.erase(i, 1);
00122       }
00123       break;
00124     }
00125   }
00126 
00127   *ins++ = PGSTD::string(Line, token);
00128 }
00129 
00130 
00131 template<typename TUPLE> 
00132 inline pqxx::tablereader &pqxx::tablereader::operator>>(TUPLE &T)
00133 {
00134   PGSTD::string Line;
00135   if (get_raw_line(Line)) tokenize(Line, T);
00136   return *this;
00137 }
00138 
00139 

Generated on Sat Nov 15 01:48:58 2003 for libpqxx by doxygen 1.3.4