ClickHouse/src/TableFunctions/TableFunctionFactory.h

65 lines
1.8 KiB
C++
Raw Normal View History

#pragma once
#include <TableFunctions/ITableFunction.h>
2019-07-29 13:50:13 +00:00
#include <Common/IFactoryWithAliases.h>
2019-04-03 11:13:22 +00:00
#include <Common/NamePrompter.h>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <boost/noncopyable.hpp>
namespace DB
{
class Context;
2019-07-29 13:50:13 +00:00
using TableFunctionCreator = std::function<TableFunctionPtr()>;
2017-04-16 15:00:33 +00:00
/** Lets you get a table function by its name.
*/
class TableFunctionFactory final: private boost::noncopyable, public IFactoryWithAliases<TableFunctionCreator>
{
public:
static TableFunctionFactory & instance();
/// Register a function by its name.
2017-06-13 02:06:53 +00:00
/// No locking, you must register all functions before usage of get.
2020-06-14 07:44:02 +00:00
void registerFunction(const std::string & name, Value creator, CaseSensitiveness case_sensitiveness = CaseSensitive);
template <typename Function>
2019-07-29 13:50:13 +00:00
void registerFunction(CaseSensitiveness case_sensitiveness = CaseSensitive)
{
2018-01-10 00:04:08 +00:00
auto creator = [] () -> TableFunctionPtr
{
return std::make_shared<Function>();
};
2019-07-29 13:50:13 +00:00
registerFunction(Function::name, std::move(creator), case_sensitiveness);
}
/// Throws an exception if not found.
TableFunctionPtr get(const ASTPtr & ast_function, const Context & context) const;
2019-07-29 13:50:13 +00:00
/// Returns nullptr if not found.
TableFunctionPtr tryGet(const std::string & name, const Context & context) const;
2018-07-27 21:33:30 +00:00
bool isTableFunctionName(const std::string & name) const;
2019-07-29 13:50:13 +00:00
private:
2020-06-14 07:44:02 +00:00
using TableFunctions = std::unordered_map<std::string, Value>;
2020-06-14 07:44:02 +00:00
const TableFunctions & getMap() const override { return table_functions; }
2019-04-03 11:13:22 +00:00
2020-06-14 07:44:02 +00:00
const TableFunctions & getCaseInsensitiveMap() const override { return case_insensitive_table_functions; }
2019-07-29 13:50:13 +00:00
String getFactoryName() const override { return "TableFunctionFactory"; }
TableFunctions table_functions;
TableFunctions case_insensitive_table_functions;
};
}