ClickHouse/src/TableFunctions/TableFunctionMySQL.cpp

99 lines
3.0 KiB
C++
Raw Normal View History

#if !defined(ARCADIA_BUILD)
#include "config_core.h"
#endif
2017-12-28 04:29:53 +00:00
#if USE_MYSQL
#include <Core/Defines.h>
#include <Databases/MySQL/FetchTablesColumnsList.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/convertMySQLDataType.h>
2021-08-11 16:44:34 +00:00
#include <Formats/MySQLSource.h>
#include <IO/Operators.h>
#include <Interpreters/Context.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTLiteral.h>
#include <Storages/StorageMySQL.h>
#include <Storages/MySQL/MySQLSettings.h>
#include <TableFunctions/ITableFunction.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <TableFunctions/TableFunctionMySQL.h>
#include <Common/Exception.h>
#include <Common/parseAddress.h>
#include <Common/quoteString.h>
#include "registerTableFunctions.h"
2021-06-17 19:26:34 +00:00
#include <Databases/MySQL/DatabaseMySQL.h> // for fetchTablesColumnsList
#include <Common/parseRemoteDescription.h>
2017-12-28 04:28:05 +00:00
namespace DB
{
2017-12-28 04:28:05 +00:00
namespace ErrorCodes
{
2020-02-25 18:02:41 +00:00
extern const int LOGICAL_ERROR;
extern const int UNKNOWN_TABLE;
}
void TableFunctionMySQL::parseArguments(const ASTPtr & ast_function, ContextPtr context)
{
const auto & args_func = ast_function->as<ASTFunction &>();
if (!args_func.arguments)
2017-12-27 21:45:05 +00:00
throw Exception("Table function 'mysql' must have arguments.", ErrorCodes::LOGICAL_ERROR);
2021-09-03 11:16:32 +00:00
configuration = StorageMySQL::getConfiguration(args_func.arguments->children, context);
pool.emplace(configuration->database, configuration->addresses, configuration->username, configuration->password);
}
ColumnsDescription TableFunctionMySQL::getActualTableStructure(ContextPtr context) const
{
const auto & settings = context->getSettingsRef();
2021-09-03 11:16:32 +00:00
const auto tables_and_columns = fetchTablesColumnsList(*pool, configuration->database, {configuration->table}, settings, settings.mysql_datatypes_support_level);
2017-12-26 21:34:06 +00:00
2021-09-03 11:16:32 +00:00
const auto columns = tables_and_columns.find(configuration->table);
if (columns == tables_and_columns.end())
2021-09-03 11:16:32 +00:00
throw Exception("MySQL table " + (configuration->database.empty() ? "" : (backQuote(configuration->database) + "."))
+ backQuote(configuration->table) + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE);
2019-07-19 13:28:28 +00:00
return columns->second;
}
StoragePtr TableFunctionMySQL::executeImpl(
const ASTPtr & /*ast_function*/,
ContextPtr context,
const std::string & table_name,
ColumnsDescription /*cached_columns*/) const
{
auto columns = getActualTableStructure(context);
auto res = StorageMySQL::create(
2019-12-04 16:06:55 +00:00
StorageID(getDatabaseName(), table_name),
std::move(*pool),
2021-09-03 11:16:32 +00:00
configuration->database,
configuration->table,
configuration->replace_query,
configuration->on_duplicate_clause,
columns,
2019-08-24 21:20:20 +00:00
ConstraintsDescription{},
2021-04-23 12:18:23 +00:00
String{},
context,
MySQLSettings{});
2017-12-26 21:34:06 +00:00
pool.reset();
res->startup();
return res;
}
void registerTableFunctionMySQL(TableFunctionFactory & factory)
{
2017-12-28 04:28:05 +00:00
factory.registerFunction<TableFunctionMySQL>();
}
}
2017-12-28 04:29:53 +00:00
#endif