lib/IMDB/Local/DB/BaseObject.pm

filter: Statementstmt 14.1% 22 / 155 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 35.0% 7 / 20 Podpod 0.0% 0 / 10 totaltotal 11.2% 29 / 259 SCAR 72.9
CC
44
Cov
10%
initHandle
132.0
populateUsingKeys
56.0
_updateInDB
56.0
CRAP 1465.8
« lib/IMDB/Local/Title.pm ↑ Summary lib/IMDB/Local/DB.pm »
1  
2 package IMDB::Local::DB::BaseObject;
3  
4 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 IMDB::Local::DB::BaseObject - handy wrapper for objects from the database
12  
13 =head1 VERSION
14  
15 Version 0.01
16  
17 =cut
18  
19 1 our $VERSION = '0.01';
20  
21  
22 =head1 SYNOPSIS
23  
24 =head1 SUBROUTINES/METHODS
25  
26 =head2 new
27  
28 =cut
29  
30 1 ▶use IMDB::Local::DB::BaseObjectAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use base qw(IMDB::Local::DB::BaseObjectAccessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 use Class::MethodMaker
34 1 [ scalar=> [qw/imdbdb db_table db_key db_key2 db_key3/],
35 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 sub initHandle
38 {
39 0 ▶ my ($self, $table, @keys)=@_;
Subroutine initHandle: not called CC=11 SCAR=48.8
Pod undocumented
40  
41 #print STDERR "init $self, $table, $keys[0]\n";
42 # if no imdbdb handle was given in new() call, then use last handle created
43 0 ▶ if ( !defined($self->imdbdb) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->imdbdb) 0 0 0
44 0 carp "imdbdb handle missing";
45 }
46  
47 0 ▶ if ( !defined($self->imdbdb->column_info($table)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->imdbdb->column_info($table)) 0 0 0
48 0 die "unable to locate table $table";
49 }
50  
51 0 $self->db_table($table);
52 0 $self->db_key($keys[0]);
53  
54 0 ▶ if ( scalar(@keys) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys > 1) 0 0 0
55 0 $self->db_key2($keys[1]);
56 0 ▶ if ( scalar(@keys) > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys > 2) 0 0 0
57 0 $self->db_key3($keys[2]);
58 }
59 }
60  
61 0 ▶ if ( !$self->db_columns_count() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->db_columns_count) 0 0 0
62 0 $self->db_columns_reset();
63 0 for my $t (@{$self->imdbdb->column_info($table)}) {
64 0 ▶ if ( $self->can('db_ignoredColumns') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("db_ignoredColumns")) 0 0 0
65 0 my $ignore=0;
66 0 for my $k ($self->db_ignoredColumns) {
67 0 ▶ if ( $k eq $t->{COLUMN_NAME} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq $t->{"COLUMN_NAME"}) 0 0 0
68 0 $ignore=1;
69 0 last;
70 }
71 }
72 0 ▶ next if ( $ignore );
Branch: 1 branch
BranchTrueFalseTotal
if $ignore 0 0 0
73 }
74 0 $self->db_columns_push($t->{COLUMN_NAME});
75 }
76 }
77 0 $self->add_accessors($self->db_columns);
78 0 return;
79 }
80  
81 sub _add_accessors
82 {
83 0 ▶ my $self=shift;
Subroutine _add_accessors: not called CC=2 SCAR=17.9
84 0 for (@_) {
85 0 carp("added $_");
86 }
87 0 $self->SUPER::add_accessors(@_);
88 }
89  
90 sub populate($$)
91 {
92 0 ▶ my ($self, $hash)=@_;
Subroutine populate: not called CC=2 SCAR=17.9
Pod undocumented
93  
94 0 for my $k (keys %$hash) {
95 #print "setting $k:".$hash->{$k}."\n";
96 0 $self->set($k, $hash->{$k});
97 }
98 0 return(1);
99 }
100  
101 sub populateUsingKeys
102 {
103 0 ▶ my ($self, @keys)=@_;
Subroutine populateUsingKeys: not called CC=7 SCAR=40.3
Pod undocumented
104  
105 0 ▶ if ( !defined($self->db_key) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->db_key) 0 0 0
106 0 die "invalid usage, missing db_key for $self";
107 }
108  
109 0 ▶ if ( !@keys ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@keys) 0 0 0
110 0 die "no key(s)";
111 }
112  
113 0 ▶ if ( defined($self->db_key2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->db_key2) { } 0 0 0
114 0 ▶ if ( scalar(@keys) < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys < 2) 0 0 0
115 0 carp "new called without ".$self->db_key2()." value given";
116 }
117 0 ▶ if ( defined($self->db_key3) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->db_key3) { } 0 0 0
118 0 ▶ if ( scalar(@keys) < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys < 3) 0 0 0
119 0 carp "new called without ".$self->db_key3()." value given";
120 }
121 0 $self->populateFromColumns(sprintf("%s='%s' AND %s='%s'", $self->db_key, $keys[0], $self->db_key2, $keys[1], $self->db_key3, $keys[2]));
122 }
123 else {
124 0 $self->populateFromColumns(sprintf("%s='%s' AND %s='%s'", $self->db_key, $keys[0], $self->db_key2, $keys[1]));
125 }
126 }
127 else {
128 0 $self->populateFromColumns(sprintf("%s='%s'", $self->db_key, $keys[0]));
129 }
130 }
131  
132 sub populateUsingKey($$)
133 {
134 0 ▶ my ($self, $key)=@_;
Subroutine populateUsingKey: not called CC=1 SCAR=6.9
Pod undocumented
135 0 return $self->populateUsingKeys($key);
136 }
137  
138 sub populateFromColumns($$)
139 {
140 0 ▶ my ($self, $where)=@_;
Subroutine populateFromColumns: not called CC=4 SCAR=30.0
Pod undocumented
141  
142 0 ▶ if ( !defined($self->db_table) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->db_table) 0 0 0
143 0 die "invalid usage, missing db_table for $self";
144 }
145  
146 0 my $query="SELECT ".join(',', $self->db_columns)." from ".$self->db_table;
147 0 ▶ if ( defined($where) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $where) 0 0 0
148 0 $query.=' WHERE '.$where;
149 }
150 #print STDERR "populating: $query\n";
151  
152 0 my $dbh=$self->imdbdb->dbh();
153 0 my $ref=$dbh->selectrow_hashref($query);
154 0 ▶ if ( !$ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 0 0 0
155 0 print STDERR "populating failed on: $query\n";
156 0 return(undef);
157 }
158  
159 # if ( $self->can('db_ignoredColumns') ) {
160 # #print STDERR "ignoring columns\n";
161 # for my $k ($self->db_ignoredColumns) {
162 # if ( $ref->{$k} ) {
163 # #print STDERR "ignoring column $k\n";
164 # delete($ref->{$k});
165 # }
166 # }
167 # }
168 #$self->add_accessors(keys %$ref);
169 0 $self->populate($ref);
170 }
171  
172 sub _className($)
173 {
174 0 ▶ my ($self)=@_;
Subroutine _className: not called CC=1 SCAR=6.9
175  
176 0 $self=~m/(IMDB::Local::[^=]+)/o;
177 0 return($1);
178 }
179  
180 sub toText($)
181 {
182 0 ▶ my ($self)=@_;
Subroutine toText: not called CC=3 SCAR=24.8
Pod undocumented
183
184 0 my $text='';
185  
186 0 $text.="class: ".$self->_className()."\n";
187 0 if ( 1 ) {
188 0 for my $field (sort $self->get_accessors()) {
189 0 my $v=$self->$field();
190 0 ▶ $v='undef' if (!defined($v));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
191 0 $text.="\t$field: $v\n";
192 }
193 }
194 else {
195 require Data::Dumper;
196
197 my $D=new Data::Dumper([$self]);
198 $D->Indent(1);
199 my $d=$D->Dump();
200 $d=~s/\n\s*\'__FIELDS__\' => \[[^\]]+\]//ogs;
201 #$d=~s/\'([^\']+)\' => \'([^\']+)\'/$1:$2/ogs;
202 #while ($d=~s/\n /\n /) {};
203 $text.="$d\n";
204 }
205 0 return($text);
206 }
207  
208 sub _updateInDB
209 {
210 0 ▶ my $self=shift;
Subroutine _updateInDB: not called CC=7 SCAR=40.3
211 0 my $args={@_};
212  
213 0 my $stmt="UPDATE ".$self->db_table." SET ";
214 0 for my $key (keys %{$args}) {
215 0 my $value=$args->{$key};
216 0 ▶ if ( !defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value) { } 0 0 0
217 0 $stmt.="$key=NULL, ";
218 }
219 else {
220 0 $stmt.="$key='$value', ";
221 }
222 }
223 0 $stmt=~s/, $//;
224  
225 0 ▶ if ( defined($self->db_key2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->db_key2) { } 0 0 0
226 0 ▶ if ( defined($self->db_key3) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->db_key3) { } 0 0 0
227 0 $stmt.=sprintf("WHERE %s='%s' AND %s='%s' AND %s='%s'", $self->db_key, $self->get($self->db_key),
228 $self->db_key2, $self->get($self->db_key2),
229 $self->db_key3, $self->get($self->db_key3));
230 }
231 else {
232 0 $stmt.=sprintf("WHERE %s='%s' AND %s='%s'", $self->db_key, $self->get($self->db_key),
233 $self->db_key2, $self->get($self->db_key2));
234 }
235 }
236 else {
237 0 $stmt.=sprintf("WHERE %s='%s'", $self->db_key, $self->get($self->db_key));
238 }
239  
240 0 my $dbh=$self->imdbdb->dbh();
241 #print STDERR "invoking: args=".$args."\n";
242 #print STDERR "invoking: args=".%{$args}."\n";
243 #print STDERR "invoking: ".$stmt."\n";
244  
245 0 $dbh->do($stmt);
246 0 ▶ if ( $dbh->err() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->err) 0 0 0
247 0 return(0);
248 }
249  
250 # update our fields so they match
251 0 for my $key (keys %{$args}) {
252 0 my $value=$args->{$key};
253 0 $self->$key($value);
254 }
255 0 return(1);
256 }
257  
258 sub update($@)
259 {
260 0 ▶ my $self=shift;
Subroutine update: not called CC=6 SCAR=37.4
Pod undocumented
261 0 my $args={@_};
262 0 my %updates;
263
264 0 for my $key (keys %{$args}) {
265 0 my $value=$args->{$key};
266 0 my $cur=$self->get($key);
267
268 0 ▶ if ( defined($cur) != defined($value) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $cur != defined $value) { } 0 0 0
elsif (defined $value and $cur ne $value) { } 0 0 0
269 0 $updates{$key}=$value;
270 }
271 # either both undefined or both defined
272 ▶ elsif ( defined($value) && $cur ne $value ) {
Condition0%0/3
defined $value and $cur ne $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and $cur ne $value
defined $value $cur ne $value
Truth table0%0/3
A and B
A= defined $valueB= $cur ne $value
ABresult
0X0
100
111
273 0 $updates{$key}=$value;
274 }
275 }
276 0 ▶ if ( %updates ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%updates) 0 0 0
277 0 return $self->_updateInDB(%updates);
278 }
279 0 return(0);
280 }
281  
282 sub newFromDB($)
283 {
284 0 ▶ my ($self)=@_;
Subroutine newFromDB: not called CC=3 SCAR=24.8
Pod undocumented
285  
286 0 my $class=$self->_className();
287  
288 0 ▶ if ( defined($self->db_key2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->db_key2) { } 0 0 0
289 0 ▶ if ( defined($self->db_key3) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->db_key3) { } 0 0 0
290 0 return new $class(imdbdb=>$self->imdbdb(),
291 $self->db_key()=>$self->get($self->db_key()),
292 $self->db_key2()=>$self->get($self->db_key2()),
293 $self->db_key3()=>$self->get($self->db_key3()));
294 }
295 else {
296 0 return new $class(imdbdb=>$self->imdbdb(),
297 $self->db_key()=>$self->get($self->db_key()),
298 $self->db_key2()=>$self->get($self->db_key2()));
299 }
300 }
301 else {
302 0 return new $class(imdbdb=>$self->imdbdb(),
303 $self->db_key()=>$self->get($self->db_key()));
304 }
305 }
306  
307 sub delete($)
308 {
309 0 ▶ my ($self)=@_;
Subroutine delete: not called CC=3 SCAR=24.8
Pod undocumented
310  
311 0 ▶ if ( defined($self->db_key2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->db_key2) { } 0 0 0
312 0 ▶ if ( defined($self->db_key3) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->db_key3) { } 0 0 0
313 0 $self->imdbdb->execute("DELETE from ".$self->db_table()." WHERE ".
314 $self->db_key()."='".$self->get($self->db_key())."' AND ".
315 $self->db_key2()."='".$self->get($self->db_key2())."' AND ".
316 $self->db_key3()."='".$self->get($self->db_key3())."'");
317 }
318 else {
319 0 $self->imdbdb->execute("DELETE from ".$self->db_table()." WHERE ".
320 $self->db_key()."='".$self->get($self->db_key())."' AND ".
321 $self->db_key2()."='".$self->get($self->db_key2())."'");
322 }
323 }
324 else {
325 0 $self->imdbdb->execute("DELETE from ".$self->db_table()." WHERE ".
326 $self->db_key()."='".$self->get($self->db_key())."'");
327 }
328 }
329  
330 sub saveChanges($)
331 {
332 0 ▶ my ($self)=@_;
Subroutine saveChanges: not called CC=6 SCAR=37.4
Pod undocumented
333
334 0 my $clon=$self->newFromDB();
335 0 my %updates;
336  
337 0 for my $field ($self->get_accessors()) {
338 0 ▶ if ( defined($self->get($field)) != defined($clon->get($field)) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->get($field) != defined $clon->get($field)) { } 0 0 0
elsif (defined $self->get($field) and $self->get($field) ne $clon->get($field)) { } 0 0 0
339 0 $updates{$field}=$self->get($field);
340 }
341 # either both undefined or both defined
342 ▶ elsif ( defined($self->get($field)) && $self->get($field) ne $clon->get($field) ) {
Condition0%0/3
defined $self->get($field) and $self->get($field) ne $clon->get($field)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->get($field) and $self->get($field) ne $clon->get($field)
defined $self->get($field) $self->get($field) ne $clon->get($field)
Truth table0%0/3
A and B
A= defined $self->get($field)B= $self->get($field) ne $clon->get($field)
ABresult
0X0
100
111
343 0 $updates{$field}=$self->get($field);
344 }
345 }
346 0 ▶ if ( %updates ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%updates) 0 0 0
347 0 return $self->_updateInDB(%updates);
348 }
349 0 return(0);
350 }
351  
352 1 1;
« lib/IMDB/Local/Title.pm ↑ Summary lib/IMDB/Local/DB.pm »