File Coverage

blib/lib/DBIx/Recordset/DBSeq.pm
Criterion Covered Total %
statement 3 24 12.5
branch 0 14 0.0
condition 0 7 0.0
subroutine 1 3 33.3
pod 0 2 0.0
total 4 50 8.0


line stmt bran cond sub pod time code
1              
2             ###################################################################################
3             #
4             # DBIx::Recordset - Copyright (c) 1997-2000 Gerald Richter / ECOS
5             #
6             # You may distribute under the terms of either the GNU General Public
7             # License or the Artistic License, as specified in the Perl README file.
8             #
9             # THIS IS BETA SOFTWARE!
10             #
11             # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
12             # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
13             # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14             #
15             # $Id: DBSeq.pm,v 1.5 2000/06/26 05:16:18 richter Exp $
16             #
17             ###################################################################################
18              
19              
20             package DBIx::Recordset::DBSeq ;
21              
22 1     1   509 use strict 'vars' ;
  1         3  
  1         369  
23              
24              
25              
26             ## ----------------------------------------------------------------------------
27             ##
28             ## new
29             ##
30             ## creates a new DBIx::Recordset::DBSeq object.
31             ##
32             ## $dbh = Database handle
33             ## $table = table where to keep sequences
34             ##
35              
36             sub new
37              
38             {
39 0     0 0   my ($class, $dbh, $table, $min, $max) = @_ ;
40            
41              
42              
43 0   0       my $self = {
      0        
44             '*Debug' => $DBIx::Recordset::Debug,
45             '*dbh' => $dbh,
46             '*table' => $table,
47             '*DefaultMin' => $min || 1,
48             '*DefaultMax' => $max || 'NULL',
49             } ;
50              
51 0           bless ($self, $class) ;
52              
53 0           return $self ;
54             }
55              
56              
57              
58             ## ----------------------------------------------------------------------------
59             ##
60             ## NextVal
61             ##
62             ## get next value from counter
63             ##
64             ## in $name = counter name
65             ##
66              
67              
68             sub NextVal
69              
70             {
71 0     0 0   my ($self, $name) = @_ ;
72              
73 0           my $dbh = $self -> {'*dbh'} ;
74            
75 0 0         $dbh -> do ("lock table $self->{'*table'} write") or die "Cannot lock $self->{'*table'} ($DBI::errstr)" ;
76              
77              
78              
79 0 0         my $sth = $dbh -> prepare ("select cnt,maxcnt from $self->{'*table'} where name=?") or die "Cannot prepare select for $self->{'*table'} ($DBI::errstr)" ;
80              
81 0           $sth -> execute ($name) ;
82              
83 0           my $row = $sth -> fetchrow_arrayref ;
84 0           my $cnt ;
85             my $max ;
86            
87 0 0         if (!$row)
88             {
89 0           $cnt = $self->{'*DefaultMin'} ;
90 0           $max = $self->{'*DefaultMax'} ;
91 0           my $cnt1 = $cnt + 1 ;
92 0 0         $dbh -> do ("insert into $self->{'*table'} (name,cnt,maxcnt) values ('$name',$cnt1,$max)") or die "Cannot insert $self->{'*table'} ($DBI::errstr)" ;
93             }
94             else
95             {
96 0           $cnt = $row -> [0] ;
97 0 0 0       die "Max count reached for sequence $name" if (defined ($row->[1]) && $cnt+1 > $row->[1]) ;
98 0 0         $dbh -> do ("update $self->{'*table'} set cnt=cnt+1 where name='$name'") or die "Cannot update $self->{'*table'} ($DBI::errstr)" ;
99             }
100              
101 0 0         $dbh -> do ("unlock table") or die "Cannot unlock $self->{'*table'} ($DBI::errstr)" ;
102            
103 0           return $cnt ;
104             }
105              
106             1;
107              
108             __END__