attribute_definition and object_definition progress

This commit is contained in:
Sascha Kühl
2025-11-17 15:58:09 +01:00
parent 9ffcee1317
commit 7242dc2612
17 changed files with 161 additions and 205 deletions
+29 -26
View File
@@ -11,10 +11,18 @@
namespace matador::object {
enum class null_option : uint8_t {
enum class null_option_type : uint8_t {
NULLABLE, NOT_NULL
};
class object_definition;
struct attribute_options {
utils::field_attributes attributes;
null_option_type null_option{null_option_type::NOT_NULL};
int index{-1};
};
class attribute_definition {
public:
explicit attribute_definition(const char *name); // NOLINT(*-explicit-constructor)
@@ -26,12 +34,6 @@ public:
attribute_definition& operator=(attribute_definition&&) noexcept = default;
attribute_definition() = default;
template<typename Type>
attribute_definition(std::string name, std::string table_name, const utils::field_attributes& attr)
: attribute_definition(std::move(name), std::move(table_name), utils::data_type_traits<Type>::type(attr.size()), attr)
{}
attribute_definition(std::string name, std::string table_name, utils::basic_type type, const utils::field_attributes& attr);
template<typename Type>
attribute_definition(std::string name, const utils::field_attributes& attr)
@@ -39,30 +41,32 @@ public:
{}
template<typename Type>
attribute_definition(std::string name, const Type &, const utils::field_attributes& attr, null_option null_opt)
attribute_definition(std::string name, const Type &, const utils::field_attributes& attr, null_option_type null_opt)
: attribute_definition(std::move(name), utils::data_type_traits<Type>::type(attr.size()), attr, null_opt)
{}
template<size_t SIZE>
attribute_definition(std::string name, const char (&)[SIZE], const utils::field_attributes& attr, const null_option null_opt)
attribute_definition(std::string name, const char (&)[SIZE], const utils::field_attributes& attr, const null_option_type null_opt)
: attribute_definition(std::move(name), utils::data_type_traits<const char*>::type(attr.size()), attr, null_opt)
{}
attribute_definition(std::string name, utils::basic_type type, const utils::field_attributes&, null_option null_opt, int index = 0);
attribute_definition(std::string name, std::string table_name, utils::basic_type type, const utils::field_attributes&, null_option null_opt, int index = 0);
attribute_definition(std::string name, utils::basic_type type, const utils::field_attributes&, null_option_type null_opt, int index = 0);
template<typename Type>
attribute_definition(std::string name, const std::shared_ptr<attribute_definition> &ref_column, const utils::field_attributes& attr, null_option null_opt)
attribute_definition(std::string name, const std::shared_ptr<attribute_definition> &ref_column, const utils::field_attributes& attr, null_option_type null_opt)
: attribute_definition(std::move(name), utils::data_type_traits<Type>::type(attr.size()), ref_column, attr, null_opt)
{}
attribute_definition(std::string name, utils::basic_type type, int index, const std::shared_ptr<attribute_definition> &ref_column, const utils::field_attributes& attr, null_option null_opt);
attribute_definition(std::string name, utils::basic_type type, int index, const std::shared_ptr<attribute_definition> &ref_column, const utils::field_attributes& attr, null_option_type null_opt);
attribute_definition(std::string name, utils::basic_type type, const std::shared_ptr<attribute_definition> &ref_column = {});
attribute_definition(std::string name, utils::basic_type type, const attribute_options& options, const std::shared_ptr<object_definition>& obj = {}, const std::shared_ptr<attribute_definition> &ref_column = {});
[[nodiscard]] const std::string& name() const;
void name(const std::string& n);
[[nodiscard]] std::string full_name() const;
[[nodiscard]] std::string table_name() const;
void table_name(const std::string& tn);
[[nodiscard]] std::shared_ptr<object_definition> object() const;
void object(const std::shared_ptr<object_definition> &obj);
[[nodiscard]] int index() const;
[[nodiscard]] const utils::field_attributes& attributes() const;
[[nodiscard]] utils::field_attributes& attributes();
@@ -95,15 +99,14 @@ private:
using data_type_index = std::vector<utils::basic_type>;
private:
friend class object_definition;
static const data_type_index data_type_index_;
std::string name_;
std::string table_;
int index_{-1};
utils::field_attributes attributes_;
null_option null_option_{null_option::NOT_NULL};
std::shared_ptr<object_definition> object_{};
attribute_options options_;
utils::basic_type type_{utils::basic_type::type_null};
size_t size_{0};
std::shared_ptr<attribute_definition> reference_column_;
};
@@ -116,15 +119,15 @@ private:
* @param null_opt
* @return A column object with a given name
*/
attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr = utils::null_attributes, null_option null_opt = null_option::NOT_NULL);
attribute_definition 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_definition make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes, null_option null_opt = null_option::NOT_NULL)
attribute_definition 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_definition make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option null_opt);
attribute_definition make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option_type null_opt);
template < typename Type >
attribute_definition make_pk_column(const std::string &name, size_t size = 0)
@@ -143,7 +146,7 @@ attribute_definition make_fk_column(const std::string &name, size_t size, const
template < typename Type >
[[maybe_unused]] attribute_definition make_fk_column(const std::string &name, const std::shared_ptr<attribute_definition> &ref_column)
{
return {name, utils::data_type_traits<Type>::type(0), 0, ref_column, { 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL};
return {name, utils::data_type_traits<Type>::type(0), 0, ref_column, { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
}
template <>
@@ -154,7 +157,7 @@ template < typename Type >
return {
name, utils::data_type_traits<Type>::type(size), 0,
std::make_shared<attribute_definition>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(size), utils::constraints::FOREIGN_KEY),
{ 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
};
}
@@ -163,7 +166,7 @@ template < typename Type >
return {
name, utils::data_type_traits<Type>::type(0), 0,
std::make_shared<attribute_definition>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(0), utils::constraints::FOREIGN_KEY),
{ 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
};
}
@@ -26,7 +26,7 @@ public:
template<class Type>
attribute_definition generate(const char *id, Type &x, const std::shared_ptr<attribute_definition> &ref_column) {
access::process(*this, x);
return attribute_definition{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL};
return attribute_definition{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
}
template<typename ValueType>
@@ -122,7 +122,7 @@ public:
private:
[[nodiscard]] utils::result<std::shared_ptr<attribute_definition>, utils::error> determine_foreign_ref(const std::type_index &ti) const;
void insert_missing_reference_column(const std::type_index &ti, std::shared_ptr<attribute_definition> ref_column) const;
void insert_missing_reference_column(const std::type_index &ti, const std::shared_ptr<attribute_definition>& ref_column) const;
private:
size_t index_ = 0;
@@ -140,13 +140,13 @@ void attribute_definition_generator::on_primary_key(const char *id, ValueType &x
template<typename Type>
void attribute_definition_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr)
{
columns_.emplace_back(id, x, attr, null_option::NOT_NULL);
columns_.emplace_back(id, x, attr, null_option_type::NOT_NULL);
}
template<typename Type>
void attribute_definition_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr)
{
columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option::NULLABLE);
columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option_type::NULLABLE);
}
}
+9 -3
View File
@@ -18,14 +18,17 @@ public:
using const_iterator = column_by_index::const_iterator;
object_definition() = default;
object_definition(std::initializer_list<attribute_definition> columns);
explicit object_definition(const std::vector<attribute_definition> &columns);
explicit object_definition(std::string name);
object_definition(std::string name, std::initializer_list<attribute_definition> columns);
explicit object_definition(std::string name, const std::vector<attribute_definition> &columns);
object_definition(const object_definition &x);
object_definition& operator=(const object_definition &x);
object_definition(object_definition&&) noexcept = default;
object_definition& operator=(object_definition&&) noexcept = default;
~object_definition() = default;
[[nodiscard]] const std::string& name() const;
[[nodiscard]] bool has_primary_key() const;
[[nodiscard]] std::optional<attribute_definition> primary_key() const;
@@ -33,7 +36,7 @@ public:
void append(const std::string &name, long size = -1) {
append(make_column<Type>(name, size));
}
void append(attribute_definition col);
void append(attribute_definition&& col);
[[nodiscard]] const std::vector<attribute_definition>& columns() const;
@@ -60,6 +63,9 @@ private:
void add_to_map(attribute_definition &col, size_t index);
private:
friend class repository_node;
std::string name_;
column_by_index columns_;
column_by_name_map columns_by_name_;
+14 -9
View File
@@ -17,17 +17,22 @@ public:
using node_ptr = std::shared_ptr<repository_node>;
template < typename Type >
static std::shared_ptr<repository_node> make_node(object::repository& tree, const std::string& name) {
auto node = std::shared_ptr<repository_node>(new repository_node(tree, name, typeid(Type)));
static std::shared_ptr<repository_node> make_node(repository& repo, const std::string& name) {
auto node = std::shared_ptr<repository_node>(new repository_node(repo, name, typeid(Type)));
primary_key_resolver resolver;
object_definition obj{name, {}};
auto pk_info = resolver.resolve<Type>();
auto ref_column = determine_reference_column(typeid(Type), name, pk_info, tree);
auto ref_column = determine_reference_column(typeid(Type), name, pk_info, repo);
const auto attributes = attribute_definition_generator::generate<Type>(repo);
for (auto&& attr : attributes) {
obj.append(std::move(attr));
}
auto info = std::make_unique<object_info<Type>>(
node,
std::move(pk_info.pk),
ref_column,
object_definition{attribute_definition_generator::generate<Type>(tree)},
std::move(obj),
[]{ return std::make_unique<Type>(); }
);
node->info_ = std::move(info);
@@ -87,11 +92,11 @@ private:
void unlink();
static utils::result<node_ptr, utils::error> make_and_attach_node(object::repository& tree, const std::string& name, const std::type_index& ti);
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_definition> determine_reference_column(const std::type_index& ti,
const std::string& name,
const primary_key_info& pk_info,
object::repository& tree);
object_definition& obj,
const primary_key_info& pk_info,
repository& repo);
private:
friend class repository;
@@ -100,7 +105,7 @@ private:
friend class foreign_node_completer;
friend class const_repository_node_iterator;
object::repository &repo_;
repository &repo_;
std::type_index type_index_;
std::unique_ptr<basic_object_info> info_;