Tuple.h 3.0 KB

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