libpqxx
result.hxx
1 
13 #ifndef PQXX_H_RESULT
14 #define PQXX_H_RESULT
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/compiler-internal-pre.hxx"
18 
19 #include <ios>
20 #include <stdexcept>
21 
22 #include "pqxx/except.hxx"
23 #include "pqxx/types.hxx"
24 #include "pqxx/util.hxx"
25 
26 // Methods tested in eg. test module test01 are marked with "//[t01]".
27 
28 namespace pqxx
29 {
30 namespace internal
31 {
32 PQXX_LIBEXPORT void clear_result(const pq::PGresult *);
33 
34 namespace gate
35 {
36 class result_connection;
37 class result_creation;
38 class result_row;
39 class result_sql_cursor;
40 } // namespace internal::gate
41 } // namespace internal
42 
43 
45 
65 class PQXX_LIBEXPORT result
66 {
67 public:
70  using reference = row;
76 
77  result() noexcept : m_data(make_data_pointer()), m_query() {} //[t03]
78  result(const result &rhs) noexcept : //[t01]
79  m_data(rhs.m_data), m_query(rhs.m_query) {}
80 
81  result &operator=(const result &rhs) noexcept //[t10]
82  {
83  if (&rhs != this)
84  {
85  m_data = rhs.m_data;
86  m_query = rhs.m_query;
87  }
88  return *this;
89  }
90 
95  bool operator==(const result &) const noexcept; //[t70]
96  bool operator!=(const result &rhs) const noexcept //[t70]
97  { return !operator==(rhs); }
99 
100  const_reverse_iterator rbegin() const; //[t75]
101  const_reverse_iterator crbegin() const;
102  const_reverse_iterator rend() const; //[t75]
103  const_reverse_iterator crend() const;
104 
105  const_iterator begin() const noexcept; //[t01]
106  const_iterator cbegin() const noexcept;
107  inline const_iterator end() const noexcept; //[t01]
108  inline const_iterator cend() const noexcept;
109 
110  reference front() const noexcept; //[t74]
111  reference back() const noexcept; //[t75]
112 
113  PQXX_PURE size_type size() const noexcept; //[t02]
114  PQXX_PURE bool empty() const noexcept; //[t11]
115  size_type capacity() const noexcept { return size(); } //[t20]
116 
117  void swap(result &) noexcept; //[t77]
118 
119  const row operator[](size_type i) const noexcept; //[t02]
120  const row at(size_type) const; //[t10]
121 
122  void clear() noexcept { m_data.reset(); m_query.erase(); } //[t20]
123 
128  PQXX_PURE row_size_type columns() const noexcept; //[t11]
130 
132  row_size_type column_number(const char ColName[]) const; //[t11]
133 
135  row_size_type column_number(const std::string &Name) const //[t11]
136  {return column_number(Name.c_str());}
137 
139  const char *column_name(row_size_type Number) const; //[t11]
140 
142  oid column_type(row_size_type ColNum) const; //[t07]
144  oid column_type(int ColNum) const //[t07]
145  { return column_type(row_size_type(ColNum)); }
146 
148  oid column_type(const std::string &ColName) const //[t07]
149  { return column_type(column_number(ColName)); }
150 
152  oid column_type(const char ColName[]) const //[t07]
153  { return column_type(column_number(ColName)); }
154 
156  oid column_table(row_size_type ColNum) const; //[t02]
157 
159  oid column_table(int ColNum) const //[t02]
160  { return column_table(row_size_type(ColNum)); }
161 
163  oid column_table(const std::string &ColName) const //[t02]
164  { return column_table(column_number(ColName)); }
165 
167  row_size_type table_column(row_size_type ColNum) const; //[t93]
168 
170  row_size_type table_column(int ColNum) const //[t93]
171  { return table_column(row_size_type(ColNum)); }
172 
174  row_size_type table_column(const std::string &ColName) const //[t93]
175  { return table_column(column_number(ColName)); }
177 
179  PQXX_PURE const std::string &query() const noexcept; //[t70]
180 
182 
185  PQXX_PURE oid inserted_oid() const; //[t13]
186 
188 
191  PQXX_PURE size_type affected_rows() const; //[t07]
192 
193 
194 private:
195  using data_pointer = std::shared_ptr<const internal::pq::PGresult>;
196 
198  data_pointer m_data;
199 
201  static data_pointer make_data_pointer(
202  const internal::pq::PGresult *res=nullptr)
203  { return data_pointer(res, internal::clear_result); }
204 
206  std::string m_query;
207 
208  friend class pqxx::field;
209  PQXX_PURE const char *GetValue(size_type Row, row_size_type Col) const;
210  PQXX_PURE bool get_is_null(size_type Row, row_size_type Col) const;
211  PQXX_PURE field_size_type get_length(
212  size_type,
213  row_size_type) const noexcept;
214 
215  friend class pqxx::internal::gate::result_creation;
216  result(internal::pq::PGresult *rhs, const std::string &Query);
217  PQXX_PRIVATE void check_status() const;
218 
219  friend class pqxx::internal::gate::result_connection;
220  friend class pqxx::internal::gate::result_row;
221  bool operator!() const noexcept { return !m_data.get(); }
222  operator bool() const noexcept { return m_data.get() != nullptr; }
223 
224  [[noreturn]] PQXX_PRIVATE void ThrowSQLError(
225  const std::string &Err,
226  const std::string &Query) const;
227  PQXX_PRIVATE PQXX_PURE int errorposition() const;
228  PQXX_PRIVATE std::string StatusError() const;
229 
230  friend class pqxx::internal::gate::result_sql_cursor;
231  PQXX_PURE const char *cmd_status() const noexcept;
232 };
233 } // namespace pqxx
234 #include "pqxx/compiler-internal-post.hxx"
235 #endif
unsigned int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
Result set containing data returned by a query or command.
Definition: result.hxx:65
oid column_type(const std::string &ColName) const
Type of given column.
Definition: result.hxx:148
unsigned long result_size_type
Number of rows in a result set.
Definition: types.hxx:18
Reference to one row in a result.
Definition: row.hxx:40
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:125
void clear_result(const pq::PGresult *)
C++ wrapper for libpq&#39;s PQclear.
Definition: result.cxx:24
result & operator=(const result &rhs) noexcept
Definition: result.hxx:81
size_type capacity() const noexcept
Definition: result.hxx:115
oid column_type(int ColNum) const
Type of given column.
Definition: result.hxx:144
result() noexcept
Definition: result.hxx:77
signed long result_difference_type
Difference between result sizes.
Definition: types.hxx:21
row_size_type table_column(int ColNum) const
What column in its table did this column come from?
Definition: result.hxx:170
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35
bool operator!=(const result &rhs) const noexcept
Definition: result.hxx:96
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30
oid column_type(const char ColName[]) const
Type of given column.
Definition: result.hxx:152
result(const result &rhs) noexcept
Definition: result.hxx:78
result_difference_type difference_type
Definition: result.hxx:69
oid column_table(int ColNum) const
What table did this column come from?
Definition: result.hxx:159
Reference to a field in a result set.
Definition: field.hxx:48
oid column_table(const std::string &ColName) const
What table did this column come from?
Definition: result.hxx:163
void clear() noexcept
Definition: result.hxx:122
row_size_type table_column(const std::string &ColName) const
What column in its table did this column come from?
Definition: result.hxx:174
result_size_type size_type
Definition: result.hxx:68
row_size_type column_number(const std::string &Name) const
Number of given column (throws exception if it doesn&#39;t exist).
Definition: result.hxx:135