TupleDescriptor.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. DeprecatedString schema { "" };
  13. DeprecatedString table { "" };
  14. DeprecatedString name { "" };
  15. SQLType type { SQLType::Text };
  16. Order order { Order::Ascending };
  17. bool operator==(TupleElementDescriptor const&) const = default;
  18. void serialize(Serializer& serializer) const
  19. {
  20. serializer.serialize(name);
  21. serializer.serialize<u8>((u8)type);
  22. serializer.serialize<u8>((u8)order);
  23. }
  24. void deserialize(Serializer& serializer)
  25. {
  26. name = serializer.deserialize<DeprecatedString>();
  27. type = (SQLType)serializer.deserialize<u8>();
  28. order = (Order)serializer.deserialize<u8>();
  29. }
  30. size_t length() const
  31. {
  32. return (sizeof(u32) + name.length()) + 2 * sizeof(u8);
  33. }
  34. DeprecatedString to_deprecated_string() const
  35. {
  36. return DeprecatedString::formatted(" name: {} type: {} order: {}", name, SQLType_name(type), Order_name(order));
  37. }
  38. };
  39. class TupleDescriptor
  40. : public Vector<TupleElementDescriptor>
  41. , public RefCounted<TupleDescriptor> {
  42. public:
  43. TupleDescriptor() = default;
  44. ~TupleDescriptor() = default;
  45. [[nodiscard]] int compare_ignoring_names(TupleDescriptor const& other) const
  46. {
  47. if (size() != other.size())
  48. return (int)size() - (int)other.size();
  49. for (auto ix = 0u; ix < size(); ++ix) {
  50. auto elem = (*this)[ix];
  51. auto other_elem = other[ix];
  52. if ((elem.type != other_elem.type) || (elem.order != other_elem.order)) {
  53. return 1;
  54. }
  55. }
  56. return 0;
  57. }
  58. void serialize(Serializer& serializer) const
  59. {
  60. serializer.serialize<u32>(size());
  61. for (auto& element : *this) {
  62. serializer.serialize<TupleElementDescriptor>(element);
  63. }
  64. }
  65. void deserialize(Serializer& serializer)
  66. {
  67. auto sz = serializer.deserialize<u32>();
  68. for (auto ix = 0u; ix < sz; ix++) {
  69. append(serializer.deserialize<TupleElementDescriptor>());
  70. }
  71. }
  72. size_t length() const
  73. {
  74. size_t len = sizeof(u32);
  75. for (auto& element : *this) {
  76. len += element.length();
  77. }
  78. return len;
  79. }
  80. DeprecatedString to_deprecated_string() const
  81. {
  82. Vector<DeprecatedString> elements;
  83. for (auto& element : *this) {
  84. elements.append(element.to_deprecated_string());
  85. }
  86. return DeprecatedString::formatted("[\n{}\n]", DeprecatedString::join('\n', elements));
  87. }
  88. using Vector<TupleElementDescriptor>::operator==;
  89. };
  90. }