Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
ResultSet.hpp
Go to the documentation of this file.
1#pragma once
3#include <string>
4#include <vector>
5#include <unordered_map>
6#include <optional>
7#include <memory>
8#include <stdexcept>
9#include <charconv>
10#include <limits>
11#include <locale>
12#include <sstream>
13#include <type_traits>
14#include <orbit/http/json.hpp>
15
16namespace orbit::database {
17
18namespace detail {
19
25template <typename T>
26std::optional<T> convert(const std::string& text) {
27 if constexpr (std::is_same_v<T, std::string>) {
28 return text;
29 } else if constexpr (std::is_same_v<T, bool>) {
30 if (text == "t" || text == "true" || text == "1" || text == "TRUE") return true;
31 if (text == "f" || text == "false" || text == "0" || text == "FALSE") return false;
32 return std::nullopt;
33 } else if constexpr (std::is_integral_v<T>) {
34 T value{};
35 auto [end, ec] = std::from_chars(text.data(), text.data() + text.size(), value);
36 if (ec != std::errc() || end != text.data() + text.size()) return std::nullopt;
37 return value;
38 } else if constexpr (std::is_floating_point_v<T>) {
39 // A stream with the classic locale: strtod depends on the process locale.
40 std::istringstream in(text);
41 in.imbue(std::locale::classic());
42 T value{};
43 in >> value;
44 if (in.fail() || in.peek() != std::char_traits<char>::eof()) {
45 if (text == "NaN") return std::numeric_limits<T>::quiet_NaN();
46 if (text == "Infinity") return std::numeric_limits<T>::infinity();
47 if (text == "-Infinity") return -std::numeric_limits<T>::infinity();
48 return std::nullopt;
49 }
50 return value;
51 } else {
52 static_assert(sizeof(T) == 0, "get_as<T> supports bool, integers, floating point and std::string");
53 }
54}
55
56} // namespace detail
57
61class Row {
62public:
63 Row() = default;
64 Row(std::vector<std::string> values, std::shared_ptr<std::unordered_map<std::string, size_t>> col_map)
65 : values_(values.begin(), values.end()), col_map_(std::move(col_map)) {}
66
68 Row(std::vector<std::optional<std::string>> values, std::shared_ptr<std::unordered_map<std::string, size_t>> col_map)
69 : values_(std::move(values)), col_map_(std::move(col_map)) {}
70
76 std::optional<std::string> get(size_t index) const {
77 if (index < values_.size()) {
78 return values_[index];
79 }
80 return std::nullopt;
81 }
82
88 std::optional<std::string> get(const std::string& col_name) const {
89 if (!col_map_) return std::nullopt;
90 auto it = col_map_->find(col_name);
91 if (it != col_map_->end()) {
92 return get(it->second);
93 }
94 return std::nullopt;
95 }
96
103 template <typename T>
104 std::optional<T> get_as(size_t index) const {
105 auto text = get(index);
106 if (!text) return std::nullopt;
107 return detail::convert<T>(*text);
108 }
109
110 template <typename T>
111 std::optional<T> get_as(const std::string& col_name) const {
112 auto text = get(col_name);
113 if (!text) return std::nullopt;
114 return detail::convert<T>(*text);
115 }
116
118 template <typename T>
119 T value_or(const std::string& col_name, T fallback) const {
120 return get_as<T>(col_name).value_or(std::move(fallback));
121 }
122
124 bool is_null(const std::string& col_name) const {
125 if (!col_map_) return false;
126 auto it = col_map_->find(col_name);
127 return it != col_map_->end() && it->second < values_.size() && !values_[it->second];
128 }
129
133 int as_int(const std::string& col_name, int default_val = 0) const {
134 auto val = get(col_name);
135 if (!val || val->empty()) return default_val;
136 try { return std::stoi(*val); } catch (...) { return default_val; }
137 }
138
144 nlohmann::json j = nlohmann::json::object();
145 if (!col_map_) return j;
146
147 for (const auto& [name, index] : *col_map_) {
148 if (!values_[index]) {
149 j[name] = nullptr; // SQL NULL
150 continue;
151 }
152 const std::string& val = *values_[index];
153 // Automatic JSON serialization heuristics (Task 52)
154 if (val.empty()) {
155 j[name] = "";
156 continue;
157 }
158
159 if (val == "true" || val == "t" || val == "TRUE") {
160 j[name] = true;
161 } else if (val == "false" || val == "f" || val == "FALSE") {
162 j[name] = false;
163 } else {
164 // Try to parse as integer or double, otherwise keep as string
165 try {
166 size_t pos = 0;
167 long long int_val = std::stoll(val, &pos);
168 if (pos == val.length()) { // Entire string was parsed as int
169 j[name] = int_val;
170 continue;
171 }
172 } catch (...) {}
173
174 try {
175 size_t pos = 0;
176 double dbl_val = std::stod(val, &pos);
177 if (pos == val.length()) { // Entire string was parsed as double
178 j[name] = dbl_val;
179 continue;
180 }
181 } catch (...) {}
182
183 j[name] = val;
184 }
185 }
186 return j;
187 }
188
189private:
190 std::vector<std::optional<std::string>> values_;
191 std::shared_ptr<std::unordered_map<std::string, size_t>> col_map_;
192};
193
198public:
199 ResultSet() = default;
200 ResultSet(std::vector<Row> rows, uint64_t affected_rows = 0)
202
204 static ResultSet failure(std::string message) {
205 ResultSet r;
206 r.error_ = std::move(message);
207 if (r.error_.empty()) r.error_ = "unknown database error";
208 return r;
209 }
210
212 bool ok() const { return error_.empty(); }
213 const std::string& error() const { return error_; }
214
215 const std::vector<Row>& rows() const { return rows_; }
216 size_t size() const { return rows_.size(); }
217 bool empty() const { return rows_.empty(); }
218 uint64_t affected_rows() const { return affected_rows_; }
219
220 const Row& operator[](size_t index) const {
221 if (index >= rows_.size()) {
222 throw std::out_of_range("ResultSet index out of range");
223 }
224 return rows_[index];
225 }
226
231 nlohmann::json j = nlohmann::json::array();
232 for (const auto& row : rows_) {
233 j.push_back(row.to_json());
234 }
235 return j;
236 }
237
238private:
239 std::vector<Row> rows_;
240 uint64_t affected_rows_{0};
241 std::string error_;
242};
243
244} // namespace database
a class to store JSON values
Definition json.hpp:19401
void push_back(basic_json &&val)
add an object to an array
Definition json.hpp:22390
Represents a generic, unified result set from a database query.
Definition ResultSet.hpp:197
nlohmann::json to_json() const
Serializes the entire result set to a JSON array of objects.
Definition ResultSet.hpp:230
uint64_t affected_rows_
Definition ResultSet.hpp:240
const std::string & error() const
Definition ResultSet.hpp:213
std::vector< Row > rows_
Definition ResultSet.hpp:239
const std::vector< Row > & rows() const
Definition ResultSet.hpp:215
uint64_t affected_rows() const
Definition ResultSet.hpp:218
std::string error_
Definition ResultSet.hpp:241
bool ok() const
False if the query failed; error() then says why.
Definition ResultSet.hpp:212
size_t size() const
Definition ResultSet.hpp:216
static ResultSet failure(std::string message)
A result describing a failed query.
Definition ResultSet.hpp:204
const Row & operator[](size_t index) const
Definition ResultSet.hpp:220
bool empty() const
Definition ResultSet.hpp:217
ResultSet(std::vector< Row > rows, uint64_t affected_rows=0)
Definition ResultSet.hpp:200
Represents a single row of a database result set.
Definition ResultSet.hpp:61
int as_int(const std::string &col_name, int default_val=0) const
Helper to get a column value as an integer.
Definition ResultSet.hpp:133
nlohmann::json to_json() const
Serializes the row to a JSON object automatically. Tries to parse numbers and booleans from strings i...
Definition ResultSet.hpp:143
std::shared_ptr< std::unordered_map< std::string, size_t > > col_map_
Definition ResultSet.hpp:191
std::optional< std::string > get(const std::string &col_name) const
Gets a column value by column name.
Definition ResultSet.hpp:88
std::vector< std::optional< std::string > > values_
Definition ResultSet.hpp:190
Row(std::vector< std::optional< std::string > > values, std::shared_ptr< std::unordered_map< std::string, size_t > > col_map)
Values may be std::nullopt for SQL NULL.
Definition ResultSet.hpp:68
std::optional< T > get_as(size_t index) const
Gets a column converted to T (bool, an integer type, a floating point type or std::string).
Definition ResultSet.hpp:104
T value_or(const std::string &col_name, T fallback) const
get_as<T>(), or fallback when that is std::nullopt.
Definition ResultSet.hpp:119
std::optional< T > get_as(const std::string &col_name) const
Definition ResultSet.hpp:111
bool is_null(const std::string &col_name) const
True if the column exists and is SQL NULL.
Definition ResultSet.hpp:124
Row(std::vector< std::string > values, std::shared_ptr< std::unordered_map< std::string, size_t > > col_map)
Definition ResultSet.hpp:64
std::optional< std::string > get(size_t index) const
Gets a column value by index.
Definition ResultSet.hpp:76
std::optional< T > convert(const std::string &text)
Converts a column's text to T: bool, an integer, a floating-point type or std::string....
Definition ResultSet.hpp:26
Definition ConnectionPool.hpp:14
Definition json.hpp:5363