ClickHouse/dbms/src/Databases/DatabaseMySQL.h

155 lines
5.1 KiB
C++
Raw Normal View History

#pragma once
#include "config_core.h"
2019-06-13 08:19:26 +00:00
#if USE_MYSQL
#include <mysqlxx/Pool.h>
#include <Databases/DatabasesCommon.h>
2019-08-27 20:43:08 +00:00
#include <Interpreters/Context.h>
2019-10-21 14:25:21 +00:00
#include <memory>
2019-08-27 20:43:08 +00:00
namespace DB
{
/** Real-time access to table list and table structure from remote MySQL
* It doesn't make any manipulations with filesystem.
* All tables are created by calling code after real-time pull-out structure from remote MySQL
*/
class DatabaseMySQL : public IDatabase
{
public:
~DatabaseMySQL() override;
DatabaseMySQL(const Context & context_, const String & database_name_, const String & mysql_host_name_, const UInt16 & mysql_port_,
const String & mysql_database_name_, const String & mysql_user_name_, const String & mysql_user_password_);
String getEngineName() const override { return "MySQL"; }
String getDatabaseName() const override { return database_name; }
2019-06-14 16:18:48 +00:00
bool empty(const Context & context) const override;
2019-10-10 17:33:01 +00:00
DatabaseTablesIteratorPtr getTablesIterator(const Context & context, const FilterByNameFunction & filter_by_table_name = {}) override;
2019-10-11 13:51:31 +00:00
DatabaseDictionariesIteratorPtr getDictionariesIterator(const Context &, const FilterByNameFunction & = {}) override
{
2019-10-21 14:25:21 +00:00
return std::make_unique<DatabaseDictionariesSnapshotIterator>();
2019-10-11 13:51:31 +00:00
}
ASTPtr getCreateDatabaseQuery(const Context & context) const override;
2019-06-14 16:18:48 +00:00
bool isTableExist(const Context & context, const String & name) const override;
2019-10-11 13:51:31 +00:00
bool isDictionaryExist(const Context &, const String &) const override { return false; }
2019-06-14 16:18:48 +00:00
StoragePtr tryGetTable(const Context & context, const String & name) const override;
2019-06-14 16:18:48 +00:00
ASTPtr tryGetCreateTableQuery(const Context & context, const String & name) const override;
2019-10-11 13:51:31 +00:00
ASTPtr getCreateDictionaryQuery(const Context &, const String &) const override
{
throw Exception("MySQL database engine does not support dictionaries.", ErrorCodes::NOT_IMPLEMENTED);
}
ASTPtr tryGetCreateDictionaryQuery(const Context &, const String &) const override { return nullptr; }
time_t getObjectMetadataModificationTime(const Context & context, const String & name) override;
void shutdown() override;
StoragePtr detachTable(const String &) override
{
throw Exception("MySQL database engine does not support detach table.", ErrorCodes::NOT_IMPLEMENTED);
}
void detachDictionary(const String &, const Context &) override
2019-10-11 13:51:31 +00:00
{
throw Exception("MySQL database engine does not support detach dictionary.", ErrorCodes::NOT_IMPLEMENTED);
}
2019-10-10 17:33:01 +00:00
void loadStoredObjects(Context &, bool) override
{
/// do nothing
}
void removeTable(const Context &, const String &) override
{
throw Exception("MySQL database engine does not support remove table.", ErrorCodes::NOT_IMPLEMENTED);
}
2019-10-11 13:51:31 +00:00
void removeDictionary(const Context &, const String &) override
{
throw Exception("MySQL database engine does not support remove dictionary.", ErrorCodes::NOT_IMPLEMENTED);
}
void attachTable(const String &, const StoragePtr &) override
{
throw Exception("MySQL database engine does not support attach table.", ErrorCodes::NOT_IMPLEMENTED);
}
void attachDictionary(const String &, const Context &) override
2019-10-11 13:51:31 +00:00
{
throw Exception("MySQL database engine does not support attach dictionary.", ErrorCodes::NOT_IMPLEMENTED);
}
void createTable(const Context &, const String &, const StoragePtr &, const ASTPtr &) override
{
throw Exception("MySQL database engine does not support create table.", ErrorCodes::NOT_IMPLEMENTED);
}
2019-10-16 14:59:52 +00:00
void createDictionary(const Context &, const String &, const ASTPtr &) override
2019-10-11 13:51:31 +00:00
{
throw Exception("MySQL database engine does not support create dictionary.", ErrorCodes::NOT_IMPLEMENTED);
}
private:
struct MySQLStorageInfo
{
StoragePtr storage;
UInt64 modification_time;
ASTPtr create_table_query;
};
const Context global_context;
const String database_name;
const String mysql_host_name;
const UInt16 mysql_port;
const String mysql_database_name;
const String mysql_user_name;
const String mysql_user_password;
2019-06-14 16:18:48 +00:00
mutable std::mutex mutex;
std::atomic<bool> quit{false};
std::condition_variable cond;
2019-06-14 16:18:48 +00:00
mutable mysqlxx::Pool mysql_pool;
mutable std::vector<StoragePtr> outdated_tables;
mutable std::map<String, MySQLStorageInfo> local_tables_cache;
void cleanOutdatedTables();
2019-06-14 16:18:48 +00:00
void fetchTablesIntoLocalCache() const;
2019-06-14 16:18:48 +00:00
std::map<String, UInt64> fetchTablesWithModificationTime() const;
DatabaseMySQL::MySQLStorageInfo createStorageInfo(
2019-06-14 16:18:48 +00:00
const String & table_name, const NamesAndTypesList & columns_name_and_type, const UInt64 & table_modification_time) const;
2019-06-14 16:18:48 +00:00
std::map<String, NamesAndTypesList> fetchTablesColumnsList(const std::vector<String> & tables_name) const;
2019-06-14 16:18:48 +00:00
void destroyLocalCacheExtraTables(const std::map<String, UInt64> & tables_with_modification_time) const;
2019-06-14 16:18:48 +00:00
void fetchLatestTablesStructureIntoCache(const std::map<String, UInt64> & tables_modification_time) const;
ThreadFromGlobalPool thread{&DatabaseMySQL::cleanOutdatedTables, this};
};
}
2019-06-13 08:19:26 +00:00
#endif