/* * Copyright (c) 2021, Jan de Visser * Copyright (c) 2022, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include namespace SQL { template concept Boolean = SameAs, bool>; template concept Integer = (Integral && !Boolean); /** * 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 { template using IntegerType = Conditional, i64, u64>; public: explicit Value(SQLType sql_type = SQLType::Null); explicit Value(DeprecatedString); explicit Value(double); Value(Value const&); Value(Value&&); ~Value(); explicit Value(Integer auto value) : m_type(SQLType::Integer) , m_value(static_cast>(value)) { } explicit Value(Boolean auto value) : m_type(SQLType::Boolean) , m_value(value) { } explicit Value(UnixDateTime); explicit Value(Duration); static ResultOr create_tuple(NonnullRefPtr); static ResultOr create_tuple(Vector); [[nodiscard]] SQLType type() const; [[nodiscard]] StringView type_name() const; [[nodiscard]] bool is_type_compatible_with(SQLType) const; [[nodiscard]] bool is_null() const; [[nodiscard]] bool is_int() const; [[nodiscard]] auto const& value() const { return *m_value; } [[nodiscard]] DeprecatedString to_deprecated_string() const; [[nodiscard]] Optional to_double() const; [[nodiscard]] Optional to_bool() const; [[nodiscard]] Optional> to_vector() const; template [[nodiscard]] Optional to_int() const { if (is_null()) return {}; return m_value->visit( [](DeprecatedString const& value) -> Optional { if constexpr (IsSigned) return value.to_int(); else return value.to_uint(); }, [](Integer auto value) -> Optional { if (!AK::is_within_range(value)) return {}; return static_cast(value); }, [](double value) -> Optional { if (!AK::is_within_range(value)) return {}; return static_cast(round(value)); }, [](bool value) -> Optional { return static_cast(value); }, [](TupleValue const&) -> Optional { return {}; }); } Value& operator=(Value); Value& operator=(DeprecatedString); Value& operator=(double); Value& operator=(Integer auto value) { m_type = SQLType::Integer; m_value = static_cast>(value); return *this; } ResultOr assign_tuple(NonnullRefPtr); ResultOr assign_tuple(Vector); Value& operator=(Boolean auto value) { m_type = SQLType::Boolean; m_value = value; return *this; } [[nodiscard]] size_t length() const; [[nodiscard]] u32 hash() const; void serialize(Serializer&) const; void deserialize(Serializer&); [[nodiscard]] int compare(Value const&) const; bool operator==(Value const&) const; bool operator==(StringView) const; bool operator==(double) const; template bool operator==(T value) { return to_int() == value; } 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; ResultOr add(Value const&) const; ResultOr subtract(Value const&) const; ResultOr multiply(Value const&) const; ResultOr divide(Value const&) const; ResultOr modulo(Value const&) const; ResultOr negate() const; ResultOr shift_left(Value const&) const; ResultOr shift_right(Value const&) const; ResultOr bitwise_or(Value const&) const; ResultOr bitwise_and(Value const&) const; ResultOr bitwise_not() const; [[nodiscard]] TupleElementDescriptor descriptor() const; private: friend Serializer; struct TupleValue { NonnullRefPtr descriptor; Vector values; }; using ValueType = Variant; static ResultOr> infer_tuple_descriptor(Vector const& values); Value(NonnullRefPtr descriptor, Vector values); SQLType m_type { SQLType::Null }; Optional m_value; }; } template<> struct AK::Formatter : Formatter { ErrorOr format(FormatBuilder& builder, SQL::Value const& value) { return Formatter::format(builder, value.to_deprecated_string()); } }; namespace IPC { template<> ErrorOr encode(Encoder&, SQL::Value const&); template<> ErrorOr decode(Decoder&); }