Value.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. return *m_value;
  60. }
  61. [[nodiscard]] DeprecatedString to_deprecated_string() const;
  62. [[nodiscard]] Optional<double> to_double() const;
  63. [[nodiscard]] Optional<bool> to_bool() const;
  64. [[nodiscard]] Optional<Vector<Value>> to_vector() const;
  65. template<Integer T>
  66. [[nodiscard]] Optional<T> to_int() const
  67. {
  68. if (is_null())
  69. return {};
  70. return m_value->visit(
  71. [](DeprecatedString const& value) -> Optional<T> {
  72. if constexpr (IsSigned<T>)
  73. return value.to_int<T>();
  74. else
  75. return value.to_uint<T>();
  76. },
  77. [](Integer auto value) -> Optional<T> {
  78. if (!AK::is_within_range<T>(value))
  79. return {};
  80. return static_cast<T>(value);
  81. },
  82. [](double value) -> Optional<T> {
  83. if (!AK::is_within_range<T>(value))
  84. return {};
  85. return static_cast<T>(round(value));
  86. },
  87. [](bool value) -> Optional<T> { return static_cast<T>(value); },
  88. [](TupleValue const&) -> Optional<T> { return {}; });
  89. }
  90. Value& operator=(Value);
  91. Value& operator=(DeprecatedString);
  92. Value& operator=(double);
  93. Value& operator=(Integer auto value)
  94. {
  95. m_type = SQLType::Integer;
  96. m_value = static_cast<IntegerType<decltype(value)>>(value);
  97. return *this;
  98. }
  99. ResultOr<void> assign_tuple(NonnullRefPtr<TupleDescriptor>);
  100. ResultOr<void> assign_tuple(Vector<Value>);
  101. Value& operator=(Boolean auto value)
  102. {
  103. m_type = SQLType::Boolean;
  104. m_value = value;
  105. return *this;
  106. }
  107. [[nodiscard]] size_t length() const;
  108. [[nodiscard]] u32 hash() const;
  109. void serialize(Serializer&) const;
  110. void deserialize(Serializer&);
  111. [[nodiscard]] int compare(Value const&) const;
  112. bool operator==(Value const&) const;
  113. bool operator==(StringView) const;
  114. bool operator==(double) const;
  115. template<Integer T>
  116. bool operator==(T value)
  117. {
  118. return to_int<T>() == value;
  119. }
  120. bool operator!=(Value const&) const;
  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. ResultOr<Value> add(Value const&) const;
  126. ResultOr<Value> subtract(Value const&) const;
  127. ResultOr<Value> multiply(Value const&) const;
  128. ResultOr<Value> divide(Value const&) const;
  129. ResultOr<Value> modulo(Value const&) const;
  130. ResultOr<Value> negate() const;
  131. ResultOr<Value> shift_left(Value const&) const;
  132. ResultOr<Value> shift_right(Value const&) const;
  133. ResultOr<Value> bitwise_or(Value const&) const;
  134. ResultOr<Value> bitwise_and(Value const&) const;
  135. ResultOr<Value> bitwise_not() const;
  136. [[nodiscard]] TupleElementDescriptor descriptor() const;
  137. private:
  138. friend Serializer;
  139. struct TupleValue {
  140. NonnullRefPtr<TupleDescriptor> descriptor;
  141. Vector<Value> values;
  142. };
  143. using ValueType = Variant<DeprecatedString, i64, u64, double, bool, TupleValue>;
  144. static ResultOr<NonnullRefPtr<TupleDescriptor>> infer_tuple_descriptor(Vector<Value> const& values);
  145. Value(NonnullRefPtr<TupleDescriptor> descriptor, Vector<Value> values);
  146. SQLType m_type { SQLType::Null };
  147. Optional<ValueType> m_value;
  148. };
  149. }
  150. template<>
  151. struct AK::Formatter<SQL::Value> : Formatter<StringView> {
  152. ErrorOr<void> format(FormatBuilder& builder, SQL::Value const& value)
  153. {
  154. return Formatter<StringView>::format(builder, value.to_deprecated_string());
  155. }
  156. };
  157. namespace IPC {
  158. template<>
  159. ErrorOr<void> encode(Encoder&, SQL::Value const&);
  160. template<>
  161. ErrorOr<SQL::Value> decode(Decoder&);
  162. }