diff --git a/Tests/LibSQL/TestSqlDatabase.cpp b/Tests/LibSQL/TestSqlDatabase.cpp index ab1226b65e..911e5c4839 100644 --- a/Tests/LibSQL/TestSqlDatabase.cpp +++ b/Tests/LibSQL/TestSqlDatabase.cpp @@ -20,7 +20,7 @@ static NonnullRefPtr setup_schema(SQL::Database& db) { - auto schema = SQL::SchemaDef::construct("TestSchema"); + auto schema = MUST(SQL::SchemaDef::create("TestSchema")); MUST(db.add_schema(schema)); return schema; } @@ -29,7 +29,7 @@ static NonnullRefPtr setup_schema(SQL::Database& db) static NonnullRefPtr setup_table(SQL::Database& 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("IntColumn", SQL::SQLType::Integer); EXPECT_EQ(table->num_columns(), 2u); diff --git a/Userland/Libraries/LibSQL/AST/CreateSchema.cpp b/Userland/Libraries/LibSQL/AST/CreateSchema.cpp index 5f60f009a0..cef8cf45d9 100644 --- a/Userland/Libraries/LibSQL/AST/CreateSchema.cpp +++ b/Userland/Libraries/LibSQL/AST/CreateSchema.cpp @@ -12,7 +12,7 @@ namespace SQL::AST { ResultOr 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 (result.error().error() != SQLErrorCode::SchemaExists || m_is_error_if_schema_exists) diff --git a/Userland/Libraries/LibSQL/AST/CreateTable.cpp b/Userland/Libraries/LibSQL/AST/CreateTable.cpp index 384574463d..a5f383c691 100644 --- a/Userland/Libraries/LibSQL/AST/CreateTable.cpp +++ b/Userland/Libraries/LibSQL/AST/CreateTable.cpp @@ -12,7 +12,7 @@ namespace SQL::AST { ResultOr CreateTable::execute(ExecutionContext& context) const { 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) { SQLType type; diff --git a/Userland/Libraries/LibSQL/Database.cpp b/Userland/Libraries/LibSQL/Database.cpp index 123f0b641c..d567f3918c 100644 --- a/Userland/Libraries/LibSQL/Database.cpp +++ b/Userland/Libraries/LibSQL/Database.cpp @@ -54,7 +54,7 @@ ResultOr Database::open() if (result.error().error() != SQLErrorCode::SchemaDoesNotExist) return result.release_error(); - auto schema_def = SchemaDef::construct(schema_name); + auto schema_def = TRY(SchemaDef::create(schema_name)); TRY(add_schema(*schema_def)); return schema_def; } else { @@ -69,7 +69,7 @@ ResultOr Database::open() if (result.error().error() != SQLErrorCode::TableDoesNotExist) 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("Type", SQLType::Text); TRY(add_table(*internal_describe_table)); @@ -119,7 +119,7 @@ ResultOr> Database::get_schema(DeprecatedString const& if (schema_iterator.is_end() || (*schema_iterator != key)) 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); return schema_def; } @@ -163,7 +163,7 @@ ResultOr> Database::get_table(DeprecatedString const& sc return Result { SQLCommand::Unknown, SQLErrorCode::TableDoesNotExist, DeprecatedString::formatted("{}.{}", schema_name, name) }; 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()); m_table_cache.set(key.hash(), table_def); diff --git a/Userland/Libraries/LibSQL/Meta.cpp b/Userland/Libraries/LibSQL/Meta.cpp index 39473af9b1..0fa0770693 100644 --- a/Userland/Libraries/LibSQL/Meta.cpp +++ b/Userland/Libraries/LibSQL/Meta.cpp @@ -15,13 +15,18 @@ u32 Relation::hash() const return key().hash(); } -SchemaDef::SchemaDef(DeprecatedString name) - : Relation(move(name)) +ErrorOr> SchemaDef::create(DeprecatedString name) { + return adopt_nonnull_ref_or_enomem(new (nothrow) SchemaDef(move(name))); } -SchemaDef::SchemaDef(Key const& key) - : Relation(key["schema_name"].to_deprecated_string()) +ErrorOr> SchemaDef::create(Key const& key) +{ + return create(key["schema_name"].to_deprecated_string()); +} + +SchemaDef::SchemaDef(DeprecatedString name) + : Relation(move(name)) { } @@ -40,13 +45,18 @@ Key SchemaDef::make_key() NonnullRefPtr SchemaDef::index_def() { - NonnullRefPtr s_index_def = IndexDef::construct("$schema", true, 0); + NonnullRefPtr s_index_def = IndexDef::create("$schema", true, 0).release_value_but_fixme_should_propagate_errors(); if (!s_index_def->size()) { s_index_def->append_column("schema_name", SQLType::Text, Order::Ascending); } return s_index_def; } +ErrorOr> 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) : Relation(move(name), parent) , m_index(column_number) @@ -58,7 +68,7 @@ ColumnDef::ColumnDef(Relation* parent, size_t column_number, DeprecatedString na Key ColumnDef::key() const { auto key = Key(index_def()); - key["table_hash"] = parent_relation()->hash(); + key["table_hash"] = parent()->hash(); key["column_number"] = column_number(); key["column_name"] = name(); key["column_type"] = to_underlying(type()); @@ -80,7 +90,7 @@ Key ColumnDef::make_key(TableDef const& table_def) NonnullRefPtr ColumnDef::index_def() { - NonnullRefPtr s_index_def = IndexDef::construct("$column", true, 0); + NonnullRefPtr s_index_def = IndexDef::create("$column", true, 0).release_value_but_fixme_should_propagate_errors(); if (!s_index_def->size()) { s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending); s_index_def->append_column("column_number", SQLType::Integer, Order::Ascending); @@ -90,12 +100,27 @@ NonnullRefPtr ColumnDef::index_def() return s_index_def; } +ErrorOr> 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) : ColumnDef(index, index->size(), move(name), sql_type) , m_sort_order(sort_order) { } +ErrorOr> 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> 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) : Relation(move(name), pointer, table) , 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) { - 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); } @@ -126,7 +146,7 @@ NonnullRefPtr IndexDef::to_tuple_descriptor() const Key IndexDef::key() const { 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["unique"] = unique() ? 1 : 0; return key; @@ -141,7 +161,7 @@ Key IndexDef::make_key(TableDef const& table_def) NonnullRefPtr IndexDef::index_def() { - NonnullRefPtr s_index_def = IndexDef::construct("$index", true, 0); + NonnullRefPtr s_index_def = IndexDef::create("$index", true, 0).release_value_but_fixme_should_propagate_errors(); if (!s_index_def->size()) { s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending); s_index_def->append_column("index_name", SQLType::Text, Order::Ascending); @@ -150,6 +170,11 @@ NonnullRefPtr IndexDef::index_def() return s_index_def; } +ErrorOr> TableDef::create(SchemaDef* schema, DeprecatedString name) +{ + return adopt_nonnull_ref_or_enomem(new (nothrow) TableDef(schema, move(name))); +} + TableDef::TableDef(SchemaDef* schema, DeprecatedString name) : Relation(move(name), schema) , m_columns() @@ -169,7 +194,7 @@ NonnullRefPtr TableDef::to_tuple_descriptor() const Key TableDef::key() const { 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.set_block_index(block_index()); return key; @@ -177,7 +202,7 @@ Key TableDef::key() const 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); } @@ -203,7 +228,7 @@ Key TableDef::make_key(Key const& schema_key) NonnullRefPtr TableDef::index_def() { - NonnullRefPtr s_index_def = IndexDef::construct("$table", true, 0); + NonnullRefPtr s_index_def = IndexDef::create("$table", true, 0).release_value_but_fixme_should_propagate_errors(); if (!s_index_def->size()) { s_index_def->append_column("schema_hash", SQLType::Integer, Order::Ascending); s_index_def->append_column("table_name", SQLType::Text, Order::Ascending); diff --git a/Userland/Libraries/LibSQL/Meta.h b/Userland/Libraries/LibSQL/Meta.h index 5996039182..7a51ebb475 100644 --- a/Userland/Libraries/LibSQL/Meta.h +++ b/Userland/Libraries/LibSQL/Meta.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -23,53 +24,54 @@ namespace SQL { * It remains to be seen if this will survive in it's current form. */ -class Relation : public Core::EventReceiver { - C_OBJECT_ABSTRACT(Relation); - +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; } - ~Relation() override = default; virtual Key key() const = 0; - Relation const* parent_relation() const { return dynamic_cast(parent()); } protected: Relation(DeprecatedString name, Block::Index block_index, Relation* parent = nullptr) - : Core::EventReceiver(parent) + : m_name(move(name)) , m_block_index(block_index) + , m_parent(parent) { - set_name(move(name)); } explicit Relation(DeprecatedString name, Relation* parent = nullptr) - : Core::EventReceiver(parent) - , m_block_index(0) + : Relation(move(name), 0, parent) { - set_name(move(name)); } private: + DeprecatedString m_name; Block::Index m_block_index { 0 }; + Relation const* m_parent { nullptr }; }; class SchemaDef : public Relation { - C_OBJECT(SchemaDef); - 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); - explicit SchemaDef(Key const&); }; class ColumnDef : public Relation { - C_OBJECT(ColumnDef); - 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; } @@ -92,22 +94,21 @@ private: }; class KeyPartDef : public ColumnDef { - C_OBJECT(KeyPartDef); - 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::Ascending); + KeyPartDef(IndexDef*, DeprecatedString, SQLType, Order); Order m_sort_order { Order::Ascending }; }; class IndexDef : public Relation { - C_OBJECT(IndexDef); - public: - ~IndexDef() override = default; + 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; } @@ -119,8 +120,7 @@ public: static Key make_key(TableDef const& table_def); private: - IndexDef(TableDef*, DeprecatedString, bool unique = true, u32 pointer = 0); - explicit IndexDef(DeprecatedString, bool unique = true, u32 pointer = 0); + IndexDef(TableDef*, DeprecatedString, bool unique, u32 pointer); Vector> m_key_definition; bool m_unique { false }; @@ -129,9 +129,9 @@ private: }; class TableDef : public Relation { - C_OBJECT(TableDef); - public: + static ErrorOr> create(SchemaDef*, DeprecatedString); + Key key() const override; void append_column(DeprecatedString, SQLType); void append_column(Key const&);