From 2204597cfef8767472f2dd25a6e6972fd45845c7 Mon Sep 17 00:00:00 2001 From: WangZengrui Date: Sat, 9 Oct 2021 16:49:49 +0800 Subject: [PATCH] add FunctionOSKernelVersion --- .../registerFunctionsMiscellaneous.cpp | 2 ++ src/Functions/serverConstants.cpp | 24 ++++++++++++++ src/Interpreters/getOSKernelVersion.cpp | 31 ------------------- src/Interpreters/getOSKernelVersion.h | 21 ------------- 4 files changed, 26 insertions(+), 52 deletions(-) delete mode 100644 src/Interpreters/getOSKernelVersion.cpp delete mode 100644 src/Interpreters/getOSKernelVersion.h diff --git a/src/Functions/registerFunctionsMiscellaneous.cpp b/src/Functions/registerFunctionsMiscellaneous.cpp index dfd986c5f82..8fc084b3d1a 100644 --- a/src/Functions/registerFunctionsMiscellaneous.cpp +++ b/src/Functions/registerFunctionsMiscellaneous.cpp @@ -81,6 +81,7 @@ void registerFunctionQueryID(FunctionFactory & factory); void registerFunctionInitialQueryID(FunctionFactory & factory); void registerFunctionServerUUID(FunctionFactory &); void registerFunctionZooKeeperSessionUptime(FunctionFactory &); +void registerFunctionOSKernelVersion(FunctionFactory &); #if USE_ICU void registerFunctionConvertCharset(FunctionFactory &); @@ -162,6 +163,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory) registerFunctionInitialQueryID(factory); registerFunctionServerUUID(factory); registerFunctionZooKeeperSessionUptime(factory); + registerFunctionOSKernelVersion(factory); #if USE_ICU registerFunctionConvertCharset(factory); diff --git a/src/Functions/serverConstants.cpp b/src/Functions/serverConstants.cpp index 9a53a5cf582..170f4cb86ee 100644 --- a/src/Functions/serverConstants.cpp +++ b/src/Functions/serverConstants.cpp @@ -7,6 +7,10 @@ #include #include +#if defined(OS_LINUX) +# include +#endif + #if !defined(ARCADIA_BUILD) # include #endif @@ -93,6 +97,17 @@ namespace explicit FunctionZooKeeperSessionUptime(ContextPtr context) : FunctionConstantBase(context, context->getZooKeeperSessionUptime()) {} static FunctionPtr create(ContextPtr context) { return std::make_shared(context); } }; + +#if defined(OS_LINUX) + class FunctionOSKernelVersion : public FunctionConstantBase + { + public: + static constexpr auto name = "OSKernelVersion"; + explicit FunctionOSKernelVersion(ContextPtr context) : FunctionConstantBase(context, Poco::Environment::osName() + " " + Poco::Environment::osVersion()) {} + static FunctionPtr create(ContextPtr context) { return std::make_shared(context); } + }; +#endif + } @@ -140,5 +155,14 @@ void registerFunctionZooKeeperSessionUptime(FunctionFactory & factory) factory.registerFunction(); } + +void registerFunctionOSKernelVersion(FunctionFactory & factory) +{ +#if defined(OS_LINUX) + factory.registerFunction(); +#endif +} + + } diff --git a/src/Interpreters/getOSKernelVersion.cpp b/src/Interpreters/getOSKernelVersion.cpp deleted file mode 100644 index c4b4564f46e..00000000000 --- a/src/Interpreters/getOSKernelVersion.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#if defined(OS_LINUX) -#include - - -namespace DB -{ - -namespace ErrorCodes -{ - extern const int SYSTEM_ERROR; -} - -String getOSKernelVersion() -{ - struct utsname os_kernel_info; - int buf = uname(&os_kernel_info); - if (buf < 0) - { - throw Exception( - "EFAULT buffer is not valid.", - ErrorCodes::SYSTEM_ERROR); - } - else - { - return String(os_kernel_info.sysname) + " " + String(os_kernel_info.release); - } -} - -} - -#endif \ No newline at end of file diff --git a/src/Interpreters/getOSKernelVersion.h b/src/Interpreters/getOSKernelVersion.h deleted file mode 100644 index fc3c7583aef..00000000000 --- a/src/Interpreters/getOSKernelVersion.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#if defined(OS_LINUX) - -#include - -#include -#include - -namespace DB -{ - -/// Returns String with OS Kernel version. -/* To get name and information about current kernel. - For simplicity, the function can be implemented only for Linux. -*/ - -String getOSKernelVersion(); - -} - -#endif \ No newline at end of file