2020-11-20 22:47:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
#include "config_core.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_LIBPQXX
|
|
|
|
#include <ext/shared_ptr_helper.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Storages/IStorage.h>
|
2020-11-25 14:52:11 +00:00
|
|
|
#include <DataStreams/IBlockOutputStream.h>
|
2020-11-20 22:47:04 +00:00
|
|
|
#include "pqxx/pqxx"
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-11-25 21:06:33 +00:00
|
|
|
|
2021-01-09 22:42:42 +00:00
|
|
|
class PostgreSQLConnection;
|
|
|
|
using PostgreSQLConnectionPtr = std::shared_ptr<PostgreSQLConnection>;
|
2020-11-25 21:06:33 +00:00
|
|
|
using ConnectionPtr = std::shared_ptr<pqxx::connection>;
|
|
|
|
|
2020-11-20 22:47:04 +00:00
|
|
|
class StoragePostgreSQL final : public ext::shared_ptr_helper<StoragePostgreSQL>, public IStorage
|
|
|
|
{
|
|
|
|
friend struct ext::shared_ptr_helper<StoragePostgreSQL>;
|
|
|
|
public:
|
|
|
|
StoragePostgreSQL(
|
|
|
|
const StorageID & table_id_,
|
|
|
|
const std::string & remote_table_name_,
|
2021-01-09 22:42:42 +00:00
|
|
|
PostgreSQLConnectionPtr connection_,
|
2020-11-20 22:47:04 +00:00
|
|
|
const ColumnsDescription & columns_,
|
|
|
|
const ConstraintsDescription & constraints_,
|
|
|
|
const Context & context_);
|
|
|
|
|
|
|
|
String getName() const override { return "PostgreSQL"; }
|
|
|
|
|
|
|
|
Pipe read(
|
|
|
|
const Names & column_names,
|
|
|
|
const StorageMetadataPtr & /*metadata_snapshot*/,
|
|
|
|
SelectQueryInfo & query_info,
|
|
|
|
const Context & context,
|
|
|
|
QueryProcessingStage::Enum processed_stage,
|
|
|
|
size_t max_block_size,
|
|
|
|
unsigned num_streams) override;
|
|
|
|
|
2020-11-25 14:52:11 +00:00
|
|
|
BlockOutputStreamPtr write(const ASTPtr & query, const StorageMetadataPtr & /*metadata_snapshot*/, const Context & context) override;
|
|
|
|
|
2020-11-20 22:47:04 +00:00
|
|
|
private:
|
2020-11-25 14:52:11 +00:00
|
|
|
friend class PostgreSQLBlockOutputStream;
|
|
|
|
|
2020-11-20 22:47:04 +00:00
|
|
|
String remote_table_name;
|
|
|
|
Context global_context;
|
2021-01-09 22:42:42 +00:00
|
|
|
PostgreSQLConnectionPtr connection;
|
2020-11-25 14:52:11 +00:00
|
|
|
};
|
2020-12-21 19:20:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Tiny connection class to make it more convenient to use.
|
2020-12-31 11:15:05 +00:00
|
|
|
/// Connection is not made until actually used.
|
2021-01-09 22:42:42 +00:00
|
|
|
class PostgreSQLConnection
|
2020-12-21 19:20:56 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-01-09 22:42:42 +00:00
|
|
|
PostgreSQLConnection(const std::string & connection_str_) : connection_str(connection_str_) {}
|
|
|
|
PostgreSQLConnection(const PostgreSQLConnection &) = delete;
|
|
|
|
PostgreSQLConnection operator =(const PostgreSQLConnection &) = delete;
|
2020-12-21 19:20:56 +00:00
|
|
|
|
|
|
|
ConnectionPtr conn()
|
|
|
|
{
|
|
|
|
checkUpdateConnection();
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string & conn_str() { return connection_str; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ConnectionPtr connection;
|
|
|
|
std::string connection_str;
|
|
|
|
|
|
|
|
void checkUpdateConnection()
|
|
|
|
{
|
|
|
|
if (!connection || !connection->is_open())
|
|
|
|
connection = std::make_unique<pqxx::connection>(connection_str);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-20 22:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|