Skip to content

Commit 7bd63ad

Browse files
committed
Add tests for new "named parameters are unsupported" error message.
If a named parameter is passed to bind_param() ensure the expected error message is reported.
1 parent 00f1cc4 commit 7bd63ad

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

t/45bindnamedparam_error.t

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use strict;
2+
use warnings;
3+
4+
use Test::More;
5+
use DBI;
6+
use vars qw($test_dsn $test_user $test_password);
7+
use lib 't', '.';
8+
require 'lib.pl';
9+
10+
my $dbh;
11+
eval {$dbh = DBI->connect($test_dsn, $test_user, $test_password,
12+
{ RaiseError => 1, AutoCommit => 1}) or ServerError();};
13+
14+
if ($@) {
15+
plan skip_all => "no database connection";
16+
}
17+
plan tests => 15;
18+
19+
SKIP: {
20+
ok $dbh->do('SET @@auto_increment_offset = 1');
21+
ok $dbh->do('SET @@auto_increment_increment = 1');
22+
}
23+
24+
my $create= <<EOT;
25+
CREATE TEMPORARY TABLE dbd_mysql_t45bindnamedparam (
26+
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
27+
num INT(3))
28+
EOT
29+
30+
ok $dbh->do($create), "create table dbd_mysql_t45bindnamedparam";
31+
32+
ok $dbh->do("INSERT INTO dbd_mysql_t45bindnamedparam VALUES(NULL, 1)"), "insert into dbd_mysql_t45bindnamedparam (null, 1)";
33+
34+
my $rows;
35+
ok ($rows= $dbh->selectall_arrayref("SELECT * FROM dbd_mysql_t45bindnamedparam"));
36+
37+
is $rows->[0][1], 1, "\$rows->[0][1] == 1";
38+
39+
my $sth;
40+
ok ($sth = $dbh->prepare("SELECT * FROM dbd_mysql_t45bindnamedparam WHERE num = :num"));
41+
42+
$dbh->{PrintError} = 0;
43+
$dbh->{PrintWarn} = 0;
44+
eval {($sth->bind_param(":num", 1, SQL_INTEGER()));};
45+
$dbh->{PrintError} = 1;
46+
$dbh->{PrintWarn} = 1;
47+
ok defined($DBI::errstr);
48+
49+
like($DBI::errstr, qr/named parameters are unsupported/, 'bind_param reports expected error with named parameter (string type)');
50+
51+
ok ($sth->finish());
52+
53+
54+
ok ($sth = $dbh->prepare("SELECT * FROM dbd_mysql_t45bindnamedparam WHERE num = :num"));
55+
56+
$dbh->{PrintError} = 0;
57+
$dbh->{PrintWarn} = 0;
58+
eval {($sth->bind_param("\0:num", 1, SQL_INTEGER()));};
59+
$dbh->{PrintError} = 1;
60+
$dbh->{PrintWarn} = 1;
61+
ok defined($DBI::errstr);
62+
63+
like($DBI::errstr, qr/could not be coerced to a C string/, 'bind_param reports expected error with named parameter (non-string type)');
64+
65+
ok ($sth->finish());
66+
67+
68+
ok ($dbh->disconnect());

0 commit comments

Comments
 (0)