blib/lib/DBIx/Counter.pm

filter: Statementstmt 33.9% 18 / 53 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 33.3% 5 / 15 Podpod 100% 7 / 7 totaltotal 25.0% 30 / 120 CC 14 SCAR 39.9
CC
14
Cov
21%
new
132.0
_init
12.0
_db
6.0
CRAP 54.2
↑ Summary
1 package DBIx::Counter;
2  
3 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 require 5.004;
8  
9 3 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 '++' => \&inc,
11 '--' => \&dec,
12 '""' => \&value,
13 fallback => 1,
14 3 );
15  
16 3 ▶use vars qw( $VERSION $DSN $LOGIN $PASSWORD $TABLENAME );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 $VERSION = '0.03';
19  
20 sub new
21 0 ▶{
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
22 0 my $pkg = shift;
23 0 ▶ my $countername = shift or croak("No counter name supplied");
Branch: 1 branch
BranchTrueFalseTotal
my $countername = shift() or croak("No counter name supplied") 0 0 0
24 0 ▶ unshift @_, 'initial' if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
25 0 my %opts = @_;
26  
27 my $self = {
28 countername => $countername,
29 dbh => $opts{dbh},
30 dsn => $opts{dsn} || $DSN,
31 login => $opts{login} || $LOGIN,
32 password => $opts{password} || $PASSWORD,
33 tablename => $opts{tablename} || $TABLENAME || 'counters',
34 0 ▶ initial => $opts{initial} || '0',
Condition0%0/16
$opts{'dsn'} || $DBIx::Counter::DSN
l!l&&r!l&&!r
000
$opts{'login'} || $DBIx::Counter::LOGIN
l!l&&r!l&&!r
000
$opts{'password'} || $DBIx::Counter::PASSWORD
l!l&&r!l&&!r
000
$opts{'tablename'} || $DBIx::Counter::TABLENAME || 'counters'
l!l
00
$opts{"tablename"} or $DBIx::Counter::TABLENAME
l!l&&r!l&&!r
000
$opts{'initial'} || '0'
l!l
00
MC/DC0%0/2
$opts{'dsn'} || $DBIx::Counter::DSN
$opts{'dsn'} $DBIx::Counter::DSN
MC/DC0%0/2
$opts{'login'} || $DBIx::Counter::LOGIN
$opts{'login'} $DBIx::Counter::LOGIN
MC/DC0%0/2
$opts{'password'} || $DBIx::Counter::PASSWORD
$opts{'password'} $DBIx::Counter::PASSWORD
MC/DC0%0/2
$opts{'tablename'} || $DBIx::Counter::TABLENAME || 'counters'
$opts{"tablename"} $DBIx::Counter::TABLENAME
MC/DC0%0/1
$opts{'initial'} || '0'
$opts{'initial'}
Truth table0%0/3
A || B
A= $opts{'dsn'}B= $DBIx::Counter::DSN
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $opts{'login'}B= $DBIx::Counter::LOGIN
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $opts{'password'}B= $DBIx::Counter::PASSWORD
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $opts{"tablename"}B= $DBIx::Counter::TABLENAME
ABresult
1X1
011
000
Truth table0%0/2
A
A= $opts{'initial'}
Aresult
11
00
35 };
36  
37 croak("Unable to connect to database: no valid connection handle or DSN supplied")
38 0 ▶ unless $self->{dbh} or $self->{dsn};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} or $self->{"dsn"} 0 0 0
Condition0%0/3
$self->{"dbh"} or $self->{"dsn"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"dbh"} or $self->{"dsn"}
$self->{"dbh"} $self->{"dsn"}
Truth table0%0/3
A or B
A= $self->{"dbh"}B= $self->{"dsn"}
ABresult
1X1
011
000
39  
40 0 bless $self, $pkg;
41 0 $self->_init;
42 0 $self;
43 }
44  
45 sub _init
46 0 ▶{
Subroutine _init: not called CC=3 SCAR=24.8
47 0 my $self = shift;
48  
49 # create counter record if not exists
50 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $dbh = $self->_db; my($exists) = $dbh->selectrow_array(qq[select count(*) from $self->{"tablename"} where counter_id=?], undef, $self->{"countername"}); unless ($exists > 0) { $dbh->do(qq[insert into $self->{"tablename"} (counter_id,value) values (?,?)], undef, $self->{"countername"}, $self->{"initial"}); } } } or croak("Error creating counter record: $@") 0 0 0
51 0 my $dbh = $self->_db;
52 0 my ($exists) = $dbh->selectrow_array( qq{select count(*) from $self->{tablename} where counter_id=?}, undef, $self->{countername} );
53 0 ▶ unless ( $exists > 0 )
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists > 0) 0 0 0
54 {
55 0 $dbh->do( qq{insert into $self->{tablename} (counter_id,value) values (?,?)}, undef, $self->{countername}, $self->{initial} );
56 }
57 } or croak "Error creating counter record: $@";
58 }
59  
60 sub _db
61 0 ▶{
Subroutine _db: not called CC=2 SCAR=17.9
62 0 my $self = shift;
63  
64 return $self->{dbh}
65 0 ▶ || DBI->connect_cached( $self->{dsn}, $self->{login}, $self->{password}, { PrintError => 0, RaiseError => 1 } );
Condition0%0/3
$self->{'dbh'} || 'DBI'->connect_cached($self->{'dsn'}, $self->{'login'}, $self->{'password'}, {'PrintError', 0, 'RaiseError', 1})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'dbh'} || 'DBI'->connect_cached($self->{'dsn'}, $self->{'login'}, $self->{'password'}, {'PrintError', 0, 'RaiseError', 1})
$self->{'dbh'} 'DBI'->connect_cached($self->{'dsn'}, $self->{'login'}, $self->{'password'}, {'PrintError', 0, 'RaiseError', 1})
Truth table0%0/3
A || B
A= $self->{'dbh'}B= 'DBI'->connect_cached($self->{'dsn'}, $self->{'login'}, $self->{'password'}, {'PrintError', 0, 'RaiseError', 1})
ABresult
1X1
011
000
66 }
67  
68 sub _add
69 0 ▶{
Subroutine _add: not called CC=1 SCAR=6.9
70 0 my ( $self, $add ) = @_;
71 0 my $dbh = $self->_db;
72 0 my $sth_set = $dbh->prepare_cached(qq{update $self->{tablename} set value=value+? where counter_id=?});
73 0 $sth_set->execute( $add, $self->{countername} );
74 }
75  
76 sub inc
77 0 ▶{
Subroutine inc: not called CC=1 SCAR=6.9
Pod documented
78 0 my $self = shift;
79 0 $self->_add(1);
80 }
81  
82 sub dec
83 0 ▶{
Subroutine dec: not called CC=1 SCAR=6.9
Pod documented
84 0 my $self = shift;
85 0 $self->_add(-1);
86 }
87  
88 sub value
89 0 ▶{
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
90 0 my $self = shift;
91 0 my $dbh = $self->_db;
92 0 my $sth_get = $dbh->prepare_cached(qq{select value from $self->{tablename} where counter_id=?});
93  
94 0 $sth_get->execute( $self->{countername} );
95 0 my ($v) = $sth_get->fetchrow_array;
96 0 $sth_get->finish;
97  
98 0 return $v;
99 }
100  
101 0 ▶sub lock { 0 }
Subroutine lock: not called CC=1 SCAR=6.9
Pod documented
102 0 ▶sub unlock { 0 }
Subroutine unlock: not called CC=1 SCAR=6.9
Pod documented
103 0 ▶sub locked { 0 }
Subroutine locked: not called CC=1 SCAR=6.9
Pod documented
104  
105 3 1;
106  
107 __END__
↑ Summary