tablereader.hxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /** Definition of the pqxx::tablereader class.
  2. *
  3. * pqxx::tablereader enables optimized batch reads from a database table.
  4. *
  5. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/tablereader instead.
  6. *
  7. * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
  8. *
  9. * See COPYING for copyright license. If you did not receive a file called
  10. * COPYING with this source code, please notify the distributor of this mistake,
  11. * or contact the author.
  12. */
  13. #ifndef PQXX_H_TABLEREADER
  14. #define PQXX_H_TABLEREADER
  15. #include "pqxx/compiler-public.hxx"
  16. #include "pqxx/compiler-internal-pre.hxx"
  17. #include "pqxx/result.hxx"
  18. #include "pqxx/tablestream.hxx"
  19. namespace pqxx
  20. {
  21. /// @deprecated Use stream_from instead.
  22. /** Efficiently pull data directly out of a table.
  23. * @warning This class does not work reliably with multibyte encodings. Using
  24. * it with some multi-byte encodings may pose a security risk.
  25. */
  26. class PQXX_LIBEXPORT tablereader : public tablestream
  27. {
  28. public:
  29. PQXX_DEPRECATED tablereader(
  30. transaction_base &,
  31. const std::string &Name,
  32. const std::string &Null=std::string{});
  33. template<typename ITER>
  34. PQXX_DEPRECATED tablereader(
  35. transaction_base &,
  36. const std::string &Name,
  37. ITER begincolumns,
  38. ITER endcolumns);
  39. template<typename ITER>
  40. PQXX_DEPRECATED tablereader(
  41. transaction_base &,
  42. const std::string &Name,
  43. ITER begincolumns,
  44. ITER endcolumns,
  45. const std::string &Null);
  46. ~tablereader() noexcept;
  47. template<typename TUPLE> tablereader &operator>>(TUPLE &);
  48. operator bool() const noexcept { return not m_done; }
  49. bool operator!() const noexcept { return m_done; }
  50. bool get_raw_line(std::string &Line);
  51. template<typename TUPLE>
  52. void tokenize(std::string, TUPLE &) const;
  53. virtual void complete() override;
  54. private:
  55. void set_up(
  56. transaction_base &T,
  57. const std::string &RName,
  58. const std::string &Columns=std::string{});
  59. PQXX_PRIVATE void reader_close();
  60. std::string extract_field(
  61. const std::string &,
  62. std::string::size_type &) const;
  63. bool m_done;
  64. };
  65. template<typename ITER> inline
  66. tablereader::tablereader(
  67. transaction_base &T,
  68. const std::string &Name,
  69. ITER begincolumns,
  70. ITER endcolumns) :
  71. namedclass{Name, "tablereader"},
  72. tablestream{T, std::string{}},
  73. m_done{true}
  74. {
  75. set_up(T, Name, columnlist(begincolumns, endcolumns));
  76. }
  77. template<typename ITER> inline
  78. tablereader::tablereader(
  79. transaction_base &T,
  80. const std::string &Name,
  81. ITER begincolumns,
  82. ITER endcolumns,
  83. const std::string &Null) :
  84. namedclass{Name, "tablereader"},
  85. tablestream{T, Null},
  86. m_done{true}
  87. {
  88. set_up(T, Name, columnlist(begincolumns, endcolumns));
  89. }
  90. template<typename TUPLE>
  91. inline void tablereader::tokenize(std::string Line, TUPLE &T) const
  92. {
  93. std::back_insert_iterator<TUPLE> ins = std::back_inserter(T);
  94. std::string::size_type here = 0;
  95. while (here < Line.size()) *ins++ = extract_field(Line, here);
  96. }
  97. template<typename TUPLE>
  98. inline tablereader &pqxx::tablereader::operator>>(TUPLE &T)
  99. {
  100. std::string Line;
  101. if (get_raw_line(Line)) tokenize(Line, T);
  102. return *this;
  103. }
  104. } // namespace pqxx
  105. #include "pqxx/compiler-internal-post.hxx"
  106. #endif