#include #if USE_MYSQL #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; } StorageMySQL::StorageMySQL( const std::string & name, mysqlxx::Pool && pool, const std::string & remote_database_name, const std::string & remote_table_name, const NamesAndTypesList & columns) : name(name) , remote_database_name(remote_database_name) , remote_table_name(remote_table_name) , columns(columns) , pool(std::move(pool)) { } BlockInputStreams StorageMySQL::read( const Names & column_names, const SelectQueryInfo & query_info, const Context & context, QueryProcessingStage::Enum & processed_stage, size_t max_block_size, unsigned) { check(column_names); processed_stage = QueryProcessingStage::FetchColumns; String query = transformQueryForExternalDatabase(*query_info.query, columns, remote_database_name, remote_table_name, context); Block sample_block; for (const String & name : column_names) { auto column_data = getColumn(name); sample_block.insert({ column_data.type, column_data.name }); } return { std::make_shared(pool.Get(), query, sample_block, max_block_size) }; } void registerStorageMySQL(StorageFactory & factory) { factory.registerStorage("MySQL", [](const StorageFactory::Arguments & args) { ASTs & engine_args = args.engine_args; if (engine_args.size() != 5) throw Exception( "Storage MySQL requires exactly 5 parameters: MySQL('host:port', database, table, 'user', 'password').", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); for (size_t i = 0; i < 5; ++i) engine_args[i] = evaluateConstantExpressionOrIdentifierAsLiteral(engine_args[i], args.local_context); /// 3306 is the default MySQL port. auto parsed_host_port = parseAddress(static_cast(*engine_args[0]).value.safeGet(), 3306); const String & remote_database = static_cast(*engine_args[1]).value.safeGet(); const String & remote_table = static_cast(*engine_args[2]).value.safeGet(); const String & username = static_cast(*engine_args[3]).value.safeGet(); const String & password = static_cast(*engine_args[4]).value.safeGet(); mysqlxx::Pool pool(remote_database, parsed_host_port.first, username, password, parsed_host_port.second); return StorageMySQL::create( args.table_name, std::move(pool), remote_database, remote_table, args.columns); }); } } #endif