ClickHouse/dbms/src/Interpreters/InterpreterShowTablesQuery.cpp

56 lines
1.4 KiB
C++
Raw Normal View History

2012-06-18 07:49:19 +00:00
#include <DB/IO/ReadBufferFromString.h>
#include <DB/Parsers/ASTShowTablesQuery.h>
#include <DB/Parsers/ASTIdentifier.h>
2012-06-18 07:49:19 +00:00
#include <DB/Interpreters/executeQuery.h>
#include <DB/Interpreters/InterpreterShowTablesQuery.h>
#include <mysqlxx/Manip.h>
namespace DB
{
InterpreterShowTablesQuery::InterpreterShowTablesQuery(ASTPtr query_ptr_, Context & context_)
: query_ptr(query_ptr_), context(context_)
{
}
String InterpreterShowTablesQuery::getRewrittenQuery()
{
const ASTShowTablesQuery & query = typeid_cast<const ASTShowTablesQuery &>(*query_ptr);
String format_or_nothing;
if (query.format)
format_or_nothing = " FORMAT " + typeid_cast<const ASTIdentifier &>(*query.format).name;
2012-06-18 07:49:19 +00:00
/// SHOW DATABASES
if (query.databases)
return "SELECT name FROM system.databases" + format_or_nothing;
2012-06-18 07:49:19 +00:00
2012-08-02 17:33:31 +00:00
String database = query.from.empty() ? context.getCurrentDatabase() : query.from;
2012-06-18 07:49:19 +00:00
context.assertDatabaseExists(database);
std::stringstream rewritten_query;
rewritten_query << "SELECT name FROM system.tables WHERE database = " << mysqlxx::quote << database;
if (!query.like.empty())
rewritten_query << " AND name " << (query.not_like ? "NOT " : "") << "LIKE " << mysqlxx::quote << query.like;
rewritten_query << format_or_nothing;
2012-06-18 07:49:19 +00:00
return rewritten_query.str();
}
BlockIO InterpreterShowTablesQuery::execute()
{
return executeQuery(getRewrittenQuery(), context, true);
2012-06-18 07:49:19 +00:00
}
}