Skip Menu |

This queue is for tickets about the DBD-Mock CPAN distribution.

Report information
The Basics
Id: 33524
Status: resolved
Priority: 0/
Queue: DBD-Mock

People
Owner: Nobody in particular
Requestors: DROLSKY [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 1.37



Subject: Implementation of bind_col()
I've attached a patch to implement bind_col() for DBD::Mock. I didn't change any docs because the patch just makes it act like DBI, it doesn't do anything special, and I can't see any point in analyzing what columns were bound (there's really nothing to see except references).
Subject: dbd-mock.patch
diff -Nru ../DBD-Mock-1.36/lib/DBD/Mock.pm ./lib/DBD/Mock.pm --- ../DBD-Mock-1.36/lib/DBD/Mock.pm 2007-10-19 21:47:10.000000000 -0500 +++ ./lib/DBD/Mock.pm 2008-02-22 18:09:57.000000000 -0600 @@ -568,6 +568,14 @@ $DBD::Mock::st::imp_data_size = 0; +sub bind_col { + my ($sth, $param_num, $ref, $attr) = @_; + + my $tracker = $sth->FETCH( 'mock_my_history' ); + $tracker->bind_col( $param_num, $ref ); + return 1; +} + sub bind_param { my ($sth, $param_num, $val, $attr) = @_; my $tracker = $sth->FETCH( 'mock_my_history' ); @@ -668,7 +676,16 @@ $dbh->{mock_can_fetch}++ if $dbh->{mock_can_fetch} < 0; my $tracker = $sth->FETCH( 'mock_my_history' ); - return $tracker->next_record; + + my $record = $tracker->next_record; + + if ( my @cols = $tracker->bind_cols() ) { + for my $i ( grep { ref $cols[$_] } 0..$#cols ) { + ${ $cols[$i] } = $record->[$i]; + } + } + + return $record; } sub fetchrow_array { @@ -983,6 +1000,11 @@ return scalar @{$self->{bound_params}}; } +sub bind_col { + my ($self, $param_num, $ref) = @_; + $self->{bind_cols}->[$param_num - 1] = $ref; +} + sub bound_param { my ($self, $param_num, $value) = @_; $self->{bound_params}->[$param_num - 1] = $value; @@ -994,6 +1016,11 @@ push @{$self->{bound_params}}, @values; } +sub bind_cols { + my $self = shift; + return @{$self->{bind_cols}}; +} + sub bind_params { my ($self, @values) = @_; @{$self->{bound_params}} = @values; diff -Nru ../DBD-Mock-1.36/t/026_st_bind_col.t ./t/026_st_bind_col.t --- ../DBD-Mock-1.36/t/026_st_bind_col.t 1969-12-31 18:00:00.000000000 -0600 +++ ./t/026_st_bind_col.t 2008-02-22 18:10:12.000000000 -0600 @@ -0,0 +1,47 @@ +use 5.006; + +use strict; +use warnings; + +use Test::More tests => 10; + +BEGIN { + use_ok('DBD::Mock'); + use_ok('DBI'); +} + +my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); + +$dbh->{mock_add_resultset} = [ + [ 'id', 'type', 'inventory_id' ], + [ '1', 'european', '42' ], + [ '27', 'african', '2' ], + ]; + +my $sth = $dbh->prepare( 'SELECT id, type, inventory_id FROM Swallow' ); + +$sth->execute(); + +{ + my ($id, $type, $inventory_id); + + $sth->bind_col( 1, \$id ); + $sth->bind_col( 2, \$type ); + $sth->bind_col( 3, \$inventory_id ); + + ok( $sth->fetch(), 'fetch() returned data' ); + is( $id, 1, 'bind_col to $id == 1' ); + is( $type, 'european', 'bind_col to $type == "european"' ); + is( $inventory_id, 42, 'bind_col to $inventory_id == 42' ); +} + +{ + my %hash; + + $sth->bind_columns( \( @hash{ qw( id type inventory_id ) } ) ); + + ok( $sth->fetch(), 'fetch() returned data' ); + is( $hash{id}, 27, 'bind_columns with hash, id == 1' ); + is( $hash{type}, 'african', 'bind_columns with hash, type == "african"' ); + is( $hash{inventory_id}, 2, 'bind_columns with hash, inventory_id == 2' ); +}