2014-01-28 16:45:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-06-10 09:04:31 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <common/singleton.h>
|
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <Common/Exception.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
2014-01-28 16:45:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-06-10 09:04:31 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-06-10 09:04:31 +00:00
|
|
|
class TableFunctionFactory : public Singleton<TableFunctionFactory>
|
2014-01-28 16:45:10 +00:00
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
private:
|
|
|
|
/// No std::function, for smaller object size and less indirection.
|
|
|
|
using Creator = TableFunctionPtr(*)();
|
|
|
|
using TableFunctions = std::unordered_map<String, Creator>;
|
|
|
|
|
|
|
|
TableFunctions functions;
|
|
|
|
|
2014-01-28 16:45:10 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
TableFunctionPtr get(
|
2017-06-10 09:04:31 +00:00
|
|
|
const String & name,
|
2017-04-01 07:20:54 +00:00
|
|
|
const Context & context) const;
|
2017-06-10 09:04:31 +00:00
|
|
|
|
|
|
|
/// Register a table function by its name.
|
|
|
|
template <typename Function>
|
|
|
|
void registerFunction()
|
|
|
|
{
|
2017-06-12 00:26:22 +00:00
|
|
|
if (!functions.emplace(std::string(Function::name), []{ return TableFunctionPtr(std::make_unique<Function>()); }).second)
|
2017-06-10 09:04:31 +00:00
|
|
|
throw Exception("TableFunctionFactory: the table function name '" + String(Function::name) + "' is not unique",
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
2014-01-28 16:45:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|