result.hxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /** Definitions for the pqxx::result class and support classes.
  2. *
  3. * pqxx::result represents the set of result rows from a database query.
  4. *
  5. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/result 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_RESULT
  14. #define PQXX_H_RESULT
  15. #include "pqxx/compiler-public.hxx"
  16. #include "pqxx/compiler-internal-pre.hxx"
  17. #include <ios>
  18. #include <memory>
  19. #include <stdexcept>
  20. #include "pqxx/except.hxx"
  21. #include "pqxx/types.hxx"
  22. #include "pqxx/util.hxx"
  23. #include "pqxx/internal/encodings.hxx"
  24. // Methods tested in eg. test module test01 are marked with "//[t01]".
  25. namespace pqxx
  26. {
  27. namespace internal
  28. {
  29. PQXX_LIBEXPORT void clear_result(const pq::PGresult *);
  30. namespace gate
  31. {
  32. class result_connection;
  33. class result_creation;
  34. class result_row;
  35. class result_sql_cursor;
  36. } // namespace internal::gate
  37. } // namespace internal
  38. enum class result_format
  39. {
  40. text = 0,
  41. binary = 1
  42. };
  43. /// Result set containing data returned by a query or command.
  44. /** This behaves as a container (as defined by the C++ standard library) and
  45. * provides random access const iterators to iterate over its rows. A row
  46. * can also be accessed by indexing a result R by the row's zero-based
  47. * number:
  48. *
  49. * @code
  50. * for (result::size_type i=0; i < R.size(); ++i) Process(R[i]);
  51. * @endcode
  52. *
  53. * Result sets in libpqxx are lightweight, reference-counted wrapper objects
  54. * which are relatively small and cheap to copy. Think of a result object as
  55. * a "smart pointer" to an underlying result set.
  56. *
  57. * @warning The result set that a result object points to is not thread-safe.
  58. * If you copy a result object, it still refers to the same underlying result
  59. * set. So never copy, destroy, query, or otherwise access a result while
  60. * another thread may be copying, destroying, querying, or otherwise accessing
  61. * the same result set--even if it is doing so through a different result
  62. * object!
  63. */
  64. class PQXX_LIBEXPORT result
  65. {
  66. public:
  67. using size_type = result_size_type;
  68. using difference_type = result_difference_type;
  69. using reference = row;
  70. using const_iterator = const_result_iterator;
  71. using pointer = const_iterator;
  72. using iterator = const_iterator;
  73. using const_reverse_iterator = const_reverse_result_iterator;
  74. using reverse_iterator = const_reverse_iterator;
  75. result() noexcept : //[t03]
  76. m_data(make_data_pointer()),
  77. m_query(),
  78. m_encoding(internal::encoding_group::MONOBYTE)
  79. {}
  80. result(const result &rhs) noexcept =default; //[t01]
  81. result &operator=(const result &rhs) noexcept =default; //[t10]
  82. /**
  83. * @name Comparisons
  84. */
  85. //@{
  86. bool operator==(const result &) const noexcept; //[t70]
  87. bool operator!=(const result &rhs) const noexcept //[t70]
  88. { return not operator==(rhs); }
  89. //@}
  90. const_reverse_iterator rbegin() const; //[t75]
  91. const_reverse_iterator crbegin() const;
  92. const_reverse_iterator rend() const; //[t75]
  93. const_reverse_iterator crend() const;
  94. const_iterator begin() const noexcept; //[t01]
  95. const_iterator cbegin() const noexcept;
  96. inline const_iterator end() const noexcept; //[t01]
  97. inline const_iterator cend() const noexcept;
  98. reference front() const noexcept; //[t74]
  99. reference back() const noexcept; //[t75]
  100. PQXX_PURE size_type size() const noexcept; //[t02]
  101. PQXX_PURE bool empty() const noexcept; //[t11]
  102. size_type capacity() const noexcept { return size(); } //[t20]
  103. void swap(result &) noexcept; //[t77]
  104. const row operator[](size_type i) const noexcept; //[t02]
  105. const row at(size_type) const; //[t10]
  106. void clear() noexcept { m_data.reset(); m_query = nullptr; } //[t20]
  107. /**
  108. * @name Column information
  109. */
  110. //@{
  111. /// Number of columns in result.
  112. PQXX_PURE row_size_type columns() const noexcept; //[t11]
  113. /// Number of given column (throws exception if it doesn't exist).
  114. row_size_type column_number(const char ColName[]) const; //[t11]
  115. /// Number of given column (throws exception if it doesn't exist).
  116. row_size_type column_number(const std::string &Name) const //[t11]
  117. {return column_number(Name.c_str());}
  118. /// Name of column with this number (throws exception if it doesn't exist)
  119. const char *column_name(row_size_type Number) const; //[t11]
  120. /// Type of given column
  121. oid column_type(row_size_type ColNum) const; //[t07]
  122. /// Type of given column
  123. oid column_type(int ColNum) const //[t07]
  124. { return column_type(row_size_type(ColNum)); }
  125. /// Type of given column
  126. oid column_type(const std::string &ColName) const //[t07]
  127. { return column_type(column_number(ColName)); }
  128. /// Type of given column
  129. oid column_type(const char ColName[]) const //[t07]
  130. { return column_type(column_number(ColName)); }
  131. /// What table did this column come from?
  132. oid column_table(row_size_type ColNum) const; //[t02]
  133. /// What table did this column come from?
  134. oid column_table(int ColNum) const //[t02]
  135. { return column_table(row_size_type(ColNum)); }
  136. /// What table did this column come from?
  137. oid column_table(const std::string &ColName) const //[t02]
  138. { return column_table(column_number(ColName)); }
  139. /// What column in its table did this column come from?
  140. row_size_type table_column(row_size_type ColNum) const; //[t93]
  141. /// What column in its table did this column come from?
  142. row_size_type table_column(int ColNum) const //[t93]
  143. { return table_column(row_size_type(ColNum)); }
  144. /// What column in its table did this column come from?
  145. row_size_type table_column(const std::string &ColName) const //[t93]
  146. { return table_column(column_number(ColName)); }
  147. //@}
  148. /// Query that produced this result, if available (empty string otherwise)
  149. PQXX_PURE const std::string &query() const noexcept; //[t70]
  150. /// If command was @c INSERT of 1 row, return oid of inserted row
  151. /** @return Identifier of inserted row if exactly one row was inserted, or
  152. * oid_none otherwise.
  153. */
  154. PQXX_PURE oid inserted_oid() const; //[t13]
  155. /// If command was @c INSERT, @c UPDATE, or @c DELETE: number of affected rows
  156. /** @return Number of affected rows if last command was @c INSERT, @c UPDATE,
  157. * or @c DELETE; zero for all other commands.
  158. */
  159. PQXX_PURE size_type affected_rows() const; //[t07]
  160. private:
  161. using data_pointer = std::shared_ptr<const internal::pq::PGresult>;
  162. /// Underlying libpq result set.
  163. data_pointer m_data;
  164. /// Factory for data_pointer.
  165. static data_pointer make_data_pointer(
  166. const internal::pq::PGresult *res=nullptr)
  167. { return data_pointer{res, internal::clear_result}; }
  168. /// Query string.
  169. std::shared_ptr<std::string> m_query;
  170. internal::encoding_group m_encoding;
  171. static const std::string s_empty_string;
  172. friend class pqxx::field;
  173. PQXX_PURE const char *GetValue(size_type Row, row_size_type Col) const;
  174. PQXX_PURE bool get_is_null(size_type Row, row_size_type Col) const;
  175. PQXX_PURE field_size_type get_length(
  176. size_type,
  177. row_size_type) const noexcept;
  178. friend class pqxx::internal::gate::result_creation;
  179. result(
  180. internal::pq::PGresult *rhs,
  181. const std::string &Query,
  182. internal::encoding_group enc);
  183. PQXX_PRIVATE void check_status() const;
  184. friend class pqxx::internal::gate::result_connection;
  185. friend class pqxx::internal::gate::result_row;
  186. bool operator!() const noexcept { return not m_data.get(); }
  187. operator bool() const noexcept { return m_data.get() != nullptr; }
  188. [[noreturn]] PQXX_PRIVATE void ThrowSQLError(
  189. const std::string &Err,
  190. const std::string &Query) const;
  191. PQXX_PRIVATE PQXX_PURE int errorposition() const;
  192. PQXX_PRIVATE std::string StatusError() const;
  193. friend class pqxx::internal::gate::result_sql_cursor;
  194. PQXX_PURE const char *cmd_status() const noexcept;
  195. };
  196. } // namespace pqxx
  197. #include "pqxx/compiler-internal-post.hxx"
  198. #endif