5#include <unordered_map>
26std::optional<T>
convert(
const std::string& text) {
27 if constexpr (std::is_same_v<T, std::string>) {
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;
33 }
else if constexpr (std::is_integral_v<T>) {
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;
38 }
else if constexpr (std::is_floating_point_v<T>) {
40 std::istringstream in(text);
41 in.imbue(std::locale::classic());
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();
52 static_assert(
sizeof(T) == 0,
"get_as<T> supports bool, integers, floating point and std::string");
64 Row(std::vector<std::string> values, std::shared_ptr<std::unordered_map<std::string, size_t>> col_map)
68 Row(std::vector<std::optional<std::string>> values, std::shared_ptr<std::unordered_map<std::string, size_t>> col_map)
76 std::optional<std::string>
get(
size_t index)
const {
88 std::optional<std::string>
get(
const std::string& col_name)
const {
92 return get(it->second);
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);
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);
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));
124 bool is_null(
const std::string& col_name)
const {
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; }
147 for (
const auto& [name, index] : *
col_map_) {
152 const std::string& val = *
values_[index];
159 if (val ==
"true" || val ==
"t" || val ==
"TRUE") {
161 }
else if (val ==
"false" || val ==
"f" || val ==
"FALSE") {
167 long long int_val = std::stoll(val, &pos);
168 if (pos == val.length()) {
176 double dbl_val = std::stod(val, &pos);
177 if (pos == val.length()) {
190 std::vector<std::optional<std::string>>
values_;
191 std::shared_ptr<std::unordered_map<std::string, size_t>>
col_map_;
206 r.
error_ = std::move(message);
207 if (r.
error_.empty()) r.
error_ =
"unknown database error";
221 if (index >=
rows_.size()) {
222 throw std::out_of_range(
"ResultSet index out of range");
232 for (
const auto& row :
rows_) {
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