attribute_definition and object_definition progress
This commit is contained in:
@@ -1,66 +1,45 @@
|
||||
#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)
|
||||
: name_(name)
|
||||
, attributes_(utils::null_attributes) {
|
||||
: attribute_definition(name, utils::basic_type::type_null, {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)
|
||||
, type_(type){
|
||||
: 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 null_opt,
|
||||
const null_option_type null_opt,
|
||||
const int index)
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, type_(type) {
|
||||
}
|
||||
|
||||
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 int index)
|
||||
: name_(std::move(name))
|
||||
, table_(std::move(table_name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, type_(type) {
|
||||
: 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 null_opt)
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, type_(type)
|
||||
, reference_column_(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 {
|
||||
@@ -72,31 +51,31 @@ void attribute_definition::name( const std::string& n ) {
|
||||
}
|
||||
|
||||
std::string attribute_definition::full_name() const {
|
||||
return table_ + "." + name_;
|
||||
return object_ ? object_->name() + "." + name_ : name_;
|
||||
}
|
||||
|
||||
std::string attribute_definition::table_name() const {
|
||||
return table_;
|
||||
std::shared_ptr<object_definition> attribute_definition::object() const {
|
||||
return object_;
|
||||
}
|
||||
|
||||
void attribute_definition::table_name( const std::string& tn ) {
|
||||
table_= tn;
|
||||
void attribute_definition::object(const std::shared_ptr<object_definition>& obj) {
|
||||
object_ = obj;
|
||||
}
|
||||
|
||||
int attribute_definition::index() const {
|
||||
return index_;
|
||||
return options_.index;
|
||||
}
|
||||
|
||||
const utils::field_attributes &attribute_definition::attributes() const {
|
||||
return attributes_;
|
||||
return options_.attributes;
|
||||
}
|
||||
|
||||
utils::field_attributes& attribute_definition::attributes() {
|
||||
return attributes_;
|
||||
return options_.attributes;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_nullable() const {
|
||||
return null_option_ == null_option::NULLABLE;
|
||||
return options_.null_option == null_option_type::NULLABLE;
|
||||
}
|
||||
|
||||
utils::basic_type attribute_definition::type() const {
|
||||
@@ -104,7 +83,7 @@ utils::basic_type attribute_definition::type() const {
|
||||
}
|
||||
|
||||
void attribute_definition::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
|
||||
attributes_ = attr;
|
||||
options_.attributes = attr;
|
||||
type_ = type;
|
||||
}
|
||||
|
||||
@@ -153,13 +132,13 @@ bool attribute_definition::is_null() const {
|
||||
}
|
||||
|
||||
attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr,
|
||||
null_option null_opt) {
|
||||
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 null_opt) {
|
||||
null_option_type null_opt) {
|
||||
return make_column(name, utils::data_type_traits<std::string>::type(attr.size()), attr, null_opt);
|
||||
}
|
||||
|
||||
@@ -172,7 +151,7 @@ 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
|
||||
{size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
@@ -180,8 +159,8 @@ 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
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
@@ -190,7 +169,7 @@ attribute_definition make_fk_column<std::string>(const std::string &name, size_t
|
||||
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
|
||||
{size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,12 @@
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
attribute_definition_generator::attribute_definition_generator(std::vector<object::attribute_definition> &columns, const repository &repo)
|
||||
attribute_definition_generator::attribute_definition_generator(std::vector<attribute_definition> &columns, const repository &repo)
|
||||
: columns_(columns)
|
||||
, repo_(repo)
|
||||
{}
|
||||
|
||||
void attribute_definition_generator::on_revision(const char *id, uint64_t &rev)
|
||||
{
|
||||
void attribute_definition_generator::on_revision(const char *id, uint64_t &rev) {
|
||||
on_attribute(id, rev);
|
||||
}
|
||||
|
||||
@@ -17,7 +16,7 @@ utils::result<std::shared_ptr<attribute_definition>, utils::error> attribute_def
|
||||
return repo_.reference_column(ti);
|
||||
}
|
||||
|
||||
void attribute_definition_generator::insert_missing_reference_column(const std::type_index& ti, std::shared_ptr<attribute_definition> ref_column) const {
|
||||
void attribute_definition_generator::insert_missing_reference_column(const std::type_index& ti, const std::shared_ptr<attribute_definition>& ref_column) const {
|
||||
const_cast<repository&>(repo_).missing_references_.insert({ti, ref_column});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,24 @@
|
||||
#include "matador/object/object_definition.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
object_definition::object_definition(const std::initializer_list<attribute_definition> columns)
|
||||
: columns_(columns) {
|
||||
object_definition::object_definition(std::string name)
|
||||
: name_(std::move(name)) {}
|
||||
|
||||
object_definition::object_definition(std::string name, const std::initializer_list<attribute_definition> columns)
|
||||
: name_(std::move(name))
|
||||
, columns_(columns) {
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(const std::vector<attribute_definition> &columns)
|
||||
: columns_(columns) {
|
||||
object_definition::object_definition(std::string name, const std::vector<attribute_definition> &columns)
|
||||
: name_(std::move(name))
|
||||
, columns_(columns) {
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(const object_definition &x)
|
||||
: columns_(x.columns_)
|
||||
: name_(x.name_)
|
||||
, columns_(x.columns_)
|
||||
, pk_index_(x.pk_index_) {
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
@@ -25,6 +31,7 @@ object_definition &object_definition::operator=(const object_definition &x)
|
||||
return *this;
|
||||
}
|
||||
|
||||
name_ = x.name_;
|
||||
columns_ = x.columns_;
|
||||
columns_by_name_.clear();
|
||||
pk_index_ = x.pk_index_;
|
||||
@@ -34,8 +41,11 @@ object_definition &object_definition::operator=(const object_definition &x)
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool object_definition::has_primary_key() const
|
||||
{
|
||||
const std::string& object_definition::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
bool object_definition::has_primary_key() const {
|
||||
return pk_index_ > -1;
|
||||
}
|
||||
|
||||
@@ -48,14 +58,12 @@ std::optional<attribute_definition> object_definition::primary_key() const
|
||||
return columns_[pk_index_];
|
||||
}
|
||||
|
||||
void object_definition::append(attribute_definition col)
|
||||
{
|
||||
void object_definition::append(attribute_definition&& col) {
|
||||
auto &ref = columns_.emplace_back(std::move(col));
|
||||
add_to_map(ref, columns_.size()-1);
|
||||
}
|
||||
|
||||
const std::vector<object::attribute_definition> &object_definition::columns() const
|
||||
{
|
||||
const std::vector<object::attribute_definition> &object_definition::columns() const {
|
||||
return columns_;
|
||||
}
|
||||
|
||||
@@ -71,12 +79,12 @@ const attribute_definition &object_definition::at( const size_t index) const
|
||||
|
||||
object_definition::iterator object_definition::find(const std::string &column_name)
|
||||
{
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
const auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::find(const std::string &column_name) const {
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
const auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
|
||||
@@ -45,10 +45,12 @@ const basic_object_info &repository_node::info() const {
|
||||
|
||||
void repository_node::update_name(const std::string& name) {
|
||||
name_ = name;
|
||||
info_->reference_column()->table_name(name);
|
||||
if (info_->reference_column() && info_->reference_column()->object() != nullptr) {
|
||||
info_->reference_column()->object()->name_ = name;
|
||||
}
|
||||
}
|
||||
|
||||
const object::repository& repository_node::schema() const {
|
||||
const repository& repository_node::schema() const {
|
||||
return repo_;
|
||||
}
|
||||
|
||||
@@ -96,27 +98,27 @@ void repository_node::unlink() {
|
||||
previous_sibling_.reset();
|
||||
}
|
||||
|
||||
utils::result<repository_node::node_ptr, utils::error> repository_node::make_and_attach_node(object::repository& tree, const std::string& name, const std::type_index& ti) {
|
||||
const auto node = std::shared_ptr<repository_node>(new repository_node(tree, name, ti));
|
||||
utils::result<repository_node::node_ptr, utils::error> repository_node::make_and_attach_node(repository& repo, const std::string& name, const std::type_index& ti) {
|
||||
const auto node = std::shared_ptr<repository_node>(new repository_node(repo, name, ti));
|
||||
|
||||
return tree.attach_node(node, "");
|
||||
return repo.attach_node(node, "");
|
||||
}
|
||||
|
||||
std::shared_ptr<attribute_definition> repository_node::determine_reference_column(const std::type_index& ti, const std::string& name, const primary_key_info& pk_info, object::repository& tree) {
|
||||
const auto it = tree.missing_references_.find(ti);
|
||||
if (it == tree.missing_references_.end()) {
|
||||
return std::make_shared<attribute_definition>(pk_info.pk_column_name, name, pk_info.type, utils::constraints::FOREIGN_KEY);
|
||||
std::shared_ptr<attribute_definition> repository_node::determine_reference_column(const std::type_index& ti, object_definition& obj, const primary_key_info& pk_info, repository& repo) {
|
||||
const auto it = repo.missing_references_.find(ti);
|
||||
if (it == repo.missing_references_.end()) {
|
||||
return std::make_shared<attribute_definition>(pk_info.pk_column_name, pk_info.type, attribute_options{utils::constraints::FOREIGN_KEY});
|
||||
}
|
||||
|
||||
auto ref_column = it->second;
|
||||
tree.missing_references_.erase(it);
|
||||
repo.missing_references_.erase(it);
|
||||
ref_column->name(pk_info.pk_column_name);
|
||||
ref_column->table_name(name);
|
||||
ref_column->object(obj);
|
||||
ref_column->change_type(pk_info.type);
|
||||
ref_column->attributes() = utils::constraints::FOREIGN_KEY;
|
||||
|
||||
if (name.empty()) {
|
||||
tree.missing_references_.insert({ti, ref_column});
|
||||
if (obj.name().empty()) {
|
||||
repo.missing_references_.insert({ti, ref_column});
|
||||
}
|
||||
|
||||
return ref_column;
|
||||
|
||||
Reference in New Issue
Block a user