1#pragma once
2
3#include <Storages/IStorage.h>
4#include <Poco/URI.h>
5#include <ext/shared_ptr_helper.h>
6
7
8namespace DB
9{
10/**
11 * This class represents table engine for external urls.
12 * It sends HTTP GET to server when select is called and
13 * HTTP POST when insert is called. In POST request the data is send
14 * using Chunked transfer encoding, so server have to support it.
15 */
16class IStorageURLBase : public IStorage
17{
18public:
19 String getTableName() const override { return table_name; }
20 String getDatabaseName() const override { return database_name; }
21
22 BlockInputStreams read(
23 const Names & column_names,
24 const SelectQueryInfo & query_info,
25 const Context & context,
26 QueryProcessingStage::Enum processed_stage,
27 size_t max_block_size,
28 unsigned num_streams) override;
29
30 BlockOutputStreamPtr write(const ASTPtr & query, const Context & context) override;
31
32 void rename(const String & new_path_to_db, const String & new_database_name, const String & new_table_name, TableStructureWriteLockHolder &) override;
33
34protected:
35 IStorageURLBase(
36 const Poco::URI & uri_,
37 const Context & context_,
38 const std::string & database_name_,
39 const std::string & table_name_,
40 const String & format_name_,
41 const ColumnsDescription & columns_,
42 const ConstraintsDescription & constraints_,
43 const String & compression_method_);
44
45 Poco::URI uri;
46 const Context & context_global;
47 String compression_method;
48
49private:
50 String format_name;
51 String table_name;
52 String database_name;
53
54 virtual std::string getReadMethod() const;
55
56 virtual std::vector<std::pair<std::string, std::string>> getReadURIParams(
57 const Names & column_names,
58 const SelectQueryInfo & query_info,
59 const Context & context,
60 QueryProcessingStage::Enum & processed_stage,
61 size_t max_block_size) const;
62
63 virtual std::function<void(std::ostream &)> getReadPOSTDataCallback(
64 const Names & column_names,
65 const SelectQueryInfo & query_info,
66 const Context & context,
67 QueryProcessingStage::Enum & processed_stage,
68 size_t max_block_size) const;
69
70 virtual Block getHeaderBlock(const Names & column_names) const = 0;
71};
72
73
74class StorageURL : public ext::shared_ptr_helper<StorageURL>, public IStorageURLBase
75{
76 friend struct ext::shared_ptr_helper<StorageURL>;
77public:
78 StorageURL(
79 const Poco::URI & uri_,
80 const std::string & database_name_,
81 const std::string & table_name_,
82 const String & format_name_,
83 const ColumnsDescription & columns_,
84 const ConstraintsDescription & constraints_,
85 Context & context_,
86 const String & compression_method_)
87 : IStorageURLBase(uri_, context_, database_name_, table_name_, format_name_, columns_, constraints_, compression_method_)
88 {
89 }
90
91 String getName() const override
92 {
93 return "URL";
94 }
95
96 Block getHeaderBlock(const Names & /*column_names*/) const override
97 {
98 return getSampleBlock();
99 }
100};
101}
102