Meta.h 4.3 KB

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