检查 MariaDB / MySQL 中是否存在(内部)函数/功能/命令

问题描述 投票:0回答:1

我在我的应用程序中使用 MEDIAN 函数。当我将其部署到生产服务器上时,我得到

Syntax error
,因为有旧版本的 MariaDB。

有没有办法检查某个函数是否存在? PHP 中类似 function_exists() 的东西?

我可以像这样检查版本

SELECT VERSION();
并维护一些功能和版本列表。但我正在寻找更简单、更通用的解决方案。

现在我这样做:

// PHP + NETTE framework

$sqlVersionOK = $this->checkSqlServerVersion([10,3,3]); 

if ($sqlVersionOK) { /* ... */ } else { /* ... */ }

public function checkSqlServerVersion(array $nums):bool {
    
    $version = $this->repository->query('SELECT VERSION() AS num;')->fetch();

    $version = explode('.', $version->num);
    $ver = array_map(function ($item) { return (int) filter_var($item, FILTER_SANITIZE_NUMBER_INT); }, $version);
    
    if ($ver[0] > $nums[0]) { return TRUE; } // major number is bigger
    elseif($ver[0]==$nums[0] and $ver[1] >= $nums[1]) { return TRUE; } // major number is the same, minor is bigger or same
    elseif($ver[0]==$nums[0] and $ver[1] == $nums[1] and $ver[2] >= $nums[1]) { return TRUE; } // major and minor numbers are the same, patch is bigger or same
    else { return FALSE; } 
    
}

谢谢。

php mariadb nette
1个回答
0
投票

10.6 添加了 information_schema.keywords 但对您来说还不够早。 (而且我不确定它涵盖所有功能)。

给定 MEDIAN 是 10.3+ 的窗口函数,而仅在 10.2 中添加的窗口函数给出了何时可以找到替代窗口的非常有限的窗口(双关语)。

存储聚合函数仅在 10.3 中添加,因此不可能在 10.2 中创建等效的 SQL 函数。

MariaDB 有一个可执行注释语法,其中某些函数仅在服务器版本足够时才执行,例如:

/*M!100303 SELECT MEDIAN(star_rating) OVER (PARTITION BY name) FROM book_rating */

这将是 10.2 实例上的空查询。

然而,更务实的答案是:

  • 在生产环境中运行与测试/开发环境相同的版本
  • 即使 10.3 也刚刚到达上游维护结束,请抓住机会升级到更新版本并获得所有好处,而无需花费太多时间在未维护的软件上编写替代方案。
© www.soinside.com 2019 - 2023. All rights reserved.