2018-06-05 19:46:49 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Parsers/TablePropertiesQueriesASTs.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <DataStreams/BlockIO.h>
|
|
|
|
#include <DataStreams/copyData.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2020-01-26 09:49:53 +00:00
|
|
|
#include <Access/AccessFlags.h>
|
2017-05-23 18:24:43 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterShowCreateQuery.h>
|
2016-12-12 07:24:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-02-14 07:15:38 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2018-03-07 19:52:22 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
extern const int THERE_IS_NO_QUERY;
|
2018-02-14 07:15:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 07:24:56 +00:00
|
|
|
BlockIO InterpreterShowCreateQuery::execute()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockIO res;
|
|
|
|
res.in = executeImpl();
|
|
|
|
return res;
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block InterpreterShowCreateQuery::getSampleBlock()
|
|
|
|
{
|
2018-02-15 18:54:12 +00:00
|
|
|
return Block{{
|
|
|
|
ColumnString::create(),
|
|
|
|
std::make_shared<DataTypeString>(),
|
|
|
|
"statement"}};
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr InterpreterShowCreateQuery::executeImpl()
|
|
|
|
{
|
2018-03-13 13:28:32 +00:00
|
|
|
ASTPtr create_query;
|
2019-10-11 13:21:52 +00:00
|
|
|
ASTQueryWithTableAndOutput * show_query;
|
2019-12-15 06:34:43 +00:00
|
|
|
if ((show_query = query_ptr->as<ASTShowCreateTableQuery>()))
|
2019-10-11 13:21:52 +00:00
|
|
|
{
|
2020-02-21 15:22:28 +00:00
|
|
|
auto resolve_table_type = show_query->temporary ? Context::ResolveExternal : Context::ResolveOrdinary;
|
2020-03-13 19:46:16 +00:00
|
|
|
auto table_id = context.resolveStorageID(*show_query, resolve_table_type);
|
2020-03-06 14:36:01 +00:00
|
|
|
context.checkAccess(AccessType::SHOW_COLUMNS, table_id);
|
2020-02-21 15:22:28 +00:00
|
|
|
create_query = DatabaseCatalog::instance().getDatabase(table_id.database_name)->getCreateTableQuery(context, table_id.table_name);
|
2019-10-11 13:21:52 +00:00
|
|
|
}
|
2019-12-15 06:34:43 +00:00
|
|
|
else if ((show_query = query_ptr->as<ASTShowCreateDatabaseQuery>()))
|
2019-10-11 13:21:52 +00:00
|
|
|
{
|
|
|
|
if (show_query->temporary)
|
|
|
|
throw Exception("Temporary databases are not possible.", ErrorCodes::SYNTAX_ERROR);
|
2020-02-21 15:22:28 +00:00
|
|
|
show_query->database = context.resolveDatabase(show_query->database);
|
2020-03-06 14:36:01 +00:00
|
|
|
context.checkAccess(AccessType::SHOW_DATABASES, show_query->database);
|
2020-02-10 13:10:17 +00:00
|
|
|
create_query = DatabaseCatalog::instance().getDatabase(show_query->database)->getCreateDatabaseQuery(context);
|
2019-10-11 13:21:52 +00:00
|
|
|
}
|
2019-12-15 06:34:43 +00:00
|
|
|
else if ((show_query = query_ptr->as<ASTShowCreateDictionaryQuery>()))
|
2019-10-11 13:21:52 +00:00
|
|
|
{
|
|
|
|
if (show_query->temporary)
|
|
|
|
throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR);
|
2020-02-21 15:22:28 +00:00
|
|
|
show_query->database = context.resolveDatabase(show_query->database);
|
2020-03-06 14:36:01 +00:00
|
|
|
context.checkAccess(AccessType::SHOW_DICTIONARIES, show_query->database, show_query->table);
|
2020-02-21 15:22:28 +00:00
|
|
|
create_query = DatabaseCatalog::instance().getDatabase(show_query->database)->getCreateDictionaryQuery(context, show_query->table);
|
2019-10-11 13:21:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 06:34:43 +00:00
|
|
|
if (!create_query && show_query && show_query->temporary)
|
2019-10-11 13:21:52 +00:00
|
|
|
throw Exception("Unable to show the create query of " + show_query->table + ". Maybe it was created by the system.", ErrorCodes::THERE_IS_NO_QUERY);
|
2018-02-14 04:00:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::stringstream stream;
|
2020-04-03 23:05:16 +00:00
|
|
|
formatAST(*create_query, stream, false, false);
|
2017-04-01 07:20:54 +00:00
|
|
|
String res = stream.str();
|
2016-12-12 07:24:56 +00:00
|
|
|
|
2017-12-15 18:23:05 +00:00
|
|
|
MutableColumnPtr column = ColumnString::create();
|
2017-07-21 06:35:58 +00:00
|
|
|
column->insert(res);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<OneBlockInputStream>(Block{{
|
2017-12-15 18:23:05 +00:00
|
|
|
std::move(column),
|
2017-04-01 07:20:54 +00:00
|
|
|
std::make_shared<DataTypeString>(),
|
|
|
|
"statement"}});
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|