Declarative/Row.pm

filter: Statementstmt 15.0% 11 / 73 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 42.8% 3 / 7 Podpod 100% 2 / 2 totaltotal 10.6% 16 / 150 CC 22 SCAR 43.2
CC
22
Cov
9%
commit
132.0
__create_where
72.0
delete
12.0
CRAP 75.1
« Declarative/JRow.pm ↑ Summary Declarative/Schema.pm »
1 package DBIx::ORM::Declarative::Row;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 @ISA = qw(DBIx::ORM::Declarative::Table);
8  
9 # Mark a row for deletion
10 sub delete
11 0 ▶{
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
12 0 my ($self) = @_;
13 0 ▶ carp "delete: not a class method" and return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
Condition0%0/5
ref $self || carp('delete: not a class method') && (return)
l!l&&r!l&&!r
000
carp("delete: not a class method") and return
!ll
00
MC/DC0%0/2
ref $self || carp('delete: not a class method') && (return)
ref $self carp("delete: not a class method")
Truth table0%0/3
A || B
A= ref $selfB= carp("delete: not a class method")
ABresult
1X1
011
000
14 0 $self->{__deleted} = 1;
15 0 return $self;
16 }
17  
18 # Set the data items for this item
19 sub __set_data
20 0 ▶{
Subroutine __set_data: not called CC=3 SCAR=24.8
21 0 my ($self, @data) = @_;
22 0 ▶ carp "not a class method" and return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
Condition0%0/5
ref $self || carp('not a class method') && (return)
l!l&&r!l&&!r
000
carp("not a class method") and return
!ll
00
MC/DC0%0/2
ref $self || carp('not a class method') && (return)
ref $self carp("not a class method")
Truth table0%0/3
A || B
A= ref $selfB= carp("not a class method")
ABresult
1X1
011
000
23  
24 0 @{$self->{__data}}{$self->_column_sql_names} = @data;
25 0 return $self;
26 }
27  
28 # How do we find this item?
29 sub __create_where
30 0 ▶{
Subroutine __create_where: not called CC=8 SCAR=42.8
31 0 my ($self) = @_;
32 0 ▶ carp "not a class method" and return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
Condition0%0/5
ref $self || carp('not a class method') && (return)
l!l&&r!l&&!r
000
carp("not a class method") and return
!ll
00
MC/DC0%0/2
ref $self || carp('not a class method') && (return)
ref $self carp("not a class method")
Truth table0%0/3
A || B
A= ref $selfB= carp("not a class method")
ABresult
1X1
011
000
33  
34 # create the where clause, if we haven't already
35 0 ▶ if(not exists $self->{__whereinfo})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__whereinfo"}) 0 0 0
36 {
37 # Column info
38 0 my @cols = $self->_columns;
39 0 my %name2sql = $self->_column_map;
40 0 my @binds;
41 my @wclause;
42  
43 # We use either the first set of unique keys, or everything
44 0 my @un = $self->_unique_keys;
45 0 my @colv;
46 0 ▶ @colv = map { $name2sql{$_} } @{$un[0]} if @un;
Branch: 1 branch
BranchTrueFalseTotal
if @un 0 0 0
47 0 ▶ @colv = map { $_->{sql_name} } @cols unless @colv;
Branch: 1 branch
BranchTrueFalseTotal
unless @colv 0 0 0
48  
49 # Walk the list of columns
50 0 for my $k (@colv)
51 {
52 # If the data is defined, we use it. Otherwise, we use IS NULL
53 0 ▶ if(defined($self->{__data}{$k}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'__data'}{$k}) { } 0 0 0
54 {
55 0 push @wclause, "$k=?";
56 0 push @binds, $self->{__data}{$k};
57 }
58 else
59 {
60 0 push @wclause, "$k IS NULL";
61 }
62 }
63  
64 # Save the where clause and bind values
65 0 @{$self->{__whereinfo}} = (join(' AND ', @wclause), @binds);
66 }
67  
68 0 return @{$self->{__whereinfo}};
69 }
70  
71 # Save changes to the database
72 sub commit
73 0 ▶{
Subroutine commit: not called CC=11 SCAR=48.8
Pod documented
74 0 my ($self) = @_;
75  
76 # Parameter checking
77 0 ▶ carp "commit: not a class method" and return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
Condition0%0/5
ref $self || carp('commit: not a class method') && (return)
l!l&&r!l&&!r
000
carp("commit: not a class method") and return
!ll
00
MC/DC0%0/2
ref $self || carp('commit: not a class method') && (return)
ref $self carp("commit: not a class method")
Truth table0%0/3
A || B
A= ref $selfB= carp("commit: not a class method")
ABresult
1X1
011
000
78 0 my $handle = $self->handle;
79 0 ▶ carp "can't commit without a database handle" and return unless $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 0 0 0
Condition0%0/5
$handle || carp(q[can't commit without a database handle]) && (return)
l!l&&r!l&&!r
000
carp("can't commit without a database handle") and return
!ll
00
MC/DC0%0/2
$handle || carp(q[can't commit without a database handle]) && (return)
$handle carp("can't commit without a database handle")
Truth table0%0/3
A || B
A= $handleB= carp("can't commit without a database handle")
ABresult
1X1
011
000
80 0 ▶ return $self unless $self->{__deleted} or $self->{__dirty};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__deleted"} or $self->{"__dirty"} 0 0 0
Condition0%0/3
$self->{"__deleted"} or $self->{"__dirty"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"__deleted"} or $self->{"__dirty"}
$self->{"__deleted"} $self->{"__dirty"}
Truth table0%0/3
A or B
A= $self->{"__deleted"}B= $self->{"__dirty"}
ABresult
1X1
011
000
81  
82 0 my ($where, @binds) = $self->__create_where;
83 0 my $sql;
84  
85 # Create the SQL command
86 0 ▶ if($self->{__deleted})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'__deleted'}) { } 0 0 0
87 {
88 0 $sql = "DELETE FROM " . $self->_sql_name . " WHERE $where";
89 }
90 else
91 {
92 0 my @cols = $self->_columns;
93 0 $sql = "UPDATE " . $self->_sql_name . " SET ";
94 0 $sql .= join(',', map { $_->{sql_name} . '=?' } @cols);
95 0 unshift @binds, map { $self->{__data}{$_->{sql_name}} } @cols;
96  
97 0 $sql .= " WHERE $where";
98 }
99  
100 0 ▶ unshift @binds, undef if @binds; # Avoid DBI lossage
Branch: 1 branch
BranchTrueFalseTotal
if @binds 0 0 0
101
102 # Execute the SQL
103 0 ▶ if(not $handle->do($sql, @binds))
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->do($sql, @binds)) 0 0 0
104 {
105 0 carp "Database error: ", $handle->errstr;
106 0 $self->__do_rollback;
107 0 return;
108 }
109  
110 # Clean up the object
111 0 delete $self->{__dirty};
112 0 delete $self->{__whereinfo};
113  
114 # If we've deleted this data, we're no longer a row object
115 0 ▶ if($self->{__deleted})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'__deleted'}) { } 0 0 0
116 {
117 0 delete $self->{__data};
118 0 delete $self->{__deleted};
119 0 bless $self, $self->_class;
120 }
121 else
122 {
123 # Reload the "where" clause
124 0 $self->__create_where
125 }
126  
127 # Commit and return
128 0 local ($SIG{__WARN__}) = $self->w__noop;
129 0 $handle->commit;
130 0 return $self;
131 }
132 1 1;
133  
134 __END__
« Declarative/JRow.pm ↑ Summary Declarative/Schema.pm »