blib/lib/CPANPLUS/Internals/Source/SQLite/Tie.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 134 CC 5 SCAR 16.5
CC
5
Cov
0%
FETCH
20.0
NEXTKEY
6.0
TIEHASH
2.0
CRAP 5.2
1 package CPANPLUS::Internals::Source::SQLite::Tie;
2  
3 use strict;
4 use warnings;
5  
6 use CPANPLUS::Error;
7 use CPANPLUS::Module;
8 use CPANPLUS::Module::Fake;
9 use CPANPLUS::Module::Author::Fake;
10 use CPANPLUS::Internals::Constants;
11  
12 use Params::Check qw[check];
13 use Module::Load::Conditional qw[can_load];
14 use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
15  
16 use vars qw[@ISA $VERSION];
17 $VERSION = "0.9914";
18  
19 require Tie::Hash;
20 push @ISA, 'Tie::StdHash';
21  
22  
23 sub TIEHASH {
24 my $class = shift;
25 my %hash = @_;
26  
27 my $tmpl = {
28 dbh => { required => 1 },
29 table => { required => 1 },
30 key => { required => 1 },
31 cb => { required => 1 },
32 offset => { default => 0 },
33 };
34  
35 my $args = check( $tmpl, \%hash ) or return;
36 my $obj = bless { %$args, store => {} } , $class;
37  
38 return $obj;
39 }
40  
41 sub FETCH {
42 my $self = shift;
43 my $key = shift or return;
44 my $dbh = $self->{dbh};
45 my $cb = $self->{cb};
46 my $table = $self->{table};
47  
48  
49 ### did we look this one up before?
50 if( my $obj = $self->{store}->{$key} ) {
51 return $obj;
52 }
53  
54 my $res = $dbh->query(
55 "SELECT * from $table where $self->{key} = ?", $key
56 ) or do {
57 error( $dbh->error );
58 return;
59 };
60  
61 my $href = $res->hash;
62  
63 ### get rid of the primary key
64 delete $href->{'id'};
65  
66 ### no results?
67 return unless keys %$href;
68  
69 ### expand author if needed
70 ### XXX no longer generic :(
71 if( $table eq 'module' ) {
72 $href->{author} = $cb->author_tree( $href->{author } ) or return;
73 }
74  
75 my $class = {
76 module => 'CPANPLUS::Module',
77 author => 'CPANPLUS::Module::Author',
78 }->{ $table };
79  
80 my $obj = $self->{store}->{$key} = $class->new( %$href, _id => $cb->_id );
81  
82 return $obj;
83 }
84  
85 sub STORE {
86 my $self = shift;
87 my $key = shift;
88 my $val = shift;
89  
90 $self->{store}->{$key} = $val;
91 }
92  
93 1;
94  
95 sub FIRSTKEY {
96 my $self = shift;
97 my $dbh = $self->{'dbh'};
98  
99 my $res = $dbh->query(
100 "select $self->{key} from $self->{table} order by $self->{key} limit 1"
101 );
102  
103 $self->{offset} = 0;
104  
105 my $key = $res->flat->[0];
106  
107 return $key;
108 }
109  
110 sub NEXTKEY {
111 my $self = shift;
112 my $dbh = $self->{'dbh'};
113  
114 my $res = $dbh->query(
115 "select $self->{key} from $self->{table} ".
116 "order by $self->{key} limit 1 offset $self->{offset}"
117 );
118  
119 $self->{offset} +=1;
120  
121 my $key = $res->flat->[0];
122 my $val = $self->FETCH( $key );
123  
124 ### use each() semantics
125 return wantarray ? ( $key, $val ) : $key;
126 }
127  
128 sub EXISTS { !!$_[0]->FETCH( $_[1] ) }
129  
130 sub SCALAR {
131 my $self = shift;
132 my $dbh = $self->{'dbh'};
133  
134 my $res = $dbh->query( "select count(*) from $self->{table}" );
135  
136 return $res->flat;
137 }
138  
139 ### intentionally left blank
140 sub DELETE { }
141 sub CLEAR { }
142