Meta.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/NonnullOwnPtr.h>
  8. #include <AK/NonnullOwnPtrVector.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/Result.h>
  11. #include <AK/String.h>
  12. #include <AK/Vector.h>
  13. #include <LibCore/Object.h>
  14. #include <LibSQL/Forward.h>
  15. #include <LibSQL/Key.h>
  16. #include <LibSQL/Type.h>
  17. namespace SQL {
  18. /**
  19. * This file declares objects describing tables, indexes, and columns.
  20. * It remains to be seen if this will survive in it's current form.
  21. */
  22. class Relation : public Core::Object {
  23. C_OBJECT_ABSTRACT(Relation);
  24. public:
  25. u32 hash() const { return key().hash(); }
  26. u32 pointer() const { return m_pointer; }
  27. void set_pointer(u32 pointer) { m_pointer = pointer; }
  28. ~Relation() override = default;
  29. virtual Key key() const = 0;
  30. Relation const* parent_relation() const { return dynamic_cast<Relation const*>(parent()); }
  31. protected:
  32. Relation(String name, u32 pointer, Relation* parent = nullptr)
  33. : Core::Object(parent)
  34. , m_pointer(pointer)
  35. {
  36. set_name(move(name));
  37. }
  38. explicit Relation(String name, Relation* parent = nullptr)
  39. : Core::Object(parent)
  40. , m_pointer(0)
  41. {
  42. set_name(move(name));
  43. }
  44. private:
  45. u32 m_pointer { 0 };
  46. };
  47. class SchemaDef : public Relation {
  48. C_OBJECT(SchemaDef);
  49. public:
  50. Key key() const override;
  51. static NonnullRefPtr<IndexDef> index_def();
  52. static Key make_key();
  53. private:
  54. explicit SchemaDef(String);
  55. explicit SchemaDef(Key const&);
  56. };
  57. class ColumnDef : public Relation {
  58. C_OBJECT(ColumnDef);
  59. public:
  60. Key key() const override;
  61. SQLType type() const { return m_type; }
  62. size_t column_number() const { return m_index; }
  63. static NonnullRefPtr<IndexDef> index_def();
  64. static Key make_key(TableDef const&);
  65. protected:
  66. ColumnDef(Relation*, size_t, String, SQLType);
  67. private:
  68. size_t m_index;
  69. SQLType m_type { SQLType::Text };
  70. };
  71. class KeyPartDef : public ColumnDef {
  72. C_OBJECT(KeyPartDef);
  73. public:
  74. KeyPartDef(IndexDef*, String, SQLType, Order = Order::Ascending);
  75. Order sort_order() const { return m_sort_order; }
  76. private:
  77. Order m_sort_order { Order::Ascending };
  78. };
  79. class IndexDef : public Relation {
  80. C_OBJECT(IndexDef);
  81. public:
  82. ~IndexDef() override = default;
  83. NonnullRefPtrVector<KeyPartDef> key_definition() const { return m_key_definition; }
  84. bool unique() const { return m_unique; }
  85. [[nodiscard]] size_t size() const { return m_key_definition.size(); }
  86. void append_column(String, SQLType, Order = Order::Ascending);
  87. Key key() const override;
  88. [[nodiscard]] TupleDescriptor to_tuple_descriptor() const;
  89. static NonnullRefPtr<IndexDef> index_def();
  90. static Key make_key(TableDef const& table_def);
  91. private:
  92. IndexDef(TableDef*, String, bool unique = true, u32 pointer = 0);
  93. explicit IndexDef(String, bool unique = true, u32 pointer = 0);
  94. NonnullRefPtrVector<KeyPartDef> m_key_definition;
  95. bool m_unique { false };
  96. friend TableDef;
  97. };
  98. class TableDef : public Relation {
  99. C_OBJECT(TableDef);
  100. public:
  101. Key key() const override;
  102. void append_column(String, SQLType);
  103. void append_column(Key const&);
  104. size_t num_columns() { return m_columns.size(); }
  105. size_t num_indexes() { return m_indexes.size(); }
  106. NonnullRefPtrVector<ColumnDef> columns() const { return m_columns; }
  107. NonnullRefPtrVector<IndexDef> indexes() const { return m_indexes; }
  108. [[nodiscard]] TupleDescriptor to_tuple_descriptor() const;
  109. static NonnullRefPtr<IndexDef> index_def();
  110. static Key make_key(SchemaDef const& schema_def);
  111. static Key make_key(Key const& schema_key);
  112. private:
  113. explicit TableDef(SchemaDef*, String);
  114. NonnullRefPtrVector<ColumnDef> m_columns;
  115. NonnullRefPtrVector<IndexDef> m_indexes;
  116. };
  117. }