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