summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/AST/Describe.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibSQL/AST/Describe.cpp')
-rw-r--r--Userland/Libraries/LibSQL/AST/Describe.cpp42
1 files changed, 42 insertions, 0 deletions
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;
+}
+
+}