Skip to content

#475: Improve error message for unsupported named parameters #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions dbdimp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4209,6 +4209,23 @@ int dbd_bind_ph(SV *sth, imp_sth_t *imp_sth, SV *param, SV *value,

if (param_num <= 0 || param_num > DBIc_NUM_PARAMS(imp_sth))
{
if (!looks_like_number(param))
{
STRLEN len;
char *paramstring;
paramstring = SvPV(param, len);
if(paramstring[len] == 0 && strlen(paramstring) == len)
{
do_error(sth, JW_ERR_ILLEGAL_PARAM_NUM, form("named parameters are unsupported: %s", paramstring), NULL);
return FALSE;
}
else
{
do_error(sth, JW_ERR_ILLEGAL_PARAM_NUM, "<param> could not be coerced to a C string", NULL);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test for this error?

What if there are multiple params. Does it indicate which one is the issue? Could/should it?

return FALSE;
}
}

do_error(sth, JW_ERR_ILLEGAL_PARAM_NUM, "Illegal parameter number", NULL);
return FALSE;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/DBD/mysql.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,10 @@ parameter value and C<execute> the statement again, with other values
unchanged. The attribute remains properly populated after the C<finish>
method is called, with the values from the last execution.
MySQL does not support named place holders in C<bind_param>. If a
string is passed to C<bind_param> as the parameter index then a
"named parameters are unsupported" error is reported.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"named parameters are unsupported" error is reported.
`JW_ERR_ILLEGAL_PARAM_NUM` with a message of "named parameters are unsupported" error is reported.

btw. I think JW stands for Jim Winstead. See git log

=item mysql_gtids
Returns GTID(s) if GTID session tracking is ensabled in the server via
Expand Down
53 changes: 53 additions & 0 deletions t/45bindnamedparam_error.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use strict;
use warnings;

use Test::More;
use DBI;
use vars qw($test_dsn $test_user $test_password);
use lib 't', '.';
require 'lib.pl';

my $dbh;
eval {$dbh = DBI->connect($test_dsn, $test_user, $test_password,
{ RaiseError => 1, AutoCommit => 1}) or ServerError();};

if ($@) {
plan skip_all => "no database connection";
}
plan tests => 11;

SKIP: {
skip 'SET @@auto_increment_offset needs MySQL >= 5.0.2', 2 unless $dbh->{mysql_serverversion} >= 50002;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DBD::mysql needs MySQL 8.0 or newer, so this shouldn't be needed. But on the other hand... it doesn't hurt either.

ok $dbh->do('SET @@auto_increment_offset = 1');
ok $dbh->do('SET @@auto_increment_increment = 1');
}

my $create= <<EOT;
CREATE TEMPORARY TABLE dbd_mysql_t45bindnamedparam (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
num INT(3))
EOT

ok $dbh->do($create), "create table dbd_mysql_t45bindnamedparam";

ok $dbh->do("INSERT INTO dbd_mysql_t45bindnamedparam VALUES(NULL, 1)"), "insert into dbd_mysql_t45bindnamedparam (null, 1)";

my $rows;
ok ($rows= $dbh->selectall_arrayref("SELECT * FROM dbd_mysql_t45bindnamedparam"));

is $rows->[0][1], 1, "\$rows->[0][1] == 1";

ok (my $sth = $dbh->prepare("SELECT * FROM dbd_mysql_t45bindnamedparam WHERE num = :num"));

$dbh->{PrintError} = 0;
$dbh->{PrintWarn} = 0;
eval {($sth->bind_param(":num", 1, SQL_INTEGER()));};
$dbh->{PrintError} = 1;
$dbh->{PrintWarn} = 1;
ok defined($DBI::errstr);

like($DBI::errstr, qr/named parameters are unsupported/, 'bind_param reports exepcted error with named parameter');

ok ($sth->finish());

ok ($dbh->disconnect());