4#include <unordered_map>
18 Row(std::vector<std::string> values, std::shared_ptr<std::unordered_map<std::string, size_t>> col_map)
19 : values_(
std::move(values)), col_map_(
std::move(col_map)) {}
26 std::optional<std::string>
get(
size_t index)
const {
27 if (index < values_.size()) {
28 return values_[index];
38 std::optional<std::string>
get(
const std::string& col_name)
const {
39 if (!col_map_)
return std::nullopt;
40 auto it = col_map_->find(col_name);
41 if (it != col_map_->end()) {
42 return get(it->second);
50 int as_int(
const std::string& col_name,
int default_val = 0)
const {
51 auto val =
get(col_name);
52 if (!val || val->empty())
return default_val;
53 try {
return std::stoi(*val); }
catch (...) {
return default_val; }
62 if (!col_map_)
return j;
64 for (
const auto& [name, index] : *col_map_) {
65 const std::string& val = values_[index];
73 if (val ==
"true" || val ==
"t" || val ==
"TRUE") {
75 }
else if (val ==
"false" || val ==
"f" || val ==
"FALSE") {
81 long long int_val = std::stoll(val, &pos);
82 if (pos == val.length()) {
90 double dbl_val = std::stod(val, &pos);
91 if (pos == val.length()) {
104 std::vector<std::string> values_;
105 std::shared_ptr<std::unordered_map<std::string, size_t>> col_map_;
117 const std::vector<Row>&
rows()
const {
return rows_; }
118 size_t size()
const {
return rows_.size(); }
119 bool empty()
const {
return rows_.empty(); }
123 if (index >= rows_.size()) {
124 throw std::out_of_range(
"ResultSet index out of range");
134 for (
const auto& row : rows_) {
141 std::vector<Row> rows_;
142 uint64_t affected_rows_{0};
Represents a generic, unified result set from a database query.
Definition ResultSet.hpp:111
nlohmann::json to_json() const
Serializes the entire result set to a JSON array of objects.
Definition ResultSet.hpp:132
const Row & operator[](size_t index) const
Definition ResultSet.hpp:122
const std::vector< Row > & rows() const
Definition ResultSet.hpp:117
ResultSet(std::vector< Row > rows, uint64_t affected_rows=0)
Definition ResultSet.hpp:114
bool empty() const
Definition ResultSet.hpp:119
size_t size() const
Definition ResultSet.hpp:118
uint64_t affected_rows() const
Definition ResultSet.hpp:120
Represents a single row of a database result set.
Definition ResultSet.hpp:15
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:60
std::optional< std::string > get(const std::string &col_name) const
Gets a column value by column name.
Definition ResultSet.hpp:38
std::optional< std::string > get(size_t index) const
Gets a column value by index.
Definition ResultSet.hpp:26
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:50
Row(std::vector< std::string > values, std::shared_ptr< std::unordered_map< std::string, size_t > > col_map)
Definition ResultSet.hpp:18
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
Definition ConnectionPool.hpp:10