renamed class attribute_definition to attribute

This commit is contained in:
Sascha Kühl
2025-11-21 09:22:00 +01:00
parent dae3c1645b
commit 758284c2b3
44 changed files with 490 additions and 491 deletions
+163
View File
@@ -0,0 +1,163 @@
#ifndef QUERY_COLUMN_DEFINITION_HPP
#define QUERY_COLUMN_DEFINITION_HPP
#include "matador/utils/basic_types.hpp"
#include "matador/utils/default_type_traits.hpp"
#include "matador/utils/field_attributes.hpp"
#include <memory>
namespace matador::object {
enum class null_option_type : uint8_t {
NULLABLE, NOT_NULL
};
struct attribute_options {
utils::field_attributes attributes;
null_option_type null_option{null_option_type::NOT_NULL};
int index{-1};
};
class attribute {
public:
explicit attribute(const char *name); // NOLINT(*-explicit-constructor)
explicit attribute(std::string name); // NOLINT(*-explicit-constructor)
attribute(const attribute&) = default;
attribute& operator=(const attribute&) = default;
attribute(attribute&&) noexcept = default;
attribute& operator=(attribute&&) noexcept = default;
attribute() = default;
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;
[[nodiscard]] int index() const;
[[nodiscard]] const utils::field_attributes& attributes() const;
[[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);
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;
[[nodiscard]] bool is_bool() const;
[[nodiscard]] bool is_string() const;
[[nodiscard]] bool is_varchar() const;
[[nodiscard]] bool is_date() const;
[[nodiscard]] bool is_time() const;
[[nodiscard]] bool is_blob() const;
[[nodiscard]] bool is_null() const;
template< typename Type >
[[nodiscard]] bool is_type_of() const {
return type() == utils::data_type_traits<Type>::type(attributes().size());
}
private:
std::string name_;
std::string table_name_;
attribute_options options_;
utils::basic_type type_{utils::basic_type::type_null};
std::shared_ptr<attribute> reference_column_;
};
/**
* User defined literal to have a shortcut creating a column object
* @param name Name of the column
* @param type
* @param attr Length of the column name
* @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::PRIMARY_KEY });
}
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::FOREIGN_KEY }};
}
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::FOREIGN_KEY }, 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::FOREIGN_KEY),
{ 0, utils::constraints::FOREIGN_KEY }, 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::FOREIGN_KEY}),
{ 0, utils::constraints::FOREIGN_KEY }, 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);
}
#endif //QUERY_COLUMN_DEFINITION_HPP
@@ -1,163 +0,0 @@
#ifndef QUERY_COLUMN_DEFINITION_HPP
#define QUERY_COLUMN_DEFINITION_HPP
#include "matador/utils/basic_types.hpp"
#include "matador/utils/default_type_traits.hpp"
#include "matador/utils/field_attributes.hpp"
#include <memory>
namespace matador::object {
enum class null_option_type : uint8_t {
NULLABLE, NOT_NULL
};
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)
explicit attribute_definition(std::string name); // NOLINT(*-explicit-constructor)
attribute_definition(const attribute_definition&) = default;
attribute_definition& operator=(const attribute_definition&) = default;
attribute_definition(attribute_definition&&) noexcept = default;
attribute_definition& operator=(attribute_definition&&) noexcept = default;
attribute_definition() = default;
attribute_definition(std::string name,
utils::basic_type type,
const utils::field_attributes&,
null_option_type null_opt,
int index = 0);
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,
std::string table_name,
const attribute_options& options,
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]] int index() const;
[[nodiscard]] const utils::field_attributes& attributes() const;
[[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);
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_definition> reference_column() const;
[[nodiscard]] bool is_foreign_reference() const;
[[nodiscard]] bool is_integer() const;
[[nodiscard]] bool is_floating_point() const;
[[nodiscard]] bool is_bool() const;
[[nodiscard]] bool is_string() const;
[[nodiscard]] bool is_varchar() const;
[[nodiscard]] bool is_date() const;
[[nodiscard]] bool is_time() const;
[[nodiscard]] bool is_blob() const;
[[nodiscard]] bool is_null() const;
template< typename Type >
[[nodiscard]] bool is_type_of() const {
return type() == utils::data_type_traits<Type>::type(attributes().size());
}
private:
std::string name_;
std::string table_name_;
attribute_options options_;
utils::basic_type type_{utils::basic_type::type_null};
std::shared_ptr<attribute_definition> reference_column_;
};
/**
* User defined literal to have a shortcut creating a column object
* @param name Name of the column
* @param type
* @param attr Length of the column name
* @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_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_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_type null_opt);
template < typename Type >
attribute_definition make_pk_column(const std::string &name, size_t size = 0)
{
return make_column<Type>(name, { size, utils::constraints::PRIMARY_KEY });
}
template <>
attribute_definition make_pk_column<std::string>(const std::string &name, size_t size);
template < typename Type >
attribute_definition make_fk_column(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column) {
return {name, utils::data_type_traits<Type>::type(size), ref_column, { size, utils::constraints::FOREIGN_KEY }};
}
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_type::NOT_NULL};
}
template <>
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column);
template < typename Type >
[[maybe_unused]] attribute_definition 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_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_type::NOT_NULL
};
}
template < typename Type >
[[maybe_unused]] attribute_definition 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_definition>(ref_column_name, utils::data_type_traits<Type>::type(0), ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}),
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
};
}
template <>
[[maybe_unused]] attribute_definition 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_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);
}
#endif //QUERY_COLUMN_DEFINITION_HPP
@@ -1,7 +1,7 @@
#ifndef QUERY_COLUMN_DEFINITION_GENERATOR_HPP
#define QUERY_COLUMN_DEFINITION_GENERATOR_HPP
#include "matador/object/attribute_definition.hpp"
#include "matador/object/attribute.hpp"
#include "matador/utils/access.hpp"
#include "matador/utils/data_type_traits.hpp"
@@ -24,9 +24,9 @@ public:
fk_attribute_generator() = default;
template<class Type>
attribute_definition generate(const char *id, Type &x, const std::shared_ptr<attribute_definition> &ref_column) {
attribute generate(const char *id, Type &x, const std::shared_ptr<attribute> &ref_column) {
access::process(*this, x);
return attribute_definition{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
return attribute{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
}
template<typename ValueType>
@@ -54,15 +54,15 @@ private:
class attribute_definition_generator final {
private:
attribute_definition_generator(std::vector<attribute_definition> &columns, const repository &repo);
attribute_definition_generator(std::vector<attribute> &columns, const repository &repo);
public:
~attribute_definition_generator() = default;
template < class Type >
static std::vector<attribute_definition> generate(const repository &repo)
static std::vector<attribute> generate(const repository &repo)
{
std::vector<attribute_definition> columns;
std::vector<attribute> columns;
attribute_definition_generator gen(columns, repo);
Type obj;
access::process(gen, obj);
@@ -70,8 +70,8 @@ public:
}
template < class Type >
static std::vector<attribute_definition> generate(const Type& obj, const repository &repo) {
std::vector<attribute_definition> columns;
static std::vector<attribute> generate(const Type& obj, const repository &repo) {
std::vector<attribute> columns;
attribute_definition_generator gen(columns, repo);
access::process(gen, obj);
return columns;
@@ -98,12 +98,12 @@ public:
template <class Pointer>
void on_foreign_key(const char *id, Pointer &x) {
std::shared_ptr<attribute_definition> ref_column;
std::shared_ptr<attribute> ref_column;
std::type_index ti = typeid(typename Pointer::value_type);
if (const auto result = determine_foreign_ref(std::type_index(ti))) {
ref_column = *result;
} else {
ref_column = std::make_shared<attribute_definition>();
ref_column = std::make_shared<attribute>();
insert_missing_reference_column(ti, ref_column);
}
if (x.empty()) {
@@ -121,12 +121,12 @@ 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_definition>, 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_definition>& ref_column) 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;
std::vector<attribute_definition> &columns_;
std::vector<attribute> &columns_;
const repository &repo_;
fk_attribute_generator fk_column_generator_;
+7 -7
View File
@@ -1,7 +1,7 @@
#ifndef BASIC_PROTOTYPE_INFO_HPP
#define BASIC_PROTOTYPE_INFO_HPP
#include "matador/object/attribute_definition.hpp"
#include "matador/object/attribute.hpp"
#include "matador/object/relation_endpoint.hpp"
#include "matador/utils/identifier.hpp"
@@ -24,8 +24,8 @@ public:
[[nodiscard]] std::type_index type_index() const;
[[nodiscard]] std::string name() const;
[[nodiscard]] const std::vector<attribute_definition>& attributes() const;
[[nodiscard]] std::shared_ptr<attribute_definition> reference_column() const;
[[nodiscard]] const std::vector<attribute>& attributes() const;
[[nodiscard]] std::shared_ptr<attribute> reference_column() const;
[[nodiscard]] bool has_primary_key() const;
[[nodiscard]] const utils::identifier& primary_key() const;
@@ -49,14 +49,14 @@ public:
[[nodiscard]] bool endpoints_empty() const;
protected:
basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute_definition> &attributes, utils::identifier &&pk, const std::shared_ptr<attribute_definition> &pk_as_fk_column);
basic_object_info(std::shared_ptr<repository_node> node, const std::vector<attribute_definition> &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);
protected:
std::shared_ptr<repository_node> node_; /**< prototype node of the represented object type */
std::vector<attribute_definition> attributes_;
std::vector<attribute> attributes_;
std::optional<utils::identifier> identifier_;
std::shared_ptr<attribute_definition> pk_as_fk_column_;
std::shared_ptr<attribute> pk_as_fk_column_;
t_endpoint_map relation_endpoints_;
};
+3 -3
View File
@@ -12,15 +12,15 @@ public:
using create_func = std::function<std::unique_ptr<Type>()>;
object_info(const std::shared_ptr<repository_node>& node,
const std::vector<attribute_definition> &attributes,
const std::vector<attribute> &attributes,
utils::identifier &&pk,
const std::shared_ptr<attribute_definition> &ref_column,
const std::shared_ptr<attribute> &ref_column,
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_definition> &attributes,
const std::vector<attribute> &attributes,
create_func&& creator)
: basic_object_info(node, attributes)
, creator_(std::move(creator)){
+2 -2
View File
@@ -150,7 +150,7 @@ public:
return utils::ok(basic_object_info_ref{result.value()->info()});
}
[[nodiscard]] utils::result<std::shared_ptr<attribute_definition>, utils::error> reference_column(
[[nodiscard]] utils::result<std::shared_ptr<attribute>, utils::error> reference_column(
const std::type_index &type_index) const;
void dump(std::ostream &os) const;
@@ -187,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_definition> > missing_references_;
std::unordered_map<std::type_index, std::shared_ptr<attribute> > missing_references_;
};
}
+1 -1
View File
@@ -90,7 +90,7 @@ 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_definition> determine_reference_column(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);