query/source/core/object/attribute_definition.cpp

196 lines
6.0 KiB
C++

#include "matador/object/attribute_definition.hpp"
#include <ostream>
#include <utility>
namespace matador::object {
attribute_definition::attribute_definition(const char *name)
: name_(name)
, attributes_(utils::null_attributes) {
}
attribute_definition::attribute_definition(std::string name)
: name_(std::move(name))
, attributes_(utils::null_attributes) {
}
attribute_definition::attribute_definition(std::string name,
std::string table_name,
const utils::basic_type type,
const utils::field_attributes& attr)
: name_(std::move(name))
, table_(std::move(table_name))
, index_(0)
, attributes_(attr)
, value_(type, attr.size()) {
}
attribute_definition::attribute_definition(std::string name, const utils::basic_type type,
const utils::field_attributes &attr, const null_option null_opt,
const size_t index)
: name_(std::move(name))
, index_(index)
, attributes_(attr)
, null_option_(null_opt)
, value_(type, attr.size()) {
}
attribute_definition::attribute_definition(std::string name,
std::string table_name,
const utils::basic_type type,
const utils::field_attributes &attr, const null_option null_opt,
const size_t index)
: name_(std::move(name))
, table_(std::move(table_name))
, index_(index)
, attributes_(attr)
, null_option_(null_opt)
, value_(type, attr.size()) {
}
attribute_definition::attribute_definition(std::string name,
const utils::basic_type type,
const size_t index,
const std::shared_ptr<attribute_definition> &ref_column,
const utils::field_attributes &attr, const null_option null_opt)
: name_(std::move(name))
, index_(index)
, attributes_(attr)
, null_option_(null_opt)
, value_(type, attr.size())
, 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 table_ + "." + name_;
}
std::string attribute_definition::table_name() const {
return table_;
}
int attribute_definition::index() const {
return index_;
}
const utils::field_attributes &attribute_definition::attributes() const {
return attributes_;
}
bool attribute_definition::is_nullable() const {
return null_option_ == null_option::NULLABLE;
}
utils::basic_type attribute_definition::type() const {
return value_.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 value_.is_integer();
}
bool attribute_definition::is_floating_point() const {
return value_.is_floating_point();
}
bool attribute_definition::is_bool() const {
return value_.is_bool();
}
bool attribute_definition::is_string() const {
return value_.is_string();
}
bool attribute_definition::is_varchar() const {
return value_.is_varchar();
}
bool attribute_definition::is_date() const {
return value_.is_date();
}
bool attribute_definition::is_time() const {
return value_.is_time();
}
bool attribute_definition::is_blob() const {
return value_.is_blob();
}
bool attribute_definition::is_null() const {
return value_.is_null();
}
void attribute_definition::type(utils::basic_type /*type*/) {
// type_ = type;
// utils::initialize_by_utils::basic_type(type, value_);
}
std::string attribute_definition::str() const {
return value_.str();
}
std::ostream &operator<<(std::ostream &out, const attribute_definition &col) {
out << col.str();
return out;
}
attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr,
null_option 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 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::FOREIGN_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::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, ref_table_name, utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY),
{ 0, utils::constraints::FOREIGN_KEY }, null_option::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::NOT_NULL
};
}
}