TupleDescriptor.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/Serializer.h>
  9. #include <LibSQL/Type.h>
  10. namespace SQL {
  11. struct TupleElementDescriptor {
  12. String name { "" };
  13. SQLType type { SQLType::Text };
  14. Order order { Order::Ascending };
  15. bool operator==(TupleElementDescriptor const&) const = default;
  16. void serialize(Serializer& serializer) const
  17. {
  18. serializer.serialize(name);
  19. serializer.serialize<u8>((u8)type);
  20. serializer.serialize<u8>((u8)order);
  21. }
  22. void deserialize(Serializer& serializer)
  23. {
  24. name = serializer.deserialize<String>();
  25. type = (SQLType)serializer.deserialize<u8>();
  26. order = (Order)serializer.deserialize<u8>();
  27. }
  28. size_t length() const
  29. {
  30. return (sizeof(u32) + name.length()) + 2 * sizeof(u8);
  31. }
  32. };
  33. class TupleDescriptor
  34. : public Vector<TupleElementDescriptor>
  35. , public RefCounted<TupleDescriptor> {
  36. public:
  37. TupleDescriptor() = default;
  38. ~TupleDescriptor() = default;
  39. [[nodiscard]] int compare_ignoring_names(TupleDescriptor const& other) const
  40. {
  41. if (size() != other.size())
  42. return (int)size() - (int)other.size();
  43. for (auto ix = 0u; ix < size(); ++ix) {
  44. auto elem = (*this)[ix];
  45. auto other_elem = other[ix];
  46. if ((elem.type != other_elem.type) || (elem.order != other_elem.order)) {
  47. return 1;
  48. }
  49. }
  50. return 0;
  51. }
  52. void serialize(Serializer& serializer) const
  53. {
  54. serializer.serialize<u32>(size());
  55. for (auto& element : *this) {
  56. serializer.serialize<TupleElementDescriptor>(element);
  57. }
  58. }
  59. void deserialize(Serializer& serializer)
  60. {
  61. auto sz = serializer.deserialize<u32>();
  62. for (auto ix = 0u; ix < sz; ix++) {
  63. append(serializer.deserialize<TupleElementDescriptor>());
  64. }
  65. }
  66. size_t length() const
  67. {
  68. size_t len = sizeof(u32);
  69. for (auto& element : *this) {
  70. len += element.length();
  71. }
  72. return len;
  73. }
  74. using Vector<TupleElementDescriptor>::operator==;
  75. };
  76. }