added column generator and tests

This commit is contained in:
2023-11-11 08:29:22 +01:00
parent ae236746ad
commit 30f2126225
20 changed files with 917 additions and 19 deletions
+75
View File
@@ -0,0 +1,75 @@
#ifndef QUERY_ACCESS_HPP
#define QUERY_ACCESS_HPP
#include <string>
namespace matador::utils {
enum class cascade_type;
template < class Type, template < class ... > class ContainerType >
class container;
class field_attributes;
namespace access {
template<class Operator, class Type>
void process(Operator &op, Type &object) {
object.process(op);
}
template<class Operator, class Type>
void process(Operator &op, const Type &object) {
const_cast<Type &>(object).process(op);
}
template< class Operator, class Type >
void primary_key(Operator &op, const char *id, Type &value) {
op.on_primary_key(id, value);
}
template< class Operator >
void primary_key(Operator &op, const char *id, std::string &value, size_t size ) {
op.on_primary_key(id, value, size);
}
template<class Operator>
void revision(Operator &op, const char *id, unsigned long long &value) {
op.on_revision(id, value);
}
template<class Operator, class Type>
void attribute(Operator &op, const char *id, Type &value, const field_attributes &attr) {
op.on_attribute(id, value, attr);
}
template<class Operator, class Type>
void attribute(Operator &op, const char *id, Type &value) {
op.on_attribute(id, value);
}
template<class Operator, class Type>
void has_one(Operator &op, const char *id, Type &value, cascade_type cascade) {
op.on_has_one(id, value, cascade);
}
template<class Operator, class Type>
void belongs_to(Operator &op, const char *id, Type &value, cascade_type cascade) {
op.on_belongs_to(id, value, cascade);
}
template<class Operator, class Type, template<class ...> class ContainerType>
void has_many(Operator &op, const char *id, container<Type, ContainerType> &container, cascade_type cascade) {
op.on_has_many(id, container, cascade);
}
template<class Operator, class Type, template<class ...> class ContainerType>
void has_many(Operator &op, const char *id, container<Type, ContainerType> &container, const char *left_column, const char *right_column, cascade_type cascade) {
op.on_has_many(id, container, left_column, right_column, cascade);
}
}
}
#endif //QUERY_ACCESS_HPP
+30
View File
@@ -0,0 +1,30 @@
#ifndef QUERY_CASCADE_TYPE_HPP
#define QUERY_CASCADE_TYPE_HPP
#include <cstdint>
namespace matador::utils {
/**
* @brief Cascade types for database actions
*/
enum class cascade_type : uint8_t
{
NONE = 0, /**< Cascade type none */
REMOVE = 1, /**< Cascade type remove */
UPDATE = 2, /**< Cascade type update */
INSERT = 4, /**< Cascade type insert */
ALL = REMOVE | UPDATE | INSERT /**< Cascade type all */
};
inline cascade_type operator~ (cascade_type a) { return (cascade_type)~(int)a; }
inline cascade_type operator| (cascade_type a, cascade_type b) { return (cascade_type)((int)a | (int)b); }
inline cascade_type operator& (cascade_type a, cascade_type b) { return (cascade_type)((int)a & (int)b); }
inline cascade_type operator^ (cascade_type a, cascade_type b) { return (cascade_type)((int)a ^ (int)b); }
inline cascade_type& operator|= (cascade_type& a, cascade_type b) { return (cascade_type&)((int&)a |= (int)b); }
inline cascade_type& operator&= (cascade_type& a, cascade_type b) { return (cascade_type&)((int&)a &= (int)b); }
inline cascade_type& operator^= (cascade_type& a, cascade_type b) { return (cascade_type&)((int&)a ^= (int)b); }
}
#endif //QUERY_CASCADE_TYPE_HPP
+50
View File
@@ -0,0 +1,50 @@
#ifndef QUERY_IDENTIFIABLE_HPP
#define QUERY_IDENTIFIABLE_HPP
#include "matador/utils/identifier.hpp"
namespace matador::utils {
/**
* Base class for all pointer object
* which can contain an identifiable
*/
class identifiable
{
public:
virtual ~identifiable() = default;
/**
* Resets the object_holder with the given
* identifier. If the type of identifier differs
* from internal type an exception is thrown
*
* @param id The identifier to set
*/
virtual void reset(const identifier &id) = 0;
/**
* Returns true if serializable has a primary key
*
* @return true if serializable has a primary key
*/
[[nodiscard]] virtual bool has_primary_key() const = 0;
/**
* Gets the primary key of the foreign serializable
*
* @return The primary key of the foreign serializable
*/
[[nodiscard]] virtual const identifier& primary_key() const = 0;
virtual identifier& primary_key() = 0;
/**
* Creates a new identifier object.
*
* @return Returns a new identifier object.
*/
[[nodiscard]] virtual identifier create_identifier() const = 0;
};
}
#endif //QUERY_IDENTIFIABLE_HPP
+223
View File
@@ -0,0 +1,223 @@
#ifndef QUERY_IDENTIFIER_HPP
#define QUERY_IDENTIFIER_HPP
#include "matador/utils/field_attributes.hpp"
#include <memory>
#include <string>
#include <typeindex>
#include <type_traits>
namespace matador::utils {
struct null_type_t {};
namespace detail {
enum class identifier_type_t : unsigned int {
INTEGRAL_TYPE,
STRING_TYPE,
NULL_TYPE
};
template<typename Type, class Enabled = void>
struct identifier_type_traits;
template<typename Type>
struct identifier_type_traits<Type, typename std::enable_if<std::is_integral<Type>::value>::type> {
static identifier_type_t type() { return identifier_type_t::INTEGRAL_TYPE; }
static std::string type_string() { return "integral"; }
static bool is_valid(Type value) { return value > 0; }
static std::string to_string(Type value) { return std::to_string(value); }
};
template<typename Type>
struct identifier_type_traits<Type, typename std::enable_if<std::is_same<Type, std::string>::value>::type> {
static identifier_type_t type() { return identifier_type_t::STRING_TYPE; }
static std::string type_string() { return "string"; }
static bool is_valid(const Type &value) { return !value.empty(); }
static std::string to_string(Type value) { return value; }
};
template<typename Type>
struct identifier_type_traits<Type, typename std::enable_if<std::is_same<Type, null_type_t>::value>::type> {
static identifier_type_t type() { return identifier_type_t::NULL_TYPE; }
static std::string type_string() { return "null"; }
static bool is_valid() { return false; }
static std::string to_string() { return "null_pk"; }
};
}
class identifier_serializer
{
public:
virtual ~identifier_serializer() = default;
virtual void serialize(short &, const field_attributes &) = 0;
virtual void serialize(int &, const field_attributes &) = 0;
virtual void serialize(long &, const field_attributes &) = 0;
virtual void serialize(long long &, const field_attributes &) = 0;
virtual void serialize(unsigned short &, const field_attributes &) = 0;
virtual void serialize(unsigned int &, const field_attributes &) = 0;
virtual void serialize(unsigned long &, const field_attributes &) = 0;
virtual void serialize(unsigned long long &, const field_attributes &) = 0;
virtual void serialize(std::string &, const field_attributes &) = 0;
virtual void serialize(null_type_t &, const field_attributes &) = 0;
};
class identifier
{
private:
struct base
{
explicit base(const std::type_index &ti, detail::identifier_type_t id_type);
base(const base &x) = delete;
base &operator=(const base &x) = delete;
base(base &&x) = delete;
base &operator=(base &&x) = delete;
virtual ~base() = default;
template<typename Type>
bool is_similar_type() const
{
return identifier_type_ == detail::identifier_type_traits<Type>::type();
}
bool is_similar_type(const base &x) const;
detail::identifier_type_t type() const;
virtual base *copy() const = 0;
virtual bool equal_to(const base &x) const = 0;
virtual bool less(const base &x) const = 0;
virtual bool is_valid() const = 0;
virtual void serialize(identifier_serializer &s) = 0;
virtual std::string str() const = 0;
virtual size_t hash() const = 0;
std::type_index type_index_;
detail::identifier_type_t identifier_type_;
};
template<class IdType>
struct pk : public base
{
using self = pk<IdType>;
explicit pk(const IdType &id, size_t size = 0) : base(std::type_index(typeid(IdType)), detail::identifier_type_traits<IdType>::type())
, id_(id)
, size_(size) {}
base *copy() const final {
return new self(id_, size_);
}
bool equal_to(const base &x) const final {
return static_cast<const pk<IdType> &>(x).id_ == id_;
}
bool less(const base &x) const final {
return static_cast<const pk<IdType> &>(x).id_ < id_;
}
bool is_valid() const final
{
return detail::identifier_type_traits<IdType>::is_valid(id_);
}
std::string str() const final
{
return detail::identifier_type_traits<IdType>::to_string(id_);
}
void serialize(identifier_serializer &s) final {
s.serialize(id_, size_);
}
size_t hash() const final {
std::hash<IdType> hash_func;
return hash_func(id_);
}
IdType id_;
size_t size_{};
};
struct null_pk : public base
{
null_pk();
base *copy() const final;
bool equal_to(const base &x) const final;
bool less(const base &x) const final;
bool is_valid() const final;
void serialize(identifier_serializer &s) final;
std::string str() const final;
size_t hash() const final;
null_type_t null_;
};
public:
identifier();
template<typename Type>
explicit identifier(const Type &id, long size = -1)
: id_(std::make_shared<pk<Type>>(id, size)) {}
identifier(const identifier &x);
identifier &operator=(const identifier &x);
identifier(identifier &&x) noexcept ;
identifier &operator=(identifier &&x) noexcept;
template<typename Type>
identifier &operator=(const Type &value)
{
id_ = std::make_shared<pk<Type>>(value);
return *this;
}
~identifier() = default;
bool operator==(const identifier &x) const;
bool operator!=(const identifier &x) const;
bool operator<(const identifier &x) const;
bool operator<=(const identifier &x) const;
bool operator>(const identifier &x) const;
bool operator>=(const identifier &x) const;
bool is_similar_type(const identifier &x) const;
template<typename Type>
bool is_similar_type() const
{
return id_->is_similar_type<Type>();
}
std::string str() const;
const std::type_index &type_index() const;
identifier share() const;
size_t use_count() const;
bool is_null() const;
bool is_valid() const;
void clear();
void serialize(identifier_serializer &s);
size_t hash() const;
friend std::ostream &operator<<(std::ostream &out, const identifier &id);
private:
explicit identifier(const std::shared_ptr<base>& id);
private:
std::shared_ptr<base> id_;
};
static identifier null_identifier{};
struct id_pk_hash
{
size_t operator()(const identifier &id) const;
};
}
#endif //QUERY_IDENTIFIER_HPP