2012-12-18 20:06:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
|
2017-07-10 04:34:14 +00:00
|
|
|
#include <ext/singleton.h>
|
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
|
|
|
*/
|
2017-07-10 04:34:14 +00:00
|
|
|
class FunctionFactory : public ext::singleton<FunctionFactory>
|
2012-12-18 20:06:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
friend class StorageSystemFunctions;
|
2015-04-24 15:49:30 +00:00
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
public:
|
2018-02-02 08:33:36 +00:00
|
|
|
using Creator = std::function<FunctionBuilderPtr(const Context &)>;
|
2014-08-22 00:57:20 +00:00
|
|
|
|
2017-08-14 04:23:38 +00:00
|
|
|
/// For compatibility with SQL, it's possible to specify that certain function name is case insensitive.
|
|
|
|
enum CaseSensitiveness
|
|
|
|
{
|
|
|
|
CaseSensitive,
|
|
|
|
CaseInsensitive
|
|
|
|
};
|
|
|
|
|
2017-08-18 17:06:22 +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);
|
2017-06-10 09:04:31 +00:00
|
|
|
|
2018-02-02 08:33:36 +00:00
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
static FunctionBuilderPtr registerDefaultFunction(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<Function>(Function::create(context));
|
|
|
|
}
|
|
|
|
|
2017-08-14 04:23:38 +00:00
|
|
|
template <typename Function>
|
|
|
|
void registerFunction()
|
|
|
|
{
|
2018-02-02 08:33:36 +00:00
|
|
|
if constexpr (std::is_base_of<IFunction, Function>::value)
|
|
|
|
registerFunction(Function::name, ®isterDefaultFunction<Function>);
|
|
|
|
else
|
|
|
|
registerFunction(Function::name, &Function::create);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-08-18 17:06:22 +00:00
|
|
|
|
|
|
|
/// Throws an exception if not found.
|
2018-02-02 08:33:36 +00:00
|
|
|
FunctionBuilderPtr get(const std::string & name, const Context & context) const;
|
2017-08-18 17:06:22 +00:00
|
|
|
|
|
|
|
/// Returns nullptr if not found.
|
2018-02-02 08:33:36 +00:00
|
|
|
FunctionBuilderPtr tryGet(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;
|
2012-12-18 20:06:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|