ValueImpl.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/ScopeGuard.h>
  10. #include <AK/String.h>
  11. #include <AK/Variant.h>
  12. #include <LibSQL/Forward.h>
  13. #include <LibSQL/Serializer.h>
  14. #include <LibSQL/TupleDescriptor.h>
  15. #include <LibSQL/Type.h>
  16. #include <string.h>
  17. namespace SQL {
  18. class Value;
  19. class BaseImpl {
  20. public:
  21. explicit BaseImpl(SQLType type = SQLType::Null)
  22. : m_type(type)
  23. {
  24. }
  25. [[nodiscard]] SQLType type() const { return m_type; }
  26. [[nodiscard]] String type_name() const { return SQLType_name(type()); }
  27. private:
  28. SQLType m_type { SQLType::Null };
  29. };
  30. class NullImpl : public BaseImpl {
  31. public:
  32. explicit NullImpl()
  33. : BaseImpl(SQLType::Null)
  34. {
  35. }
  36. [[nodiscard]] static bool is_null() { return true; }
  37. [[nodiscard]] static String to_string() { return "(null)"; }
  38. [[nodiscard]] static Optional<int> to_int() { return {}; }
  39. [[nodiscard]] static Optional<double> to_double() { return {}; }
  40. [[nodiscard]] static Optional<bool> to_bool() { return {}; }
  41. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  42. static void assign(Value const&) { }
  43. static void assign_string(String const&) { }
  44. static void assign_int(int) { }
  45. static void assign_double(double) { }
  46. static void assign_bool(bool) { }
  47. static void assign_vector(Vector<Value> const&) { }
  48. [[nodiscard]] static size_t length() { return 0; }
  49. [[nodiscard]] static bool can_cast(Value const&);
  50. [[nodiscard]] static int compare(Value const&);
  51. static void serialize(Serializer&) { }
  52. static void deserialize(Serializer&) { }
  53. [[nodiscard]] static u32 hash() { return 0; }
  54. };
  55. template<typename T>
  56. class Impl : public BaseImpl {
  57. public:
  58. [[nodiscard]] bool is_null() const
  59. {
  60. return !m_value.has_value();
  61. }
  62. [[nodiscard]] T const& value() const
  63. {
  64. VERIFY(m_value.has_value());
  65. return m_value.value();
  66. }
  67. [[nodiscard]] size_t length() const
  68. {
  69. return sizeof(T);
  70. }
  71. void serialize(Serializer& serializer) const
  72. {
  73. serializer.serialize(value());
  74. }
  75. void deserialize(Serializer& serializer)
  76. {
  77. T value;
  78. serializer.deserialize_to(value);
  79. m_value = value;
  80. }
  81. protected:
  82. explicit Impl(SQLType sql_type)
  83. : BaseImpl(sql_type)
  84. {
  85. }
  86. Optional<T> m_value {};
  87. };
  88. class TextImpl : public Impl<String> {
  89. public:
  90. explicit TextImpl()
  91. : Impl(SQLType::Text)
  92. {
  93. }
  94. [[nodiscard]] String to_string() const;
  95. [[nodiscard]] Optional<int> to_int() const;
  96. [[nodiscard]] Optional<double> to_double() const;
  97. [[nodiscard]] Optional<bool> to_bool() const;
  98. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  99. void assign(Value const&);
  100. void assign_string(String const&);
  101. void assign_int(int);
  102. void assign_double(double);
  103. void assign_bool(bool);
  104. void assign_vector(Vector<Value> const&) { m_value = {}; }
  105. [[nodiscard]] size_t length() const;
  106. [[nodiscard]] static bool can_cast(Value const&) { return true; }
  107. [[nodiscard]] int compare(Value const& other) const;
  108. [[nodiscard]] u32 hash() const;
  109. };
  110. class IntegerImpl : public Impl<int> {
  111. public:
  112. IntegerImpl()
  113. : Impl(SQLType::Integer)
  114. {
  115. }
  116. [[nodiscard]] String to_string() const;
  117. [[nodiscard]] Optional<int> to_int() const;
  118. [[nodiscard]] Optional<double> to_double() const;
  119. [[nodiscard]] Optional<bool> to_bool() const;
  120. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  121. void assign(Value const&);
  122. void assign_string(String const&);
  123. void assign_int(int);
  124. void assign_double(double);
  125. void assign_bool(bool);
  126. void assign_vector(Vector<Value> const&) { m_value = {}; }
  127. [[nodiscard]] static bool can_cast(Value const&);
  128. [[nodiscard]] int compare(Value const& other) const;
  129. [[nodiscard]] u32 hash() const;
  130. };
  131. class FloatImpl : public Impl<double> {
  132. public:
  133. explicit FloatImpl()
  134. : Impl(SQLType::Float)
  135. {
  136. }
  137. [[nodiscard]] String to_string() const;
  138. [[nodiscard]] Optional<int> to_int() const;
  139. [[nodiscard]] Optional<double> to_double() const;
  140. [[nodiscard]] static Optional<bool> to_bool() { return {}; }
  141. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  142. void assign(Value const&);
  143. void assign_string(String const&);
  144. void assign_int(int);
  145. void assign_double(double);
  146. void assign_bool(bool) { m_value = {}; }
  147. void assign_vector(Vector<Value> const&) { m_value = {}; }
  148. [[nodiscard]] static bool can_cast(Value const&);
  149. [[nodiscard]] int compare(Value const& other) const;
  150. // Using floats in hash functions is a bad idea. Let's disable that for now.
  151. [[nodiscard]] static u32 hash() { VERIFY_NOT_REACHED(); }
  152. };
  153. class BooleanImpl : public Impl<bool> {
  154. public:
  155. explicit BooleanImpl()
  156. : Impl(SQLType::Boolean)
  157. {
  158. }
  159. [[nodiscard]] String to_string() const;
  160. [[nodiscard]] Optional<int> to_int() const;
  161. [[nodiscard]] static Optional<double> to_double();
  162. [[nodiscard]] Optional<bool> to_bool() const;
  163. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  164. void assign(Value const&);
  165. void assign_string(String const&);
  166. void assign_int(int);
  167. void assign_double(double);
  168. void assign_bool(bool);
  169. void assign_vector(Vector<Value> const&) { m_value = {}; }
  170. [[nodiscard]] static bool can_cast(Value const&);
  171. [[nodiscard]] int compare(Value const& other) const;
  172. [[nodiscard]] u32 hash() const;
  173. };
  174. using BaseTypeImpl = Variant<NullImpl, TextImpl, IntegerImpl, FloatImpl, BooleanImpl>;
  175. class ContainerValueImpl : public Impl<Vector<BaseTypeImpl>> {
  176. public:
  177. virtual ~ContainerValueImpl() = default;
  178. [[nodiscard]] String to_string() const;
  179. [[nodiscard]] static Optional<int> to_int() { return {}; }
  180. [[nodiscard]] static Optional<double> to_double() { return {}; }
  181. [[nodiscard]] static Optional<bool> to_bool() { return {}; }
  182. [[nodiscard]] bool to_vector(Vector<Value>&) const;
  183. void assign_string(String const&) { m_value = {}; }
  184. void assign_int(int) { m_value = {}; }
  185. void assign_double(double) { m_value = {}; }
  186. void assign_bool(bool) { m_value = {}; }
  187. void assign_vector(Vector<Value> const&);
  188. [[nodiscard]] u32 hash() const;
  189. virtual bool validate_before_assignment(Vector<Value> const&) { return true; }
  190. virtual bool validate(BaseTypeImpl const&) { return true; }
  191. virtual bool validate_after_assignment() { return true; }
  192. [[nodiscard]] Vector<String> to_string_vector() const;
  193. [[nodiscard]] size_t length() const;
  194. [[nodiscard]] size_t size() const { return is_null() ? 0 : value().size(); }
  195. bool append(Value const&);
  196. bool append(BaseTypeImpl const& value);
  197. void serialize_values(Serializer&) const;
  198. void deserialize_values(Serializer&);
  199. protected:
  200. explicit ContainerValueImpl(SQLType sql_type)
  201. : Impl(sql_type)
  202. {
  203. }
  204. };
  205. class TupleImpl : public ContainerValueImpl {
  206. public:
  207. explicit TupleImpl(NonnullRefPtr<TupleDescriptor> const& descriptor)
  208. : ContainerValueImpl(SQLType::Tuple)
  209. , m_descriptor(descriptor)
  210. {
  211. }
  212. explicit TupleImpl()
  213. : ContainerValueImpl(SQLType::Tuple)
  214. {
  215. }
  216. void assign(Value const&);
  217. [[nodiscard]] size_t length() const;
  218. [[nodiscard]] bool can_cast(Value const&) const;
  219. [[nodiscard]] int compare(Value const& other) const;
  220. virtual bool validate_before_assignment(Vector<Value> const&) override;
  221. virtual bool validate(BaseTypeImpl const&) override;
  222. virtual bool validate_after_assignment() override;
  223. void serialize(Serializer&) const;
  224. void deserialize(Serializer&);
  225. private:
  226. void infer_descriptor();
  227. void extend_descriptor(Value const&);
  228. RefPtr<TupleDescriptor> m_descriptor;
  229. bool m_descriptor_inferred { false };
  230. };
  231. class ArrayImpl : public ContainerValueImpl {
  232. public:
  233. explicit ArrayImpl(SQLType element_type, Optional<size_t> const& max_size = {})
  234. : ContainerValueImpl(SQLType::Array)
  235. , m_element_type(element_type)
  236. , m_max_size(max_size)
  237. {
  238. }
  239. explicit ArrayImpl()
  240. : ContainerValueImpl(SQLType::Array)
  241. , m_element_type(SQLType::Null)
  242. {
  243. }
  244. void assign(Value const&);
  245. [[nodiscard]] size_t length() const;
  246. [[nodiscard]] bool can_cast(Value const&) const;
  247. [[nodiscard]] int compare(Value const& other) const;
  248. void serialize(Serializer&) const;
  249. void deserialize(Serializer&);
  250. virtual bool validate(BaseTypeImpl const&) override;
  251. private:
  252. SQLType m_element_type { SQLType::Text };
  253. Optional<size_t> m_max_size {};
  254. };
  255. using ValueTypeImpl = Variant<NullImpl, TextImpl, IntegerImpl, FloatImpl, BooleanImpl, TupleImpl, ArrayImpl>;
  256. }