query/source/core/object/attribute.cpp

179 lines
5.7 KiB
C++

#include "matador/object/attribute.hpp"
#include <ostream>
#include <utility>
namespace matador::object {
attribute::attribute(const char *name)
: attribute(name, utils::basic_type::type_null, "", {utils::null_attributes}) {
}
attribute::attribute(std::string name)
: attribute(std::move(name), utils::basic_type::type_null, "", {utils::null_attributes}) {
}
attribute::attribute(std::string name,
const utils::basic_type type,
const utils::field_attributes &attr,
const null_option_type null_opt,
const int index)
: attribute(std::move(name), type, "", {attr, null_opt, index}) {
}
attribute::attribute(std::string name,
const utils::basic_type type,
const int index,
const std::shared_ptr<attribute> &ref_column,
const utils::field_attributes &attr,
const null_option_type null_opt)
: attribute(std::move(name), type, "", {attr, null_opt, index}, ref_column) {
}
attribute::attribute( std::string name, const utils::basic_type type, const std::shared_ptr<attribute>& ref_column )
: attribute(std::move(name), type, {}, {}, ref_column) {
}
attribute::attribute(std::string name,
const utils::basic_type type,
std::string table_name,
const attribute_options& options,
const std::shared_ptr<attribute>& ref_column)
: name_(std::move(name))
, table_name_(std::move(table_name))
, options_( options )
, type_( type )
, reference_column_( ref_column ) {
}
const std::string &attribute::name() const {
return name_;
}
void attribute::name( const std::string& n ) {
name_ = n;
}
std::string attribute::full_name() const {
return !table_name_.empty() ? table_name_ + "." + name_ : name_;
}
int attribute::index() const {
return options_.index;
}
const utils::field_attributes &attribute::attributes() const {
return options_.attributes;
}
utils::field_attributes& attribute::attributes() {
return options_.attributes;
}
bool attribute::is_nullable() const {
return options_.null_option == null_option_type::NULLABLE;
}
utils::basic_type attribute::type() const {
return type_;
}
const std::string& attribute::table_name() const {
return table_name_;
}
void attribute::table_name( const std::string& name ) {
table_name_ = name;
}
void attribute::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
options_.attributes = attr;
type_ = type;
}
std::shared_ptr<attribute> attribute::reference_column() const {
return reference_column_;
}
bool attribute::is_foreign_reference() const {
return reference_column_ != nullptr;
}
bool attribute::is_integer() const {
return type_ >= utils::basic_type::type_int8 && type_ <= utils::basic_type::type_uint64;
}
bool attribute::is_floating_point() const {
return type_ == utils::basic_type::type_float || type_ == utils::basic_type::type_double;
}
bool attribute::is_bool() const {
return type_ == utils::basic_type::type_bool;
}
bool attribute::is_string() const {
return type_ == utils::basic_type::type_text;
}
bool attribute::is_varchar() const {
return type_ == utils::basic_type::type_varchar;
}
bool attribute::is_date() const {
return type_ == utils::basic_type::type_date;
}
bool attribute::is_time() const {
return type_ == utils::basic_type::type_time;
}
bool attribute::is_blob() const {
return type_ == utils::basic_type::type_blob;
}
bool attribute::is_null() const {
return type_ == utils::basic_type::type_null;
}
attribute make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr,
null_option_type null_opt) {
return {name, type, attr, null_opt};
}
template<>
attribute make_column<std::string>(const std::string &name, utils::field_attributes attr,
null_option_type null_opt) {
return make_column(name, utils::data_type_traits<std::string>::type(attr.size()), attr, null_opt);
}
template<>
attribute make_pk_column<std::string>(const std::string &name, size_t size) {
return make_column<std::string>(name, {size, utils::constraints::PrimaryKey});
}
template<>
attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column) {
return {
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
{size, utils::constraints::ForeignKey}, null_option_type::NOT_NULL
};
}
template<>
attribute make_fk_column<std::string>( const std::string& name, const std::string& ref_table_name, const std::string& ref_column_name ) {
return {
name, utils::basic_type::type_varchar, 0,
std::make_shared<attribute>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::ForeignKey}),
{ 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
};
}
template<>
attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
const auto ref_column = std::make_shared<attribute>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::ForeignKey});
return {
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
{size, utils::constraints::ForeignKey}, null_option_type::NOT_NULL
};
}
}