initial matador ng commit
This commit is contained in:
@@ -1,178 +1,177 @@
|
||||
#ifndef QUERY_IDENTIFIER_HPP
|
||||
#define QUERY_IDENTIFIER_HPP
|
||||
#ifndef MATADOR_IDENTIFIER_HPP
|
||||
#define MATADOR_IDENTIFIER_HPP
|
||||
|
||||
#include "matador/utils/default_type_traits.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
#include "matador/utils/basic_types.hpp"
|
||||
#include "matador/utils/types.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(int8_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(int16_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(int32_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(int64_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(uint8_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(uint16_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(uint32_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(uint64_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(const char*, const field_attributes &) = 0;
|
||||
virtual void serialize(std::string &, const field_attributes &) = 0;
|
||||
virtual void serialize(null_type_t &, const field_attributes &) = 0;
|
||||
};
|
||||
|
||||
template<typename Type, class Enabled = void>
|
||||
struct identifier_type_traits;
|
||||
|
||||
template<typename Type>
|
||||
struct identifier_type_traits<Type, std::enable_if_t<std::is_integral_v<Type>>> {
|
||||
static bool is_valid(Type value) { return value > 0; }
|
||||
static std::string to_string(const Type value) { return std::to_string(value); }
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
struct identifier_type_traits<Type, std::enable_if_t<std::is_same_v<Type, std::string>>> {
|
||||
static bool is_valid(const Type &value) { return !value.empty(); }
|
||||
static std::string to_string(const Type &value) { return value; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct identifier_type_traits<null_type_t, void> {
|
||||
static bool is_valid() { return false; }
|
||||
static std::string to_string() { return "null"; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct identifier_type_traits<const char*, void> {
|
||||
static bool is_valid(const char *value);
|
||||
static std::string to_string(const char *value);
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename Type>
|
||||
size_t hash(const Type &value) {
|
||||
return std::hash<Type>()(value);
|
||||
}
|
||||
|
||||
size_t hash(const char *value);
|
||||
|
||||
}
|
||||
|
||||
class identifier
|
||||
{
|
||||
private:
|
||||
struct base
|
||||
{
|
||||
explicit base(const std::type_index &ti, detail::identifier_type_t id_type);
|
||||
base(const std::type_index &ti, basic_type 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;
|
||||
[[nodiscard]] virtual base *copy() const = 0;
|
||||
[[nodiscard]] virtual bool equal_to(const base &x) const = 0;
|
||||
[[nodiscard]] virtual bool less(const base &x) const = 0;
|
||||
[[nodiscard]] 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;
|
||||
[[nodiscard]] virtual std::string str() const = 0;
|
||||
[[nodiscard]] virtual size_t hash() const = 0;
|
||||
|
||||
std::type_index type_index_;
|
||||
detail::identifier_type_t identifier_type_;
|
||||
basic_type type_{basic_type::type_null};
|
||||
};
|
||||
|
||||
template<class IdType>
|
||||
struct pk : public base
|
||||
struct pk final : base
|
||||
{
|
||||
using self = pk<IdType>;
|
||||
using self = pk;
|
||||
|
||||
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) {}
|
||||
explicit pk(const IdType &id)
|
||||
: base(std::type_index(typeid(IdType)), data_type_traits<IdType>::type(1))
|
||||
, id_(id)
|
||||
, size_(sizeof(IdType)) {}
|
||||
|
||||
base *copy() const final {
|
||||
return new self(id_, size_);
|
||||
[[nodiscard]] base *copy() const override {
|
||||
return new self(id_);
|
||||
}
|
||||
|
||||
bool equal_to(const base &x) const final {
|
||||
return static_cast<const pk<IdType> &>(x).id_ == id_;
|
||||
[[nodiscard]] bool equal_to(const base &x) const override {
|
||||
return static_cast<const pk&>(x).id_ == id_;
|
||||
}
|
||||
|
||||
bool less(const base &x) const final {
|
||||
return static_cast<const pk<IdType> &>(x).id_ < id_;
|
||||
[[nodiscard]] bool less(const base &x) const override {
|
||||
return id_ < static_cast<const pk&>(x).id_;
|
||||
}
|
||||
|
||||
bool is_valid() const final
|
||||
{
|
||||
return detail::identifier_type_traits<IdType>::is_valid(id_);
|
||||
[[nodiscard]] bool is_valid() const override {
|
||||
return identifier_type_traits<IdType>::is_valid(id_);
|
||||
}
|
||||
|
||||
std::string str() const final
|
||||
{
|
||||
return detail::identifier_type_traits<IdType>::to_string(id_);
|
||||
[[nodiscard]] std::string str() const override {
|
||||
return identifier_type_traits<IdType>::to_string(id_);
|
||||
}
|
||||
|
||||
void serialize(identifier_serializer &s) final {
|
||||
void serialize(identifier_serializer &s) override {
|
||||
s.serialize(id_, size_);
|
||||
}
|
||||
|
||||
size_t hash() const final {
|
||||
std::hash<IdType> hash_func;
|
||||
return hash_func(id_);
|
||||
[[nodiscard]] size_t hash() const override {
|
||||
return detail::hash(id_);
|
||||
}
|
||||
|
||||
IdType id_;
|
||||
size_t size_{};
|
||||
};
|
||||
|
||||
struct null_pk : public base
|
||||
struct null_pk final : 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;
|
||||
[[nodiscard]] base *copy() const override;
|
||||
[[nodiscard]] bool equal_to(const base &x) const override;
|
||||
[[nodiscard]] bool less(const base &x) const override;
|
||||
[[nodiscard]] bool is_valid() const override;
|
||||
void serialize(identifier_serializer &s) override;
|
||||
[[nodiscard]] std::string str() const override;
|
||||
[[nodiscard]] size_t hash() const override;
|
||||
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)) {}
|
||||
explicit identifier(const Type &id)
|
||||
: id_(std::make_shared<pk<Type>>(id)) {}
|
||||
explicit identifier(const char *id)
|
||||
: id_(std::make_shared<pk<const char*>>(id)) {}
|
||||
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)
|
||||
identifier& operator=(const Type &value)
|
||||
{
|
||||
id_ = std::make_shared<pk<Type>>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
identifier& operator=(const char *value)
|
||||
{
|
||||
id_ = std::make_shared<pk<const char*>>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
~identifier() = default;
|
||||
|
||||
bool operator==(const identifier &x) const;
|
||||
@@ -182,26 +181,28 @@ public:
|
||||
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>();
|
||||
}
|
||||
[[nodiscard]] std::string str() const;
|
||||
[[nodiscard]] const std::type_index &type_index() const;
|
||||
[[nodiscard]] basic_type type() const;
|
||||
|
||||
std::string str() const;
|
||||
const std::type_index &type_index() const;
|
||||
[[nodiscard]] identifier share() const;
|
||||
[[nodiscard]] size_t use_count() const;
|
||||
|
||||
identifier share() const;
|
||||
size_t use_count() const;
|
||||
[[nodiscard]] bool is_integer() const;
|
||||
[[nodiscard]] bool is_floating_point() const;
|
||||
[[nodiscard]] bool is_bool() 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;
|
||||
|
||||
bool is_null() const;
|
||||
bool is_valid() const;
|
||||
[[nodiscard]] bool is_valid() const;
|
||||
void clear();
|
||||
|
||||
void serialize(identifier_serializer &s);
|
||||
void serialize(identifier_serializer &s) const;
|
||||
|
||||
size_t hash() const;
|
||||
[[nodiscard]] size_t hash() const;
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &out, const identifier &id);
|
||||
|
||||
@@ -214,10 +215,12 @@ private:
|
||||
|
||||
static identifier null_identifier{};
|
||||
|
||||
/// @cond MATADOR_DEV
|
||||
struct id_pk_hash
|
||||
{
|
||||
size_t operator()(const identifier &id) const;
|
||||
};
|
||||
|
||||
/// @endcond
|
||||
}
|
||||
#endif //QUERY_IDENTIFIER_HPP
|
||||
|
||||
#endif //MATADOR_IDENTIFIER_HPP
|
||||
|
||||
Reference in New Issue
Block a user