Add new feature: SHOW DATABASES LIKE

This commit is contained in:
hexiaoting 2020-09-07 10:54:59 +08:00
parent 5b0822902b
commit a3671a15f6
5 changed files with 52 additions and 1 deletions

View File

@ -31,7 +31,21 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
/// SHOW DATABASES
if (query.databases)
return "SELECT name FROM system.databases";
{
std::stringstream rewritten_query;
rewritten_query << "SELECT name FROM system.databases";
if (!query.like.empty())
{
rewritten_query << " WHERE name " << (query.not_like ? "NOT " : "") << "LIKE " << std::quoted(query.like, '\'');
}
if (query.limit_length)
rewritten_query << " LIMIT " << query.limit_length;
DUMP(rewritten_query.str());
return rewritten_query.str();
}
/// SHOW CLUSTER/CLUSTERS
if (query.clusters)

View File

@ -18,6 +18,20 @@ void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, Format
if (databases)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "SHOW DATABASES" << (settings.hilite ? hilite_none : "");
if (!like.empty())
settings.ostr
<< (settings.hilite ? hilite_keyword : "")
<< (not_like ? " NOT" : "")
<< (case_insensitive_like ? " ILIKE " : " LIKE ")
<< (settings.hilite ? hilite_none : "")
<< std::quoted(like, '\'');
if (limit_length)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIMIT " << (settings.hilite ? hilite_none : "");
limit_length->formatImpl(settings, state, frame);
}
}
else if (clusters)
{

View File

@ -46,6 +46,25 @@ bool ParserShowTablesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
if (s_databases.ignore(pos))
{
query->databases = true;
if (s_not.ignore(pos, expected))
query->not_like = true;
if (bool insensitive = s_ilike.ignore(pos, expected); insensitive || s_like.ignore(pos, expected))
{
if (insensitive)
query->case_insensitive_like = true;
if (!like_p.parse(pos, like, expected))
return false;
}
else if (query->not_like)
return false;
if (s_limit.ignore(pos, expected))
{
if (!exp_elem.parse(pos, query->limit_length, expected))
return false;
}
}
else if (s_clusters.ignore(pos))
{

View File

@ -0,0 +1 @@
test_01470

View File

@ -0,0 +1,3 @@
create database if not exists test_01470;
show databases like '%01470';
drop database test_01470;