Value.h 4.0 KB

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