From 1a2d5e9b0811f3762a0e3819872e971bb22cccb9 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 16 Apr 2014 12:25:23 +0200 Subject: [PATCH] Bug 11425: Get column names using DBIX --- C4/Items.pm | 15 ++++++++------- C4/SQLHelper.pm | 33 --------------------------------- t/db_dependent/SQLHelper.t | 4 ---- 3 files changed, 8 insertions(+), 44 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index eb4fdb1..b08f1b3 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -35,7 +35,8 @@ use DateTime::Format::MySQL; use Data::Dumper; # used as part of logging item record changes, not just for # debugging; so please don't remove this use Koha::DateUtils qw/dt_from_string/; -use C4::SQLHelper qw(GetColumns); + +use Koha::Database; use vars qw($VERSION @ISA @EXPORT); @@ -2596,9 +2597,9 @@ sub _SearchItems_build_where_fragment { }; } } else { - my @columns = GetColumns('items'); - push @columns, GetColumns('biblio'); - push @columns, GetColumns('biblioitems'); + my @columns = Koha::Database->new()->schema()->resultset('Item')->result_source->columns; + push @columns, Koha::Database->new()->schema()->resultset('Biblio')->result_source->columns; + push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns; my @operators = qw(= != > < >= <= like); my $field = $filter->{field}; if ( (0 < grep /^$field$/, @columns) or (substr($field, 0, 5) eq 'marc:') ) { @@ -2725,9 +2726,9 @@ sub SearchItems { $query .= qq{ WHERE $where_str }; } - my @columns = GetColumns('items'); - push @columns, GetColumns('biblio'); - push @columns, GetColumns('biblioitems'); + my @columns = Koha::Database->new()->schema()->resultset('Item')->result_source->columns; + push @columns, Koha::Database->new()->schema()->resultset('Biblio')->result_source->columns; + push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns; my $sortby = (0 < grep {$params->{sortby} eq $_} @columns) ? $params->{sortby} : 'itemnumber'; my $sortorder = (uc($params->{sortorder}) eq 'ASC') ? 'ASC' : 'DESC'; diff --git a/C4/SQLHelper.pm b/C4/SQLHelper.pm index 55204eb..b867094 100644 --- a/C4/SQLHelper.pm +++ b/C4/SQLHelper.pm @@ -56,7 +56,6 @@ BEGIN { UpdateInTable GetPrimaryKeys clear_columns_cache - GetColumns ); %EXPORT_TAGS = ( all =>[qw( InsertInTable DeleteInTable SearchInTable UpdateInTable GetPrimaryKeys)] ); @@ -297,38 +296,6 @@ sub _get_columns { return $hashref->{$tablename}; } -=head2 GetColumns - - my @columns = GetColumns($tablename); - -Given a tablename, returns an array of columns names. - -=cut - -sub GetColumns { - my ($tablename) = @_; - - return unless $tablename; - return unless $tablename =~ /^[a-zA-Z0-9_]+$/; - - # Get the database handle. - my $dbh = C4::Context->dbh; - - # Pure ANSI SQL goodness. - my $sql = "SELECT * FROM $tablename WHERE 1=0;"; - - # Run the SQL statement to load STH's readonly properties. - my $sth = $dbh->prepare($sql); - my $rv = $sth->execute(); - - my @data; - if ($rv) { - @data = @{$sth->{NAME}}; - } - - return @data; -} - =head2 _filter_columns =over 4 diff --git a/t/db_dependent/SQLHelper.t b/t/db_dependent/SQLHelper.t index f585736..6074608 100755 --- a/t/db_dependent/SQLHelper.t +++ b/t/db_dependent/SQLHelper.t @@ -64,7 +64,3 @@ ok($status>0 && !($@), "DeleteInTable OK"); $status=DeleteInTable("branches", {branchcode => 'ZZZZ'}); ok($status>0 && !($@), "DeleteInTable (branch) OK"); -my @biblio_columns = C4::SQLHelper::GetColumns('biblio'); -my @expected_columns = qw(biblionumber frameworkcode author title unititle notes - serial seriestitle copyrightdate timestamp datecreated abstract); -is_deeply([sort @biblio_columns], [sort @expected_columns], "GetColumns('biblio') returns all columns of biblio table"); -- 1.7.10.4