1 | #include <IO/ReadBufferFromString.h> |
2 | #include <Parsers/ASTShowTablesQuery.h> |
3 | #include <Parsers/formatAST.h> |
4 | #include <Interpreters/Context.h> |
5 | #include <Interpreters/executeQuery.h> |
6 | #include <Interpreters/InterpreterShowTablesQuery.h> |
7 | #include <Common/typeid_cast.h> |
8 | #include <iomanip> |
9 | #include <sstream> |
10 | |
11 | |
12 | namespace DB |
13 | { |
14 | |
15 | namespace ErrorCodes |
16 | { |
17 | extern const int SYNTAX_ERROR; |
18 | } |
19 | |
20 | |
21 | InterpreterShowTablesQuery::InterpreterShowTablesQuery(const ASTPtr & query_ptr_, Context & context_) |
22 | : query_ptr(query_ptr_), context(context_) |
23 | { |
24 | } |
25 | |
26 | |
27 | String InterpreterShowTablesQuery::getRewrittenQuery() |
28 | { |
29 | const auto & query = query_ptr->as<ASTShowTablesQuery &>(); |
30 | |
31 | /// SHOW DATABASES |
32 | if (query.databases) |
33 | return "SELECT name FROM system.databases" ; |
34 | |
35 | if (query.temporary && !query.from.empty()) |
36 | throw Exception("The `FROM` and `TEMPORARY` cannot be used together in `SHOW TABLES`" , ErrorCodes::SYNTAX_ERROR); |
37 | |
38 | String database = query.from.empty() ? context.getCurrentDatabase() : query.from; |
39 | |
40 | /** The parameter check_database_access_rights is reset when the SHOW TABLES query is processed, |
41 | * So that all clients can see a list of all databases and tables in them regardless of their access rights |
42 | * to these databases. |
43 | */ |
44 | context.assertDatabaseExists(database, false); |
45 | |
46 | std::stringstream rewritten_query; |
47 | rewritten_query << "SELECT name FROM system." ; |
48 | |
49 | if (query.dictionaries) |
50 | rewritten_query << "dictionaries " ; |
51 | else |
52 | rewritten_query << "tables " ; |
53 | |
54 | rewritten_query << "WHERE " ; |
55 | |
56 | if (query.temporary) |
57 | { |
58 | if (query.dictionaries) |
59 | throw Exception("Temporary dictionaries are not possible." , ErrorCodes::SYNTAX_ERROR); |
60 | rewritten_query << "is_temporary" ; |
61 | } |
62 | else |
63 | rewritten_query << "database = " << std::quoted(database, '\''); |
64 | |
65 | if (!query.like.empty()) |
66 | rewritten_query << " AND name " << (query.not_like ? "NOT " : "" ) << "LIKE " << std::quoted(query.like, '\''); |
67 | |
68 | if (query.limit_length) |
69 | rewritten_query << " LIMIT " << query.limit_length; |
70 | |
71 | return rewritten_query.str(); |
72 | } |
73 | |
74 | |
75 | BlockIO InterpreterShowTablesQuery::execute() |
76 | { |
77 | return executeQuery(getRewrittenQuery(), context, true); |
78 | } |
79 | |
80 | |
81 | } |
82 | |