Bug 7785: remove MySQL-specific syntax from C4::Members::columns()

The initial thought was to remove this function. However,
tools/import_borrowers.pl uses it. So rather than remove
it to solve the problem, it was reworked to a more generic
solution which runs faster.

By accessing $sth->{NAME} directly, the driver becomes
responsible for filling it correctly. This happens when a SELECT
is done on the borrowers table. It does not even have to have
data in the result set!

The columns method could be more generic and used elsewhere too.
Comparison between the old method and the STH method showed a
significant time difference. The old method took 35 seconds
for 40k iterations versus 19 seconds for the same amount of
iterations with the STH method regardless of the size of the
borrowers table.

Signed-off-by: Srdjan <srdjan@catalyst.net.nz>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
This commit is contained in:
Mark Tompsett 2013-06-29 20:46:51 -04:00 committed by Galen Charlton
parent 5868490f02
commit 07716ca15b
2 changed files with 70 additions and 2 deletions

View File

@ -695,10 +695,54 @@ sub GetMemberIssuesAndFines {
return ($overdue_count, $issue_count, $total_fines);
}
sub columns(;$) {
return @{C4::Context->dbh->selectcol_arrayref("SHOW columns from borrowers")};
=head2 columns
C4::Member->columns
=head3 USAGE
use C4::Member;
my @borrower_columns = C4::Member->columns;
=head3 RETURNS
The array of borrowers' table columns on success.
An empty array on failure.
=head3 NOTES
This runs significantly faster than the previous code while
being mostly SQL-agnostic.
=cut
sub columns {
# Pure ANSI SQL goodness.
my $sql = 'SELECT * FROM borrowers WHERE 1=0;';
# Get the database handle.
my $dbh = C4::Context->dbh;
# Run the SQL statement to load STH's readonly properties.
my $sth = $dbh->prepare($sql);
my $rv = $sth->execute();
# This only fails if the table doesn't exist.
# This will always be called AFTER an install or upgrade,
# so borrowers will exist!
my @data;
if ($sth->{NUM_OF_FIELDS}>0) {
@data = @{$sth->{NAME}};
}
else {
@data = ();
}
return @data;
}
=head2 ModMember
my $success = ModMember(borrowernumber => $borrowernumber,

View File

@ -0,0 +1,24 @@
#!/usr/bin/perl
#
# This is to test C4/Members
# It requires a working Koha database with the sample data
use Modern::Perl;
use Test::More tests => 2;
BEGIN {
use_ok('C4::Members');
}
my @borrowers_columns = C4::Members->columns;
ok(
$#borrowers_columns > 1,
'C4::Member->column returned a reasonable number of columns ('
. ( $#borrowers_columns + 1 ) . ')'
)
or diag(
'WARNING: Check that the borrowers table exists and has the correct fields defined.'
);
exit;