db_conn  v0.2.1-alpha
Database Connection API
result_set.hpp
1 /*
2  * File: result_set.hpp
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef RESULT_SET_HPP
20 #define RESULT_SET_HPP
21 
22 #include "utilities.hpp"
23 
24 namespace vgi { namespace dbconn { namespace dbi {
25 
26 class statement;
27 
33 {
34  // c++11 uses 'move' for return of std::string and std::vector and thus efficient
35  virtual ~iresult_set() { }
36  virtual bool has_data() = 0;
37  virtual bool more_results() = 0;
38  virtual size_t row_count() const = 0;
39  virtual size_t rows_affected() const = 0;
40  virtual size_t column_count() const = 0;
41  virtual bool next() = 0;
42  virtual bool prev() = 0;
43  virtual bool first() = 0;
44  virtual bool last() = 0;
45  virtual std::string column_name(size_t col_idx) = 0;
46  virtual int column_index(const std::string& col_name) = 0;
47  virtual bool is_null(size_t col_idx) = 0;
48  virtual int16_t get_short(size_t col_idx) = 0;
49  virtual uint16_t get_ushort(size_t col_idx) = 0;
50  virtual int32_t get_int(size_t col_idx) = 0;
51  virtual uint32_t get_uint(size_t col_idx) = 0;
52  virtual int64_t get_long(size_t col_idx) = 0;
53  virtual uint64_t get_ulong(size_t col_idx) = 0;
54  virtual float get_float(size_t col_idx) = 0;
55  virtual double get_double(size_t col_idx) = 0;
56  virtual bool get_bool(size_t col_idx) = 0;
57  virtual char get_char(size_t col_idx) = 0;
58  virtual std::string get_string(size_t col_idx) = 0;
59  virtual int get_date(size_t col_idx) = 0;
60  virtual double get_time(size_t col_idx) = 0;
61  virtual time_t get_datetime(size_t col_idx) = 0;
62  virtual char16_t get_u16char(size_t col_idx) = 0;
63  virtual std::u16string get_u16string(size_t col_idx) = 0;
64  virtual std::vector<uint8_t> get_binary(size_t col_idx) = 0;
65 };
66 
67 
79 {
80 public:
85  result_set(const result_set& rs) : rs_impl(rs.rs_impl)
86  {
87  }
88 
93  result_set(result_set&& rs) : rs_impl(std::move(rs.rs_impl))
94  {
95  }
96 
103  {
104  if (this != &rs)
105  rs_impl = rs.rs_impl;
106  return *this;
107  }
108 
115  {
116  if (this != &rs)
117  rs_impl = std::move(rs.rs_impl);
118  return *this;
119  }
120 
128  bool has_data()
129  {
130  return rs_impl->has_data();
131  }
132 
138  {
139  return rs_impl->more_results();
140  }
141 
146  size_t row_count() const
147  {
148  return rs_impl->row_count();
149  }
150 
155  size_t rows_affected() const
156  {
157  return rs_impl->rows_affected();
158  }
159 
164  size_t column_count() const
165  {
166  return rs_impl->column_count();
167  }
168 
175  const std::string column_name(size_t col_idx)
176  {
177  return std::move(rs_impl->column_name(col_idx));
178  }
179 
186  int column_index(const std::string& col_name)
187  {
188  return rs_impl->column_index(col_name);
189  }
190 
195  bool next()
196  {
197  return rs_impl->next();
198  }
199 
205  bool prev()
206  {
207  return rs_impl->prev();
208  }
209 
215  bool first()
216  {
217  return rs_impl->first();
218  }
219 
225  bool last()
226  {
227  return rs_impl->last();
228  }
229 
236  bool is_null(size_t col_idx)
237  {
238  return rs_impl->is_null(col_idx);
239  }
240 
247  bool is_null(const std::string& colname)
248  {
249  return rs_impl->is_null(rs_impl->column_index(colname));
250  }
251 
252 #define get_type_by_index(t) get_##t(size_t col_idx) { return rs_impl->get_##t(col_idx); }
253 #define get_type_by_name(t) get_##t(const std::string& colname) { return rs_impl->get_##t(rs_impl->column_index(colname)); }
254 
255  int16_t get_type_by_index(short);
256  int16_t get_type_by_name(short);
257  uint16_t get_type_by_index(ushort);
258  uint16_t get_type_by_name(ushort);
259  int32_t get_type_by_index(int);
260  int32_t get_type_by_name(int);
261  uint32_t get_type_by_index(uint);
262  uint32_t get_type_by_name(uint);
263  int64_t get_type_by_index(long);
264  int64_t get_type_by_name(long);
265  uint64_t get_type_by_index(ulong);
266  uint64_t get_type_by_name(ulong);
267  float get_type_by_index(float);
268  float get_type_by_name(float);
269  double get_type_by_index(double);
270  double get_type_by_name(double);
271  bool get_type_by_index(bool);
272  bool get_type_by_name(bool);
273  char get_type_by_index(char);
274  char get_type_by_name(char);
275  std::string get_type_by_index(string);
276  std::string get_type_by_name(string);
277  int get_type_by_index(date);
278  int get_type_by_name(date);
279  double get_type_by_index(time);
280  double get_type_by_name(time);
281  time_t get_type_by_index(datetime);
282  time_t get_type_by_name(datetime);
283  char16_t get_type_by_index(u16char);
284  char16_t get_type_by_name(u16char);
285  std::u16string get_type_by_index(u16string);
286  std::u16string get_type_by_name(u16string);
287  std::vector<uint8_t> get_type_by_index(binary);
288  std::vector<uint8_t> get_type_by_name(binary);
289 
290 
291 private:
292  friend class statement;
293  result_set(iresult_set* rs) : rs_impl(rs) { }
294 
295 private:
296  iresult_set* rs_impl;
297 
298 }; // result_set
299 
300 } } } // namespace vgi::dbconn::dbi
301 
302 #endif // RESULT_SET_HPP
303 
iresult_set - is an interface that describes common functionality for all concrete native implementat...
Definition: result_set.hpp:32
const std::string column_name(size_t col_idx)
Function returns column name by column index or throws an exception if index is invalid.
Definition: result_set.hpp:175
statement - is a class that manages native driver statement handle.
Definition: statement.hpp:72
bool has_data()
Function checks if current result set contains data.
Definition: result_set.hpp:128
bool more_results()
Function checks if there is another data set or status.
Definition: result_set.hpp:137
STL namespace.
size_t rows_affected() const
Function returns number of affected rows after execution of command.
Definition: result_set.hpp:155
result_set(const result_set &rs)
Copy constructor.
Definition: result_set.hpp:85
bool last()
Function moves iterator to the last row of the current result data set This function can only be used...
Definition: result_set.hpp:225
bool first()
Function moves iterator to the first row of the current result data set This function can only be use...
Definition: result_set.hpp:215
size_t column_count() const
Function returns number of columns of current result set or zero.
Definition: result_set.hpp:164
result_set & operator=(const result_set &rs)
Assignment copy operator.
Definition: result_set.hpp:102
bool is_null(const std::string &colname)
Function checks if cell data is NULL by column name or throws an exception if index is invalid...
Definition: result_set.hpp:247
int column_index(const std::string &col_name)
Function returns column index by column name, if column name is invalid then -1 is returned...
Definition: result_set.hpp:186
bool is_null(size_t col_idx)
Function checks if cell data is NULL by column index or throws an exception if index is invalid...
Definition: result_set.hpp:236
result_set & operator=(result_set &&rs)
Assignment move operator.
Definition: result_set.hpp:114
bool prev()
Function moves iterator to the previous row of the current result data set This function can only be ...
Definition: result_set.hpp:205
size_t row_count() const
Function returns current row count while iterating through the result set and total number of rows af...
Definition: result_set.hpp:146
result_set - is a class that manages native driver result_set handle.
Definition: result_set.hpp:78
bool next()
Function moves iterator to the next row of the current result data set.
Definition: result_set.hpp:195
result_set(result_set &&rs)
Move constructor.
Definition: result_set.hpp:93