EntityModel/Query/Condition.pm

filter: Statementstmt 86.1% 62 / 72 Branchbran 68.7% 22 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod 100% 6 / 6 totaltotal 79.6% 98 / 123 CC 18 SCAR 22.2
CC
18
Cov
79%
parseCondition
15.0
parseBranch
5.2
new
2.0
CRAP 9.2
« EntityModel/Query.pm ↑ Summary EntityModel/Model.pm »
1 package EntityModel::Query::Condition;
2 {
3 35 $EntityModel::Query::Condition::VERSION = '0.102';
4 }
5 35 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 _isa => [qw{EntityModel::Query::Base}],
7 'expr' => { type => 'string' },
8 'branch' => 'object'
9 35 };
10 35 ▶no if $] >= 5.017011, warnings => "experimental::smartmatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 EntityModel::Query::Condition - a condition clause for where, on etc.
15  
16 =head1 VERSION
17  
18 version 0.102
19  
20 =head1 SYNOPSIS
21  
22 =head1 DESCRIPTION
23  
24 =cut
25  
26 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
27 6 my $class = shift;
28 6 my $self = bless { }, $class;
29 6 ▶ $self->branch($self->parseCondition(ref $_[0] ? (@_) : ({ @_ })));
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 6 0 6
30 6 return $self;
31 }
32  
33 =head2 inlineSQL
34  
35 Returns the "inline SQL" representation for this condition. See L<EntityModel::Query> description for more details on what this means.
36  
37 =cut
38  
39 12 ▶sub inlineSQL {
Subroutine inlineSQL: called CC=1 SCAR=0.0
Pod documented
40 12 my $self = shift;
41 12 return [ $self->parseBranch($self->branch) ];
42 }
43  
44 =head2 parseCondition
45  
46 An array reference expands out as follows:
47  
48 [ x => 3 ]
49  
50 =cut
51  
52 6 ▶sub parseCondition {
Subroutine parseCondition: called CC=13 SCAR=27.1
Pod documented
53 6 my $self = shift;
54 6 my $data = shift;
55  
56 # Accept hashrefs, but turn them into arrayref by default.
57 6 ▶ $data = [ %$data ] if ref $data ~~ 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ~~ "HASH" 1 5 6
58  
59 6 my @list = @$data;
60  
61 6 my @node;
62 my @tree;
63 ITEM:
64 6 ▶ while(@list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 8 6 14
65 8 my $k = shift(@list);
66 8 my $item;
67 8 ▶ if(ref $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $k) { } 0 8 8
68 0 $item = $self->parseCondition($k);
69 } else {
70 8 my ($start, $directive) = $k =~ /^(.)(.*)$/;
71 # If we have a directive such as and, or, etc. then we're switching mode
72 8 ▶ if($start eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($start eq '-') { } 1 7 8
73 1 ▶ if($directive eq 'subquery') {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive eq 'subquery') { } 0 1 1
74 0 $item = $self->parseQuery(shift(@list));
75 } else {
76 1 push @node, $directive;
77 1 next ITEM;
78 }
79 } else {
80 7 ▶ $k = $self->quoteIdentifier($k) unless ref $k;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $k 7 0 7
81 7 my $v = shift(@list);
82 7 ▶ if(!ref($v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $v) { } 4 3 7
elsif (ref $v ~~ ['HASH', 'SCALAR']) { } 3 0 3
83 4 $item = [ $k, $v ];
84 } elsif(ref($v) ~~ [qw{HASH SCALAR}]) {
85 3 $item = [ $k, $v ];
86 } else {
87 0 $v = $self->parseCondition($v);
88 0 $item = [ $k, $v ];
89 }
90 }
91 }
92 7 ▶ if(@node) {
Branch: 1 branch
BranchTrueFalseTotal
if (@node) 1 6 7
93 1 my $prev = pop(@tree);
94 1 ▶ die 'no previous item?' unless $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev 0 1 1
95  
96 1 my $entry = {
97 op => join(' ', @node),
98 left => $prev,
99 right => $item
100 };
101 1 $item = $entry;
102 1 @node = ();
103 }
104 7 push @tree, $item;
105 }
106 6 ▶ return $tree[0] if @tree == 1 && ref $tree[0];
Branch: 1 branch
BranchTrueFalseTotal
if @tree == 1 and ref $tree[0] 6 0 6
Condition33%1/3
@tree == 1 and ref $tree[0]
!ll&&!rl&&r
006
MC/DC0%0/2
@tree == 1 and ref $tree[0]
@tree == 1 ref $tree[0]
Truth table33%1/3
A and B
A= @tree == 1B= ref $tree[0]
ABresult
0X0
100
111
107 0 return \@tree;
108 }
109  
110 =head2 parseBranch
111  
112 For a hashref, the following three items should be in the hash:
113  
114 =over 4
115  
116 =item * left - left node of the branch, this will be recursed into as appropriate
117  
118 =item * right - right node of the branch, will be recursed as required
119  
120 =item * op - the operation to perform, such as =, and, or, etc.
121  
122 =back
123  
124 An arrayref will use the '=' operation for all entries unless the second element is
125 a hashref, in which case the key will be used as the operation and the value as the
126 RHS.
127  
128 =cut
129  
130 16 ▶sub parseBranch {
Subroutine parseBranch: called CC=5 SCAR=16.4
Pod documented
131 16 my $self = shift;
132 16 my $item = shift;
133 16 my @query;
134 16 ▶ if(ref $item ~~ 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $item ~~ 'HASH') { } 2 14 16
elsif (ref $item ~~ 'ARRAY') { } 14 0 14
elsif (ref $item) { } 0 0 0
135 2 push @query, '(';
136 2 push @query, $self->parseBranch($item->{left});
137 2 push @query, ' ' . $item->{op} . ' ';
138 2 push @query, $self->parseBranch($item->{right});
139 2 push @query, ')';
140 } elsif(ref $item ~~ 'ARRAY') {
141 14 my ($k, $v) = @$item;
142 14 push @query, $k;
143 14 my $op = '=';
144 14 ▶ if(ref $v eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH") 6 8 14
145 6 ($op, $v) = %$v;
146 }
147 14 push @query, " $op ";
148 14 push @query, \$v;
149 } elsif(ref $item) {
150 0 push @query, $item;
151 } else {
152 0 push @query, $item;
153 }
154 16 return @query;
155 }
156  
157 =head2 parseQuery
158  
159 =cut
160  
161 0 ▶sub parseQuery {
Subroutine parseQuery: not called CC=1 SCAR=6.9
Pod documented
162 0 my $self = shift;
163 0 ' Query ';
164 }
165  
166 =head2 quoteIdentifier
167  
168 Convert an identifier to the quoted version.
169  
170 =cut
171  
172 7 ▶sub quoteIdentifier {
Subroutine quoteIdentifier: called CC=1 SCAR=0.2
Pod documented
173 7 my $self = shift;
174 7 my $k = shift;
175 7 return $k;
176 0 return '"'. $k . '"';
177 }
178  
179 35 1;
180  
181 __END__
« EntityModel/Query.pm ↑ Summary EntityModel/Model.pm »