1#include <Functions/IFunctionImpl.h>
2#include <Functions/FunctionFactory.h>
3#include <Interpreters/Context.h>
4#include <DataTypes/DataTypeString.h>
5#include <Core/Field.h>
6
7
8namespace DB
9{
10
11class FunctionCurrentDatabase : public IFunction
12{
13 const String db_name;
14
15public:
16 static constexpr auto name = "currentDatabase";
17 static FunctionPtr create(const Context & context)
18 {
19 return std::make_shared<FunctionCurrentDatabase>(context.getCurrentDatabase());
20 }
21
22 explicit FunctionCurrentDatabase(const String & db_name_) : db_name{db_name_}
23 {
24 }
25
26 String getName() const override
27 {
28 return name;
29 }
30 size_t getNumberOfArguments() const override
31 {
32 return 0;
33 }
34
35 DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
36 {
37 return std::make_shared<DataTypeString>();
38 }
39
40 bool isDeterministic() const override { return false; }
41
42 void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
43 {
44 block.getByPosition(result).column = DataTypeString().createColumnConst(input_rows_count, db_name);
45 }
46};
47
48
49void registerFunctionCurrentDatabase(FunctionFactory & factory)
50{
51 factory.registerFunction<FunctionCurrentDatabase>();
52}
53
54}
55