summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/AST
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-11-29 08:47:22 -0500
committerLinus Groh <mail@linusgroh.de>2022-11-30 11:43:13 +0100
commit4b70908dc4c249f8b292b8a617afbc6adb824a4f (patch)
tree2b62766f3c786e781cc086aba1fb4a322fc50379 /Userland/Libraries/LibSQL/AST
parent56843baff928a31e5b4520e38b36dd0de35acd9f (diff)
downloadserenity-4b70908dc4c249f8b292b8a617afbc6adb824a4f.zip
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.
Diffstat (limited to 'Userland/Libraries/LibSQL/AST')
-rw-r--r--Userland/Libraries/LibSQL/AST/CreateTable.cpp17
-rw-r--r--Userland/Libraries/LibSQL/AST/Describe.cpp10
-rw-r--r--Userland/Libraries/LibSQL/AST/Insert.cpp5
-rw-r--r--Userland/Libraries/LibSQL/AST/Select.cpp2
4 files changed, 9 insertions, 25 deletions
diff --git a/Userland/Libraries/LibSQL/AST/CreateTable.cpp b/Userland/Libraries/LibSQL/AST/CreateTable.cpp
index 2312e0f23d..a40409ee53 100644
--- a/Userland/Libraries/LibSQL/AST/CreateTable.cpp
+++ b/Userland/Libraries/LibSQL/AST/CreateTable.cpp
@@ -12,16 +12,9 @@ namespace SQL::AST {
ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const
{
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));
- 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);
+ auto table_def = TableDef::construct(schema_def, m_table_name);
- for (auto& column : m_columns) {
+ for (auto const& column : m_columns) {
SQLType type;
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);
}
- 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 };
}
diff --git a/Userland/Libraries/LibSQL/AST/Describe.cpp b/Userland/Libraries/LibSQL/AST/Describe.cpp
index 180243b782..8cc6dfcba8 100644
--- a/Userland/Libraries/LibSQL/AST/Describe.cpp
+++ b/Userland/Libraries/LibSQL/AST/Describe.cpp
@@ -14,15 +14,9 @@ namespace SQL::AST {
ResultOr<ResultSet> DescribeTable::execute(ExecutionContext& context) const
{
- auto schema_name = m_qualified_table_name->schema_name();
- auto table_name = m_qualified_table_name->table_name();
-
+ auto const& schema_name = m_qualified_table_name->schema_name();
+ auto const& table_name = m_qualified_table_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 descriptor = describe_table_def->to_tuple_descriptor();
diff --git a/Userland/Libraries/LibSQL/AST/Insert.cpp b/Userland/Libraries/LibSQL/AST/Insert.cpp
index 0c9cfb4fa9..52fde78402 100644
--- a/Userland/Libraries/LibSQL/AST/Insert.cpp
+++ b/Userland/Libraries/LibSQL/AST/Insert.cpp
@@ -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));
- 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);
for (auto& column : m_column_names) {
if (!row.has(column))
diff --git a/Userland/Libraries/LibSQL/AST/Select.cpp b/Userland/Libraries/LibSQL/AST/Select.cpp
index 663634c0a2..8dfa6a93cc 100644
--- a/Userland/Libraries/LibSQL/AST/Select.cpp
+++ b/Userland/Libraries/LibSQL/AST/Select.cpp
@@ -24,8 +24,6 @@ ResultOr<ResultSet> Select::execute(ExecutionContext& context) const
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()));
- 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) {
for (auto& col : table_def->columns()) {