2012-05-21 20:38:34 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/DataStreams/IBlockOutputStream.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Client/Connection.h>
|
2014-07-04 11:21:53 +00:00
|
|
|
|
#include <Yandex/logger_useful.h>
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
2015-03-26 08:47:02 +00:00
|
|
|
|
#include <statdaemons/NetException.h>
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Позволяет выполнить запрос INSERT на удалённом сервере и отправить данные.
|
|
|
|
|
*/
|
|
|
|
|
class RemoteBlockOutputStream : public IBlockOutputStream
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-06-28 06:38:18 +00:00
|
|
|
|
RemoteBlockOutputStream(Connection & connection_, const String & query_, const Settings * settings_ = nullptr)
|
2014-08-12 00:15:17 +00:00
|
|
|
|
: connection(connection_), query(query_), settings(settings_)
|
2012-05-21 20:38:34 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-18 19:49:31 +00:00
|
|
|
|
/// Можно вызывать после writePrefix, чтобы получить структуру таблицы.
|
|
|
|
|
Block getSampleBlock() const
|
2012-05-21 20:38:34 +00:00
|
|
|
|
{
|
2014-09-18 19:49:31 +00:00
|
|
|
|
return sample_block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void writePrefix() override
|
|
|
|
|
{
|
|
|
|
|
/** Отправляет запрос и получает блок-пример, описывающий структуру таблицы.
|
|
|
|
|
* Он нужен, чтобы знать, какие блоки передавать в метод write.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-08-12 00:15:17 +00:00
|
|
|
|
connection.sendQuery(query, "", QueryProcessingStage::Complete, settings);
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
|
|
|
|
Connection::Packet packet = connection.receivePacket();
|
|
|
|
|
|
|
|
|
|
if (Protocol::Server::Data == packet.type)
|
2014-09-18 19:49:31 +00:00
|
|
|
|
{
|
|
|
|
|
sample_block = packet.block;
|
2014-09-22 23:21:15 +00:00
|
|
|
|
|
|
|
|
|
if (!sample_block)
|
|
|
|
|
throw Exception("Logical error: empty block received as table structure", ErrorCodes::LOGICAL_ERROR);
|
2014-09-18 19:49:31 +00:00
|
|
|
|
}
|
2012-06-19 22:46:02 +00:00
|
|
|
|
else if (Protocol::Server::Exception == packet.type)
|
|
|
|
|
{
|
|
|
|
|
packet.exception->rethrow();
|
2014-09-18 19:49:31 +00:00
|
|
|
|
return;
|
2012-06-19 22:46:02 +00:00
|
|
|
|
}
|
2012-05-21 20:38:34 +00:00
|
|
|
|
else
|
2015-03-26 08:47:02 +00:00
|
|
|
|
throw NetException("Unexpected packet from server (expected Data or Exception, got "
|
2013-08-04 00:42:35 +00:00
|
|
|
|
+ String(Protocol::Server::toString(packet.type)) + ")", ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
|
2012-05-21 20:38:34 +00:00
|
|
|
|
}
|
2014-07-11 00:29:59 +00:00
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
2014-09-18 19:49:31 +00:00
|
|
|
|
void write(const Block & block) override
|
2012-05-21 20:38:34 +00:00
|
|
|
|
{
|
2014-09-22 23:21:15 +00:00
|
|
|
|
if (!sample_block)
|
|
|
|
|
throw Exception("You must call IBlockOutputStream::writePrefix before IBlockOutputStream::write", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
2014-06-05 12:02:18 +00:00
|
|
|
|
if (!blocksHaveEqualStructure(block, sample_block))
|
|
|
|
|
{
|
|
|
|
|
std::stringstream message;
|
2014-06-06 14:50:27 +00:00
|
|
|
|
message << "Block structure is different from table structure.\n"
|
2014-06-05 12:02:18 +00:00
|
|
|
|
<< "\nTable structure:\n(" << sample_block.dumpStructure() << ")\nBlock structure:\n(" << block.dumpStructure() << ")\n";
|
|
|
|
|
|
2014-07-04 11:21:53 +00:00
|
|
|
|
LOG_ERROR(&Logger::get("RemoteBlockOutputStream"), message.str());
|
2014-06-05 12:02:18 +00:00
|
|
|
|
throw DB::Exception(message.str());
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
connection.sendData(block);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-07-11 00:29:59 +00:00
|
|
|
|
/// Отправить блок данных, который уже был заранее сериализован (и, если надо, сжат), который следует прочитать из input-а.
|
2014-08-21 12:58:07 +00:00
|
|
|
|
void writePrepared(ReadBuffer & input, size_t size = 0)
|
2014-07-11 00:29:59 +00:00
|
|
|
|
{
|
2014-09-18 19:49:31 +00:00
|
|
|
|
/// Не можем использовать sample_block.
|
2014-08-21 12:58:07 +00:00
|
|
|
|
connection.sendPreparedData(input, size);
|
2014-07-11 00:29:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-18 19:49:31 +00:00
|
|
|
|
void writeSuffix() override
|
2012-05-21 20:38:34 +00:00
|
|
|
|
{
|
|
|
|
|
/// Пустой блок означает конец данных.
|
|
|
|
|
connection.sendData(Block());
|
|
|
|
|
|
|
|
|
|
/// Получаем пакет EndOfStream.
|
|
|
|
|
Connection::Packet packet = connection.receivePacket();
|
|
|
|
|
|
2012-06-19 22:46:02 +00:00
|
|
|
|
if (Protocol::Server::EndOfStream == packet.type)
|
|
|
|
|
{
|
|
|
|
|
/// Ничего.
|
|
|
|
|
}
|
|
|
|
|
else if (Protocol::Server::Exception == packet.type)
|
|
|
|
|
packet.exception->rethrow();
|
|
|
|
|
else
|
2015-03-26 08:47:02 +00:00
|
|
|
|
throw NetException("Unexpected packet from server (expected EndOfStream or Exception, got "
|
2013-08-04 00:42:35 +00:00
|
|
|
|
+ String(Protocol::Server::toString(packet.type)) + ")", ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
|
2012-05-21 20:38:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Connection & connection;
|
2012-06-07 20:02:41 +00:00
|
|
|
|
String query;
|
2015-06-28 06:38:18 +00:00
|
|
|
|
const Settings * settings;
|
2014-06-05 12:02:18 +00:00
|
|
|
|
Block sample_block;
|
2012-05-21 20:38:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|