From a2df4c2011c90ac36a92f6cb6f472104ba1dfd01 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 5 Sep 2018 18:41:32 +0000 Subject: [PATCH] Bug 21281: Correct t/db_dependent/Creators/Lib.t failures TEST PLAN --------- 1) apply first patch 2) kshell kohadev 3) prove -r -v t/db_dependent/Creators/ -- failures 4) apply this patch 5) repeat 2,3 -- no failures 6) run koha qa test tools --- C4/Creators/Lib.pm | 21 +++++++++++++++++---- t/db_dependent/Creators/Lib.t | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/C4/Creators/Lib.pm b/C4/Creators/Lib.pm index 46ae21a328..f5616a5c05 100644 --- a/C4/Creators/Lib.pm +++ b/C4/Creators/Lib.pm @@ -67,7 +67,11 @@ C4::Creators::Lib sub _SELECT { my @params = @_; - my $query = "SELECT `$params[0]` FROM $params[1]"; + my $fields_list = $params[0]; + if (index($fields_list, ' ')==-1 && index($fields_list,',')==-1 && $fields_list ne '*') { + $fields_list = "`$fields_list`"; + } + my $query = "SELECT $fields_list FROM $params[1]"; $params[2] ? $query .= " WHERE $params[2];" : $query .= ';'; my $sth = C4::Context->dbh->prepare($query); # $sth->{'TraceLevel'} = 3; @@ -145,16 +149,25 @@ my $output_formats = [ sub _build_query { my ( $params, $table ) = @_; my @fields = exists $params->{fields} ? @{ $params->{fields} } : (); - my $query = "SELECT " . ( @fields ? join(', ', map {"`$_`"} @fields ) : '*' ) . " FROM $table"; + my @fields2 = (); + foreach my $field_name (@fields) { + if (index($field_name,' ')==-1 && $field_name ne '*') { + push @fields2, "`$field_name`"; + } else { + push @fields2, $field_name; + } + } + @fields = @fields2; + my $query = "SELECT " . ( @fields ? join(', ', @fields ) : '*' ) . " FROM $table"; my @where_args; if ( exists $params->{filters} ) { $query .= ' WHERE 1 '; while ( my ( $field, $values ) = each %{ $params->{filters} } ) { if ( ref( $values ) ) { - $query .= " AND $field IN ( " . ( ('?,') x (@$values-1) ) . "? ) "; # a comma separates elements in a list... + $query .= " AND `$field` IN ( " . ( ('?,') x (@$values-1) ) . "? ) "; # a comma separates elements in a list... push @where_args, @$values; } else { - $query .= " AND $field = ? "; + $query .= " AND `$field` = ? "; push @where_args, $values; } } diff --git a/t/db_dependent/Creators/Lib.t b/t/db_dependent/Creators/Lib.t index 7a981dee48..d1808508ea 100644 --- a/t/db_dependent/Creators/Lib.t +++ b/t/db_dependent/Creators/Lib.t @@ -398,7 +398,7 @@ is( $templates->[0]->{creator}, 'Labels', 'creator is good $templates = get_all_templates( { filters => { rows => [-42, 7]} } ); is( @$templates, $count, 'There is 1 template matching' ); # With orderby param ------------------ -$templates = get_all_templates( { orderby => 'rows DESC' } ); +$templates = get_all_templates( { orderby => '`rows` DESC' } ); $query = ' SELECT count(*) -- 2.11.0