aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Core/ExternalTable.h
blob: f682bf9b27f56f85ac33634b9cd2ea26a9b3e451 (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
#pragma once

#include <Client/Connection.h>
#include <Core/Block.h>
#include <Interpreters/Context_fwd.h>
#include <IO/ReadBuffer.h>
#include <Server/HTTP/HTMLForm.h>

#include <iosfwd>
#include <memory>
#include <string>
#include <vector>


namespace Poco::Net
{
class NameValueCollection;
class MessageHeader;
}

namespace boost::program_options
{
class variables_map;
}


namespace DB
{

/// The base class containing the basic information about external table and
/// basic functions for extracting this information from text fields.
class BaseExternalTable
{
public:
    std::string file;       /// File with data or '-' if stdin
    std::string name;       /// The name of the table
    std::string format;     /// Name of the data storage format

    /// Description of the table structure: (column name, data type name)
    std::vector<std::pair<std::string, std::string>> structure;

    std::unique_ptr<ReadBuffer> read_buffer;
    Block sample_block;

    virtual ~BaseExternalTable() = default;

    /// Initialize read_buffer, depending on the data source. By default, does nothing.
    virtual void initReadBuffer() {}

    /// Get the table data - a pair (a stream with the contents of the table, the name of the table)
    ExternalTableDataPtr getData(ContextPtr context);

protected:
    /// Clear all accumulated information
    void clear();

    /// Construct the `structure` vector from the text field `structure`
    virtual void parseStructureFromStructureField(const std::string & argument);

    /// Construct the `structure` vector from the text field `types`
    virtual void parseStructureFromTypesField(const std::string & argument);

private:
    /// Initialize sample_block according to the structure of the table stored in the `structure`
    void initSampleBlock();
};


/// Parsing of external table used in the tcp client.
class ExternalTable : public BaseExternalTable
{
public:
    void initReadBuffer() override;

    /// Extract parameters from variables_map, which is built on the client command line
    explicit ExternalTable(const boost::program_options::variables_map & external_options);
};


/// Parsing of external table used when sending tables via http
/// The `handlePart` function will be called for each table passed,
/// so it's also necessary to call `clean` at the end of the `handlePart`.
class ExternalTablesHandler : public HTMLForm::PartHandler, BaseExternalTable, WithMutableContext
{
public:
    ExternalTablesHandler(ContextMutablePtr context_, const Poco::Net::NameValueCollection & params_) : WithMutableContext(context_), params(params_) {}

    void handlePart(const Poco::Net::MessageHeader & header, ReadBuffer & stream) override;

private:
    const Poco::Net::NameValueCollection & params;
};


}