2012-05-21 20:38:34 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
#include <Poco/Net/StreamSocket.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Core/Block.h>
|
|
|
|
|
#include <DB/Core/Progress.h>
|
|
|
|
|
#include <DB/Core/Protocol.h>
|
|
|
|
|
#include <DB/Core/QueryProcessingStage.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/IO/ReadBufferFromPocoSocket.h>
|
|
|
|
|
#include <DB/IO/WriteBufferFromPocoSocket.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypeFactory.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/DataStreams/IBlockInputStream.h>
|
|
|
|
|
#include <DB/DataStreams/IBlockOutputStream.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using Poco::SharedPtr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Соединение с сервером БД для использования в клиенте.
|
|
|
|
|
* Как использовать - см. Core/Protocol.h
|
|
|
|
|
* (Реализацию на стороне сервера - см. Server/TCPHandler.h)
|
2012-05-30 06:46:57 +00:00
|
|
|
|
*
|
|
|
|
|
* В качестве default_database может быть указана пустая строка
|
|
|
|
|
* - в этом случае сервер использует свою БД по-умолчанию.
|
2012-05-16 18:03:00 +00:00
|
|
|
|
*/
|
|
|
|
|
class Connection
|
|
|
|
|
{
|
|
|
|
|
public:
|
2012-05-30 06:46:57 +00:00
|
|
|
|
Connection(const String & host_, UInt16 port_, const String & default_database_,
|
2012-05-16 18:03:00 +00:00
|
|
|
|
DataTypeFactory & data_type_factory_,
|
2012-05-23 19:51:30 +00:00
|
|
|
|
const String & client_name_ = "client",
|
2012-05-16 18:03:00 +00:00
|
|
|
|
Protocol::Compression::Enum compression_ = Protocol::Compression::Enable)
|
2012-05-30 06:46:57 +00:00
|
|
|
|
: host(host_), port(port_), default_database(default_database_), client_name(client_name_), connected(false),
|
2012-05-16 18:20:45 +00:00
|
|
|
|
server_version_major(0), server_version_minor(0), server_revision(0),
|
2012-05-21 06:49:05 +00:00
|
|
|
|
socket(), in(new ReadBufferFromPocoSocket(socket)), out(new WriteBufferFromPocoSocket(socket)),
|
2012-05-16 18:03:00 +00:00
|
|
|
|
query_id(0), compression(compression_), data_type_factory(data_type_factory_)
|
|
|
|
|
{
|
2012-05-21 20:38:34 +00:00
|
|
|
|
/// Соединеняемся не сразу, а при первой необходимости.
|
2012-05-16 18:03:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~Connection() {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Пакет, который может быть получен от сервера.
|
|
|
|
|
struct Packet
|
|
|
|
|
{
|
|
|
|
|
UInt64 type;
|
|
|
|
|
|
|
|
|
|
Block block;
|
|
|
|
|
SharedPtr<Exception> exception;
|
|
|
|
|
Progress progress;
|
|
|
|
|
|
|
|
|
|
Packet() : type(Protocol::Server::Hello) {}
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-16 18:20:45 +00:00
|
|
|
|
void getServerVersion(String & name, UInt64 & version_major, UInt64 & version_minor, UInt64 & revision);
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
2012-06-07 20:02:41 +00:00
|
|
|
|
/// query_id не должен быть равен 0.
|
|
|
|
|
void sendQuery(const String & query, UInt64 query_id_ = 1, UInt64 stage = QueryProcessingStage::Complete);
|
2012-05-16 18:03:00 +00:00
|
|
|
|
void sendCancel();
|
2012-05-21 20:38:34 +00:00
|
|
|
|
void sendData(const Block & block);
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
|
|
|
|
/// Проверить, если ли данные, которые можно прочитать.
|
|
|
|
|
bool poll(size_t timeout_microseconds = 0);
|
|
|
|
|
|
|
|
|
|
/// Получить пакет от сервера.
|
|
|
|
|
Packet receivePacket();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
String host;
|
|
|
|
|
UInt16 port;
|
2012-05-30 06:46:57 +00:00
|
|
|
|
String default_database;
|
2012-05-16 18:20:45 +00:00
|
|
|
|
|
2012-05-23 19:51:30 +00:00
|
|
|
|
String client_name;
|
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
bool connected;
|
|
|
|
|
|
2012-05-16 18:20:45 +00:00
|
|
|
|
String server_name;
|
|
|
|
|
UInt64 server_version_major;
|
|
|
|
|
UInt64 server_version_minor;
|
|
|
|
|
UInt64 server_revision;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
|
|
|
|
Poco::Net::StreamSocket socket;
|
2012-05-21 06:49:05 +00:00
|
|
|
|
SharedPtr<ReadBufferFromPocoSocket> in;
|
|
|
|
|
SharedPtr<WriteBufferFromPocoSocket> out;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
|
|
|
|
UInt64 query_id;
|
|
|
|
|
UInt64 compression; /// Сжимать ли данные при взаимодействии с сервером.
|
|
|
|
|
|
|
|
|
|
DataTypeFactory & data_type_factory;
|
|
|
|
|
|
|
|
|
|
/// Откуда читать результат выполнения запроса.
|
|
|
|
|
SharedPtr<ReadBuffer> maybe_compressed_in;
|
|
|
|
|
BlockInputStreamPtr block_in;
|
|
|
|
|
|
|
|
|
|
/// Куда писать данные INSERT-а.
|
|
|
|
|
SharedPtr<WriteBuffer> maybe_compressed_out;
|
|
|
|
|
BlockOutputStreamPtr block_out;
|
|
|
|
|
|
|
|
|
|
void connect();
|
|
|
|
|
void sendHello();
|
|
|
|
|
void receiveHello();
|
|
|
|
|
void forceConnected();
|
|
|
|
|
bool ping();
|
|
|
|
|
|
|
|
|
|
Block receiveData();
|
|
|
|
|
SharedPtr<Exception> receiveException();
|
|
|
|
|
Progress receiveProgress();
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
|
|
|
|
typedef SharedPtr<Connection> ConnectionPtr;
|
|
|
|
|
typedef std::vector<ConnectionPtr> Connections;
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
}
|