1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 03:18:11 +00:00

LibSQL+SQLServer: Return a NonnullRefPtr from Database::get_table

Database::get_table currently either returns a RefPtr to an existing
table, a nullptr if the table doesn't exist, or an Error if some
internal error occured. Change this to return a NonnullRefPtr to an
exisiting table, or a SQL::Result with any error, including if the
table was not found. Callers can then handle that specific error code
if they want.

Returning a NonnullRefPtr will enable some further cleanup. This had
some fallout of needing to change some other methods' return types from
AK::ErrorOr to SQL::Result so that TRY may continue to be used.
This commit is contained in:
Timothy Flynn 2022-11-29 08:47:22 -05:00 committed by Linus Groh
parent 56843baff9
commit 4b70908dc4
8 changed files with 56 additions and 79 deletions

View file

@ -44,10 +44,7 @@ NonnullRefPtr<SQL::TableDef> setup_table(SQL::Database& db)
void insert_into_table(SQL::Database& db, int count) void insert_into_table(SQL::Database& db, int count)
{ {
auto table_or_error = db.get_table("TestSchema", "TestTable"); auto table = MUST(db.get_table("TestSchema", "TestTable"));
EXPECT(!table_or_error.is_error());
auto table = table_or_error.value();
EXPECT(table);
for (int ix = 0; ix < count; ix++) { for (int ix = 0; ix < count; ix++) {
SQL::Row row(*table); SQL::Row row(*table);
@ -63,10 +60,7 @@ void insert_into_table(SQL::Database& db, int count)
void verify_table_contents(SQL::Database& db, int expected_count) void verify_table_contents(SQL::Database& db, int expected_count)
{ {
auto table_or_error = db.get_table("TestSchema", "TestTable"); auto table = MUST(db.get_table("TestSchema", "TestTable"));
EXPECT(!table_or_error.is_error());
auto table = table_or_error.value();
EXPECT(table);
int sum = 0; int sum = 0;
int count = 0; int count = 0;
@ -196,10 +190,8 @@ TEST_CASE(get_table_from_database)
{ {
auto db = SQL::Database::construct("/tmp/test.db"); auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error()); EXPECT(!db->open().is_error());
auto table_or_error = db->get_table("TestSchema", "TestTable");
EXPECT(!table_or_error.is_error()); auto table = MUST(db->get_table("TestSchema", "TestTable"));
auto table = table_or_error.value();
EXPECT(table);
EXPECT_EQ(table->name(), "TestTable"); EXPECT_EQ(table->name(), "TestTable");
EXPECT_EQ(table->num_columns(), 2u); EXPECT_EQ(table->num_columns(), 2u);
} }

View file

@ -81,7 +81,6 @@ TEST_CASE(create_table)
create_table(database); create_table(database);
auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE"); auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE");
EXPECT(!table_or_error.is_error()); EXPECT(!table_or_error.is_error());
EXPECT(table_or_error.value());
} }
TEST_CASE(insert_into_table) TEST_CASE(insert_into_table)
@ -93,9 +92,7 @@ TEST_CASE(insert_into_table)
auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test', 42 );"); auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test', 42 );");
EXPECT(result.size() == 1); EXPECT(result.size() == 1);
auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE"); auto table = MUST(database->get_table("TESTSCHEMA", "TESTTABLE"));
EXPECT(!table_or_error.is_error());
auto table = table_or_error.value();
int count = 0; int count = 0;
auto rows_or_error = database->select_all(*table); auto rows_or_error = database->select_all(*table);
@ -172,9 +169,8 @@ TEST_CASE(insert_without_column_names)
auto result = execute(database, "INSERT INTO TestSchema.TestTable VALUES ('Test_1', 42), ('Test_2', 43);"); auto result = execute(database, "INSERT INTO TestSchema.TestTable VALUES ('Test_1', 42), ('Test_2', 43);");
EXPECT(result.size() == 2); EXPECT(result.size() == 2);
auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE"); auto table = MUST(database->get_table("TESTSCHEMA", "TESTTABLE"));
EXPECT(!table_or_error.is_error()); auto rows_or_error = database->select_all(*table);
auto rows_or_error = database->select_all(*(table_or_error.value()));
EXPECT(!rows_or_error.is_error()); EXPECT(!rows_or_error.is_error());
EXPECT_EQ(rows_or_error.value().size(), 2u); EXPECT_EQ(rows_or_error.value().size(), 2u);
} }

View file

@ -12,16 +12,9 @@ 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 = TRY(context.database->get_table(m_schema_name, m_table_name)); auto table_def = TableDef::construct(schema_def, m_table_name);
if (table_def) {
if (m_is_error_if_table_exists)
return Result { SQLCommand::Create, SQLErrorCode::TableExists, m_table_name };
return ResultSet { SQLCommand::Create };
}
table_def = TableDef::construct(schema_def, m_table_name); for (auto const& column : m_columns) {
for (auto& column : m_columns) {
SQLType type; SQLType type;
if (column.type_name()->name().is_one_of("VARCHAR"sv, "TEXT"sv)) if (column.type_name()->name().is_one_of("VARCHAR"sv, "TEXT"sv))
@ -38,7 +31,11 @@ ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const
table_def->append_column(column.name(), type); table_def->append_column(column.name(), type);
} }
TRY(context.database->add_table(*table_def)); if (auto result = context.database->add_table(*table_def); result.is_error()) {
if (result.error().error() != SQLErrorCode::TableExists || m_is_error_if_table_exists)
return result.release_error();
}
return ResultSet { SQLCommand::Create }; return ResultSet { SQLCommand::Create };
} }

View file

@ -14,15 +14,9 @@ namespace SQL::AST {
ResultOr<ResultSet> DescribeTable::execute(ExecutionContext& context) const ResultOr<ResultSet> DescribeTable::execute(ExecutionContext& context) const
{ {
auto schema_name = m_qualified_table_name->schema_name(); auto const& schema_name = m_qualified_table_name->schema_name();
auto table_name = m_qualified_table_name->table_name(); auto const& table_name = m_qualified_table_name->table_name();
auto table_def = TRY(context.database->get_table(schema_name, table_name)); auto table_def = TRY(context.database->get_table(schema_name, table_name));
if (!table_def) {
if (schema_name.is_empty())
schema_name = "default"sv;
return Result { SQLCommand::Describe, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, table_name) };
}
auto describe_table_def = MUST(context.database->get_table("master"sv, "internal_describe_table"sv)); auto describe_table_def = MUST(context.database->get_table("master"sv, "internal_describe_table"sv));
auto descriptor = describe_table_def->to_tuple_descriptor(); auto descriptor = describe_table_def->to_tuple_descriptor();

View file

@ -25,11 +25,6 @@ ResultOr<ResultSet> Insert::execute(ExecutionContext& context) const
{ {
auto table_def = TRY(context.database->get_table(m_schema_name, m_table_name)); auto table_def = TRY(context.database->get_table(m_schema_name, m_table_name));
if (!table_def) {
auto schema_name = m_schema_name.is_empty() ? String("default"sv) : m_schema_name;
return Result { SQLCommand::Insert, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, m_table_name) };
}
Row row(table_def); Row row(table_def);
for (auto& column : m_column_names) { for (auto& column : m_column_names) {
if (!row.has(column)) if (!row.has(column))

View file

@ -24,8 +24,6 @@ ResultOr<ResultSet> Select::execute(ExecutionContext& context) const
return Result { SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented"sv }; return Result { SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented"sv };
auto table_def = TRY(context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name())); auto table_def = TRY(context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name()));
if (!table_def)
return Result { SQLCommand::Select, SQLErrorCode::TableDoesNotExist, table_descriptor.table_name() };
if (result_column_list.size() == 1 && result_column_list[0].type() == ResultType::All) { if (result_column_list.size() == 1 && result_column_list[0].type() == ResultType::All) {
for (auto& col : table_def->columns()) { for (auto& col : table_def->columns()) {

View file

@ -61,12 +61,14 @@ ResultOr<void> Database::open()
(void)TRY(ensure_schema_exists("default"sv)); (void)TRY(ensure_schema_exists("default"sv));
auto master_schema = TRY(ensure_schema_exists("master"sv)); auto master_schema = TRY(ensure_schema_exists("master"sv));
auto table_def = TRY(get_table("master", "internal_describe_table")); if (auto result = get_table("master"sv, "internal_describe_table"sv); result.is_error()) {
if (!table_def) { if (result.error().error() != SQLErrorCode::TableDoesNotExist)
auto describe_internal_table = TableDef::construct(master_schema, "internal_describe_table"); return result.release_error();
describe_internal_table->append_column("Name", SQLType::Text);
describe_internal_table->append_column("Type", SQLType::Text); auto internal_describe_table = TableDef::construct(master_schema, "internal_describe_table");
TRY(add_table(*describe_internal_table)); internal_describe_table->append_column("Name", SQLType::Text);
internal_describe_table->append_column("Type", SQLType::Text);
TRY(add_table(*internal_describe_table));
} }
return {}; return {};
@ -118,16 +120,18 @@ ResultOr<NonnullRefPtr<SchemaDef>> Database::get_schema(String const& schema)
return schema_def; return schema_def;
} }
ErrorOr<void> Database::add_table(TableDef& table) ResultOr<void> Database::add_table(TableDef& table)
{ {
VERIFY(is_open()); VERIFY(is_open());
if (!m_tables->insert(table.key())) {
warnln("Duplicate table name '{}'.'{}'"sv, table.parent()->name(), table.name()); if (!m_tables->insert(table.key()))
return Error::from_string_literal("Duplicate table name"); return Result { SQLCommand::Unknown, SQLErrorCode::TableExists, table.name() };
}
for (auto& column : table.columns()) { for (auto& column : table.columns()) {
VERIFY(m_table_columns->insert(column.key())); if (!m_table_columns->insert(column.key()))
VERIFY_NOT_REACHED();
} }
return {}; return {};
} }
@ -138,32 +142,33 @@ Key Database::get_table_key(String const& schema_name, String const& table_name)
return key; return key;
} }
ResultOr<RefPtr<TableDef>> Database::get_table(String const& schema, String const& name) ResultOr<NonnullRefPtr<TableDef>> Database::get_table(String const& schema, String const& name)
{ {
VERIFY(is_open()); VERIFY(is_open());
auto schema_name = schema; auto schema_name = schema;
if (schema.is_null() || schema.is_empty()) if (schema.is_empty())
schema_name = "default"; schema_name = "default"sv;
Key key = get_table_key(schema_name, name); Key key = get_table_key(schema_name, name);
auto table_def_opt = m_table_cache.get(key.hash()); if (auto it = m_table_cache.find(key.hash()); it != m_table_cache.end())
if (table_def_opt.has_value()) return it->value;
return RefPtr<TableDef>(table_def_opt.value());
auto table_iterator = m_tables->find(key); auto table_iterator = m_tables->find(key);
if (table_iterator.is_end() || (*table_iterator != key)) { if (table_iterator.is_end() || (*table_iterator != key))
return RefPtr<TableDef>(nullptr); return Result { SQLCommand::Unknown, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, name) };
}
auto schema_def = TRY(get_schema(schema)); auto schema_def = TRY(get_schema(schema));
auto ret = TableDef::construct(schema_def, name); auto table_def = TableDef::construct(schema_def, name);
ret->set_pointer((*table_iterator).pointer()); table_def->set_pointer((*table_iterator).pointer());
m_table_cache.set(key.hash(), ret); m_table_cache.set(key.hash(), table_def);
auto hash = ret->hash();
auto column_key = ColumnDef::make_key(ret); auto table_hash = table_def->hash();
for (auto column_iterator = m_table_columns->find(column_key); auto column_key = ColumnDef::make_key(table_def);
!column_iterator.is_end() && ((*column_iterator)["table_hash"].to_u32().value() == hash); for (auto it = m_table_columns->find(column_key); !it.is_end() && ((*it)["table_hash"].to_u32().value() == table_hash); ++it)
column_iterator++) { table_def->append_column(*it);
ret->append_column(*column_iterator);
} return table_def;
return RefPtr<TableDef>(ret);
} }
ErrorOr<Vector<Row>> Database::select_all(TableDef const& table) ErrorOr<Vector<Row>> Database::select_all(TableDef const& table)

View file

@ -37,9 +37,9 @@ public:
static Key get_schema_key(String const&); static Key get_schema_key(String const&);
ResultOr<NonnullRefPtr<SchemaDef>> get_schema(String const&); ResultOr<NonnullRefPtr<SchemaDef>> get_schema(String const&);
ErrorOr<void> add_table(TableDef& table); ResultOr<void> add_table(TableDef& table);
static Key get_table_key(String const&, String const&); static Key get_table_key(String const&, String const&);
ResultOr<RefPtr<TableDef>> get_table(String const&, String const&); ResultOr<NonnullRefPtr<TableDef>> get_table(String const&, String const&);
ErrorOr<Vector<Row>> select_all(TableDef const&); ErrorOr<Vector<Row>> select_all(TableDef const&);
ErrorOr<Vector<Row>> match(TableDef const&, Key const&); ErrorOr<Vector<Row>> match(TableDef const&, Key const&);
@ -57,7 +57,7 @@ private:
RefPtr<BTree> m_table_columns; RefPtr<BTree> m_table_columns;
HashMap<u32, NonnullRefPtr<SchemaDef>> m_schema_cache; HashMap<u32, NonnullRefPtr<SchemaDef>> m_schema_cache;
HashMap<u32, RefPtr<TableDef>> m_table_cache; HashMap<u32, NonnullRefPtr<TableDef>> m_table_cache;
}; };
} }