| 1 | #pragma once |
|---|---|
| 2 | |
| 3 | #include <TableFunctions/ITableFunction.h> |
| 4 | #include <Common/IFactoryWithAliases.h> |
| 5 | #include <Common/NamePrompter.h> |
| 6 | |
| 7 | |
| 8 | #include <functional> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <unordered_map> |
| 12 | #include <boost/noncopyable.hpp> |
| 13 | |
| 14 | |
| 15 | namespace DB |
| 16 | { |
| 17 | |
| 18 | class Context; |
| 19 | |
| 20 | using TableFunctionCreator = std::function<TableFunctionPtr()>; |
| 21 | |
| 22 | /** Lets you get a table function by its name. |
| 23 | */ |
| 24 | class TableFunctionFactory final: private boost::noncopyable, public IFactoryWithAliases<TableFunctionCreator> |
| 25 | { |
| 26 | public: |
| 27 | |
| 28 | static TableFunctionFactory & instance(); |
| 29 | |
| 30 | /// Register a function by its name. |
| 31 | /// No locking, you must register all functions before usage of get. |
| 32 | void registerFunction(const std::string & name, Creator creator, CaseSensitiveness case_sensitiveness = CaseSensitive); |
| 33 | |
| 34 | template <typename Function> |
| 35 | void registerFunction(CaseSensitiveness case_sensitiveness = CaseSensitive) |
| 36 | { |
| 37 | auto creator = [] () -> TableFunctionPtr |
| 38 | { |
| 39 | return std::make_shared<Function>(); |
| 40 | }; |
| 41 | registerFunction(Function::name, std::move(creator), case_sensitiveness); |
| 42 | } |
| 43 | |
| 44 | /// Throws an exception if not found. |
| 45 | TableFunctionPtr get(const std::string & name, const Context & context) const; |
| 46 | |
| 47 | /// Returns nullptr if not found. |
| 48 | TableFunctionPtr tryGet(const std::string & name, const Context & context) const; |
| 49 | |
| 50 | bool isTableFunctionName(const std::string & name) const; |
| 51 | |
| 52 | private: |
| 53 | using TableFunctions = std::unordered_map<std::string, Creator>; |
| 54 | |
| 55 | const TableFunctions & getCreatorMap() const override { return table_functions; } |
| 56 | |
| 57 | const TableFunctions & getCaseInsensitiveCreatorMap() const override { return case_insensitive_table_functions; } |
| 58 | |
| 59 | String getFactoryName() const override { return "TableFunctionFactory"; } |
| 60 | |
| 61 | TableFunctions table_functions; |
| 62 | TableFunctions case_insensitive_table_functions; |
| 63 | }; |
| 64 | |
| 65 | } |
| 66 |