1#pragma once
2
3#include <string>
4#include <Columns/IColumn.h>
5#include <Formats/FormatSettings.h>
6#include <Parsers/IdentifierQuotingStyle.h>
7
8
9namespace DB
10{
11struct DictionaryStructure;
12class WriteBuffer;
13
14
15/** Builds a query to load data from external database.
16 */
17struct ExternalQueryBuilder
18{
19 const DictionaryStructure & dict_struct;
20 std::string db;
21 std::string table;
22 std::string schema;
23 const std::string & where;
24
25 IdentifierQuotingStyle quoting_style;
26
27
28 ExternalQueryBuilder(
29 const DictionaryStructure & dict_struct_,
30 const std::string & db_,
31 const std::string & table_,
32 const std::string & where_,
33 IdentifierQuotingStyle quoting_style_);
34
35 /** Generate a query to load all data. */
36 std::string composeLoadAllQuery() const;
37
38 /** Generate a query to load data after certain time point */
39 std::string composeUpdateQuery(const std::string & update_field, const std::string & time_point) const;
40
41 /** Generate a query to load data by set of UInt64 keys. */
42 std::string composeLoadIdsQuery(const std::vector<UInt64> & ids);
43
44 /** Generate a query to load data by set of composite keys.
45 * There are two methods of specification of composite keys in WHERE:
46 * 1. (x = c11 AND y = c12) OR (x = c21 AND y = c22) ...
47 * 2. (x, y) IN ((c11, c12), (c21, c22), ...)
48 */
49 enum LoadKeysMethod
50 {
51 AND_OR_CHAIN,
52 IN_WITH_TUPLES,
53 };
54
55 std::string composeLoadKeysQuery(const Columns & key_columns, const std::vector<size_t> & requested_rows, LoadKeysMethod method);
56
57
58private:
59 const FormatSettings format_settings;
60
61 /// Expression in form (x = c1 AND y = c2 ...)
62 void composeKeyCondition(const Columns & key_columns, const size_t row, WriteBuffer & out) const;
63
64 /// Expression in form (x, y, ...)
65 std::string composeKeyTupleDefinition() const;
66
67 /// Expression in form (c1, c2, ...)
68 void composeKeyTuple(const Columns & key_columns, const size_t row, WriteBuffer & out) const;
69
70 /// Write string with specified quoting style.
71 void writeQuoted(const std::string & s, WriteBuffer & out) const;
72};
73
74}
75