Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
ResultSet.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <vector>
4#include <unordered_map>
5#include <optional>
6#include <memory>
7#include <stdexcept>
8#include <orbit/http/json.hpp>
9
10namespace database {
11
15class Row {
16public:
17 Row() = default;
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)) {}
20
26 std::optional<std::string> get(size_t index) const {
27 if (index < values_.size()) {
28 return values_[index];
29 }
30 return std::nullopt;
31 }
32
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);
43 }
44 return std::nullopt;
45 }
46
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; }
54 }
55
61 nlohmann::json j = nlohmann::json::object();
62 if (!col_map_) return j;
63
64 for (const auto& [name, index] : *col_map_) {
65 const std::string& val = values_[index];
66
67 // Automatic JSON serialization heuristics (Task 52)
68 if (val.empty()) {
69 j[name] = nullptr;
70 continue;
71 }
72
73 if (val == "true" || val == "t" || val == "TRUE") {
74 j[name] = true;
75 } else if (val == "false" || val == "f" || val == "FALSE") {
76 j[name] = false;
77 } else {
78 // Try to parse as integer or double, otherwise keep as string
79 try {
80 size_t pos = 0;
81 long long int_val = std::stoll(val, &pos);
82 if (pos == val.length()) { // Entire string was parsed as int
83 j[name] = int_val;
84 continue;
85 }
86 } catch (...) {}
87
88 try {
89 size_t pos = 0;
90 double dbl_val = std::stod(val, &pos);
91 if (pos == val.length()) { // Entire string was parsed as double
92 j[name] = dbl_val;
93 continue;
94 }
95 } catch (...) {}
96
97 j[name] = val;
98 }
99 }
100 return j;
101 }
102
103private:
104 std::vector<std::string> values_;
105 std::shared_ptr<std::unordered_map<std::string, size_t>> col_map_;
106};
107
112public:
113 ResultSet() = default;
114 ResultSet(std::vector<Row> rows, uint64_t affected_rows = 0)
115 : rows_(std::move(rows)), affected_rows_(affected_rows) {}
116
117 const std::vector<Row>& rows() const { return rows_; }
118 size_t size() const { return rows_.size(); }
119 bool empty() const { return rows_.empty(); }
120 uint64_t affected_rows() const { return affected_rows_; }
121
122 const Row& operator[](size_t index) const {
123 if (index >= rows_.size()) {
124 throw std::out_of_range("ResultSet index out of range");
125 }
126 return rows_[index];
127 }
128
133 nlohmann::json j = nlohmann::json::array();
134 for (const auto& row : rows_) {
135 j.push_back(row.to_json());
136 }
137 return j;
138 }
139
140private:
141 std::vector<Row> rows_;
142 uint64_t affected_rows_{0};
143};
144
145} // namespace database
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
Row()=default
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
Definition json.hpp:5363