added column generator and tests
This commit is contained in:
@@ -17,7 +17,11 @@ public:
|
||||
|
||||
template<typename Type>
|
||||
explicit column(std::string name, utils::field_attributes attr = utils::null_attributes)
|
||||
: column(std::move(name), data_type_traits<Type>::data_type(), attr)
|
||||
: column(std::move(name), data_type_traits<Type>::builtin_type(attr.size()), attr)
|
||||
{}
|
||||
template<typename Type>
|
||||
column(std::string name, const Type &, utils::field_attributes attr = utils::null_attributes)
|
||||
: column(std::move(name), data_type_traits<Type>::builtin_type(attr.size()), attr)
|
||||
{}
|
||||
column(std::string name, data_type_t type, utils::field_attributes attr = utils::null_attributes);
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#ifndef QUERY_COLUMN_GENERATOR_HPP
|
||||
#define QUERY_COLUMN_GENERATOR_HPP
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/types.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace matador::utils {
|
||||
class identifiable;
|
||||
enum class cascade_type;
|
||||
}
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class column_generator
|
||||
{
|
||||
private:
|
||||
explicit column_generator(std::vector<column> &columns);
|
||||
|
||||
public:
|
||||
~column_generator() = default;
|
||||
|
||||
template < class Type >
|
||||
static std::vector<column> generate()
|
||||
{
|
||||
std::vector<column> columns;
|
||||
column_generator gen(columns);
|
||||
Type obj;
|
||||
matador::utils::access::process(gen, obj);
|
||||
return std::move(columns);
|
||||
}
|
||||
|
||||
template < class V >
|
||||
void on_primary_key(const char *, V &x, typename std::enable_if<std::is_integral<V>::value && !std::is_same<bool, V>::value>::type* = 0);
|
||||
void on_primary_key(const char *id, std::string &pk, size_t size);
|
||||
void on_revision(const char *id, unsigned long long &/*rev*/);
|
||||
|
||||
template<typename Type>
|
||||
void on_attribute(const char *id, Type &x, const utils::field_attributes &attr = utils::null_attributes);
|
||||
|
||||
void on_belongs_to(const char *id, utils::identifiable &x, utils::cascade_type);
|
||||
void on_has_one(const char *id, utils::identifiable &x, utils::cascade_type);
|
||||
// void on_has_many(const char *, abstract_container &, const char *, const char *, cascade_type) {}
|
||||
// void on_has_many(const char *, abstract_container &, cascade_type) {}
|
||||
|
||||
private:
|
||||
size_t index_ = 0;
|
||||
std::vector<column> &columns_;
|
||||
|
||||
// typed_column_identifier_serializer column_identifier_serializer_;
|
||||
};
|
||||
|
||||
template<typename V>
|
||||
void column_generator::on_primary_key(const char *id, V &x, typename std::enable_if<std::is_integral<V>::value && !std::is_same<bool, V>::value>::type*)
|
||||
{
|
||||
on_attribute(id, x, { utils::constraints::PRIMARY_KEY });
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void column_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr)
|
||||
{
|
||||
columns_.emplace_back(id, x, attr);
|
||||
}
|
||||
|
||||
}
|
||||
#endif //QUERY_COLUMN_GENERATOR_HPP
|
||||
@@ -100,6 +100,7 @@ public:
|
||||
query_builder& remove();
|
||||
|
||||
query_builder& table(const std::string &table, std::initializer_list<column> columns);
|
||||
query_builder& table(const std::string &table, const std::vector<column> &columns);
|
||||
query_builder& table(const std::string &table);
|
||||
query_builder& into(const std::string &table, std::initializer_list<std::string> column_names);
|
||||
query_builder& values(std::initializer_list<any_type> values);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define QUERY_QUERY_INTERMEDIATES_HPP
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
#include "matador/sql/key_value_pair.hpp"
|
||||
#include "matador/sql/query_result.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
@@ -136,6 +137,11 @@ public:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
query_execute_finish table(const std::string &table, std::initializer_list<column> columns);
|
||||
template<class Type>
|
||||
query_execute_finish table(const std::string &table)
|
||||
{
|
||||
return {db(), query().table(table, column_generator::generate<Type>())};
|
||||
}
|
||||
};
|
||||
|
||||
class query_drop_intermediate : query_intermediate
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user