ClickHouse/dbms/src/TableFunctions/TableFunctionFactory.h

54 lines
1.1 KiB
C++
Raw Normal View History

#pragma once
#include <TableFunctions/ITableFunction.h>
#include <ext/singleton.h>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
namespace DB
{
class Context;
2017-04-16 15:00:33 +00:00
/** Lets you get a table function by its name.
*/
class TableFunctionFactory final: public ext::singleton<TableFunctionFactory>
{
public:
using Creator = std::function<TableFunctionPtr()>;
/// 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.
void registerFunction(const std::string & name, Creator creator);
template <typename Function>
void registerFunction()
{
2018-01-10 00:04:08 +00:00
auto creator = [] () -> TableFunctionPtr
{
return std::make_shared<Function>();
};
registerFunction(Function::name, std::move(creator));
}
/// Throws an exception if not found.
TableFunctionPtr get(
const std::string & name,
const Context & context) const;
bool isTableFunctionName(const std::string & name) const;
private:
using TableFunctions = std::unordered_map<std::string, Creator>;
TableFunctions functions;
};
}