ClickHouse/dbms/src/Interpreters/InterpreterShowTablesQuery.cpp

66 lines
1.9 KiB
C++
Raw Normal View History

#include <IO/ReadBufferFromString.h>
2012-06-18 07:49:19 +00:00
#include <Parsers/ASTShowTablesQuery.h>
#include <Parsers/ASTIdentifier.h>
2012-06-18 07:49:19 +00:00
2017-05-23 18:24:43 +00:00
#include <Interpreters/Context.h>
#include <Interpreters/executeQuery.h>
#include <Interpreters/InterpreterShowTablesQuery.h>
2012-06-18 07:49:19 +00:00
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
2017-04-16 05:40:17 +00:00
#include <iomanip>
2018-02-02 14:45:36 +00:00
#include <AggregateFunctions/AggregateFunctionSequenceMatch.h>
2012-06-18 07:49:19 +00:00
namespace DB
{
2017-05-23 18:24:43 +00:00
InterpreterShowTablesQuery::InterpreterShowTablesQuery(const ASTPtr & query_ptr_, Context & context_)
: query_ptr(query_ptr_), context(context_)
2012-06-18 07:49:19 +00:00
{
}
String InterpreterShowTablesQuery::getRewrittenQuery()
{
const ASTShowTablesQuery & query = typeid_cast<const ASTShowTablesQuery &>(*query_ptr);
/// SHOW DATABASES
if (query.databases)
return "SELECT name FROM system.databases";
2012-06-18 07:49:19 +00:00
2018-02-02 14:45:36 +00:00
if (query.temporary && !query.from.empty())
throw Exception("The `FROM` and `TEMPORARY` cannot be used together in `SHOW TABLES`", ErrorCodes::SYNTAX_ERROR);
String database = query.from.empty() ? context.getCurrentDatabase() : query.from;
2015-10-01 15:10:41 +00:00
2017-04-02 17:37:49 +00:00
/** The parameter check_database_access_rights is reset when the SHOW TABLES query is processed,
* So that all clients can see a list of all databases and tables in them regardless of their access rights
* to these databases.
*/
context.assertDatabaseExists(database, false);
2012-06-18 07:49:19 +00:00
std::stringstream rewritten_query;
2018-02-02 13:17:45 +00:00
rewritten_query << "SELECT name FROM system.tables WHERE ";
if (query.temporary)
2018-02-02 14:45:36 +00:00
rewritten_query << "temporary = 1";
2018-02-02 13:17:45 +00:00
else
rewritten_query << "database = " << std::quoted(database, '\'');
2012-06-18 07:49:19 +00:00
if (!query.like.empty())
2017-04-16 05:40:17 +00:00
rewritten_query << " AND name " << (query.not_like ? "NOT " : "") << "LIKE " << std::quoted(query.like, '\'');
return rewritten_query.str();
2012-06-18 07:49:19 +00:00
}
BlockIO InterpreterShowTablesQuery::execute()
{
return executeQuery(getRewrittenQuery(), context, true);
2012-06-18 07:49:19 +00:00
}
}