X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FSQLHelper.pm;h=44707428216e19b706e2d02c5f591fdcbe192ae9;hb=d4260af339b74b597e96b5223e5ee8d88de5925d;hp=d2df0a533ef39ad8fddd1a46181163a793b3b6fd;hpb=486c0b075e81be34ba5892a7adccfac7f1ed4ce4;p=koha.git diff --git a/C4/SQLHelper.pm b/C4/SQLHelper.pm index d2df0a533e..4470742821 100644 --- a/C4/SQLHelper.pm +++ b/C4/SQLHelper.pm @@ -13,9 +13,9 @@ package C4::SQLHelper; # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use strict; @@ -97,7 +97,7 @@ $searchtype is string Can be "start_with" or "exact" sub SearchInTable{ my ($tablename,$filters,$orderby, $limit, $columns_out, $filter_columns,$searchtype) = @_; -# $searchtype||="start_with"; + $searchtype||="exact"; my $dbh = C4::Context->dbh; $columns_out||=["*"]; my $sql = do { local $"=', '; @@ -116,10 +116,13 @@ sub SearchInTable{ } if ($orderby){ #Order by desc by default - my @orders=map{ "$_".($$orderby{$_}? " DESC" : "") } keys %$orderby; - $sql.= do { local $"=', '; - qq{ ORDER BY @orders} - }; + my @orders; + foreach my $order (@$orderby){ + push @orders,map{ "$_".($order->{$_}? " DESC " : "") } keys %$order; + } + $sql.= do { local $"=', '; + qq{ ORDER BY @orders} + }; } if ($limit){ $sql.=qq{ LIMIT }.join(",",@$limit); @@ -137,7 +140,7 @@ sub SearchInTable{ =over 4 - $data_id_in_table = &InsertInTable($tablename,$data_hashref); + $data_id_in_table = &InsertInTable($tablename,$data_hashref,$withprimarykeys); =back @@ -146,9 +149,9 @@ sub SearchInTable{ =cut sub InsertInTable{ - my ($tablename,$data) = @_; + my ($tablename,$data,$withprimarykeys) = @_; my $dbh = C4::Context->dbh; - my ($keys,$values)=_filter_hash($tablename,$data,0); + my ($keys,$values)=_filter_hash($tablename,$data,($withprimarykeys?"exact":0)); my $query = qq{ INSERT INTO $tablename SET }.join(", ",@$keys); $debug && warn $query, join(",",@$values); @@ -233,7 +236,7 @@ sub DeleteInTable{ sub GetPrimaryKeys($) { my $tablename=shift; my $hash_columns=_get_columns($tablename); - return grep { $$hash_columns{$_}{'Key'} =~/PRI/i} keys %$hash_columns; + return grep { $hash_columns->{$_}->{'Key'} =~/PRI/i} keys %$hash_columns; } =head2 _get_columns @@ -341,6 +344,7 @@ sub _filter_fields{ } } else{ + $debug && warn "filterstring : $filter_input"; my ($keys, $values) = _filter_string($tablename,$filter_input, $searchtype,$filtercolumns); if ($keys){ my $stringkey="(".join (") AND (",@$keys).")"; @@ -364,8 +368,8 @@ sub _filter_hash{ my $elements=join "|",@columns_filtered; foreach my $field (grep {/\b($elements)\b/} keys %$filter_input){ ## supposed to be a hash of simple values, hashes of arrays could be implemented - $$filter_input{$field}=format_date_in_iso($$filter_input{$field}) if ($$columns{$field}{Type}=~/date/ && $$filter_input{$field} !~C4::Dates->regexp("iso")); - my ($tmpkeys, $localvalues)=_Process_Operands($$filter_input{$field},"$tablename.$field",$searchtype,$columns); + $filter_input->{$field}=format_date_in_iso($filter_input->{$field}) if ($columns->{$field}{Type}=~/date/ && $filter_input->{$field} !~C4::Dates->regexp("iso")); + my ($tmpkeys, $localvalues)=_Process_Operands($filter_input->{$field},"$tablename.$field",$searchtype,$columns); if (@$tmpkeys){ push @values, @$localvalues; push @keys, @$tmpkeys; @@ -414,28 +418,28 @@ sub _Process_Operands{ push @tmpkeys, " $field = ? "; push @values, $operand; #By default, exact search - unless ($searchtype){ + if (!$searchtype ||$searchtype eq "exact"){ return \@tmpkeys,\@values; } - if ($searchtype eq "contain"){ - my $col_field=(index($field,".")>0?substr($field, index($field,".")+1):$field); - if ($field=~/(?0?substr($field, index($field,".")+1):$field); + if ($field=~/(?0?substr($field, index($field,".")+1):$field); - if ($field=~/(?{$col_field}->{Type}=~/varchar|text/i){ + my @localvaluesextended; + if ($searchtype eq "contain"){ + push @tmpkeys,(" $field LIKE ? "); + push @localvaluesextended,("\%$operand\%") ; + } + if ($searchtype eq "field_start_with"){ + push @tmpkeys,("$field LIKE ?"); + push @localvaluesextended, ("$operand\%") ; + } + if ($searchtype eq "start_with"){ + push @tmpkeys,("$field LIKE ?","$field LIKE ?"); + push @localvaluesextended, ("$operand\%", " $operand\%") ; + } + push @values,@localvaluesextended; } push @localkeys,qq{ (}.join(" OR ",@tmpkeys).qq{) }; return (\@localkeys,\@values);