1 | #pragma once |
2 | |
3 | #include <string> |
4 | #include <vector> |
5 | #include <memory> |
6 | #include <iosfwd> |
7 | |
8 | #include <Poco/Net/PartHandler.h> |
9 | |
10 | #include <Core/Block.h> |
11 | #include <Client/Connection.h> |
12 | #include <IO/ReadBuffer.h> |
13 | |
14 | |
15 | namespace Poco |
16 | { |
17 | namespace Net |
18 | { |
19 | class NameValueCollection; |
20 | class ; |
21 | } |
22 | } |
23 | |
24 | namespace boost |
25 | { |
26 | namespace program_options |
27 | { |
28 | class variables_map; |
29 | } |
30 | } |
31 | |
32 | |
33 | namespace DB |
34 | { |
35 | |
36 | class Context; |
37 | |
38 | |
39 | /// The base class containing the basic information about external table and |
40 | /// basic functions for extracting this information from text fields. |
41 | class BaseExternalTable |
42 | { |
43 | public: |
44 | std::string file; /// File with data or '-' if stdin |
45 | std::string name; /// The name of the table |
46 | std::string format; /// Name of the data storage format |
47 | |
48 | /// Description of the table structure: (column name, data type name) |
49 | std::vector<std::pair<std::string, std::string>> structure; |
50 | |
51 | std::unique_ptr<ReadBuffer> read_buffer; |
52 | Block sample_block; |
53 | |
54 | virtual ~BaseExternalTable() {} |
55 | |
56 | /// Initialize read_buffer, depending on the data source. By default, does nothing. |
57 | virtual void initReadBuffer() {} |
58 | |
59 | /// Get the table data - a pair (a stream with the contents of the table, the name of the table) |
60 | ExternalTableData getData(const Context & context); |
61 | |
62 | protected: |
63 | /// Clear all accumulated information |
64 | void clean(); |
65 | |
66 | /// Function for debugging information output |
67 | void write(); |
68 | |
69 | static std::vector<std::string> split(const std::string & s, const std::string & d); |
70 | |
71 | /// Construct the `structure` vector from the text field `structure` |
72 | virtual void parseStructureFromStructureField(const std::string & argument); |
73 | |
74 | /// Construct the `structure` vector from the text field `types` |
75 | virtual void parseStructureFromTypesField(const std::string & argument); |
76 | |
77 | private: |
78 | /// Initialize sample_block according to the structure of the table stored in the `structure` |
79 | void initSampleBlock(); |
80 | }; |
81 | |
82 | |
83 | /// Parsing of external table used in the tcp client. |
84 | class ExternalTable : public BaseExternalTable |
85 | { |
86 | public: |
87 | void initReadBuffer() override; |
88 | |
89 | /// Extract parameters from variables_map, which is built on the client command line |
90 | ExternalTable(const boost::program_options::variables_map & external_options); |
91 | }; |
92 | |
93 | |
94 | /// Parsing of external table used when sending tables via http |
95 | /// The `handlePart` function will be called for each table passed, |
96 | /// so it's also necessary to call `clean` at the end of the `handlePart`. |
97 | class ExternalTablesHandler : public Poco::Net::PartHandler, BaseExternalTable |
98 | { |
99 | public: |
100 | ExternalTablesHandler(Context & context_, const Poco::Net::NameValueCollection & params_) : context(context_), params(params_) {} |
101 | |
102 | void handlePart(const Poco::Net::MessageHeader & , std::istream & stream); |
103 | |
104 | private: |
105 | Context & context; |
106 | const Poco::Net::NameValueCollection & params; |
107 | std::unique_ptr<ReadBuffer> read_buffer_impl; |
108 | }; |
109 | |
110 | |
111 | } |
112 | |