Meta.h 3.9 KB

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