From 3e3717a86112b17e2b2e5bb7a0215e46b388a632 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 Signed-off-by: Bernardo Gonzalez Kriegel Signed-off-by: Kyle M Hall --- 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 a2a30766f8..62e89c8f9b 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); @@ -2637,9 +2638,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:') ) { @@ -2766,9 +2767,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 55204eb735..b8670942c5 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 f5857365cf..6074608418 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"); -- 2.39.5