DBI.pm

filter: Statementstmt 35.7% 229 / 641 Branchbran 18.2% 43 / 236 Conditioncond 20.4% 20 / 98 MC/DCmcdc 5.1% 3 / 58 Subroutinesub 35.5% 49 / 138 Podpod 69.2% 36 / 52 totaltotal 31.0% 380 / 1223 CC 162 SCAR 28.2
CC
162
Cov
30%
_deflated_column
110.0
constrain_column
56.0
retrieve
53.6
CRAP 16.8
« DBI/Query.pm ↑ Summary DBI/Search/Basic.pm »
1 package Class::DBI::__::Base;
2  
3 4 require 5.006;
4  
5 4 ▶use Class::Trigger 0.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use base qw(Class::Accessor Class::Data::Inheritable Ima::DBI);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Class::DBI;
9  
10 4 ▶use version; $VERSION = qv('3.0.17');
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use base "Class::DBI::__::Base";
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use Class::DBI::ColumnGrouper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Class::DBI::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use UNIVERSAL::moniker;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 ▶use vars qw($Weaken_Is_Available);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
27 4 $Weaken_Is_Available = 1;
28 4 eval {
29 4 require Scalar::Util;
30 4 import Scalar::Util qw(weaken);
31 };
32 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
33 0 $Weaken_Is_Available = 0;
34 }
35 }
36  
37 use overload
38 0 ▶ '""' => sub { shift->stringify_self },
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 ▶ bool => sub { not shift->_undefined_primary },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 4 fallback => 1;
41  
42 0 ▶sub stringify_self {
Subroutine stringify_self: not called CC=4 SCAR=30.0
Pod undocumented
43 0 my $self = shift;
44 0 ▶ return (ref $self || $self) unless $self; # empty PK
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
45 0 my @cols = $self->columns('Stringify');
46 0 ▶ @cols = $self->primary_columns unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 0 0 0
47 0 return join "/", $self->get(@cols);
48 }
49  
50 0 ▶sub _undefined_primary {
Subroutine _undefined_primary: not called CC=1 SCAR=6.9
51 0 my $self = shift;
52 0 return grep !defined, $self->_attrs($self->primary_columns);
53 }
54  
55 #----------------------------------------------------------------------
56 # Deprecations
57 #----------------------------------------------------------------------
58  
59 4 __PACKAGE__->mk_classdata('__hasa_rels' => {});
60  
61 {
62 4 my %deprecated = (
63 # accessor_name => 'accessor_name_for', # 3.0.7
64 # mutator_name => 'accessor_name_for', # 3.0.7
65 );
66  
67 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 4 ▶ while (my ($old, $new) = each %deprecated) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %deprecated) 0 4 4
69 0 ▶ *$old = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 my @caller = caller;
71 0 warn
72 "Use of '$old' is deprecated at $caller[1] line $caller[2]. Use '$new' instead\n";
73 0 goto &$new;
74 0 };
75 }
76 }
77  
78 #----------------------------------------------------------------------
79 # Our Class Data
80 #----------------------------------------------------------------------
81 4 __PACKAGE__->mk_classdata('__AutoCommit');
82 4 __PACKAGE__->mk_classdata('__hasa_list');
83 4 __PACKAGE__->mk_classdata('_table');
84 4 __PACKAGE__->mk_classdata('_table_alias');
85 4 __PACKAGE__->mk_classdata('sequence');
86 4 __PACKAGE__->mk_classdata('__grouper' => Class::DBI::ColumnGrouper->new());
87 4 __PACKAGE__->mk_classdata('__data_type' => {});
88 4 __PACKAGE__->mk_classdata('__driver');
89 4 __PACKAGE__->mk_classdata('iterator_class' => 'Class::DBI::Iterator');
90 4 __PACKAGE__->mk_classdata('purge_object_index_every' => 1000);
91 4 __PACKAGE__->add_searcher(search => "Class::DBI::Search::Basic",);
92  
93 4 __PACKAGE__->add_relationship_type(
94 has_a => "Class::DBI::Relationship::HasA",
95 has_many => "Class::DBI::Relationship::HasMany",
96 might_have => "Class::DBI::Relationship::MightHave",
97 );
98 4 __PACKAGE__->mk_classdata('__meta_info' => {});
99  
100 #----------------------------------------------------------------------
101 # SQL we'll need
102 #----------------------------------------------------------------------
103 4 __PACKAGE__->set_sql(MakeNewObj => <<'');
104 INSERT INTO __TABLE__ (%s)
105 VALUES (%s)
106  
107 4 __PACKAGE__->set_sql(update => <<"");
108 UPDATE __TABLE__
109 SET %s
110 WHERE __IDENTIFIER__
111  
112 4 __PACKAGE__->set_sql(Nextval => <<'');
113 SELECT NEXTVAL ('%s')
114  
115 4 __PACKAGE__->set_sql(SearchSQL => <<'');
116 SELECT %s
117 FROM %s
118 WHERE %s
119  
120 4 __PACKAGE__->set_sql(RetrieveAll => <<'');
121 SELECT __ESSENTIAL__
122 FROM __TABLE__
123  
124 4 __PACKAGE__->set_sql(Retrieve => <<'');
125 SELECT __ESSENTIAL__
126 FROM __TABLE__
127 WHERE %s
128  
129 4 __PACKAGE__->set_sql(Flesh => <<'');
130 SELECT %s
131 FROM __TABLE__
132 WHERE __IDENTIFIER__
133  
134 4 __PACKAGE__->set_sql(single => <<'');
135 SELECT %s
136 FROM __TABLE__
137  
138 4 __PACKAGE__->set_sql(DeleteMe => <<"");
139 DELETE
140 FROM __TABLE__
141 WHERE __IDENTIFIER__
142  
143  
144 4 __PACKAGE__->mk_classdata('sql_transformer_class');
145 4 __PACKAGE__->sql_transformer_class('Class::DBI::SQL::Transformer');
146  
147 # Override transform_sql from Ima::DBI to provide some extra
148 # transformations
149 0 ▶sub transform_sql {
Subroutine transform_sql: not called CC=1 SCAR=6.9
Pod documented
150 0 my ($self, $sql, @args) = @_;
151 0 my $tclass = $self->sql_transformer_class;
152 0 $self->_require_class($tclass);
153 0 my $T = $tclass->new($self, $sql, @args);
154 0 return $self->SUPER::transform_sql($T->sql => $T->args);
155 }
156  
157 #----------------------------------------------------------------------
158 # EXCEPTIONS
159 #----------------------------------------------------------------------
160  
161 0 ▶sub _carp {
Subroutine _carp: not called CC=2 SCAR=17.9
162 0 my ($self, $msg) = @_;
163 0 ▶ Carp::carp($msg || $self);
Condition0%0/3
$msg || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$msg || $self
$msg $self
Truth table0%0/3
A || B
A= $msgB= $self
ABresult
1X1
011
000
164 0 return;
165 }
166  
167 1 ▶sub _croak {
Subroutine _croak: called CC=2 SCAR=8.1
168 1 my ($self, $msg) = @_;
169 1 ▶ Carp::croak($msg || $self);
Condition33%1/3
$msg || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
$msg || $self
$msg $self
Truth table33%1/3
A || B
A= $msgB= $self
ABresult
1X1
011
000
170 }
171  
172 0 ▶sub _db_error {
Subroutine _db_error: not called CC=1 SCAR=6.9
173 0 my ($self, %info) = @_;
174 0 my $msg = delete $info{msg};
175 0 return $self->_croak($msg, %info);
176 }
177  
178 #----------------------------------------------------------------------
179 # SET UP
180 #----------------------------------------------------------------------
181  
182 0 ▶sub connection {
Subroutine connection: not called CC=1 SCAR=6.9
Pod documented
183 0 my $class = shift;
184 0 $class->set_db(Main => @_);
185 }
186  
187 {
188 4 my %Per_DB_Attr_Defaults = (
189 pg => { AutoCommit => 0 },
190 oracle => { AutoCommit => 0 },
191 );
192  
193 0 ▶ sub _default_attributes {
Subroutine _default_attributes: not called CC=2 SCAR=17.9
194 0 my $class = shift;
195 return (
196 $class->SUPER::_default_attributes,
197 FetchHashKeyName => 'NAME_lc',
198 ShowErrorStatement => 1,
199 AutoCommit => 1,
200 ChopBlanks => 1,
201 0 ▶ %{ $Per_DB_Attr_Defaults{ lc $class->__driver } || {} },
Condition0%0/2
$Per_DB_Attr_Defaults{lc $class->__driver} || {}
l!l
00
MC/DC0%0/1
$Per_DB_Attr_Defaults{lc $class->__driver} || {}
$Per_DB_Attr_Defaults{lc $class->__driver}
Truth table0%0/2
A
A= $Per_DB_Attr_Defaults{lc $class->__driver}
Aresult
11
00
202 );
203 }
204 }
205  
206 4 ▶sub set_db {
Subroutine set_db: not called CC=1 SCAR=4.1
Pod documented
207 0 my ($class, $db_name, $data_source, $user, $password, $attr) = @_;
208  
209 # 'dbi:Pg:dbname=foo' we want 'Pg'. I think this is enough.
210 0 my ($driver) = $data_source =~ /^dbi:(\w+)/i;
211 0 $class->__driver($driver);
212 0 $class->SUPER::set_db('Main', $data_source, $user, $password, $attr);
213 }
214  
215 6 ▶sub table {
Subroutine table: called CC=5 SCAR=17.8
Pod documented
216 6 my ($proto, $table, $alias) = @_;
217 6 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
218 6 ▶ $class->_table($table) if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 5 1 6
219 6 ▶ $class->table_alias($alias) if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 6 6
220 6 ▶ return $class->_table || $class->_table($class->table_alias);
Condition33%1/3
$class->_table || $class->_table($class->table_alias)
l!l&&r!l&&!r
600
MC/DC0%0/2
$class->_table || $class->_table($class->table_alias)
$class->_table $class->_table($class->table_alias)
Truth table33%1/3
A || B
A= $class->_tableB= $class->_table($class->table_alias)
ABresult
1X1
011
000
221 }
222  
223 1 ▶sub table_alias {
Subroutine table_alias: called CC=4 SCAR=16.4
Pod documented
224 1 my ($proto, $alias) = @_;
225 1 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
226 1 ▶ $class->_table_alias($alias) if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 1 1
227 1 ▶ return $class->_table_alias || $class->_table_alias($class->moniker);
Condition33%1/3
$class->_table_alias || $class->_table_alias($class->moniker)
l!l&&r!l&&!r
010
MC/DC0%0/2
$class->_table_alias || $class->_table_alias($class->moniker)
$class->_table_alias $class->_table_alias($class->moniker)
Truth table33%1/3
A || B
A= $class->_table_aliasB= $class->_table_alias($class->moniker)
ABresult
1X1
011
000
228 }
229  
230 34 ▶sub columns {
Subroutine columns: called CC=7 SCAR=19.5
Pod documented
231 34 my $proto = shift;
232 34 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0340
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
233 34 ▶ my $group = shift || "All";
Condition100%2/2
shift() || 'All'
l!l
286
MC/DC100%1/1
shift() || 'All'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
234 34 ▶ return $class->_set_columns($group => @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 15 19 34
235 19 ▶ return $class->all_columns if $group eq "All";
Branch: 1 branch
BranchTrueFalseTotal
if $group eq "All" 10 9 19
236 9 ▶ return $class->primary_column if $group eq "Primary";
Branch: 1 branch
BranchTrueFalseTotal
if $group eq "Primary" 4 5 9
237 5 ▶ return $class->_essential if $group eq "Essential";
Branch: 1 branch
BranchTrueFalseTotal
if $group eq "Essential" 4 1 5
238 1 return $class->__grouper->group_cols($group);
239 }
240  
241 31 ▶sub _column_class { 'Class::DBI::Column' }
Subroutine _column_class: called CC=1 SCAR=0.0
242  
243 15 ▶sub _set_columns {
Subroutine _set_columns: called CC=2 SCAR=6.9
244 15 my ($class, $group, @columns) = @_;
245  
246 15 ▶ my @cols = map ref $_ ? $_ : $class->_column_class->new($_), @columns;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 31 32
247  
248 # Careful to take copy
249 15 $class->__grouper(Class::DBI::ColumnGrouper->clone($class->__grouper)
250 ->add_group($group => @cols));
251 15 $class->_mk_column_accessors(@cols);
252 15 return @columns;
253 }
254  
255 10 ▶sub all_columns { shift->__grouper->all_columns }
Subroutine all_columns: called CC=1 SCAR=0.0
Pod undocumented
256  
257 0 ▶sub id {
Subroutine id: not called CC=6 SCAR=37.4
Pod documented
258 0 my $self = shift;
259 0 ▶ my $class = ref($self)
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or return $self->_croak("Can't call id() as a class method") 0 0 0
260 or return $self->_croak("Can't call id() as a class method");
261  
262 # we don't use get() here because all objects should have
263 # exisitng values for PK columns, or else loop endlessly
264 0 my @pk_values = $self->_attrs($self->primary_columns);
265 0 ▶ UNIVERSAL::can($_ => 'id') and $_ = $_->id for @pk_values;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($_, "id") and $_ = $_->id 0 0 0
266 0 ▶ return @pk_values if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
267 0 ▶ $self->_croak(
Branch: 1 branch
BranchTrueFalseTotal
if @pk_values > 1 0 0 0
268 "id called in scalar context for class with multiple primary key columns")
269 if @pk_values > 1;
270 0 return $pk_values[0];
271 }
272  
273 14 ▶sub primary_column {
Subroutine primary_column: called CC=3 SCAR=11.0
Pod undocumented
274 14 my $self = shift;
275 14 my @primary_columns = $self->__grouper->primary;
276 14 ▶ return @primary_columns if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 12 2 14
277 2 ▶ $self->_carp(
Branch: 1 branch
BranchTrueFalseTotal
if @primary_columns > 1 0 2 2
278 ref($self)
279 . " has multiple primary columns, but fetching in scalar context")
280 if @primary_columns > 1;
281 2 return $primary_columns[0];
282 }
283 4 *primary_columns = \&primary_column;
284  
285 4 ▶sub _essential { shift->__grouper->essential }
Subroutine _essential: called CC=1 SCAR=0.0
286  
287 6 ▶sub find_column {
Subroutine find_column: called CC=1 SCAR=0.0
Pod documented
288 6 my ($class, $want) = @_;
289 6 return $class->__grouper->find_column($want);
290 }
291  
292 1 ▶sub _find_columns {
Subroutine _find_columns: called CC=1 SCAR=0.0
293 1 my $class = shift;
294 1 my $cg = $class->__grouper;
295 1 return map $cg->find_column($_), @_;
296 }
297  
298 0 ▶sub has_real_column { # is really in the database
Subroutine has_real_column: not called CC=2 SCAR=17.9
Pod undocumented
299 0 my ($class, $want) = @_;
300 0 ▶ return ($class->find_column($want) || return)->in_database;
Condition0%0/2
$class->find_column($want) || (return)
l!l
00
MC/DC0%0/1
$class->find_column($want) || (return)
$class->find_column($want)
Truth table0%0/2
A
A= $class->find_column($want)
Aresult
11
00
301 }
302  
303 0 ▶sub data_type {
Subroutine data_type: not called CC=2 SCAR=17.9
Pod undocumented
304 0 my $class = shift;
305 0 my %datatype = @_;
306 0 ▶ while (my ($col, $type) = each %datatype) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %datatype) 0 0 0
307 0 $class->_add_data_type($col, $type);
308 }
309 }
310  
311 0 ▶sub _add_data_type {
Subroutine _add_data_type: not called CC=1 SCAR=6.9
312 0 my ($class, $col, $type) = @_;
313 0 my $datatype = $class->__data_type;
314 0 $datatype->{$col} = $type;
315 0 $class->__data_type($datatype);
316 }
317  
318 # Make a set of accessors for each of a list of columns. We construct
319 # the method name by calling accessor_name_for() and mutator_name_for()
320 # with the normalized column name.
321  
322 # mutator name will be the same as accessor name unless you override it.
323  
324 # If both the accessor and mutator are to have the same method name,
325 # (which will always be true unless you override mutator_name_for), a
326 # read-write method is constructed for it. If they differ we create both
327 # a read-only accessor and a write-only mutator.
328  
329 15 ▶sub _mk_column_accessors {
Subroutine _mk_column_accessors: called CC=6 SCAR=17.9
330 15 my $class = shift;
331 15 foreach my $col (@_) {
332  
333 32 my $default_accessor = $col->accessor;
334  
335 32 my $acc = $class->accessor_name_for($col);
336 32 my $mut = $class->mutator_name_for($col);
337  
338 32 my %method = ();
339  
340 32 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($acc eq $mut or $mut eq $default_accessor) { } 25 7 32
Condition66%2/3
$acc eq $mut or $mut eq $default_accessor
l!l&&r!l&&!r
2507
MC/DC50%1/2
$acc eq $mut or $mut eq $default_accessor
$acc eq $mut $mut eq $default_accessor
Truth table66%2/3
A or B
A= $acc eq $mutB= $mut eq $default_accessor
ABresult
1X1
011
000
341 ($acc eq $mut) # if they are the same
342 or ($mut eq $default_accessor)
343 ) { # or only the accessor was customized
344 25 %method = ('_' => $acc); # make the accessor the mutator too
345 25 $col->accessor($acc);
346 25 $col->mutator($acc);
347 } else {
348 7 %method = (
349 _ro_ => $acc,
350 _wo_ => $mut,
351 );
352 7 $col->accessor($acc);
353 7 $col->mutator($mut);
354 }
355  
356 32 foreach my $type (keys %method) {
357 39 my $name = $method{$type};
358 39 my $acc_type = "make${type}accessor";
359 39 my $accessor = $class->$acc_type($col->name_lc);
360 39 $class->_make_method($_, $accessor) for ($name, "_${name}_accessor");
361 }
362 }
363 }
364  
365 180 ▶sub _make_method {
Subroutine _make_method: called CC=5 SCAR=16.5
366 180 my ($class, $name, $method) = @_;
367 180 ▶ return if defined &{"$class\::$name"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${class}::$name";} 78 102 180
368 102 ▶ $class->_carp("Column '$name' in $class clashes with built-in method")
Branch: 1 branch
BranchTrueFalseTotal
if "Class::DBI"->can($name) and not $name eq "id" && join(" ", $class->primary_columns) eq "id" 0 102 102
Condition50%3/6
"Class::DBI"->can($name) and not $name eq "id" && join(" ", $class->primary_columns) eq "id"
!ll&&!rl&&r
9570
$name eq 'id' && join(' ', $class->primary_columns) eq 'id'
!ll&&!rl&&r
007
MC/DC0%0/3
"Class::DBI"->can($name) and not $name eq "id" && join(" ", $class->primary_columns) eq "id"
"Class::DBI"->can($name) $name eq 'id' join(' ', $class->primary_columns) eq 'id'
Truth table50%2/4
A and not(B && C)
A= "Class::DBI"->can($name)B= $name eq 'id'C= join(' ', $class->primary_columns) eq 'id'
ABCresult
0XX0
1110
10X1
1101
369 if Class::DBI->can($name)
370 and not($name eq "id" and join(" ", $class->primary_columns) eq "id");
371 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
372 102 *{"$class\::$name"} = $method;
373 102 $class->_make_method(lc $name => $method);
374 }
375  
376 32 ▶sub accessor_name_for {
Subroutine accessor_name_for: called CC=2 SCAR=8.4
Pod documented
377 32 my ($class, $column) = @_;
378 32 ▶ if ($class->can('accessor_name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("accessor_name")) 0 32 32
379 0 warn "Use of 'accessor_name' is deprecated. Use 'accessor_name_for' instead\n";
380 0 return $class->accessor_name($column)
381 }
382 32 return $column->accessor;
383 }
384  
385 25 ▶sub mutator_name_for {
Subroutine mutator_name_for: called CC=2 SCAR=8.4
Pod documented
386 25 my ($class, $column) = @_;
387 25 ▶ if ($class->can('mutator_name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("mutator_name")) 0 25 25
388 0 warn "Use of 'mutator_name' is deprecated. Use 'mutator_name_for' instead\n";
389 0 return $class->mutator_name($column)
390 }
391 25 return $column->mutator;
392 }
393  
394 0 ▶sub autoupdate {
Subroutine autoupdate: not called CC=2 SCAR=17.9
Pod documented
395 0 my $proto = shift;
396 0 ▶ ref $proto ? $proto->_obj_autoupdate(@_) : $proto->_class_autoupdate(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 0 0 0
397 }
398  
399 0 ▶sub _obj_autoupdate {
Subroutine _obj_autoupdate: not called CC=3 SCAR=24.8
400 0 my ($self, $set) = @_;
401 0 my $class = ref $self;
402 0 ▶ $self->{__AutoCommit} = $set if defined $set;
Branch: 1 branch
BranchTrueFalseTotal
if defined $set 0 0 0
403 defined $self->{__AutoCommit}
404 ? $self->{__AutoCommit}
405 0 ▶ : $class->_class_autoupdate;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'__AutoCommit'} ? : 0 0 0
406 }
407  
408 0 ▶sub _class_autoupdate {
Subroutine _class_autoupdate: not called CC=2 SCAR=17.9
409 0 my ($class, $set) = @_;
410 0 ▶ $class->__AutoCommit($set) if defined $set;
Branch: 1 branch
BranchTrueFalseTotal
if defined $set 0 0 0
411 0 return $class->__AutoCommit;
412 }
413  
414 0 ▶sub make_read_only {
Subroutine make_read_only: not called CC=2 SCAR=17.9
Pod undocumented
415 0 my $proto = shift;
416 0 ▶ $proto->add_trigger("before_$_" => sub { _croak "$proto is read only" })
Subroutine __ANON__: not called CC=1 SCAR=6.9
417 0 foreach qw/create delete update/;
418 0 return $proto;
419 }
420  
421 0 ▶sub find_or_create {
Subroutine find_or_create: not called CC=3 SCAR=24.8
Pod documented
422 0 my $class = shift;
423 0 ▶ my $hash = ref $_[0] eq "HASH" ? shift: {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
424 0 my ($exists) = $class->search($hash);
425 0 ▶ return defined($exists) ? $exists : $class->insert($hash);
Branch: 1 branch
BranchTrueFalseTotal
defined $exists ? : 0 0 0
426 }
427  
428 0 ▶sub insert {
Subroutine insert: not called CC=6 SCAR=37.4
Pod documented
429 0 my $class = shift;
430 0 ▶ return $class->_croak("insert needs a hashref") unless ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 0 0
431 0 my $info = { %{ +shift } }; # make sure we take a copy
432  
433 0 my $data;
434 0 ▶ while (my ($k, $v) = each %$info) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$info) 0 0 0
435 my $col = $class->find_column($k)
436 0 ▶ || (List::Util::first { $_->mutator eq $k } $class->columns)
Subroutine __ANON__: not called CC=1 SCAR=6.9
437 0 ▶ || (List::Util::first { $_->accessor eq $k } $class->columns)
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/9
$class->find_column($k) || &List::Util::first(sub { $_->mutator eq $k; } , $class->columns) || &List::Util::first(sub { $_->accessor eq $k; } , $class->columns) || $class->_croak("$k is not a column of $class")
l!l&&r!l&&!r
000
$class->find_column($k) or &List::Util::first(sub { $_->mutator eq $k; } , $class->columns) or &List::Util::first(sub { $_->accessor eq $k; } , $class->columns)
l!l&&r!l&&!r
000
$class->find_column($k) or &List::Util::first(sub { $_->mutator eq $k; } , $class->columns)
l!l&&r!l&&!r
000
MC/DC0%0/4
$class->find_column($k) || &List::Util::first(sub { $_->mutator eq $k; } , $class->columns) || &List::Util::first(sub { $_->accessor eq $k; } , $class->columns) || $class->_croak("$k is not a column of $class")
$class->find_column($k) &List::Util::first(sub { $_->mutator eq $k; } , $class->columns) &List::Util::first(sub { $_->accessor eq $k; } , $class->columns) $class->_croak("$k is not a column of $class")
Truth table0%0/5
A or B or C || D
A= $class->find_column($k)B= &List::Util::first(sub { $_->mutator eq $k; } , $class->columns)C= &List::Util::first(sub { $_->accessor eq $k; } , $class->columns)D= $class->_croak("$k is not a column of $class")
ABCDresult
1XXX1
01XX1
001X1
00011
00000
438 || $class->_croak("$k is not a column of $class");
439 0 $data->{$col} = $v;
440 }
441  
442 0 $class->normalize_column_values($data);
443 0 $class->validate_column_values($data);
444 0 return $class->_insert($data);
445 }
446  
447 4 *create = \&insert;
448  
449 #----------------------------------------------------------------------
450 # Low Level Data Access
451 #----------------------------------------------------------------------
452  
453 0 ▶sub _attrs {
Subroutine _attrs: not called CC=1 SCAR=6.9
454 0 my ($self, @atts) = @_;
455 0 return @{$self}{@atts};
456 }
457 4 *_attr = \&_attrs;
458  
459 0 ▶sub _attribute_store {
Subroutine _attribute_store: not called CC=2 SCAR=17.9
460 0 my $self = shift;
461 0 ▶ my $vals = @_ == 1 ? shift: {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
462 0 my (@cols) = keys %$vals;
463 0 @{$self}{@cols} = @{$vals}{@cols};
464 }
465  
466 # If you override this method, you must use the same mechanism to log changes
467 # for future updates, as other parts of Class::DBI depend on it.
468 0 ▶sub _attribute_set {
Subroutine _attribute_set: not called CC=3 SCAR=24.8
469 0 my $self = shift;
470 0 ▶ my $vals = @_ == 1 ? shift: {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
471  
472 # We increment instead of setting to 1 because it might be useful to
473 # someone to know how many times a value has changed between updates.
474 0 for my $col (keys %$vals) { $self->{__Changed}{$col}++; }
475 0 $self->_attribute_store($vals);
476 }
477  
478 0 ▶sub _attribute_delete {
Subroutine _attribute_delete: not called CC=1 SCAR=6.9
479 0 my ($self, @attributes) = @_;
480 0 delete @{$self}{@attributes};
481 }
482  
483 0 ▶sub _attribute_exists {
Subroutine _attribute_exists: not called CC=1 SCAR=6.9
484 0 my ($self, $attribute) = @_;
485 0 exists $self->{$attribute};
486 }
487  
488 #----------------------------------------------------------------------
489 # Live Object Index (using weak refs if available)
490 #----------------------------------------------------------------------
491  
492 4 my %Live_Objects;
493 4 my $Init_Count = 0;
494  
495 0 ▶sub _init {
Subroutine _init: not called CC=3 SCAR=24.8
496 0 my $class = shift;
497 0 ▶ my $data = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
498 0 my $key = $class->_live_object_key($data);
499 0 ▶ return $Live_Objects{$key} || $class->_fresh_init($key => $data);
Condition0%0/3
$Live_Objects{$key} || $class->_fresh_init($key, $data)
l!l&&r!l&&!r
000
MC/DC0%0/2
$Live_Objects{$key} || $class->_fresh_init($key, $data)
$Live_Objects{$key} $class->_fresh_init($key, $data)
Truth table0%0/3
A || B
A= $Live_Objects{$key}B= $class->_fresh_init($key, $data)
ABresult
1X1
011
000
500 }
501  
502 0 ▶sub _fresh_init {
Subroutine _fresh_init: not called CC=4 SCAR=30.0
503 0 my ($class, $key, $data) = @_;
504 0 my $obj = bless {}, $class;
505 0 $obj->_attribute_store(%$data);
506  
507 # don't store it unless all keys are present
508 0 ▶ if ($key && $Weaken_Is_Available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key and $Class::DBI::Weaken_Is_Available) 0 0 0
Condition0%0/3
$key and $Class::DBI::Weaken_Is_Available
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $Class::DBI::Weaken_Is_Available
$key $Class::DBI::Weaken_Is_Available
Truth table0%0/3
A and B
A= $keyB= $Class::DBI::Weaken_Is_Available
ABresult
0X0
100
111
509 0 weaken($Live_Objects{$key} = $obj);
510  
511 # time to clean up your room?
512 0 ▶ $class->purge_dead_from_object_index
Branch: 1 branch
BranchTrueFalseTotal
if ++$Init_Count % $class->purge_object_index_every == 0 0 0 0
513 if ++$Init_Count % $class->purge_object_index_every == 0;
514 }
515 0 return $obj;
516 }
517  
518 0 ▶sub _live_object_key {
Subroutine _live_object_key: not called CC=3 SCAR=24.8
519 0 my ($me, $data) = @_;
520 0 ▶ my $class = ref($me) || $me;
Condition0%0/3
ref $me || $me
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $me || $me
ref $me $me
Truth table0%0/3
A || B
A= ref $meB= $me
ABresult
1X1
011
000
521 0 my @primary = $class->primary_columns;
522  
523 # no key unless all PK columns are defined
524 0 ▶ return "" unless @primary == grep defined $data->{$_}, @primary;
Branch: 1 branch
BranchTrueFalseTotal
unless @primary == grep(defined($data->{$_}), @primary) 0 0 0
525  
526 # create single unique key for this object
527 0 return join "\030", $class, map $_ . "\032" . $data->{$_}, sort @primary;
528 }
529  
530 0 ▶sub purge_dead_from_object_index {
Subroutine purge_dead_from_object_index: not called CC=1 SCAR=6.9
Pod undocumented
531 0 delete @Live_Objects{ grep !defined $Live_Objects{$_}, keys %Live_Objects };
532 }
533  
534 0 ▶sub remove_from_object_index {
Subroutine remove_from_object_index: not called CC=1 SCAR=6.9
Pod documented
535 0 my $self = shift;
536 0 my $obj_key = $self->_live_object_key({ $self->_as_hash });
537 0 delete $Live_Objects{$obj_key};
538 }
539  
540 5 ▶sub clear_object_index {
Subroutine clear_object_index: called CC=1 SCAR=0.0
Pod documented
541 5 %Live_Objects = ();
542 }
543  
544 #----------------------------------------------------------------------
545  
546 0 ▶sub _prepopulate_id {
Subroutine _prepopulate_id: not called CC=3 SCAR=24.8
547 0 my $self = shift;
548 0 my @primary_columns = $self->primary_columns;
549 0 ▶ return $self->_croak(
Branch: 1 branch
BranchTrueFalseTotal
if @primary_columns > 1 0 0 0
550 sprintf "Can't create %s object with null primary key columns (%s)",
551 ref $self, $self->_undefined_primary)
552 if @primary_columns > 1;
553 0 ▶ $self->_attribute_store($primary_columns[0] => $self->_next_in_sequence)
Branch: 1 branch
BranchTrueFalseTotal
if $self->sequence 0 0 0
554 if $self->sequence;
555 }
556  
557 0 ▶sub _insert {
Subroutine _insert: not called CC=6 SCAR=37.4
558 0 my ($proto, $data) = @_;
559 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
560  
561 0 my $self = $class->_init($data);
562 0 $self->call_trigger('before_create');
563 0 $self->call_trigger('deflate_for_create');
564  
565 0 ▶ $self->_prepopulate_id if $self->_undefined_primary;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_undefined_primary 0 0 0
566  
567 # Reinstate data
568 0 my ($real, $temp) = ({}, {});
569 0 foreach my $col (grep $self->_attribute_exists($_), $self->all_columns) {
570 0 ▶ ($class->has_real_column($col) ? $real : $temp)->{$col} =
Branch: 1 branch
BranchTrueFalseTotal
$class->has_real_column($col) ? : 0 0 0
571 $self->_attrs($col);
572 }
573 0 $self->_insert_row($real);
574  
575 0 my @primary_columns = $class->primary_columns;
576 $self->_attribute_store(
577 0 ▶ $primary_columns[0] => $real->{ $primary_columns[0] })
Branch: 1 branch
BranchTrueFalseTotal
if @primary_columns == 1 0 0 0
578 if @primary_columns == 1;
579  
580 0 delete $self->{__Changed};
581  
582 0 my %primary_columns;
583 0 @primary_columns{@primary_columns} = ();
584 0 my @discard_columns = grep !exists $primary_columns{$_}, keys %$real;
585 0 $self->call_trigger('create', discard_columns => \@discard_columns); # XXX
586  
587 # Empty everything back out again!
588 0 $self->_attribute_delete(@discard_columns);
589 0 $self->call_trigger('after_create');
590 0 return $self;
591 }
592  
593 0 ▶sub _next_in_sequence {
Subroutine _next_in_sequence: not called CC=1 SCAR=6.9
594 0 my $self = shift;
595 0 return $self->sql_Nextval($self->sequence)->select_val;
596 }
597  
598 0 ▶sub _auto_increment_value {
Subroutine _auto_increment_value: not called CC=4 SCAR=30.0
599 0 my $self = shift;
600 0 my $dbh = $self->db_Main;
601  
602 # Try to do this in a standard method. Fall back to MySQL/SQLite
603 # specific versions. TODO remove these when last_insert_id is more
604 # widespread.
605 # Note: I don't believe the last_insert_id can be zero. We need to
606 # switch to defined() checks if it can.
607 my $id = $dbh->last_insert_id(undef, undef, $self->table, undef) # std
608 || $dbh->{mysql_insertid} # mysql
609 0 ▶ || eval { $dbh->func('last_insert_rowid') }
Branch: 1 branch
BranchTrueFalseTotal
my $id = $dbh->last_insert_id(undef, undef, $self->table, undef) || $dbh->{"mysql_insertid"} || eval { do { $dbh->func("last_insert_rowid") } } or $self->_croak("Can't get last insert id") 0 0 0
Condition0%0/6
$dbh->last_insert_id(undef, undef, $self->table, undef) || $dbh->{'mysql_insertid'} || eval { do { $dbh->func('last_insert_rowid') } }
l!l&&r!l&&!r
000
$dbh->last_insert_id(undef, undef, $self->table, undef) or $dbh->{"mysql_insertid"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$dbh->last_insert_id(undef, undef, $self->table, undef) || $dbh->{'mysql_insertid'} || eval { do { $dbh->func('last_insert_rowid') } }
$dbh->last_insert_id(undef, undef, $self->table, undef) $dbh->{"mysql_insertid"} eval { do { $dbh->func('last_insert_rowid') } }
Truth table0%0/4
A or B || C
A= $dbh->last_insert_id(undef, undef, $self->table, undef)B= $dbh->{"mysql_insertid"}C= eval { do { $dbh->func('last_insert_rowid') } }
ABCresult
1XX1
01X1
0011
0000
610 or $self->_croak("Can't get last insert id");
611 0 return $id;
612 }
613  
614 0 ▶sub _insert_row {
Subroutine _insert_row: not called CC=4 SCAR=30.0
615 0 my $self = shift;
616 0 my $data = shift;
617 0 eval {
618 0 my @columns = keys %$data;
619 0 my $sth = $self->sql_MakeNewObj(
620 join(', ', @columns),
621 join(', ', map $self->_column_placeholder($_), @columns),
622 );
623 0 $self->_bind_param($sth, \@columns);
624 0 $sth->execute(values %$data);
625 0 my @primary_columns = $self->primary_columns;
626 $data->{ $primary_columns[0] } = $self->_auto_increment_value
627 if @primary_columns == 1
628 0 ▶ && !defined $data->{ $primary_columns[0] };
Branch: 1 branch
BranchTrueFalseTotal
if @primary_columns == 1 and not defined $$data{$primary_columns[0]} 0 0 0
Condition0%0/3
@primary_columns == 1 and not defined $$data{$primary_columns[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
@primary_columns == 1 and not defined $$data{$primary_columns[0]}
@primary_columns == 1 not defined $$data{$primary_columns[0]}
Truth table0%0/3
A and not(B)
A= @primary_columns == 1B= not defined $$data{$primary_columns[0]}
ABresult
0X0
100
111
629 };
630 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
631 0 my $class = ref $self;
632 0 return $self->_db_error(
633 msg => "Can't insert new $class: $@",
634 err => $@,
635 method => 'insert'
636 );
637 }
638 0 return 1;
639 }
640  
641 0 ▶sub _bind_param {
Subroutine _bind_param: not called CC=4 SCAR=30.0
642 0 my ($class, $sth, $keys) = @_;
643 0 ▶ my $datatype = $class->__data_type or return;
Branch: 1 branch
BranchTrueFalseTotal
my $datatype = $class->__data_type or return 0 0 0
644 0 for my $i (0 .. $#$keys) {
645 0 ▶ if (my $type = $datatype->{ $keys->[$i] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $$datatype{$keys->[$i]}) 0 0 0
646 0 $sth->bind_param($i + 1, undef, $type);
647 }
648 }
649 }
650  
651 1 ▶sub retrieve {
Subroutine retrieve: called CC=8 SCAR=39.8
Pod documented
652 1 my $class = shift;
653 1 ▶ my @primary_columns = $class->primary_columns
Branch: 1 branch
BranchTrueFalseTotal
my(@primary_columns) = $class->primary_columns or return $class->_croak("Can't retrieve unless primary columns are defined") 0 1 1
654 or return $class->_croak(
655 "Can't retrieve unless primary columns are defined");
656 0 my %key_value;
657 0 ▶ if (@_ == 1 && @primary_columns == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and @primary_columns == 1) { } 0 0 0
Condition0%0/3
@_ == 1 and @primary_columns == 1
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and @primary_columns == 1
@_ == 1 @primary_columns == 1
Truth table0%0/3
A and B
A= @_ == 1B= @primary_columns == 1
ABresult
0X0
100
111
658 0 my $id = shift;
659 0 ▶ return unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
660 0 ▶ return $class->_croak("Can't retrieve a reference") if ref($id);
Branch: 1 branch
BranchTrueFalseTotal
if ref $id 0 0 0
661 0 $key_value{ $primary_columns[0] } = $id;
662 } else {
663 0 %key_value = @_;
664 0 ▶ $class->_croak(
Branch: 1 branch
BranchTrueFalseTotal
if keys %key_value < @primary_columns 0 0 0
665 "$class->retrieve(@_) parameters don't include values for all primary key columns (@primary_columns)"
666 )
667 if keys %key_value < @primary_columns;
668 }
669 0 my @rows = $class->search(%key_value);
670 0 ▶ $class->_carp("$class->retrieve(@_) selected " . @rows . " rows")
Branch: 1 branch
BranchTrueFalseTotal
if @rows > 1 0 0 0
671 if @rows > 1;
672 0 return $rows[0];
673 }
674  
675 # Get the data, as a hash, but setting certain values to whatever
676 # we pass. Used by copy() and move().
677 # This can take either a primary key, or a hashref of all the columns
678 # to change.
679 0 ▶sub _data_hash {
Subroutine _data_hash: not called CC=4 SCAR=30.0
680 0 my $self = shift;
681 0 my %data = $self->_as_hash;
682 0 my @primary_columns = $self->primary_columns;
683 0 delete @data{@primary_columns};
684 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
685 0 my $arg = shift;
686 0 ▶ unless (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $arg) 0 0 0
687 0 ▶ $self->_croak("Need hash-ref to edit copied column values")
Branch: 1 branch
BranchTrueFalseTotal
unless @primary_columns == 1 0 0 0
688 unless @primary_columns == 1;
689 0 $arg = { $primary_columns[0] => $arg };
690 }
691 0 @data{ keys %$arg } = values %$arg;
692 }
693 0 return \%data;
694 }
695  
696 0 ▶sub _as_hash {
Subroutine _as_hash: not called CC=1 SCAR=6.9
697 0 my $self = shift;
698 0 my @columns = $self->all_columns;
699 0 my %data;
700 0 @data{@columns} = $self->get(@columns);
701 0 return %data;
702 }
703  
704 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
705 0 my $self = shift;
706 0 return $self->insert($self->_data_hash(@_));
707 }
708  
709 #----------------------------------------------------------------------
710 # CONSTRUCT
711 #----------------------------------------------------------------------
712  
713 0 ▶sub construct {
Subroutine construct: not called CC=2 SCAR=17.9
Pod undocumented
714 0 my ($proto, $data) = @_;
715 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
716 0 my $self = $class->_init($data);
717 0 $self->call_trigger('select');
718 0 return $self;
719 }
720  
721 0 ▶sub move {
Subroutine move: not called CC=3 SCAR=24.8
Pod documented
722 0 my ($class, $old_obj, @data) = @_;
723 0 $class->_carp("move() is deprecated. If you really need it, "
724 . "you should tell me quickly so I can abandon my plan to remove it.");
725 0 ▶ return $old_obj->_croak("Can't move to an unrelated class")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa(ref $old_obj) or $old_obj->isa($class) 0 0 0
Condition0%0/3
$class->isa(ref $old_obj) or $old_obj->isa($class)
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->isa(ref $old_obj) or $old_obj->isa($class)
$class->isa(ref $old_obj) $old_obj->isa($class)
Truth table0%0/3
A or B
A= $class->isa(ref $old_obj)B= $old_obj->isa($class)
ABresult
1X1
011
000
726 unless $class->isa(ref $old_obj)
727 or $old_obj->isa($class);
728 0 return $class->insert($old_obj->_data_hash(@data));
729 }
730  
731 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
732 0 my $self = shift;
733 0 ▶ return $self->_search_delete(@_) if not ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
734 0 $self->remove_from_object_index;
735 0 $self->call_trigger('before_delete');
736  
737 0 eval { $self->sql_DeleteMe->execute($self->id) };
738 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
739 0 return $self->_db_error(
740 msg => "Can't delete $self: $@",
741 err => $@,
742 method => 'delete'
743 );
744 }
745 0 $self->call_trigger('after_delete');
746 0 undef %$self;
747 0 bless $self, 'Class::DBI::Object::Has::Been::Deleted';
748 0 return 1;
749 }
750  
751 0 ▶sub _search_delete {
Subroutine _search_delete: not called CC=2 SCAR=17.9
752 0 my ($class, @args) = @_;
753 0 $class->_carp(
754 "Delete as class method is deprecated. Use search and delete_all instead."
755 );
756 0 my $it = $class->search_like(@args);
757 0 ▶ while (my $obj = $it->next) { $obj->delete }
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $it->next) 0 0 0
758 0 return 1;
759 }
760  
761 # Return the placeholder to be used in UPDATE and INSERT queries.
762 # Overriding this is deprecated in favour of
763 # __PACKAGE__->find_column('entered')->placeholder('IF(1, CURDATE(), ?));
764  
765 0 ▶sub _column_placeholder {
Subroutine _column_placeholder: not called CC=1 SCAR=6.9
766 0 my ($self, $column) = @_;
767 0 return $self->find_column($column)->placeholder;
768 }
769  
770 0 ▶sub update {
Subroutine update: not called CC=4 SCAR=30.0
Pod documented
771 0 my $self = shift;
772 0 ▶ my $class = ref($self)
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or return $self->_croak("Can't call update as a class method") 0 0 0
773 or return $self->_croak("Can't call update as a class method");
774  
775 0 $self->call_trigger('before_update');
776 0 ▶ return -1 unless my @changed_cols = $self->is_changed;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@changed_cols) = $self->is_changed 0 0 0
777 0 $self->call_trigger('deflate_for_update');
778 0 my @primary_columns = $self->primary_columns;
779 0 my $sth = $self->sql_update($self->_update_line);
780 0 $class->_bind_param($sth, \@changed_cols);
781 0 my $rows = eval { $sth->execute($self->_update_vals, $self->id); };
782 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
783 0 return $self->_db_error(
784 msg => "Can't update $self: $@",
785 err => $@,
786 method => 'update'
787 );
788 }
789  
790 # enable this once new fixed DBD::SQLite is released:
791 0 if (0 and $rows != 1) { # should always only update one row
792 $self->_croak("Can't update $self: row not found") if $rows == 0;
793 $self->_croak("Can't update $self: updated more than one row");
794 }
795  
796 0 $self->call_trigger('after_update', discard_columns => \@changed_cols);
797  
798 # delete columns that changed (in case adding to DB modifies them again)
799 0 $self->_attribute_delete(@changed_cols);
800 0 delete $self->{__Changed};
801 0 return 1;
802 }
803  
804 0 ▶sub _update_line {
Subroutine _update_line: not called CC=1 SCAR=6.9
805 0 my $self = shift;
806 0 join(', ', map "$_ = " . $self->_column_placeholder($_), $self->is_changed);
807 }
808  
809 0 ▶sub _update_vals {
Subroutine _update_vals: not called CC=1 SCAR=6.9
810 0 my $self = shift;
811 0 $self->_attrs($self->is_changed);
812 }
813  
814 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
815 0 my ($self) = shift;
816 0 ▶ if (my @changed = $self->is_changed) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@changed) = $self->is_changed) 0 0 0
817 0 my $class = ref $self;
818 0 $self->_carp("$class $self destroyed without saving changes to "
819 . join(', ', @changed));
820 }
821 }
822  
823 0 ▶sub discard_changes {
Subroutine discard_changes: not called CC=2 SCAR=17.9
Pod documented
824 0 my $self = shift;
825 0 ▶ return $self->_croak("Can't discard_changes while autoupdate is on")
Branch: 1 branch
BranchTrueFalseTotal
if $self->autoupdate 0 0 0
826 if $self->autoupdate;
827 0 $self->_attribute_delete($self->is_changed);
828 0 delete $self->{__Changed};
829 0 return 1;
830 }
831  
832 # We override the get() method from Class::Accessor to fetch the data for
833 # the column (and associated) columns from the database, using the _flesh()
834 # method. We also allow get to be called with a list of keys, instead of
835 # just one.
836  
837 0 ▶sub get {
Subroutine get: not called CC=4 SCAR=30.0
Pod documented
838 0 my $self = shift;
839 0 ▶ return $self->_croak("Can't fetch data as class method") unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
840  
841 0 my @cols = $self->_find_columns(@_);
842 0 ▶ return $self->_croak("Can't get() nothing!") unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 0 0 0
843  
844 0 ▶ if (my @fetch_cols = grep !$self->_attribute_exists($_), @cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@fetch_cols) = grep((!$self->_attribute_exists($_)), @cols)) 0 0 0
845 0 $self->_flesh($self->__grouper->groups_for(@fetch_cols));
846 }
847  
848 0 return $self->_attrs(@cols);
849 }
850  
851 0 ▶sub _flesh {
Subroutine _flesh: not called CC=2 SCAR=17.9
852 0 my ($self, @groups) = @_;
853 0 my @real = grep $_ ne "TEMP", @groups;
854 0 ▶ if (my @want = grep !$self->_attribute_exists($_),
Branch: 1 branch
BranchTrueFalseTotal
if (my(@want) = grep((!$self->_attribute_exists($_)), $self->__grouper->columns_in(@real))) 0 0 0
855 $self->__grouper->columns_in(@real)) {
856 0 my %row;
857 0 @row{@want} = $self->sql_Flesh(join ", ", @want)->select_row($self->id);
858 0 $self->_attribute_store(\%row);
859 0 $self->call_trigger('select');
860 }
861 0 return 1;
862 }
863  
864 # We also override set() from Class::Accessor so we can keep track of
865 # changes, and either write to the database now (if autoupdate is on),
866 # or when update() is called.
867 0 ▶sub set {
Subroutine set: not called CC=5 SCAR=34.0
Pod documented
868 0 my $self = shift;
869 0 my $column_values = {@_};
870  
871 0 $self->normalize_column_values($column_values);
872 0 $self->validate_column_values($column_values);
873  
874 0 ▶ while (my ($column, $value) = each %$column_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$column_values) 0 0 0
875 0 ▶ my $col = $self->find_column($column) or die "No such column: $column\n";
Branch: 1 branch
BranchTrueFalseTotal
my $col = $self->find_column($column) or die "No such column: $column\n" 0 0 0
876 0 $self->_attribute_set($col => $value);
877  
878 # $self->SUPER::set($column, $value);
879  
880 0 eval { $self->call_trigger("after_set_$column") }; # eg inflate
881 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
882 0 $self->_attribute_delete($column);
883 0 return $self->_croak("after_set_$column trigger error: $@", err => $@);
884 }
885 }
886  
887 0 ▶ $self->update if $self->autoupdate;
Branch: 1 branch
BranchTrueFalseTotal
if $self->autoupdate 0 0 0
888 0 return 1;
889 }
890  
891 0 ▶sub is_changed {
Subroutine is_changed: not called CC=1 SCAR=6.9
Pod documented
892 0 my $self = shift;
893 0 grep $self->has_real_column($_), keys %{ $self->{__Changed} };
894 }
895  
896 0 ▶sub any_changed { keys %{ shift->{__Changed} } }
Subroutine any_changed: not called CC=1 SCAR=6.9
Pod undocumented
897  
898 # By default do nothing. Subclasses should override if required.
899 #
900 # Given a hash ref of column names and proposed new values,
901 # edit the values in the hash if required.
902 # For insert $self is the class name (not an object ref).
903 0 ▶sub normalize_column_values {
Subroutine normalize_column_values: not called CC=1 SCAR=6.9
Pod undocumented
904 0 my ($self, $column_values) = @_;
905 }
906  
907 # Given a hash ref of column names and proposed new values
908 # validate that the whole set of new values in the hash
909 # is valid for the object in relation to its current values
910 # For insert $self is the class name (not an object ref).
911 0 ▶sub validate_column_values {
Subroutine validate_column_values: not called CC=4 SCAR=30.0
Pod undocumented
912 0 my ($self, $column_values) = @_;
913 0 my @errors;
914 0 foreach my $column (keys %$column_values) {
915 0 eval {
916 0 $self->call_trigger("before_set_$column", $column_values->{$column},
917 $column_values);
918 };
919 0 ▶ push @errors, $column => $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
920 }
921 0 ▶ return unless @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 0 0 0
922 0 $self->_croak(
923 "validate_column_values error: " . join(" ", @errors),
924 method => 'validate_column_values',
925 data => {@errors}
926 );
927 }
928  
929 # We override set_sql() from Ima::DBI so it has a default database connection.
930 36 ▶sub set_sql {
Subroutine set_sql: called CC=3 SCAR=11.0
Pod documented
931 36 my ($class, $name, $sql, $db, @others) = @_;
932 36 ▶ $db ||= 'Main';
Condition50%1/2
$db ||= "Main"
l!l
036
MC/DC0%0/1
$db ||= "Main"
$db
Truth table50%1/2
A
A= $db
Aresult
11
00
933 36 $class->SUPER::set_sql($name, $sql, $db, @others);
934 36 ▶ $class->_generate_search_sql($name) if $sql =~ /select/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /select/i 24 12 36
935 36 return 1;
936 }
937  
938 24 ▶sub _generate_search_sql {
Subroutine _generate_search_sql: called CC=2 SCAR=7.1
939 24 my ($class, $name) = @_;
940 24 my $method = "search_$name";
941 24 ▶ defined &{"$class\::$method"}
Branch: 1 branch
BranchTrueFalseTotal
defined &{"${class}::$method";} and return $class->_carp("$method() already exists") 0 24 24
942 and return $class->_carp("$method() already exists");
943 24 my $sql_method = "sql_$name";
944 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
945 24 ▶ *{"$class\::$method"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=2.6
946 0 my ($class, @args) = @_;
947 0 return $class->sth_to_objects($name, \@args);
948 24 };
949 }
950  
951 0 ▶sub dbi_commit { my $proto = shift; $proto->SUPER::commit(@_); }
Subroutine dbi_commit: not called CC=1 SCAR=6.9
Pod documented
952 0 ▶sub dbi_rollback { my $proto = shift; $proto->SUPER::rollback(@_); }
Subroutine dbi_rollback: not called CC=1 SCAR=6.9
Pod documented
953  
954 #----------------------------------------------------------------------
955 # Constraints / Triggers
956 #----------------------------------------------------------------------
957  
958 0 ▶sub constrain_column {
Subroutine constrain_column: not called CC=7 SCAR=40.3
Pod documented
959 0 my $class = shift;
960 0 ▶ my $col = $class->find_column(+shift)
Branch: 1 branch
BranchTrueFalseTotal
my $col = $class->find_column(shift()) or return $class->_croak("constraint_column needs a valid column") 0 0 0
961 or return $class->_croak("constraint_column needs a valid column");
962 0 ▶ my $how = shift
Branch: 1 branch
BranchTrueFalseTotal
my $how = shift() or return $class->_croak("constrain_column needs a constraint") 0 0 0
963 or return $class->_croak("constrain_column needs a constraint");
964 0 ▶ if (ref $how eq "ARRAY") {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $how eq 'ARRAY') { } 0 0 0
elsif (ref $how eq 'Regexp') { } 0 0 0
elsif (ref $how eq 'CODE') { } 0 0 0
965 0 my %hash = map { $_ => 1 } @$how;
966 0 ▶ $class->add_constraint(list => $col => sub { exists $hash{ +shift } });
Subroutine __ANON__: not called CC=1 SCAR=6.9
967 } elsif (ref $how eq "Regexp") {
968 0 ▶ $class->add_constraint(regexp => $col => sub { shift =~ $how });
Subroutine __ANON__: not called CC=1 SCAR=6.9
969 } elsif (ref $how eq "CODE") {
970 $class->add_constraint(
971 0 ▶ code => $col => sub { local $_ = $_[0]; $how->($_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
972 } else {
973 0 my $try_method = sprintf '_constrain_by_%s', $how->moniker;
974 0 ▶ if (my $dispatch = $class->can($try_method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dispatch = $class->can($try_method)) { } 0 0 0
975 0 $class->$dispatch($col => ($how, @_));
976 } else {
977 0 $class->_croak("Don't know how to constrain $col with $how");
978 }
979 }
980 }
981  
982 0 ▶sub add_constraint {
Subroutine add_constraint: not called CC=6 SCAR=37.4
Pod undocumented
983 0 my $class = shift;
984 0 ▶ $class->_invalid_object_method('add_constraint()') if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
985 0 ▶ my $name = shift or return $class->_croak("Constraint needs a name");
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return $class->_croak("Constraint needs a name") 0 0 0
986 0 ▶ my $column = $class->find_column(+shift)
Branch: 1 branch
BranchTrueFalseTotal
my $column = $class->find_column(shift()) or return $class->_croak("Constraint $name needs a valid column") 0 0 0
987 or return $class->_croak("Constraint $name needs a valid column");
988 0 ▶ my $code = shift
Branch: 1 branch
BranchTrueFalseTotal
my $code = shift() or return $class->_croak("Constraint $name needs a code reference") 0 0 0
989 or return $class->_croak("Constraint $name needs a code reference");
990 0 ▶ return $class->_croak("Constraint $name '$code' is not a code reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
991 unless ref($code) eq "CODE";
992  
993 0 $column->is_constrained(1);
994 $class->add_trigger(
995 0 ▶ "before_set_$column" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
996 0 my ($self, $value, $column_values) = @_;
997 0 ▶ $code->($value, $self, $column, $column_values)
Branch: 1 branch
BranchTrueFalseTotal
&$code($value, $self, $column, $column_values) or return $self->_croak("$class $column fails '${name}' constraint with '${value}'", "method", "before_set_$column", "exception_type", "constraint_failure", "data", {"column", $column, "value", $value, "constraint_name", $name}) 0 0 0
998 or return $self->_croak(
999 "$class $column fails '$name' constraint with '$value'",
1000 method => "before_set_$column",
1001 exception_type => 'constraint_failure',
1002 data => {
1003 column => $column,
1004 value => $value,
1005 constraint_name => $name,
1006 }
1007 );
1008 }
1009 0 );
1010 }
1011  
1012 14 ▶sub add_trigger {
Subroutine add_trigger: called CC=4 SCAR=15.6
Pod undocumented
1013 14 my ($self, $name, @args) = @_;
1014 14 ▶ return $self->_croak("on_setting trigger no longer exists")
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "on_setting" 0 14 14
1015 if $name eq "on_setting";
1016 14 ▶ $self->_carp(
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "create" or $name eq "delete" 0 14 14
Condition33%1/3
$name eq "create" or $name eq "delete"
l!l&&r!l&&!r
0014
MC/DC0%0/2
$name eq "create" or $name eq "delete"
$name eq "create" $name eq "delete"
Truth table33%1/3
A or B
A= $name eq "create"B= $name eq "delete"
ABresult
1X1
011
000
1017 "$name trigger deprecated: use before_$name or after_$name instead")
1018 if ($name eq "create" or $name eq "delete");
1019 14 $self->SUPER::add_trigger($name => @args);
1020 }
1021  
1022 #----------------------------------------------------------------------
1023 # Inflation
1024 #----------------------------------------------------------------------
1025  
1026 4 ▶sub add_relationship_type {
Subroutine add_relationship_type: called CC=2 SCAR=6.9
Pod documented
1027 4 my ($self, %rels) = @_;
1028 4 ▶ while (my ($name, $class) = each %rels) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %rels) 12 4 16
1029 12 $self->_require_class($class);
1030 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1031 12 ▶ *{"$self\::$name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1032 5 my $proto = shift;
1033 5 $class->set_up($name => $proto => @_);
1034 12 };
1035 }
1036 }
1037  
1038 5 ▶sub _extend_meta {
Subroutine _extend_meta: called CC=2 SCAR=7.0
1039 5 my ($class, $type, $subtype, $val) = @_;
1040 5 ▶ my %hash = %{ Clone::clone($class->__meta_info || {}) };
Condition50%1/2
$class->__meta_info || {}
l!l
50
MC/DC0%0/1
$class->__meta_info || {}
$class->__meta_info
Truth table50%1/2
A
A= $class->__meta_info
Aresult
11
00
1041 5 $hash{$type}->{$subtype} = $val;
1042 5 $class->__meta_info(\%hash);
1043 }
1044  
1045 4 ▶sub meta_info {
Subroutine meta_info: called CC=3 SCAR=11.3
Pod undocumented
1046 4 my ($class, $type, $subtype) = @_;
1047 4 my $meta = $class->__meta_info;
1048 4 ▶ return $meta unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 3 4
1049 3 ▶ return $meta->{$type} unless $subtype;
Branch: 1 branch
BranchTrueFalseTotal
unless $subtype 3 0 3
1050 0 return $meta->{$type}->{$subtype};
1051 }
1052  
1053 0 ▶sub _simple_bless {
Subroutine _simple_bless: not called CC=1 SCAR=6.9
1054 0 my ($class, $pri) = @_;
1055 0 return $class->_init({ $class->primary_column => $pri });
1056 }
1057  
1058 0 ▶sub _deflated_column {
Subroutine _deflated_column: not called CC=10 SCAR=47.0
1059 0 my ($self, $col, $val) = @_;
1060 0 ▶ $val ||= $self->_attrs($col) if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 0 0
Condition0%0/3
$val ||= $self->_attrs($col)
l!l&&r!l&&!r
000
MC/DC0%0/2
$val ||= $self->_attrs($col)
$val $self->_attrs($col)
Truth table0%0/3
A ||= B
A= $valB= $self->_attrs($col)
ABresult
1X1
011
000
1061 0 ▶ return $val unless ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 0 0 0
1062 0 ▶ my $meta = $self->meta_info(has_a => $col) or return $val;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $self->meta_info("has_a", $col) or return $val 0 0 0
1063 0 my ($a_class, %meths) = ($meta->foreign_class, %{ $meta->args });
1064 0 ▶ if (my $deflate = $meths{'deflate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $deflate = $meths{"deflate"}) 0 0 0
1065 0 ▶ $val = $val->$deflate(ref $deflate eq 'CODE' ? $self : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $deflate eq 'CODE' ? : 0 0 0
1066 0 ▶ return $val unless ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 0 0 0
1067 }
1068 0 ▶ return $self->_croak("Can't deflate $col: $val is not a $a_class")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($val, $a_class) 0 0 0
1069 unless UNIVERSAL::isa($val, $a_class);
1070 0 ▶ return $val->id if UNIVERSAL::isa($val => 'Class::DBI');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($val, "Class::DBI") 0 0 0
1071 0 return "$val";
1072 }
1073  
1074 #----------------------------------------------------------------------
1075 # SEARCH
1076 #----------------------------------------------------------------------
1077  
1078 0 ▶sub retrieve_all { shift->sth_to_objects('RetrieveAll') }
Subroutine retrieve_all: not called CC=1 SCAR=6.9
Pod documented
1079  
1080 0 ▶sub retrieve_from_sql {
Subroutine retrieve_from_sql: not called CC=1 SCAR=6.9
Pod documented
1081 0 my ($class, $sql, @vals) = @_;
1082 0 $sql =~ s/^\s*(WHERE)\s*//i;
1083 0 return $class->sth_to_objects($class->sql_Retrieve($sql), \@vals);
1084 }
1085  
1086 4 ▶sub add_searcher {
Subroutine add_searcher: called CC=3 SCAR=11.1
Pod undocumented
1087 4 my ($self, %rels) = @_;
1088 4 ▶ while (my ($name, $class) = each %rels) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %rels) 4 4 8
1089 4 $self->_require_class($class);
1090 4 ▶ $self->_croak("$class is not a valid Searcher")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("run_search") 0 4 4
1091 unless $class->can('run_search');
1092 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1093 4 ▶ *{"$self\::$name"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
1094 0 $class->new(@_)->run_search;
1095 4 };
1096 }
1097 }
1098  
1099 # This should really be its own Search subclass. But the _do_search
1100 # version has been publicised as the way to do this. We need to
1101 # deprecate this eventually.
1102  
1103 0 ▶sub search_like { shift->_do_search(LIKE => @_) }
Subroutine search_like: not called CC=1 SCAR=6.9
Pod documented
1104  
1105 0 ▶sub _do_search {
Subroutine _do_search: not called CC=1 SCAR=6.9
1106 0 my ($class, $type, @args) = @_;
1107 0 $class->_require_class('Class::DBI::Search::Basic');
1108 0 my $search = Class::DBI::Search::Basic->new($class, @args);
1109 0 $search->type($type);
1110 0 $search->run_search;
1111 }
1112  
1113 #----------------------------------------------------------------------
1114 # CONSTRUCTORS
1115 #----------------------------------------------------------------------
1116  
1117 0 ▶sub add_constructor {
Subroutine add_constructor: not called CC=3 SCAR=20.0
Pod documented
1118 0 my ($class, $method, $fragment) = @_;
1119 0 ▶ return $class->_croak("constructors needs a name") unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 0 0
1120 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1121 0 my $meth = "$class\::$method";
1122 return $class->_carp("$method already exists in $class")
1123 0 ▶ if *$meth{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if *$meth{"CODE"} 0 0 0
1124 0 ▶ *$meth = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1125 0 my $self = shift;
1126 0 $self->sth_to_objects($self->sql_Retrieve($fragment), \@_);
1127 0 };
1128 }
1129  
1130 0 ▶sub sth_to_objects {
Subroutine sth_to_objects: not called CC=6 SCAR=37.4
Pod undocumented
1131 0 my ($class, $sth, $args) = @_;
1132 0 ▶ $class->_croak("sth_to_objects needs a statement handle") unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 0 0
1133 0 ▶ unless (UNIVERSAL::isa($sth => "DBI::st")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($sth, "DBI::st")) 0 0 0
1134 0 my $meth = "sql_$sth";
1135 0 $sth = $class->$meth();
1136 }
1137 0 my (%data, @rows);
1138 0 eval {
1139 0 ▶ $sth->execute(@$args) unless $sth->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $sth->{"Active"} 0 0 0
1140 0 $sth->bind_columns(\(@data{ @{ $sth->{NAME_lc} } }));
1141 0 ▶ push @rows, {%data} while $sth->fetch;
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetch and do { push @rows, {%data} } 0 0 0
1142 };
1143 0 ▶ return $class->_croak("$class can't $sth->{Statement}: $@", err => $@)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1144 if $@;
1145 0 return $class->_ids_to_objects(\@rows);
1146 }
1147 4 *_sth_to_objects = \&sth_to_objects;
1148  
1149 0 ▶sub _my_iterator {
Subroutine _my_iterator: not called CC=1 SCAR=6.9
1150 0 my $self = shift;
1151 0 my $class = $self->iterator_class;
1152 0 $self->_require_class($class);
1153 0 return $class;
1154 }
1155  
1156 0 ▶sub _ids_to_objects {
Subroutine _ids_to_objects: not called CC=3 SCAR=24.8
1157 0 my ($class, $data) = @_;
1158 0 ▶ return $#$data + 1 unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
1159 0 ▶ return map $class->construct($_), @$data if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
1160 0 return $class->_my_iterator->new($class => $data);
1161 }
1162  
1163 #----------------------------------------------------------------------
1164 # SINGLE VALUE SELECTS
1165 #----------------------------------------------------------------------
1166  
1167 0 ▶sub _single_row_select {
Subroutine _single_row_select: not called CC=1 SCAR=6.9
1168 0 my ($self, $sth, @args) = @_;
1169 0 Carp::confess("_single_row_select is deprecated in favour of select_row");
1170 0 return $sth->select_row(@args);
1171 }
1172  
1173 0 ▶sub _single_value_select {
Subroutine _single_value_select: not called CC=1 SCAR=6.9
1174 0 my ($self, $sth, @args) = @_;
1175 0 $self->_carp("_single_value_select is deprecated in favour of select_val");
1176 0 return $sth->select_val(@args);
1177 }
1178  
1179 0 ▶sub count_all { shift->sql_single("COUNT(*)")->select_val }
Subroutine count_all: not called CC=1 SCAR=6.9
Pod documented
1180  
1181 0 ▶sub maximum_value_of {
Subroutine maximum_value_of: not called CC=1 SCAR=6.9
Pod documented
1182 0 my ($class, $col) = @_;
1183 0 $class->sql_single("MAX($col)")->select_val;
1184 }
1185  
1186 0 ▶sub minimum_value_of {
Subroutine minimum_value_of: not called CC=1 SCAR=6.9
Pod documented
1187 0 my ($class, $col) = @_;
1188 0 $class->sql_single("MIN($col)")->select_val;
1189 }
1190  
1191 0 ▶sub _unique_entries {
Subroutine _unique_entries: not called CC=1 SCAR=6.9
1192 0 my ($class, %tmp) = shift;
1193 0 return grep !$tmp{$_}++, @_;
1194 }
1195  
1196 0 ▶sub _invalid_object_method {
Subroutine _invalid_object_method: not called CC=1 SCAR=6.9
1197 0 my ($self, $method) = @_;
1198 0 $self->_carp(
1199 "$method should be called as a class method not an object method");
1200 }
1201  
1202 #----------------------------------------------------------------------
1203 # misc stuff
1204 #----------------------------------------------------------------------
1205  
1206 5 ▶sub _extend_class_data {
Subroutine _extend_class_data: called CC=2 SCAR=6.9
1207 5 my ($class, $struct, $key, $value) = @_;
1208 5 ▶ my %hash = %{ $class->$struct() || {} };
Condition100%2/2
$class->$struct || {}
l!l
32
MC/DC100%1/1
$class->$struct || {}
$class->$struct
Truth table100%2/2
A
A= $class->$struct
Aresult
11
00
1209 5 $hash{$key} = $value;
1210 5 $class->$struct(\%hash);
1211 }
1212  
1213 4 my %required_classes; # { required_class => class_that_last_required_it, ... }
1214  
1215 23 ▶sub _require_class {
Subroutine _require_class: called CC=6 SCAR=21.2
1216 23 my ($self, $load_class) = @_;
1217 23 ▶ $required_classes{$load_class} ||= my $for_class = ref($self) || $self;
Condition33%2/6
$required_classes{$load_class} ||= (my $for_class = ref $self || $self)
l!l&&r!l&&!r
0230
ref $self || $self
l!l&&r!l&&!r
0230
MC/DC0%0/3
$required_classes{$load_class} ||= (my $for_class = ref $self || $self)
$required_classes{$load_class} ref $self $self
Truth table0%0/4
A ||= B || C
A= $required_classes{$load_class}B= ref $selfC= $self
ABCresult
1XX1
01X1
0011
0000
1218  
1219 # return quickly if class already exists
1220 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1221 23 ▶ return if exists ${"$load_class\::"}{ISA};
Branch: 1 branch
BranchTrueFalseTotal
if exists do { "${load_class}::" }->{"ISA"} 4 19 23
1222 19 (my $load_module = $load_class) =~ s!::!/!g;
1223 19 ▶ return if eval { require "$load_module.pm" };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require "$load_module.pm" } } 19 0 19
1224  
1225 # Only ignore "Can't locate" errors for the specific module we're loading
1226 0 ▶ return if $@ =~ /^Can't locate \Q$load_module\E\.pm /;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /^Can't locate \Q$load_module\E\.pm / 0 0 0
1227  
1228 # Other fatal errors (syntax etc) must be reported (as per base.pm).
1229 0 chomp $@;
1230  
1231 # This error message prefix is especially handy when dealing with
1232 # classes that are being loaded by other classes recursively.
1233 # The final message shows the path, e.g.:
1234 # Foo can't load Bar: Bar can't load Baz: syntax error at line ...
1235 0 $self->_croak("$for_class can't load $load_class: $@");
1236 }
1237  
1238 0 ▶sub _check_classes { # may automatically call from CHECK block in future
Subroutine _check_classes: not called CC=3 SCAR=24.8
1239 0 ▶ while (my ($load_class, $by_class) = each %required_classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %required_classes) 0 0 0
1240 0 ▶ next if $load_class->isa("Class::DBI");
Branch: 1 branch
BranchTrueFalseTotal
if $load_class->isa("Class::DBI") 0 0 0
1241 0 $by_class->_croak(
1242 "Class $load_class used by $by_class has not been loaded");
1243 }
1244 }
1245  
1246 4 1;
1247  
1248 __END__
« DBI/Query.pm ↑ Summary DBI/Search/Basic.pm »