2012-12-18 20:06:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2018-07-25 16:08:23 +00:00
|
|
|
#include <Common/IFactoryWithAliases.h>
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2017-08-14 04:23:38 +00:00
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
2012-12-18 20:06:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class Context;
|
|
|
|
|
|
|
|
|
2016-07-09 23:37:29 +00:00
|
|
|
/** Creates function by name.
|
|
|
|
* Function could use for initialization (take ownership of shared_ptr, for example)
|
|
|
|
* some dictionaries from Context.
|
2012-12-18 20:06:36 +00:00
|
|
|
*/
|
2019-12-09 14:41:55 +00:00
|
|
|
class FunctionFactory : private boost::noncopyable, public IFactoryWithAliases<std::function<FunctionOverloadResolverImplPtr(const Context &)>>
|
2012-12-18 20:06:36 +00:00
|
|
|
{
|
2017-08-18 17:06:22 +00:00
|
|
|
public:
|
2017-08-14 04:23:38 +00:00
|
|
|
|
2019-08-22 03:24:05 +00:00
|
|
|
static FunctionFactory & instance();
|
|
|
|
|
2018-02-02 08:33:36 +00:00
|
|
|
template <typename Function>
|
2018-02-06 19:34:53 +00:00
|
|
|
void registerFunction(CaseSensitiveness case_sensitiveness = CaseSensitive)
|
2018-02-02 08:33:36 +00:00
|
|
|
{
|
2018-02-06 19:34:53 +00:00
|
|
|
registerFunction<Function>(Function::name, case_sensitiveness);
|
2018-02-02 08:33:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 04:23:38 +00:00
|
|
|
template <typename Function>
|
2018-02-06 19:34:53 +00:00
|
|
|
void registerFunction(const std::string & name, CaseSensitiveness case_sensitiveness = CaseSensitive)
|
2017-08-14 04:23:38 +00:00
|
|
|
{
|
2018-02-02 08:33:36 +00:00
|
|
|
if constexpr (std::is_base_of<IFunction, Function>::value)
|
2018-02-06 19:34:53 +00:00
|
|
|
registerFunction(name, &createDefaultFunction<Function>, case_sensitiveness);
|
2018-02-02 08:33:36 +00:00
|
|
|
else
|
2018-02-06 19:34:53 +00:00
|
|
|
registerFunction(name, &Function::create, case_sensitiveness);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-08-18 17:06:22 +00:00
|
|
|
|
|
|
|
/// Throws an exception if not found.
|
2019-12-08 21:06:37 +00:00
|
|
|
FunctionOverloadResolverPtr get(const std::string & name, const Context & context) const;
|
2017-08-18 17:06:22 +00:00
|
|
|
|
|
|
|
/// Returns nullptr if not found.
|
2019-12-08 21:06:37 +00:00
|
|
|
FunctionOverloadResolverPtr tryGet(const std::string & name, const Context & context) const;
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
/// Throws an exception if not found.
|
|
|
|
FunctionOverloadResolverImplPtr getImpl(const std::string & name, const Context & context) const;
|
|
|
|
|
|
|
|
/// Returns nullptr if not found.
|
|
|
|
FunctionOverloadResolverImplPtr tryGetImpl(const std::string & name, const Context & context) const;
|
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
private:
|
|
|
|
using Functions = std::unordered_map<std::string, Creator>;
|
|
|
|
|
|
|
|
Functions functions;
|
|
|
|
Functions case_insensitive_functions;
|
2018-02-06 19:34:53 +00:00
|
|
|
|
|
|
|
template <typename Function>
|
2019-12-09 14:41:55 +00:00
|
|
|
static FunctionOverloadResolverImplPtr createDefaultFunction(const Context & context)
|
2018-02-06 19:34:53 +00:00
|
|
|
{
|
2019-12-09 14:41:55 +00:00
|
|
|
return std::make_unique<DefaultFunctionBuilder>(Function::create(context));
|
2018-02-06 19:34:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 16:08:23 +00:00
|
|
|
const Functions & getCreatorMap() const override { return functions; }
|
|
|
|
|
|
|
|
const Functions & getCaseInsensitiveCreatorMap() const override { return case_insensitive_functions; }
|
|
|
|
|
|
|
|
String getFactoryName() const override { return "FunctionFactory"; }
|
|
|
|
|
2018-02-06 19:34:53 +00:00
|
|
|
/// Register a function by its name.
|
|
|
|
/// No locking, you must register all functions before usage of get.
|
|
|
|
void registerFunction(
|
|
|
|
const std::string & name,
|
|
|
|
Creator creator,
|
|
|
|
CaseSensitiveness case_sensitiveness = CaseSensitive);
|
2012-12-18 20:06:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|