Skip to content

Commit

Permalink
Merge pull request #2448 from RotherOSS/issue-#2447-do_array
Browse files Browse the repository at this point in the history
Issue #2447: add the method DoArray()
  • Loading branch information
bschmalhofer authored Aug 8, 2023
2 parents 8f7d0ba + 17945b1 commit 73017fe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
42 changes: 39 additions & 3 deletions Kernel/System/DB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,38 @@ sub Do {
return 1;
}

=head2 DoArray()
to insert, update or delete multiple values
my @Dogs = qw(Ferdinand Wastl Bello);
my @Owners = qw(Madleine Ferdinand Jaques);
$DBObject->DoArray(
SQL => "INSERT INTO dogs (name, owner) VALUES (?, ?)",
Bind => [ \@Dogs, \@Owners ],
);
=cut

sub DoArray {
my ( $Self, %Param ) = @_;

my ( $PrepareSuccess, @BindVariables ) = $Self->Prepare(
%Param,
DoArray => 1,
Execute => 0,
);

return unless $PrepareSuccess;

# the statement handle has been prepared in Prepare()
return $Self->{Cursor}->execute_array(
{ ArrayTupleStatus => \my @TupleStatus },
@BindVariables
);
}

sub _InitMirrorDB {
my ( $Self, %Param ) = @_;

Expand Down Expand Up @@ -684,6 +716,8 @@ will return
my ($Success, @BindVariables) = (1, 'dog1', 'dog2' );
Another internally used param is C<DoArray>. That parameter indicates that the array bind values are used.
=cut

sub Prepare {
Expand All @@ -694,6 +728,7 @@ sub Prepare {
my $Limit = $Param{Limit} || '';
my $Start = $Param{Start} || '';
my $Execute = $Param{Execute} // 1;
my $DoArray = $Param{DoArray} // 0;

# check needed stuff
if ( !$Param{SQL} ) {
Expand Down Expand Up @@ -770,15 +805,16 @@ sub Prepare {
# check bind params
my @Array;
if ( $Param{Bind} ) {
my $RefType = $DoArray ? 'ARRAY' : 'SCALAR';
for my $Data ( $Param{Bind}->@* ) {
if ( ref $Data eq 'SCALAR' ) {
push @Array, $Data->$*;
if ( ref $Data eq $RefType ) {
push @Array, $DoArray ? $Data : $Data->$*;
}
else {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Caller => 1,
Priority => 'Error',
Message => 'No SCALAR param in Bind!',
Message => qq{No $RefType parameter in Bind!},
);

return;
Expand Down
16 changes: 9 additions & 7 deletions scripts/test/DB/ConvenienceMethods.t
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ my @Countries = (
[ 'Germany', 'Deutschland', 'ජර්මනිය' ],
[ 'Sri Lanka', 'Sri Lanka', 'ශ්රී ලංකාව' ],
);
for my $Country (@Countries) {
my $Do = $DBObject->Do(
SQL => 'INSERT INTO test_countries (country_en, country_de, country_si) VALUES (?, ?, ?)',
Bind => [ \( $Country->@* ) ],
);
ok( $Do, "insert country $Country->[0]" );
}
my $DoArraySuccess = $DBObject->DoArray(
SQL => 'INSERT INTO test_countries (country_en, country_de, country_si) VALUES (?, ?, ?)',
Bind => [
[ map { $_->[0] } @Countries ],
[ map { $_->[1] } @Countries ],
[ map { $_->[2] } @Countries ],
],
);
ok( $DoArraySuccess, "insert countries" );

subtest 'SelectAll' => sub {

Expand Down

0 comments on commit 73017fe

Please sign in to comment.