mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-26 06:18:59 +00:00
The result of a SQL statement execution is either: 1. An error. 2. The list of rows inserted, deleted, selected, etc. (2) is currently represented by a combination of the Result class and the ResultSet list it holds. This worked okay, but issues start to arise when trying to use Result in non-statement contexts (for example, when introducing Result to SQL expression execution). What we really need is for Result to be a thin wrapper that represents both (1) and (2), and to not have any explicit members like a ResultSet. So this commit removes ResultSet from Result, and introduces ResultOr, which is just an alias for AK::ErrorOrr. Statement execution now returns ResultOr<ResultSet> instead of Result. This further opens the door for expression execution to return ResultOr<Value> in the future. Lastly, this moves some other context held by Result over to ResultSet. This includes the row count (which is really just the size of ResultSet) and the command for which the result is for.
44 lines
1.3 KiB
C++
44 lines
1.3 KiB
C++
/*
|
|
* Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibSQL/AST/AST.h>
|
|
#include <LibSQL/Database.h>
|
|
#include <LibSQL/Meta.h>
|
|
#include <LibSQL/ResultSet.h>
|
|
#include <LibSQL/Row.h>
|
|
|
|
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 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();
|
|
|
|
ResultSet result { SQLCommand::Describe };
|
|
TRY(result.try_ensure_capacity(table_def->columns().size()));
|
|
|
|
for (auto& column : table_def->columns()) {
|
|
Tuple tuple(descriptor);
|
|
tuple[0] = column.name();
|
|
tuple[1] = SQLType_name(column.type());
|
|
|
|
result.insert_row(tuple, Tuple {});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|