Value.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Format.h>
  9. #include <AK/Optional.h>
  10. #include <AK/String.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Variant.h>
  13. #include <AK/Vector.h>
  14. #include <LibSQL/Forward.h>
  15. #include <LibSQL/Result.h>
  16. #include <LibSQL/Type.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. explicit Value(SQLType sql_type = SQLType::Null);
  26. explicit Value(String);
  27. explicit Value(int);
  28. explicit Value(u32);
  29. explicit Value(double);
  30. Value(Value const&);
  31. Value(Value&&);
  32. ~Value();
  33. static ResultOr<Value> create_tuple(NonnullRefPtr<TupleDescriptor>);
  34. static ResultOr<Value> create_tuple(Vector<Value>);
  35. template<typename T>
  36. requires(SameAs<RemoveCVReference<T>, bool>) explicit Value(T value)
  37. : m_type(SQLType::Boolean)
  38. , m_value(value)
  39. {
  40. }
  41. [[nodiscard]] SQLType type() const;
  42. [[nodiscard]] StringView type_name() const;
  43. [[nodiscard]] bool is_null() const;
  44. [[nodiscard]] String to_string() const;
  45. [[nodiscard]] Optional<int> to_int() const;
  46. [[nodiscard]] Optional<u32> to_u32() const;
  47. [[nodiscard]] Optional<double> to_double() const;
  48. [[nodiscard]] Optional<bool> to_bool() const;
  49. [[nodiscard]] Optional<Vector<Value>> to_vector() const;
  50. Value& operator=(Value);
  51. Value& operator=(String);
  52. Value& operator=(int);
  53. Value& operator=(u32);
  54. Value& operator=(double);
  55. ResultOr<void> assign_tuple(NonnullRefPtr<TupleDescriptor>);
  56. ResultOr<void> assign_tuple(Vector<Value>);
  57. template<typename T>
  58. requires(SameAs<RemoveCVReference<T>, bool>) Value& operator=(T value)
  59. {
  60. m_type = SQLType::Boolean;
  61. m_value = value;
  62. return *this;
  63. }
  64. [[nodiscard]] size_t length() const;
  65. [[nodiscard]] u32 hash() const;
  66. void serialize(Serializer&) const;
  67. void deserialize(Serializer&);
  68. [[nodiscard]] int compare(Value const&) const;
  69. bool operator==(Value const&) const;
  70. bool operator==(StringView) const;
  71. bool operator==(int) const;
  72. bool operator==(u32) const;
  73. bool operator==(double) const;
  74. bool operator!=(Value const&) const;
  75. bool operator<(Value const&) const;
  76. bool operator<=(Value const&) const;
  77. bool operator>(Value const&) const;
  78. bool operator>=(Value const&) const;
  79. ResultOr<Value> add(Value const&) const;
  80. ResultOr<Value> subtract(Value const&) const;
  81. ResultOr<Value> multiply(Value const&) const;
  82. ResultOr<Value> divide(Value const&) const;
  83. ResultOr<Value> modulo(Value const&) const;
  84. ResultOr<Value> shift_left(Value const&) const;
  85. ResultOr<Value> shift_right(Value const&) const;
  86. ResultOr<Value> bitwise_or(Value const&) const;
  87. ResultOr<Value> bitwise_and(Value const&) const;
  88. [[nodiscard]] TupleElementDescriptor descriptor() const;
  89. private:
  90. friend Serializer;
  91. struct TupleValue {
  92. NonnullRefPtr<TupleDescriptor> descriptor;
  93. Vector<Value> values;
  94. };
  95. using ValueType = Variant<String, int, double, bool, TupleValue>;
  96. static ResultOr<NonnullRefPtr<TupleDescriptor>> infer_tuple_descriptor(Vector<Value> const& values);
  97. Value(NonnullRefPtr<TupleDescriptor> descriptor, Vector<Value> values);
  98. SQLType m_type { SQLType::Null };
  99. Optional<ValueType> m_value;
  100. };
  101. }
  102. template<>
  103. struct AK::Formatter<SQL::Value> : Formatter<StringView> {
  104. ErrorOr<void> format(FormatBuilder& builder, SQL::Value const& value)
  105. {
  106. return Formatter<StringView>::format(builder, value.to_string());
  107. }
  108. };