#include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int SYNTAX_ERROR; extern const int THERE_IS_NO_QUERY; } BlockIO InterpreterShowCreateQuery::execute() { BlockIO res; res.in = executeImpl(); return res; } Block InterpreterShowCreateQuery::getSampleBlock() { return Block{{ ColumnString::create(), std::make_shared(), "statement"}}; } BlockInputStreamPtr InterpreterShowCreateQuery::executeImpl() { ASTPtr create_query; ASTQueryWithTableAndOutput * show_query; if ((show_query = query_ptr->as())) { auto resolve_table_type = show_query->temporary ? Context::ResolveExternal : Context::ResolveOrdinary; auto table_id = context.resolveStorageID(*show_query, resolve_table_type); context.checkAccess(AccessType::SHOW_COLUMNS, table_id); create_query = DatabaseCatalog::instance().getDatabase(table_id.database_name)->getCreateTableQuery(table_id.table_name, context); } else if ((show_query = query_ptr->as())) { if (show_query->temporary) throw Exception("Temporary databases are not possible.", ErrorCodes::SYNTAX_ERROR); show_query->database = context.resolveDatabase(show_query->database); context.checkAccess(AccessType::SHOW_DATABASES, show_query->database); create_query = DatabaseCatalog::instance().getDatabase(show_query->database)->getCreateDatabaseQuery(); } else if ((show_query = query_ptr->as())) { if (show_query->temporary) throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR); show_query->database = context.resolveDatabase(show_query->database); context.checkAccess(AccessType::SHOW_DICTIONARIES, show_query->database, show_query->table); create_query = DatabaseCatalog::instance().getDatabase(show_query->database)->getCreateDictionaryQuery(show_query->table); } if (!create_query) throw Exception("Unable to show the create query of " + show_query->table + ". Maybe it was created by the system.", ErrorCodes::THERE_IS_NO_QUERY); if (!context.getSettingsRef().show_table_uuid_in_table_create_query_if_not_nil) { auto & create = create_query->as(); create.uuid = UUIDHelpers::Nil; } WriteBufferFromOwnString buf; formatAST(*create_query, buf, false, false); String res = buf.str(); MutableColumnPtr column = ColumnString::create(); column->insert(res); return std::make_shared(Block{{ std::move(column), std::make_shared(), "statement"}}); } }