1#include <sstream>
2
3#include <Storages/IStorage.h>
4#include <Parsers/TablePropertiesQueriesASTs.h>
5#include <Parsers/formatAST.h>
6#include <DataStreams/OneBlockInputStream.h>
7#include <DataStreams/BlockIO.h>
8#include <DataStreams/copyData.h>
9#include <DataTypes/DataTypesNumber.h>
10#include <DataTypes/DataTypeString.h>
11#include <Columns/ColumnString.h>
12#include <Common/typeid_cast.h>
13#include <Interpreters/Context.h>
14#include <Interpreters/InterpreterShowCreateQuery.h>
15
16
17namespace DB
18{
19
20namespace ErrorCodes
21{
22 extern const int SYNTAX_ERROR;
23 extern const int THERE_IS_NO_QUERY;
24}
25
26BlockIO InterpreterShowCreateQuery::execute()
27{
28 BlockIO res;
29 res.in = executeImpl();
30 return res;
31}
32
33
34Block InterpreterShowCreateQuery::getSampleBlock()
35{
36 return Block{{
37 ColumnString::create(),
38 std::make_shared<DataTypeString>(),
39 "statement"}};
40}
41
42
43BlockInputStreamPtr InterpreterShowCreateQuery::executeImpl()
44{
45 ASTPtr create_query;
46 ASTQueryWithTableAndOutput * show_query;
47 if ((show_query = query_ptr->as<ASTShowCreateTableQuery>()))
48 {
49 if (show_query->temporary)
50 create_query = context.getCreateExternalTableQuery(show_query->table);
51 else
52 create_query = context.getDatabase(show_query->database)->getCreateTableQuery(context, show_query->table);
53 }
54 else if ((show_query = query_ptr->as<ASTShowCreateDatabaseQuery>()))
55 {
56 if (show_query->temporary)
57 throw Exception("Temporary databases are not possible.", ErrorCodes::SYNTAX_ERROR);
58 create_query = context.getDatabase(show_query->database)->getCreateDatabaseQuery();
59 }
60 else if ((show_query = query_ptr->as<ASTShowCreateDictionaryQuery>()))
61 {
62 if (show_query->temporary)
63 throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR);
64 create_query = context.getDatabase(show_query->database)->getCreateDictionaryQuery(context, show_query->table);
65 }
66
67 if (!create_query && show_query && show_query->temporary)
68 throw Exception("Unable to show the create query of " + show_query->table + ". Maybe it was created by the system.", ErrorCodes::THERE_IS_NO_QUERY);
69
70 std::stringstream stream;
71 formatAST(*create_query, stream, false, true);
72 String res = stream.str();
73
74 MutableColumnPtr column = ColumnString::create();
75 column->insert(res);
76
77 return std::make_shared<OneBlockInputStream>(Block{{
78 std::move(column),
79 std::make_shared<DataTypeString>(),
80 "statement"}});
81}
82
83}
84