Meta.h 4.1 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/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. 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, String, 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. C_OBJECT(KeyPartDef);
  79. public:
  80. KeyPartDef(IndexDef*, String, SQLType, Order = Order::Ascending);
  81. Order sort_order() const { return m_sort_order; }
  82. private:
  83. Order m_sort_order { Order::Ascending };
  84. };
  85. class IndexDef : public Relation {
  86. C_OBJECT(IndexDef);
  87. public:
  88. ~IndexDef() override = default;
  89. NonnullRefPtrVector<KeyPartDef> 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(String, 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*, String, bool unique = true, u32 pointer = 0);
  99. explicit IndexDef(String, bool unique = true, u32 pointer = 0);
  100. NonnullRefPtrVector<KeyPartDef> m_key_definition;
  101. bool m_unique { false };
  102. friend TableDef;
  103. };
  104. class TableDef : public Relation {
  105. C_OBJECT(TableDef);
  106. public:
  107. Key key() const override;
  108. void append_column(String, SQLType);
  109. void append_column(Key const&);
  110. size_t num_columns() { return m_columns.size(); }
  111. size_t num_indexes() { return m_indexes.size(); }
  112. NonnullRefPtrVector<ColumnDef> columns() const { return m_columns; }
  113. NonnullRefPtrVector<IndexDef> indexes() const { return m_indexes; }
  114. [[nodiscard]] NonnullRefPtr<TupleDescriptor> to_tuple_descriptor() const;
  115. static NonnullRefPtr<IndexDef> index_def();
  116. static Key make_key(SchemaDef const& schema_def);
  117. static Key make_key(Key const& schema_key);
  118. private:
  119. explicit TableDef(SchemaDef*, String);
  120. NonnullRefPtrVector<ColumnDef> m_columns;
  121. NonnullRefPtrVector<IndexDef> m_indexes;
  122. };
  123. }