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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Позволяет выполнить запрос INSERT на удалённом сервере и отправить данные.
|
|
|
|
|
*/
|
|
|
|
|
class RemoteBlockOutputStream : public IBlockOutputStream
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-08-12 00:15:17 +00:00
|
|
|
|
RemoteBlockOutputStream(Connection & connection_, const String & query_, Settings * settings_ = nullptr)
|
|
|
|
|
: connection(connection_), query(query_), settings(settings_)
|
2012-05-21 20:38:34 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Отправляет запрос и получает блок-пример, описывающий структуру таблицы.
|
|
|
|
|
* Он нужен, чтобы знать, какие блоки передавать в метод write.
|
|
|
|
|
* Вызывайте только перед write.
|
|
|
|
|
*/
|
|
|
|
|
Block sendQueryAndGetSampleBlock()
|
|
|
|
|
{
|
2014-08-12 00:15:17 +00:00
|
|
|
|
connection.sendQuery(query, "", QueryProcessingStage::Complete, settings);
|
2012-05-21 20:38:34 +00:00
|
|
|
|
sent_query = true;
|
|
|
|
|
|
|
|
|
|
Connection::Packet packet = connection.receivePacket();
|
|
|
|
|
|
|
|
|
|
if (Protocol::Server::Data == packet.type)
|
2014-06-05 12:02:18 +00:00
|
|
|
|
return sample_block = packet.block;
|
2012-06-19 22:46:02 +00:00
|
|
|
|
else if (Protocol::Server::Exception == packet.type)
|
|
|
|
|
{
|
|
|
|
|
packet.exception->rethrow();
|
|
|
|
|
return Block();
|
|
|
|
|
}
|
2012-05-21 20:38:34 +00:00
|
|
|
|
else
|
2012-06-19 22:46:02 +00:00
|
|
|
|
throw Exception("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
|
|
|
|
|
|
|
|
|
void write(const Block & block)
|
|
|
|
|
{
|
|
|
|
|
if (!sent_query)
|
|
|
|
|
sendQueryAndGetSampleBlock();
|
|
|
|
|
|
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-а.
|
|
|
|
|
void writePrepared(ReadBuffer & input)
|
|
|
|
|
{
|
|
|
|
|
if (!sent_query)
|
|
|
|
|
sendQueryAndGetSampleBlock(); /// Никак не можем использовать sample_block.
|
|
|
|
|
|
|
|
|
|
connection.sendPreparedData(input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
void writeSuffix()
|
|
|
|
|
{
|
|
|
|
|
/// Пустой блок означает конец данных.
|
|
|
|
|
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
|
|
|
|
|
throw Exception("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;
|
2014-08-12 00:15:17 +00:00
|
|
|
|
Settings * settings;
|
2014-06-05 12:02:18 +00:00
|
|
|
|
Block sample_block;
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
2014-06-09 09:14:56 +00:00
|
|
|
|
bool sent_query = false;
|
2012-05-21 20:38:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|