2012-05-21 20:38:34 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2012-10-16 19:20:58 +00:00
|
|
|
|
#include <Yandex/logger_useful.h>
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
#include <Poco/Net/StreamSocket.h>
|
|
|
|
|
|
2012-07-26 19:42:20 +00:00
|
|
|
|
#include <DB/Core/Defines.h>
|
2012-05-16 18:03:00 +00:00
|
|
|
|
#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
|
|
|
|
*/
|
2012-10-22 19:03:01 +00:00
|
|
|
|
class Connection : private boost::noncopyable
|
2012-05-16 18:03:00 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2012-05-30 06:46:57 +00:00
|
|
|
|
Connection(const String & host_, UInt16 port_, const String & default_database_,
|
2012-08-02 17:33:31 +00:00
|
|
|
|
const DataTypeFactory & data_type_factory_,
|
2012-05-23 19:51:30 +00:00
|
|
|
|
const String & client_name_ = "client",
|
2012-07-26 19:42:20 +00:00
|
|
|
|
Protocol::Compression::Enum compression_ = Protocol::Compression::Enable,
|
|
|
|
|
Poco::Timespan connect_timeout_ = Poco::Timespan(DBMS_DEFAULT_CONNECT_TIMEOUT_SEC, 0),
|
|
|
|
|
Poco::Timespan receive_timeout_ = Poco::Timespan(DBMS_DEFAULT_RECEIVE_TIMEOUT_SEC, 0),
|
|
|
|
|
Poco::Timespan send_timeout_ = Poco::Timespan(DBMS_DEFAULT_SEND_TIMEOUT_SEC, 0))
|
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-07-26 19:42:20 +00:00
|
|
|
|
query_id(0), compression(compression_), data_type_factory(data_type_factory_),
|
2012-10-16 19:20:58 +00:00
|
|
|
|
connect_timeout(connect_timeout_), receive_timeout(receive_timeout_), send_timeout(send_timeout_),
|
|
|
|
|
log(&Logger::get("Connection (" + Poco::Net::SocketAddress(host, port).toString() + ")"))
|
2012-05-16 18:03:00 +00:00
|
|
|
|
{
|
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-10-16 18:14:51 +00:00
|
|
|
|
/// Адрес сервера - для сообщений в логе и в эксепшенах.
|
|
|
|
|
String getServerAddress() const;
|
|
|
|
|
|
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();
|
2012-10-12 18:19:44 +00:00
|
|
|
|
|
2012-11-02 20:13:41 +00:00
|
|
|
|
/// Если ещё не соединено, или соединение разорвано - соединиться. Если не получилось - кинуть исключение.
|
|
|
|
|
void forceConnected();
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
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; /// Сжимать ли данные при взаимодействии с сервером.
|
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
const DataTypeFactory & data_type_factory;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
2012-07-26 19:42:20 +00:00
|
|
|
|
Poco::Timespan connect_timeout;
|
|
|
|
|
Poco::Timespan receive_timeout;
|
|
|
|
|
Poco::Timespan send_timeout;
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
/// Откуда читать результат выполнения запроса.
|
|
|
|
|
SharedPtr<ReadBuffer> maybe_compressed_in;
|
|
|
|
|
BlockInputStreamPtr block_in;
|
|
|
|
|
|
|
|
|
|
/// Куда писать данные INSERT-а.
|
|
|
|
|
SharedPtr<WriteBuffer> maybe_compressed_out;
|
|
|
|
|
BlockOutputStreamPtr block_out;
|
|
|
|
|
|
2012-10-16 19:20:58 +00:00
|
|
|
|
Logger * log;
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
void connect();
|
2012-10-18 23:38:16 +00:00
|
|
|
|
void disconnect();
|
2012-05-16 18:03:00 +00:00
|
|
|
|
void sendHello();
|
|
|
|
|
void receiveHello();
|
|
|
|
|
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
|
|
|
|
}
|