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 <memory>
21 #include <stdexcept>
22 
23 #include "pqxx/except.hxx"
24 #include "pqxx/types.hxx"
25 #include "pqxx/util.hxx"
26 
27 #include "pqxx/internal/encodings.hxx"
28 
29 
30 // Methods tested in eg. test module test01 are marked with "//[t01]".
31 
32 namespace pqxx
33 {
34 namespace internal
35 {
36 PQXX_LIBEXPORT void clear_result(const pq::PGresult *);
37 
38 namespace gate
39 {
40 class result_connection;
41 class result_creation;
42 class result_row;
43 class result_sql_cursor;
44 } // namespace internal::gate
45 } // namespace internal
46 
47 
49 
69 class PQXX_LIBEXPORT result
70 {
71 public:
74  using reference = row;
80 
81  result() noexcept : //[t03]
82  m_data(make_data_pointer()),
83  m_query(),
84  m_encoding(internal::encoding_group::MONOBYTE)
85  {}
86  result(const result &rhs) noexcept =default; //[t01]
87 
88  result &operator=(const result &rhs) noexcept =default; //[t10]
89 
94  bool operator==(const result &) const noexcept; //[t70]
95  bool operator!=(const result &rhs) const noexcept //[t70]
96  { return not operator==(rhs); }
98 
99  const_reverse_iterator rbegin() const; //[t75]
100  const_reverse_iterator crbegin() const;
101  const_reverse_iterator rend() const; //[t75]
102  const_reverse_iterator crend() const;
103 
104  const_iterator begin() const noexcept; //[t01]
105  const_iterator cbegin() const noexcept;
106  inline const_iterator end() const noexcept; //[t01]
107  inline const_iterator cend() const noexcept;
108 
109  reference front() const noexcept; //[t74]
110  reference back() const noexcept; //[t75]
111 
112  PQXX_PURE size_type size() const noexcept; //[t02]
113  PQXX_PURE bool empty() const noexcept; //[t11]
114  size_type capacity() const noexcept { return size(); } //[t20]
115 
116  void swap(result &) noexcept; //[t77]
117 
118  const row operator[](size_type i) const noexcept; //[t02]
119  const row at(size_type) const; //[t10]
120 
121  void clear() noexcept { m_data.reset(); m_query = nullptr; } //[t20]
122 
127  PQXX_PURE row_size_type columns() const noexcept; //[t11]
129 
131  row_size_type column_number(const char ColName[]) const; //[t11]
132 
134  row_size_type column_number(const std::string &Name) const //[t11]
135  {return column_number(Name.c_str());}
136 
138  const char *column_name(row_size_type Number) const; //[t11]
139 
141  oid column_type(row_size_type ColNum) const; //[t07]
143  oid column_type(int ColNum) const //[t07]
144  { return column_type(row_size_type(ColNum)); }
145 
147  oid column_type(const std::string &ColName) const //[t07]
148  { return column_type(column_number(ColName)); }
149 
151  oid column_type(const char ColName[]) const //[t07]
152  { return column_type(column_number(ColName)); }
153 
155  oid column_table(row_size_type ColNum) const; //[t02]
156 
158  oid column_table(int ColNum) const //[t02]
159  { return column_table(row_size_type(ColNum)); }
160 
162  oid column_table(const std::string &ColName) const //[t02]
163  { return column_table(column_number(ColName)); }
164 
166  row_size_type table_column(row_size_type ColNum) const; //[t93]
167 
169  row_size_type table_column(int ColNum) const //[t93]
170  { return table_column(row_size_type(ColNum)); }
171 
173  row_size_type table_column(const std::string &ColName) const //[t93]
174  { return table_column(column_number(ColName)); }
176 
178  PQXX_PURE const std::string &query() const noexcept; //[t70]
179 
181 
184  PQXX_PURE oid inserted_oid() const; //[t13]
185 
187 
190  PQXX_PURE size_type affected_rows() const; //[t07]
191 
192 
193 private:
194  using data_pointer = std::shared_ptr<const internal::pq::PGresult>;
195 
197  data_pointer m_data;
198 
200  static data_pointer make_data_pointer(
201  const internal::pq::PGresult *res=nullptr)
202  { return data_pointer{res, internal::clear_result}; }
203 
205  std::shared_ptr<std::string> m_query;
206 
207  internal::encoding_group m_encoding;
208 
209  static const std::string s_empty_string;
210 
211  friend class pqxx::field;
212  PQXX_PURE const char *GetValue(size_type Row, row_size_type Col) const;
213  PQXX_PURE bool get_is_null(size_type Row, row_size_type Col) const;
214  PQXX_PURE field_size_type get_length(
215  size_type,
216  row_size_type) const noexcept;
217 
218  friend class pqxx::internal::gate::result_creation;
219  result(
220  internal::pq::PGresult *rhs,
221  const std::string &Query,
222  internal::encoding_group enc);
223 
224  PQXX_PRIVATE void check_status() const;
225 
226  friend class pqxx::internal::gate::result_connection;
227  friend class pqxx::internal::gate::result_row;
228  bool operator!() const noexcept { return not m_data.get(); }
229  operator bool() const noexcept { return m_data.get() != nullptr; }
230 
231  [[noreturn]] PQXX_PRIVATE void ThrowSQLError(
232  const std::string &Err,
233  const std::string &Query) const;
234  PQXX_PRIVATE PQXX_PURE int errorposition() const;
235  PQXX_PRIVATE std::string StatusError() const;
236 
237  friend class pqxx::internal::gate::result_sql_cursor;
238  PQXX_PURE const char *cmd_status() const noexcept;
239 };
240 } // namespace pqxx
241 #include "pqxx/compiler-internal-post.hxx"
242 #endif
oid column_type(int ColNum) const
Type of given column.
Definition: result.hxx:143
signed long result_difference_type
Difference between result sizes.
Definition: types.hxx:21
unsigned long result_size_type
Number of rows in a result set.
Definition: types.hxx:18
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:69
oid column_table(const std::string &ColName) const
What table did this column come from?
Definition: result.hxx:162
Reference to one row in a result.
Definition: row.hxx:40
void clear_result(const pq::PGresult *)
C++ wrapper for libpq&#39;s PQclear.
Definition: result.cxx:30
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:120
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:134
row_size_type table_column(const std::string &ColName) const
What column in its table did this column come from?
Definition: result.hxx:173
size_type capacity() const noexcept
Definition: result.hxx:114
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30
row_size_type table_column(int ColNum) const
What column in its table did this column come from?
Definition: result.hxx:169
Reference to a field in a result set.
Definition: field.hxx:49
result_difference_type difference_type
Definition: result.hxx:73
oid column_type(const char ColName[]) const
Type of given column.
Definition: result.hxx:151
result() noexcept
Definition: result.hxx:81
bool operator!=(const result &rhs) const noexcept
Definition: result.hxx:95
result_size_type size_type
Definition: result.hxx:72
void clear() noexcept
Definition: result.hxx:121
oid column_table(int ColNum) const
What table did this column come from?
Definition: result.hxx:158
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
oid column_type(const std::string &ColName) const
Type of given column.
Definition: result.hxx:147
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35