Value.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/TupleDescriptor.h>
  14. #include <LibSQL/Type.h>
  15. #include <LibSQL/ValueImpl.h>
  16. #include <string.h>
  17. namespace SQL {
  18. /**
  19. * A `Value` is an atomic piece of SQL data`. A `Value` has a basic type
  20. * (Text/String, Integer, Float, etc). Richer types are implemented in higher
  21. * level layers, but the resulting data is stored in these `Value` objects.
  22. */
  23. class Value {
  24. public:
  25. Value(Value&) = default;
  26. Value(Value const&) = default;
  27. explicit Value(SQLType sql_type = SQLType::Null);
  28. template<typename... Ts>
  29. explicit Value(Variant<Ts...> impl)
  30. : m_impl(impl)
  31. {
  32. }
  33. enum SetImplementation {
  34. SetImplementationSingleton
  35. };
  36. template<typename I>
  37. Value(SetImplementation, I&& impl)
  38. {
  39. m_impl.set<I>(forward<I>(impl));
  40. }
  41. Value(SQLType, Value const&);
  42. Value(SQLType, String const&);
  43. Value(SQLType, char const*);
  44. Value(SQLType, int);
  45. Value(SQLType, double);
  46. Value(SQLType, bool);
  47. explicit Value(String const&);
  48. explicit Value(char const*);
  49. explicit Value(int);
  50. explicit Value(double);
  51. explicit Value(bool);
  52. ~Value() = default;
  53. [[nodiscard]] bool is_null() const;
  54. [[nodiscard]] SQLType type() const;
  55. [[nodiscard]] String type_name() const;
  56. [[nodiscard]] BaseTypeImpl downcast_to_basetype() const;
  57. template<typename Impl>
  58. Impl const& get_impl(Badge<Impl>) const { return m_impl.get<Impl>(); }
  59. [[nodiscard]] String to_string() const;
  60. [[nodiscard]] Optional<int> to_int() const;
  61. [[nodiscard]] Optional<u32> to_u32() const;
  62. [[nodiscard]] Optional<double> to_double() const;
  63. [[nodiscard]] Optional<bool> to_bool() const;
  64. [[nodiscard]] Optional<Vector<Value>> to_vector() const;
  65. explicit operator String() const;
  66. explicit operator int() const;
  67. explicit operator u32() const;
  68. explicit operator double() const;
  69. explicit operator bool() const;
  70. void assign(Value const& other_value);
  71. void assign(String const& string_value);
  72. void assign(int const& int_value);
  73. void assign(double const& double_value);
  74. void assign(bool const& bool_value);
  75. void assign(Vector<Value> const& values);
  76. Value& operator=(Value const& other);
  77. Value& operator=(String const&);
  78. Value& operator=(char const*);
  79. Value& operator=(int);
  80. Value& operator=(u32);
  81. Value& operator=(double);
  82. Value& operator=(bool);
  83. Value& operator=(Vector<Value> const&);
  84. [[nodiscard]] size_t length() const;
  85. [[nodiscard]] u32 hash() const;
  86. [[nodiscard]] bool can_cast(Value const&) const;
  87. void serialize(Serializer&) const;
  88. void deserialize(Serializer&);
  89. [[nodiscard]] int compare(Value const&) const;
  90. bool operator==(Value const&) const;
  91. bool operator==(String const&) const;
  92. bool operator==(int) const;
  93. bool operator==(double) const;
  94. bool operator!=(Value const&) const;
  95. bool operator<(Value const&) const;
  96. bool operator<=(Value const&) const;
  97. bool operator>(Value const&) const;
  98. bool operator>=(Value const&) const;
  99. static Value const& null();
  100. static Value create_tuple(NonnullRefPtr<TupleDescriptor> const&);
  101. static Value create_array(SQLType element_type, Optional<size_t> const& max_size = {});
  102. private:
  103. void setup(SQLType type);
  104. ValueTypeImpl m_impl { NullImpl() };
  105. friend Serializer;
  106. };
  107. }