00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00023
00024
00025
00026 namespace pqxx
00027 {
00028
00030
00042 class PQXX_LIBEXPORT TableReader : public TableStream
00043 {
00044 public:
00045 TableReader(Transaction_base &, const PGSTD::string &RName);
00046 ~TableReader();
00047
00048 template<typename TUPLE> TableReader &operator>>(TUPLE &);
00049
00050 operator bool() const throw () { return !m_Done; }
00051 bool operator!() const throw () { return m_Done; }
00052
00054
00057 bool GetRawLine(PGSTD::string &Line);
00058
00059 template<typename TUPLE>
00060 void Tokenize(PGSTD::string, TUPLE &) const;
00061
00062 private:
00063 bool m_Done;
00064 };
00065
00066 }
00067
00068
00069
00070
00071 template<typename TUPLE>
00072 inline void pqxx::TableReader::Tokenize(PGSTD::string Line,
00073 TUPLE &T) const
00074 {
00075 PGSTD::back_insert_iterator<TUPLE> ins = PGSTD::back_inserter(T);
00076
00077
00078 PGSTD::string::size_type token = 0;
00079 for (PGSTD::string::size_type i=0; i < Line.size(); ++i)
00080 {
00081 switch (Line[i])
00082 {
00083 case '\t':
00084 *ins++ = PGSTD::string(Line, token, i-token);
00085 token = i+1;
00086 break;
00087
00088 case '\\':
00089
00090 if ((i+1) >= Line.size())
00091 throw PGSTD::runtime_error("Row ends in backslash");
00092
00093 switch (Line[i+1])
00094 {
00095 case 'N':
00096
00097 Line.replace(i, 2, NullStr());
00098 i += NullStr().size() - 1;
00099 break;
00100
00101 case 't':
00102 Line.replace(i++, 2, "\t");
00103 break;
00104
00105 case 'n':
00106 Line.replace(i++, 2, "\n");
00107 break;
00108
00109 default:
00110 Line.erase(i, 1);
00111 }
00112 break;
00113 }
00114 }
00115
00116 *ins++ = PGSTD::string(Line, token);
00117 }
00118
00119
00120 template<typename TUPLE>
00121 inline pqxx::TableReader &pqxx::TableReader::operator>>(TUPLE &T)
00122 {
00123 PGSTD::string Line;
00124 if (GetRawLine(Line)) Tokenize(Line, T);
00125 return *this;
00126 }
00127
00128
00129
00130 #endif
00131