Value.h 5.6 KB

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