/* * Copyright (c) 2021, Jan de Visser * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include namespace SQL { /** * This file declares objects describing tables, indexes, and columns. * It remains to be seen if this will survive in it's current form. */ class Relation : public RefCounted { public: virtual ~Relation() = default; DeprecatedString const& name() const { return m_name; } Relation const* parent() const { return m_parent; } u32 hash() const; Block::Index block_index() const { return m_block_index; } void set_block_index(Block::Index block_index) { m_block_index = block_index; } virtual Key key() const = 0; protected: Relation(DeprecatedString name, Block::Index block_index, Relation* parent = nullptr) : m_name(move(name)) , m_block_index(block_index) , m_parent(parent) { } explicit Relation(DeprecatedString name, Relation* parent = nullptr) : Relation(move(name), 0, parent) { } private: DeprecatedString m_name; Block::Index m_block_index { 0 }; Relation const* m_parent { nullptr }; }; class SchemaDef : public Relation { public: static ErrorOr> create(DeprecatedString name); static ErrorOr> create(Key const&); Key key() const override; static NonnullRefPtr index_def(); static Key make_key(); private: explicit SchemaDef(DeprecatedString); }; class ColumnDef : public Relation { public: static ErrorOr> create(Relation*, size_t, DeprecatedString, SQLType); Key key() const override; SQLType type() const { return m_type; } size_t column_number() const { return m_index; } void set_not_null(bool can_not_be_null) { m_not_null = can_not_be_null; } bool not_null() const { return m_not_null; } void set_default_value(Value const& default_value); Value const& default_value() const { return m_default; } static NonnullRefPtr index_def(); static Key make_key(TableDef const&); protected: ColumnDef(Relation*, size_t, DeprecatedString, SQLType); private: size_t m_index; SQLType m_type { SQLType::Text }; bool m_not_null { false }; Value m_default; }; class KeyPartDef : public ColumnDef { public: static ErrorOr> create(IndexDef*, DeprecatedString, SQLType, Order = Order::Ascending); Order sort_order() const { return m_sort_order; } private: KeyPartDef(IndexDef*, DeprecatedString, SQLType, Order); Order m_sort_order { Order::Ascending }; }; class IndexDef : public Relation { public: static ErrorOr> create(TableDef*, DeprecatedString, bool unique = true, u32 pointer = 0); static ErrorOr> create(DeprecatedString, bool unique = true, u32 pointer = 0); Vector> const& key_definition() const { return m_key_definition; } bool unique() const { return m_unique; } [[nodiscard]] size_t size() const { return m_key_definition.size(); } void append_column(DeprecatedString, SQLType, Order = Order::Ascending); Key key() const override; [[nodiscard]] NonnullRefPtr to_tuple_descriptor() const; static NonnullRefPtr index_def(); static Key make_key(TableDef const& table_def); private: IndexDef(TableDef*, DeprecatedString, bool unique, u32 pointer); Vector> m_key_definition; bool m_unique { false }; friend TableDef; }; class TableDef : public Relation { public: static ErrorOr> create(SchemaDef*, DeprecatedString); Key key() const override; void append_column(DeprecatedString, SQLType); void append_column(Key const&); size_t num_columns() { return m_columns.size(); } size_t num_indexes() { return m_indexes.size(); } Vector> const& columns() const { return m_columns; } Vector> const& indexes() const { return m_indexes; } [[nodiscard]] NonnullRefPtr to_tuple_descriptor() const; static NonnullRefPtr index_def(); static Key make_key(SchemaDef const& schema_def); static Key make_key(Key const& schema_key); private: explicit TableDef(SchemaDef*, DeprecatedString); Vector> m_columns; Vector> m_indexes; }; }