2022-09-28 13:29:29 +00:00
|
|
|
#include "config.h"
|
2017-12-28 04:29:53 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if USE_MYSQL
|
2023-11-06 03:33:23 +00:00
|
|
|
|
|
|
|
#include <Storages/StorageMySQL.h>
|
2021-10-15 20:18:20 +00:00
|
|
|
#include <Processors/Sources/MySQLSource.h>
|
2021-03-31 14:02:51 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2021-05-15 04:40:43 +00:00
|
|
|
#include <Storages/MySQL/MySQLSettings.h>
|
2021-12-13 22:06:46 +00:00
|
|
|
#include <Storages/MySQL/MySQLHelpers.h>
|
2021-03-31 14:02:51 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/parseAddress.h>
|
|
|
|
#include <Common/quoteString.h>
|
|
|
|
#include "registerTableFunctions.h"
|
|
|
|
|
2023-05-19 00:44:27 +00:00
|
|
|
#include <Databases/MySQL/DatabaseMySQL.h>
|
2021-03-31 14:02:51 +00:00
|
|
|
#include <Common/parseRemoteDescription.h>
|
2020-09-09 12:18:02 +00:00
|
|
|
|
2017-12-28 04:28:05 +00:00
|
|
|
|
2017-12-05 13:32:02 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2017-12-28 04:28:05 +00:00
|
|
|
|
2017-12-05 13:32:02 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2017-12-05 13:32:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:33:23 +00:00
|
|
|
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 * getStorageTypeName() 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;
|
|
|
|
};
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void TableFunctionMySQL::parseArguments(const ASTPtr & ast_function, ContextPtr context)
|
2017-12-05 13:32:02 +00:00
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & args_func = ast_function->as<ASTFunction &>();
|
2017-12-05 13:32:02 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
if (!args_func.arguments)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function 'mysql' must have arguments.");
|
2017-12-05 13:32:02 +00:00
|
|
|
|
2022-08-29 13:41:32 +00:00
|
|
|
auto & args = args_func.arguments->children;
|
|
|
|
|
2021-12-13 22:06:46 +00:00
|
|
|
MySQLSettings mysql_settings;
|
2022-08-29 13:41:32 +00:00
|
|
|
|
2021-12-13 22:06:46 +00:00
|
|
|
const auto & settings = context->getSettingsRef();
|
2021-12-15 08:18:43 +00:00
|
|
|
mysql_settings.connect_timeout = settings.external_storage_connect_timeout_sec;
|
|
|
|
mysql_settings.read_write_timeout = settings.external_storage_rw_timeout_sec;
|
2022-08-29 13:41:32 +00:00
|
|
|
|
2022-10-20 09:43:55 +00:00
|
|
|
for (auto * it = args.begin(); it != args.end(); ++it)
|
2022-08-29 13:41:32 +00:00
|
|
|
{
|
|
|
|
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);
|
2021-12-13 22:06:46 +00:00
|
|
|
pool.emplace(createMySQLPoolWithFailover(*configuration, mysql_settings));
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 08:56:07 +00:00
|
|
|
ColumnsDescription TableFunctionMySQL::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2023-05-19 00:44:27 +00:00
|
|
|
return StorageMySQL::getTableStructureFromData(*pool, configuration->database, configuration->table, context);
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 21:29:37 +00:00
|
|
|
StoragePtr TableFunctionMySQL::executeImpl(
|
|
|
|
const ASTPtr & /*ast_function*/,
|
|
|
|
ContextPtr context,
|
|
|
|
const std::string & table_name,
|
2023-11-09 15:50:10 +00:00
|
|
|
ColumnsDescription cached_columns,
|
2023-07-06 08:56:07 +00:00
|
|
|
bool /*is_insert_query*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2022-05-04 10:01:29 +00:00
|
|
|
auto res = std::make_shared<StorageMySQL>(
|
2019-12-04 16:06:55 +00:00
|
|
|
StorageID(getDatabaseName(), table_name),
|
2020-10-14 12:19:29 +00:00
|
|
|
std::move(*pool),
|
2021-09-03 11:16:32 +00:00
|
|
|
configuration->database,
|
|
|
|
configuration->table,
|
|
|
|
configuration->replace_query,
|
|
|
|
configuration->on_duplicate_clause,
|
2023-11-09 15:50:10 +00:00
|
|
|
cached_columns,
|
2019-08-24 21:20:20 +00:00
|
|
|
ConstraintsDescription{},
|
2021-04-23 12:18:23 +00:00
|
|
|
String{},
|
2021-05-15 04:40:43 +00:00
|
|
|
context,
|
|
|
|
MySQLSettings{});
|
2017-12-26 21:34:06 +00:00
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
pool.reset();
|
|
|
|
|
2017-12-05 13:32:02 +00:00
|
|
|
res->startup();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-11-06 03:33:23 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 13:32:02 +00:00
|
|
|
|
|
|
|
void registerTableFunctionMySQL(TableFunctionFactory & factory)
|
|
|
|
{
|
2017-12-28 04:28:05 +00:00
|
|
|
factory.registerFunction<TableFunctionMySQL>();
|
2017-12-05 13:32:02 +00:00
|
|
|
}
|
2023-11-06 03:33:23 +00:00
|
|
|
|
2017-12-05 13:32:02 +00:00
|
|
|
}
|
2017-12-28 04:29:53 +00:00
|
|
|
|
|
|
|
#endif
|