2020-12-09 21:14:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-09-28 13:29:29 +00:00
|
|
|
#include "config.h"
|
2020-12-13 22:48:40 +00:00
|
|
|
#include "DictionaryStructure.h"
|
|
|
|
#include "IDictionarySource.h"
|
2020-12-09 21:14:16 +00:00
|
|
|
|
|
|
|
#if USE_LIBPQXX
|
2020-12-13 22:48:40 +00:00
|
|
|
#include "ExternalQueryBuilder.h"
|
2020-12-09 21:14:16 +00:00
|
|
|
#include <Core/Block.h>
|
2021-12-21 13:41:53 +00:00
|
|
|
#include <Common/LocalDateTime.h>
|
2021-05-08 14:54:44 +00:00
|
|
|
#include <Core/PostgreSQL/PoolWithFailover.h>
|
2020-12-09 21:14:16 +00:00
|
|
|
|
2021-01-11 10:23:44 +00:00
|
|
|
|
2020-12-09 21:14:16 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Allows loading dictionaries from a PostgreSQL database
|
|
|
|
class PostgreSQLDictionarySource final : public IDictionarySource
|
|
|
|
{
|
|
|
|
public:
|
2021-06-10 08:34:57 +00:00
|
|
|
struct Configuration
|
|
|
|
{
|
|
|
|
const String db;
|
|
|
|
const String schema;
|
|
|
|
const String table;
|
2021-07-29 20:57:06 +00:00
|
|
|
const String query;
|
2021-06-10 08:34:57 +00:00
|
|
|
const String where;
|
|
|
|
const String invalidate_query;
|
|
|
|
const String update_field;
|
|
|
|
const UInt64 update_lag;
|
|
|
|
};
|
|
|
|
|
2020-12-09 21:14:16 +00:00
|
|
|
PostgreSQLDictionarySource(
|
|
|
|
const DictionaryStructure & dict_struct_,
|
2021-06-10 08:34:57 +00:00
|
|
|
const Configuration & configuration_,
|
2021-05-06 07:02:13 +00:00
|
|
|
postgres::PoolWithFailoverPtr pool_,
|
2020-12-09 21:14:16 +00:00
|
|
|
const Block & sample_block_);
|
|
|
|
|
|
|
|
/// copy-constructor is provided in order to support cloneability
|
|
|
|
PostgreSQLDictionarySource(const PostgreSQLDictionarySource & other);
|
|
|
|
PostgreSQLDictionarySource & operator=(const PostgreSQLDictionarySource &) = delete;
|
|
|
|
|
2022-05-20 19:49:31 +00:00
|
|
|
QueryPipeline loadAll() override;
|
|
|
|
QueryPipeline loadUpdatedAll() override;
|
|
|
|
QueryPipeline loadIds(const std::vector<UInt64> & ids) override;
|
|
|
|
QueryPipeline loadKeys(const Columns & key_columns, const std::vector<size_t> & requested_rows) override;
|
2020-12-09 21:14:16 +00:00
|
|
|
|
|
|
|
bool isModified() const override;
|
|
|
|
bool supportsSelectiveLoad() const override;
|
|
|
|
bool hasUpdateField() const override;
|
|
|
|
|
|
|
|
DictionarySourcePtr clone() const override;
|
|
|
|
std::string toString() const override;
|
|
|
|
|
|
|
|
private:
|
2021-05-09 15:42:54 +00:00
|
|
|
String getUpdateFieldAndDate();
|
|
|
|
String doInvalidateQuery(const std::string & request) const;
|
2022-05-20 19:49:31 +00:00
|
|
|
QueryPipeline loadBase(const String & query);
|
2020-12-09 21:14:16 +00:00
|
|
|
|
|
|
|
const DictionaryStructure dict_struct;
|
2021-06-10 08:34:57 +00:00
|
|
|
const Configuration configuration;
|
2021-05-05 22:46:52 +00:00
|
|
|
postgres::PoolWithFailoverPtr pool;
|
2021-06-10 08:34:57 +00:00
|
|
|
Block sample_block;
|
2020-12-09 21:14:16 +00:00
|
|
|
Poco::Logger * log;
|
|
|
|
ExternalQueryBuilder query_builder;
|
|
|
|
const std::string load_all_query;
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> update_time;
|
|
|
|
mutable std::string invalidate_query_response;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-08-06 08:41:45 +00:00
|
|
|
#endif
|