1#pragma once
2
3#include "config_core.h"
4#if USE_MYSQL
5
6#include <mysqlxx/Pool.h>
7#include <Databases/DatabasesCommon.h>
8#include <Interpreters/Context.h>
9#include <memory>
10#include <Parsers/ASTCreateQuery.h>
11
12
13namespace DB
14{
15
16/** Real-time access to table list and table structure from remote MySQL
17 * It doesn't make any manipulations with filesystem.
18 * All tables are created by calling code after real-time pull-out structure from remote MySQL
19 */
20class DatabaseMySQL : public IDatabase
21{
22public:
23 ~DatabaseMySQL() override;
24
25 DatabaseMySQL(
26 const Context & global_context, const String & database_name, const String & metadata_path,
27 const ASTStorage * database_engine_define, const String & database_name_in_mysql, mysqlxx::Pool && pool);
28
29 String getEngineName() const override { return "MySQL"; }
30
31 bool empty(const Context & context) const override;
32
33 DatabaseTablesIteratorPtr getTablesIterator(const Context & context, const FilterByNameFunction & filter_by_table_name = {}) override;
34
35 ASTPtr getCreateDatabaseQuery() const override;
36
37 bool isTableExist(const Context & context, const String & name) const override;
38
39 StoragePtr tryGetTable(const Context & context, const String & name) const override;
40
41 time_t getObjectMetadataModificationTime(const String & name) const override;
42
43 void shutdown() override;
44
45 void drop(const Context & /*context*/) override;
46
47 String getMetadataPath() const override;
48
49 void createTable(const Context &, const String & table_name, const StoragePtr & storage, const ASTPtr & create_query) override;
50
51 void loadStoredObjects(Context &, bool) override;
52
53 StoragePtr detachTable(const String & table_name) override;
54
55 void removeTable(const Context &, const String & table_name) override;
56
57 void attachTable(const String & table_name, const StoragePtr & storage) override;
58
59
60protected:
61 ASTPtr getCreateTableQueryImpl(const Context & context, const String & name, bool throw_on_error) const override;
62
63private:
64 Context global_context;
65 String metadata_path;
66 ASTPtr database_engine_define;
67 String database_name_in_mysql;
68
69 mutable std::mutex mutex;
70 std::atomic<bool> quit{false};
71 std::condition_variable cond;
72
73 using MySQLPool = mysqlxx::Pool;
74 using ModifyTimeAndStorage = std::pair<UInt64, StoragePtr>;
75
76 mutable MySQLPool mysql_pool;
77 mutable std::vector<StoragePtr> outdated_tables;
78 mutable std::map<String, ModifyTimeAndStorage> local_tables_cache;
79
80 std::unordered_set<String> remove_or_detach_tables;
81
82 void cleanOutdatedTables();
83
84 void fetchTablesIntoLocalCache() const;
85
86 std::map<String, UInt64> fetchTablesWithModificationTime() const;
87
88 std::map<String, NamesAndTypesList> fetchTablesColumnsList(const std::vector<String> & tables_name) const;
89
90 void destroyLocalCacheExtraTables(const std::map<String, UInt64> & tables_with_modification_time) const;
91
92 void fetchLatestTablesStructureIntoCache(const std::map<String, UInt64> & tables_modification_time) const;
93
94 ThreadFromGlobalPool thread{&DatabaseMySQL::cleanOutdatedTables, this};
95};
96
97}
98
99#endif
100