| 1 | #pragma once |
| 2 | |
| 3 | #include <Common/NamePrompter.h> |
| 4 | #include <Parsers/IAST_fwd.h> |
| 5 | #include <Storages/ColumnsDescription.h> |
| 6 | #include <Storages/ConstraintsDescription.h> |
| 7 | #include <Storages/IStorage_fwd.h> |
| 8 | #include <Storages/registerStorages.h> |
| 9 | #include <unordered_map> |
| 10 | |
| 11 | |
| 12 | namespace DB |
| 13 | { |
| 14 | |
| 15 | class Context; |
| 16 | class ASTCreateQuery; |
| 17 | class ASTStorage; |
| 18 | |
| 19 | |
| 20 | /** Allows to create a table by the name and parameters of the engine. |
| 21 | * In 'columns' Nested data structures must be flattened. |
| 22 | * You should subsequently call IStorage::startup method to work with table. |
| 23 | */ |
| 24 | class StorageFactory : private boost::noncopyable, public IHints<1, StorageFactory> |
| 25 | { |
| 26 | public: |
| 27 | |
| 28 | static StorageFactory & instance(); |
| 29 | |
| 30 | struct Arguments |
| 31 | { |
| 32 | const String & engine_name; |
| 33 | ASTs & engine_args; |
| 34 | ASTStorage * storage_def; |
| 35 | const ASTCreateQuery & query; |
| 36 | /// Path to table data. |
| 37 | /// Relative to <path> from server config (possibly <path> of some <disk> of some <volume> for *MergeTree) |
| 38 | const String & relative_data_path; |
| 39 | const String & table_name; |
| 40 | const String & database_name; |
| 41 | Context & local_context; |
| 42 | Context & context; |
| 43 | const ColumnsDescription & columns; |
| 44 | const ConstraintsDescription & constraints; |
| 45 | bool attach; |
| 46 | bool has_force_restore_data_flag; |
| 47 | }; |
| 48 | |
| 49 | using Creator = std::function<StoragePtr(const Arguments & arguments)>; |
| 50 | |
| 51 | StoragePtr get( |
| 52 | const ASTCreateQuery & query, |
| 53 | const String & relative_data_path, |
| 54 | const String & table_name, |
| 55 | const String & database_name, |
| 56 | Context & local_context, |
| 57 | Context & context, |
| 58 | const ColumnsDescription & columns, |
| 59 | const ConstraintsDescription & constraints, |
| 60 | bool attach, |
| 61 | bool has_force_restore_data_flag) const; |
| 62 | |
| 63 | /// Register a table engine by its name. |
| 64 | /// No locking, you must register all engines before usage of get. |
| 65 | void registerStorage(const std::string & name, Creator creator); |
| 66 | |
| 67 | const auto & getAllStorages() const |
| 68 | { |
| 69 | return storages; |
| 70 | } |
| 71 | |
| 72 | std::vector<String> getAllRegisteredNames() const override |
| 73 | { |
| 74 | std::vector<String> result; |
| 75 | auto getter = [](const auto & pair) { return pair.first; }; |
| 76 | std::transform(storages.begin(), storages.end(), std::back_inserter(result), getter); |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | using Storages = std::unordered_map<std::string, Creator>; |
| 82 | Storages storages; |
| 83 | }; |
| 84 | |
| 85 | } |
| 86 | |