3 # Copyright 2009 Biblibre SARL
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use List::MoreUtils qw(first_value any);
25 use C4::Dates qw(format_date_in_iso);
28 use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
31 my $servers = C4::Context->config('memcached_servers');
33 require Memoize::Memcached;
34 import Memoize::Memcached qw(memoize_memcached);
37 servers => [$servers],
38 key_prefix => C4::Context->config('memcached_namespace') || 'koha',
41 memoize_memcached( '_get_columns', memcached => $memcached, expire_time => 600000 ); #cache for 10 minutes
42 memoize_memcached( 'GetPrimaryKeys', memcached => $memcached, expire_time => 600000 ); #cache for 10 minutes
47 # set the version for version checking
58 %EXPORT_TAGS = ( all =>[qw( InsertInTable DeleteInTable SearchInTable UpdateInTable GetPrimaryKeys)]
67 C4::SQLHelper - Perl Module containing convenience functions for SQL Handling
75 This module contains routines for adding, modifying and Searching Data in MysqlDB
81 $hashref = &SearchInTable($tablename,$data, $orderby, $limit,
82 $columns_out, $filtercolumns, $searchtype);
85 $tablename Name of the table (string)
90 - data_hashref : will be considered as an AND of all the data searched
92 - data_array_ref on hashrefs : Will be considered as an OR of Datahasref elements
94 $orderby is an arrayref of hashref with fieldnames as key and 0 or 1 as values (ASCENDING or DESCENDING order)
96 $limit is an array ref on 2 values in order to limit results to MIN..MAX
98 $columns_out is an array ref on field names is used to limit results on those fields (* by default)
100 $filtercolums is an array ref on field names : is used to limit expansion of research for strings
102 $searchtype is string Can be "start_with" or "exact"
104 This query builder is very limited, it should be replaced with DBIx::Class
106 Meanwhile adding support for special key '' in case of a data_hashref to
107 support filters of type
109 ( f1 = a OR f2 = a ) AND fx = b AND fy = c
111 Call for the query above is:
113 SearchInTable($tablename, {'' => a, fx => b, fy => c}, $orderby, $limit,
114 $columns_out, [f1, f2], 'exact');
116 NOTE: Current implementation may remove parts of the iinput hashrefs. If that is a problem
117 a copy needs to be created in _filter_fields() below
122 my ($tablename,$filters,$orderby, $limit, $columns_out, $filter_columns,$searchtype) = @_;
123 $searchtype||="exact";
124 my $dbh = C4::Context->dbh;
125 $columns_out||=["*"];
126 my $sql = do { local $"=', ';
127 qq{ SELECT @$columns_out from $tablename}
131 my ($keys,$values)=_filter_fields($tablename,$filters,$searchtype,$filter_columns);
133 my @criteria=grep{defined($_) && $_ !~/^\W$/ }@$keys;
135 $sql.= do { local $"=') OR (';
136 qq{ WHERE (@criteria) }
141 #Order by desc by default
143 foreach my $order ( ref($orderby) ? @$orderby : $orderby ){
145 push @orders,map{ "$_".($order->{$_}? " DESC " : "") } keys %$order;
150 $sql.= do { local $"=', ';
151 qq{ ORDER BY @orders}
155 $sql.=qq{ LIMIT }.join(",",@$limit);
158 $debug && $values && warn $sql," ",join(",",@$values);
159 $sth = $dbh->prepare_cached($sql);
160 eval{$sth->execute(@$values)};
161 warn $@ if ($@ && $debug);
162 my $results = $sth->fetchall_arrayref( {} );
168 $data_id_in_table = &InsertInTable($tablename,$data_hashref,$withprimarykeys);
170 Insert Data in table and returns the id of the row inserted
175 my ($tablename,$data,$withprimarykeys) = @_;
176 my $dbh = C4::Context->dbh;
177 my ($keys,$values)=_filter_hash($tablename,$data,($withprimarykeys?"exact":0));
178 my $query = qq{ INSERT INTO $tablename SET }.join(", ",@$keys);
180 $debug && warn $query, join(",",@$values);
181 my $sth = $dbh->prepare_cached($query);
182 eval{$sth->execute(@$values)};
183 warn $@ if ($@ && $debug);
185 return $dbh->last_insert_id(undef, undef, $tablename, undef);
190 $status = &UpdateInTable($tablename,$data_hashref);
192 Update Data in table and returns the status of the operation
197 my ($tablename,$data) = @_;
198 my @field_ids=GetPrimaryKeys($tablename);
199 my @ids=@$data{@field_ids};
200 my $dbh = C4::Context->dbh;
201 my ($keys,$values)=_filter_hash($tablename,$data,0);
202 return unless ($keys);
204 qq{ UPDATE $tablename
205 SET }.join(",",@$keys).qq{
206 WHERE }.join (" AND ",map{ "$_=?" }@field_ids);
207 $debug && warn $query, join(",",@$values,@ids);
209 my $sth = $dbh->prepare_cached($query);
211 eval{$result=$sth->execute(@$values,@ids)};
212 warn $@ if ($@ && $debug);
218 $status = &DeleteInTable($tablename,$data_hashref);
220 Delete Data in table and returns the status of the operation
225 my ($tablename,$data) = @_;
226 my $dbh = C4::Context->dbh;
227 my ($keys,$values)=_filter_fields($tablename,$data,1);
229 my $query = do { local $"=') AND (';
230 qq{ DELETE FROM $tablename WHERE (@$keys)};
232 $debug && warn $query, join(",",@$values);
233 my $sth = $dbh->prepare_cached($query);
235 eval{$result=$sth->execute(@$values)};
236 warn $@ if ($@ && $debug);
241 =head2 GetPrimaryKeys
243 @primarykeys = &GetPrimaryKeys($tablename)
245 Get the Primary Key field names of the table
249 sub GetPrimaryKeys($) {
251 my $hash_columns=_get_columns($tablename);
252 return grep { $hash_columns->{$_}->{'Key'} =~/PRI/i} keys %$hash_columns;
257 _get_columns($tablename)
260 Returns a hashref of all the fieldnames of the table
268 sub _get_columns($) {
269 my ($tablename) = @_;
270 unless ( exists( $hashref->{$tablename} ) ) {
271 my $dbh = C4::Context->dbh;
272 my $sth = $dbh->prepare_cached(qq{SHOW COLUMNS FROM $tablename });
274 my $columns = $sth->fetchall_hashref(qw(Field));
275 $hashref->{$tablename} = $columns;
277 return $hashref->{$tablename};
280 =head2 _filter_columns
284 _filter_columns($tablename,$research, $filtercolumns)
290 - indicator on purpose whether all fields should be returned or only non Primary keys
291 - array_ref to columns to limit to
293 Returns an array of all the fieldnames of the table
294 If it is not for research purpose, filter primary keys
298 sub _filter_columns ($$;$) {
299 my ($tablename,$research, $filtercolumns)=@_;
301 return (@$filtercolumns);
304 my $columns=_get_columns($tablename);
306 return keys %$columns;
309 return grep {my $column=$_; any {$_ ne $column }GetPrimaryKeys($tablename) } keys %$columns;
313 =head2 _filter_fields
319 - a string or a hashref (containing, fieldnames and datatofilter) or an arrayref to one of those elements
320 - an indicator of operation whether it is a wide research or a narrow one
321 - an array ref to columns to restrict string filter to.
323 Returns a ref of key array to use in SQL functions
324 and a ref to value array
329 my ($tablename,$filter_input,$searchtype,$filtercolumns)=@_;
332 if (ref($filter_input) eq "HASH"){
334 if (my $special = delete $filter_input->{''}) { # XXX destroyes '' key
335 ($keys, $values) = _filter_fields($tablename,$special, $searchtype,$filtercolumns);
337 my ($hkeys, $hvalues) = _filter_hash($tablename,$filter_input, $searchtype);
339 push @$keys, @$hkeys;
340 push @$values, @$hvalues;
343 my $stringkey="(".join (") AND (",@$keys).")";
344 return [$stringkey],$values;
349 } elsif (ref($filter_input) eq "ARRAY"){
350 foreach my $element_data (@$filter_input){
351 my ($localkeys,$localvalues)=_filter_fields($tablename,$element_data,$searchtype,$filtercolumns);
353 @$localkeys=grep{defined($_) && $_ !~/^\W*$/}@$localkeys;
359 push @values, @$localvalues;
364 $debug && warn "filterstring : $filter_input";
365 my ($keys, $values) = _filter_string($tablename,$filter_input, $searchtype,$filtercolumns);
367 my $stringkey="(".join (") AND (",@$keys).")";
368 return [$stringkey],$values;
375 return (\@keys,\@values);
379 my ($tablename,$filter_input, $searchtype)=@_;
381 my $columns= _get_columns($tablename);
382 my @columns_filtered= _filter_columns($tablename,$searchtype);
384 #Filter Primary Keys of table
385 my $elements=join "|",@columns_filtered;
386 foreach my $field (grep {/\b($elements)\b/} keys %$filter_input){
387 ## supposed to be a hash of simple values, hashes of arrays could be implemented
388 $filter_input->{$field}=format_date_in_iso($filter_input->{$field})
389 if $columns->{$field}{Type}=~/date/ &&
390 $filter_input->{$field} !~C4::Dates->regexp("iso");
391 my ($tmpkeys, $localvalues)=_Process_Operands($filter_input->{$field},"$tablename.$field",$searchtype,$columns);
393 push @values, @$localvalues;
394 push @keys, @$tmpkeys;
398 return (\@keys,\@values);
406 my ($tablename,$filter_input, $searchtype,$filtercolumns)=@_;
407 return () unless($filter_input);
408 my @operands=split /\s+/,$filter_input;
410 # An act of desperation
411 $searchtype = 'contain' if @operands > 1 && $searchtype =~ /start_with/o;
413 my @columns_filtered= _filter_columns($tablename,$searchtype,$filtercolumns);
414 my $columns= _get_columns($tablename);
416 foreach my $operand (@operands){
418 foreach my $field (@columns_filtered){
419 my ($tmpkeys, $localvalues)=_Process_Operands($operand,"$tablename.$field",$searchtype,$columns);
421 push @values,@$localvalues;
422 push @localkeys,@$tmpkeys;
425 my $sql= join (' OR ', @localkeys);
430 return (\@keys,\@values);
436 sub _Process_Operands{
437 my ($operand, $field, $searchtype,$columns)=@_;
442 $operand = [$operand] unless ref $operand eq 'ARRAY';
443 foreach (@$operand) {
444 push @tmpkeys, " $field = ? ";
447 #By default, exact search
448 if (!$searchtype ||$searchtype eq "exact"){
449 return \@tmpkeys,\@values;
451 my $col_field=(index($field,".")>0?substr($field, index($field,".")+1):$field);
452 if ($field=~/(?<!zip)code|(?<!card)number/ && $searchtype ne "exact"){
453 push @tmpkeys,(" $field= '' ","$field IS NULL");
455 if ($columns->{$col_field}->{Type}=~/varchar|text/i){
456 my @localvaluesextended;
457 if ($searchtype eq "contain"){
458 foreach (@$operand) {
459 push @tmpkeys,(" $field LIKE ? ");
460 push @localvaluesextended,("\%$_\%") ;
463 if ($searchtype eq "field_start_with"){
464 foreach (@$operand) {
465 push @tmpkeys,("$field LIKE ?");
466 push @localvaluesextended, ("$_\%") ;
469 if ($searchtype eq "start_with"){
470 foreach (@$operand) {
471 push @tmpkeys,("$field LIKE ?","$field LIKE ?");
472 push @localvaluesextended, ("$_\%", " $_\%") ;
475 push @values,@localvaluesextended;
477 push @localkeys,qq{ (}.join(" OR ",@tmpkeys).qq{) };
478 return (\@localkeys,\@values);