1#pragma once
2
3#include <memory>
4#include <Poco/Logger.h>
5#include <Client/ConnectionPoolWithFailover.h>
6#include <Interpreters/Context.h>
7#include "DictionaryStructure.h"
8#include "ExternalQueryBuilder.h"
9#include "IDictionarySource.h"
10
11
12namespace DB
13{
14/** Allows loading dictionaries from local or remote ClickHouse instance
15 * @todo use ConnectionPoolWithFailover
16 * @todo invent a way to keep track of source modifications
17 */
18class ClickHouseDictionarySource final : public IDictionarySource
19{
20public:
21 ClickHouseDictionarySource(
22 const DictionaryStructure & dict_struct_,
23 const Poco::Util::AbstractConfiguration & config,
24 const std::string & config_prefix,
25 const Block & sample_block_,
26 const Context & context);
27
28 /// copy-constructor is provided in order to support cloneability
29 ClickHouseDictionarySource(const ClickHouseDictionarySource & other);
30 ClickHouseDictionarySource & operator=(const ClickHouseDictionarySource &) = delete;
31
32 BlockInputStreamPtr loadAll() override;
33
34 BlockInputStreamPtr loadUpdatedAll() override;
35
36 BlockInputStreamPtr loadIds(const std::vector<UInt64> & ids) override;
37
38 BlockInputStreamPtr loadKeys(const Columns & key_columns, const std::vector<size_t> & requested_rows) override;
39
40 bool isModified() const override;
41 bool supportsSelectiveLoad() const override { return true; }
42
43 bool hasUpdateField() const override;
44
45 DictionarySourcePtr clone() const override { return std::make_unique<ClickHouseDictionarySource>(*this); }
46
47 std::string toString() const override;
48
49private:
50 std::string getUpdateFieldAndDate();
51
52 BlockInputStreamPtr createStreamForSelectiveLoad(const std::string & query);
53
54 std::string doInvalidateQuery(const std::string & request) const;
55
56 std::chrono::time_point<std::chrono::system_clock> update_time;
57 const DictionaryStructure dict_struct;
58 const std::string host;
59 const UInt16 port;
60 const bool secure;
61 const std::string user;
62 const std::string password;
63 const std::string db;
64 const std::string table;
65 const std::string where;
66 const std::string update_field;
67 std::string invalidate_query;
68 mutable std::string invalidate_query_response;
69 ExternalQueryBuilder query_builder;
70 Block sample_block;
71 Context context;
72 const bool is_local;
73 ConnectionPoolWithFailoverPtr pool;
74 const std::string load_all_query;
75 Poco::Logger * log = &Poco::Logger::get("ClickHouseDictionarySource");
76};
77
78}
79