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;
|
2013-06-29 18:03:57 +00:00
|
|
|
String format_name = format_ast ? dynamic_cast<ASTIdentifier &>(*format_ast).name : context.getDefaultFormat();
|
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-06-18 14:12:10 +00:00
|
|
|
col.column = col.type->createColumn();
|
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
|
|
|
|
|
|
|
{
|
2014-03-19 10:45:13 +00:00
|
|
|
StoragePtr table = context.getTable(ast.database, ast.table);
|
|
|
|
auto table_lock = table->lockStructure(false);
|
|
|
|
columns = 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
|
|
|
|
|
|
|
|
|
|
|
}
|