aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/clickhouse/client/query.h
blob: fc5879b4f593095a04bed9a6c41573925ab7c83c (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once

#include "block.h"

#include <util/generic/string.h>

#include <cstdint>
#include <functional>
#include <memory>

namespace NClickHouse {
    /**
 * Settings of individual query.
 */
    struct TQuerySettings {
        /// Максимальное количество потоков выполнения запроса. По-умолчанию - определять автоматически.
        int MaxThreads = 0;
        /// Считать минимумы и максимумы столбцов результата.
        bool Extremes = false;
        /// Тихо пропускать недоступные шарды.
        bool SkipUnavailableShards = false;
        /// Write statistics about read rows, bytes, time elapsed, etc.
        bool OutputFormatWriteStatistics = true;
        /// Use client timezone for interpreting DateTime string values, instead of adopting server timezone.
        bool UseClientTimeZone = false;

        // connect_timeout
        // max_block_size
        // distributed_group_by_no_merge = false
        // strict_insert_defaults = 0
        // network_compression_method = LZ4
        // priority = 0
    };

    struct TException {
        int Code = 0;
        TString Name;
        TString DisplayText;
        TString StackTrace;
        /// Pointer to nested exception.
        std::unique_ptr<TException> Nested;
    };

    struct TProfile {
        ui64 rows = 0;
        ui64 blocks = 0;
        ui64 bytes = 0;
        ui64 rows_before_limit = 0;
        bool applied_limit = false;
        bool calculated_rows_before_limit = false;
    };

    struct TProgress {
        ui64 rows = 0;
        ui64 bytes = 0;
        ui64 total_rows = 0;
    };

    class TQueryEvents {
    public:
        virtual ~TQueryEvents() {
        }

        /// Some data was received.
        virtual void OnData(const TBlock& block) = 0;

        virtual void OnServerException(const TException& e) = 0;

        virtual void OnProfile(const TProfile& profile) = 0;

        virtual void OnProgress(const TProgress& progress) = 0;

        virtual void OnFinish() = 0;
    };

    using TExceptionCallback = std::function<void(const TException& e)>;
    using TProfileCallback = std::function<void(const TProfile& profile)>;
    using TProgressCallback = std::function<void(const TProgress& progress)>;
    using TSelectCallback = std::function<void(const TBlock& block)>;

    class TQuery: public TQueryEvents {
    public:
        TQuery();
        TQuery(const char* query);
        TQuery(const TString& query);
        ~TQuery();

        ///
        inline TString GetText() const {
            return Query_;
        }

        /// Set handler for receiving result data.
        inline TQuery& OnData(TSelectCallback cb) {
            SelectCb_ = cb;
            return *this;
        }

        /// Set handler for receiving server's exception.
        inline TQuery& OnException(TExceptionCallback cb) {
            ExceptionCb_ = cb;
            return *this;
        }

        /// Set handler for receiving a profile of query execution.
        inline TQuery& OnProfile(TProfileCallback pb) {
            ProfileCb_ = pb;
            return *this;
        }

        /// Set handler for receiving a progress of query exceution.
        inline TQuery& OnProgress(TProgressCallback cb) {
            ProgressCb_ = cb;
            return *this;
        }

    private:
        void OnData(const TBlock& block) override {
            if (SelectCb_) {
                SelectCb_(block);
            }
        }

        void OnServerException(const TException& e) override {
            if (ExceptionCb_) {
                ExceptionCb_(e);
            }
        }

        void OnProfile(const TProfile& profile) override {
            if (ProfileCb_) {
                ProfileCb_(profile);
            }
        }

        void OnProgress(const TProgress& progress) override {
            if (ProgressCb_) {
                ProgressCb_(progress);
            }
        }

        void OnFinish() override {
        }

    private:
        TString Query_;
        TExceptionCallback ExceptionCb_;
        TProfileCallback ProfileCb_;
        TProgressCallback ProgressCb_;
        TSelectCallback SelectCb_;
    };

}