Perl DBI(MySQL)在预准备语句中放置单引号而不是实际参数

我正在尝试做一个简单的查询作为准备好的声明,但没有成功.这是代码
package sqltest;
use DBI;

DBI->trace(2);

my $dbh = DBI->connect('dbi:MysqL:database=test;host=***;port=3306','the_username','****');
my $prep = 'SELECT me.id,me.session_data,me.expires FROM sys_session me WHERE me.id = ?';
$dbh->{raiseerror} = 1;
my $sth = $dbh->prepare($prep);
$sth->bind_param(1,'session:06b6d2138df949524092eefc066ee5ab3598bf96');
$sth->execute;
DBI::dump_results($sth);

MysqL服务器响应”’附近的语法错误.

DBI跟踪的输出显示

-> bind_param for DBD::MysqL::st (DBI::st=HASH(0x21e35cc)~0x21e34f4 1 'session:06b6d2138df949524092eefc066ee5ab3598bf96') thr#3ccdb4
 Called: dbd_bind_ph
  <- bind_param= ( 1 ) [1 items] at perl_test_dbi_params.pl line 10
[...]
>parse_params statement SELECT me.id,me.expires FROM sys_session me WHERE me.id = ?
Binding parameters: SELECT me.id,me.expires FROM sys_session me WHERE me.id = '
[...]
DBD::MysqL::st execute Failed: You have an error in your sql Syntax; check the manual that corresponds to your MysqL server version for the right Syntax to use near ''' at line 1

所以对我而言,看起来声明并没有得到应有的准备.
当我发送没有参数的查询时,它按预期工作.

在这里想念什么?

DBI版本是DBI 1.637-ithread,MysqL版本是5.5.57-0 deb8u1

使用针对MSWin32-x86-multi-thread-64int构建的Windows perl 5,版本26,subversion 1(v5.26.1)进行测试
和Ubuntu perl 5,版本22,颠覆1(v5.22.1)为x86_64-linux-gnu-thread-multi构建

EDIT1:
for context:我在使用Catalyst和Catalyst::Plugin::Session::Store::DBIC时注意到了这个问题.这里,id-column是一个Varchar(72)类型,它包含一个session-id.

EDIT2:

> DBD :: MysqL版本是4.043
>通过$sth->执行绑定(‘session:foo’);导致同样的问题
>通过$sth-> bind_param绑定(‘session:foo’,sql_VARCHAR);导致同样的问题
>绑定数字字段确实有效,但只能使用显式类型定义$sth-> bind_param(1,1512407082,sql_INTEGER);

EDIT3:
我找到了做更多测试的时间,但没有令人满意的结果:

>我能够使用较旧的服务器进行测试并且有效. DBI和DBD :: MysqL的版本是相同的,但我发现服务器使用的是MysqL 5.5客户端,在DBI-trace中报告为MysqL_VERSION_ID 50557,而我的两个原始测试服务器都使用MysqL 5.7 MysqL_VERSION_ID 50720和MysqL_VERSION_ID 50716
>使用$dbh-> {MysqL_server_prepare} = 1;它有效!也许这有助于找到这个问题的人,但我现在更愿意找到问题的真正原因

解决方法

从您的跟踪日志可以看出,问号占位符(?)被DBD :: MysqL替换为一个撇号(‘).所以它是纯DBD :: MysqL的bug.乍一看它完全没有意义……因为占位符被放入两个撇号的参数替换.

可以在那里找到执行此占位符替换的相关代码https://metacpan.org/source/MICHIELB/DBD-mysql-4.043/dbdimp.c#L784-786

*ptr++ = '\'';
          ptr += MysqL_real_escape_string(sock,ptr,valbuf,vallen);
          *ptr++ = '\'';

所以问题是,上面的C代码可以在* ptr缓冲区中产生一个撇号吗?答案是肯定的,当MysqL_real_escape_string()返回相同值的整数作为指针大小减去1 – 当两个撇号被写入* ptr缓冲区中的相同位置时,模拟递减1的数值运算.

这会发生吗?是的,它可以,因为Oracle在MysqL 5.7.6客户端库中更改了MysqL_real_escape_string()C函数的API:

https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-6.html#mysqld-5-7-6-feature

Incompatible Change: A new C API function,MysqL_real_escape_string_quote(),has been implemented as a replacement for MysqL_real_escape_string() because the latter function can fail to properly encode characters when the NO_BACKSLASH_ESCAPES sql mode is enabled. In this case,MysqL_real_escape_string() cannot escape quote characters except by doubling them,and to do this properly,it must kNow more information about the quoting context than is available. MysqL_real_escape_string_quote() takes an extra argument for specifying the quoting context. For usage details,see MysqL_real_escape_string_quote().

MysqL 5.7.6版本的MysqL_real_escape_string()文档说:

https://dev.mysql.com/doc/refman/5.7/en/mysql-real-escape-string.html

Return Values: The length of the encoded string that is placed into the to argument,not including the terminating null byte,or -1 if an error occurs.

因此,如果在MysqL服务器上启用NO_BACKSLASH_ESCAPES sql模式,则MysqL 5.7.6客户端的MysqL_real_escape_string()无法正常工作并返回错误,因此-1会转换为unsigned long. unsigned long在32bit和64bit x86平台上都与指针大小相同,因此在DBD :: MysqL驱动程序的C代码之上产生一个撇号字符.

现在我在下面的pull请求中修复了DBD::MariaDB驱动程序(DBD :: MysqL的fork)的这个问题:https://github.com/gooddata/DBD-MariaDB/pull/77

所以当使用MysqL 5.7客户端库编译时,DBD :: MariaDB也是兼容的.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...