fixed record class
This commit is contained in:
parent
ac8b2ed4e4
commit
ff3a2e4217
|
|
@ -1,3 +1,4 @@
|
||||||
.idea
|
.idea
|
||||||
cmake-build-debug
|
cmake-build-debug
|
||||||
Testing
|
Testing
|
||||||
|
debug
|
||||||
|
|
|
||||||
|
|
@ -107,8 +107,10 @@ int main()
|
||||||
|
|
||||||
auto update_authors_sql = c.query(s)
|
auto update_authors_sql = c.query(s)
|
||||||
.update(qh::authors)
|
.update(qh::authors)
|
||||||
.set({{qh::authors.first_name, "Stephen"},
|
.set({
|
||||||
{qh::authors.last_name, "King"}})
|
{qh::authors.first_name, "Stephen"},
|
||||||
|
{qh::authors.last_name, "King"}
|
||||||
|
})
|
||||||
.where(qh::authors.last_name == "Crichton")
|
.where(qh::authors.last_name == "Crichton")
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,9 @@ struct column;
|
||||||
class record
|
class record
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
using field_by_index = std::vector<field>;
|
using field_ref = std::reference_wrapper<field>;
|
||||||
using field_index_pair = std::pair<std::reference_wrapper<field>, field_by_index::difference_type>;
|
using field_by_index = std::vector<field_ref>;
|
||||||
|
using field_index_pair = std::pair<field, field_by_index::difference_type>;
|
||||||
using field_by_name_map = std::unordered_map<std::string, field_index_pair>;
|
using field_by_name_map = std::unordered_map<std::string, field_index_pair>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
@ -36,13 +37,13 @@ public:
|
||||||
void process(Operator &op)
|
void process(Operator &op)
|
||||||
{
|
{
|
||||||
for(auto &f : fields_) {
|
for(auto &f : fields_) {
|
||||||
f.process(op);
|
f.get().process(op);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void append(field col);
|
void append(const field &col);
|
||||||
|
|
||||||
[[nodiscard]] const std::vector<field>& columns() const;
|
[[nodiscard]] const std::vector<field_ref>& columns() const;
|
||||||
|
|
||||||
[[nodiscard]] const field& at(const column &col) const;
|
[[nodiscard]] const field& at(const column &col) const;
|
||||||
[[nodiscard]] const field& at(size_t index) const;
|
[[nodiscard]] const field& at(size_t index) const;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#include "matador/sql/noop_connection.hpp"
|
#include "matador/sql/noop_connection.hpp"
|
||||||
#include "matador/sql/dialect_builder.hpp"
|
#include "matador/sql/dialect_builder.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
|
||||||
|
|
@ -7,23 +7,27 @@
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
record::record(std::initializer_list<field> columns)
|
record::record(std::initializer_list<field> columns)
|
||||||
: fields_(columns)
|
|
||||||
{
|
{
|
||||||
init();
|
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)
|
record::record(const std::vector<field> &columns)
|
||||||
: fields_(columns)
|
|
||||||
{
|
{
|
||||||
init();
|
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 record &x)
|
record::record(const record &x)
|
||||||
: fields_(x.fields_)
|
: fields_by_name_(x.fields_by_name_)
|
||||||
//, pk_index_(x.pk_index_)
|
//, pk_index_(x.pk_index_)
|
||||||
{
|
{
|
||||||
for (auto& col : fields_) {
|
for (auto& col : fields_by_name_) {
|
||||||
add_to_map(col, col.index());
|
fields_.push_back(std::ref(col.second.first));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,16 +37,16 @@ record &record::operator=(const record &x)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
fields_ = x.fields_;
|
fields_by_name_ = x.fields_by_name_;
|
||||||
fields_by_name_.clear();
|
fields_.clear();
|
||||||
// pk_index_ = x.pk_index_;
|
// pk_index_ = x.pk_index_;
|
||||||
for (auto& col : fields_) {
|
for (auto& col : fields_by_name_) {
|
||||||
add_to_map(col, col.index());
|
fields_.push_back(std::ref(col.second.first));
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<field> &record::columns() const
|
const std::vector<record::field_ref> &record::columns() const
|
||||||
{
|
{
|
||||||
return fields_;
|
return fields_;
|
||||||
}
|
}
|
||||||
|
|
@ -63,7 +67,9 @@ const std::vector<field> &record::columns() const
|
||||||
|
|
||||||
const field &record::at(const column &col) const
|
const field &record::at(const column &col) const
|
||||||
{
|
{
|
||||||
return fields_by_name_.at(col.name).first;
|
const auto &res = fields_by_name_.at(col.name);
|
||||||
|
const auto &f = res.first;
|
||||||
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
const field &record::at(size_t index) const
|
const field &record::at(size_t index) const
|
||||||
|
|
@ -82,10 +88,10 @@ record::const_iterator record::find(const std::string &column_name) const {
|
||||||
return it != fields_by_name_.end() ? fields_.begin() + it->second.second : fields_.end();
|
return it != fields_by_name_.end() ? fields_.begin() + it->second.second : fields_.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void record::append(field col)
|
void record::append(const field &col)
|
||||||
{
|
{
|
||||||
auto &ref = fields_.emplace_back(std::move(col));
|
const auto it = fields_by_name_.emplace(col.name(), field_index_pair {col, fields_.size()});
|
||||||
add_to_map(ref, fields_.size() - 1);
|
fields_.push_back(std::ref(it.first->second.first));
|
||||||
}
|
}
|
||||||
|
|
||||||
record::iterator record::begin()
|
record::iterator record::begin()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue