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

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-2003, Jeroen T. Vermeulen <jtv@xs4all.nl>
00011  *
00012  * See COPYING for copyright license.  If you did not receive a file called
00013  * COPYING with this source code, please notify the distributor of this mistake,
00014  * or contact the author.
00015  *
00016  *-------------------------------------------------------------------------
00017  */
00018 #ifndef PQXX_TABLEREADER_H
00019 #define PQXX_TABLEREADER_H
00020 
00021 #include <string>
00022 
00023 #include "pqxx/result.h"
00024 #include "pqxx/tablestream.h"
00025 
00026 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00027  */
00028 
00029 
00030 namespace pqxx
00031 {
00032 
00034 
00046 class PQXX_LIBEXPORT tablereader : public tablestream
00047 {
00048 public:
00049   tablereader(transaction_base &, const PGSTD::string &RName);          //[t6]
00050   ~tablereader();                                                       //[t6]
00051 
00052   template<typename TUPLE> tablereader &operator>>(TUPLE &);            //[t8]
00053 
00054   operator bool() const throw () { return !m_Done; }                    //[t6]
00055   bool operator!() const throw () { return m_Done; }                    //[t6]
00056 
00058 
00061   bool GetRawLine(PGSTD::string &Line);                                 //[t8]
00062 
00063   template<typename TUPLE> 
00064   void Tokenize(PGSTD::string, TUPLE &) const;                          //[t8]
00065 
00066 private:
00067   bool m_Done;
00068 };
00069 
00071 typedef tablereader TableReader;
00072 
00073 }
00074 
00075 // TODO: Find meaningful definition of input iterator
00076 
00077 
00078 template<typename TUPLE> 
00079 inline void pqxx::tablereader::Tokenize(PGSTD::string Line, 
00080                                         TUPLE &T) const
00081 {
00082   PGSTD::back_insert_iterator<TUPLE> ins = PGSTD::back_inserter(T);
00083 
00084   // Filter and tokenize line, inserting tokens at end of T
00085   PGSTD::string::size_type token = 0;
00086   for (PGSTD::string::size_type i=0; i < Line.size(); ++i)
00087   {
00088     switch (Line[i])
00089     {
00090     case '\t': // End of token
00091       *ins++ = PGSTD::string(Line, token, i-token);
00092       token = i+1;
00093       break;
00094 
00095     case '\\':
00096       // Ignore the backslash and accept literally whatever comes after it 
00097       if ((i+1) >= Line.size()) 
00098         throw PGSTD::runtime_error("Row ends in backslash");
00099 
00100       switch (Line[i+1])
00101       {
00102       case 'N':
00103         // This is a \N, signifying a NULL value.
00104         Line.replace(i, 2, NullStr());
00105         i += NullStr().size() - 1;
00106         break;
00107       
00108       case 't':
00109         Line.replace(i++, 2, "\t");
00110         break;
00111 
00112       case 'n':
00113         Line.replace(i++, 2, "\n");
00114         break;
00115 
00116       default:
00117         Line.erase(i, 1);
00118       }
00119       break;
00120     }
00121   }
00122 
00123   *ins++ = PGSTD::string(Line, token);
00124 }
00125 
00126 
00127 template<typename TUPLE> 
00128 inline pqxx::tablereader &pqxx::tablereader::operator>>(TUPLE &T)
00129 {
00130   PGSTD::string Line;
00131   if (GetRawLine(Line)) Tokenize(Line, T);
00132   return *this;
00133 }
00134 
00135 
00136 
00137 #endif
00138 

Generated on Sat Jun 7 00:49:34 2003 for libpqxx by doxygen1.3-rc3