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
+2 -2
View File
@@ -13,7 +13,7 @@ add_library(matador-core STATIC
../../include/matador/net/reactor.hpp
../../include/matador/net/select_fd_sets.hpp
../../include/matador/net/socket_interrupter.hpp
../../include/matador/object/attribute_definition.hpp
../../include/matador/object/attribute.hpp
../../include/matador/object/attribute_definition_generator.hpp
../../include/matador/object/basic_object_info.hpp
../../include/matador/object/error_code.hpp
@@ -74,7 +74,7 @@ add_library(matador-core STATIC
logger/log_manager.cpp
logger/logger.cpp
logger/rotating_file_sink.cpp
object/attribute_definition.cpp
object/attribute.cpp
object/attribute_definition_generator.cpp
object/basic_object_info.cpp
object/error_code.cpp
+178
View File
@@ -0,0 +1,178 @@
#include "matador/object/attribute.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, "", {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 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))
, table_name_(std::move(table_name))
, options_( options )
, type_( type )
, reference_column_( ref_column ) {
}
const std::string &attribute::name() const {
return name_;
}
void attribute::name( const std::string& n ) {
name_ = n;
}
std::string attribute::full_name() const {
return !table_name_.empty() ? table_name_ + "." + name_ : name_;
}
int attribute::index() const {
return options_.index;
}
const utils::field_attributes &attribute::attributes() const {
return options_.attributes;
}
utils::field_attributes& attribute::attributes() {
return options_.attributes;
}
bool attribute::is_nullable() const {
return options_.null_option == null_option_type::NULLABLE;
}
utils::basic_type attribute::type() const {
return type_;
}
const std::string& attribute::table_name() const {
return table_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;
}
bool attribute::is_floating_point() const {
return type_ == utils::basic_type::type_float || type_ == utils::basic_type::type_double;
}
bool attribute::is_bool() const {
return type_ == utils::basic_type::type_bool;
}
bool attribute::is_string() const {
return type_ == utils::basic_type::type_text;
}
bool attribute::is_varchar() const {
return type_ == utils::basic_type::type_varchar;
}
bool attribute::is_date() const {
return type_ == utils::basic_type::type_date;
}
bool attribute::is_time() const {
return type_ == utils::basic_type::type_time;
}
bool attribute::is_blob() const {
return type_ == utils::basic_type::type_blob;
}
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::PRIMARY_KEY});
}
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::FOREIGN_KEY}, 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::FOREIGN_KEY}),
{ 0, utils::constraints::FOREIGN_KEY }, 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::FOREIGN_KEY});
return {
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
{size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL
};
}
}
-178
View File
@@ -1,178 +0,0 @@
#include "matador/object/attribute_definition.hpp"
#include <ostream>
#include <utility>
namespace matador::object {
attribute_definition::attribute_definition(const char *name)
: attribute_definition(name, utils::basic_type::type_null, "", {utils::null_attributes}) {
}
attribute_definition::attribute_definition(std::string name)
: 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_type null_opt,
const int index)
: 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_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,
std::string table_name,
const attribute_options& options,
const std::shared_ptr<attribute_definition>& ref_column)
: name_(std::move(name))
, table_name_(std::move(table_name))
, options_( options )
, type_( type )
, reference_column_( ref_column ) {
}
const std::string &attribute_definition::name() const {
return name_;
}
void attribute_definition::name( const std::string& n ) {
name_ = n;
}
std::string attribute_definition::full_name() const {
return !table_name_.empty() ? table_name_ + "." + name_ : name_;
}
int attribute_definition::index() const {
return options_.index;
}
const utils::field_attributes &attribute_definition::attributes() const {
return options_.attributes;
}
utils::field_attributes& attribute_definition::attributes() {
return options_.attributes;
}
bool attribute_definition::is_nullable() const {
return options_.null_option == null_option_type::NULLABLE;
}
utils::basic_type attribute_definition::type() const {
return type_;
}
const std::string& attribute_definition::table_name() const {
return table_name_;
}
void attribute_definition::table_name( const std::string& name ) {
table_name_ = name;
}
void attribute_definition::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
options_.attributes = attr;
type_ = type;
}
std::shared_ptr<attribute_definition> attribute_definition::reference_column() const {
return reference_column_;
}
bool attribute_definition::is_foreign_reference() const {
return reference_column_ != nullptr;
}
bool attribute_definition::is_integer() const {
return type_ >= utils::basic_type::type_int8 && type_ <= utils::basic_type::type_uint64;
}
bool attribute_definition::is_floating_point() const {
return type_ == utils::basic_type::type_float || type_ == utils::basic_type::type_double;
}
bool attribute_definition::is_bool() const {
return type_ == utils::basic_type::type_bool;
}
bool attribute_definition::is_string() const {
return type_ == utils::basic_type::type_text;
}
bool attribute_definition::is_varchar() const {
return type_ == utils::basic_type::type_varchar;
}
bool attribute_definition::is_date() const {
return type_ == utils::basic_type::type_date;
}
bool attribute_definition::is_time() const {
return type_ == utils::basic_type::type_time;
}
bool attribute_definition::is_blob() const {
return type_ == utils::basic_type::type_blob;
}
bool attribute_definition::is_null() const {
return type_ == utils::basic_type::type_null;
}
attribute_definition 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_definition 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_definition make_pk_column<std::string>(const std::string &name, size_t size) {
return make_column<std::string>(name, {size, utils::constraints::PRIMARY_KEY});
}
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_type::NOT_NULL
};
}
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, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}),
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
};
}
template<>
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) {
const auto ref_column = std::make_shared<attribute_definition>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY});
return {
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
{size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL
};
}
}
@@ -3,7 +3,7 @@
namespace matador::object {
attribute_definition_generator::attribute_definition_generator(std::vector<attribute_definition> &columns, const repository &repo)
attribute_definition_generator::attribute_definition_generator(std::vector<attribute> &columns, const repository &repo)
: columns_(columns)
, repo_(repo)
{}
@@ -12,11 +12,11 @@ void attribute_definition_generator::on_revision(const char *id, uint64_t &rev)
on_attribute(id, rev);
}
utils::result<std::shared_ptr<attribute_definition>, utils::error> attribute_definition_generator::determine_foreign_ref(const std::type_index &ti) const {
utils::result<std::shared_ptr<attribute>, utils::error> attribute_definition_generator::determine_foreign_ref(const std::type_index &ti) const {
return repo_.reference_column(ti);
}
void attribute_definition_generator::insert_missing_reference_column(const std::type_index& ti, const 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>& ref_column) const {
const_cast<repository&>(repo_).missing_references_.insert({ti, ref_column});
}
+5 -5
View File
@@ -6,9 +6,9 @@
namespace matador::object {
basic_object_info::basic_object_info(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> &pk_as_fk_column)
const std::shared_ptr<attribute> &pk_as_fk_column)
: node_(std::move(node))
, attributes_(attributes)
, identifier_(std::move(pk))
@@ -16,7 +16,7 @@ basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
}
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
const std::vector<attribute_definition> &attributes)
const std::vector<attribute> &attributes)
: node_(std::move(node))
, attributes_(attributes) {}
@@ -28,11 +28,11 @@ std::string basic_object_info::name() const {
return node_->name();
}
const std::vector<attribute_definition>& basic_object_info::attributes() const {
const std::vector<attribute>& basic_object_info::attributes() const {
return attributes_;
}
std::shared_ptr<attribute_definition> basic_object_info::reference_column() const {
std::shared_ptr<attribute> basic_object_info::reference_column() const {
return pk_as_fk_column_;
}
+1 -1
View File
@@ -62,7 +62,7 @@ utils::result<basic_object_info_ref, utils::error> repository::basic_info( const
return utils::ok(basic_object_info_ref{result.value()->info()});
}
utils::result<std::shared_ptr<attribute_definition>, utils::error> repository::reference_column(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::ok((*result)->info().reference_column());
+2 -2
View File
@@ -104,13 +104,13 @@ utils::result<repository_node::node_ptr, utils::error> repository_node::make_and
return repo.attach_node(node, "");
}
std::shared_ptr<attribute_definition> repository_node::determine_reference_column(const std::type_index& ti,
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 std::make_shared<attribute_definition>(pk_info.pk_column_name, pk_info.type, table_name, attribute_options{utils::constraints::FOREIGN_KEY});
return std::make_shared<attribute>(pk_info.pk_column_name, pk_info.type, table_name, attribute_options{utils::constraints::FOREIGN_KEY});
}
auto ref_column = it->second;
+1 -1
View File
@@ -127,7 +127,7 @@ matador::utils::result<void, matador::utils::error> matador::orm::schema::drop_t
return utils::ok<void>();
}
matador::utils::result<std::vector<matador::object::attribute_definition>, matador::utils::error> matador::orm::schema::describe_table(const std::string& table_name) const {
matador::utils::result<std::vector<matador::object::attribute>, matador::utils::error> matador::orm::schema::describe_table(const std::string& table_name) const {
const auto c = pool_.acquire();
if (!c.valid()) {
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
+2 -2
View File
@@ -103,7 +103,7 @@ utils::result<sql::query_result<sql::record>, utils::error> session::fetch_all(c
if (const auto rit = std::find_if(it->second.begin(), it->second.end(), [&col](const auto &value) {
return value.name() == col.name();
}); rit != it->second.end()) {
const_cast<object::attribute_definition &>(col).change_type(rit->type());
const_cast<object::attribute &>(col).change_type(rit->type());
}
}
auto res = fetch(q);
@@ -120,7 +120,7 @@ utils::result<size_t, utils::error> session::execute(const std::string &sql) con
return execute(sql::query_context{sql});
}
std::vector<object::attribute_definition> session::describe_table(const std::string &table_name) const {
std::vector<object::attribute> session::describe_table(const std::string &table_name) const {
const auto c = cache_.pool().acquire();
if (!c.valid()) {
throw std::logic_error("no database connection available");
@@ -7,7 +7,7 @@
namespace matador::query {
namespace detail {
sql::record *create_prototype(const std::vector<object::attribute_definition> &prototype) {
sql::record *create_prototype(const std::vector<object::attribute> &prototype) {
auto result = std::make_unique<sql::record>();
for (const auto &col: prototype) {
result->append({
@@ -8,11 +8,11 @@ query_create_intermediate::query_create_intermediate() {
context_->parts.push_back(std::make_unique<internal::query_create_part>());
}
executable_query query_create_intermediate::table(const class table &tab, const std::initializer_list<object::attribute_definition> columns) {
return this->table(tab, std::vector<object::attribute_definition>{columns});
executable_query query_create_intermediate::table(const class table &tab, const std::initializer_list<object::attribute> columns) {
return this->table(tab, std::vector<object::attribute>{columns});
}
executable_query query_create_intermediate::table(const class table &tab, const std::vector<object::attribute_definition> &columns) {
executable_query query_create_intermediate::table(const class table &tab, const std::vector<object::attribute> &columns) {
context_->parts.push_back(std::make_unique<internal::query_create_table_part>(tab, columns));
return {context_};
}
+2 -2
View File
@@ -344,7 +344,7 @@ void query_create_part::accept(query_part_visitor &visitor)
visitor.visit(*this);
}
query_create_table_part::query_create_table_part(class table table, std::vector<object::attribute_definition> columns)
query_create_table_part::query_create_table_part(class table table, std::vector<object::attribute> columns)
: query_part(sql::dialect_token::Table)
, table_(std::move(table))
, columns_(std::move(columns)) {}
@@ -354,7 +354,7 @@ const table &query_create_table_part::table() const
return table_;
}
const std::vector<object::attribute_definition> &query_create_table_part::columns() const
const std::vector<object::attribute> &query_create_table_part::columns() const
{
return columns_;
}
@@ -7,14 +7,14 @@ namespace matador::sql {
detail::pk_reader::pk_reader(query_result_reader &reader)
: reader_(reader) {}
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, std::vector<object::attribute_definition> &&prototype, const size_t column_index)
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, std::vector<object::attribute> &&prototype, const size_t column_index)
: column_index_(column_index)
, prototype_(std::move(prototype))
, reader_(std::move(reader))
, pk_reader_(*reader_)
{}
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, const std::vector<object::attribute_definition> &prototype, const size_t column_index)
query_result_impl::query_result_impl(std::unique_ptr<query_result_reader> &&reader, const std::vector<object::attribute> &prototype, const size_t column_index)
: column_index_(column_index)
, prototype_(prototype)
, reader_(std::move(reader))
@@ -43,7 +43,7 @@ query_result_impl::on_attribute(const char *id, utils::value &val, const utils::
reader_->read_value(id, column_index_++, val, attr.size());
}
const std::vector<object::attribute_definition>& query_result_impl::prototype() const
const std::vector<object::attribute>& query_result_impl::prototype() const
{
return prototype_;
}
+3 -3
View File
@@ -291,7 +291,7 @@ void query_compiler::visit(internal::query_create_part &/*create_part*/)
struct fk_context {
std::string column;
std::shared_ptr<object::attribute_definition> reference_column;
std::shared_ptr<object::attribute> reference_column;
};
struct column_context
@@ -300,7 +300,7 @@ struct column_context
std::vector<fk_context> foreign_contexts;
};
std::string build_create_column(const object::attribute_definition &col, const sql::dialect &d, column_context &context);
std::string build_create_column(const object::attribute &col, const sql::dialect &d, column_context &context);
void query_compiler::visit(internal::query_create_table_part &part)
{
@@ -387,7 +387,7 @@ void query_compiler::visit(internal::query_drop_table_part &part)
query_.sql += " " + build_table_name(part.token(), *dialect_, query_.table_name);
}
std::string build_create_column(const object::attribute_definition &col, const sql::dialect &d, column_context &context)
std::string build_create_column(const object::attribute &col, const sql::dialect &d, column_context &context)
{
std::string result = d.prepare_identifier_string(col.name()) + " " + d.data_type_at(col.type());
if (col.attributes().size() > 0) {
+3 -3
View File
@@ -137,7 +137,7 @@ utils::result<void, utils::error> connection::rollback() const {
return utils::ok<void>();
}
utils::result<std::vector<object::attribute_definition>, utils::error> connection::describe(const std::string &table_name) const {
utils::result<std::vector<object::attribute>, utils::error> connection::describe(const std::string &table_name) const {
return connection_->describe(table_name);
}
@@ -154,7 +154,7 @@ utils::result<size_t, utils::error> connection::execute(const std::string &sql)
return connection_->execute(sql);
}
bool has_unknown_columns(const std::vector<object::attribute_definition> &columns) {
bool has_unknown_columns(const std::vector<object::attribute> &columns) {
return std::any_of(std::begin(columns), std::end(columns), [](const auto &col) {
return col.type() == utils::basic_type::type_null;
});
@@ -229,7 +229,7 @@ utils::result<std::unique_ptr<statement_impl>, utils::error> connection::perform
[&col](const auto &value) { return value.name() == col.name(); }
);
if (col.type() == utils::basic_type::type_null && rit != result->end()) {
const_cast<object::attribute_definition&>(col).change_type(rit->type());
const_cast<object::attribute&>(col).change_type(rit->type());
}
}
}
+1 -1
View File
@@ -6,7 +6,7 @@
namespace matador::sql::detail {
template<>
record *create_prototype<record>(const std::vector<object::attribute_definition> &prototype) {
record *create_prototype<record>(const std::vector<object::attribute> &prototype) {
auto result = std::make_unique<record>();
for (const auto &col: prototype) {
result->append({