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 // TODO: Support SQL arrays
29 
30 namespace pqxx
31 {
32 namespace internal
33 {
34 PQXX_LIBEXPORT void clear_result(const pq::PGresult *);
35 
36 namespace gate
37 {
38 class result_connection;
39 class result_creation;
40 class result_row;
41 class result_sql_cursor;
42 } // namespace internal::gate
43 } // namespace internal
44 
45 
47 
67 class PQXX_LIBEXPORT result
68 {
69 public:
72  using reference = row;
78 
79  result() noexcept : m_data(make_data_pointer()), m_query() {} //[t03]
80  result(const result &rhs) noexcept : //[t01]
81  m_data(rhs.m_data), m_query(rhs.m_query) {}
82 
83  result &operator=(const result &rhs) noexcept //[t10]
84  {
85  if (&rhs != this)
86  {
87  m_data = rhs.m_data;
88  m_query = rhs.m_query;
89  }
90  return *this;
91  }
92 
97  bool operator==(const result &) const noexcept; //[t70]
98  bool operator!=(const result &rhs) const noexcept //[t70]
99  { return !operator==(rhs); }
101 
102  const_reverse_iterator rbegin() const; //[t75]
103  const_reverse_iterator crbegin() const;
104  const_reverse_iterator rend() const; //[t75]
105  const_reverse_iterator crend() const;
106 
107  const_iterator begin() const noexcept; //[t01]
108  const_iterator cbegin() const noexcept;
109  inline const_iterator end() const noexcept; //[t01]
110  inline const_iterator cend() const noexcept;
111 
112  reference front() const noexcept; //[t74]
113  reference back() const noexcept; //[t75]
114 
115  PQXX_PURE size_type size() const noexcept; //[t02]
116  PQXX_PURE bool empty() const noexcept; //[t11]
117  size_type capacity() const noexcept { return size(); } //[t20]
118 
119  void swap(result &) noexcept; //[t77]
120 
121  const row operator[](size_type i) const noexcept; //[t02]
122  const row at(size_type) const; //[t10]
123 
124  void clear() noexcept { m_data.reset(); m_query.erase(); } //[t20]
125 
130  PQXX_PURE row_size_type columns() const noexcept; //[t11]
132 
134  row_size_type column_number(const char ColName[]) const; //[t11]
135 
137  row_size_type column_number(const std::string &Name) const //[t11]
138  {return column_number(Name.c_str());}
139 
141  const char *column_name(row_size_type Number) const; //[t11]
142 
144  oid column_type(row_size_type ColNum) const; //[t07]
146  oid column_type(int ColNum) const //[t07]
147  { return column_type(row_size_type(ColNum)); }
148 
150  oid column_type(const std::string &ColName) const //[t07]
151  { return column_type(column_number(ColName)); }
152 
154  oid column_type(const char ColName[]) const //[t07]
155  { return column_type(column_number(ColName)); }
156 
158  oid column_table(row_size_type ColNum) const; //[t02]
159 
161  oid column_table(int ColNum) const //[t02]
162  { return column_table(row_size_type(ColNum)); }
163 
165  oid column_table(const std::string &ColName) const //[t02]
166  { return column_table(column_number(ColName)); }
167 
169  row_size_type table_column(row_size_type ColNum) const; //[t93]
170 
172  row_size_type table_column(int ColNum) const //[t93]
173  { return table_column(row_size_type(ColNum)); }
174 
176  row_size_type table_column(const std::string &ColName) const //[t93]
177  { return table_column(column_number(ColName)); }
179 
181  PQXX_PURE const std::string &query() const noexcept; //[t70]
182 
184 
187  PQXX_PURE oid inserted_oid() const; //[t13]
188 
190 
193  PQXX_PURE size_type affected_rows() const; //[t07]
194 
195 
196 private:
197  using data_pointer = std::shared_ptr<const internal::pq::PGresult>;
198 
200  data_pointer m_data;
201 
203  static data_pointer make_data_pointer(
204  const internal::pq::PGresult *res=nullptr)
205  { return data_pointer(res, internal::clear_result); }
206 
208  std::string m_query;
209 
210  friend class pqxx::field;
211  PQXX_PURE const char *GetValue(size_type Row, row_size_type Col) const;
212  PQXX_PURE bool get_is_null(size_type Row, row_size_type Col) const;
213  PQXX_PURE field_size_type get_length(
214  size_type,
215  row_size_type) const noexcept;
216 
217  friend class pqxx::internal::gate::result_creation;
218  result(internal::pq::PGresult *rhs, const std::string &Query);
219  PQXX_PRIVATE void check_status() const;
220 
221  friend class pqxx::internal::gate::result_connection;
222  friend class pqxx::internal::gate::result_row;
223  bool operator!() const noexcept { return !m_data.get(); }
224  operator bool() const noexcept { return m_data.get() != nullptr; }
225 
226  [[noreturn]] PQXX_PRIVATE void ThrowSQLError(
227  const std::string &Err,
228  const std::string &Query) const;
229  PQXX_PRIVATE PQXX_PURE int errorposition() const;
230  PQXX_PRIVATE std::string StatusError() const;
231 
232  friend class pqxx::internal::gate::result_sql_cursor;
233  PQXX_PURE const char *cmd_status() const noexcept;
234 };
235 } // namespace pqxx
236 #include "pqxx/compiler-internal-post.hxx"
237 #endif
oid column_table(int ColNum) const
What table did this column come from?
Definition: result.hxx:161
bool operator!=(const result &rhs) const noexcept
Definition: result.hxx:98
result(const result &rhs) noexcept
Definition: result.hxx:80
Reference to one row in a result.
Definition: row.hxx:40
oid column_type(const char ColName[]) const
Type of given column.
Definition: result.hxx:154
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:125
result_difference_type difference_type
Definition: result.hxx:71
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
oid column_table(const std::string &ColName) const
What table did this column come from?
Definition: result.hxx:165
void clear() noexcept
Definition: result.hxx:124
result() noexcept
Definition: result.hxx:79
Result set containing data returned by a query or command.
Definition: result.hxx:67
result_size_type size_type
Definition: result.hxx:70
unsigned int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
size_type capacity() const noexcept
Definition: result.hxx:117
oid column_type(const std::string &ColName) const
Type of given column.
Definition: result.hxx:150
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:137
signed long result_difference_type
Difference between result sizes.
Definition: types.hxx:21
result & operator=(const result &rhs) noexcept
Definition: result.hxx:83
unsigned long result_size_type
Number of rows in a result set.
Definition: types.hxx:18
void clear_result(const pq::PGresult *)
C++ wrapper for libpq&#39;s PQclear.
Definition: result.cxx:24
row_size_type table_column(int ColNum) const
What column in its table did this column come from?
Definition: result.hxx:172
oid column_type(int ColNum) const
Type of given column.
Definition: result.hxx:146
Reference to a field in a result set.
Definition: field.hxx:41
row_size_type table_column(const std::string &ColName) const
What column in its table did this column come from?
Definition: result.hxx:176
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30