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 # set the version for version checking
42 %EXPORT_TAGS = ( all
=>[qw( InsertInTable DeleteInTable SearchInTable UpdateInTable GetPrimaryKeys)]
51 C4::SQLHelper - Perl Module containing convenience functions for SQL Handling
59 This module contains routines for adding, modifying and Searching Data in MysqlDB
65 $hashref = &SearchInTable($tablename,$data, $orderby, $limit,
66 $columns_out, $filtercolumns, $searchtype);
69 $tablename Name of the table (string)
74 - data_hashref : will be considered as an AND of all the data searched
76 - data_array_ref on hashrefs : Will be considered as an OR of Datahasref elements
78 $orderby is an arrayref of hashref with fieldnames as key and 0 or 1 as values (ASCENDING or DESCENDING order)
80 $limit is an array ref on 2 values in order to limit results to MIN..MAX
82 $columns_out is an array ref on field names is used to limit results on those fields (* by default)
84 $filtercolums is an array ref on field names : is used to limit expansion of research for strings
86 $searchtype is string Can be "start_with" or "exact"
91 my ($tablename,$filters,$orderby, $limit, $columns_out, $filter_columns,$searchtype) = @_;
92 $searchtype||="exact";
93 my $dbh = C4
::Context
->dbh;
95 my $sql = do { local $"=', ';
96 qq{ SELECT @$columns_out from $tablename}
100 my ($keys,$values)=_filter_fields($tablename,$filters,$searchtype,$filter_columns);
102 my @criteria=grep{defined($_) && $_ !~/^\W$/ }@$keys;
104 $sql.= do { local $"=') AND (';
105 qq{ WHERE
(@criteria) }
110 #Order by desc by default
112 foreach my $order (@
$orderby){
113 push @orders,map{ "$_".($order->{$_}?
" DESC " : "") } keys %$order;
115 $sql.= do { local $"=', ';
116 qq{ ORDER BY @orders}
120 $sql.=qq{ LIMIT }.join(",",@$limit);
123 $debug && $values && warn $sql," ",join(",",@$values);
124 $sth = $dbh->prepare_cached($sql);
125 eval{$sth->execute(@$values)};
126 warn $@ if ($@ && $debug);
127 my $results = $sth->fetchall_arrayref( {} );
133 $data_id_in_table = &InsertInTable($tablename,$data_hashref,$withprimarykeys);
135 Insert Data in table and returns the id of the row inserted
140 my ($tablename,$data,$withprimarykeys) = @_;
141 my $dbh = C4::Context->dbh;
142 my ($keys,$values)=_filter_hash($tablename,$data,($withprimarykeys?"exact
":0));
143 my $query = qq{ INSERT INTO $tablename SET }.join(", ",@$keys);
145 $debug && warn $query, join(",",@$values);
146 my $sth = $dbh->prepare_cached($query);
147 eval{$sth->execute(@$values)};
148 warn $@ if ($@ && $debug);
150 return $dbh->last_insert_id(undef, undef, $tablename, undef);
155 $status = &UpdateInTable($tablename,$data_hashref);
157 Update Data in table and returns the status of the operation
162 my ($tablename,$data) = @_;
163 my @field_ids=GetPrimaryKeys($tablename);
164 my @ids=@$data{@field_ids};
165 my $dbh = C4::Context->dbh;
166 my ($keys,$values)=_filter_hash($tablename,$data,0);
167 return unless ($keys);
169 qq{ UPDATE $tablename
170 SET }.join(",",@$keys).qq{
171 WHERE }.join (" AND
",map{ "$_=?
" }@field_ids);
172 $debug && warn $query, join(",",@$values,@ids);
174 my $sth = $dbh->prepare_cached($query);
176 eval{$result=$sth->execute(@$values,@ids)};
177 warn $@ if ($@ && $debug);
183 $status = &DeleteInTable($tablename,$data_hashref);
185 Delete Data in table and returns the status of the operation
190 my ($tablename,$data) = @_;
191 my $dbh = C4::Context->dbh;
192 my ($keys,$values)=_filter_fields($tablename,$data,1);
194 my $query = do { local $"=') AND (';
195 qq{ DELETE FROM
$tablename WHERE
(@
$keys)};
197 $debug && warn $query, join(",",@
$values);
198 my $sth = $dbh->prepare_cached($query);
200 eval{$result=$sth->execute(@
$values)};
201 warn $@
if ($@
&& $debug);
206 =head2 GetPrimaryKeys
208 @primarykeys = &GetPrimaryKeys($tablename)
210 Get the Primary Key field names of the table
214 sub GetPrimaryKeys
($) {
216 my $hash_columns=_get_columns
($tablename);
217 return grep { $hash_columns->{$_}->{'Key'} =~/PRI/i} keys %$hash_columns;
222 _get_columns($tablename)
225 Returns a hashref of all the fieldnames of the table
233 sub _get_columns
($) {
235 my $dbh=C4
::Context
->dbh;
236 my $sth=$dbh->prepare_cached(qq{SHOW COLUMNS FROM
$tablename });
238 my $columns= $sth->fetchall_hashref(qw(Field));
241 =head2 _filter_columns
243 _filter_columns($tablename,$research, $filtercolumns)
247 - indicator on purpose whether all fields should be returned or only non Primary keys
248 - array_ref to columns to limit to
250 Returns an array of all the fieldnames of the table
251 If it is not for research purpose, filter primary keys
255 sub _filter_columns
($$;$) {
256 my ($tablename,$research, $filtercolumns)=@_;
258 return (@
$filtercolumns);
261 my $columns=_get_columns
($tablename);
263 return keys %$columns;
266 return grep {my $column=$_; any
{$_ ne $column }GetPrimaryKeys
($tablename) } keys %$columns;
270 =head2 _filter_fields
276 - a string or a hashref (containing, fieldnames and datatofilter) or an arrayref to one of those elements
277 - an indicator of operation whether it is a wide research or a narrow one
278 - an array ref to columns to restrict string filter to.
280 Returns a ref of key array to use in SQL functions
281 and a ref to value array
286 my ($tablename,$filter_input,$searchtype,$filtercolumns)=@_;
289 if (ref($filter_input) eq "HASH"){
290 my ($keys, $values) = _filter_hash
($tablename,$filter_input, $searchtype);
292 my $stringkey="(".join (") AND (",@
$keys).")";
293 return [$stringkey],$values;
298 } elsif (ref($filter_input) eq "ARRAY"){
299 foreach my $element_data (@
$filter_input){
300 my ($localkeys,$localvalues)=_filter_fields
($tablename,$element_data,$searchtype,$filtercolumns);
302 @
$localkeys=grep{defined($_) && $_ !~/^\W*$/}@
$localkeys;
308 push @values, @$localvalues;
313 $debug && warn "filterstring
: $filter_input";
314 my ($keys, $values) = _filter_string($tablename,$filter_input, $searchtype,$filtercolumns);
316 my $stringkey="(".join (") AND
(",@$keys).")";
317 return [$stringkey],$values;
324 return (\@keys,\@values);
328 my ($tablename,$filter_input, $searchtype)=@_;
330 my $columns= _get_columns($tablename);
331 my @columns_filtered= _filter_columns($tablename,$searchtype);
333 #Filter Primary Keys of table
334 my $elements=join "|",@columns_filtered;
335 foreach my $field (grep {/\b($elements)\b/} keys %$filter_input){
336 ## supposed to be a hash of simple values, hashes of arrays could be implemented
337 $filter_input->{$field}=format_date_in_iso($filter_input->{$field}) if ($columns->{$field}{Type}=~/date/ && $filter_input->{$field} !~C4::Dates->regexp("iso
"));
338 my ($tmpkeys, $localvalues)=_Process_Operands($filter_input->{$field},"$tablename.$field",$searchtype,$columns);
340 push @values, @$localvalues;
341 push @keys, @$tmpkeys;
345 return (\@keys,\@values);
353 my ($tablename,$filter_input, $searchtype,$filtercolumns)=@_;
354 return () unless($filter_input);
355 my @operands=split / /,$filter_input;
356 my @columns_filtered= _filter_columns($tablename,$searchtype,$filtercolumns);
357 my $columns= _get_columns($tablename);
359 foreach my $operand (@operands){
361 foreach my $field (@columns_filtered){
362 my ($tmpkeys, $localvalues)=_Process_Operands($operand,"$tablename.$field",$searchtype,$columns);
364 push @values,@$localvalues;
365 push @localkeys,@$tmpkeys;
368 my $sql= join (' OR ', @localkeys);
373 return (\@keys,\@values);
379 sub _Process_Operands{
380 my ($operand, $field, $searchtype,$columns)=@_;
384 push @tmpkeys, " $field = ?
";
385 push @values, $operand;
386 #By default, exact search
387 if (!$searchtype ||$searchtype eq "exact
"){
388 return \@tmpkeys,\@values;
390 my $col_field=(index($field,".")>0?substr($field, index($field,".")+1):$field);
391 if ($field=~/(?<!zip)code|(?<!card)number/ && $searchtype ne "exact
"){
392 push @tmpkeys,(" $field= '' ","$field IS NULL
");
394 if ($columns->{$col_field}->{Type}=~/varchar|text/i){
395 my @localvaluesextended;
396 if ($searchtype eq "contain
"){
397 push @tmpkeys,(" $field LIKE ?
");
398 push @localvaluesextended,("\
%$operand\
%") ;
400 if ($searchtype eq "field_start_with
"){
401 push @tmpkeys,("$field LIKE ?
");
402 push @localvaluesextended, ("$operand\
%") ;
404 if ($searchtype eq "start_with
"){
405 push @tmpkeys,("$field LIKE ?
","$field LIKE ?
");
406 push @localvaluesextended, ("$operand\
%", " $operand\
%") ;
408 push @values,@localvaluesextended;
410 push @localkeys,qq{ (}.join(" OR
",@tmpkeys).qq{) };
411 return (\@localkeys,\@values);