1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:57:35 +00:00

LibSQL: Implement a DESCRIBE TABLE statement

This statement (for now) outputs the name and types of the different
attributes in a table. It's not standard SQL but all DBMSs that I know
of implement a sort of statement for such functionality.

Since the output of DESCRIBE TABLE is just a relation, an internal
schema, `master` was created and a table definition for DESCRIBE into
it. The table definition and the master schema are not accessible by the
user.
This commit is contained in:
Mahmoud Mandour 2021-12-06 15:30:38 +02:00 committed by Andreas Kling
parent cd4dba87fa
commit f6233913ad
9 changed files with 93 additions and 0 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -1050,4 +1051,18 @@ private:
RefPtr<LimitClause> m_limit_clause;
};
class DescribeTable : public Statement {
public:
DescribeTable(NonnullRefPtr<QualifiedTableName> qualified_table_name)
: m_qualified_table_name(move(qualified_table_name))
{
}
NonnullRefPtr<QualifiedTableName> qualified_table_name() const { return m_qualified_table_name; }
RefPtr<SQLResult> execute(ExecutionContext&) const override;
private:
NonnullRefPtr<QualifiedTableName> m_qualified_table_name;
};
}

View file

@ -0,0 +1,42 @@
/*
* 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/Row.h>
namespace SQL::AST {
RefPtr<SQLResult> 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_or_error = context.database->get_table(schema_name, table_name);
auto table_def = table_def_or_error.release_value();
if (!table_def) {
if (schema_name.is_null() || schema_name.is_empty())
schema_name = "default";
return SQLResult::construct(SQLCommand::Describe, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, table_name));
}
auto describe_table_def = context.database->get_table("master", "internal_describe_table").value();
NonnullRefPtr<TupleDescriptor> descriptor = describe_table_def->to_tuple_descriptor();
context.result = SQLResult::construct();
for (auto& column : table_def->columns()) {
Tuple tuple(descriptor);
tuple[0] = column.name();
tuple[1] = SQLType_name(column.type());
context.result->insert(tuple, Tuple {});
}
return context.result;
}
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -46,6 +47,8 @@ NonnullRefPtr<Statement> Parser::parse_statement()
return parse_alter_table_statement();
case TokenType::Drop:
return parse_drop_table_statement();
case TokenType::Describe:
return parse_describe_table_statement();
case TokenType::Insert:
return parse_insert_statement({});
case TokenType::Update:
@ -181,6 +184,16 @@ NonnullRefPtr<DropTable> Parser::parse_drop_table_statement()
return create_ast_node<DropTable>(move(schema_name), move(table_name), is_error_if_table_does_not_exist);
}
NonnullRefPtr<DescribeTable> Parser::parse_describe_table_statement()
{
consume(TokenType::Describe);
consume(TokenType::Table);
auto table_name = parse_qualified_table_name();
return create_ast_node<DescribeTable>(move(table_name));
}
NonnullRefPtr<Insert> Parser::parse_insert_statement(RefPtr<CommonTableExpressionList> common_table_expression_list)
{
// https://sqlite.org/lang_insert.html

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -59,6 +60,7 @@ private:
NonnullRefPtr<CreateTable> parse_create_table_statement();
NonnullRefPtr<AlterTable> parse_alter_table_statement();
NonnullRefPtr<DropTable> parse_drop_table_statement();
NonnullRefPtr<DescribeTable> parse_describe_table_statement();
NonnullRefPtr<Insert> parse_insert_statement(RefPtr<CommonTableExpressionList>);
NonnullRefPtr<Update> parse_update_statement(RefPtr<CommonTableExpressionList>);
NonnullRefPtr<Delete> parse_delete_statement(RefPtr<CommonTableExpressionList>);

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
* Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -53,6 +54,7 @@ namespace SQL::AST {
__ENUMERATE_SQL_TOKEN("DEFERRED", Deferred, Keyword) \
__ENUMERATE_SQL_TOKEN("DELETE", Delete, Keyword) \
__ENUMERATE_SQL_TOKEN("DESC", Desc, Keyword) \
__ENUMERATE_SQL_TOKEN("DESCRIBE", Describe, Keyword) \
__ENUMERATE_SQL_TOKEN("DETACH", Detach, Keyword) \
__ENUMERATE_SQL_TOKEN("DISTINCT", Distinct, Keyword) \
__ENUMERATE_SQL_TOKEN("DO", Do, Keyword) \