-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathTableFunctionMySQL.cpp
More file actions
140 lines (112 loc) · 4.02 KB
/
Copy pathTableFunctionMySQL.cpp
File metadata and controls
140 lines (112 loc) · 4.02 KB
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
#include "config.h"
#if USE_MYSQL
#include <Storages/StorageMySQL.h>
#include <Core/Settings.h>
#include <Processors/Sources/MySQLSource.h>
#include <Interpreters/Context.h>
#include <Parsers/ASTFunction.h>
#include <Storages/MySQL/MySQLSettings.h>
#include <Storages/MySQL/MySQLHelpers.h>
#include <TableFunctions/ITableFunction.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <Common/Exception.h>
#include <Common/parseAddress.h>
#include <Common/quoteString.h>
#include <TableFunctions/registerTableFunctions.h>
#include <Databases/MySQL/DatabaseMySQL.h>
#include <Common/parseRemoteDescription.h>
namespace DB
{
namespace Setting
{
extern const SettingsUInt64 external_storage_connect_timeout_sec;
extern const SettingsUInt64 external_storage_rw_timeout_sec;
}
namespace MySQLSetting
{
extern const MySQLSettingsUInt64 connect_timeout;
extern const MySQLSettingsUInt64 read_write_timeout;
}
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
namespace
{
/* mysql ('host:port', database, table, user, password) - creates a temporary StorageMySQL.
* The structure of the table is taken from the mysql query DESCRIBE table.
* If there is no such table, an exception is thrown.
*/
class TableFunctionMySQL : public ITableFunction
{
public:
static constexpr auto name = "mysql";
std::string getName() const override
{
return name;
}
private:
StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override;
const char * getStorageEngineName() const override { return "MySQL"; }
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
mutable std::optional<mysqlxx::PoolWithFailover> pool;
std::optional<StorageMySQL::Configuration> configuration;
};
void TableFunctionMySQL::parseArguments(const ASTPtr & ast_function, ContextPtr context)
{
const auto & args_func = ast_function->as<ASTFunction &>();
if (!args_func.arguments)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function 'mysql' must have arguments.");
auto & args = args_func.arguments->children;
MySQLSettings mysql_settings;
const auto & settings = context->getSettingsRef();
mysql_settings[MySQLSetting::connect_timeout] = settings[Setting::external_storage_connect_timeout_sec];
mysql_settings[MySQLSetting::read_write_timeout] = settings[Setting::external_storage_rw_timeout_sec];
for (auto it = args.begin(); it != args.end(); ++it)
{
const ASTSetQuery * settings_ast = (*it)->as<ASTSetQuery>();
if (settings_ast)
{
mysql_settings.loadFromQuery(*settings_ast);
args.erase(it);
break;
}
}
configuration = StorageMySQL::getConfiguration(args, context, mysql_settings);
pool.emplace(createMySQLPoolWithFailover(*configuration, mysql_settings));
}
ColumnsDescription TableFunctionMySQL::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const
{
return StorageMySQL::getTableStructureFromData(*pool, configuration->database, configuration->table, context);
}
StoragePtr TableFunctionMySQL::executeImpl(
const ASTPtr & /*ast_function*/,
ContextPtr context,
const std::string & table_name,
ColumnsDescription cached_columns,
bool /*is_insert_query*/) const
{
auto res = std::make_shared<StorageMySQL>(
StorageID(getDatabaseName(), table_name),
std::move(*pool),
configuration->database,
configuration->table,
configuration->replace_query,
configuration->on_duplicate_clause,
cached_columns,
ConstraintsDescription{},
String{},
context,
MySQLSettings{});
pool.reset();
res->startup();
return res;
}
}
void registerTableFunctionMySQL(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionMySQL>({});
}
}
#endif