Compare commits
No commits in common. "f47b3bb87f0a7a5f4eee92f6f490b948a2731e6d" and "ede5a8c6364051fd866b699d3328870218d3564e" have entirely different histories.
f47b3bb87f
...
ede5a8c636
|
|
@ -23,6 +23,7 @@ class attribute_generator;
|
|||
|
||||
class attribute {
|
||||
public:
|
||||
explicit attribute(const char *name); // NOLINT(*-explicit-constructor)
|
||||
explicit attribute(std::string name); // NOLINT(*-explicit-constructor)
|
||||
|
||||
attribute(const attribute&) = default;
|
||||
|
|
@ -34,8 +35,24 @@ public:
|
|||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
const utils::field_attributes&,
|
||||
null_option_type null_opt,
|
||||
int index = 0);
|
||||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
int index,
|
||||
const std::shared_ptr<attribute> &ref_column,
|
||||
const utils::field_attributes& attr,
|
||||
null_option_type null_opt);
|
||||
|
||||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
const std::shared_ptr<attribute> &ref_column = {});
|
||||
attribute(std::string name,
|
||||
utils::basic_type type,
|
||||
std::string table_name,
|
||||
const attribute_options& options,
|
||||
const std::shared_ptr<attribute> &ref_column = {});
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
void name(const std::string& n);
|
||||
[[nodiscard]] std::string full_name() const;
|
||||
|
|
@ -44,14 +61,15 @@ public:
|
|||
[[nodiscard]] utils::field_attributes& attributes();
|
||||
[[nodiscard]] bool is_nullable() const;
|
||||
[[nodiscard]] utils::basic_type type() const;
|
||||
[[nodiscard]] object* owner() const;
|
||||
// [[nodiscard]] const std::string& table_name() const;
|
||||
// void table_name(const std::string& name);
|
||||
[[nodiscard]] const std::string& table_name() const;
|
||||
void table_name(const std::string& name);
|
||||
void change_type(utils::basic_type type, const utils::field_attributes &attr = utils::null_attributes);
|
||||
template < typename Type >
|
||||
void change_type(const utils::field_attributes &attr = utils::null_attributes) {
|
||||
type_ = utils::data_type_traits<Type>::type(attr.size());
|
||||
}
|
||||
[[nodiscard]] std::shared_ptr<attribute> reference_column() const;
|
||||
[[nodiscard]] bool is_foreign_reference() const;
|
||||
|
||||
[[nodiscard]] bool is_integer() const;
|
||||
[[nodiscard]] bool is_floating_point() const;
|
||||
|
|
@ -74,8 +92,11 @@ private:
|
|||
|
||||
std::string name_;
|
||||
object *owner_{nullptr};
|
||||
std::string table_name_;
|
||||
attribute_options options_;
|
||||
utils::basic_type type_{utils::basic_type::type_null};
|
||||
|
||||
std::shared_ptr<attribute> reference_column_;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -87,62 +108,62 @@ private:
|
|||
* @param null_opt
|
||||
* @return A column object with a given name
|
||||
*/
|
||||
// attribute make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL);
|
||||
//
|
||||
// template < typename Type >
|
||||
// attribute make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL)
|
||||
// {
|
||||
// return make_column(name, utils::data_type_traits<Type>::type(0), attr, null_opt);
|
||||
// }
|
||||
// template <>
|
||||
// attribute make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option_type null_opt);
|
||||
//
|
||||
// template < typename Type >
|
||||
// attribute make_pk_column(const std::string &name, size_t size = 0)
|
||||
// {
|
||||
// return make_column<Type>(name, { size, utils::constraints::PrimaryKey });
|
||||
// }
|
||||
//
|
||||
// template <>
|
||||
// attribute make_pk_column<std::string>(const std::string &name, size_t size);
|
||||
//
|
||||
// template < typename Type >
|
||||
// attribute make_fk_column(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column) {
|
||||
// return {name, utils::data_type_traits<Type>::type(size), ref_column, { size, utils::constraints::ForeignKey }};
|
||||
// }
|
||||
//
|
||||
// template < typename Type >
|
||||
// [[maybe_unused]] attribute make_fk_column(const std::string &name, const std::shared_ptr<attribute> &ref_column)
|
||||
// {
|
||||
// return {name, utils::data_type_traits<Type>::type(0), 0, ref_column, { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL};
|
||||
// }
|
||||
//
|
||||
// template <>
|
||||
// [[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column);
|
||||
//
|
||||
// template < typename Type >
|
||||
// [[maybe_unused]] attribute make_fk_column(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
// return {
|
||||
// name, utils::data_type_traits<Type>::type(size), 0,
|
||||
// std::make_shared<attribute>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(size), utils::constraints::ForeignKey),
|
||||
// { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// template < typename Type >
|
||||
// [[maybe_unused]] attribute make_fk_column(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
// return {
|
||||
// name, utils::data_type_traits<Type>::type(0), 0,
|
||||
// std::make_shared<attribute>(ref_column_name, utils::data_type_traits<Type>::type(0), ref_table_name, attribute_options{utils::constraints::ForeignKey}),
|
||||
// { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// template <>
|
||||
// [[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name);
|
||||
//
|
||||
// template <>
|
||||
// [[maybe_unused]] 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);
|
||||
attribute make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL);
|
||||
|
||||
template < typename Type >
|
||||
attribute make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL)
|
||||
{
|
||||
return make_column(name, utils::data_type_traits<Type>::type(0), attr, null_opt);
|
||||
}
|
||||
template <>
|
||||
attribute make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option_type null_opt);
|
||||
|
||||
template < typename Type >
|
||||
attribute make_pk_column(const std::string &name, size_t size = 0)
|
||||
{
|
||||
return make_column<Type>(name, { size, utils::constraints::PrimaryKey });
|
||||
}
|
||||
|
||||
template <>
|
||||
attribute make_pk_column<std::string>(const std::string &name, size_t size);
|
||||
|
||||
template < typename Type >
|
||||
attribute make_fk_column(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column) {
|
||||
return {name, utils::data_type_traits<Type>::type(size), ref_column, { size, utils::constraints::ForeignKey }};
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute make_fk_column(const std::string &name, const std::shared_ptr<attribute> &ref_column)
|
||||
{
|
||||
return {name, utils::data_type_traits<Type>::type(0), 0, ref_column, { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL};
|
||||
}
|
||||
|
||||
template <>
|
||||
[[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column);
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute make_fk_column(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
return {
|
||||
name, utils::data_type_traits<Type>::type(size), 0,
|
||||
std::make_shared<attribute>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(size), utils::constraints::ForeignKey),
|
||||
{ 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute make_fk_column(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
return {
|
||||
name, utils::data_type_traits<Type>::type(0), 0,
|
||||
std::make_shared<attribute>(ref_column_name, utils::data_type_traits<Type>::type(0), ref_table_name, attribute_options{utils::constraints::ForeignKey}),
|
||||
{ 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template <>
|
||||
[[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name);
|
||||
|
||||
template <>
|
||||
[[maybe_unused]] 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);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@
|
|||
#include "matador/utils/data_type_traits.hpp"
|
||||
#include "matador/utils/error.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
#include "matador/utils/identifier.hpp"
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
#include "matador/utils/result.hpp"
|
||||
|
||||
|
|
@ -92,16 +91,18 @@ public:
|
|||
on_foreign_key(id, x);
|
||||
}
|
||||
template<class Pointer>
|
||||
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {
|
||||
// on_foreign_key(id, x);
|
||||
}
|
||||
|
||||
template <class Pointer>
|
||||
void on_foreign_key(const char *id, Pointer &x) {
|
||||
attribute* ref_column;
|
||||
std::shared_ptr<attribute> ref_column;
|
||||
std::type_index ti = typeid(typename Pointer::value_type);
|
||||
if (auto result = determine_foreign_ref(std::type_index(ti))) {
|
||||
if (const auto result = determine_foreign_ref(std::type_index(ti))) {
|
||||
ref_column = *result;
|
||||
} else {
|
||||
ref_column = nullptr;
|
||||
ref_column = std::make_shared<attribute>();
|
||||
insert_missing_reference_column(ti, ref_column);
|
||||
}
|
||||
if (x.empty()) {
|
||||
|
|
@ -119,17 +120,8 @@ public:
|
|||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
[[nodiscard]] utils::result<attribute*, utils::error> determine_foreign_ref(const std::type_index &ti) const;
|
||||
void insert_missing_reference_column(const std::type_index &ti, attribute* ref_column) const;
|
||||
|
||||
template<typename ValueType>
|
||||
attribute &emplace_attribute(const char *id, const utils::field_attributes& attr, null_option_type null_option) {
|
||||
auto &ref = columns_.emplace_back(id, utils::data_type_traits<ValueType>::type(attr.size()), attr, null_option);
|
||||
ref.owner_ = &obj_;
|
||||
return ref;
|
||||
}
|
||||
|
||||
void prepare_primary_key(attribute &ref, utils::identifier &&pk) const;
|
||||
[[nodiscard]] utils::result<std::shared_ptr<attribute>, utils::error> determine_foreign_ref(const std::type_index &ti) const;
|
||||
void insert_missing_reference_column(const std::type_index &ti, const std::shared_ptr<attribute>& ref_column) const;
|
||||
|
||||
private:
|
||||
size_t index_ = 0;
|
||||
|
|
@ -142,18 +134,19 @@ private:
|
|||
|
||||
template<typename ValueType>
|
||||
void attribute_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
||||
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NOT_NULL);
|
||||
prepare_primary_key(ref, utils::identifier(x));
|
||||
on_attribute(id, x, { attr.size(), utils::constraints::PrimaryKey });
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void attribute_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) {
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NOT_NULL);
|
||||
auto &ref = columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option_type::NOT_NULL);
|
||||
ref.owner_ = &obj_;
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void attribute_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr) {
|
||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NULLABLE);
|
||||
auto &ref = columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option_type::NULLABLE);
|
||||
ref.owner_ = &obj_;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,11 +25,10 @@ public:
|
|||
[[nodiscard]] std::type_index type_index() const;
|
||||
[[nodiscard]] std::string name() const;
|
||||
[[nodiscard]] const std::vector<attribute>& attributes() const;
|
||||
// [[nodiscard]] std::shared_ptr<attribute> reference_column() const;
|
||||
[[nodiscard]] std::shared_ptr<attribute> reference_column() const;
|
||||
|
||||
[[nodiscard]] bool has_primary_key() const;
|
||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||
[[nodiscard]] attribute* primary_key_attribute() const;
|
||||
|
||||
endpoint_iterator register_relation_endpoint(const std::type_index &type, const std::shared_ptr<relation_endpoint> &endpoint);
|
||||
void unregister_relation_endpoint(const std::type_index &type);
|
||||
|
|
@ -50,13 +49,14 @@ public:
|
|||
[[nodiscard]] bool endpoints_empty() const;
|
||||
|
||||
protected:
|
||||
// basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute> &attributes, utils::identifier &&pk, const std::shared_ptr<attribute> &pk_as_fk_column);
|
||||
// basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute> &attributes);
|
||||
basic_object_info(std::shared_ptr<repository_node> node, std::unique_ptr<object> &&obj);
|
||||
basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute> &attributes, utils::identifier &&pk, const std::shared_ptr<attribute> &pk_as_fk_column);
|
||||
basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute> &attributes);
|
||||
|
||||
protected:
|
||||
std::unique_ptr<object> object_;
|
||||
std::shared_ptr<repository_node> node_; /**< prototype node of the represented object type */
|
||||
std::vector<attribute> attributes_;
|
||||
std::optional<utils::identifier> identifier_;
|
||||
std::shared_ptr<attribute> pk_as_fk_column_;
|
||||
t_endpoint_map relation_endpoints_;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ enum class error_code : uint8_t {
|
|||
OK = 0,
|
||||
NodeNotFound = 1,
|
||||
NodeAlreadyExists = 2,
|
||||
NoPrimaryKey = 3,
|
||||
Failure,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@
|
|||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/constraints_generator.hpp"
|
||||
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class repository;
|
||||
|
|
@ -29,15 +27,9 @@ public:
|
|||
void add_attribute(attribute attr);
|
||||
void add_constraint(class constraint c);
|
||||
|
||||
[[nodiscard]] attribute* primary_key_attribute() const;
|
||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||
[[nodiscard]] bool has_primary_key() const;
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const std::string& alias() const;
|
||||
|
||||
void update_name(const std::string& name);
|
||||
|
||||
[[nodiscard]] bool has_attributes() const;
|
||||
[[nodiscard]] size_t attribute_count() const;
|
||||
[[nodiscard]] const std::vector<attribute>& attributes() const;
|
||||
|
|
@ -48,13 +40,10 @@ public:
|
|||
|
||||
private:
|
||||
friend class constraints_generator;
|
||||
friend class attribute_generator;
|
||||
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
|
||||
attribute* pk_attribute_{nullptr};
|
||||
utils::identifier pk_identifier_;
|
||||
std::vector<attribute> attributes_;
|
||||
std::vector<class constraint> constraints_;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -12,12 +12,19 @@ public:
|
|||
using create_func = std::function<std::unique_ptr<Type>()>;
|
||||
|
||||
object_info(const std::shared_ptr<repository_node>& node,
|
||||
std::unique_ptr<object> &&obj,
|
||||
const std::vector<attribute> &attributes,
|
||||
utils::identifier &&pk,
|
||||
const std::shared_ptr<attribute> &ref_column,
|
||||
create_func&& creator)
|
||||
: basic_object_info(node, std::move(obj))
|
||||
: basic_object_info(node, attributes, std::move(pk), ref_column)
|
||||
, creator_(std::move(creator)){
|
||||
}
|
||||
object_info(const std::shared_ptr<repository_node>& node,
|
||||
const std::vector<attribute> &attributes,
|
||||
create_func&& creator)
|
||||
: basic_object_info(node, attributes)
|
||||
, creator_(std::move(creator)){
|
||||
}
|
||||
|
||||
explicit object_info(const std::shared_ptr<repository_node>& node)
|
||||
: basic_object_info(node, {}) {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -151,7 +151,8 @@ public:
|
|||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
[[nodiscard]] utils::result<attribute*, utils::error> primary_key_attribute(const std::type_index &type_index) const;
|
||||
[[nodiscard]] utils::result<std::shared_ptr<attribute>, utils::error> reference_column(
|
||||
const std::type_index &type_index) const;
|
||||
|
||||
void dump(std::ostream &os) const;
|
||||
static void dump(std::ostream &os, const node_ptr& node);
|
||||
|
|
@ -187,7 +188,7 @@ private:
|
|||
t_type_index_node_map nodes_by_type_;
|
||||
logger::logger log_;
|
||||
|
||||
std::unordered_map<std::type_index, attribute*> missing_references_;
|
||||
std::unordered_map<std::type_index, std::shared_ptr<attribute> > missing_references_;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,10 @@ public:
|
|||
const auto attributes = attribute_generator::generate<Type>(repo, *obj);
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
node,
|
||||
std::move(obj),
|
||||
attributes,
|
||||
std::move(pk_info.pk),
|
||||
ref_column,
|
||||
// obj,
|
||||
[]{ return std::make_unique<Type>(); }
|
||||
);
|
||||
node->info_ = std::move(info);
|
||||
|
|
@ -90,10 +93,10 @@ private:
|
|||
void unlink();
|
||||
|
||||
static utils::result<node_ptr, utils::error> make_and_attach_node(repository& repo, const std::string& name, const std::type_index& ti);
|
||||
static attribute* determine_reference_column(const std::type_index& ti,
|
||||
const std::string& table_name,
|
||||
const primary_key_info& pk_info,
|
||||
repository& repo);
|
||||
static std::shared_ptr<attribute> determine_reference_column(const std::type_index& ti,
|
||||
const std::string& table_name,
|
||||
const primary_key_info& pk_info,
|
||||
repository& repo);
|
||||
|
||||
private:
|
||||
friend class repository;
|
||||
|
|
|
|||
|
|
@ -1,22 +1,49 @@
|
|||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/object.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, {}, null_option_type::NOT_NULL) {
|
||||
: 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 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))
|
||||
, options_{attr, null_opt}
|
||||
, type_(type)
|
||||
{}
|
||||
, table_name_(std::move(table_name))
|
||||
, options_( options )
|
||||
, type_( type )
|
||||
, reference_column_( ref_column ) {
|
||||
}
|
||||
|
||||
const std::string &attribute::name() const {
|
||||
return name_;
|
||||
|
|
@ -27,7 +54,7 @@ void attribute::name( const std::string& n ) {
|
|||
}
|
||||
|
||||
std::string attribute::full_name() const {
|
||||
return owner_ ? owner_->name() + "." + name_ : name_;
|
||||
return !table_name_.empty() ? table_name_ + "." + name_ : name_;
|
||||
}
|
||||
|
||||
int attribute::index() const {
|
||||
|
|
@ -50,23 +77,27 @@ utils::basic_type attribute::type() const {
|
|||
return type_;
|
||||
}
|
||||
|
||||
object* attribute::owner() const {
|
||||
return owner_;
|
||||
const std::string& attribute::table_name() const {
|
||||
return table_name_;
|
||||
}
|
||||
|
||||
// const std::string& attribute::table_name() const {
|
||||
// return owner_ ? owner_->name() : "";
|
||||
// }
|
||||
//
|
||||
// void attribute::table_name( const std::string& name ) {
|
||||
// table_name_ = 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;
|
||||
}
|
||||
|
|
@ -103,45 +134,45 @@ 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
|
||||
// };
|
||||
// }
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
#include "matador/object/object.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
|
|
@ -14,16 +13,12 @@ void attribute_generator::on_revision(const char *id, uint64_t &rev) {
|
|||
on_attribute(id, rev);
|
||||
}
|
||||
|
||||
utils::result<attribute*, utils::error> attribute_generator::determine_foreign_ref(const std::type_index &ti) const {
|
||||
return repo_.primary_key_attribute(ti);
|
||||
utils::result<std::shared_ptr<attribute>, utils::error> attribute_generator::determine_foreign_ref(const std::type_index &ti) const {
|
||||
return repo_.reference_column(ti);
|
||||
}
|
||||
|
||||
void attribute_generator::insert_missing_reference_column(const std::type_index& ti, attribute* ref_column) const {
|
||||
void attribute_generator::insert_missing_reference_column(const std::type_index& ti, const std::shared_ptr<attribute>& ref_column) const {
|
||||
const_cast<repository&>(repo_).missing_references_.insert({ti, ref_column});
|
||||
}
|
||||
|
||||
void attribute_generator::prepare_primary_key(attribute& ref, utils::identifier &&pk) const {
|
||||
obj_.pk_attribute_ = &ref;
|
||||
obj_.pk_identifier_ = std::move(pk);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,24 +5,20 @@
|
|||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
// basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
// const std::vector<attribute> &attributes,
|
||||
// utils::identifier &&pk,
|
||||
// const std::shared_ptr<attribute> &pk_as_fk_column)
|
||||
// : node_(std::move(node))
|
||||
// , attributes_(attributes)
|
||||
// , identifier_(std::move(pk))
|
||||
// , pk_as_fk_column_(pk_as_fk_column) {
|
||||
// }
|
||||
//
|
||||
// basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
// const std::vector<attribute> &attributes)
|
||||
// : node_(std::move(node))
|
||||
// , attributes_(attributes) {}
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
const std::vector<attribute> &attributes,
|
||||
utils::identifier &&pk,
|
||||
const std::shared_ptr<attribute> &pk_as_fk_column)
|
||||
: node_(std::move(node))
|
||||
, attributes_(attributes)
|
||||
, identifier_(std::move(pk))
|
||||
, pk_as_fk_column_(pk_as_fk_column) {
|
||||
}
|
||||
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node, std::unique_ptr<object>&& obj)
|
||||
: object_(std::move(obj))
|
||||
, node_(std::move(node)) {}
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
const std::vector<attribute> &attributes)
|
||||
: node_(std::move(node))
|
||||
, attributes_(attributes) {}
|
||||
|
||||
std::type_index basic_object_info::type_index() const {
|
||||
return node_->type_index();
|
||||
|
|
@ -33,23 +29,19 @@ std::string basic_object_info::name() const {
|
|||
}
|
||||
|
||||
const std::vector<attribute>& basic_object_info::attributes() const {
|
||||
return object_->attributes();
|
||||
return attributes_;
|
||||
}
|
||||
|
||||
// std::shared_ptr<attribute> basic_object_info::reference_column() const {
|
||||
// return pk_as_fk_column_;
|
||||
// }
|
||||
std::shared_ptr<attribute> basic_object_info::reference_column() const {
|
||||
return pk_as_fk_column_;
|
||||
}
|
||||
|
||||
bool basic_object_info::has_primary_key() const {
|
||||
return object_->has_primary_key();
|
||||
return identifier_.has_value();
|
||||
}
|
||||
|
||||
const utils::identifier& basic_object_info::primary_key() const {
|
||||
return object_->primary_key();
|
||||
}
|
||||
|
||||
attribute* basic_object_info::primary_key_attribute() const {
|
||||
return object_->primary_key_attribute();
|
||||
return identifier_.value();
|
||||
}
|
||||
|
||||
basic_object_info::endpoint_iterator basic_object_info::register_relation_endpoint(const std::type_index &type, const std::shared_ptr<relation_endpoint> &endpoint) {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ void constraints_generator::create_pk_constraint(const std::string& name) const
|
|||
}
|
||||
|
||||
void constraints_generator::create_fk_constraint(const std::string& name, const basic_object_info& info) const {
|
||||
const auto *pk_attribute = info.primary_key_attribute();
|
||||
const auto pk_attribute = info.reference_column();
|
||||
class constraint pk_constraint("FK_" + obj_.name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::ForeignKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) {
|
||||
|
|
@ -27,7 +27,7 @@ void constraints_generator::create_fk_constraint(const std::string& name, const
|
|||
}
|
||||
pk_constraint.owner_ = &obj_;
|
||||
pk_constraint.ref_column_name_ = pk_attribute->name();
|
||||
pk_constraint.ref_table_name_ = pk_attribute->owner() ? pk_attribute->owner()->name() : "";
|
||||
pk_constraint.ref_table_name_ = pk_attribute->table_name();
|
||||
constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,8 +14,6 @@ std::string object_category_impl::message(const int ev) const {
|
|||
return "Node not found";
|
||||
case error_code::NodeAlreadyExists:
|
||||
return "Node already exists";
|
||||
case error_code::NoPrimaryKey:
|
||||
return "No primary key";
|
||||
case error_code::Failure:
|
||||
return "Failure";
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -21,18 +21,6 @@ void object::add_constraint( class constraint c ) {
|
|||
ref.owner_ = this;
|
||||
}
|
||||
|
||||
attribute* object::primary_key_attribute() const {
|
||||
return pk_attribute_;
|
||||
}
|
||||
|
||||
const utils::identifier& object::primary_key() const {
|
||||
return pk_identifier_;
|
||||
}
|
||||
|
||||
bool object::has_primary_key() const {
|
||||
return pk_attribute_ != nullptr;
|
||||
}
|
||||
|
||||
const std::string& object::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
|
@ -41,10 +29,6 @@ const std::string& object::alias() const {
|
|||
return alias_;
|
||||
}
|
||||
|
||||
void object::update_name(const std::string& name) {
|
||||
name_ = name;
|
||||
}
|
||||
|
||||
bool object::has_attributes() const {
|
||||
return attributes_.empty();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,16 +62,13 @@ utils::result<basic_object_info_ref, utils::error> repository::basic_info( const
|
|||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
utils::result<attribute*, utils::error> repository::primary_key_attribute(const std::type_index &type_index) const {
|
||||
utils::result<std::shared_ptr<attribute>, utils::error> repository::reference_column(const std::type_index &type_index) const {
|
||||
const auto result = find_node(type_index);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
if (result) {
|
||||
return utils::ok((*result)->info().reference_column());
|
||||
}
|
||||
|
||||
if (!result.value()->info().has_primary_key()) {
|
||||
return utils::failure(make_error(error_code::NodeAlreadyExists, "Object '" + result.value()->name() + "' does not have a primary key."));
|
||||
}
|
||||
return utils::ok((*result)->info().primary_key_attribute());
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
void repository::dump(std::ostream &os) const {
|
||||
|
|
|
|||
|
|
@ -45,9 +45,9 @@ const basic_object_info &repository_node::info() const {
|
|||
|
||||
void repository_node::update_name(const std::string& name) {
|
||||
name_ = name;
|
||||
// if (info_->reference_column()) {
|
||||
// info_->reference_column()->table_name(name);
|
||||
// }
|
||||
if (info_->reference_column()) {
|
||||
info_->reference_column()->table_name(name);
|
||||
}
|
||||
}
|
||||
|
||||
const repository& repository_node::schema() const {
|
||||
|
|
@ -104,19 +104,19 @@ utils::result<repository_node::node_ptr, utils::error> repository_node::make_and
|
|||
return repo.attach_node(node, "");
|
||||
}
|
||||
|
||||
attribute* repository_node::determine_reference_column(const std::type_index& ti,
|
||||
const std::string& table_name,
|
||||
const primary_key_info& pk_info,
|
||||
repository& repo) {
|
||||
std::shared_ptr<attribute> repository_node::determine_reference_column(const std::type_index& ti,
|
||||
const std::string& table_name,
|
||||
const primary_key_info& pk_info,
|
||||
repository& repo) {
|
||||
const auto it = repo.missing_references_.find(ti);
|
||||
if (it == repo.missing_references_.end()) {
|
||||
return new attribute(pk_info.pk_column_name, pk_info.type, {utils::constraints::ForeignKey}, null_option_type::NOT_NULL);
|
||||
return std::make_shared<attribute>(pk_info.pk_column_name, pk_info.type, table_name, attribute_options{utils::constraints::ForeignKey});
|
||||
}
|
||||
|
||||
auto ref_column = it->second;
|
||||
repo.missing_references_.erase(it);
|
||||
ref_column->name(pk_info.pk_column_name);
|
||||
ref_column->owner()->update_name(table_name);
|
||||
ref_column->table_name(table_name);
|
||||
ref_column->change_type(pk_info.type);
|
||||
ref_column->attributes() = utils::constraints::ForeignKey;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,15 +7,14 @@
|
|||
#include "../test/models/book.hpp"
|
||||
|
||||
TEST_CASE("Generate object from type", "[object][generate]") {
|
||||
using namespace matador;
|
||||
object::repository repo;
|
||||
auto result = repo.attach<test::author>("authors");
|
||||
REQUIRE(result);
|
||||
using namespace matador;
|
||||
object::repository repo;
|
||||
auto result = repo.attach<test::author>("authors");
|
||||
REQUIRE(result);
|
||||
|
||||
const auto obj = object::object::generate<test::book>(repo, "books");
|
||||
REQUIRE(obj->name() == "books");
|
||||
REQUIRE(obj->alias().empty());
|
||||
REQUIRE(obj->attributes().size() == 4);
|
||||
REQUIRE(obj->constraints().size() == 2);
|
||||
REQUIRE(obj->has_primary_key());
|
||||
const auto obj = object::object::generate<test::book>(repo, "books");
|
||||
REQUIRE(obj->name() == "books");
|
||||
REQUIRE(obj->alias().empty());
|
||||
REQUIRE(obj->attributes().size() == 4);
|
||||
REQUIRE(obj->constraints().size() == 2);
|
||||
}
|
||||
Loading…
Reference in New Issue