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 | |
8 | namespace DB |
9 | { |
10 | |
11 | class FunctionCurrentUser : public IFunction |
12 | { |
13 | const String user_name; |
14 | |
15 | public: |
16 | static constexpr auto name = "currentUser"; |
17 | static FunctionPtr create(const Context & context) |
18 | { |
19 | return std::make_shared<FunctionCurrentUser>(context.getClientInfo().initial_user); |
20 | } |
21 | |
22 | explicit FunctionCurrentUser(const String & user_name_) : user_name{user_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, user_name); |
45 | } |
46 | }; |
47 | |
48 | |
49 | void registerFunctionCurrentUser(FunctionFactory & factory) |
50 | { |
51 | factory.registerFunction<FunctionCurrentUser>(); |
52 | factory.registerAlias("user", FunctionCurrentUser::name, FunctionFactory::CaseInsensitive); |
53 | } |
54 | |
55 | } |
56 |