CC: | brios777 [...] gmail.com |
Subject: | proposed patch for DBIx::Log4perl |
Date: | Sat, 16 Jan 2010 17:45:27 -0500 |
To: | bug-DBIx-Log4perl [...] rt.cpan.org |
From: | Bill Rios <brios777 [...] gmail.com> |
Hi,
This module has proved to be a great way to log queries from my perl
apps without having to turn on mysql's query log (which would be huge
and probably hurt the server's performance quite a bit). I came across
this issue though after changing the DBI->connect() calls to
DBIx::Log4perl->connect() in a fairly large perl application. There
were several places in the perl code where the return value of the
fetchrow_array() function was being set to a scalar instead of an array.
This was being done as a quick way to grab the value for a query that
only returns one value (that is, one row with one column). So the DBI
module returns the value in the database (actually it apparently returns
the last column of the row). The DBIx::Log4perl module returns the
number of columns in the row.
Example DB table:
Show quoted text
mysql> desc t1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| str1 | varchar(25) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
Show quoted textmysql> select * from t1;
+----+-------+
| id | str1 |
+----+-------+
| 1 | test |
| 2 | hello |
+----+-------+
2 rows in set (0.00 sec)
test.pl (using the DBI module):
#!/usr/bin/perl
use strict;
use DBI;
my $dbh = DBI->connect("dbi:mysql:test:localhost", "root", "") or die DBI->errstr();
my $q = "select id, str1 from t1";
my $sth = $dbh->prepare($q);
$sth->execute();
my $val = $sth->fetchrow_array();
$sth->finish();
$dbh->disconnect();
print "val=$val\n";
output from test.pl:
val=test
test2.pl (using DBIx::Log4perl)
#!/usr/bin/perl
use strict;
use DBIx::Log4perl;
my $dbh = DBIx::Log4perl->connect("dbi:mysql:test:localhost", "root", "", {DBIx_l4p_init => "/etc/mylog.conf", DBIx_l4p_class => "DBIx::Log4perl"}) or die DBI->errstr();
my $q = "select id, str1 from t1";
my $sth = $dbh->prepare($q);
$sth->execute();
my $val = $sth->fetchrow_array();
$sth->finish();
$dbh->disconnect();
print "val=$val\n";
output from test2.pl:
val=2
Proposed patch to st.pm:
244c244
< return @row;
---
Show quoted text> return wantarray() ? @row : $row[@row - 1];
After applying this patch, test.pl and test2.pl produce the same output
as expected.
BTW, thanks for writing this module! :-)
Regards,
Bill Rios
Message body is not shown because sender requested not to inline it.