--- a/C4/Members.pm +++ a/C4/Members.pm @@ -695,10 +695,55 @@ 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 = (); + } + $sth->finish(); + return @data; } + =head2 ModMember my $success = ModMember(borrowernumber => $borrowernumber, --- a/t/db_dependent/Members_columns.t +++ a/t/db_dependent/Members_columns.t @@ -0,0 +1,25 @@ +#!/usr/bin/perl +# +# This is to test C4/Members +# It requires a working Koha database with the sample data + +use strict; +use warnings; + +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; --