Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

tablereader.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/tablereader.h
00005  *
00006  *   DESCRIPTION
00007  *      definition of the pqxx::TableReader class.
00008  *   pqxx::TableReader enables optimized batch reads from a database table
00009  *
00010  * Copyright (c) 2001-2002, Jeroen T. Vermeulen <jtv@xs4all.nl>
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef PQXX_TABLEREADER_H
00015 #define PQXX_TABLEREADER_H
00016 
00017 #include <string>
00018 
00019 #include "pqxx/result.h"
00020 #include "pqxx/tablestream.h"
00021 
00022 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00023  */
00024 
00025 
00026 namespace pqxx
00027 {
00028 
00030 
00042 class PQXX_LIBEXPORT TableReader : public TableStream
00043 {
00044 public:
00045   TableReader(Transaction &Trans, PGSTD::string Name);                  //[t6]
00046   ~TableReader();                                                       //[t6]
00047 
00048   TableReader &operator>>(Result &);
00049   TableReader &operator>>(PGSTD::string &);
00050 
00051   template<typename TUPLE> TableReader &operator>>(TUPLE &);            //[t8]
00052 
00053   operator bool() const { return !m_Done; }                             //[t6]
00054   bool operator!() const { return m_Done; }                             //[t6]
00055 
00056   bool GetRawLine(PGSTD::string &Line);                                 //[t8]
00057 
00058   template<typename TUPLE> 
00059   void Tokenize(PGSTD::string, TUPLE &) const;                          //[t8]
00060 
00061 private:
00062   bool m_Done;
00063 };
00064 
00065 }
00066 
00067 // TODO: Find meaningful definition of input iterator
00068 
00069 
00070 template<typename TUPLE> 
00071 inline void pqxx::TableReader::Tokenize(PGSTD::string Line, TUPLE &T) const
00072 {
00073   PGSTD::back_insert_iterator<TUPLE> ins = PGSTD::back_inserter(T);
00074 
00075   // Filter and tokenize line, inserting tokens at end of T
00076   PGSTD::string::size_type token = 0;
00077   for (PGSTD::string::size_type i=0; i < Line.size(); ++i)
00078   {
00079     switch (Line[i])
00080     {
00081     case '\t': // End of token
00082       *ins++ = PGSTD::string(Line, token, i-token);
00083       token = i+1;
00084       break;
00085 
00086     case '\\':
00087       // Ignore the backslash and accept literally whatever comes after it 
00088       if ((i+1) >= Line.size()) 
00089         throw PGSTD::runtime_error("Row ends in backslash");
00090 
00091       switch (Line[i+1])
00092       {
00093       case 'N':
00094         // This is a \N, signifying a NULL value.
00095         Line.replace(i, 2, NullStr());
00096         i += NullStr().size() - 1;
00097         break;
00098       
00099       case 't':
00100         Line.replace(i++, 2, "\t");
00101         break;
00102 
00103       case 'n':
00104         Line.replace(i++, 2, "\n");
00105         break;
00106 
00107       default:
00108         Line.erase(i, 1);
00109       }
00110       break;
00111     }
00112   }
00113 
00114   *ins++ = PGSTD::string(Line, token);
00115 }
00116 
00117 
00118 template<typename TUPLE> 
00119 inline pqxx::TableReader &pqxx::TableReader::operator>>(TUPLE &T)
00120 {
00121   PGSTD::string Line;
00122   if (GetRawLine(Line)) Tokenize(Line, T);
00123   return *this;
00124 }
00125 
00126 
00127 
00128 #endif
00129 

Generated on Sat Oct 5 15:55:45 2002 for libpqxx by doxygen1.2.18