From 2086615a35b8924b6b4b77f555a13d56507d3e26 Mon Sep 17 00:00:00 2001 From: Henri-Damien LAURENT Date: Thu, 14 Jan 2010 18:51:28 +0100 Subject: [PATCH] SQLHelper : InsertInTable enhancement InsertInTable can now add values to table where primaryKey requires user input such as branches table --- C4/SQLHelper.pm | 8 ++++---- t/db_dependent/SQLHelper.t | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/C4/SQLHelper.pm b/C4/SQLHelper.pm index 0b0c520304..08ba90f648 100644 --- a/C4/SQLHelper.pm +++ b/C4/SQLHelper.pm @@ -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); diff --git a/t/db_dependent/SQLHelper.t b/t/db_dependent/SQLHelper.t index 3c2fd48a9a..c620ebb5d1 100755 --- a/t/db_dependent/SQLHelper.t +++ b/t/db_dependent/SQLHelper.t @@ -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); -- 2.39.5