Perl DBD::MySQL 执行不支持限制关键字后的字符串参数。始终被视为数字

Bry*_*eld 1 mysql quotes perl sanitization limit

我们最近使用最新的 DBI MySQL、Perl 等设置了一个新操作系统。

这种模式发现了我们的几个应用程序,并且在安装在新操作系统上时被破坏了。

my $sth = $dbh->prepare('select null colname, null colname2 limit 0'
        . ' union all select x, y from tbl where col like ?'
        . ' union all select z, a from tbl2 where col like ?');
$sth->execute('%test%', '%test%') or die ...;
Run Code Online (Sandbox Code Playgroud)

该错误是 MySQL 语法错误。我们已经确定了参数引用方式中的问题。

在上面的例子中,它在 MySQL 中是这样解析的。

select null colname, null colname2 limit 0
union all select x, y from tbl where col like 
union all select z, a from tbl2 where col like 
Run Code Online (Sandbox Code Playgroud)

但是,如果'%test%'用数字换出(例如'555'),它就会通过。

select null colname, null colname2 limit 0
union all select x, y from tbl where col like 555
union all select z, a from tbl2 where col like 555
Run Code Online (Sandbox Code Playgroud)

请注意没有引号。

我们已经确定这与limit关键字的存在有关。删除关键字可以解决语法错误。

select null colname, null colname2
union all select x, y from tbl where col like '%test%'
union all select z, a from tbl2 where col like '%test%'
Run Code Online (Sandbox Code Playgroud)

此外,'555'在 Perl 中的设置现在会在'555'MySQL 中产生,带引号。

我们发现使用派生表是目前最快的解决方法,

select * from (select null colname, null colname2 limit 0)a
union all select x, y from tbl where col like '%test%'
union all select z, a from tbl2 where col like '%test%'
Run Code Online (Sandbox Code Playgroud)

(只试过括号,但需要派生表才能工作)但我很好奇是否有办法通过 DBI/DBD::MySQL 接口来控制引用方法。(避免更新声明)

要么更改默认行为以禁用limit关键字逻辑,
要么将特定参数类型强制为字符串?

cho*_*oba 5

解析包含 LIMIT 的命令是有问题的。试试DBD::MariaDB,它希望已经解决了这个问题(这里)。