TupleDescriptor.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/Type.h>
  9. namespace SQL {
  10. struct TupleElement {
  11. String name { "" };
  12. SQLType type { SQLType::Text };
  13. Order order { Order::Ascending };
  14. bool operator==(TupleElement const&) const = default;
  15. };
  16. class TupleDescriptor
  17. : public Vector<TupleElement>
  18. , public RefCounted<TupleDescriptor> {
  19. public:
  20. TupleDescriptor() = default;
  21. ~TupleDescriptor() = default;
  22. [[nodiscard]] size_t data_length() const
  23. {
  24. size_t sz = sizeof(u32);
  25. for (auto& part : *this) {
  26. sz += size_of(part.type);
  27. }
  28. return sz;
  29. }
  30. [[nodiscard]] int compare_ignoring_names(TupleDescriptor const& other) const
  31. {
  32. if (size() != other.size())
  33. return (int)size() - (int)other.size();
  34. for (auto ix = 0u; ix < size(); ++ix) {
  35. auto elem = (*this)[ix];
  36. auto other_elem = other[ix];
  37. if ((elem.type != other_elem.type) || (elem.order != other_elem.order)) {
  38. return 1;
  39. }
  40. }
  41. return 0;
  42. }
  43. using Vector<TupleElement>::operator==;
  44. };
  45. }