Debian Codegraph @

t/027_named_parameters.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9 use_ok('DBD::Mock');
10 }
11
12 my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
13
14 my $session = DBD::Mock::Session->new(
15 (
16 {
17 statement => 'SELECT * FROM foo WHERE id = ? and active = ?',
18 bound_params => [ '613', 'yes' ],
19 results => [ ['foo'], [10] ]
20 },
21 {
22 statement => 'SELECT * FROM foo WHERE id = ? and active = ?',
23 bound_params => [ '613', 'yes' ],
24 results => [ ['foo'], [10] ]
25 },
26 {
27 statement =>
28 'SELECT * FROM foo WHERE id = :id and active = :active',
29 bound_params => [ '101', 'no' ],
30 results => [ ['bar'], [15] ]
31 },
32 {
33 statement =>
34 'SELECT * FROM foo WHERE id = :id and active = :active',
35 bound_params => [ '101', 'no' ],
36 results => [ ['bar'], [15] ]
37 },
38
39 )
40 );
41
42 $dbh->{mock_session} = $session;
43
44 my $sth = $dbh->prepare('SELECT * FROM foo WHERE id = ? and active = ?');
45 $sth->bind_param( 1 => '613' );
46 $sth->bind_param( 2 => 'yes' );
47 ok( $sth->execute, 'Execute using positional parameters' );
48
49 $sth = $dbh->prepare('SELECT * FROM foo WHERE id = ? and active = ?');
50 ok( $sth->execute( '613', 'yes' ), 'Execute using positional parameters #2' );
51
52 $sth = $dbh->prepare('SELECT * FROM foo WHERE id = :id and active = :active');
53 $sth->bind_param( ':id' => '101' );
54 $sth->bind_param( ':active' => 'no' );
55 ok( $sth->execute, 'Execute using named parameters' );
56
57 $sth = $dbh->prepare('SELECT * FROM foo WHERE id = :id and active = :active');
58 ok( $sth->execute( '101', 'no' ), 'Execute using named parameters #2' );
59
60 done_testing();