/* * Copyright (c) 2021, Jan de Visser * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include namespace SQL { /** * A `Value` is an atomic piece of SQL data`. A `Value` has a basic type * (Text/String, Integer, Float, etc). Richer types are implemented in higher * level layers, but the resulting data is stored in these `Value` objects. */ class Value { public: Value(Value&) = default; Value(Value const&) = default; explicit Value(SQLType sql_type = SQLType::Null); template explicit Value(Variant impl) : m_impl(impl) { } enum SetImplementation { SetImplementationSingleton }; template Value(SetImplementation, I&& impl) { m_impl.set(forward(impl)); } Value(SQLType, Value const&); Value(SQLType, String const&); Value(SQLType, char const*); Value(SQLType, int); Value(SQLType, double); Value(SQLType, bool); explicit Value(String const&); explicit Value(char const*); explicit Value(int); explicit Value(double); explicit Value(bool); ~Value() = default; [[nodiscard]] bool is_null() const; [[nodiscard]] SQLType type() const; [[nodiscard]] String type_name() const; [[nodiscard]] BaseTypeImpl downcast_to_basetype() const; template Impl const& get_impl(Badge) const { return m_impl.get(); } [[nodiscard]] String to_string() const; [[nodiscard]] Optional to_int() const; [[nodiscard]] Optional to_u32() const; [[nodiscard]] Optional to_double() const; [[nodiscard]] Optional to_bool() const; [[nodiscard]] Optional> to_vector() const; explicit operator String() const; explicit operator int() const; explicit operator u32() const; explicit operator double() const; explicit operator bool() const; void assign(Value const& other_value); void assign(String const& string_value); void assign(int const& int_value); void assign(double const& double_value); void assign(bool const& bool_value); void assign(Vector const& values); Value& operator=(Value const& other); Value& operator=(String const&); Value& operator=(char const*); Value& operator=(int); Value& operator=(u32); Value& operator=(double); Value& operator=(bool); Value& operator=(Vector const&); [[nodiscard]] size_t length() const; [[nodiscard]] u32 hash() const; [[nodiscard]] bool can_cast(Value const&) const; void serialize(Serializer&) const; void deserialize(Serializer&); [[nodiscard]] int compare(Value const&) const; bool operator==(Value const&) const; bool operator==(String const&) const; bool operator==(int) const; bool operator==(double) const; bool operator!=(Value const&) const; bool operator<(Value const&) const; bool operator<=(Value const&) const; bool operator>(Value const&) const; bool operator>=(Value const&) const; static Value const& null(); static Value create_tuple(NonnullRefPtr const&); static Value create_array(SQLType element_type, Optional const& max_size = {}); private: void setup(SQLType type); ValueTypeImpl m_impl { NullImpl() }; friend Serializer; }; }