2017-12-05 13:32:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-09-28 13:29:29 +00:00
|
|
|
#include "config.h"
|
2020-04-16 12:31:57 +00:00
|
|
|
|
2017-12-29 15:48:20 +00:00
|
|
|
#if USE_MYSQL
|
|
|
|
|
2021-03-24 18:15:31 +00:00
|
|
|
#include <Storages/IStorage.h>
|
2021-05-15 04:40:43 +00:00
|
|
|
#include <Storages/MySQL/MySQLSettings.h>
|
2021-03-24 18:15:31 +00:00
|
|
|
#include <mysqlxx/PoolWithFailover.h>
|
2017-12-05 13:32:02 +00:00
|
|
|
|
2021-09-28 22:17:26 +00:00
|
|
|
namespace Poco
|
|
|
|
{
|
|
|
|
class Logger;
|
|
|
|
}
|
2017-12-13 16:46:57 +00:00
|
|
|
|
2017-12-05 13:32:02 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2023-02-20 20:37:38 +00:00
|
|
|
class NamedCollection;
|
|
|
|
|
2017-12-26 21:34:06 +00:00
|
|
|
/** Implements storage in the MySQL database.
|
|
|
|
* Use ENGINE = mysql(host_port, database_name, table_name, user_name, password)
|
|
|
|
*/
|
2022-05-03 06:43:28 +00:00
|
|
|
class StorageMySQL final : public IStorage, WithContext
|
2017-12-05 13:32:02 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-12-26 21:34:06 +00:00
|
|
|
StorageMySQL(
|
2019-12-04 16:06:55 +00:00
|
|
|
const StorageID & table_id_,
|
2021-03-24 18:15:31 +00:00
|
|
|
mysqlxx::PoolWithFailover && pool_,
|
2019-08-03 11:02:40 +00:00
|
|
|
const std::string & remote_database_name_,
|
|
|
|
const std::string & remote_table_name_,
|
2021-04-10 23:33:54 +00:00
|
|
|
bool replace_query_,
|
2019-08-03 11:02:40 +00:00
|
|
|
const std::string & on_duplicate_clause_,
|
|
|
|
const ColumnsDescription & columns_,
|
2019-08-24 21:20:20 +00:00
|
|
|
const ConstraintsDescription & constraints_,
|
2021-04-23 12:18:23 +00:00
|
|
|
const String & comment,
|
2021-05-15 04:40:43 +00:00
|
|
|
ContextPtr context_,
|
|
|
|
const MySQLSettings & mysql_settings_);
|
2017-12-05 13:32:02 +00:00
|
|
|
|
2017-12-26 21:34:06 +00:00
|
|
|
std::string getName() const override { return "MySQL"; }
|
2017-12-05 13:32:02 +00:00
|
|
|
|
2020-08-03 13:54:14 +00:00
|
|
|
Pipe read(
|
2017-12-26 21:34:06 +00:00
|
|
|
const Names & column_names,
|
2021-07-09 03:15:41 +00:00
|
|
|
const StorageSnapshotPtr & storage_snapshot,
|
2020-09-20 17:52:17 +00:00
|
|
|
SelectQueryInfo & query_info,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2018-04-19 14:47:09 +00:00
|
|
|
QueryProcessingStage::Enum processed_stage,
|
2019-02-18 23:38:44 +00:00
|
|
|
size_t max_block_size,
|
2022-10-07 10:46:45 +00:00
|
|
|
size_t num_streams) override;
|
2017-12-05 13:32:02 +00:00
|
|
|
|
2023-06-07 18:33:08 +00:00
|
|
|
SinkToStoragePtr write(const ASTPtr & query, const StorageMetadataPtr & /*metadata_snapshot*/, ContextPtr context, bool async_insert) override;
|
2018-05-10 09:23:38 +00:00
|
|
|
|
2023-02-20 20:37:38 +00:00
|
|
|
struct Configuration
|
|
|
|
{
|
2023-04-13 17:33:58 +00:00
|
|
|
using Addresses = std::vector<std::pair<String, UInt16>>;
|
|
|
|
|
2023-02-20 20:37:38 +00:00
|
|
|
String host;
|
|
|
|
UInt16 port = 0;
|
|
|
|
String username = "default";
|
|
|
|
String password;
|
|
|
|
String database;
|
|
|
|
String table;
|
|
|
|
|
|
|
|
bool replace_query = false;
|
|
|
|
String on_duplicate_clause;
|
|
|
|
|
2023-04-13 17:33:58 +00:00
|
|
|
Addresses addresses; /// Failover replicas.
|
2023-02-20 20:37:38 +00:00
|
|
|
String addresses_expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Configuration getConfiguration(ASTs engine_args, ContextPtr context_, MySQLSettings & storage_settings);
|
|
|
|
|
|
|
|
static Configuration processNamedCollectionResult(
|
2023-04-13 17:33:58 +00:00
|
|
|
const NamedCollection & named_collection, MySQLSettings & storage_settings,
|
|
|
|
ContextPtr context_, bool require_table = true);
|
2021-09-02 13:01:26 +00:00
|
|
|
|
2023-05-19 00:44:27 +00:00
|
|
|
static ColumnsDescription getTableStructureFromData(
|
|
|
|
mysqlxx::PoolWithFailover & pool_,
|
|
|
|
const String & database,
|
|
|
|
const String & table,
|
|
|
|
const ContextPtr & context_);
|
|
|
|
|
2017-12-05 13:32:02 +00:00
|
|
|
private:
|
2021-07-23 19:33:59 +00:00
|
|
|
friend class StorageMySQLSink;
|
2017-12-26 21:34:06 +00:00
|
|
|
|
|
|
|
std::string remote_database_name;
|
|
|
|
std::string remote_table_name;
|
2018-05-13 02:34:49 +00:00
|
|
|
bool replace_query;
|
|
|
|
std::string on_duplicate_clause;
|
2017-12-26 21:34:06 +00:00
|
|
|
|
2021-05-15 04:40:43 +00:00
|
|
|
MySQLSettings mysql_settings;
|
|
|
|
|
2021-03-27 14:35:44 +00:00
|
|
|
mysqlxx::PoolWithFailoverPtr pool;
|
2021-09-28 22:17:26 +00:00
|
|
|
|
2024-01-23 17:04:50 +00:00
|
|
|
LoggerPtr log;
|
2017-12-05 13:32:02 +00:00
|
|
|
};
|
2017-12-26 21:34:06 +00:00
|
|
|
|
2017-12-05 13:32:02 +00:00
|
|
|
}
|
2017-12-29 15:48:20 +00:00
|
|
|
|
|
|
|
#endif
|