Subject: | $dbh->do() function has problem with encoding |
Here is test script:
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=postgres", "", "");
$dbh->do("CREATE TEMPORARY TABLE t(s VARCHAR(10))");
$dbh->do("INSERT INTO t(s) VALUES('\xC3\xA1')");
my ($val1) = $dbh->selectrow_array("SELECT s FROM t");
print 'do - ' . ($val1 eq "\xC3\xA1" ? 'OK' : 'FAIL') . ': ' . (join ', ', map ord, split //, $val1) . "\n";
$dbh->do("DROP TABLE t");
$dbh->do("CREATE TEMPORARY TABLE t(s VARCHAR(10))");
my $sth = $dbh->prepare("INSERT INTO t(s) VALUES('\xC3\xA1')");
$sth->execute();
my ($val2) = $dbh->selectrow_array("SELECT s FROM t");
print 'prepare+execute - ' . ($val2 eq "\xC3\xA1" ? 'OK' : 'FAIL') . ': ' . (join ', ', map ord, split //, $val2) . "\n";
$dbh->do("DROP TABLE t");
It inserts two characters C3 (LATIN CAPITAL LETTER A WITH TILDE) and A1 (INVERTED EXCLAMATION MARK) into database, then fetches it and checks that inserted value is the same as fetched. Plus prints ordinals of those characters (in decimal notation).
There are two INSERT variants, one via do() and one via prepare()+execute().
Apparently, that variant via do() does not work and returns different data as inserted. Variant via prepare()+execute() is working fine.
Here is output from that script with DBD::Pg version 3.6.2:
do - FAIL: 225
prepare+execute - OK: 195, 161
195 in dec is C3 in hex and 161 in dec is A1 in hex, so output from prepare()+execute() is OK.
I consider this a bug as do() should be really just equivalent of prepare()+execute().