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