168 lines
3.2 KiB
C++
168 lines
3.2 KiB
C++
#include "matador/sql/record.hpp"
|
|
#include "matador/sql/column.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
|
|
namespace matador::sql {
|
|
|
|
record::record(std::initializer_list<column_definition> columns)
|
|
: columns_(columns)
|
|
{
|
|
init();
|
|
}
|
|
|
|
record::record(const std::vector<column_definition> &columns)
|
|
: columns_(columns)
|
|
{
|
|
init();
|
|
}
|
|
|
|
record::record(const record &x)
|
|
: columns_(x.columns_)
|
|
, pk_index_(x.pk_index_)
|
|
{
|
|
for (auto& col : columns_) {
|
|
add_to_map(col, col.index());
|
|
}
|
|
}
|
|
|
|
record &record::operator=(const record &x)
|
|
{
|
|
if (&x == this) {
|
|
return *this;
|
|
}
|
|
|
|
columns_ = x.columns_;
|
|
columns_by_name_.clear();
|
|
pk_index_ = x.pk_index_;
|
|
for (auto& col : columns_) {
|
|
add_to_map(col, col.index());
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
const std::vector<column_definition> &record::columns() const
|
|
{
|
|
return columns_;
|
|
}
|
|
|
|
bool record::has_primary_key() const
|
|
{
|
|
return pk_index_ > -1;
|
|
}
|
|
|
|
const column_definition &record::primary_key() const
|
|
{
|
|
if (!has_primary_key()) {
|
|
throw std::logic_error("record has no primary key");
|
|
}
|
|
|
|
return columns_[pk_index_];
|
|
}
|
|
|
|
const column_definition &record::at(const column &col) const
|
|
{
|
|
auto ref = columns_by_name_.at(col.name).first;
|
|
return columns_by_name_.at(col.name).first;
|
|
}
|
|
|
|
//const column_definition &record::at(const std::string &name) const
|
|
//{
|
|
// auto ref = columns_by_name_.at(name).first;
|
|
// return columns_by_name_.at(name).first;
|
|
//}
|
|
|
|
const column_definition &record::at(size_t index) const
|
|
{
|
|
return columns_.at(index);
|
|
}
|
|
|
|
record::iterator record::find(const std::string &column_name)
|
|
{
|
|
auto it = columns_by_name_.find(column_name);
|
|
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
|
}
|
|
|
|
record::const_iterator record::find(const std::string &column_name) const {
|
|
auto it = columns_by_name_.find(column_name);
|
|
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
|
}
|
|
|
|
void record::append(column_definition col)
|
|
{
|
|
auto &ref = columns_.emplace_back(std::move(col));
|
|
add_to_map(ref, columns_.size()-1);
|
|
}
|
|
|
|
record::iterator record::begin()
|
|
{
|
|
return columns_.begin();
|
|
}
|
|
|
|
record::const_iterator record::begin() const
|
|
{
|
|
return columns_.begin();
|
|
}
|
|
|
|
record::const_iterator record::cbegin() const
|
|
{
|
|
return columns_.cbegin();
|
|
}
|
|
|
|
record::iterator record::end()
|
|
{
|
|
return columns_.end();
|
|
}
|
|
|
|
record::const_iterator record::end() const
|
|
{
|
|
return columns_.end();
|
|
}
|
|
|
|
record::const_iterator record::cend() const
|
|
{
|
|
return columns_.cend();
|
|
}
|
|
|
|
size_t record::size() const
|
|
{
|
|
return columns_.size();
|
|
}
|
|
|
|
bool record::empty() const
|
|
{
|
|
return columns_.empty();
|
|
}
|
|
|
|
void record::clear()
|
|
{
|
|
columns_.clear();
|
|
columns_by_name_.clear();
|
|
}
|
|
|
|
bool record::unknown() const
|
|
{
|
|
return std::all_of(std::begin(columns_), std::end(columns_), [](const auto &col) {
|
|
return col.type() == data_type_t::type_unknown;
|
|
});
|
|
}
|
|
|
|
void record::init()
|
|
{
|
|
size_t index{0};
|
|
for(auto &col : columns_) {
|
|
add_to_map(col, index++);
|
|
}
|
|
}
|
|
|
|
void record::add_to_map(column_definition &col, size_t index)
|
|
{
|
|
columns_by_name_.emplace(col.name(), column_index_pair {std::ref(col), index});
|
|
if (utils::is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
|
pk_index_ = static_cast<int>(index);
|
|
}
|
|
}
|
|
|
|
}
|