AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
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#pragma once
21
22#include <string>
23#include <vector>
24#include <sstream>
25#include <map>
26#include <optional>
27
28#include "sql/sqlException.hpp"
29
30#include "date/dateFormatter.hpp"
31#include "sql/types/date.hpp"
32#include "sql/types/time.hpp"
33#include "sql/types/timestamp.hpp"
34
35
36namespace anch {
37 namespace sql {
38
50 class ResultSet {
51
52 // Attributes +
53 protected:
55 std::map<std::string, std::size_t> _fields;
56 // Attributes -
57
58 // Constructors +
59 public:
63 ResultSet() noexcept;
64 // Constructors -
65
66 // Destructor +
67 public:
71 virtual ~ResultSet() noexcept;
72 // Destructor -
73
74 // Methods +
88 template<typename T>
89 bool get(std::size_t idx, T& out);
90
102 template<typename T>
103 bool get(const std::string field, T& out) {
104 auto search = _fields.find(field);
105 if(search == _fields.end()) {
106 std::ostringstream msg;
107 msg << "Field " << field << " does not exist in result set. "
108 "Available fields are [";
109 for(auto iter = _fields.cbegin() ; iter != _fields.cend() ; ++iter) {
110 if(iter != _fields.cbegin()) {
111 msg << ',';
112 }
113 msg << iter->first;
114 }
115 msg << ']';
116 throw SqlException(msg.str());
117 }
118 return get<T>(search->second, out);
119 }
120
132 template<typename T>
133 std::optional<T> get(std::size_t idx);
134
146 template<typename T>
147 const std::optional<T> get(const std::string field) {
148 auto search = _fields.find(field);
149 if(search == _fields.end()) {
150 std::ostringstream msg;
151 msg << "Field " << field << " does not exist in result set. "
152 "Available fields are [";
153 for(auto iter = _fields.cbegin() ; iter != _fields.cend() ; ++iter) {
154 if(iter != _fields.cbegin()) {
155 msg << ',';
156 }
157 msg << iter->first;
158 }
159 msg << ']';
160 throw SqlException(msg.str());
161 }
162 return get<T>(search->second);
163 }
164
172 virtual bool next() = 0;
173
174 protected:
183 virtual bool getValue(std::size_t idx, std::string& out) = 0;
184
194 virtual std::optional<std::string> getValue(std::size_t idx) = 0;
195
202
209
216
223
230
237 // Methods -
238
239 };
240
241 }
242}
Definition dateFormatter.hpp:58
virtual std::optional< std::string > getValue(std::size_t idx)=0
virtual bool getValue(std::size_t idx, std::string &out)=0
const anch::date::DateFormatter & getDefaultTimeFormatter()
const std::optional< T > get(const std::string field)
Definition resultSet.hpp:147
std::map< std::string, std::size_t > _fields
Definition resultSet.hpp:55
virtual const anch::date::DateFormatter & getTimestampFormatter()=0
virtual bool next()=0
virtual const anch::date::DateFormatter & getDateFormatter()=0
bool get(std::size_t idx, T &out)
const anch::date::DateFormatter & getDefaultTimestampFormatter()
virtual const anch::date::DateFormatter & getTimeFormatter()=0
const anch::date::DateFormatter & getDefaultDateFormatter()
std::optional< T > get(std::size_t idx)
SQL exception.
Definition sqlException.hpp:38
SQL namespace.
Definition clauses.hpp:26
AnCH framework base namespace.
Definition app.hpp:28