1#include "duckdb/main/extension_util.hpp"
2#include "duckdb/function/scalar_function.hpp"
3#include "duckdb/parser/parsed_data/create_type_info.hpp"
4#include "duckdb/parser/parsed_data/create_copy_function_info.hpp"
5#include "duckdb/parser/parsed_data/create_pragma_function_info.hpp"
6#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
7#include "duckdb/parser/parsed_data/create_table_function_info.hpp"
8#include "duckdb/parser/parsed_data/create_macro_info.hpp"
9#include "duckdb/catalog/catalog.hpp"
10#include "duckdb/main/config.hpp"
11
12namespace duckdb {
13
14void ExtensionUtil::RegisterFunction(DatabaseInstance &db, ScalarFunctionSet set) {
15 D_ASSERT(!set.name.empty());
16 CreateScalarFunctionInfo info(std::move(set));
17 auto &system_catalog = Catalog::GetSystemCatalog(db);
18 auto data = CatalogTransaction::GetSystemTransaction(db);
19 system_catalog.CreateFunction(transaction: data, info);
20}
21
22void ExtensionUtil::RegisterFunction(DatabaseInstance &db, ScalarFunction function) {
23 D_ASSERT(!function.name.empty());
24 ScalarFunctionSet set(function.name);
25 set.AddFunction(function: std::move(function));
26 RegisterFunction(db, set: std::move(set));
27}
28
29void ExtensionUtil::RegisterFunction(DatabaseInstance &db, TableFunction function) {
30 D_ASSERT(!function.name.empty());
31 TableFunctionSet set(function.name);
32 set.AddFunction(function: std::move(function));
33 RegisterFunction(db, function: std::move(set));
34}
35
36void ExtensionUtil::RegisterFunction(DatabaseInstance &db, TableFunctionSet function) {
37 D_ASSERT(!function.name.empty());
38 CreateTableFunctionInfo info(std::move(function));
39 auto &system_catalog = Catalog::GetSystemCatalog(db);
40 auto data = CatalogTransaction::GetSystemTransaction(db);
41 system_catalog.CreateFunction(transaction: data, info);
42}
43
44void ExtensionUtil::RegisterFunction(DatabaseInstance &db, PragmaFunction function) {
45 D_ASSERT(!function.name.empty());
46 PragmaFunctionSet set(function.name);
47 set.AddFunction(function: std::move(function));
48 RegisterFunction(db, function: std::move(set));
49}
50
51void ExtensionUtil::RegisterFunction(DatabaseInstance &db, PragmaFunctionSet function) {
52 D_ASSERT(!function.name.empty());
53 auto function_name = function.name;
54 CreatePragmaFunctionInfo info(std::move(function_name), std::move(function));
55 auto &system_catalog = Catalog::GetSystemCatalog(db);
56 auto data = CatalogTransaction::GetSystemTransaction(db);
57 system_catalog.CreatePragmaFunction(transaction: data, info);
58}
59
60void ExtensionUtil::RegisterFunction(DatabaseInstance &db, CopyFunction function) {
61 CreateCopyFunctionInfo info(std::move(function));
62 auto &system_catalog = Catalog::GetSystemCatalog(db);
63 auto data = CatalogTransaction::GetSystemTransaction(db);
64 system_catalog.CreateCopyFunction(transaction: data, info);
65}
66
67void ExtensionUtil::RegisterFunction(DatabaseInstance &db, CreateMacroInfo &info) {
68 auto &system_catalog = Catalog::GetSystemCatalog(db);
69 auto data = CatalogTransaction::GetSystemTransaction(db);
70 system_catalog.CreateFunction(transaction: data, info);
71}
72
73void ExtensionUtil::RegisterType(DatabaseInstance &db, string type_name, LogicalType type) {
74 D_ASSERT(!type_name.empty());
75 CreateTypeInfo info(std::move(type_name), std::move(type));
76 info.temporary = true;
77 info.internal = true;
78 auto &system_catalog = Catalog::GetSystemCatalog(db);
79 auto data = CatalogTransaction::GetSystemTransaction(db);
80 system_catalog.CreateType(transaction: data, info);
81}
82
83void ExtensionUtil::RegisterCastFunction(DatabaseInstance &db, const LogicalType &source, const LogicalType &target,
84 BoundCastInfo function, int64_t implicit_cast_cost) {
85 auto &config = DBConfig::GetConfig(db);
86 auto &casts = config.GetCastFunctions();
87 casts.RegisterCastFunction(source, target, function: std::move(function), implicit_cast_cost);
88}
89
90} // namespace duckdb
91