added more tests

This commit is contained in:
Sascha Kühl
2025-02-06 16:17:47 +01:00
parent c76aa56440
commit 0b70f5d4ee
68 changed files with 2117 additions and 263 deletions
-174
View File
@@ -1,174 +0,0 @@
#ifndef QUERY_COLUMN_DEFINITION_HPP
#define QUERY_COLUMN_DEFINITION_HPP
#include "matador/utils/basic_type_converter.hpp"
#include "matador/utils/basic_types.hpp"
#include "matador/utils/default_type_traits.hpp"
#include "matador/utils/field_attributes.hpp"
#include "matador/utils/value.hpp"
#include <vector>
namespace matador::sql {
enum class null_option : uint8_t {
NULLABLE, NOT_NULL
};
class column_definition {
public:
explicit column_definition(const char *name); // NOLINT(*-explicit-constructor)
explicit column_definition(std::string name); // NOLINT(*-explicit-constructor)
column_definition(const column_definition&) = default;
column_definition& operator=(const column_definition&) = default;
column_definition(column_definition&&) noexcept = default;
column_definition& operator=(column_definition&&) noexcept = default;
template<typename Type>
explicit column_definition(std::string name, const utils::field_attributes& attr)
: column_definition(std::move(name), utils::data_type_traits<Type>::type(attr.size()), attr)
{}
template<typename Type>
column_definition(std::string name, const Type &, const utils::field_attributes& attr, null_option null_opt)
: column_definition(std::move(name), utils::data_type_traits<Type>::type(attr.size()), attr, null_opt)
{}
template<size_t SIZE>
column_definition(std::string name, const char (&)[SIZE], const utils::field_attributes& attr, const null_option null_opt)
: column_definition(std::move(name), utils::data_type_traits<const char*>::type(attr.size()), attr, null_opt)
{}
column_definition(std::string name, utils::basic_type type, const utils::field_attributes&, null_option null_opt, size_t index = 0);
template<typename Type>
column_definition(std::string name, std::string ref_table, std::string ref_column, const utils::field_attributes& attr, null_option null_opt)
: column_definition(std::move(name), utils::data_type_traits<Type>::type(attr.size()), ref_table, ref_column, attr, null_opt)
{}
column_definition(std::string name, utils::basic_type type, size_t index, std::string ref_table, std::string ref_column, const utils::field_attributes& attr, null_option null_opt);
[[nodiscard]] const std::string& name() const;
[[nodiscard]] std::string full_name() const;
[[nodiscard]] std::string table_name() const;
[[nodiscard]] int index() const;
[[nodiscard]] const utils::field_attributes& attributes() const;
[[nodiscard]] bool is_nullable() const;
[[nodiscard]] utils::basic_type type() const;
[[nodiscard]] const std::string& ref_table() const;
[[nodiscard]] const std::string& ref_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;
void type(utils::basic_type type);
template< typename Type >
[[nodiscard]] bool is_type_of() const {
return std::holds_alternative<Type>(value_);
}
[[nodiscard]] std::string str() const;
template<typename Type>
void set(const Type &value, const utils::field_attributes &attr = utils::null_attributes)
{
attributes_ = attr;
value_ = value;
}
void set(const std::string &value, const utils::field_attributes &attr)
{
value_ = value;
attributes_ = attr;
}
void set(const char *value, const utils::field_attributes &attr)
{
value_ = std::string(value);
attributes_ = attr;
}
template<class Type>
std::optional<Type> as() const
{
return value_.as<Type>();
}
friend std::ostream& operator<<(std::ostream &out, const column_definition &col);
private:
template<class Operator>
void process(Operator &op)
{
op.on_attribute(name_.c_str(), value_, attributes_);
}
using data_type_index = std::vector<utils::basic_type>;
private:
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};
utils::value value_;
std::string ref_table_;
std::string ref_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 given name
*/
column_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);
template < typename Type >
column_definition make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes, null_option null_opt = null_option::NOT_NULL)
{
return make_column(name, utils::data_type_traits<Type>::type(0), attr, null_opt);
}
template <>
column_definition make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option null_opt);
template < typename Type >
column_definition make_pk_column(const std::string &name, size_t size = 0)
{
return make_column<Type>(name, { size, utils::constraints::PRIMARY_KEY });
}
template <>
column_definition make_pk_column<std::string>(const std::string &name, size_t size);
template < typename Type >
column_definition make_fk_column(const std::string &name, size_t size, const std::string &ref_table, const std::string &ref_column)
{
return {name, utils::data_type_traits<Type>::type(size), ref_table, ref_column, { size, utils::constraints::FOREIGN_KEY }};
}
template < typename Type >
[[maybe_unused]] column_definition make_fk_column(const std::string &name, const std::string &ref_table, const std::string &ref_column)
{
return {name, utils::data_type_traits<Type>::type(0), 0, ref_table, ref_column, { 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL};
}
template <>
column_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table, const std::string &ref_column);
}
#endif //QUERY_COLUMN_DEFINITION_HPP
+129
View File
@@ -0,0 +1,129 @@
#ifndef QUERY_COLUMN_GENERATOR_HPP
#define QUERY_COLUMN_GENERATOR_HPP
#include "matador/utils/access.hpp"
#include "matador/utils/field_attributes.hpp"
#include "matador/utils/foreign_attributes.hpp"
#include "matador/sql/column.hpp"
#include "matador/object/schema.hpp"
#include <string>
#include <vector>
#include <stack>
namespace matador::sql {
class column_generator
{
private:
column_generator(std::vector<column> &column_infos,
const object::schema &ts,
const std::string &table_name,
bool force_lazy);
public:
~column_generator() = default;
template < class Type >
static std::vector<column> generate(const object::schema &scm, bool force_lazy = false)
{
const auto info = scm.info<Type>();
if (!info) {
return {};
}
std::vector<column> columns;
column_generator gen(columns, scm, info.value().get().name(), force_lazy);
Type obj;
matador::access::process(gen, obj);
return std::move(columns);
}
template < class V >
void on_primary_key(const char *id, V &, std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>>* = nullptr)
{
push(id);
}
void on_primary_key(const char *id, std::string &, size_t);
void on_revision(const char *id, unsigned long long &/*rev*/);
template<typename Type>
void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes)
{
push(id);
}
template<class Pointer>
void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &attr)
{
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
push(id);
} else {
const auto info = table_schema_.info<typename Pointer::value_type>();
if (!info) {
return;
}
table_name_stack_.push(info.value().get().name());
typename Pointer::value_type obj;
matador::access::process(*this, obj);
table_name_stack_.pop();
}
}
template<class Pointer>
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
{
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
push(id);
} else {
const auto info = table_schema_.info<typename Pointer::value_type>();
if (!info) {
return;
}
table_name_stack_.push(info.value().get().name());
typename Pointer::value_type obj;
matador::access::process(*this, obj);
table_name_stack_.pop();
}
}
template<class ContainerType>
void on_has_many(ContainerType &, const char *, const utils::foreign_attributes &attr)
{
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
return;
}
const auto info = table_schema_.info<typename ContainerType::value_type::value_type>();
if (!info) {
return;
}
table_name_stack_.push(info.value().get().name());
typename ContainerType::value_type::value_type obj;
matador::access::process(*this, obj);
table_name_stack_.pop();
}
template<class ContainerType>
void on_has_many_to_many(const char *id, ContainerType &c, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &attr)
{
}
template<class ContainerType>
void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &attr)
{
}
private:
void push(const std::string &column_name);
private:
std::stack<std::string> table_name_stack_;
std::vector<column> &column_infos_;
const object::schema &table_schema_;
int column_index{0};
bool force_lazy_{false};
};
}
#endif //QUERY_COLUMN_GENERATOR_HPP
+2 -2
View File
@@ -2,7 +2,7 @@
#define QUERY_CONNECTION_HPP
#include "matador/sql/abstract_sql_logger.hpp"
#include "matador/sql/column_definition.hpp"
#include "matador/object/attribute_definition.hpp"
#include "matador/sql/connection_info.hpp"
#include "matador/sql/executor.hpp"
#include "matador/sql/statement.hpp"
@@ -126,7 +126,7 @@ public:
*/
[[nodiscard]] utils::result<void, utils::error> rollback() const;
[[nodiscard]] utils::result<std::vector<column_definition>, utils::error> describe(const std::string &table_name) const;
[[nodiscard]] utils::result<std::vector<object::attribute_definition>, utils::error> describe(const std::string &table_name) const;
[[nodiscard]] utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) const;
[[nodiscard]] utils::result<bool, utils::error> exists(const std::string &table_name) const;
@@ -3,7 +3,7 @@
#include <memory>
#include "matador/sql/column_definition.hpp"
#include "matador/object/attribute_definition.hpp"
#include "matador/sql/connection_info.hpp"
#include "matador/sql/query_context.hpp"
#include "matador/utils/error.hpp"
@@ -36,7 +36,7 @@ public:
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch(const query_context &context) = 0;
virtual utils::result<std::unique_ptr<statement_impl>, utils::error> prepare(const query_context &context) = 0;
virtual utils::result<std::vector<column_definition>, utils::error> describe(const std::string &table) = 0;
virtual utils::result<std::vector<object::attribute_definition>, utils::error> describe(const std::string &table) = 0;
virtual utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) = 0;
[[nodiscard]] const class dialect &dialect() const;
@@ -8,7 +8,7 @@
#include "matador/sql/interface/query_result_reader.hpp"
#include "matador/sql/column_definition.hpp"
#include "matador/object/attribute_definition.hpp"
#include <memory>
#include <string>
@@ -61,8 +61,8 @@ private:
class query_result_impl
{
public:
query_result_impl(std::unique_ptr<query_result_reader> &&reader, std::vector<column_definition> &&prototype, size_t column_index = 0);
query_result_impl(std::unique_ptr<query_result_reader> &&reader, const std::vector<column_definition> &prototype, size_t column_index = 0);
query_result_impl(std::unique_ptr<query_result_reader> &&reader, std::vector<object::attribute_definition> &&prototype, size_t column_index = 0);
query_result_impl(std::unique_ptr<query_result_reader> &&reader, const std::vector<object::attribute_definition> &prototype, size_t column_index = 0);
template<typename ValueType>
void on_primary_key(const char *id, ValueType &value, std::enable_if_t<std::is_integral_v<ValueType> && !std::is_same_v<bool, ValueType>>* = nullptr)
@@ -149,11 +149,11 @@ public:
return true;
}
[[nodiscard]] const std::vector<column_definition>& prototype() const;
[[nodiscard]] const std::vector<object::attribute_definition>& prototype() const;
protected:
size_t column_index_ = 0;
std::vector<column_definition> prototype_;
std::vector<object::attribute_definition> prototype_;
std::unique_ptr<query_result_reader> reader_;
detail::pk_reader pk_reader_;
};
@@ -161,7 +161,7 @@ protected:
namespace detail {
template<typename ValueType>
void detail::pk_reader::on_primary_key(const char *id, ValueType &value, std::enable_if_t<std::is_integral_v<ValueType> && !std::is_same_v<bool, ValueType>> *)
void pk_reader::on_primary_key(const char *id, ValueType &value, std::enable_if_t<std::is_integral_v<ValueType> && !std::is_same_v<bool, ValueType>> *)
{
utils::data_type_traits<ValueType>::read_value(reader_, id, column_index_++, value);
}
+2 -2
View File
@@ -1,7 +1,7 @@
#ifndef QUERY_QUERY_DATA_HPP
#define QUERY_QUERY_DATA_HPP
#include "matador/sql/column_definition.hpp"
#include "matador/object/attribute_definition.hpp"
#include "matador/sql/table.hpp"
#include "matador/utils/types.hpp"
@@ -25,7 +25,7 @@ struct query_context
sql_command command{};
std::string command_name;
sql::table table{""};
std::vector<column_definition> prototype;
std::vector<object::attribute_definition> prototype;
std::vector<std::string> result_vars;
std::vector<std::string> bind_vars;
std::vector<utils::database_type> bind_types;
+3 -3
View File
@@ -1,7 +1,7 @@
#ifndef QUERY_QUERY_RESULT_HPP
#define QUERY_QUERY_RESULT_HPP
#include "matador/sql/column_definition.hpp"
#include "matador/object/attribute_definition.hpp"
#include "matador/sql/internal/query_result_impl.hpp"
@@ -111,13 +111,13 @@ private:
namespace detail {
template < typename Type >
Type* create_prototype(const std::vector<column_definition> &/*prototype*/)
Type* create_prototype(const std::vector<object::attribute_definition> &/*prototype*/)
{
return new Type{};
}
template <>
record* create_prototype<record>(const std::vector<column_definition> &prototype);
record* create_prototype<record>(const std::vector<object::attribute_definition> &prototype);
}
+1 -2
View File
@@ -10,8 +10,7 @@ namespace matador::sql {
struct column;
struct table
{
struct table {
table() = default;
table(const char *name); // NOLINT(*-explicit-constructor)
table(std::string name); // NOLINT(*-explicit-constructor)