1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 23:37:43 +00:00

LibSQL: Remove Core::EventReceiver parent from SQL::Relation

This relationship was only used to provide a name, factory methods, and
parent-child relationships for the relations.
This commit is contained in:
Timothy Flynn 2023-08-07 11:58:03 -04:00 committed by Tim Flynn
parent 5ad78cab8d
commit 3ea4c56d04
6 changed files with 76 additions and 51 deletions

View file

@ -20,7 +20,7 @@
static NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database& db) static NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database& db)
{ {
auto schema = SQL::SchemaDef::construct("TestSchema"); auto schema = MUST(SQL::SchemaDef::create("TestSchema"));
MUST(db.add_schema(schema)); MUST(db.add_schema(schema));
return schema; return schema;
} }
@ -29,7 +29,7 @@ static NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database& db)
static NonnullRefPtr<SQL::TableDef> setup_table(SQL::Database& db) static NonnullRefPtr<SQL::TableDef> setup_table(SQL::Database& db)
{ {
auto schema = setup_schema(db); auto schema = setup_schema(db);
auto table = SQL::TableDef::construct(schema, "TestTable"); auto table = MUST(SQL::TableDef::create(schema, "TestTable"));
table->append_column("TextColumn", SQL::SQLType::Text); table->append_column("TextColumn", SQL::SQLType::Text);
table->append_column("IntColumn", SQL::SQLType::Integer); table->append_column("IntColumn", SQL::SQLType::Integer);
EXPECT_EQ(table->num_columns(), 2u); EXPECT_EQ(table->num_columns(), 2u);

View file

@ -12,7 +12,7 @@ namespace SQL::AST {
ResultOr<ResultSet> CreateSchema::execute(ExecutionContext& context) const ResultOr<ResultSet> CreateSchema::execute(ExecutionContext& context) const
{ {
auto schema_def = SchemaDef::construct(m_schema_name); auto schema_def = TRY(SchemaDef::create(m_schema_name));
if (auto result = context.database->add_schema(*schema_def); result.is_error()) { if (auto result = context.database->add_schema(*schema_def); result.is_error()) {
if (result.error().error() != SQLErrorCode::SchemaExists || m_is_error_if_schema_exists) if (result.error().error() != SQLErrorCode::SchemaExists || m_is_error_if_schema_exists)

View file

@ -12,7 +12,7 @@ namespace SQL::AST {
ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const
{ {
auto schema_def = TRY(context.database->get_schema(m_schema_name)); auto schema_def = TRY(context.database->get_schema(m_schema_name));
auto table_def = TableDef::construct(schema_def, m_table_name); auto table_def = TRY(TableDef::create(schema_def, m_table_name));
for (auto const& column : m_columns) { for (auto const& column : m_columns) {
SQLType type; SQLType type;

View file

@ -54,7 +54,7 @@ ResultOr<void> Database::open()
if (result.error().error() != SQLErrorCode::SchemaDoesNotExist) if (result.error().error() != SQLErrorCode::SchemaDoesNotExist)
return result.release_error(); return result.release_error();
auto schema_def = SchemaDef::construct(schema_name); auto schema_def = TRY(SchemaDef::create(schema_name));
TRY(add_schema(*schema_def)); TRY(add_schema(*schema_def));
return schema_def; return schema_def;
} else { } else {
@ -69,7 +69,7 @@ ResultOr<void> Database::open()
if (result.error().error() != SQLErrorCode::TableDoesNotExist) if (result.error().error() != SQLErrorCode::TableDoesNotExist)
return result.release_error(); return result.release_error();
auto internal_describe_table = TableDef::construct(master_schema, "internal_describe_table"); auto internal_describe_table = TRY(TableDef::create(master_schema, "internal_describe_table"));
internal_describe_table->append_column("Name", SQLType::Text); internal_describe_table->append_column("Name", SQLType::Text);
internal_describe_table->append_column("Type", SQLType::Text); internal_describe_table->append_column("Type", SQLType::Text);
TRY(add_table(*internal_describe_table)); TRY(add_table(*internal_describe_table));
@ -119,7 +119,7 @@ ResultOr<NonnullRefPtr<SchemaDef>> Database::get_schema(DeprecatedString const&
if (schema_iterator.is_end() || (*schema_iterator != key)) if (schema_iterator.is_end() || (*schema_iterator != key))
return Result { SQLCommand::Unknown, SQLErrorCode::SchemaDoesNotExist, schema_name }; return Result { SQLCommand::Unknown, SQLErrorCode::SchemaDoesNotExist, schema_name };
auto schema_def = SchemaDef::construct(*schema_iterator); auto schema_def = TRY(SchemaDef::create(*schema_iterator));
m_schema_cache.set(key.hash(), schema_def); m_schema_cache.set(key.hash(), schema_def);
return schema_def; return schema_def;
} }
@ -163,7 +163,7 @@ ResultOr<NonnullRefPtr<TableDef>> Database::get_table(DeprecatedString const& sc
return Result { SQLCommand::Unknown, SQLErrorCode::TableDoesNotExist, DeprecatedString::formatted("{}.{}", schema_name, name) }; return Result { SQLCommand::Unknown, SQLErrorCode::TableDoesNotExist, DeprecatedString::formatted("{}.{}", schema_name, name) };
auto schema_def = TRY(get_schema(schema)); auto schema_def = TRY(get_schema(schema));
auto table_def = TableDef::construct(schema_def, name); auto table_def = TRY(TableDef::create(schema_def, name));
table_def->set_block_index((*table_iterator).block_index()); table_def->set_block_index((*table_iterator).block_index());
m_table_cache.set(key.hash(), table_def); m_table_cache.set(key.hash(), table_def);

View file

@ -15,13 +15,18 @@ u32 Relation::hash() const
return key().hash(); return key().hash();
} }
SchemaDef::SchemaDef(DeprecatedString name) ErrorOr<NonnullRefPtr<SchemaDef>> SchemaDef::create(DeprecatedString name)
: Relation(move(name))
{ {
return adopt_nonnull_ref_or_enomem(new (nothrow) SchemaDef(move(name)));
} }
SchemaDef::SchemaDef(Key const& key) ErrorOr<NonnullRefPtr<SchemaDef>> SchemaDef::create(Key const& key)
: Relation(key["schema_name"].to_deprecated_string()) {
return create(key["schema_name"].to_deprecated_string());
}
SchemaDef::SchemaDef(DeprecatedString name)
: Relation(move(name))
{ {
} }
@ -40,13 +45,18 @@ Key SchemaDef::make_key()
NonnullRefPtr<IndexDef> SchemaDef::index_def() NonnullRefPtr<IndexDef> SchemaDef::index_def()
{ {
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$schema", true, 0); NonnullRefPtr<IndexDef> s_index_def = IndexDef::create("$schema", true, 0).release_value_but_fixme_should_propagate_errors();
if (!s_index_def->size()) { if (!s_index_def->size()) {
s_index_def->append_column("schema_name", SQLType::Text, Order::Ascending); s_index_def->append_column("schema_name", SQLType::Text, Order::Ascending);
} }
return s_index_def; return s_index_def;
} }
ErrorOr<NonnullRefPtr<ColumnDef>> ColumnDef::create(Relation* parent, size_t column_number, DeprecatedString name, SQLType sql_type)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) ColumnDef(parent, column_number, move(name), sql_type));
}
ColumnDef::ColumnDef(Relation* parent, size_t column_number, DeprecatedString name, SQLType sql_type) ColumnDef::ColumnDef(Relation* parent, size_t column_number, DeprecatedString name, SQLType sql_type)
: Relation(move(name), parent) : Relation(move(name), parent)
, m_index(column_number) , m_index(column_number)
@ -58,7 +68,7 @@ ColumnDef::ColumnDef(Relation* parent, size_t column_number, DeprecatedString na
Key ColumnDef::key() const Key ColumnDef::key() const
{ {
auto key = Key(index_def()); auto key = Key(index_def());
key["table_hash"] = parent_relation()->hash(); key["table_hash"] = parent()->hash();
key["column_number"] = column_number(); key["column_number"] = column_number();
key["column_name"] = name(); key["column_name"] = name();
key["column_type"] = to_underlying(type()); key["column_type"] = to_underlying(type());
@ -80,7 +90,7 @@ Key ColumnDef::make_key(TableDef const& table_def)
NonnullRefPtr<IndexDef> ColumnDef::index_def() NonnullRefPtr<IndexDef> ColumnDef::index_def()
{ {
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$column", true, 0); NonnullRefPtr<IndexDef> s_index_def = IndexDef::create("$column", true, 0).release_value_but_fixme_should_propagate_errors();
if (!s_index_def->size()) { if (!s_index_def->size()) {
s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending); s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending);
s_index_def->append_column("column_number", SQLType::Integer, Order::Ascending); s_index_def->append_column("column_number", SQLType::Integer, Order::Ascending);
@ -90,12 +100,27 @@ NonnullRefPtr<IndexDef> ColumnDef::index_def()
return s_index_def; return s_index_def;
} }
ErrorOr<NonnullRefPtr<KeyPartDef>> KeyPartDef::create(IndexDef* index, DeprecatedString name, SQLType sql_type, Order sort_order)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) KeyPartDef(index, move(name), sql_type, sort_order));
}
KeyPartDef::KeyPartDef(IndexDef* index, DeprecatedString name, SQLType sql_type, Order sort_order) KeyPartDef::KeyPartDef(IndexDef* index, DeprecatedString name, SQLType sql_type, Order sort_order)
: ColumnDef(index, index->size(), move(name), sql_type) : ColumnDef(index, index->size(), move(name), sql_type)
, m_sort_order(sort_order) , m_sort_order(sort_order)
{ {
} }
ErrorOr<NonnullRefPtr<IndexDef>> IndexDef::create(TableDef* table, DeprecatedString name, bool unique, u32 pointer)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) IndexDef(table, move(name), unique, pointer));
}
ErrorOr<NonnullRefPtr<IndexDef>> IndexDef::create(DeprecatedString name, bool unique, u32 pointer)
{
return create(nullptr, move(name), unique, pointer);
}
IndexDef::IndexDef(TableDef* table, DeprecatedString name, bool unique, u32 pointer) IndexDef::IndexDef(TableDef* table, DeprecatedString name, bool unique, u32 pointer)
: Relation(move(name), pointer, table) : Relation(move(name), pointer, table)
, m_key_definition() , m_key_definition()
@ -103,14 +128,9 @@ IndexDef::IndexDef(TableDef* table, DeprecatedString name, bool unique, u32 poin
{ {
} }
IndexDef::IndexDef(DeprecatedString name, bool unique, u32 pointer)
: IndexDef(nullptr, move(name), unique, pointer)
{
}
void IndexDef::append_column(DeprecatedString name, SQLType sql_type, Order sort_order) void IndexDef::append_column(DeprecatedString name, SQLType sql_type, Order sort_order)
{ {
auto part = KeyPartDef::construct(this, move(name), sql_type, sort_order); auto part = KeyPartDef::create(this, move(name), sql_type, sort_order).release_value_but_fixme_should_propagate_errors();
m_key_definition.append(part); m_key_definition.append(part);
} }
@ -126,7 +146,7 @@ NonnullRefPtr<TupleDescriptor> IndexDef::to_tuple_descriptor() const
Key IndexDef::key() const Key IndexDef::key() const
{ {
auto key = Key(index_def()->to_tuple_descriptor()); auto key = Key(index_def()->to_tuple_descriptor());
key["table_hash"] = parent_relation()->key().hash(); key["table_hash"] = parent()->key().hash();
key["index_name"] = name(); key["index_name"] = name();
key["unique"] = unique() ? 1 : 0; key["unique"] = unique() ? 1 : 0;
return key; return key;
@ -141,7 +161,7 @@ Key IndexDef::make_key(TableDef const& table_def)
NonnullRefPtr<IndexDef> IndexDef::index_def() NonnullRefPtr<IndexDef> IndexDef::index_def()
{ {
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$index", true, 0); NonnullRefPtr<IndexDef> s_index_def = IndexDef::create("$index", true, 0).release_value_but_fixme_should_propagate_errors();
if (!s_index_def->size()) { if (!s_index_def->size()) {
s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending); s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending);
s_index_def->append_column("index_name", SQLType::Text, Order::Ascending); s_index_def->append_column("index_name", SQLType::Text, Order::Ascending);
@ -150,6 +170,11 @@ NonnullRefPtr<IndexDef> IndexDef::index_def()
return s_index_def; return s_index_def;
} }
ErrorOr<NonnullRefPtr<TableDef>> TableDef::create(SchemaDef* schema, DeprecatedString name)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) TableDef(schema, move(name)));
}
TableDef::TableDef(SchemaDef* schema, DeprecatedString name) TableDef::TableDef(SchemaDef* schema, DeprecatedString name)
: Relation(move(name), schema) : Relation(move(name), schema)
, m_columns() , m_columns()
@ -169,7 +194,7 @@ NonnullRefPtr<TupleDescriptor> TableDef::to_tuple_descriptor() const
Key TableDef::key() const Key TableDef::key() const
{ {
auto key = Key(index_def()->to_tuple_descriptor()); auto key = Key(index_def()->to_tuple_descriptor());
key["schema_hash"] = parent_relation()->key().hash(); key["schema_hash"] = parent()->key().hash();
key["table_name"] = name(); key["table_name"] = name();
key.set_block_index(block_index()); key.set_block_index(block_index());
return key; return key;
@ -177,7 +202,7 @@ Key TableDef::key() const
void TableDef::append_column(DeprecatedString name, SQLType sql_type) void TableDef::append_column(DeprecatedString name, SQLType sql_type)
{ {
auto column = ColumnDef::construct(this, num_columns(), move(name), sql_type); auto column = ColumnDef::create(this, num_columns(), move(name), sql_type).release_value_but_fixme_should_propagate_errors();
m_columns.append(column); m_columns.append(column);
} }
@ -203,7 +228,7 @@ Key TableDef::make_key(Key const& schema_key)
NonnullRefPtr<IndexDef> TableDef::index_def() NonnullRefPtr<IndexDef> TableDef::index_def()
{ {
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$table", true, 0); NonnullRefPtr<IndexDef> s_index_def = IndexDef::create("$table", true, 0).release_value_but_fixme_should_propagate_errors();
if (!s_index_def->size()) { if (!s_index_def->size()) {
s_index_def->append_column("schema_hash", SQLType::Integer, Order::Ascending); s_index_def->append_column("schema_hash", SQLType::Integer, Order::Ascending);
s_index_def->append_column("table_name", SQLType::Text, Order::Ascending); s_index_def->append_column("table_name", SQLType::Text, Order::Ascending);

View file

@ -8,6 +8,7 @@
#include <AK/DeprecatedString.h> #include <AK/DeprecatedString.h>
#include <AK/NonnullRefPtr.h> #include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <AK/Result.h> #include <AK/Result.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/EventReceiver.h> #include <LibCore/EventReceiver.h>
@ -23,53 +24,54 @@ namespace SQL {
* It remains to be seen if this will survive in it's current form. * It remains to be seen if this will survive in it's current form.
*/ */
class Relation : public Core::EventReceiver { class Relation : public RefCounted<Relation> {
C_OBJECT_ABSTRACT(Relation);
public: public:
virtual ~Relation() = default;
DeprecatedString const& name() const { return m_name; }
Relation const* parent() const { return m_parent; }
u32 hash() const; u32 hash() const;
Block::Index block_index() const { return m_block_index; } Block::Index block_index() const { return m_block_index; }
void set_block_index(Block::Index block_index) { m_block_index = block_index; } void set_block_index(Block::Index block_index) { m_block_index = block_index; }
~Relation() override = default;
virtual Key key() const = 0; virtual Key key() const = 0;
Relation const* parent_relation() const { return dynamic_cast<Relation const*>(parent()); }
protected: protected:
Relation(DeprecatedString name, Block::Index block_index, Relation* parent = nullptr) Relation(DeprecatedString name, Block::Index block_index, Relation* parent = nullptr)
: Core::EventReceiver(parent) : m_name(move(name))
, m_block_index(block_index) , m_block_index(block_index)
, m_parent(parent)
{ {
set_name(move(name));
} }
explicit Relation(DeprecatedString name, Relation* parent = nullptr) explicit Relation(DeprecatedString name, Relation* parent = nullptr)
: Core::EventReceiver(parent) : Relation(move(name), 0, parent)
, m_block_index(0)
{ {
set_name(move(name));
} }
private: private:
DeprecatedString m_name;
Block::Index m_block_index { 0 }; Block::Index m_block_index { 0 };
Relation const* m_parent { nullptr };
}; };
class SchemaDef : public Relation { class SchemaDef : public Relation {
C_OBJECT(SchemaDef);
public: public:
static ErrorOr<NonnullRefPtr<SchemaDef>> create(DeprecatedString name);
static ErrorOr<NonnullRefPtr<SchemaDef>> create(Key const&);
Key key() const override; Key key() const override;
static NonnullRefPtr<IndexDef> index_def(); static NonnullRefPtr<IndexDef> index_def();
static Key make_key(); static Key make_key();
private: private:
explicit SchemaDef(DeprecatedString); explicit SchemaDef(DeprecatedString);
explicit SchemaDef(Key const&);
}; };
class ColumnDef : public Relation { class ColumnDef : public Relation {
C_OBJECT(ColumnDef);
public: public:
static ErrorOr<NonnullRefPtr<ColumnDef>> create(Relation*, size_t, DeprecatedString, SQLType);
Key key() const override; Key key() const override;
SQLType type() const { return m_type; } SQLType type() const { return m_type; }
size_t column_number() const { return m_index; } size_t column_number() const { return m_index; }
@ -92,22 +94,21 @@ private:
}; };
class KeyPartDef : public ColumnDef { class KeyPartDef : public ColumnDef {
C_OBJECT(KeyPartDef);
public: public:
static ErrorOr<NonnullRefPtr<KeyPartDef>> create(IndexDef*, DeprecatedString, SQLType, Order = Order::Ascending);
Order sort_order() const { return m_sort_order; } Order sort_order() const { return m_sort_order; }
private: private:
KeyPartDef(IndexDef*, DeprecatedString, SQLType, Order = Order::Ascending); KeyPartDef(IndexDef*, DeprecatedString, SQLType, Order);
Order m_sort_order { Order::Ascending }; Order m_sort_order { Order::Ascending };
}; };
class IndexDef : public Relation { class IndexDef : public Relation {
C_OBJECT(IndexDef);
public: public:
~IndexDef() override = default; static ErrorOr<NonnullRefPtr<IndexDef>> create(TableDef*, DeprecatedString, bool unique = true, u32 pointer = 0);
static ErrorOr<NonnullRefPtr<IndexDef>> create(DeprecatedString, bool unique = true, u32 pointer = 0);
Vector<NonnullRefPtr<KeyPartDef>> const& key_definition() const { return m_key_definition; } Vector<NonnullRefPtr<KeyPartDef>> const& key_definition() const { return m_key_definition; }
bool unique() const { return m_unique; } bool unique() const { return m_unique; }
@ -119,8 +120,7 @@ public:
static Key make_key(TableDef const& table_def); static Key make_key(TableDef const& table_def);
private: private:
IndexDef(TableDef*, DeprecatedString, bool unique = true, u32 pointer = 0); IndexDef(TableDef*, DeprecatedString, bool unique, u32 pointer);
explicit IndexDef(DeprecatedString, bool unique = true, u32 pointer = 0);
Vector<NonnullRefPtr<KeyPartDef>> m_key_definition; Vector<NonnullRefPtr<KeyPartDef>> m_key_definition;
bool m_unique { false }; bool m_unique { false };
@ -129,9 +129,9 @@ private:
}; };
class TableDef : public Relation { class TableDef : public Relation {
C_OBJECT(TableDef);
public: public:
static ErrorOr<NonnullRefPtr<TableDef>> create(SchemaDef*, DeprecatedString);
Key key() const override; Key key() const override;
void append_column(DeprecatedString, SQLType); void append_column(DeprecatedString, SQLType);
void append_column(Key const&); void append_column(Key const&);