00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <string>
00020
00021 #include "pqxx/result"
00022 #include "pqxx/tablestream"
00023
00024
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);
00048 ~tablereader();
00049
00050 template<typename TUPLE> tablereader &operator>>(TUPLE &);
00051
00052 operator bool() const throw () { return !m_Done; }
00053 bool operator!() const throw () { return m_Done; }
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);
00068
00069 template<typename TUPLE>
00070 void tokenize(PGSTD::string, TUPLE &) const;
00071
00072 private:
00073 bool m_Done;
00074 };
00075
00076
00077 }
00078
00079
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
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':
00095 *ins++ = PGSTD::string(Line, token, i-token);
00096 token = i+1;
00097 break;
00098
00099 case '\\':
00100
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
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