/* * 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 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: explicit Value(SQLType sql_type = SQLType::Null); explicit Value(String); explicit Value(int); explicit Value(u32); explicit Value(double); Value(Value const&); Value(Value&&); ~Value(); static ResultOr create_tuple(NonnullRefPtr); static ResultOr create_tuple(Vector); template requires(SameAs, bool>) explicit Value(T value) : m_type(SQLType::Boolean) , m_value(value) { } [[nodiscard]] SQLType type() const; [[nodiscard]] StringView type_name() const; [[nodiscard]] bool is_null() const; [[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; Value& operator=(Value); Value& operator=(String); Value& operator=(int); Value& operator=(u32); Value& operator=(double); ResultOr assign_tuple(NonnullRefPtr); ResultOr assign_tuple(Vector); template requires(SameAs, bool>) Value& operator=(T 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==(int) const; bool operator==(u32) 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; 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 shift_left(Value const&) const; ResultOr shift_right(Value const&) const; ResultOr bitwise_or(Value const&) const; ResultOr bitwise_and(Value const&) 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_string()); } };