2017-08-14 04:23:38 +00:00
|
|
|
#include <Poco/String.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Common/Exception.h>
|
2012-12-18 20:06:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int UNKNOWN_FUNCTION;
|
2016-01-12 02:21:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 04:23:38 +00:00
|
|
|
|
|
|
|
void FunctionFactory::registerFunction(const String & name, Creator creator, CaseSensitiveness case_sensitiveness)
|
2014-08-22 00:57:20 +00:00
|
|
|
{
|
2017-08-14 04:23:38 +00:00
|
|
|
if (!functions.emplace(name, creator).second)
|
|
|
|
throw Exception("FunctionFactory: the function name '" + name + "' is not unique",
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (case_sensitiveness == CaseInsensitive
|
|
|
|
&& !case_insensitive_functions.emplace(Poco::toLower(name), creator).second)
|
|
|
|
throw Exception("FunctionFactory: the case insensitive function name '" + name + "' is not unique",
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
2014-08-22 00:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-18 20:06:36 +00:00
|
|
|
FunctionPtr FunctionFactory::get(
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string & name,
|
|
|
|
const Context & context) const
|
2015-11-29 08:06:29 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto res = tryGet(name, context);
|
|
|
|
if (!res)
|
|
|
|
throw Exception("Unknown function " + name, ErrorCodes::UNKNOWN_FUNCTION);
|
|
|
|
return res;
|
2015-11-29 08:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FunctionPtr FunctionFactory::tryGet(
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string & name,
|
|
|
|
const Context & context) const
|
2012-12-18 20:06:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto it = functions.find(name);
|
|
|
|
if (functions.end() != it)
|
|
|
|
return it->second(context);
|
2017-08-14 04:23:38 +00:00
|
|
|
|
|
|
|
it = case_insensitive_functions.find(Poco::toLower(name));
|
|
|
|
if (case_insensitive_functions.end() != it)
|
|
|
|
return it->second(context);
|
|
|
|
|
|
|
|
return {};
|
2012-12-18 20:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|