Browse Source

Bug 11425: Get column names using DBIX

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
3.18.x
Jonathan Druart 9 years ago
committed by Tomas Cohen Arazi
parent
commit
3e3717a861
  1. 15
      C4/Items.pm
  2. 33
      C4/SQLHelper.pm
  3. 4
      t/db_dependent/SQLHelper.t

15
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';

33
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

4
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");

Loading…
Cancel
Save