Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

execute_array() #2

Merged
merged 7 commits into from Oct 10, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*\.sw?
6 changes: 6 additions & 0 deletions Build.PL
Expand Up @@ -20,6 +20,12 @@ my $build = Module::Build->new(
create_makefile_pl => 'traditional',
recursive_test_files => 1,
add_to_cleanup => [ '*.bak', ],

meta_merge => {
resources => {
repository => 'https://github.com/bluescreen10/dbd-mock',
},
},
);

$build->create_build_script;
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
Revision history for Perl extension DBD::Mock.

{{NEXT}}
- Add git-repo url to meta-data

1.43
- Segregated into different packages
- Removed code coverage from POD
Expand Down
36 changes: 36 additions & 0 deletions lib/DBD/Mock/st.pm
Expand Up @@ -20,6 +20,10 @@ sub bind_param {
return 1;
}

sub bind_param_array {
bind_param(@_);
}

sub bind_param_inout {
my ( $sth, $param_num, $val, $max_len ) = @_;

Expand All @@ -37,6 +41,38 @@ sub bind_param_inout {
return 1;
}

sub execute_array {
my ( $sth, $attr, @bind_values ) = @_;

# no bind values means we're relying on prior calls to bind_param_array()
# for our data
my $tracker = $sth->FETCH('mock_my_history');
# don't use a reference; there's some magic attached to it somewhere
# so make it a lovely, simple array as soon as possible
my @bound = @{ $tracker->bound_params() };
foreach my $p (@bound) {
my $result = $sth->execute( @$p );
# store the result from execute() if ArrayTupleStatus attribute is
# passed
push @{ $attr->{ArrayTupleStatus} }, $result
if (exists $attr->{ArrayTupleStatus});
}

# TODO: the docs say:
# When called in scalar context the execute_array() method returns the
# number of tuples executed, or undef if an error occurred. Like
# execute(), a successful execute_array() always returns true regardless
# of the number of tuples executed, even if it's zero. If there were any
# errors the ArrayTupleStatus array can be used to discover which tuples
# failed and with what errors.
# When called in list context the execute_array() method returns two
# scalars; $tuples is the same as calling execute_array() in scalar
# context and $rows is the number of rows affected for each tuple, if
# available or -1 if the driver cannot determine this.
# We have glossed over this...
return scalar @bound;
}

sub execute {
my ( $sth, @params ) = @_;
my $dbh = $sth->{Database};
Expand Down
34 changes: 34 additions & 0 deletions t/030_st_execute_array.t
@@ -0,0 +1,34 @@
use strict;
use warnings;

use Test::More;

# test style cribbed from t/013_st_execute_bound_params.t

BEGIN {
use_ok('DBD::Mock');
use_ok('DBI');
}

my $sql = 'INSERT INTO staff (first_name, last_name, dept) VALUES(?, ?, ?)';

{
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
my $sth = eval { $dbh->prepare( $sql ) };

# taken from: https://metacpan.org/module/DBI#Statement-Handle-Methods
$dbh->{RaiseError} = 1; # save having to check each method call
$sth = $dbh->prepare($sql);

$sth->bind_param_array(1, [ 'John', 'Mary', 'Tim' ]);
$sth->bind_param_array(2, [ 'Booth', 'Todd', 'Robinson' ]);
# TODO: $sth->bind_param_array(3, "SALES"); # scalar will be reused for each row

eval {
$sth->execute_array( { ArrayTupleStatus => \my @tuple_status } );
};
ok( ! $@, 'Called execute_array() ok' )
or diag $@;
}

done_testing;