Skip to content

Commit

Permalink
Merge pull request #2457 from RotherOSS/issue-#2447-array_tuple_fetch
Browse files Browse the repository at this point in the history
Issue #2447 array tuple fetch
  • Loading branch information
bschmalhofer authored Aug 11, 2023
2 parents e1050b8 + d71e2ea commit be4b4a2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
28 changes: 25 additions & 3 deletions Kernel/System/DB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -572,16 +572,37 @@ sub Do {

=head2 DoArray()
to insert, update or delete multiple values
to insert, update or delete multiple values. There are two variants. You can bind the column arrays.
For convenience, when a plain scalar is bound, then it is used for all rows.
my @Dogs = qw(Ferdinand Wastl Bello);
my @Owners = qw(Madleine Ferdinand Jaques);
$DBObject->DoArray(
my $NumInserts = $DBObject->DoArray(
SQL => "INSERT INTO dogs (name, relation, owner) VALUES (?, ?, ?)",
Bind => [ \@Dogs, 'is owned by', \@Owners ],
);
A subroutine that generates the rows can be passed.
my $FetchTuple = sub {
state $Index = -1;
my @Dogs = (
[ 'Maxl', 'loves', 'Michaela' ],
[ 'Wussel', 'loves', 'Michaela' ],
);
$Index++;
return if $Index >= 2;
return $Dogs[$Index];
};
my $NumInserts = $DBObject->DoArray(
SQL => "INSERT INTO dogs (name, relation, owner) VALUES (?, ?, ?)",
ArrayTupleFetch => $FetchTuple,
);
=cut

sub DoArray {
Expand All @@ -603,7 +624,8 @@ sub DoArray {
$Attributes{ArrayTupleFetch} = $Param{ArrayTupleFetch};
}

return $Self->{Cursor}->execute_array(
# return the number of handled tuples
return scalar $Self->{Cursor}->execute_array(
\%Attributes,
@BindVariables
);
Expand Down
35 changes: 29 additions & 6 deletions scripts/test/DB/ConvenienceMethods.t
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ The tested methods are:
=cut

use v5.24;
use strict;
use warnings;
use utf8;
use utf8;

# core modules

Expand Down Expand Up @@ -73,14 +73,14 @@ ok( $DBObject->Do( SQL => $CreateTableSQL ), 'table created' );

# country translations, sorted by the English name
my @Countries = (
[ 'Colombia', 'Kolumbien', 'කොලොම්බියාව', 1 ],
[ 'Germany', 'Deutschland', 'ජර්මනිය', 1 ],
[ 'Sri Lanka', 'Sri Lanka', 'ශ්රී ලංකාව', 1 ],
[ 'Austria', 'Österreich', 'ඔස්ට්රියාව', 1 ],
[ 'Colombia', 'Kolumbien', 'කොලොම්බියාව', 1 ],
[ 'Germany', 'Deutschland', 'ජර්මනිය', 1 ],
);

# Insert the values.
# is_country is inserted as a scalar, will be set for all rows
my $DoArraySuccess = $DBObject->DoArray(
my $NumInserted = $DBObject->DoArray(
SQL => 'INSERT INTO test_countries (country_en, country_de, country_si, is_country) VALUES (?, ?, ?, ?)',
Bind => [
[ map { $_->[0] } @Countries ],
Expand All @@ -89,7 +89,30 @@ my $DoArraySuccess = $DBObject->DoArray(
1,
],
);
ok( $DoArraySuccess, "insert countries" );
is( $NumInserted, scalar(@Countries), 'insert countries with column bind' );

# Add more countries with ArrayTupleFetch.
my @MoreCountries = (
[ 'Malawi', 'Malawi', 'මලාවි', 1 ],
[ 'Sri Lanka', 'Sri Lanka', 'ශ්රී ලංකාව', 1 ],
);
push @Countries, @MoreCountries;

my $FetchMoreCountries = sub {
state $Index = -1;

$Index++;

return if $Index >= scalar @MoreCountries;
return $MoreCountries[$Index];
};

my $NumMoreInserted = $DBObject->DoArray(
SQL => 'INSERT INTO test_countries (country_en, country_de, country_si, is_country) VALUES (?, ?, ?, ?)',
ArrayTupleFetch => $FetchMoreCountries,
);

is( $NumMoreInserted, scalar(@MoreCountries), 'insert more countries with ArrayTupleFetch' );

subtest 'SelectAll' => sub {

Expand Down

0 comments on commit be4b4a2

Please sign in to comment.