Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

execute_array() #2

Merged
merged 7 commits into from over 1 year ago

4 participants

Chisel Wright Shlomi Fish Gines R Mariano Wahlmann
Chisel Wright

We've recently started using DBI's execute_array() at work.

Out testing used DBD::Mock which doesn't support execute_array().

This set of commits adds the failing test, then implements execute_array() in DBD::Mock.

The testing is a bit sparse, but should be easy enough to extend as and when other users of the module require it.

Behaviour is as we (currently) expect from our test suite with this addition.

Chisel Wright Add git-repo url to meta-data
This saves people hunting around github on the offchange that there's a
git-repo of the project. Now it's in the meta data and should be a lot easier
for people to find.
a6f8fa8
Chisel Wright Add failing test for execute_array()
This is something we've tried using, and hence mocking in tests at $work
recently.
This script fails with the same error we're seeing, so is a good starting
point for adding the functionality:

    $ prove -lv t/030_st_execute_array.t
    t/030_st_execute_array.t ..
    ok 1 - use DBD::Mock;
    ok 2 - use DBI;
    DBD::Mock::st execute_array failed: 3 bind values supplied but 0 expected at t/030_st_execute_array.t line 28.
    not ok 3 - Called execute_array() ok

    #   Failed test 'Called execute_array() ok'
    #   at t/030_st_execute_array.t line 30.
    1..3
    # Looks like you failed 1 test of 3.
    Dubious, test returned 1 (wstat 256, 0x100)
    Failed 1/3 subtests
c9d7bd7
Chisel Wright git: ignore vi* swap files 62e4f4a
Chisel Wright execute_array(): don't test single scalar value c51ed6e
Chisel Wright execute_array(): diag any errors in test 9c5a03a
Chisel Wright Add bind_param_array() method
This seems to behave the same as bind_param() in our mocked world, so we just
pass the call on.
09e5fb9
Chisel Wright Add execute_array() method to DBD::Mock::st
This is an early prototype that no longer dies when we call execute_array() on
a mocked $dbh - but the testing doesn't yet have very high coverage to prove
that it behaves as expected.
8729225
Gines R
Collaborator

@bluescreen10 i'm merging this change, looks good to me!

Gines R ginesr merged commit efc9492 into from October 09, 2012
Gines R ginesr closed this October 09, 2012
Mariano Wahlmann
Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 7 unique commits by 1 author.

Sep 27, 2012
Chisel Wright Add git-repo url to meta-data
This saves people hunting around github on the offchange that there's a
git-repo of the project. Now it's in the meta data and should be a lot easier
for people to find.
a6f8fa8
Chisel Wright Add failing test for execute_array()
This is something we've tried using, and hence mocking in tests at $work
recently.
This script fails with the same error we're seeing, so is a good starting
point for adding the functionality:

    $ prove -lv t/030_st_execute_array.t
    t/030_st_execute_array.t ..
    ok 1 - use DBD::Mock;
    ok 2 - use DBI;
    DBD::Mock::st execute_array failed: 3 bind values supplied but 0 expected at t/030_st_execute_array.t line 28.
    not ok 3 - Called execute_array() ok

    #   Failed test 'Called execute_array() ok'
    #   at t/030_st_execute_array.t line 30.
    1..3
    # Looks like you failed 1 test of 3.
    Dubious, test returned 1 (wstat 256, 0x100)
    Failed 1/3 subtests
c9d7bd7
Chisel Wright git: ignore vi* swap files 62e4f4a
Chisel Wright execute_array(): don't test single scalar value c51ed6e
Chisel Wright execute_array(): diag any errors in test 9c5a03a
Chisel Wright Add bind_param_array() method
This seems to behave the same as bind_param() in our mocked world, so we just
pass the call on.
09e5fb9
Chisel Wright Add execute_array() method to DBD::Mock::st
This is an early prototype that no longer dies when we call execute_array() on
a mocked $dbh - but the testing doesn't yet have very high coverage to prove
that it behaves as expected.
8729225
This page is out of date. Refresh to see the latest.
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+*\.sw?
6  Build.PL
@@ -20,6 +20,12 @@ my $build = Module::Build->new(
20 20
     create_makefile_pl   => 'traditional',
21 21
     recursive_test_files => 1,
22 22
     add_to_cleanup       => [ '*.bak', ],
  23
+
  24
+    meta_merge => {
  25
+        resources => {
  26
+            repository  => 'https://github.com/bluescreen10/dbd-mock',
  27
+        },
  28
+    },
23 29
 );
24 30
 
25 31
 $build->create_build_script;
3  Changes
... ...
@@ -1,5 +1,8 @@
1 1
 Revision history for Perl extension DBD::Mock.
2 2
 
  3
+{{NEXT}}
  4
+    - Add git-repo url to meta-data
  5
+
3 6
 1.43
4 7
     - Segregated into different packages
5 8
     - Removed code coverage from POD
36  lib/DBD/Mock/st.pm
@@ -20,6 +20,10 @@ sub bind_param {
20 20
     return 1;
21 21
 }
22 22
 
  23
+sub bind_param_array {
  24
+    bind_param(@_);
  25
+}
  26
+
23 27
 sub bind_param_inout {
24 28
     my ( $sth, $param_num, $val, $max_len ) = @_;
25 29
 
@@ -37,6 +41,38 @@ sub bind_param_inout {
37 41
     return 1;
38 42
 }
39 43
 
  44
+sub execute_array {
  45
+    my ( $sth, $attr, @bind_values ) = @_;
  46
+
  47
+    # no bind values means we're relying on prior calls to bind_param_array()
  48
+    # for our data
  49
+    my $tracker = $sth->FETCH('mock_my_history');
  50
+    # don't use a reference; there's some magic attached to it somewhere
  51
+    # so make it a lovely, simple array as soon as possible
  52
+    my @bound = @{ $tracker->bound_params() };
  53
+    foreach my $p (@bound) {
  54
+        my $result = $sth->execute( @$p );
  55
+        # store the result from execute() if ArrayTupleStatus attribute is
  56
+        # passed
  57
+        push @{ $attr->{ArrayTupleStatus} }, $result
  58
+            if (exists $attr->{ArrayTupleStatus});
  59
+    }
  60
+
  61
+    # TODO: the docs say:
  62
+    #   When called in scalar context the execute_array() method returns the
  63
+    #   number of tuples executed, or undef if an error occurred. Like
  64
+    #   execute(), a successful execute_array() always returns true regardless
  65
+    #   of the number of tuples executed, even if it's zero. If there were any
  66
+    #   errors the ArrayTupleStatus array can be used to discover which tuples
  67
+    #   failed and with what errors.
  68
+    #   When called in list context the execute_array() method returns two
  69
+    #   scalars; $tuples is the same as calling execute_array() in scalar
  70
+    #   context and $rows is the number of rows affected for each tuple, if
  71
+    #   available or -1 if the driver cannot determine this. 
  72
+    # We have glossed over this...
  73
+    return scalar @bound;
  74
+}
  75
+
40 76
 sub execute {
41 77
     my ( $sth, @params ) = @_;
42 78
     my $dbh = $sth->{Database};
34  t/030_st_execute_array.t
... ...
@@ -0,0 +1,34 @@
  1
+use strict;
  2
+use warnings;
  3
+
  4
+use Test::More;
  5
+
  6
+# test style cribbed from t/013_st_execute_bound_params.t
  7
+
  8
+BEGIN {
  9
+    use_ok('DBD::Mock');  
  10
+    use_ok('DBI');
  11
+}
  12
+
  13
+my $sql = 'INSERT INTO staff (first_name, last_name, dept) VALUES(?, ?, ?)';
  14
+
  15
+{
  16
+    my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
  17
+    my $sth = eval { $dbh->prepare( $sql ) };
  18
+
  19
+    # taken from: https://metacpan.org/module/DBI#Statement-Handle-Methods
  20
+    $dbh->{RaiseError} = 1;        # save having to check each method call
  21
+    $sth = $dbh->prepare($sql);
  22
+
  23
+    $sth->bind_param_array(1, [ 'John', 'Mary', 'Tim' ]);
  24
+    $sth->bind_param_array(2, [ 'Booth', 'Todd', 'Robinson' ]);
  25
+    # TODO: $sth->bind_param_array(3, "SALES"); # scalar will be reused for each row
  26
+
  27
+    eval {
  28
+        $sth->execute_array( { ArrayTupleStatus => \my @tuple_status } );
  29
+    };
  30
+    ok( ! $@, 'Called execute_array() ok' )
  31
+        or diag $@;
  32
+}
  33
+
  34
+done_testing;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.