Value.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/ByteBuffer.h>
  8. #include <AK/Function.h>
  9. #include <AK/String.h>
  10. #include <AK/Variant.h>
  11. #include <LibSQL/Type.h>
  12. namespace SQL {
  13. /**
  14. * A `Value` is an atomic piece of SQL data. A `Value` has a basic type
  15. * (Text/String, Integer, Float, etc). Richer types are implemented in higher
  16. * level layers, but the resulting data is stored in these `Value` objects.
  17. */
  18. class Value {
  19. public:
  20. explicit Value(SQLType sql_type = SQLType::Text);
  21. Value(SQLType sql_type, ByteBuffer& buffer, size_t& offset);
  22. Value(Value const& other);
  23. ~Value();
  24. static Value const& null();
  25. Value& operator=(Value&& other) noexcept
  26. {
  27. (*this) = other;
  28. return (*this);
  29. }
  30. Value& operator=(Value const& other);
  31. Value& operator=(String const&);
  32. Value& operator=(String&& string)
  33. {
  34. operator=(string);
  35. return *this;
  36. }
  37. Value& operator=(int);
  38. Value& operator=(u32);
  39. Value& operator=(double);
  40. Value& set_null();
  41. Optional<String> to_string() const;
  42. explicit operator String() const;
  43. Optional<int> to_int() const;
  44. explicit operator int() const;
  45. Optional<double> to_double() const;
  46. explicit operator double() const;
  47. Optional<u32> to_u32() const;
  48. explicit operator u32() const;
  49. [[nodiscard]] SQLType type() const { return m_type; }
  50. [[nodiscard]] const char* type_name() const { return m_type_name(); }
  51. [[nodiscard]] size_t size() const { return m_size(); }
  52. [[nodiscard]] int compare(Value const& other) const { return m_compare(other); }
  53. [[nodiscard]] bool is_null() const { return m_is_null; }
  54. [[nodiscard]] bool can_cast(Value const&) const;
  55. [[nodiscard]] u32 hash() const { return (is_null()) ? 0 : m_hash(); }
  56. bool operator==(Value const& other) const { return m_compare(other) == 0; }
  57. bool operator==(String const& other) const;
  58. bool operator==(int other) const;
  59. bool operator==(double other) const;
  60. bool operator!=(Value const& other) const { return m_compare(other) != 0; }
  61. bool operator<(Value const& other) const { return m_compare(other) < 0; }
  62. bool operator<=(Value const& other) const { return m_compare(other) <= 0; }
  63. bool operator>(Value const& other) const { return m_compare(other) > 0; }
  64. bool operator>=(Value const& other) const { return m_compare(other) >= 0; }
  65. void serialize(ByteBuffer& buffer) const
  66. {
  67. VERIFY(!is_null());
  68. m_serialize(buffer);
  69. }
  70. private:
  71. void setup(SQLType sql_type);
  72. void setup_text();
  73. void setup_int();
  74. void setup_float();
  75. Function<Optional<String>()> m_to_string;
  76. Function<Optional<int>()> m_to_int;
  77. Function<Optional<double>()> m_to_double;
  78. Function<void(Value const&)> m_assign_value;
  79. Function<void(String const&)> m_assign_string;
  80. Function<void(int)> m_assign_int;
  81. Function<void(double)> m_assign_double;
  82. Function<int(Value const&)> m_compare;
  83. Function<void(ByteBuffer&)> m_serialize;
  84. Function<void(ByteBuffer&, size_t& offset)> m_deserialize;
  85. Function<size_t()> m_size;
  86. Function<const char*()> m_type_name;
  87. Function<bool(Value const&)> m_can_cast;
  88. Function<u32()> m_hash;
  89. SQLType m_type { SQLType::Text };
  90. bool m_is_null { true };
  91. Variant<String, int, double> m_impl {};
  92. };
  93. }