summaryrefslogtreecommitdiff
path: root/t/11fetch.t
blob: 5f2fedcd6d09f5a690dbba44ff8ba0f5f9f687ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!perl -w
# vim:ts=8:sw=4
$|=1;

use strict;

use Test::More;
use DBI;
use Storable qw(dclone);
use Data::Dumper;

$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Quotekeys = 0;

plan tests => 24;

my $dbh = DBI->connect("dbi:Sponge:foo","","", {
        PrintError => 0,
        RaiseError => 1,
});

my $source_rows = [ # data for DBD::Sponge to return via fetch
    [ 41,	"AAA",	9	],
    [ 41,	"BBB",	9	],
    [ 42,	"BBB",	undef	],
    [ 43,	"ccc",	7	],
    [ 44,	"DDD",	6	],
];

sub go {
    my $source = shift || $source_rows;
    my $sth = $dbh->prepare("foo", {
	rows => dclone($source),
	NAME => [ qw(C1 C2 C3) ],
    });
    ok($sth->execute(), $DBI::errstr);
    return $sth;
}

my($sth, $col0, $col1, $col2, $rows);

# --- fetchrow_arrayref
# --- fetchrow_array
# etc etc

# --- fetchall_hashref
my @fetchall_hashref_results = (	# single keys
  C1 => {
    41  => { C1 => 41, C2 => 'BBB', C3 => 9 },
    42  => { C1 => 42, C2 => 'BBB', C3 => undef },
    43  => { C1 => 43, C2 => 'ccc', C3 => 7 },
    44  => { C1 => 44, C2 => 'DDD', C3 => 6 }
  },
  C2 => {
    AAA => { C1 => 41, C2 => 'AAA', C3 => 9 },
    BBB => { C1 => 42, C2 => 'BBB', C3 => undef },
    DDD => { C1 => 44, C2 => 'DDD', C3 => 6 },
    ccc => { C1 => 43, C2 => 'ccc', C3 => 7 }
  },
  [ 'C2' ] => {				# single key within arrayref
    AAA => { C1 => 41, C2 => 'AAA', C3 => 9 },
    BBB => { C1 => 42, C2 => 'BBB', C3 => undef },
    DDD => { C1 => 44, C2 => 'DDD', C3 => 6 },
    ccc => { C1 => 43, C2 => 'ccc', C3 => 7 }
  },
);
push @fetchall_hashref_results, (	# multiple keys
  [ 'C1', 'C2' ] => {
    '41' => {
      AAA => { C1 => '41', C2 => 'AAA', C3 => 9 },
      BBB => { C1 => '41', C2 => 'BBB', C3 => 9 }
    },
    '42' => {
      BBB => { C1 => '42', C2 => 'BBB', C3 => undef }
    },
    '43' => {
      ccc => { C1 => '43', C2 => 'ccc', C3 => 7 }
    },
    '44' => {
      DDD => { C1 => '44', C2 => 'DDD', C3 => 6 }
    }
  },
);

my %dump;

while (my $keyfield = shift @fetchall_hashref_results) {
    my $expected = shift @fetchall_hashref_results;
    my $k = (ref $keyfield) ? "[@$keyfield]" : $keyfield;
    print "# fetchall_hashref($k)\n";
    ok($sth = go());
    my $result = $sth->fetchall_hashref($keyfield);
    ok($result);
    is_deeply($result, $expected);
    # $dump{$k} = dclone $result; # just for adding tests
}

warn Dumper \%dump if %dump;

# test assignment to NUM_OF_FIELDS automatically alters the row buffer
$sth = go();
my $row = $sth->fetchrow_arrayref;
is scalar @$row, 3;
is $sth->{NUM_OF_FIELDS}, 3;
is scalar @{ $sth->_get_fbav }, 3;
$sth->{NUM_OF_FIELDS} = 4;
is $sth->{NUM_OF_FIELDS}, 4;
is scalar @{ $sth->_get_fbav }, 4;
$sth->{NUM_OF_FIELDS} = 2;
is $sth->{NUM_OF_FIELDS}, 2;
is scalar @{ $sth->_get_fbav }, 2;

$sth->finish;


if (0) {
    my @perf = map { [ int($_/100), $_, $_ ] } 0..10000;
    require Benchmark;
    Benchmark::timethis(10, sub { go(\@perf)->fetchall_hashref([ 'C1','C2','C3' ]) });
}


1; # end