File Coverage

blib/lib/DBIx/Counter.pm
Criterion Covered Total %
statement 15 50 30.0
branch 0 10 0.0
condition 0 19 0.0
subroutine 5 15 33.3
pod 7 7 100.0
total 27 101 26.7


line stmt bran cond sub pod time code
1             package DBIx::Counter;
2              
3 2     2   49647 use DBI;
  2         39110  
  2         131  
4 2     2   62 use Carp qw(carp croak);
  2         4  
  2         146  
5 2     2   11 use strict;
  2         9  
  2         144  
6              
7             require 5.004;
8              
9             use overload (
10 2         23 '++' => \&inc,
11             '--' => \&dec,
12             '""' => \&value,
13             fallback => 1,
14 2     2   3608 );
  2         2101  
15              
16 2     2   174 use vars qw( $VERSION $DSN $LOGIN $PASSWORD $TABLENAME );
  2         4  
  2         1337  
17              
18             $VERSION = '0.03';
19              
20             sub new
21             {
22 0     0 1   my $pkg = shift;
23 0 0         my $countername = shift or croak("No counter name supplied");
24 0 0         unshift @_, 'initial' if @_ % 2;
25 0           my %opts = @_;
26              
27 0   0       my $self = {
      0        
      0        
      0        
      0        
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             initial => $opts{initial} || '0',
35             };
36              
37 0 0 0       croak("Unable to connect to database: no valid connection handle or DSN supplied")
38             unless $self->{dbh} or $self->{dsn};
39              
40 0           bless $self, $pkg;
41 0           $self->_init;
42 0           $self;
43             }
44              
45             sub _init
46             {
47 0     0     my $self = shift;
48              
49             # create counter record if not exists
50 0 0         eval {
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 0         unless ( $exists > 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             {
62 0     0     my $self = shift;
63              
64 0   0       return $self->{dbh}
65             || DBI->connect_cached( $self->{dsn}, $self->{login}, $self->{password}, { PrintError => 0, RaiseError => 1 } );
66             }
67              
68             sub _add
69             {
70 0     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             {
78 0     0 1   my $self = shift;
79 0           $self->_add(1);
80             }
81              
82             sub dec
83             {
84 0     0 1   my $self = shift;
85 0           $self->_add(-1);
86             }
87              
88             sub value
89             {
90 0     0 1   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     0 1   sub lock { 0 }
102 0     0 1   sub unlock { 0 }
103 0     0 1   sub locked { 0 }
104              
105             1;
106              
107             __END__