Meta.h 4.6 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/ByteString.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/Result.h>
  11. #include <AK/Vector.h>
  12. #include <LibCore/EventReceiver.h>
  13. #include <LibSQL/Forward.h>
  14. #include <LibSQL/Heap.h>
  15. #include <LibSQL/Type.h>
  16. #include <LibSQL/Value.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 RefCounted<Relation> {
  23. public:
  24. virtual ~Relation() = default;
  25. ByteString const& name() const { return m_name; }
  26. Relation const* parent() const { return m_parent; }
  27. u32 hash() const;
  28. Block::Index block_index() const { return m_block_index; }
  29. void set_block_index(Block::Index block_index) { m_block_index = block_index; }
  30. virtual Key key() const = 0;
  31. protected:
  32. Relation(ByteString name, Block::Index block_index, Relation* parent = nullptr)
  33. : m_name(move(name))
  34. , m_block_index(block_index)
  35. , m_parent(parent)
  36. {
  37. }
  38. explicit Relation(ByteString name, Relation* parent = nullptr)
  39. : Relation(move(name), 0, parent)
  40. {
  41. }
  42. private:
  43. ByteString m_name;
  44. Block::Index m_block_index { 0 };
  45. Relation const* m_parent { nullptr };
  46. };
  47. class SchemaDef : public Relation {
  48. public:
  49. static ErrorOr<NonnullRefPtr<SchemaDef>> create(ByteString name);
  50. static ErrorOr<NonnullRefPtr<SchemaDef>> create(Key const&);
  51. Key key() const override;
  52. static NonnullRefPtr<IndexDef> index_def();
  53. static Key make_key();
  54. private:
  55. explicit SchemaDef(ByteString);
  56. };
  57. class ColumnDef : public Relation {
  58. public:
  59. static ErrorOr<NonnullRefPtr<ColumnDef>> create(Relation*, size_t, ByteString, SQLType);
  60. Key key() const override;
  61. SQLType type() const { return m_type; }
  62. size_t column_number() const { return m_index; }
  63. void set_not_null(bool can_not_be_null) { m_not_null = can_not_be_null; }
  64. bool not_null() const { return m_not_null; }
  65. void set_default_value(Value const& default_value);
  66. Value const& default_value() const { return m_default; }
  67. static NonnullRefPtr<IndexDef> index_def();
  68. static Key make_key(TableDef const&);
  69. protected:
  70. ColumnDef(Relation*, size_t, ByteString, SQLType);
  71. private:
  72. size_t m_index;
  73. SQLType m_type { SQLType::Text };
  74. bool m_not_null { false };
  75. Value m_default;
  76. };
  77. class KeyPartDef : public ColumnDef {
  78. public:
  79. static ErrorOr<NonnullRefPtr<KeyPartDef>> create(IndexDef*, ByteString, SQLType, Order = Order::Ascending);
  80. Order sort_order() const { return m_sort_order; }
  81. private:
  82. KeyPartDef(IndexDef*, ByteString, SQLType, Order);
  83. Order m_sort_order { Order::Ascending };
  84. };
  85. class IndexDef : public Relation {
  86. public:
  87. static ErrorOr<NonnullRefPtr<IndexDef>> create(TableDef*, ByteString, bool unique = true, u32 pointer = 0);
  88. static ErrorOr<NonnullRefPtr<IndexDef>> create(ByteString, bool unique = true, u32 pointer = 0);
  89. Vector<NonnullRefPtr<KeyPartDef>> const& key_definition() const { return m_key_definition; }
  90. bool unique() const { return m_unique; }
  91. [[nodiscard]] size_t size() const { return m_key_definition.size(); }
  92. void append_column(ByteString, SQLType, Order = Order::Ascending);
  93. Key key() const override;
  94. [[nodiscard]] NonnullRefPtr<TupleDescriptor> to_tuple_descriptor() const;
  95. static NonnullRefPtr<IndexDef> index_def();
  96. static Key make_key(TableDef const& table_def);
  97. private:
  98. IndexDef(TableDef*, ByteString, bool unique, u32 pointer);
  99. Vector<NonnullRefPtr<KeyPartDef>> m_key_definition;
  100. bool m_unique { false };
  101. friend TableDef;
  102. };
  103. class TableDef : public Relation {
  104. public:
  105. static ErrorOr<NonnullRefPtr<TableDef>> create(SchemaDef*, ByteString);
  106. Key key() const override;
  107. void append_column(ByteString, 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*, ByteString);
  119. Vector<NonnullRefPtr<ColumnDef>> m_columns;
  120. Vector<NonnullRefPtr<IndexDef>> m_indexes;
  121. };
  122. }