AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
resultSet.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 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 <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-sql-export.hpp"
23
24#include <chrono>
25#include <concepts>
26#include <string>
27#include <vector>
28#include <sstream>
29#include <map>
30#include <optional>
31
32#include "sql/sqlException.hpp"
33
34#include "date/dateFormatter.hpp"
35#include "date/date.hpp"
36#include "date/dateTime.hpp"
37#include "date/time.hpp"
38
39#ifdef ANCH_UUID
40#include "uuid.hpp"
41#endif
42
43
44namespace anch::sql {
45
53 template<typename T>
54 concept SystemTimePoint = requires { typename T::duration; }
55 && std::same_as<T, std::chrono::sys_time<typename T::duration>>;
56
57 class Connection;
59
73 class ANCH_SQL_EXPORT ResultSet {
74
75 friend class Connection;
76 friend class PreparedStatement;
77
78 // Attributes +
79 protected:
81 std::map<std::string, std::size_t> _fields;
82
83 private:
85 Connection* _connection;
86
88 PreparedStatement* _statement;
89 // Attributes -
90
91 // Constructors +
92 public:
96 ResultSet() noexcept;
97 // Constructors -
98
99 // Destructor +
100 public:
104 virtual ~ResultSet() noexcept;
105 // Destructor -
106
107 // Methods +
123 template<typename T>
124 bool get(std::size_t idx, T& out);
125
142 template<anch::sql::SystemTimePoint T>
143 bool get(std::size_t idx, T& out);
144
156 template<typename T>
157 bool get(const std::string field, T& out) {
158 auto search = _fields.find(field);
159 if(search == _fields.end()) {
160 std::ostringstream msg;
161 msg << "Field " << field << " does not exist in result set. "
162 "Available fields are [";
163 for(auto iter = _fields.cbegin() ; iter != _fields.cend() ; ++iter) {
164 if(iter != _fields.cbegin()) {
165 msg << ',';
166 }
167 msg << iter->first;
168 }
169 msg << ']';
170 throw SqlException(msg.str());
171 }
172 return get<T>(search->second, out);
173 }
174
188 template<typename T>
189 std::optional<T> get(std::size_t idx);
190
205 template<anch::sql::SystemTimePoint T>
206 std::optional<T> get(std::size_t idx);
207
219 template<typename T>
220 const std::optional<T> get(const std::string field) {
221 auto search = _fields.find(field);
222 if(search == _fields.end()) {
223 std::ostringstream msg;
224 msg << "Field " << field << " does not exist in result set. "
225 "Available fields are [";
226 for(auto iter = _fields.cbegin() ; iter != _fields.cend() ; ++iter) {
227 if(iter != _fields.cbegin()) {
228 msg << ',';
229 }
230 msg << iter->first;
231 }
232 msg << ']';
233 throw SqlException(msg.str());
234 }
235 return get<T>(search->second);
236 }
237
247 std::vector<std::string> columns() const;
248
258 std::optional<std::size_t> columnIndex(const std::string& name) const;
259
267 bool next();
268
269 protected:
277 virtual bool fetchNext() = 0;
278
284 void invalidateStatement() noexcept;
285
294 virtual bool getValue(std::size_t idx, std::string& out) = 0;
295
305 virtual std::optional<std::string> getValue(std::size_t idx) = 0;
306
312 virtual const anch::date::DateFormatter& getTimestampFormatter() = 0;
313
319 const anch::date::DateFormatter& getDefaultTimestampFormatter();
320 // Methods -
321
322 };
323
324 // Field conversion specializations +
336 template<>
337 bool
338 ResultSet::get<std::string>(std::size_t idx, std::string& out);
339
351 template<>
352 bool
353 ResultSet::get<int64_t>(std::size_t idx, int64_t& out);
354
366 template<>
367 bool
368 ResultSet::get<int32_t>(std::size_t idx, int32_t& out);
369
381 template<>
382 bool
383 ResultSet::get<int16_t>(std::size_t idx, int16_t& out);
384
396 template<>
397 bool
398 ResultSet::get<uint64_t>(std::size_t idx, uint64_t& out);
399
411 template<>
412 bool
413 ResultSet::get<uint32_t>(std::size_t idx, uint32_t& out);
414
426 template<>
427 bool
428 ResultSet::get<uint16_t>(std::size_t idx, uint16_t& out);
429
443 template<>
444 bool
445 ResultSet::get<double>(std::size_t idx, double& out);
446
460 template<>
461 bool
462 ResultSet::get<float>(std::size_t idx, float& out);
463
477 template<>
478 bool
479 ResultSet::get<bool>(std::size_t idx, bool& out);
480
492 template<>
493 bool
494 ResultSet::get<anch::date::Date>(std::size_t idx, anch::date::Date& out);
495
507 template<>
508 bool
509 ResultSet::get<anch::date::Time>(std::size_t idx, anch::date::Time& out);
510
522 template<>
523 bool
524 ResultSet::get<anch::date::DateTime>(std::size_t idx, anch::date::DateTime& out);
525
526#ifdef ANCH_UUID
540 template<>
541 bool
542 ResultSet::get<anch::UUID>(std::size_t idx, anch::UUID& out);
543#endif
544
554 template<>
555 std::optional<std::string>
556 ResultSet::get<std::string>(std::size_t idx);
557
567 template<>
568 std::optional<int64_t>
569 ResultSet::get<int64_t>(std::size_t idx);
570
580 template<>
581 std::optional<int32_t>
582 ResultSet::get<int32_t>(std::size_t idx);
583
593 template<>
594 std::optional<int16_t>
595 ResultSet::get<int16_t>(std::size_t idx);
596
606 template<>
607 std::optional<uint64_t>
608 ResultSet::get<uint64_t>(std::size_t idx);
609
619 template<>
620 std::optional<uint32_t>
621 ResultSet::get<uint32_t>(std::size_t idx);
622
632 template<>
633 std::optional<uint16_t>
634 ResultSet::get<uint16_t>(std::size_t idx);
635
647 template<>
648 std::optional<double>
649 ResultSet::get<double>(std::size_t idx);
650
662 template<>
663 std::optional<float>
664 ResultSet::get<float>(std::size_t idx);
665
677 template<>
678 std::optional<bool>
679 ResultSet::get<bool>(std::size_t idx);
680
690 template<>
691 std::optional<anch::date::Date>
692 ResultSet::get<anch::date::Date>(std::size_t idx);
693
703 template<>
704 std::optional<anch::date::Time>
705 ResultSet::get<anch::date::Time>(std::size_t idx);
706
716 template<>
717 std::optional<anch::date::DateTime>
719
720#ifdef ANCH_UUID
732 template<>
733 std::optional<anch::UUID>
734 ResultSet::get<anch::UUID>(std::size_t idx);
735#endif
736 // Field conversion specializations -
737
738} // anch::sql
739
740#include "sql/impl/resultSet.hpp"
UUID generator and parser.
Definition uuid.hpp:51
SQL connection virtual class.
Definition connection.hpp:109
SQL prepared statement.
Definition preparedStatement.hpp:54
SQL result representation.
Definition resultSet.hpp:73
virtual bool getValue(std::size_t idx, std::string &out)=0
std::optional< std::size_t > columnIndex(const std::string &name) const
std::vector< std::string > columns() const
const std::optional< T > get(const std::string field)
Definition resultSet.hpp:220
std::map< std::string, std::size_t > _fields
Definition resultSet.hpp:81
virtual const anch::date::DateFormatter & getTimestampFormatter()=0
virtual bool fetchNext()=0
bool get(std::size_t idx, T &out)
const anch::date::DateFormatter & getDefaultTimestampFormatter()
void invalidateStatement() noexcept
std::optional< T > get(std::size_t idx)
SQL exception.
Definition sqlException.hpp:39
SqlException(std::string msg, bool ok=true) noexcept
Definition resultSet.hpp:54
Dates namespace.
SQL namespace.
Definition clauses.hpp:28
AnCH framework base namespace.
Definition app.hpp:30