query/source/core/utils/identifier.cpp

203 lines
4.0 KiB
C++

#include "matador/utils/identifier.hpp"
#include <cstring>
#include <stdexcept>
#include <ostream>
namespace matador::utils {
size_t detail::hash(const char *value) {
return std::hash<std::string_view>()(std::string_view(value, std::strlen(value)));
}
bool identifier_type_traits<const char*>::is_valid(const char *value) {
return value != nullptr && strlen(value) > 0;
}
std::string identifier_type_traits<const char*>::to_string(const char *value) {
return value;
}
identifier::base::base(const std::type_index &ti, const basic_type type)
: type_index_(ti)
, type_(type)
{}
identifier::null_pk::null_pk()
: base(std::type_index(typeid(null_type_t)), basic_type::type_null)
{}
identifier::base* identifier::null_pk::copy() const
{
return new null_pk;
}
bool identifier::null_pk::equal_to(const base &x) const
{
return type_index_ == x.type_index_;
}
bool identifier::null_pk::less(const base &x) const
{
return type_index_ == x.type_index_;
}
bool identifier::null_pk::is_valid() const
{
return identifier_type_traits<null_type_t>::is_valid();
}
std::string identifier::null_pk::str() const
{
return identifier_type_traits<null_type_t>::to_string();
}
void identifier::null_pk::serialize(identifier_serializer &s)
{
s.serialize(null_, {});
}
size_t identifier::null_pk::hash() const
{
return std::hash<nullptr_t>()(nullptr);
}
identifier::identifier()
: id_(std::make_shared<null_pk>()) {}
identifier::identifier(const identifier &x)
: id_(x.id_->copy()) {}
identifier &identifier::operator=(const identifier &x) {
if (this == &x) {
return *this;
}
id_.reset(x.id_->copy());
return *this;
}
identifier::identifier(identifier &&x) noexcept
: id_(std::move(x.id_)) {
x.clear();
}
identifier &identifier::operator=(identifier &&x) noexcept {
id_ = std::move(x.id_);
x.clear();
return *this;
}
bool identifier::operator==(const identifier &x) const {
return id_->equal_to(*x.id_);
}
bool identifier::operator!=(const identifier &x) const {
return !operator==(x);
}
bool identifier::operator<(const identifier &x) const {
return id_->less(*x.id_);
}
bool identifier::operator<=(const identifier &x) const {
return operator==(x) || operator<(x);
}
bool identifier::operator>(const identifier &x) const {
return !operator<=(x);
}
bool identifier::operator>=(const identifier &x) const {
return !operator<(x);
}
std::string identifier::str() const {
return id_->str();
}
const std::type_index &identifier::type_index() const {
return id_->type_index_;
}
basic_type identifier::type() const {
return id_->type_;
}
identifier identifier::share() const
{
return identifier(id_);
}
size_t identifier::use_count() const
{
return id_.use_count();
}
bool identifier::is_integer() const {
return id_->type_ >= basic_type::type_int8 && id_->type_ <= basic_type::type_uint64;
}
bool identifier::is_floating_point() const {
return id_->type_ == basic_type::type_float || id_->type_ == basic_type::type_double;
}
bool identifier::is_bool() const {
return id_->type_ == basic_type::type_bool;
}
bool identifier::is_varchar() const {
return id_->type_ == basic_type::type_varchar;
}
bool identifier::is_date() const {
return id_->type_ == basic_type::type_date;
}
bool identifier::is_time() const {
return id_->type_ == basic_type::type_time;
}
bool identifier::is_blob() const {
return id_->type_ == basic_type::type_blob;
}
bool identifier::is_null() const
{
return type_index() == null_identifier.type_index();
}
bool identifier::is_valid() const
{
return id_->is_valid();
}
void identifier::clear()
{
id_ = std::make_unique<null_pk>();
}
void identifier::serialize(identifier_serializer &s) const {
id_->serialize(s);
}
size_t identifier::hash() const
{
return id_->hash();
}
identifier::identifier(const std::shared_ptr<base> &id)
: id_(id)
{}
std::ostream &operator<<(std::ostream &out, const identifier &id)
{
out << id.str();
return out;
}
size_t id_pk_hash::operator()(const identifier &id) const
{
return id.hash();
}
}