mycli compatibility

This commit is contained in:
Amos Bird 2021-03-02 18:53:06 +08:00
parent 3df57da2d8
commit ad4fd75fb4
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
7 changed files with 53 additions and 1 deletions

View File

@ -0,0 +1,44 @@
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunctionImpl.h>
#include <Interpreters/Context.h>
namespace DB
{
namespace
{
/// Get the connection ID. It's used for MySQL handler only.
class FunctionConnectionID : public IFunction
{
public:
static constexpr auto name = "connectionID";
FunctionConnectionID(const Context & context_) : context(context_) {}
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionConnectionID>(context); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 0; }
DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override { return std::make_shared<DataTypeUInt64>(); }
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr & result_type, size_t input_rows_count) const override
{
return result_type->createColumnConst(input_rows_count, context.getClientInfo().connection_id);
}
private:
const Context & context;
};
}
void registerFunctionConnectionID(FunctionFactory & factory)
{
factory.registerFunction<FunctionConnectionID>();
factory.registerAlias("connection_id", "connectionID");
}
}

View File

@ -80,6 +80,7 @@ private:
std::unordered_map<String, TypeAndValue> global_variable_map
= {{"max_allowed_packet", {std::make_shared<DataTypeInt32>(), 67108864}},
{"version", {std::make_shared<DataTypeString>(), "5.7.30"}},
{"version_comment", {std::make_shared<DataTypeString>(), ""}},
{"transaction_isolation", {std::make_shared<DataTypeString>(), "READ-UNCOMMITTED"}}};
};

View File

@ -69,6 +69,7 @@ void registerFunctionErrorCodeToName(FunctionFactory &);
void registerFunctionTcpPort(FunctionFactory &);
void registerFunctionByteSize(FunctionFactory &);
void registerFunctionFile(FunctionFactory & factory);
void registerFunctionConnectionID(FunctionFactory & factory);
#if USE_ICU
void registerFunctionConvertCharset(FunctionFactory &);
@ -138,6 +139,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory)
registerFunctionTcpPort(factory);
registerFunctionByteSize(factory);
registerFunctionFile(factory);
registerFunctionConnectionID(factory);
#if USE_ICU
registerFunctionConvertCharset(factory);

View File

@ -210,6 +210,7 @@ SRCS(
cbrt.cpp
coalesce.cpp
concat.cpp
connectionID.cpp
convertCharset.cpp
cos.cpp
cosh.cpp

View File

@ -84,6 +84,9 @@ public:
String http_user_agent;
String http_referer;
/// For mysql
UInt64 connection_id = 0;
/// Comma separated list of forwarded IP addresses (from X-Forwarded-For for HTTP interface).
/// It's expected that proxy appends the forwarded address to the end of the list.
/// The element can be trusted only if you trust the corresponding proxy.

View File

@ -92,6 +92,7 @@ void MySQLHandler::run()
connection_context.makeSessionContext();
connection_context.getClientInfo().interface = ClientInfo::Interface::MYSQL;
connection_context.setDefaultFormat("MySQLWire");
connection_context.getClientInfo().connection_id = connection_id;
in = std::make_shared<ReadBufferFromPocoSocket>(socket());
out = std::make_shared<WriteBufferFromPocoSocket>(socket());

View File

@ -61,7 +61,7 @@ protected:
std::shared_ptr<MySQLProtocol::PacketEndpoint> packet_endpoint;
private:
size_t connection_id = 0;
UInt64 connection_id = 0;
size_t server_capability_flags = 0;
size_t client_capability_flags = 0;