SQLHelper : InsertInTable enhancement
authorHenri-Damien LAURENT <henridamien.laurent@biblibre.com>
Thu, 14 Jan 2010 17:51:28 +0000 (18:51 +0100)
committerHenri-Damien LAURENT <henridamien.laurent@biblibre.com>
Thu, 28 Jan 2010 14:11:54 +0000 (15:11 +0100)
InsertInTable can now add values to table where primaryKey requires
user input such as branches table

C4/SQLHelper.pm
t/db_dependent/SQLHelper.t

index 0b0c520..08ba90f 100644 (file)
@@ -140,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
 
@@ -149,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);
@@ -418,7 +418,7 @@ sub _Process_Operands{
        push @tmpkeys, " $field = ? ";
        push @values, $operand;
        #By default, exact search
-       unless ($searchtype){
+       if (!$searchtype ||$searchtype eq "exact"){
                return \@tmpkeys,\@values;
        }
        my $col_field=(index($field,".")>0?substr($field, index($field,".")+1):$field);
index 3c2fd48..c620ebb 100755 (executable)
@@ -10,7 +10,7 @@ use YAML;
 use C4::Debug;
 use C4::SQLHelper qw(:all);
 
-use Test::More tests => 18;
+use Test::More tests => 19;
 
 #1
 BEGIN {
@@ -19,6 +19,8 @@ BEGIN {
 use C4::Category;
 use C4::Branch;
 my @categories=C4::Category->all;
+my $insert;
+ok(($insert=InsertInTable("branches",{branchcode=>"ZZZZ",branchname=>"Brancheinconnue",city=>" ",zipcode=>" "},1))==0,"AddBranch (Insert In Table with primary key defined)");
 my $branches=C4::Branch->GetBranches;
 my @branchcodes=keys %$branches;
 my ($borrid, $borrtmp);