X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FSQLHelper.pm;h=e1cb5d0a9aa35e8787d95e867c69077184d2a87f;hb=f91bd36399b1ab46649482f80e0ff1b438a8d9f6;hp=44ecd6afe9542d7290b0d740f16b63b034b88c29;hpb=687943ff38e76cd4d842858542479170e726f55d;p=koha_gimpoz diff --git a/C4/SQLHelper.pm b/C4/SQLHelper.pm index 44ecd6afe9..e1cb5d0a9a 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; @@ -24,10 +24,26 @@ use List::MoreUtils qw(first_value any); use C4::Context; use C4::Dates qw(format_date_in_iso); use C4::Debug; -use YAML; require Exporter; use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS); +eval { + my $servers = C4::Context->config('memcached_servers'); + if ($servers) { + require Memoize::Memcached; + import Memoize::Memcached qw(memoize_memcached); + + my $memcached = { + servers => [$servers], + key_prefix => C4::Context->config('memcached_namespace') || 'koha', + expire_time => 600 + }; # cache for 10 mins + + memoize_memcached( '_get_columns', memcached => $memcached ); + memoize_memcached( 'GetPrimaryKeys', memcached => $memcached ); + } +}; + BEGIN { # set the version for version checking $VERSION = 0.5; @@ -39,13 +55,14 @@ BEGIN { SearchInTable UpdateInTable GetPrimaryKeys + clear_columns_cache ); %EXPORT_TAGS = ( all =>[qw( InsertInTable DeleteInTable SearchInTable UpdateInTable GetPrimaryKeys)] ); } my $tablename; -my $hash; +my $hashref; =head1 NAME @@ -61,37 +78,51 @@ This module contains routines for adding, modifying and Searching Data in MysqlD =head1 FUNCTIONS -=over 2 - -=back - - =head2 SearchInTable -=over 4 + $hashref = &SearchInTable($tablename,$data, $orderby, $limit, + $columns_out, $filtercolumns, $searchtype); - $hashref = &SearchInTable($tablename,$data, $orderby, $limit, $columns_out, $filtercolumns, $searchtype); - -=back $tablename Name of the table (string) + $data may contain - string + - data_hashref : will be considered as an AND of all the data searched + - data_array_ref on hashrefs : Will be considered as an OR of Datahasref elements $orderby is an arrayref of hashref with fieldnames as key and 0 or 1 as values (ASCENDING or DESCENDING order) -$limit is an array ref on 2 values + +$limit is an array ref on 2 values in order to limit results to MIN..MAX + $columns_out is an array ref on field names is used to limit results on those fields (* by default) + $filtercolums is an array ref on field names : is used to limit expansion of research for strings -$searchtype is string Can be "wide" or "exact" + +$searchtype is string Can be "start_with" or "exact" + +This query builder is very limited, it should be replaced with DBIx::Class +or similar very soon +Meanwhile adding support for special key '' in case of a data_hashref to +support filters of type + + ( f1 = a OR f2 = a ) AND fx = b AND fy = c + +Call for the query above is: + + SearchInTable($tablename, {'' => a, fx => b, fy => c}, $orderby, $limit, + $columns_out, [f1, f2], 'exact'); + +NOTE: Current implementation may remove parts of the iinput hashrefs. If that is a problem +a copy needs to be created in _filter_fields() below =cut sub SearchInTable{ my ($tablename,$filters,$orderby, $limit, $columns_out, $filter_columns,$searchtype) = @_; -warn "searchtype : ",$searchtype; -# $searchtype||="start_with"; + $searchtype||="exact"; my $dbh = C4::Context->dbh; $columns_out||=["*"]; my $sql = do { local $"=', '; @@ -103,17 +134,24 @@ warn "searchtype : ",$searchtype; if ($keys){ my @criteria=grep{defined($_) && $_ !~/^\W$/ }@$keys; if (@criteria) { - $sql.= do { local $"=') AND ('; + $sql.= do { local $"=') OR ('; qq{ WHERE (@criteria) } }; } } 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 ( ref($orderby) ? @$orderby : $orderby ){ + if (ref $order) { + push @orders,map{ "$_".($order->{$_}? " DESC " : "") } keys %$order; + } else { + push @orders,$order; + } + } + $sql.= do { local $"=', '; + qq{ ORDER BY @orders} + }; } if ($limit){ $sql.=qq{ LIMIT }.join(",",@$limit); @@ -121,46 +159,40 @@ warn "searchtype : ",$searchtype; $debug && $values && warn $sql," ",join(",",@$values); $sth = $dbh->prepare_cached($sql); - $sth->execute(@$values); + eval{$sth->execute(@$values)}; + warn $@ if ($@ && $debug); my $results = $sth->fetchall_arrayref( {} ); return $results; } =head2 InsertInTable -=over 4 + $data_id_in_table = &InsertInTable($tablename,$data_hashref,$withprimarykeys); - $data_id_in_table = &InsertInTable($tablename,$data_hashref); +Insert Data in table and returns the id of the row inserted -=back - - Insert Data in table - and returns the id of the row inserted =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); my $sth = $dbh->prepare_cached($query); - $sth->execute( @$values); + eval{$sth->execute(@$values)}; + warn $@ if ($@ && $debug); return $dbh->last_insert_id(undef, undef, $tablename, undef); } =head2 UpdateInTable -=over 4 - $status = &UpdateInTable($tablename,$data_hashref); -=back +Update Data in table and returns the status of the operation - Update Data in table - and returns the status of the operation =cut sub UpdateInTable{ @@ -169,6 +201,7 @@ sub UpdateInTable{ my @ids=@$data{@field_ids}; my $dbh = C4::Context->dbh; my ($keys,$values)=_filter_hash($tablename,$data,0); + return unless ($keys); my $query = qq{ UPDATE $tablename SET }.join(",",@$keys).qq{ @@ -176,20 +209,18 @@ sub UpdateInTable{ $debug && warn $query, join(",",@$values,@ids); my $sth = $dbh->prepare_cached($query); - return $sth->execute( @$values,@ids); - + my $result; + eval{$result=$sth->execute(@$values,@ids)}; + warn $@ if ($@ && $debug); + return $result; } =head2 DeleteInTable -=over 4 - $status = &DeleteInTable($tablename,$data_hashref); -=back +Delete Data in table and returns the status of the operation - Delete Data in table - and returns the status of the operation =cut sub DeleteInTable{ @@ -202,34 +233,47 @@ sub DeleteInTable{ }; $debug && warn $query, join(",",@$values); my $sth = $dbh->prepare_cached($query); - return $sth->execute( @$values); + my $result; + eval{$result=$sth->execute(@$values)}; + warn $@ if ($@ && $debug); + return $result; } } =head2 GetPrimaryKeys -=over 4 - @primarykeys = &GetPrimaryKeys($tablename) -=back +Get the Primary Key field names of the table - Get the Primary Key field names of the table =cut 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 -=over 4 +=head2 clear_columns_cache -_get_columns($tablename) + C4::SQLHelper->clear_columns_cache(); + +cleans the internal cache of sysprefs. Please call this method if +you update a tables structure. Otherwise, your new changes +will not be seen by this process. + +=cut + +sub clear_columns_cache { + %$hashref = (); +} -=back + + +=head2 _get_columns + + _get_columns($tablename) Given a tablename Returns a hashref of all the fieldnames of the table @@ -241,11 +285,15 @@ With =cut sub _get_columns($) { - my ($tablename)=@_; - my $dbh=C4::Context->dbh; - my $sth=$dbh->prepare_cached(qq{SHOW COLUMNS FROM $tablename }); - $sth->execute; - my $columns= $sth->fetchall_hashref(qw(Field)); + my ($tablename) = @_; + unless ( exists( $hashref->{$tablename} ) ) { + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare_cached(qq{SHOW COLUMNS FROM $tablename }); + $sth->execute; + my $columns = $sth->fetchall_hashref(qw(Field)); + $hashref->{$tablename} = $columns; + } + return $hashref->{$tablename}; } =head2 _filter_columns @@ -283,11 +331,7 @@ sub _filter_columns ($$;$) { } =head2 _filter_fields -=over 4 - -_filter_fields - -=back + _filter_fields Given - a tablename @@ -305,13 +349,21 @@ sub _filter_fields{ my @keys; my @values; if (ref($filter_input) eq "HASH"){ - my ($keys, $values) = _filter_hash($tablename,$filter_input, $searchtype); + my ($keys, $values); + if (my $special = delete $filter_input->{''}) { # XXX destroyes '' key + ($keys, $values) = _filter_fields($tablename,$special, $searchtype,$filtercolumns); + } + my ($hkeys, $hvalues) = _filter_hash($tablename,$filter_input, $searchtype); + if ($hkeys){ + push @$keys, @$hkeys; + push @$values, @$hvalues; + } if ($keys){ - my $stringkey="(".join (") AND (",@$keys).")"; - return [$stringkey],$values; + my $stringkey="(".join (") AND (",@$keys).")"; + return [$stringkey],$values; } else { - return (); + return (); } } elsif (ref($filter_input) eq "ARRAY"){ foreach my $element_data (@$filter_input){ @@ -328,7 +380,15 @@ sub _filter_fields{ } } else{ - return _filter_string($tablename,$filter_input,$searchtype,$filtercolumns); + $debug && warn "filterstring : $filter_input"; + my ($keys, $values) = _filter_string($tablename,$filter_input, $searchtype,$filtercolumns); + if ($keys){ + my $stringkey="(".join (") AND (",@$keys).")"; + return [$stringkey],$values; + } + else { + return (); + } } return (\@keys,\@values); @@ -344,8 +404,10 @@ 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; @@ -362,12 +424,16 @@ sub _filter_hash{ sub _filter_string{ my ($tablename,$filter_input, $searchtype,$filtercolumns)=@_; return () unless($filter_input); - my @operands=split / /,$filter_input; + my @operands=split /\s+/,$filter_input; + + # An act of desperation + $searchtype = 'contain' if @operands > 1 && $searchtype =~ /start_with/o; + my @columns_filtered= _filter_columns($tablename,$searchtype,$filtercolumns); my $columns= _get_columns($tablename); my (@values,@keys); - my @localkeys; foreach my $operand (@operands){ + my @localkeys; foreach my $field (@columns_filtered){ my ($tmpkeys, $localvalues)=_Process_Operands($operand,"$tablename.$field",$searchtype,$columns); if ($tmpkeys){ @@ -375,9 +441,9 @@ sub _filter_string{ push @localkeys,@$tmpkeys; } } + my $sql= join (' OR ', @localkeys); + push @keys, $sql; } - my $sql= join (' OR ', @localkeys); - push @keys, $sql; if (@keys){ return (\@keys,\@values); @@ -391,21 +457,41 @@ sub _Process_Operands{ my @values; my @tmpkeys; my @localkeys; - push @tmpkeys, " $field = ? "; - push @values, $operand; - unless ($searchtype){ + + $operand = [$operand] unless ref $operand eq 'ARRAY'; + foreach (@$operand) { + push @tmpkeys, " $field = ? "; + push @values, $_; + } + #By default, exact search + if (!$searchtype ||$searchtype eq "exact"){ return \@tmpkeys,\@values; } - if ($searchtype eq "start_with"){ - my $col_field=(index($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"){ + foreach (@$operand) { + push @tmpkeys,(" $field LIKE ? "); + push @localvaluesextended,("\%$_\%") ; + } + } + if ($searchtype eq "field_start_with"){ + foreach (@$operand) { + push @tmpkeys,("$field LIKE ?"); + push @localvaluesextended, ("$_\%") ; + } + } + if ($searchtype eq "start_with"){ + foreach (@$operand) { + push @tmpkeys,("$field LIKE ?","$field LIKE ?"); + push @localvaluesextended, ("$_\%", " $_\%") ; + } + } + push @values,@localvaluesextended; } push @localkeys,qq{ (}.join(" OR ",@tmpkeys).qq{) }; return (\@localkeys,\@values);