Tuple.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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&, ByteBuffer&, size_t&);
  29. Tuple(NonnullRefPtr<TupleDescriptor> const&, ByteBuffer&);
  30. Tuple(Tuple const&);
  31. virtual ~Tuple() = default;
  32. Tuple& operator=(Tuple const&);
  33. [[nodiscard]] String to_string() const;
  34. explicit operator String() const { return to_string(); }
  35. [[nodiscard]] Vector<String> to_string_vector() const;
  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. bool operator>=(Tuple const& other) const { return compare(other) >= 0; }
  42. [[nodiscard]] bool is_null() const { return m_data.is_empty(); }
  43. [[nodiscard]] bool has(String const& name) const { return index_of(name).has_value(); }
  44. Value const& operator[](size_t ix) const;
  45. Value& operator[](size_t ix);
  46. Value const& operator[](String const& name) const;
  47. Value& operator[](String const& name);
  48. void append(Value const&);
  49. Tuple& operator+=(Value const&);
  50. [[nodiscard]] bool is_compatible(Tuple const&) const;
  51. [[nodiscard]] u32 pointer() const { return m_pointer; }
  52. void set_pointer(u32 ptr) { m_pointer = ptr; }
  53. [[nodiscard]] size_t size() const { return m_data.size(); }
  54. [[nodiscard]] size_t length() const { return m_descriptor->size(); }
  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. virtual void serialize(ByteBuffer&) const;
  60. [[nodiscard]] virtual size_t data_length() const { return descriptor()->data_length(); }
  61. protected:
  62. [[nodiscard]] Optional<size_t> index_of(String) const;
  63. void copy_from(Tuple const&);
  64. void deserialize(ByteBuffer&, size_t&);
  65. private:
  66. NonnullRefPtr<TupleDescriptor> m_descriptor;
  67. Vector<Value> m_data;
  68. u32 m_pointer { 0 };
  69. };
  70. }