diff options
-rw-r--r-- | Userland/Libraries/LibSQL/AST/AST.h | 15 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/AST/Describe.cpp | 42 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/AST/Parser.cpp | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/AST/Parser.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/AST/Token.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/Database.cpp | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/Database.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/SQLResult.h | 1 |
9 files changed, 93 insertions, 0 deletions
diff --git a/Userland/Libraries/LibSQL/AST/AST.h b/Userland/Libraries/LibSQL/AST/AST.h index 1f284372f3..dd0e8c6dc5 100644 --- a/Userland/Libraries/LibSQL/AST/AST.h +++ b/Userland/Libraries/LibSQL/AST/AST.h @@ -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; +}; + } diff --git a/Userland/Libraries/LibSQL/AST/Describe.cpp b/Userland/Libraries/LibSQL/AST/Describe.cpp new file mode 100644 index 0000000000..fb6c8c1be4 --- /dev/null +++ b/Userland/Libraries/LibSQL/AST/Describe.cpp @@ -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; +} + +} diff --git a/Userland/Libraries/LibSQL/AST/Parser.cpp b/Userland/Libraries/LibSQL/AST/Parser.cpp index 585b5657bc..51abcf8d0b 100644 --- a/Userland/Libraries/LibSQL/AST/Parser.cpp +++ b/Userland/Libraries/LibSQL/AST/Parser.cpp @@ -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 diff --git a/Userland/Libraries/LibSQL/AST/Parser.h b/Userland/Libraries/LibSQL/AST/Parser.h index aff2a8815b..6a4db5695d 100644 --- a/Userland/Libraries/LibSQL/AST/Parser.h +++ b/Userland/Libraries/LibSQL/AST/Parser.h @@ -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>); diff --git a/Userland/Libraries/LibSQL/AST/Token.h b/Userland/Libraries/LibSQL/AST/Token.h index 5c58d6f98d..e51e5e8ff7 100644 --- a/Userland/Libraries/LibSQL/AST/Token.h +++ b/Userland/Libraries/LibSQL/AST/Token.h @@ -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) \ diff --git a/Userland/Libraries/LibSQL/CMakeLists.txt b/Userland/Libraries/LibSQL/CMakeLists.txt index 3abf929a3d..26e2795a02 100644 --- a/Userland/Libraries/LibSQL/CMakeLists.txt +++ b/Userland/Libraries/LibSQL/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES AST/CreateSchema.cpp AST/CreateTable.cpp + AST/Describe.cpp AST/Expression.cpp AST/Insert.cpp AST/Lexer.cpp diff --git a/Userland/Libraries/LibSQL/Database.cpp b/Userland/Libraries/LibSQL/Database.cpp index e0574f1d59..e4ed4960e6 100644 --- a/Userland/Libraries/LibSQL/Database.cpp +++ b/Userland/Libraries/LibSQL/Database.cpp @@ -1,5 +1,6 @@ /* * 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 */ @@ -47,6 +48,21 @@ ErrorOr<void> Database::open() default_schema = SchemaDef::construct("default"); TRY(add_schema(*default_schema)); } + + auto master_schema = TRY(get_schema("master")); + if (!master_schema) { + master_schema = SchemaDef::construct("master"); + TRY(add_schema(*master_schema)); + } + + auto table_def = TRY(get_table("master", "internal_describe_table")); + if (!table_def) { + auto describe_internal_table = TableDef::construct(master_schema, "internal_describe_table"); + describe_internal_table->append_column("Name", SQLType::Text); + describe_internal_table->append_column("Type", SQLType::Text); + TRY(add_table(*describe_internal_table)); + } + return {}; } diff --git a/Userland/Libraries/LibSQL/Database.h b/Userland/Libraries/LibSQL/Database.h index 662036c994..555a8c1981 100644 --- a/Userland/Libraries/LibSQL/Database.h +++ b/Userland/Libraries/LibSQL/Database.h @@ -1,5 +1,6 @@ /* * 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 */ diff --git a/Userland/Libraries/LibSQL/SQLResult.h b/Userland/Libraries/LibSQL/SQLResult.h index c0bc4a73ea..c4b2ebd9be 100644 --- a/Userland/Libraries/LibSQL/SQLResult.h +++ b/Userland/Libraries/LibSQL/SQLResult.h @@ -19,6 +19,7 @@ namespace SQL { #define ENUMERATE_SQL_COMMANDS(S) \ S(Create) \ S(Delete) \ + S(Describe) \ S(Insert) \ S(Select) \ S(Update) |