2012-05-08 11:19:00 +00:00
|
|
|
#include <DB/Columns/ColumnString.h>
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
|
|
|
#include <DB/DataStreams/OneBlockInputStream.h>
|
2015-09-24 03:50:09 +00:00
|
|
|
#include <DB/Storages/System/StorageSystemTables.h>
|
2014-11-13 12:27:42 +00:00
|
|
|
#include <DB/Common/VirtualColumnUtils.h>
|
2016-03-19 01:18:49 +00:00
|
|
|
#include <DB/Databases/IDatabase.h>
|
2012-05-08 11:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:19:00 +00:00
|
|
|
|
2015-01-21 04:30:33 +00:00
|
|
|
StorageSystemTables::StorageSystemTables(const std::string & name_)
|
2015-03-26 23:32:16 +00:00
|
|
|
: name(name_),
|
|
|
|
columns
|
|
|
|
{
|
2016-05-28 07:48:40 +00:00
|
|
|
{"database", std::make_shared<DataTypeString>()},
|
|
|
|
{"name", std::make_shared<DataTypeString>()},
|
|
|
|
{"engine", std::make_shared<DataTypeString>()},
|
2015-03-26 23:32:16 +00:00
|
|
|
}
|
2012-05-08 11:19:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-17 01:27:35 +00:00
|
|
|
static ColumnWithTypeAndName getFilteredDatabases(ASTPtr query, const Context & context)
|
2015-01-21 04:30:33 +00:00
|
|
|
{
|
2015-07-17 01:27:35 +00:00
|
|
|
ColumnWithTypeAndName column;
|
2015-01-21 04:30:33 +00:00
|
|
|
column.name = "database";
|
2016-05-28 07:48:40 +00:00
|
|
|
column.type = std::make_shared<DataTypeString>();
|
2016-05-28 05:31:36 +00:00
|
|
|
column.column = std::make_shared<ColumnString>();
|
2015-01-21 04:30:33 +00:00
|
|
|
|
|
|
|
Block block;
|
|
|
|
block.insert(column);
|
2016-03-19 01:18:49 +00:00
|
|
|
for (const auto & db : context.getDatabases())
|
2015-03-26 23:32:16 +00:00
|
|
|
column.column->insert(db.first);
|
|
|
|
|
2015-01-21 04:30:33 +00:00
|
|
|
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
|
|
|
|
2015-07-17 01:27:35 +00:00
|
|
|
ColumnWithTypeAndName col_db;
|
2012-05-08 11:19:00 +00:00
|
|
|
col_db.name = "database";
|
2016-05-28 07:48:40 +00:00
|
|
|
col_db.type = std::make_shared<DataTypeString>();
|
2016-05-28 05:31:36 +00:00
|
|
|
col_db.column = std::make_shared<ColumnString>();
|
2012-05-08 11:19:00 +00:00
|
|
|
block.insert(col_db);
|
|
|
|
|
2015-07-17 01:27:35 +00:00
|
|
|
ColumnWithTypeAndName col_name;
|
2012-05-08 11:19:00 +00:00
|
|
|
col_name.name = "name";
|
2016-05-28 07:48:40 +00:00
|
|
|
col_name.type = std::make_shared<DataTypeString>();
|
2016-05-28 05:31:36 +00:00
|
|
|
col_name.column = std::make_shared<ColumnString>();
|
2012-05-08 11:19:00 +00:00
|
|
|
block.insert(col_name);
|
|
|
|
|
2015-07-17 01:27:35 +00:00
|
|
|
ColumnWithTypeAndName col_engine;
|
2012-05-08 11:19:00 +00:00
|
|
|
col_engine.name = "engine";
|
2016-05-28 07:48:40 +00:00
|
|
|
col_engine.type = std::make_shared<DataTypeString>();
|
2016-05-28 05:31:36 +00:00
|
|
|
col_engine.column = std::make_shared<ColumnString>();
|
2012-05-08 11:19:00 +00:00
|
|
|
block.insert(col_engine);
|
|
|
|
|
2015-07-17 01:27:35 +00:00
|
|
|
ColumnWithTypeAndName 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();
|
2016-03-19 01:18:49 +00:00
|
|
|
auto database = context.tryGetDatabase(database_name);
|
2014-11-13 12:27:42 +00:00
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
if (!database)
|
|
|
|
{
|
|
|
|
/// Базу данных только что удалили.
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-13 12:27:42 +00:00
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
for (auto iterator = database->getIterator(); iterator->isValid(); iterator->next())
|
2012-05-08 11:19:00 +00:00
|
|
|
{
|
2014-11-13 12:27:42 +00:00
|
|
|
col_db.column->insert(database_name);
|
2016-03-19 01:18:49 +00:00
|
|
|
col_name.column->insert(iterator->name());
|
|
|
|
col_engine.column->insert(iterator->table()->getName());
|
2012-05-08 11:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-03 15:30:10 +00:00
|
|
|
|
2012-05-08 11:19:00 +00:00
|
|
|
return BlockInputStreams(1, new OneBlockInputStream(block));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|