aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Client/Suggest.h
blob: 5cecdc4501b0cf2f780d3f319e916e065e2e5756 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once

#include "ConnectionParameters.h"

#include <Client/Connection.h>
#include <Client/IServerConnection.h>
#include <Client/LocalConnection.h>
#include <Client/LineReader.h>
#include <IO/ConnectionTimeouts.h>
#include <atomic>
#include <thread>


namespace DB
{

class Suggest : public LineReader::Suggest, boost::noncopyable
{
public:
    Suggest();

    ~Suggest()
    {
        if (loading_thread.joinable())
            loading_thread.join();
    }

    /// Load suggestions for clickhouse-client.
    template <typename ConnectionType>
    void load(ContextPtr context, const ConnectionParameters & connection_parameters, Int32 suggestion_limit);

    void load(IServerConnection & connection,
              const ConnectionTimeouts & timeouts,
              Int32 suggestion_limit);

    /// Older server versions cannot execute the query loading suggestions.
    static constexpr int MIN_SERVER_REVISION = DBMS_MIN_PROTOCOL_VERSION_WITH_VIEW_IF_PERMITTED;

    int getLastError() const { return last_error.load(); }

private:
    void fetch(IServerConnection & connection, const ConnectionTimeouts & timeouts, const std::string & query);

    void fillWordsFromBlock(const Block & block);

    /// Words are fetched asynchronously.
    std::thread loading_thread;

    std::atomic<int> last_error { -1 };
};

}