lib/DBIx/EAV/Table.pm

filter: Statementstmt 87.6% 64 / 73 Branchbran 61.5% 16 / 26 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 12 / 12 Podpod 85.7% 6 / 7 totaltotal 78.0% 103 / 132 SCAR 31.1
CC
19
Cov
79%
delete
10.6
insert
4.2
BUILD
3.2
CRAP 22.5
« lib/DBIx/EAV/Schema.pm ↑ Summary Build »
1 package DBIx::EAV::Table;
2  
3 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use constant {
6 SQL_DEBUG => $ENV{DBIX_EAV_TRACE}
7 10 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 my $sql = SQL::Abstract->new;
10  
11 10 has '_dbh', is => 'ro', required => 1, init_arg => 'dbh';
12 10 has 'name', is => 'ro', required => 1;
13 10 has 'columns', is => 'ro', required => 1;
14 10 has 'tenant_id', is => 'ro';
15  
16  
17 sub BUILD {
18 108 ▶ my $self = shift;
Subroutine BUILD: called CC=3 SCAR=11.7
Pod undocumented
19  
20 108 ▶ die sprintf "Error instantiating table '%s': tenant_id is required!"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_column("tenant_id") and not defined $self->tenant_id 0 108 108
Condition66%2/3
$self->has_column("tenant_id") and not defined $self->tenant_id
!ll&&!rl&&r
97110
MC/DC0%0/2
$self->has_column("tenant_id") and not defined $self->tenant_id
$self->has_column("tenant_id") not defined $self->tenant_id
Truth table66%2/3
A and not(B)
A= $self->has_column("tenant_id")B= not defined $self->tenant_id
ABresult
0X0
100
111
21 if $self->has_column('tenant_id') && !defined $self->tenant_id;
22 }
23  
24  
25  
26 sub has_column {
27 1099 ▶ my ($self, $name) = @_;
Subroutine has_column: called CC=3 SCAR=11.0
Pod documented
28 1099 foreach (@{$self->columns}) {
29 3856 ▶ return 1 if $_ eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $name 138 3718 3856
30 }
31 961 0;
32 }
33  
34 sub select {
35 468 ▶ my ($self, $where) = @_;
Subroutine select: called CC=2 SCAR=7.0
Pod documented
36 468 ▶ $where //= {};
Condition50%1/2
$where //= {}
l!l
4680
MC/DC0%0/1
$where //= {}
$where
Truth table50%1/2
A
A= $where
Aresult
11
00
37 468 $where = $self->_mangle_where($where);
38  
39 468 my ($stmt, @bind) = $sql->select($self->name.' AS me', $self->columns, $where);
40 468 my ($rv, $sth) = $self->_do($stmt, \@bind);
41 468 $sth;
42 }
43  
44 sub select_one {
45 459 ▶ my ($self, $where) = @_;
Subroutine select_one: called CC=1 SCAR=0.0
Pod documented
46 459 $self->select($where)->fetchrow_hashref;
47 }
48  
49 sub insert {
50 475 ▶ my ($self, $data) = @_;
Subroutine insert: called CC=4 SCAR=14.3
Pod documented
51  
52 475 ▶ $data->{tenant_id} = $self->tenant_id
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_column("tenant_id") 39 436 475
53 if $self->has_column('tenant_id');
54  
55 475 my ($stmt, @bind) = $sql->insert($self->name, $data);
56 475 my ($rv, $sth) = $self->_do($stmt, \@bind);
57  
58 475 ▶ if ($rv == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 1) { } 475 0 475
59 475 ▶ return $self->_dbh->last_insert_id(undef, undef, undef, undef) || 1;
Condition50%1/2
$self->_dbh->last_insert_id(undef, undef, undef, undef) || 1
l!l
4750
MC/DC0%0/1
$self->_dbh->last_insert_id(undef, undef, undef, undef) || 1
$self->_dbh->last_insert_id(undef, undef, undef, undef)
Truth table50%1/2
A
A= $self->_dbh->last_insert_id(undef, undef, undef, undef)
Aresult
11
00
60 }
61 else {
62 0 $rv;
63 }
64  
65 }
66  
67 sub update {
68 3 ▶ my ($self, $data, $where) = @_;
Subroutine update: called CC=1 SCAR=0.0
Pod documented
69 3 $where = $self->_mangle_where($where);
70  
71 3 my ($stmt, @bind) = $sql->update($self->name, $data, $where);
72 3 my ($rv, $sth) = $self->_do($stmt, \@bind);
73 3 $rv;
74 }
75  
76 sub delete {
77 109 ▶ my ($self, $where, $opts) = @_;
Subroutine delete: called CC=7 SCAR=23.6
Pod documented
78 109 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
0109
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
79  
80 109 ▶ my $stmt = $opts->{join} ? sprintf("DELETE me FROM %s AS me", $self->name)
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'join'} ? : 0 109 109
81 : sprintf("DELETE FROM %s", $self->name);
82  
83 # JOIN
84 109 ▶ while (my ($table, $spec) = each %{ $opts->{join} || {} }) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$opts->{"join"} or {};}) 0 109 109
$opts->{"join"} or {} 109 0 109
85  
86 0 my ($join_criteria, @bind) = $sql->where($spec);
87 0 ▶ while ( (my $offset = index($join_criteria, '?')) > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $offset = index($join_criteria, "?")) > -1) 0 0 0
88 0 my $val = shift @bind;
89 0 substr($join_criteria, $offset, 1, $val);
90 }
91 0 $join_criteria =~ s/^\s*WHERE//;
92 0 $join_criteria =~ s/\btheir\./$table./g;
93 0 $stmt .= " INNER JOIN $table ON $join_criteria";
94 }
95  
96 # WHERE
97 109 my ($where_part, @bind);
98 109 ▶ if ($where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where) 45 64 109
99 45 $where = $self->_mangle_where($where);
100 45 ($where_part, @bind) = $sql->where($where);
101 45 $stmt .= " $where_part";
102 }
103  
104 109 my ($rv, $sth) = $self->_do($stmt, \@bind);
105 109 $rv;
106 }
107  
108 sub _mangle_where {
109 516 ▶ my ($self, $where) = @_;
Subroutine _mangle_where: called CC=3 SCAR=11.2
110  
111 516 ▶ return $where unless $self->has_column('tenant_id');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_column("tenant_id") 428 88 516
112  
113 88 ▶ if (ref $where eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $where eq 'HASH') { } 88 0 88
114 88 $where->{tenant_id} = $self->tenant_id;
115 }
116 else {
117 0 $where = { -and => [ tenant_id => $self->tenant_id, $where ] };
118 }
119  
120 88 $where;
121 }
122  
123  
124  
125 sub _do {
126 1149 ▶ my ($self, $stmt, $bind) = @_;
Subroutine _do: called CC=3 SCAR=11.2
127  
128 1149 if (SQL_DEBUG) {
129 my $i = 0;
130 printf STDERR "$stmt: %s\n",
131 join(' ', map { $i++.'='.$_ } @$bind);
132 }
133  
134 1149 my $sth = $self->_dbh->prepare($stmt);
135 1149 ▶ my $rv = $sth->execute(ref $bind eq 'ARRAY' ? @$bind : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $bind eq 'ARRAY' ? : 1149 0 1149
136 1149 ▶ die $sth->errstr unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 1149 1149
137  
138 1149 return ($rv, $sth);
139 }
140  
141  
142  
143  
144  
145 10 1;
146  
147 __END__
« lib/DBIx/EAV/Schema.pm ↑ Summary Build »