2013-02-21 10:02:33 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Storages/IStorage.h>
|
|
|
|
#include <DB/Parsers/TablePropertiesQueriesASTs.h>
|
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
|
|
|
#include <DB/Interpreters/Context.h>
|
|
|
|
#include <DB/DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <DB/DataStreams/BlockIO.h>
|
|
|
|
#include <DB/DataStreams/copyData.h>
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
/** Вернуть названия и типы столбцов указанной таблицы.
|
|
|
|
*/
|
|
|
|
class InterpreterDescribeQuery
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InterpreterDescribeQuery(ASTPtr query_ptr_, Context & context_)
|
|
|
|
: query_ptr(query_ptr_), context(context_) {}
|
|
|
|
|
|
|
|
BlockIO execute()
|
2013-02-21 10:02:33 +00:00
|
|
|
{
|
2013-02-22 09:41:13 +00:00
|
|
|
BlockIO res;
|
|
|
|
res.in = executeImpl();
|
|
|
|
res.in_sample = getSampleBlock();
|
2013-02-21 10:02:33 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockInputStreamPtr executeAndFormat(WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
Block sample = getSampleBlock();
|
|
|
|
ASTPtr format_ast = dynamic_cast<ASTDescribeQuery &>(*query_ptr).format;
|
|
|
|
String format_name = format_ast ? dynamic_cast<ASTIdentifier &>(*format_ast).name : "TabSeparated";
|
2013-02-21 10:02:33 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
BlockInputStreamPtr in = executeImpl();
|
2013-05-22 14:57:43 +00:00
|
|
|
BlockOutputStreamPtr out = context.getFormatFactory().getOutput(format_name, buf, sample);
|
2013-02-22 09:41:13 +00:00
|
|
|
|
|
|
|
copyData(*in, *out);
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ASTPtr query_ptr;
|
|
|
|
Context context;
|
|
|
|
|
|
|
|
Block getSampleBlock()
|
|
|
|
{
|
|
|
|
Block block;
|
|
|
|
|
|
|
|
ColumnWithNameAndType col;
|
|
|
|
col.name = "name";
|
|
|
|
col.type = new DataTypeString;
|
2013-02-21 10:02:33 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
block.insert(col);
|
|
|
|
|
|
|
|
col.name = "type";
|
|
|
|
|
|
|
|
block.insert(col);
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockInputStreamPtr executeImpl()
|
|
|
|
{
|
|
|
|
const ASTDescribeQuery & ast = dynamic_cast<const ASTDescribeQuery &>(*query_ptr);
|
|
|
|
|
|
|
|
NamesAndTypesList columns;
|
2013-02-21 10:02:33 +00:00
|
|
|
|
|
|
|
{
|
2013-02-22 09:41:13 +00:00
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(context.getMutex());
|
2013-02-21 10:02:33 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
if (!context.isTableExist(ast.database, ast.table))
|
|
|
|
throw Exception("Table " + (ast.database.empty() ? "" : ast.database + ".") + ast.table + " doesn't exist", ErrorCodes::UNKNOWN_TABLE);
|
2013-02-21 10:02:33 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
columns = context.getTable(ast.database, ast.table)->getColumnsList();
|
2013-02-21 10:02:33 +00:00
|
|
|
}
|
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
ColumnString * name_column = new ColumnString;
|
|
|
|
ColumnString * type_column = new ColumnString;
|
|
|
|
|
|
|
|
Block block;
|
|
|
|
block.insert(ColumnWithNameAndType(name_column, new DataTypeString, "name"));
|
|
|
|
block.insert(ColumnWithNameAndType(type_column, new DataTypeString, "type"));
|
|
|
|
|
|
|
|
for (NamesAndTypesList::iterator it = columns.begin(); it != columns.end(); ++it)
|
2013-02-21 10:02:33 +00:00
|
|
|
{
|
2013-02-22 09:41:13 +00:00
|
|
|
name_column->insert(it->first);
|
|
|
|
type_column->insert(it->second->getName());
|
2013-02-21 10:02:33 +00:00
|
|
|
}
|
2013-02-22 09:41:13 +00:00
|
|
|
|
|
|
|
return new OneBlockInputStream(block);
|
|
|
|
}
|
|
|
|
};
|
2013-02-21 10:02:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|