Bug 7356 - Fix various typos and mis-spellings
[koha.git] / C4 / SQLHelper.pm
index 4db8c49..cf425fd 100644 (file)
@@ -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,7 +24,6 @@ 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);
 
@@ -44,9 +43,6 @@ BEGIN {
                                );
 }
 
-my $tablename;
-my $hash;
-
 =head1 NAME
 
 C4::SQLHelper - Perl Module containing convenience functions for SQL Handling
@@ -61,18 +57,11 @@ 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)
 
@@ -93,6 +82,21 @@ $filtercolums is an array ref on field names : is used to limit expansion of res
 
 $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{
@@ -109,7 +113,7 @@ sub SearchInTable{
        if ($keys){
                my @criteria=grep{defined($_) && $_ !~/^\W$/ }@$keys;
                if (@criteria) { 
-                       $sql.= do { local $"=') AND ('; 
+                       $sql.= do { local $"=') OR ('; 
                                        qq{ WHERE (@criteria) } 
                                   }; 
                } 
@@ -117,8 +121,12 @@ sub SearchInTable{
     if ($orderby){ 
                #Order by desc by default
                my @orders;
-               foreach my $order (@$orderby){
-                       push @orders,map{ "$_".($order->{$_}? " DESC " : "") } keys %$order; 
+               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} 
@@ -138,14 +146,10 @@ sub SearchInTable{
 
 =head2 InsertInTable
 
-=over 4
-
   $data_id_in_table = &InsertInTable($tablename,$data_hashref,$withprimarykeys);
 
-=back
+Insert Data in table and returns the id of the row inserted
 
-  Insert Data in table
-  and returns the id of the row inserted
 =cut
 
 sub InsertInTable{
@@ -164,14 +168,10 @@ sub InsertInTable{
 
 =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{
@@ -180,6 +180,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{
@@ -195,14 +196,10 @@ sub UpdateInTable{
 
 =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{
@@ -224,13 +221,10 @@ sub DeleteInTable{
 
 =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($) {
@@ -241,11 +235,7 @@ sub GetPrimaryKeys($) {
 
 =head2 _get_columns
 
-=over 4
-
-_get_columns($tablename)
-
-=back
+    _get_columns($tablename)
 
 Given a tablename 
 Returns a hashref of all the fieldnames of the table
@@ -266,11 +256,7 @@ sub _get_columns($) {
 
 =head2 _filter_columns
 
-=over 4
-
-_filter_columns($tablename,$research, $filtercolumns)
-
-=back
+    _filter_columns($tablename,$research, $filtercolumns)
 
 Given 
        - a tablename 
@@ -299,11 +285,7 @@ sub _filter_columns ($$;$) {
 }
 =head2 _filter_fields
 
-=over 4
-
-_filter_fields
-
-=back
+  _filter_fields
 
 Given 
        - a tablename
@@ -321,13 +303,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){
@@ -368,7 +358,9 @@ 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"));
+               $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;
@@ -386,7 +378,11 @@ 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);
@@ -415,8 +411,12 @@ sub _Process_Operands{
        my @values;
        my @tmpkeys;
        my @localkeys;
-       push @tmpkeys, " $field = ? ";
-       push @values, $operand;
+
+    $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;
@@ -428,16 +428,22 @@ sub _Process_Operands{
        if ($columns->{$col_field}->{Type}=~/varchar|text/i){
                my @localvaluesextended;
                if ($searchtype eq "contain"){
-                       push @tmpkeys,(" $field LIKE ? ");
-                       push @localvaluesextended,("\%$operand\%") ;
+            foreach (@$operand) {
+                           push @tmpkeys,(" $field LIKE ? ");
+                           push @localvaluesextended,("\%$_\%") ;
+            }
                }
                if ($searchtype eq "field_start_with"){
-                       push @tmpkeys,("$field LIKE ?");
-                       push @localvaluesextended, ("$operand\%") ;
+            foreach (@$operand) {
+                           push @tmpkeys,("$field LIKE ?");
+                           push @localvaluesextended, ("$_\%") ;
+            }
                }
                if ($searchtype eq "start_with"){
-                       push @tmpkeys,("$field LIKE ?","$field LIKE ?");
-                       push @localvaluesextended, ("$operand\%", " $operand\%") ;
+            foreach (@$operand) {
+                           push @tmpkeys,("$field LIKE ?","$field LIKE ?");
+                           push @localvaluesextended, ("$_\%", " $_\%") ;
+            }
                }
                push @values,@localvaluesextended;
        }