lib/Class/DBI/AutoIncrement.pm

filter: Statementstmt 30.1% 32 / 106 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 52.9% 9 / 17 Podpod 100% 4 / 4 totaltotal 22.7% 45 / 198 CC 27 SCAR 38.1
CC
27
Cov
20%
autoincrement
110.0
_do_increment
72.0
_set_inheritance
30.1
CRAP 45.1
↑ Summary
1 #################################################################
2 #
3 # Class::DBI::AutoIncrement - Emulate auto-incrementing columns in a Class::DBI table
4 #
5 # $Id: AutoIncrement.pm,v 1.7 2006/06/08 08:58:32 erwan Exp $
6 #
7 # 060412 erwan Created
8 # 060517 erwan Added croak when no other parent than Class::DBI::AutoIncrement
9 # 060607 erwan Fixed rt 19752, backward compatibility issue with create()
10 # 060607 erwan Refactor to use maximum_value_of() (credits to David Westbrook)
11 #
12 #################################################################
13  
14 #################################################################
15 #
16 # an object holding data describing one auto-incremented table.
17 # in charge of incrementing that table's index
18 #
19 #################################################################
20  
21 package Class::DBI::AutoIncrement::Descriptor;
22  
23 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 Class::Accessor->mk_accessors('table', # name of the table to auto-increment
29 'column', # name of the auto-incremented column in that table
30 'min', # the start value for the index sequence
31 'step', # the increment step of the index sequence
32 'cache', # if true, the index value is cached instead of being queried upon each insert
33 'index', # value of the index used at the last insert (if caching is on)
34 );
35  
36 0 ▶sub new { return bless({},__PACKAGE__); }
Subroutine new: not called CC=1 SCAR=6.9
37  
38 2 1;
39  
40 #################################################################
41 #
42 # Class::DBI::AutoIncrement
43 #
44 #################################################################
45  
46 package Class::DBI::AutoIncrement;
47  
48 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 2 our $VERSION = '0.05';
54  
55 # set at runtime by _set_inheritance()
56 2 our @ISA;
57  
58 ##################################################################
59 #
60 # PRIVATE FUNCTIONS
61 #
62 ##################################################################
63  
64 #-----------------------------------------------------------------
65 #
66 # _set_inheritance - make Class::DBI::AutoIncrement inherit from the same parents as the calling class
67 # see discussion below.
68 #
69  
70 2 my $inherited = 0;
71  
72 0 ▶sub _set_inheritance {
Subroutine _set_inheritance: not called CC=6 SCAR=34.1
73 0 ▶ return if ($inherited);
Branch: 1 branch
BranchTrueFalseTotal
if $inherited 0 0 0
74  
75 0 my($caller) = shift;
76 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 0 my @parents = grep { $_ ne __PACKAGE__ } @{"$caller\::ISA"};
79 0 ▶ croak __PACKAGE__." expects class $caller to inherit from at least 1 more parent class" if (scalar @parents == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @parents == 0 0 0 0
80
81 # inherit from same parents as the calling class, this in order
82 # to have the proper inheritance toward the local *::DBI class,
83 # without having to know its name and explicitly 'use base' it
84  
85 # this might not always work, since it redefines the class hierarchy under time
86 # plus we then require twice the same set of parent classes...
87 # an alternative would be to emulate ::SUPER-> and skip __PACKAGE__ while
88 # calling the methods insert() and create()...
89  
90 0 foreach my $class (@parents) {
91 0 eval qq{ require $class; };
92 0 ▶ if (defined $@ && $@ ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $@ and $@ ne "") 0 0 0
Condition0%0/3
defined $@ and $@ ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $@ and $@ ne ""
defined $@ $@ ne ""
Truth table0%0/3
A and B
A= defined $@B= $@ ne ""
ABresult
0X0
100
111
93 0 confess "BUG: \'require $class\' failed because of: ".$@;
94 }
95 }
96 0 push @ISA, @parents;
97  
98 0 $inherited = 1;
99 }
100  
101 #-----------------------------------------------------------------
102 #
103 # _get_descriptor - instanciate an object holding information about the calling class
104 #
105  
106 2 my $descriptors;
107  
108 0 ▶sub _get_descriptor {
Subroutine _get_descriptor: not called CC=2 SCAR=17.9
109 0 my $class = shift;
110 0 ▶ if (!exists $descriptors->{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $descriptors->{$class}) 0 0 0
111 0 $descriptors->{$class} = new Class::DBI::AutoIncrement::Descriptor($class);
112 }
113 0 return $descriptors->{$class};
114 }
115  
116 #-----------------------------------------------------------------
117 #
118 # _do_increment - increment sequence if needed, see insert() and create()
119 #
120  
121 0 ▶sub _do_increment {
Subroutine _do_increment: not called CC=8 SCAR=42.8
122 0 my($proto,$class,$values) = @_;
123  
124 0 _set_inheritance($class);
125  
126 0 my $info = _get_descriptor($class);
127  
128 # check that we know all that should be known (column name, table name...)
129 0 ▶ if (!defined $info->column) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info->column) 0 0 0
130 0 croak "no auto-incremented column has been specified for class $class.";
131 }
132  
133 0 ▶ if (!defined $info->table) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info->table) 0 0 0
134 0 croak "no database table has been specified for class $class.";
135 }
136  
137 0 my $column = $info->column;
138  
139 # if the index column is not set, set it to its next known value
140 0 ▶ if (!exists $values->{$column} || !defined $values->{$column}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $values->{$column} && defined $values->{$column}) 0 0 0
Condition0%0/3
exists $values->{$column} && defined $values->{$column}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $values->{$column} && defined $values->{$column}
exists $values->{$column} defined $values->{$column}
Truth table0%0/3
A && B
A= exists $values->{$column}B= defined $values->{$column}
ABresult
0X0
100
111
141 0 my $index;
142  
143 0 ▶ if (!defined $info->index) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $info->index) { } 0 0 0
144
145 0 my $id = $proto->maximum_value_of($info->column);
146
147 0 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 0 0 0
148 0 $index = $id + $info->step;
149 } else {
150 # table is empty, this will be the first row inserted
151 0 $index = $info->min;
152 }
153  
154 # if caching is on, save the index for next time we need it
155 0 ▶ if ($info->cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->cache) 0 0 0
156 0 $info->index($index);
157 }
158 } else {
159 # we are caching the index, and we know the current highest index
160 0 $info->index($info->step+$info->index);
161 0 $index = $info->index;
162 }
163  
164 0 $values->{$column} = $index;
165 }
166 }
167  
168 ##################################################################
169 #
170 # PUBLIC (INHERITED) FUNCTIONS
171 #
172 ##################################################################
173  
174 #-----------------------------------------------------------------
175 #
176 # autoincrement - register which column should be automatically incremented
177 #
178  
179 0 ▶sub autoincrement {
Subroutine autoincrement: not called CC=10 SCAR=47.0
Pod documented
180 0 my($proto,$column,%args) = @_;
181 0 ▶ my $class = ref $proto || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
182 0 _set_inheritance($class);
183  
184 0 ▶ if (!defined $column) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $column) 0 0 0
185 0 croak "you must define a column name to autoincrement.";
186 }
187  
188 0 my $info = _get_descriptor($class);
189  
190 0 ▶ if (defined $info->column()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->column) 0 0 0
191 0 croak "class $class already has one auto-incremented column";
192 }
193  
194 0 $info->column($column);
195  
196 0 ▶ if (exists $args{Min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'Min'}) { } 0 0 0
197 0 ▶ if ($args{Min} !~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"Min"} =~ /^-?\d+$/) 0 0 0
198 0 croak "parameter 'Min' of method 'autoincrement' must be a number.";
199 }
200 0 $info->min($args{Min});
201 } else {
202 0 $info->min(0);
203 }
204  
205 0 ▶ if (exists $args{Step}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'Step'}) { } 0 0 0
206 0 ▶ if ($args{Step} !~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"Step"} =~ /^-?\d+$/) 0 0 0
207 0 croak "parameter 'Step' of method 'autoincrement' must be a number.";
208 }
209 0 $info->step($args{Step});
210 } else {
211 0 $info->step(1);
212 }
213  
214 0 ▶ if (exists $args{Cache} && $args{Cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'Cache'} and $args{'Cache'}) { } 0 0 0
Condition0%0/3
exists $args{"Cache"} and $args{"Cache"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"Cache"} and $args{"Cache"}
exists $args{"Cache"} $args{"Cache"}
Truth table0%0/3
A and B
A= exists $args{"Cache"}B= $args{"Cache"}
ABresult
0X0
100
111
215 0 $info->cache(1);
216 } else {
217 0 $info->cache(0);
218 }
219 }
220  
221 #-----------------------------------------------------------------
222 #
223 # table - override *::DBI->table() in order to intercept the table name
224 #
225  
226 0 ▶sub table {
Subroutine table: not called CC=3 SCAR=24.8
Pod documented
227 0 my($proto,$table,@args) = @_;
228 0 ▶ my $class = ref $proto || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
229 0 _set_inheritance($class);
230  
231 0 ▶ if (defined $table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table) 0 0 0
232 0 my $info = _get_descriptor($class);
233 0 $info->table($table);
234 }
235  
236 0 return $class->SUPER::table($table,@args);
237 }
238  
239 #-----------------------------------------------------------------
240 #
241 # insert - insert a new value, after incrementing its sequence if necessary
242 #
243  
244 0 ▶sub insert {
Subroutine insert: not called CC=2 SCAR=17.9
Pod documented
245 0 my($proto,$values) = @_;
246 0 ▶ my $class = ref $proto || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
247 0 _do_increment($proto,$class,$values);
248 0 return $class->SUPER::insert($values);
249 }
250  
251 # backward compatibility
252 0 ▶sub create {
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
253 0 my($proto,$values) = @_;
254 0 ▶ my $class = ref $proto || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
255 0 _do_increment($proto,$class,$values);
256 0 return $class->SUPER::create($values);
257 }
258  
259 2 1;
260  
261 __END__
↑ Summary