query/source/orm/sql/field.cpp

131 lines
2.2 KiB
C++

#include "matador/sql/field.hpp"
#include "matador/utils/constraints.hpp"
#include <ostream>
namespace matador::sql {
field::field(std::string name)
: name_(std::move(name))
, value_(nullptr)
{}
field::field(std::string name, const utils::basic_type dt, const field_type type, const size_t size, const int index)
: name_(std::move(name))
, type_(type)
, index_(index)
, value_(dt, size) {}
field::field(field &&x) noexcept
: name_(std::move(x.name_))
, type_(x.type_)
, index_(x.index_)
, value_(std::move(x.value_)) {
x.value_ = nullptr;
x.index_ = -1;
}
field &field::operator=(field &&x) noexcept {
name_ = std::move(x.name_);
type_ = x.type_;
index_ = x.index_;
value_ = std::move(x.value_);
x.index_ = -1;
x.value_ = nullptr;
return *this;
}
const std::string &field::name() const
{
return name_;
}
field_type field::type() const {
return type_;
}
size_t field::size() const
{
return value_.size();
}
int field::index() const
{
return index_;
}
std::ostream &operator<<(std::ostream &out, const field &col)
{
out << col.str();
return out;
}
utils::constraints field::determine_constraint(const field_type type) {
switch (type) {
case field_type::PrimaryKey:
return utils::constraints::PRIMARY_KEY;
case field_type::ForeignKey:
return utils::constraints::FOREIGN_KEY;
case field_type::Attribute:
default:
return utils::constraints::NONE;
}
}
std::string field::str() const
{
return as<std::string>().value_or("");
}
bool field::is_integer() const
{
return value_.is_integer();
}
bool field::is_floating_point() const
{
return value_.is_floating_point();
}
bool field::is_bool() const
{
return value_.is_bool();
}
bool field::is_string() const
{
return value_.is_string();
}
bool field::is_varchar() const
{
return value_.is_varchar();
}
bool field::is_blob() const
{
return value_.is_blob();
}
bool field::is_null() const
{
return value_.is_null();
}
bool field::is_primary_key() const {
return type_ == field_type::PrimaryKey;
}
bool field::is_foreign_key() const {
return type_ == field_type::ForeignKey;
}
bool field::is_attribute() const {
return type_ == field_type::Attribute;
}
}