AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
resultSet.hpp
1 /*
2  ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3  Copyright (C) 2012 Vincent Lachenal
4 
5  This file is part of ANCH Framework.
6 
7  ANCH Framework is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  ANCH Framework is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef _ANCH_SQL_RESULT_SET_H_
21 #define _ANCH_SQL_RESULT_SET_H_
22 
23 #include <string>
24 #include <vector>
25 #include <sstream>
26 #include <map>
27 
28 #include "sql/sqlException.hpp"
29 
30 namespace anch {
31  namespace sql {
32 
44  class ResultSet {
45 
46  // Attributes +
47  protected:
49  std::map<std::string, std::size_t> _fields;
50  // Attributes -
51 
52  // Constructors +
53  public:
57  ResultSet();
58  // Constructors -
59 
60  // Destructor +
61  public:
65  virtual ~ResultSet();
66  // Destructor -
67 
68  // Methods +
81  template<typename T>
82  const T* get(std::size_t idx) throw(SqlException);
83 
97  template<typename T>
98  bool get(std::size_t idx, T& out) throw(SqlException);
99 
111  template<typename T>
112  bool get(const std::string field, T& out) throw(SqlException) {
113  auto search = _fields.find(field);
114  if(search == _fields.end()) {
115  std::ostringstream msg;
116  msg << "Field " << field << " does not exist in result set. "
117  "Available fields are [";
118  for(auto iter = _fields.cbegin() ; iter != _fields.cend() ; ++iter) {
119  if(iter != _fields.cbegin()) {
120  msg << ',';
121  }
122  msg << iter->first;
123  }
124  msg << ']';
125  throw SqlException(msg.str());
126  }
127  return get<T>(search->second, out);
128  }
129 
142  template<typename T>
143  const T* get(const std::string field) throw(SqlException) {
144  auto search = _fields.find(field);
145  if(search == _fields.end()) {
146  std::ostringstream msg;
147  msg << "Field " << field << " does not exist in result set. "
148  "Available fields are [";
149  for(auto iter = _fields.cbegin() ; iter != _fields.cend() ; ++iter) {
150  if(iter != _fields.cbegin()) {
151  msg << ',';
152  }
153  msg << iter->first;
154  }
155  msg << ']';
156  throw SqlException(msg.str());
157  }
158  return get<T>(search->second);
159  }
160 
168  virtual bool next() throw(SqlException) = 0;
169 
170  protected:
179  virtual bool getValue(std::size_t idx, std::string& out) throw(SqlException) = 0;
180  // Methods -
181 
182  };
183 
184  }
185 }
186 
187 #endif // _ANCH_SQL_RESULT_SET_H_
ResultSet()
Definition: resultSet.cpp:27
virtual bool next()=0
virtual bool getValue(std::size_t idx, std::string &out)=0
SQL exception.
Definition: sqlException.hpp:39
SQL result representation.
Definition: resultSet.hpp:44
AnCH framework base namespace.
Definition: base64.hpp:28
virtual ~ResultSet()
Definition: resultSet.cpp:31
std::map< std::string, std::size_t > _fields
Definition: resultSet.hpp:49