StormByte C++ Library: Database module 0.0.9999
StormByte-Database is a StormByte library module for handling database connections
Loading...
Searching...
No Matches
row.hxx
1#pragma once
2
3#include <StormByte/database/exception.hxx>
4#include <StormByte/variadic_value.hxx>
5
6#include <algorithm>
7#include <memory>
8#include <vector>
9#include <span>
10
15namespace StormByte::Database {
20 template<class Value> class STORMBYTE_DATABASE_PUBLIC Row {
21 public:
22 using NamedValue = std::pair<std::string, std::shared_ptr<Value>>;
23 using Storage = std::vector<NamedValue>;
24 using iterator = typename Storage::iterator;
25 using const_iterator = typename Storage::const_iterator;
26
30 Row() noexcept = default;
31
35 Row(const Row&) = default;
36
40 Row(Row&&) noexcept = default;
41
45 Row& operator=(const Row&) = default;
46
50 Row& operator=(Row&&) noexcept = default;
51
55 virtual ~Row() noexcept = default;
56
63 Value& operator[](const std::string& columnName) {
64 return const_cast<Value&>(static_cast<const Row*>(this)->operator[](columnName));
65 }
66
73 const Value& operator[](const std::string& columnName) const {
74 auto it = std::find_if(m_values.begin(), m_values.end(), [&columnName](const NamedValue& item ) { return item.first == columnName; });
75 if (it == m_values.end())
76 throw ColumnNotFound(columnName);
77 return *(it->second);
78 }
79
86 Value& operator[](const size_t& columnIndex) {
87 return const_cast<Value&>(static_cast<const Row*>(this)->operator[](columnIndex));
88 }
89
96 const Value& operator[](const size_t& columnIndex) const {
97 if (columnIndex >= m_values.size())
98 throw OutOfBounds(columnIndex);
99 return *m_values[columnIndex].second;
100 }
101
105 constexpr operator bool() const noexcept {
106 return m_values.size() > 0;
107 }
108
113 iterator begin() noexcept {
114 return m_values.begin();
115 }
116
121 const_iterator begin() const noexcept {
122 return m_values.begin();
123 }
124
129 iterator end() noexcept {
130 return m_values.end();
131 }
132
137 const_iterator end() const noexcept {
138 return m_values.end();
139 }
140
145 std::span<NamedValue> Values() noexcept {
146 return m_values;
147 }
148
153 std::span<const NamedValue> Values() const noexcept {
154 return m_values;
155 }
156
162 void Add(const std::string& columnName, std::unique_ptr<Value>&& value) {
163 m_values.push_back({columnName, std::move(value)});
164 }
165
170 constexpr size_t Count() const noexcept {
171 return m_values.size();
172 }
173
174 protected:
178 Storage m_values; //< Internal storage
179 };
180}
Exception when accessing a not found column.
Definition exception.hxx:97
Exception when accessing an out of bounds column.
Definition exception.hxx:135
Row class for databases.
Definition row.hxx:20
constexpr size_t Count() const noexcept
Definition row.hxx:170
const_iterator begin() const noexcept
Definition row.hxx:121
typename Storage::iterator iterator
Shortcut alias for iterator.
Definition row.hxx:24
std::pair< std::string, std::shared_ptr< Value > > NamedValue
Shortcut alias for pair.
Definition row.hxx:22
iterator end() noexcept
Definition row.hxx:129
const Value & operator[](const size_t &columnIndex) const
Definition row.hxx:96
Storage m_values
Definition row.hxx:178
std::span< const NamedValue > Values() const noexcept
Definition row.hxx:153
std::vector< NamedValue > Storage
Shortcut alias for internal storage.
Definition row.hxx:23
const Value & operator[](const std::string &columnName) const
Definition row.hxx:73
iterator begin() noexcept
Definition row.hxx:113
void Add(const std::string &columnName, std::unique_ptr< Value > &&value)
Definition row.hxx:162
std::span< NamedValue > Values() noexcept
Definition row.hxx:145
typename Storage::const_iterator const_iterator
Shortcut alias for const iterator.
Definition row.hxx:25
const_iterator end() const noexcept
Definition row.hxx:137
Row() noexcept=default
Value & operator[](const size_t &columnIndex)
Definition row.hxx:86