Value.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/DeprecatedString.h>
  9. #include <AK/Format.h>
  10. #include <AK/Optional.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(DeprecatedString);
  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_type_compatible_with(SQLType) const;
  44. [[nodiscard]] bool is_null() const;
  45. [[nodiscard]] DeprecatedString to_deprecated_string() const;
  46. [[nodiscard]] Optional<int> to_int() const;
  47. [[nodiscard]] Optional<u32> to_u32() const;
  48. [[nodiscard]] Optional<double> to_double() const;
  49. [[nodiscard]] Optional<bool> to_bool() const;
  50. [[nodiscard]] Optional<Vector<Value>> to_vector() const;
  51. Value& operator=(Value);
  52. Value& operator=(DeprecatedString);
  53. Value& operator=(int);
  54. Value& operator=(u32);
  55. Value& operator=(double);
  56. ResultOr<void> assign_tuple(NonnullRefPtr<TupleDescriptor>);
  57. ResultOr<void> assign_tuple(Vector<Value>);
  58. template<typename T>
  59. requires(SameAs<RemoveCVReference<T>, bool>) Value& operator=(T value)
  60. {
  61. m_type = SQLType::Boolean;
  62. m_value = value;
  63. return *this;
  64. }
  65. [[nodiscard]] size_t length() const;
  66. [[nodiscard]] u32 hash() const;
  67. void serialize(Serializer&) const;
  68. void deserialize(Serializer&);
  69. [[nodiscard]] int compare(Value const&) const;
  70. bool operator==(Value const&) const;
  71. bool operator==(StringView) const;
  72. bool operator==(int) const;
  73. bool operator==(u32) const;
  74. bool operator==(double) 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. bool operator>=(Value const&) const;
  80. ResultOr<Value> add(Value const&) const;
  81. ResultOr<Value> subtract(Value const&) const;
  82. ResultOr<Value> multiply(Value const&) const;
  83. ResultOr<Value> divide(Value const&) const;
  84. ResultOr<Value> modulo(Value const&) const;
  85. ResultOr<Value> shift_left(Value const&) const;
  86. ResultOr<Value> shift_right(Value const&) const;
  87. ResultOr<Value> bitwise_or(Value const&) const;
  88. ResultOr<Value> bitwise_and(Value const&) const;
  89. [[nodiscard]] TupleElementDescriptor descriptor() const;
  90. private:
  91. friend Serializer;
  92. struct TupleValue {
  93. NonnullRefPtr<TupleDescriptor> descriptor;
  94. Vector<Value> values;
  95. };
  96. using ValueType = Variant<DeprecatedString, int, double, bool, TupleValue>;
  97. static ResultOr<NonnullRefPtr<TupleDescriptor>> infer_tuple_descriptor(Vector<Value> const& values);
  98. Value(NonnullRefPtr<TupleDescriptor> descriptor, Vector<Value> values);
  99. SQLType m_type { SQLType::Null };
  100. Optional<ValueType> m_value;
  101. };
  102. }
  103. template<>
  104. struct AK::Formatter<SQL::Value> : Formatter<StringView> {
  105. ErrorOr<void> format(FormatBuilder& builder, SQL::Value const& value)
  106. {
  107. return Formatter<StringView>::format(builder, value.to_deprecated_string());
  108. }
  109. };