2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
#include <Parsers/ASTShowTablesQuery.h>
|
2017-05-23 18:24:43 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/executeQuery.h>
|
|
|
|
#include <Interpreters/InterpreterShowTablesQuery.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-16 05:40:17 +00:00
|
|
|
#include <iomanip>
|
2018-06-07 14:42:38 +00:00
|
|
|
#include <sstream>
|
2018-02-08 19:14:59 +00:00
|
|
|
|
2012-06-18 07:49:19 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-02-08 19:53:34 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-06-18 07:49:19 +00:00
|
|
|
|
2017-05-23 18:24:43 +00:00
|
|
|
InterpreterShowTablesQuery::InterpreterShowTablesQuery(const ASTPtr & query_ptr_, Context & context_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: query_ptr(query_ptr_), context(context_)
|
2012-06-18 07:49:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String InterpreterShowTablesQuery::getRewrittenQuery()
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * query = query_ptr->as<ASTShowTablesQuery>();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// SHOW DATABASES
|
2019-03-08 09:45:34 +00:00
|
|
|
if (query->databases)
|
2017-04-01 07:20:54 +00:00
|
|
|
return "SELECT name FROM system.databases";
|
2012-06-18 07:49:19 +00:00
|
|
|
|
2019-03-08 09:45:34 +00:00
|
|
|
if (query->temporary && !query->from.empty())
|
2018-02-02 14:45:36 +00:00
|
|
|
throw Exception("The `FROM` and `TEMPORARY` cannot be used together in `SHOW TABLES`", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
2019-03-08 09:45:34 +00:00
|
|
|
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.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
context.assertDatabaseExists(database, false);
|
2012-06-18 07:49:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::stringstream rewritten_query;
|
2018-02-02 13:17:45 +00:00
|
|
|
rewritten_query << "SELECT name FROM system.tables WHERE ";
|
|
|
|
|
2019-03-08 09:45:34 +00:00
|
|
|
if (query->temporary)
|
2018-02-08 19:41:10 +00:00
|
|
|
rewritten_query << "is_temporary";
|
2018-02-02 13:17:45 +00:00
|
|
|
else
|
|
|
|
rewritten_query << "database = " << std::quoted(database, '\'');
|
2012-06-18 07:49:19 +00:00
|
|
|
|
2019-03-08 09:45:34 +00:00
|
|
|
if (!query->like.empty())
|
|
|
|
rewritten_query << " AND name " << (query->not_like ? "NOT " : "") << "LIKE " << std::quoted(query->like, '\'');
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return rewritten_query.str();
|
2012-06-18 07:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterShowTablesQuery::execute()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return executeQuery(getRewrittenQuery(), context, true);
|
2012-06-18 07:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|