fastcgi++  3.1alpha
A C++ FastCGI/Web API
results.hpp
Go to the documentation of this file.
1 
10 /*******************************************************************************
11 * Copyright (C) 2020 Eddie Carle [eddie@isatec.ca] *
12 * *
13 * This file is part of fastcgi++. *
14 * *
15 * fastcgi++ is free software: you can redistribute it and/or modify it under *
16 * the terms of the GNU Lesser General Public License as published by the Free *
17 * Software Foundation, either version 3 of the License, or (at your option) *
18 * any later version. *
19 * *
20 * fastcgi++ is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for *
23 * more details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with fastcgi++. If not, see <http://www.gnu.org/licenses/>. *
27 *******************************************************************************/
28 
29 #ifndef FASTCGIPP_SQL_RESULTS_HPP
30 #define FASTCGIPP_SQL_RESULTS_HPP
31 
32 #include <tuple>
33 #include <string>
34 #include <vector>
35 #include <chrono>
36 
37 #include "fastcgi++/address.hpp"
38 
40 namespace Fastcgipp
41 {
43  namespace SQL
44  {
46  enum class Status
47  {
48  noResult,
49  emptyQuery,
50  commandOk,
51  rowsOk,
52  copyOut,
53  copyIn,
56  fatalError,
57  copyBoth,
59  };
60 
62  const char* statusString(const Status status);
63 
64  class Connection;
65 
67  class Results_base
68  {
69  public:
71 
76  Status status() const;
77 
79  const char* errorMessage() const;
80 
82  unsigned rows() const;
83 
85  unsigned affectedRows() const;
86 
88  bool null(int row, int column) const;
89 
90  virtual ~Results_base();
91 
92  protected:
93  friend class Connection;
94 
96  void* m_res;
97 
98  Results_base():
99  m_res(nullptr)
100  {}
101 
103  int columns() const;
104 
106 
110  template<typename T> bool verifyColumn(int column) const;
111 
113  template<typename Numeric> void field(
114  int row,
115  int column,
116  std::vector<Numeric>& value) const;
117 
119 
123  template<typename T> void field(
124  int row,
125  int column,
126  T& value) const;
127  };
128 
130 
146  template<typename... Types>
147  class Results: public Results_base
148  {
149  public:
151  typedef std::tuple<Types...> Row;
152 
153  private:
155  static const int size = sizeof...(Types);
156 
157  template<int column, int... columns>
158  int verify_impl(
159  std::integer_sequence<int, column, columns...>) const
160  {
161  if(verifyColumn<
162  typename std::tuple_element<column, Row>::type>(column))
163  return verify_impl(
164  std::integer_sequence<int, columns...>{});
165  else
166  return column+1;
167  }
168 
169  template<int column>
170  int verify_impl(std::integer_sequence<int, column>) const
171  {
172  if(verifyColumn<
173  typename std::tuple_element<column, Row>::type>(column))
174  return 0;
175  else
176  return column+1;
177  }
178 
179  template<int column, int... columns> void row_impl(
180  Row &row,
181  int index,
182  std::integer_sequence<int, column, columns...>) const
183  {
184  field(index, column, std::get<column>(row));
185  row_impl(row, index, std::integer_sequence<int, columns...>{});
186  }
187 
188  template<int column> void row_impl(
190  int index,
191  std::integer_sequence<int, column>) const
192  {
193  field(index, column, std::get<column>(row));
194  }
195 
196  public:
198 
210  int verify() const
211  {
212  if(columns() != size)
213  return -1;
214  else
215  return verify_impl(
216  std::make_integer_sequence<int, size>{});
217  }
218 
220 
230  Row row(int index) const
231  {
232  Row row;
233  row_impl(
234  row,
235  index,
236  std::make_integer_sequence<int, size>{});
237  return row;
238  }
239  };
240 
242  template<>
243  class Results<>: public Results_base
244  {
245  public:
247  int verify() const
248  {
249  if(columns() == 0 && rows() ==0)
250  return 0;
251  else
252  return -1;
253  }
254  };
255  }
256 }
257 
258 #endif
Fastcgipp::SQL::Status::commandOk
@ commandOk
Fastcgipp::SQL::Status::fatalError
@ fatalError
Fastcgipp::SQL::Results::Row
std::tuple< Types... > Row
Type returned for row retrieval requests.
Definition: results.hpp:185
Fastcgipp::SQL::Status::singleTuple
@ singleTuple
Fastcgipp::SQL::Results::row
Row row(int index) const
Retrieve row data in tuple form.
Definition: results.hpp:264
Fastcgipp::SQL::Results_base::field
void field(int row, int column, std::vector< Numeric > &value) const
Extract typed array from specific row and column.
Definition: results.cpp:213
Fastcgipp::SQL::Results_base::columns
int columns() const
How many columns are associated with the underlying results.
Definition: results.cpp:492
Fastcgipp::SQL::Status
Status
Response type for SQL query results statuses.
Definition: results.hpp:80
Fastcgipp::SQL::Results::verify
int verify() const
Verify consistency between the tuple and the SQL data.
Definition: results.hpp:244
Fastcgipp::SQL::Status::noResult
@ noResult
Fastcgipp::SQL::Results::verify_impl
int verify_impl(std::integer_sequence< int, column, columns... >) const
Definition: results.hpp:192
Fastcgipp::SQL::Status::nonfatalError
@ nonfatalError
Fastcgipp::SQL::Results::row_impl
void row_impl(Row &row, int index, std::integer_sequence< int, column, columns... >) const
Definition: results.hpp:213
address.hpp
Fastcgipp::SQL::Results_base
De-templated base class for SQL query result sets.
Definition: results.hpp:101
Fastcgipp::SQL::Results_base::errorMessage
const char * errorMessage() const
Get error message associated with SQL query result.
Definition: results.cpp:474
Fastcgipp::SQL::Status::badResponse
@ badResponse
Fastcgipp::SQL::Status::rowsOk
@ rowsOk
Fastcgipp
Topmost namespace for the fastcgi++ library.
Definition: fcgistreambuf.cpp:34
Fastcgipp::SQL::Status::copyIn
@ copyIn
Fastcgipp::SQL::statusString
const char * statusString(const Status status)
Returns a text description of the specified SQL query result status.
Definition: results.cpp:497
Fastcgipp::SQL::Connection
Handles low level communication with "the other side".
Definition: connection.hpp:113
Fastcgipp::SQL::Results_base::affectedRows
unsigned affectedRows() const
How many rows were affected from the SQL query.
Definition: results.cpp:463
Fastcgipp::SQL::Results_base::rows
unsigned rows() const
How many rows were returned with the SQL query.
Definition: results.cpp:479
Fastcgipp::SQL::Status::emptyQuery
@ emptyQuery
Fastcgipp::SQL::Status::copyBoth
@ copyBoth
Fastcgipp::SQL::Results::size
static const int size
How many columns in the row?
Definition: results.hpp:189
Fastcgipp::SQL::Results_base::m_res
void * m_res
Pointer to underlying SQL results object.
Definition: results.hpp:130
Fastcgipp::SQL::Results
Holds SQL query result sets.
Definition: results.hpp:181
Fastcgipp::SQL::Results_base::Results_base
Results_base()
Definition: results.hpp:132
Fastcgipp::SQL::Results_base::verifyColumn
bool verifyColumn(int column) const
Verify type and size consistency for the specified column.
Definition: results.cpp:41
Fastcgipp::SQL::Results_base::~Results_base
virtual ~Results_base()
Definition: results.cpp:468
Fastcgipp::SQL::Status::copyOut
@ copyOut
Fastcgipp::SQL::Results_base::status
Status status() const
Get status of SQL query result.
Definition: results.cpp:433