Value.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/Serialize.h>
  13. #include <LibSQL/TupleDescriptor.h>
  14. #include <LibSQL/Type.h>
  15. #include <string.h>
  16. namespace SQL {
  17. class Value;
  18. class BaseImpl {
  19. public:
  20. explicit BaseImpl(SQLType type = SQLType::Null)
  21. : m_type(type)
  22. {
  23. }
  24. [[nodiscard]] SQLType type() const { return m_type; }
  25. [[nodiscard]] String type_name() const { return SQLType_name(type()); }
  26. private:
  27. SQLType m_type { SQLType::Null };
  28. };
  29. class NullImpl : public BaseImpl {
  30. public:
  31. explicit NullImpl()
  32. : BaseImpl(SQLType::Null)
  33. {
  34. }
  35. [[nodiscard]] static bool is_null() { return true; }
  36. [[nodiscard]] static String to_string() { return "(null)"; }
  37. [[nodiscard]] static Optional<int> to_int() { return {}; }
  38. [[nodiscard]] static Optional<double> to_double() { return {}; }
  39. [[nodiscard]] static Optional<bool> to_bool() { return {}; }
  40. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  41. static void assign(Value const&) { }
  42. static void assign_string(String const&) { }
  43. static void assign_int(int) { }
  44. static void assign_double(double) { }
  45. static void assign_bool(bool) { }
  46. static void assign_vector(Vector<Value> const&) { }
  47. [[nodiscard]] static size_t length() { return 0; }
  48. [[nodiscard]] static bool can_cast(Value const&);
  49. [[nodiscard]] static int compare(Value const&);
  50. static void serialize(ByteBuffer&) { }
  51. static void deserialize(ByteBuffer&, size_t&) { }
  52. [[nodiscard]] static u32 hash() { return 0; }
  53. };
  54. template<typename T>
  55. class Impl : public BaseImpl {
  56. public:
  57. [[nodiscard]] bool is_null() const
  58. {
  59. return !m_value.has_value();
  60. }
  61. [[nodiscard]] T const& value() const
  62. {
  63. VERIFY(m_value.has_value());
  64. return m_value.value();
  65. }
  66. [[nodiscard]] size_t length() const
  67. {
  68. return sizeof(T);
  69. }
  70. void serialize(ByteBuffer& buffer) const
  71. {
  72. serialize_to(buffer, value());
  73. }
  74. void deserialize(ByteBuffer& buffer, size_t& at_offset)
  75. {
  76. T value;
  77. deserialize_from(buffer, at_offset, value);
  78. m_value = value;
  79. }
  80. protected:
  81. explicit Impl(SQLType sql_type)
  82. : BaseImpl(sql_type)
  83. {
  84. }
  85. Optional<T> m_value {};
  86. };
  87. class TextImpl : public Impl<String> {
  88. public:
  89. explicit TextImpl()
  90. : Impl(SQLType::Text)
  91. {
  92. }
  93. [[nodiscard]] String to_string() const;
  94. [[nodiscard]] Optional<int> to_int() const;
  95. [[nodiscard]] Optional<double> to_double() const;
  96. [[nodiscard]] Optional<bool> to_bool() const;
  97. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  98. void assign(Value const&);
  99. void assign_string(String const&);
  100. void assign_int(int);
  101. void assign_double(double);
  102. void assign_bool(bool);
  103. void assign_vector(Vector<Value> const&) { m_value = {}; }
  104. [[nodiscard]] size_t length() const;
  105. [[nodiscard]] static bool can_cast(Value const&) { return true; }
  106. [[nodiscard]] int compare(Value const& other) const;
  107. [[nodiscard]] u32 hash() const;
  108. };
  109. class IntegerImpl : public Impl<int> {
  110. public:
  111. IntegerImpl()
  112. : Impl(SQLType::Integer)
  113. {
  114. }
  115. [[nodiscard]] String to_string() const;
  116. [[nodiscard]] Optional<int> to_int() const;
  117. [[nodiscard]] Optional<double> to_double() const;
  118. [[nodiscard]] Optional<bool> to_bool() const;
  119. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  120. void assign(Value const&);
  121. void assign_string(String const&);
  122. void assign_int(int);
  123. void assign_double(double);
  124. void assign_bool(bool);
  125. void assign_vector(Vector<Value> const&) { m_value = {}; }
  126. [[nodiscard]] static bool can_cast(Value const&);
  127. [[nodiscard]] int compare(Value const& other) const;
  128. [[nodiscard]] u32 hash() const;
  129. };
  130. class FloatImpl : public Impl<double> {
  131. public:
  132. explicit FloatImpl()
  133. : Impl(SQLType::Float)
  134. {
  135. }
  136. [[nodiscard]] String to_string() const;
  137. [[nodiscard]] Optional<int> to_int() const;
  138. [[nodiscard]] Optional<double> to_double() const;
  139. [[nodiscard]] static Optional<bool> to_bool() { return {}; }
  140. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  141. void assign(Value const&);
  142. void assign_string(String const&);
  143. void assign_int(int);
  144. void assign_double(double);
  145. void assign_bool(bool) { m_value = {}; }
  146. void assign_vector(Vector<Value> const&) { m_value = {}; }
  147. [[nodiscard]] static bool can_cast(Value const&);
  148. [[nodiscard]] int compare(Value const& other) const;
  149. // Using floats in hash functions is a bad idea. Let's disable that for now.
  150. [[nodiscard]] static u32 hash() { VERIFY_NOT_REACHED(); }
  151. };
  152. class BooleanImpl : public Impl<bool> {
  153. public:
  154. explicit BooleanImpl()
  155. : Impl(SQLType::Boolean)
  156. {
  157. }
  158. [[nodiscard]] String to_string() const;
  159. [[nodiscard]] Optional<int> to_int() const;
  160. [[nodiscard]] static Optional<double> to_double();
  161. [[nodiscard]] Optional<bool> to_bool() const;
  162. [[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
  163. void assign(Value const&);
  164. void assign_string(String const&);
  165. void assign_int(int);
  166. void assign_double(double);
  167. void assign_bool(bool);
  168. void assign_vector(Vector<Value> const&) { m_value = {}; }
  169. [[nodiscard]] static bool can_cast(Value const&);
  170. [[nodiscard]] int compare(Value const& other) const;
  171. [[nodiscard]] u32 hash() const;
  172. };
  173. using BaseTypeImpl = Variant<NullImpl, TextImpl, IntegerImpl, FloatImpl, BooleanImpl>;
  174. class ContainerValueImpl : public Impl<Vector<BaseTypeImpl>> {
  175. public:
  176. virtual ~ContainerValueImpl() = default;
  177. [[nodiscard]] String to_string() const;
  178. [[nodiscard]] static Optional<int> to_int() { return {}; }
  179. [[nodiscard]] static Optional<double> to_double() { return {}; }
  180. [[nodiscard]] static Optional<bool> to_bool() { return {}; }
  181. [[nodiscard]] bool to_vector(Vector<Value>&) const;
  182. void assign_string(String const&) { m_value = {}; }
  183. void assign_int(int) { m_value = {}; }
  184. void assign_double(double) { m_value = {}; }
  185. void assign_bool(bool) { m_value = {}; }
  186. void assign_vector(Vector<Value> const&);
  187. [[nodiscard]] u32 hash() const;
  188. virtual bool validate_before_assignment(Vector<Value> const&) { return true; }
  189. virtual bool validate(BaseTypeImpl const&) { return true; }
  190. virtual bool validate_after_assignment() { return true; }
  191. [[nodiscard]] Vector<String> to_string_vector() const;
  192. [[nodiscard]] size_t size() const { return is_null() ? 0 : value().size(); }
  193. bool append(Value const&);
  194. bool append(BaseTypeImpl const& value);
  195. void serialize_values(ByteBuffer& buffer) const;
  196. void deserialize_values(ByteBuffer&, size_t& at_offset);
  197. protected:
  198. explicit ContainerValueImpl(SQLType sql_type, Optional<size_t> const& max_size = {})
  199. : Impl(sql_type)
  200. , m_max_size(max_size)
  201. {
  202. }
  203. Optional<size_t> m_max_size {};
  204. };
  205. class TupleImpl : public ContainerValueImpl {
  206. public:
  207. explicit TupleImpl(NonnullRefPtr<TupleDescriptor> const& descriptor, bool is_null = true)
  208. : ContainerValueImpl(SQLType::Tuple, is_null)
  209. , m_descriptor(descriptor)
  210. {
  211. m_max_size = m_descriptor->size();
  212. }
  213. explicit TupleImpl()
  214. : ContainerValueImpl(SQLType::Tuple, {})
  215. {
  216. }
  217. void assign(Value const&);
  218. [[nodiscard]] size_t length() const;
  219. [[nodiscard]] bool can_cast(Value const&) const;
  220. [[nodiscard]] int compare(Value const& other) const;
  221. virtual bool validate(BaseTypeImpl const&) override;
  222. virtual bool validate_after_assignment() override;
  223. void serialize(ByteBuffer& buffer) const;
  224. void deserialize(ByteBuffer& buffer, size_t&);
  225. private:
  226. RefPtr<TupleDescriptor> m_descriptor;
  227. };
  228. class ArrayImpl : public ContainerValueImpl {
  229. public:
  230. explicit ArrayImpl(SQLType element_type, Optional<size_t> const& max_size = {})
  231. : ContainerValueImpl(SQLType::Array, max_size)
  232. , m_element_type(element_type)
  233. {
  234. }
  235. explicit ArrayImpl()
  236. : ContainerValueImpl(SQLType::Array, {})
  237. , m_element_type(SQLType::Null)
  238. {
  239. }
  240. void assign(Value const&);
  241. [[nodiscard]] size_t length() const;
  242. [[nodiscard]] bool can_cast(Value const&) const;
  243. [[nodiscard]] int compare(Value const& other) const;
  244. void serialize(ByteBuffer& buffer) const;
  245. void deserialize(ByteBuffer& buffer, size_t&);
  246. virtual bool validate(BaseTypeImpl const&) override;
  247. private:
  248. SQLType m_element_type { SQLType::Text };
  249. };
  250. using ValueTypeImpl = Variant<NullImpl, TextImpl, IntegerImpl, FloatImpl, BooleanImpl, TupleImpl, ArrayImpl>;
  251. /**
  252. * A `Value` is an atomic piece of SQL data. A `Value` has a basic type
  253. * (Text/String, Integer, Float, etc). Richer types are implemented in higher
  254. * level layers, but the resulting data is stored in these `Value` objects.
  255. */
  256. class Value {
  257. public:
  258. Value(Value&) = default;
  259. Value(Value const&) = default;
  260. explicit Value(SQLType sql_type = SQLType::Null);
  261. template<typename... Ts>
  262. explicit Value(Variant<Ts...> impl)
  263. : m_impl(impl)
  264. {
  265. }
  266. enum SetImplementation {
  267. SetImplementationSingleton
  268. };
  269. template<typename I>
  270. Value(SetImplementation, I&& impl)
  271. {
  272. m_impl.set<I>(forward<I>(impl));
  273. }
  274. Value(SQLType, Value const&);
  275. Value(SQLType, String const&);
  276. Value(SQLType, char const*);
  277. Value(SQLType, int);
  278. Value(SQLType, double);
  279. Value(SQLType, bool);
  280. explicit Value(String const&);
  281. explicit Value(char const*);
  282. explicit Value(int);
  283. explicit Value(double);
  284. explicit Value(bool);
  285. ~Value() = default;
  286. [[nodiscard]] bool is_null() const;
  287. [[nodiscard]] SQLType type() const;
  288. [[nodiscard]] String type_name() const;
  289. [[nodiscard]] BaseTypeImpl downcast_to_basetype() const;
  290. template<typename Impl>
  291. Impl const& get_impl(Badge<Impl>) const { return m_impl.get<Impl>(); }
  292. [[nodiscard]] String to_string() const;
  293. [[nodiscard]] Optional<int> to_int() const;
  294. [[nodiscard]] Optional<u32> to_u32() const;
  295. [[nodiscard]] Optional<double> to_double() const;
  296. [[nodiscard]] Optional<bool> to_bool() const;
  297. [[nodiscard]] Optional<Vector<Value>> to_vector() const;
  298. explicit operator String() const;
  299. explicit operator int() const;
  300. explicit operator u32() const;
  301. explicit operator double() const;
  302. explicit operator bool() const;
  303. void assign(Value const& other_value);
  304. void assign(String const& string_value);
  305. void assign(int const& int_value);
  306. void assign(double const& double_value);
  307. void assign(bool const& bool_value);
  308. void assign(Vector<Value> const& values);
  309. Value& operator=(Value const& other);
  310. Value& operator=(String const&);
  311. Value& operator=(char const*);
  312. Value& operator=(int);
  313. Value& operator=(u32);
  314. Value& operator=(double);
  315. Value& operator=(bool);
  316. Value& operator=(Vector<Value> const&);
  317. [[nodiscard]] size_t length() const;
  318. [[nodiscard]] u32 hash() const;
  319. [[nodiscard]] bool can_cast(Value const&) const;
  320. void serialize_to(ByteBuffer&) const;
  321. void deserialize(ByteBuffer&, size_t&);
  322. [[nodiscard]] int compare(Value const&) const;
  323. bool operator==(Value const&) const;
  324. bool operator==(String const&) const;
  325. bool operator==(int) const;
  326. bool operator==(double) const;
  327. bool operator!=(Value const&) const;
  328. bool operator<(Value const&) const;
  329. bool operator<=(Value const&) const;
  330. bool operator>(Value const&) const;
  331. bool operator>=(Value const&) const;
  332. static Value const& null();
  333. static Value create_tuple(NonnullRefPtr<TupleDescriptor> const&);
  334. static Value create_array(SQLType element_type, Optional<size_t> const& max_size = {});
  335. static Value deserialize_from(ByteBuffer&, size_t&);
  336. private:
  337. void setup(SQLType type);
  338. ValueTypeImpl m_impl {};
  339. };
  340. }