refactored record classe

This commit is contained in:
2026-02-15 18:33:22 +01:00
parent 746fd9e8d2
commit 9065355ee0
3 changed files with 52 additions and 98 deletions
+41 -74
View File
@@ -7,76 +7,66 @@ namespace matador::sql {
record::record(const std::initializer_list<field> columns) {
for (auto &&col :columns) {
const auto it = fields_by_name_.emplace(col.name(), field_index_pair {col, fields_.size()});
fields_.push_back(std::ref(it.first->second.first));
index_by_name_.emplace(col.name(), fields_.size());
fields_.push_back(col);
}
}
record::record(const std::vector<field> &columns) {
for (auto &&col :columns) {
const auto it = fields_by_name_.emplace(col.name(), field_index_pair {col, fields_.size()});
fields_.push_back(std::ref(it.first->second.first));
record::record(const std::vector<field> &columns)
: fields_(columns) {
for (auto i = 0u; i < columns.size(); ++i) {
index_by_name_.emplace(columns[i].name(), i);
}
}
record::record(const record &x)
: fields_by_name_(x.fields_by_name_)
{
for (auto& col : x.fields_) {
auto &it = fields_by_name_.at(col.get().name());
fields_.push_back(std::ref(it.first));
}
}
record &record::operator=(const record &x)
{
record &record::operator=(const record &x) {
if (&x == this) {
return *this;
}
fields_by_name_ = x.fields_by_name_;
fields_.clear();
// pk_index_ = x.pk_index_;
for (auto& col : x.fields_) {
auto &it = fields_by_name_.at(col.get().name());
fields_.push_back(std::ref(it.first));
}
fields_ = x.fields_;
index_by_name_ = x.index_by_name_;
return *this;
}
const std::vector<record::field_ref> &record::columns() const {
const std::vector<field> &record::columns() const {
return fields_;
}
const field &record::at(const std::string &name) const {
const auto &res = fields_by_name_.at(name);
const auto &f = res.first;
return f;
const auto it = index_by_name_.find(name);
if (it == index_by_name_.end()) {
throw std::out_of_range("Column not found");
}
return fields_[it->second];
}
const field &record::at(const size_t index) const
{
const field &record::at(const size_t index) const {
return fields_.at(index);
}
field &record::at(const std::string &name) {
auto &[fst, snd] = fields_by_name_.at(name);
return fst;
const auto it = index_by_name_.find(name);
if (it == index_by_name_.end()) {
throw std::out_of_range("Column not found");
}
return fields_[it->second];
}
field &record::at(const size_t index) {
return fields_.at(index);
}
record::iterator record::find(const std::string &column_name)
{
auto it = fields_by_name_.find(column_name);
return it != fields_by_name_.end() ? fields_.begin() + it->second.second : fields_.end();
record::iterator record::find(const std::string &column_name) {
const auto it = index_by_name_.find(column_name);
return it != index_by_name_.end() ? fields_.begin() + static_cast<long>(it->second) : fields_.end();
}
record::const_iterator record::find(const std::string &column_name) const {
auto it = fields_by_name_.find(column_name);
return it != fields_by_name_.end() ? fields_.begin() + it->second.second : fields_.end();
const auto it = index_by_name_.find(column_name);
return it != index_by_name_.end() ? fields_.begin() + static_cast<long>(it->second) : fields_.end();
}
bool record::operator==(const record &rhs) const {
@@ -95,68 +85,45 @@ bool record::operator!=(const record &rhs) const {
return !operator==(rhs);
}
void record::append(const field &col) {
const auto [fst, snd] = fields_by_name_.emplace(col.name(), field_index_pair {col, fields_.size()});
fields_.push_back(std::ref(fst->second.first));
void record::append(field col) {
index_by_name_.emplace(col.name(), fields_.size());
fields_.push_back(std::move(col));
}
record::iterator record::begin()
{
record::iterator record::begin() {
return fields_.begin();
}
record::const_iterator record::begin() const
{
record::const_iterator record::begin() const {
return fields_.begin();
}
record::const_iterator record::cbegin() const
{
record::const_iterator record::cbegin() const {
return fields_.cbegin();
}
record::iterator record::end()
{
record::iterator record::end() {
return fields_.end();
}
record::const_iterator record::end() const
{
record::const_iterator record::end() const {
return fields_.end();
}
record::const_iterator record::cend() const
{
record::const_iterator record::cend() const {
return fields_.cend();
}
size_t record::size() const
{
size_t record::size() const {
return fields_.size();
}
bool record::empty() const
{
bool record::empty() const {
return fields_.empty();
}
void record::clear()
{
void record::clear() {
fields_.clear();
fields_by_name_.clear();
index_by_name_.clear();
}
void record::init()
{
size_t index{0};
for(auto &col : fields_) {
add_to_map(col, index++);
}
}
void record::add_to_map(field &col, size_t index)
{
fields_by_name_.emplace(col.name(), field_index_pair {std::ref(col), index});
}
}