Value.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <LibIPC/Forward.h>
  15. #include <LibSQL/Forward.h>
  16. #include <LibSQL/Result.h>
  17. #include <LibSQL/Type.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. explicit Value(SQLType sql_type = SQLType::Null);
  27. explicit Value(DeprecatedString);
  28. explicit Value(int);
  29. explicit Value(u32);
  30. explicit Value(double);
  31. Value(Value const&);
  32. Value(Value&&);
  33. ~Value();
  34. static ResultOr<Value> create_tuple(NonnullRefPtr<TupleDescriptor>);
  35. static ResultOr<Value> create_tuple(Vector<Value>);
  36. template<typename T>
  37. requires(SameAs<RemoveCVReference<T>, bool>) explicit Value(T value)
  38. : m_type(SQLType::Boolean)
  39. , m_value(value)
  40. {
  41. }
  42. [[nodiscard]] SQLType type() const;
  43. [[nodiscard]] StringView type_name() const;
  44. [[nodiscard]] bool is_type_compatible_with(SQLType) const;
  45. [[nodiscard]] bool is_null() const;
  46. [[nodiscard]] DeprecatedString to_deprecated_string() const;
  47. [[nodiscard]] Optional<int> to_int() const;
  48. [[nodiscard]] Optional<u32> to_u32() const;
  49. [[nodiscard]] Optional<double> to_double() const;
  50. [[nodiscard]] Optional<bool> to_bool() const;
  51. [[nodiscard]] Optional<Vector<Value>> to_vector() const;
  52. Value& operator=(Value);
  53. Value& operator=(DeprecatedString);
  54. Value& operator=(int);
  55. Value& operator=(u32);
  56. Value& operator=(double);
  57. ResultOr<void> assign_tuple(NonnullRefPtr<TupleDescriptor>);
  58. ResultOr<void> assign_tuple(Vector<Value>);
  59. template<typename T>
  60. requires(SameAs<RemoveCVReference<T>, bool>) Value& operator=(T value)
  61. {
  62. m_type = SQLType::Boolean;
  63. m_value = value;
  64. return *this;
  65. }
  66. [[nodiscard]] size_t length() const;
  67. [[nodiscard]] u32 hash() const;
  68. void serialize(Serializer&) const;
  69. void deserialize(Serializer&);
  70. [[nodiscard]] int compare(Value const&) const;
  71. bool operator==(Value const&) const;
  72. bool operator==(StringView) const;
  73. bool operator==(int) const;
  74. bool operator==(u32) const;
  75. bool operator==(double) 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. bool operator>=(Value const&) const;
  81. ResultOr<Value> add(Value const&) const;
  82. ResultOr<Value> subtract(Value const&) const;
  83. ResultOr<Value> multiply(Value const&) const;
  84. ResultOr<Value> divide(Value const&) const;
  85. ResultOr<Value> modulo(Value const&) const;
  86. ResultOr<Value> shift_left(Value const&) const;
  87. ResultOr<Value> shift_right(Value const&) const;
  88. ResultOr<Value> bitwise_or(Value const&) const;
  89. ResultOr<Value> bitwise_and(Value const&) const;
  90. [[nodiscard]] TupleElementDescriptor descriptor() const;
  91. private:
  92. friend Serializer;
  93. struct TupleValue {
  94. NonnullRefPtr<TupleDescriptor> descriptor;
  95. Vector<Value> values;
  96. };
  97. using ValueType = Variant<DeprecatedString, int, double, bool, TupleValue>;
  98. static ResultOr<NonnullRefPtr<TupleDescriptor>> infer_tuple_descriptor(Vector<Value> const& values);
  99. Value(NonnullRefPtr<TupleDescriptor> descriptor, Vector<Value> values);
  100. SQLType m_type { SQLType::Null };
  101. Optional<ValueType> m_value;
  102. };
  103. }
  104. template<>
  105. struct AK::Formatter<SQL::Value> : Formatter<StringView> {
  106. ErrorOr<void> format(FormatBuilder& builder, SQL::Value const& value)
  107. {
  108. return Formatter<StringView>::format(builder, value.to_deprecated_string());
  109. }
  110. };
  111. namespace IPC {
  112. template<>
  113. bool encode(Encoder&, SQL::Value const&);
  114. template<>
  115. ErrorOr<void> decode(Decoder&, SQL::Value&);
  116. }