ClickHouse/dbms/Databases/DatabaseMySQL.h

100 lines
3.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>
#include <Parsers/ASTCreateQuery.h>
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 final : public IDatabase
{
public:
~DatabaseMySQL() override;
DatabaseMySQL(
const Context & global_context, const String & database_name, const String & metadata_path,
const ASTStorage * database_engine_define, const String & database_name_in_mysql, mysqlxx::Pool && pool);
String getEngineName() const override { return "MySQL"; }
2019-06-14 16:18:48 +00:00
bool empty(const Context & context) const override;
2020-03-18 03:27:32 +00:00
DatabaseTablesIteratorPtr getTablesIterator(const Context & context, const FilterByNameFunction & filter_by_table_name) override;
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-06-14 16:18:48 +00:00
StoragePtr tryGetTable(const Context & context, const String & name) const override;
time_t getObjectMetadataModificationTime(const String & name) const override;
void shutdown() override;
2019-10-25 19:07:47 +00:00
void drop(const Context & /*context*/) override;
String getMetadataPath() const override;
2019-10-11 13:51:31 +00:00
void createTable(const Context &, const String & table_name, const StoragePtr & storage, const ASTPtr & create_query) override;
void loadStoredObjects(Context &, bool) override;
StoragePtr detachTable(const String & table_name) override;
void removeTable(const Context &, const String & table_name) override;
void attachTable(const String & table_name, const StoragePtr & storage) override;
2019-11-01 12:47:55 +00:00
protected:
ASTPtr getCreateTableQueryImpl(const Context & context, const String & name, bool throw_on_error) const override;
2019-10-11 13:51:31 +00:00
private:
Context global_context;
String metadata_path;
ASTPtr database_engine_define;
String database_name_in_mysql;
2019-06-14 16:18:48 +00:00
mutable std::mutex mutex;
std::atomic<bool> quit{false};
std::condition_variable cond;
using MySQLPool = mysqlxx::Pool;
using ModifyTimeAndStorage = std::pair<UInt64, StoragePtr>;
mutable MySQLPool mysql_pool;
2019-06-14 16:18:48 +00:00
mutable std::vector<StoragePtr> outdated_tables;
mutable std::map<String, ModifyTimeAndStorage> local_tables_cache;
std::unordered_set<String> remove_or_detach_tables;
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;
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