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