Value.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Checked.h>
  9. #include <AK/DeprecatedString.h>
  10. #include <AK/Format.h>
  11. #include <AK/Optional.h>
  12. #include <AK/StringView.h>
  13. #include <AK/Variant.h>
  14. #include <AK/Vector.h>
  15. #include <LibIPC/Forward.h>
  16. #include <LibSQL/Forward.h>
  17. #include <LibSQL/Result.h>
  18. #include <LibSQL/Type.h>
  19. #include <math.h>
  20. namespace SQL {
  21. template<typename T>
  22. concept Boolean = SameAs<RemoveCVReference<T>, bool>;
  23. template<typename T>
  24. concept Integer = (Integral<T> && !Boolean<T>);
  25. /**
  26. * A `Value` is an atomic piece of SQL data`. A `Value` has a basic type
  27. * (Text/String, Integer, Float, etc). Richer types are implemented in higher
  28. * level layers, but the resulting data is stored in these `Value` objects.
  29. */
  30. class Value {
  31. template<Integer T>
  32. using IntegerType = Conditional<IsSigned<T>, i64, u64>;
  33. public:
  34. explicit Value(SQLType sql_type = SQLType::Null);
  35. explicit Value(DeprecatedString);
  36. explicit Value(double);
  37. Value(Value const&);
  38. Value(Value&&);
  39. ~Value();
  40. explicit Value(Integer auto value)
  41. : m_type(SQLType::Integer)
  42. , m_value(static_cast<IntegerType<decltype(value)>>(value))
  43. {
  44. }
  45. explicit Value(Boolean auto value)
  46. : m_type(SQLType::Boolean)
  47. , m_value(value)
  48. {
  49. }
  50. static ResultOr<Value> create_tuple(NonnullRefPtr<TupleDescriptor>);
  51. static ResultOr<Value> create_tuple(Vector<Value>);
  52. [[nodiscard]] SQLType type() const;
  53. [[nodiscard]] StringView type_name() const;
  54. [[nodiscard]] bool is_type_compatible_with(SQLType) const;
  55. [[nodiscard]] bool is_null() const;
  56. [[nodiscard]] bool is_int() const;
  57. [[nodiscard]] auto const& value() const
  58. {
  59. VERIFY(m_value.has_value());
  60. return *m_value;
  61. }
  62. [[nodiscard]] DeprecatedString to_deprecated_string() const;
  63. [[nodiscard]] Optional<double> to_double() const;
  64. [[nodiscard]] Optional<bool> to_bool() const;
  65. [[nodiscard]] Optional<Vector<Value>> to_vector() const;
  66. template<Integer T>
  67. [[nodiscard]] Optional<T> to_int() const
  68. {
  69. if (is_null())
  70. return {};
  71. return m_value->visit(
  72. [](DeprecatedString const& value) -> Optional<T> {
  73. if constexpr (IsSigned<T>)
  74. return value.to_int<T>();
  75. else
  76. return value.to_uint<T>();
  77. },
  78. [](Integer auto value) -> Optional<T> {
  79. if (!AK::is_within_range<T>(value))
  80. return {};
  81. return static_cast<T>(value);
  82. },
  83. [](double value) -> Optional<T> {
  84. if (!AK::is_within_range<T>(value))
  85. return {};
  86. return static_cast<T>(round(value));
  87. },
  88. [](bool value) -> Optional<T> { return static_cast<T>(value); },
  89. [](TupleValue const&) -> Optional<T> { return {}; });
  90. }
  91. Value& operator=(Value);
  92. Value& operator=(DeprecatedString);
  93. Value& operator=(double);
  94. Value& operator=(Integer auto value)
  95. {
  96. m_type = SQLType::Integer;
  97. m_value = static_cast<IntegerType<decltype(value)>>(value);
  98. return *this;
  99. }
  100. ResultOr<void> assign_tuple(NonnullRefPtr<TupleDescriptor>);
  101. ResultOr<void> assign_tuple(Vector<Value>);
  102. Value& operator=(Boolean auto value)
  103. {
  104. m_type = SQLType::Boolean;
  105. m_value = value;
  106. return *this;
  107. }
  108. [[nodiscard]] size_t length() const;
  109. [[nodiscard]] u32 hash() const;
  110. void serialize(Serializer&) const;
  111. void deserialize(Serializer&);
  112. [[nodiscard]] int compare(Value const&) const;
  113. bool operator==(Value const&) const;
  114. bool operator==(StringView) const;
  115. bool operator==(double) const;
  116. template<Integer T>
  117. bool operator==(T value)
  118. {
  119. return to_int<T>() == value;
  120. }
  121. bool operator!=(Value const&) const;
  122. bool operator<(Value const&) const;
  123. bool operator<=(Value const&) const;
  124. bool operator>(Value const&) const;
  125. bool operator>=(Value const&) const;
  126. ResultOr<Value> add(Value const&) const;
  127. ResultOr<Value> subtract(Value const&) const;
  128. ResultOr<Value> multiply(Value const&) const;
  129. ResultOr<Value> divide(Value const&) const;
  130. ResultOr<Value> modulo(Value const&) const;
  131. ResultOr<Value> negate() const;
  132. ResultOr<Value> shift_left(Value const&) const;
  133. ResultOr<Value> shift_right(Value const&) const;
  134. ResultOr<Value> bitwise_or(Value const&) const;
  135. ResultOr<Value> bitwise_and(Value const&) const;
  136. ResultOr<Value> bitwise_not() const;
  137. [[nodiscard]] TupleElementDescriptor descriptor() const;
  138. private:
  139. friend Serializer;
  140. struct TupleValue {
  141. NonnullRefPtr<TupleDescriptor> descriptor;
  142. Vector<Value> values;
  143. };
  144. using ValueType = Variant<DeprecatedString, i64, u64, double, bool, TupleValue>;
  145. static ResultOr<NonnullRefPtr<TupleDescriptor>> infer_tuple_descriptor(Vector<Value> const& values);
  146. Value(NonnullRefPtr<TupleDescriptor> descriptor, Vector<Value> values);
  147. SQLType m_type { SQLType::Null };
  148. Optional<ValueType> m_value;
  149. };
  150. }
  151. template<>
  152. struct AK::Formatter<SQL::Value> : Formatter<StringView> {
  153. ErrorOr<void> format(FormatBuilder& builder, SQL::Value const& value)
  154. {
  155. return Formatter<StringView>::format(builder, value.to_deprecated_string());
  156. }
  157. };
  158. namespace IPC {
  159. template<>
  160. ErrorOr<void> encode(Encoder&, SQL::Value const&);
  161. template<>
  162. ErrorOr<SQL::Value> decode(Decoder&);
  163. }