ClickHouse/src/Functions/currentDatabase.cpp
Daniël van Eeden 212686c5c4 Improve MySQL Compatibility by supporting SCHEMA()
Basically `DATABASE()` and `SCHEMA()` are aliases in MySQL.

This helps with allowing MySQL Shell to get one step closer to a
functioning connection.

To see what MySQL Shell does:

```
mysqlsh --verbose=4 --log-sql=unfiltered mysql://default@127.0.0.1:9004
```

This might also bring other tools a step closer to working with the
MySQL compatibility of ClickHouse.

Ref: #9336

Signed-off-by: Daniël van Eeden <git@myname.nl>
2023-08-19 16:24:09 +02:00

63 lines
1.7 KiB
C++

#include <Functions/IFunction.h>
#include <Functions/FunctionFactory.h>
#include <Interpreters/Context.h>
#include <DataTypes/DataTypeString.h>
#include <Core/Field.h>
namespace DB
{
namespace
{
class FunctionCurrentDatabase : public IFunction
{
const String db_name;
public:
static constexpr auto name = "currentDatabase";
static FunctionPtr create(ContextPtr context)
{
return std::make_shared<FunctionCurrentDatabase>(context->getCurrentDatabase());
}
explicit FunctionCurrentDatabase(const String & db_name_) : db_name{db_name_}
{
}
String getName() const override
{
return name;
}
size_t getNumberOfArguments() const override
{
return 0;
}
DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
{
return std::make_shared<DataTypeString>();
}
bool isDeterministic() const override { return false; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
{
return DataTypeString().createColumnConst(input_rows_count, db_name);
}
};
}
REGISTER_FUNCTION(CurrentDatabase)
{
factory.registerFunction<FunctionCurrentDatabase>();
factory.registerAlias("DATABASE", FunctionCurrentDatabase::name, FunctionFactory::CaseInsensitive);
factory.registerAlias("SCHEMA", FunctionCurrentDatabase::name, FunctionFactory::CaseInsensitive);
factory.registerAlias("current_database", FunctionCurrentDatabase::name, FunctionFactory::CaseInsensitive);
}
}