object generating progress
This commit is contained in:
@@ -23,7 +23,6 @@ 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;
|
||||
@@ -35,24 +34,8 @@ 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;
|
||||
@@ -61,15 +44,14 @@ public:
|
||||
[[nodiscard]] utils::field_attributes& attributes();
|
||||
[[nodiscard]] bool is_nullable() const;
|
||||
[[nodiscard]] utils::basic_type type() const;
|
||||
[[nodiscard]] const std::string& table_name() const;
|
||||
void table_name(const std::string& name);
|
||||
[[nodiscard]] object* owner() const;
|
||||
// [[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;
|
||||
@@ -92,11 +74,8 @@ 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_;
|
||||
};
|
||||
|
||||
|
||||
@@ -108,62 +87,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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -96,12 +96,12 @@ public:
|
||||
|
||||
template <class Pointer>
|
||||
void on_foreign_key(const char *id, Pointer &x) {
|
||||
std::shared_ptr<attribute> ref_column;
|
||||
attribute* ref_column;
|
||||
std::type_index ti = typeid(typename Pointer::value_type);
|
||||
if (const auto result = determine_foreign_ref(std::type_index(ti))) {
|
||||
if (auto result = determine_foreign_ref(std::type_index(ti))) {
|
||||
ref_column = *result;
|
||||
} else {
|
||||
ref_column = std::make_shared<attribute>();
|
||||
ref_column = nullptr;
|
||||
insert_missing_reference_column(ti, ref_column);
|
||||
}
|
||||
if (x.empty()) {
|
||||
@@ -119,8 +119,8 @@ public:
|
||||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
[[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;
|
||||
[[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) {
|
||||
|
||||
@@ -25,10 +25,11 @@ 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);
|
||||
@@ -49,15 +50,13 @@ 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, 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);
|
||||
|
||||
protected:
|
||||
std::unique_ptr<object> node_ptr_;
|
||||
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,6 +10,7 @@ enum class error_code : uint8_t {
|
||||
OK = 0,
|
||||
NodeNotFound = 1,
|
||||
NodeAlreadyExists = 2,
|
||||
NoPrimaryKey = 3,
|
||||
Failure,
|
||||
};
|
||||
|
||||
|
||||
@@ -29,13 +29,15 @@ public:
|
||||
void add_attribute(attribute attr);
|
||||
void add_constraint(class constraint c);
|
||||
|
||||
[[nodiscard]] const attribute* primary_key_attribute() const;
|
||||
[[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;
|
||||
|
||||
@@ -12,19 +12,12 @@ public:
|
||||
using create_func = std::function<std::unique_ptr<Type>()>;
|
||||
|
||||
object_info(const std::shared_ptr<repository_node>& node,
|
||||
const std::vector<attribute> &attributes,
|
||||
utils::identifier &&pk,
|
||||
const std::shared_ptr<attribute> &ref_column,
|
||||
std::unique_ptr<object> &&obj,
|
||||
create_func&& creator)
|
||||
: 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)
|
||||
: basic_object_info(node, std::move(obj))
|
||||
, creator_(std::move(creator)){
|
||||
}
|
||||
|
||||
explicit object_info(const std::shared_ptr<repository_node>& node)
|
||||
: basic_object_info(node, {}) {
|
||||
}
|
||||
|
||||
@@ -151,8 +151,7 @@ public:
|
||||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
[[nodiscard]] utils::result<std::shared_ptr<attribute>, utils::error> reference_column(
|
||||
const std::type_index &type_index) const;
|
||||
[[nodiscard]] utils::result<attribute*, utils::error> primary_key_attribute(const std::type_index &type_index) const;
|
||||
|
||||
void dump(std::ostream &os) const;
|
||||
static void dump(std::ostream &os, const node_ptr& node);
|
||||
@@ -188,7 +187,7 @@ private:
|
||||
t_type_index_node_map nodes_by_type_;
|
||||
logger::logger log_;
|
||||
|
||||
std::unordered_map<std::type_index, std::shared_ptr<attribute> > missing_references_;
|
||||
std::unordered_map<std::type_index, attribute*> missing_references_;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -28,10 +28,7 @@ public:
|
||||
const auto attributes = attribute_generator::generate<Type>(repo, *obj);
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
node,
|
||||
attributes,
|
||||
std::move(pk_info.pk),
|
||||
ref_column,
|
||||
// obj,
|
||||
std::move(obj),
|
||||
[]{ return std::make_unique<Type>(); }
|
||||
);
|
||||
node->info_ = std::move(info);
|
||||
@@ -93,10 +90,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 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);
|
||||
static 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;
|
||||
|
||||
Reference in New Issue
Block a user