2012-05-08 11:19:00 +00:00
|
|
|
#include <DB/Columns/ColumnString.h>
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
|
|
|
#include <DB/DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <DB/Storages/StorageSystemTables.h>
|
2014-11-13 12:27:42 +00:00
|
|
|
#include <DB/Common/VirtualColumnUtils.h>
|
2012-05-08 11:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2015-01-21 04:30:33 +00:00
|
|
|
StorageSystemTables::StorageSystemTables(const std::string & name_)
|
|
|
|
: name(name_)
|
2012-05-08 11:19:00 +00:00
|
|
|
{
|
|
|
|
columns.push_back(NameAndTypePair("database", new DataTypeString));
|
|
|
|
columns.push_back(NameAndTypePair("name", new DataTypeString));
|
|
|
|
columns.push_back(NameAndTypePair("engine", new DataTypeString));
|
|
|
|
}
|
|
|
|
|
2015-01-21 04:30:33 +00:00
|
|
|
StoragePtr StorageSystemTables::create(const std::string & name_)
|
2013-02-06 11:26:35 +00:00
|
|
|
{
|
2015-01-21 04:30:33 +00:00
|
|
|
return (new StorageSystemTables(name_))->thisPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ColumnWithNameAndType getFilteredDatabases(ASTPtr query, const Context & context)
|
|
|
|
{
|
|
|
|
ColumnWithNameAndType column;
|
|
|
|
column.name = "database";
|
|
|
|
column.type = new DataTypeString;
|
|
|
|
column.column = new ColumnString;
|
|
|
|
|
|
|
|
Block block;
|
|
|
|
block.insert(column);
|
|
|
|
for (auto database_it = context.getDatabases().begin(); database_it != context.getDatabases().end(); ++database_it)
|
|
|
|
{
|
|
|
|
column.column->insert(database_it->first);
|
|
|
|
}
|
|
|
|
VirtualColumnUtils::filterBlockWithQuery(query, block, context);
|
|
|
|
|
|
|
|
return block.getByPosition(0);
|
2013-02-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
2012-05-08 11:19:00 +00:00
|
|
|
|
|
|
|
BlockInputStreams StorageSystemTables::read(
|
2014-12-17 11:53:17 +00:00
|
|
|
const Names & column_names,
|
|
|
|
ASTPtr query,
|
|
|
|
const Context & context,
|
|
|
|
const Settings & settings,
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
|
|
|
const size_t max_block_size,
|
|
|
|
const unsigned threads)
|
2012-05-08 11:19:00 +00:00
|
|
|
{
|
|
|
|
check(column_names);
|
2012-05-22 18:32:45 +00:00
|
|
|
processed_stage = QueryProcessingStage::FetchColumns;
|
2012-05-08 11:19:00 +00:00
|
|
|
|
|
|
|
Block block;
|
2014-10-03 15:30:10 +00:00
|
|
|
|
2012-05-08 11:19:00 +00:00
|
|
|
ColumnWithNameAndType col_db;
|
|
|
|
col_db.name = "database";
|
|
|
|
col_db.type = new DataTypeString;
|
|
|
|
col_db.column = new ColumnString;
|
|
|
|
block.insert(col_db);
|
|
|
|
|
|
|
|
ColumnWithNameAndType col_name;
|
|
|
|
col_name.name = "name";
|
|
|
|
col_name.type = new DataTypeString;
|
|
|
|
col_name.column = new ColumnString;
|
|
|
|
block.insert(col_name);
|
|
|
|
|
|
|
|
ColumnWithNameAndType col_engine;
|
|
|
|
col_engine.name = "engine";
|
|
|
|
col_engine.type = new DataTypeString;
|
|
|
|
col_engine.column = new ColumnString;
|
|
|
|
block.insert(col_engine);
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(context.getMutex());
|
2014-10-03 15:30:10 +00:00
|
|
|
|
2015-01-21 04:30:33 +00:00
|
|
|
ColumnWithNameAndType filtered_databases_column = getFilteredDatabases(query, context);
|
2014-11-13 12:27:42 +00:00
|
|
|
|
|
|
|
for (size_t row_number = 0; row_number < filtered_databases_column.column->size(); ++row_number)
|
2012-05-08 11:19:00 +00:00
|
|
|
{
|
2014-11-13 12:27:42 +00:00
|
|
|
std::string database_name = filtered_databases_column.column->getDataAt(row_number).toString();
|
|
|
|
auto database_it = context.getDatabases().find(database_name);
|
|
|
|
|
|
|
|
if (database_it == context.getDatabases().end())
|
|
|
|
throw DB::Exception(std::string("Fail to find database " + database_name), DB::ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
for (Tables::const_iterator jt = database_it->second.begin(); jt != database_it->second.end(); ++jt)
|
2012-05-08 11:19:00 +00:00
|
|
|
{
|
2014-11-13 12:27:42 +00:00
|
|
|
col_db.column->insert(database_name);
|
2012-05-08 11:19:00 +00:00
|
|
|
col_name.column->insert(jt->first);
|
|
|
|
col_engine.column->insert(jt->second->getName());
|
|
|
|
}
|
|
|
|
}
|
2014-10-03 15:30:10 +00:00
|
|
|
|
2012-05-08 11:19:00 +00:00
|
|
|
return BlockInputStreams(1, new OneBlockInputStream(block));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|