Value.h 5.6 KB

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