Tuple.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/Vector.h>
  8. #include <LibSQL/Forward.h>
  9. #include <LibSQL/TupleDescriptor.h>
  10. #include <LibSQL/Value.h>
  11. namespace SQL {
  12. /**
  13. * A Tuple is an element of a random-access data structure persisted in a Heap.
  14. * Tuple objects stored in such a structure have a definition controlling the
  15. * number of parts or columns the tuple has, the types of the parts, and the
  16. * sort order of these parts. Besides having an optional definition, a Tuple
  17. * consists of one Value object per part. In addition, tuples have a u32 pointer
  18. * member which points to a Heap location.
  19. *
  20. * Tuple is a base class; concrete subclasses are Key, which implements the
  21. * elements of an index, and Row, which implements the rows in a table.
  22. */
  23. class Tuple {
  24. public:
  25. Tuple();
  26. explicit Tuple(NonnullRefPtr<TupleDescriptor> const&, u32 pointer = 0);
  27. Tuple(NonnullRefPtr<TupleDescriptor> const&, Serializer&);
  28. Tuple(Tuple const&);
  29. virtual ~Tuple() = default;
  30. Tuple& operator=(Tuple const&);
  31. [[nodiscard]] DeprecatedString to_deprecated_string() const;
  32. explicit operator DeprecatedString() const { return to_deprecated_string(); }
  33. bool operator<(Tuple const& other) const { return compare(other) < 0; }
  34. bool operator<=(Tuple const& other) const { return compare(other) <= 0; }
  35. bool operator==(Tuple const& other) const { return compare(other) == 0; }
  36. bool operator!=(Tuple const& other) const { return compare(other) != 0; }
  37. bool operator>(Tuple const& other) const { return compare(other) > 0; }
  38. bool operator>=(Tuple const& other) const { return compare(other) >= 0; }
  39. [[nodiscard]] bool is_null() const { return m_data.is_empty(); }
  40. [[nodiscard]] bool has(DeprecatedString const& name) const { return index_of(name).has_value(); }
  41. Value const& operator[](size_t ix) const { return m_data[ix]; }
  42. Value& operator[](size_t ix) { return m_data[ix]; }
  43. Value const& operator[](DeprecatedString const& name) const;
  44. Value& operator[](DeprecatedString const& name);
  45. void append(Value const&);
  46. Tuple& operator+=(Value const&);
  47. void extend(Tuple const&);
  48. [[nodiscard]] bool is_compatible(Tuple const&) const;
  49. [[nodiscard]] u32 pointer() const { return m_pointer; }
  50. void set_pointer(u32 ptr) { m_pointer = ptr; }
  51. [[nodiscard]] size_t size() const { return m_data.size(); }
  52. [[nodiscard]] virtual size_t length() const;
  53. void clear() { m_data.clear(); }
  54. [[nodiscard]] NonnullRefPtr<TupleDescriptor> descriptor() const { return m_descriptor; }
  55. [[nodiscard]] int compare(Tuple const&) const;
  56. [[nodiscard]] int match(Tuple const&) const;
  57. [[nodiscard]] u32 hash() const;
  58. [[nodiscard]] Vector<Value> take_data() { return move(m_data); }
  59. protected:
  60. [[nodiscard]] Optional<size_t> index_of(StringView) const;
  61. void copy_from(Tuple const&);
  62. virtual void serialize(Serializer&) const;
  63. virtual void deserialize(Serializer&);
  64. private:
  65. NonnullRefPtr<TupleDescriptor> m_descriptor;
  66. Vector<Value> m_data;
  67. u32 m_pointer { 2 * sizeof(u32) };
  68. friend Serializer;
  69. };
  70. }