query/source/core/object/attribute.cpp

148 lines
4.4 KiB
C++

#include "matador/object/attribute.hpp"
#include "matador/object/object.hpp"
#include <ostream>
#include <utility>
namespace matador::object {
attribute::attribute(std::string name)
: attribute(std::move(name), utils::basic_type::type_null, {}, null_option_type::NOT_NULL) {
}
attribute::attribute(std::string name,
const utils::basic_type type,
const utils::field_attributes &attr,
const null_option_type null_opt)
: name_(std::move(name))
, options_{attr, null_opt}
, type_(type)
{}
const std::string &attribute::name() const {
return name_;
}
void attribute::name( const std::string& n ) {
name_ = n;
}
std::string attribute::full_name() const {
return owner_ ? owner_->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_;
}
object* attribute::owner() const {
return owner_;
}
// const std::string& attribute::table_name() const {
// return owner_ ? owner_->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;
}
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
// };
// }
}