ClickHouse/src/TableFunctions/TableFunctionFactory.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.7 KiB
C++
Raw Normal View History

#pragma once
#include <TableFunctions/ITableFunction.h>
2019-07-29 13:50:13 +00:00
#include <Common/IFactoryWithAliases.h>
2019-04-03 11:13:22 +00:00
#include <Common/NamePrompter.h>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <boost/noncopyable.hpp>
namespace DB
{
class Context;
2019-07-29 13:50:13 +00:00
using TableFunctionCreator = std::function<TableFunctionPtr()>;
struct TableFunctionFactoryData
{
TableFunctionCreator creator;
TableFunctionProperties properties;
TableFunctionFactoryData() = default;
TableFunctionFactoryData(const TableFunctionFactoryData &) = default;
TableFunctionFactoryData & operator = (const TableFunctionFactoryData &) = default;
template <typename Creator>
requires (!std::is_same_v<Creator, TableFunctionFactoryData>)
TableFunctionFactoryData(Creator creator_, TableFunctionProperties properties_ = {}) /// NOLINT
: creator(std::forward<Creator>(creator_)), properties(std::move(properties_))
{
}
};
2017-04-16 15:00:33 +00:00
/** Lets you get a table function by its name.
*/
2022-08-27 20:06:03 +00:00
class TableFunctionFactory final: private boost::noncopyable, public IFactoryWithAliases<TableFunctionFactoryData>
{
public:
static TableFunctionFactory & instance();
/// 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.
2022-08-27 20:06:03 +00:00
void registerFunction(
const std::string & name,
TableFunctionCreator creator,
Documentation doc = {},
CaseSensitiveness case_sensitiveness = CaseSensitive);
template <typename Function>
2022-08-27 20:06:03 +00:00
void registerFunction(Documentation doc = {}, CaseSensitiveness case_sensitiveness = CaseSensitive)
{
2022-08-26 01:41:52 +00:00
auto creator = []() -> TableFunctionPtr { return std::make_shared<Function>(); };
registerFunction(Function::name, std::move(creator), std::move(doc), case_sensitiveness);
}
/// Throws an exception if not found.
TableFunctionPtr get(const ASTPtr & ast_function, ContextPtr context) const;
2019-07-29 13:50:13 +00:00
/// Returns nullptr if not found.
TableFunctionPtr tryGet(const std::string & name, ContextPtr context) const;
2018-07-27 21:33:30 +00:00
std::optional<TableFunctionProperties> tryGetProperties(const String & name) const;
2022-08-26 01:41:52 +00:00
bool isTableFunctionName(const std::string & name) const;
2019-07-29 13:50:13 +00:00
private:
2020-06-14 07:44:02 +00:00
using TableFunctions = std::unordered_map<std::string, Value>;
2020-06-14 07:44:02 +00:00
const TableFunctions & getMap() const override { return table_functions; }
2019-04-03 11:13:22 +00:00
2020-06-14 07:44:02 +00:00
const TableFunctions & getCaseInsensitiveMap() const override { return case_insensitive_table_functions; }
2019-07-29 13:50:13 +00:00
String getFactoryName() const override { return "TableFunctionFactory"; }
std::optional<TableFunctionProperties> tryGetPropertiesImpl(const String & name) const;
2019-07-29 13:50:13 +00:00
TableFunctions table_functions;
TableFunctions case_insensitive_table_functions;
};
}