blib/lib/SQL/Concat.pm

filter: Statementstmt 91.4% 96 / 105 Branchbran 77.2% 34 / 44 Conditioncond 72.2% 13 / 18 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 96.3% 26 / 27 Podpod 73.6% 14 / 19 totaltotal 84.2% 187 / 222 SCAR 35.2
CC
31
Cov
86%
BQ
12.0
_nonempty
8.0
concat
7.0
CRAP 33.9
↑ Summary Build »
1 package SQL::Concat;
2 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = "0.002";
8  
9 3 use MOP4Import::Base::Configure -as_base
10 , [fields => qw/sql bind/
11 , [sep => default => ' ']]
12 3 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use MOP4Import::Util qw/lexpand terse_dump/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub SQL {
16 28 ▶ MY->new(sep => ' ')->concat(@_);
Subroutine SQL: called CC=1 SCAR=0.0
Pod documented
17 }
18  
19 sub PAR {
20 1 ▶ SQL(@_)->paren;
Subroutine PAR: called CC=1 SCAR=0.0
Pod documented
21 }
22  
23 # Useful for OPT("limit ?", $limit, OPT("offset ?", $offset))
24 sub OPT {
25 10 ▶ my ($expr, $value, @rest) = @_;
Subroutine OPT: called CC=2 SCAR=6.9
Pod documented
26 10 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 4 6 10
27 6 SQL([$expr, $value], @rest);
28 }
29  
30 sub PFX {
31 6 ▶ my ($prefix, @items) = @_;
Subroutine PFX: called CC=3 SCAR=11.4
Pod documented
32 6 ▶ return unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 1 5 6
33 5 ▶ my @non_empty = _nonempty(@items)
Branch: 1 branch
BranchTrueFalseTotal
my(@non_empty) = _nonempty(@items) or return 5 0 5
34 or return;
35 0 SQL($prefix => @non_empty);
36 }
37  
38 sub _nonempty {
39 grep {
40 18 ▶ my MY $item = $_;
Subroutine _nonempty: called CC=8 SCAR=20.8
41 35 ▶ if (not defined $item
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $item && (ref $item || $item =~ /\S/)) { } 4 31 35
elsif (ref $item eq 'ARRAY') { } 6 25 31
elsif (ref $item and UNIVERSAL::can($item, 'is_empty') and $item->is_empty) { } 9 16 25
Condition83%5/6
defined $item && (ref $item || $item =~ /\S/)
!ll&&!rl&&r
0431
ref $item or $item =~ /\S/
l!l&&r!l&&!r
2744
MC/DC66%2/3
defined $item && (ref $item || $item =~ /\S/)
defined $item ref $item $item =~ /\S/
Truth table75%3/4
A && B or C
A= defined $itemB= ref $itemC= $item =~ /\S/
ABCresult
0XX0
1000
11X1
1011
42 or not ref $item and $item !~ /\S/) {
43 4 ();
44 } elsif (ref $item eq 'ARRAY') {
45 6 $item;
46 ▶ } elsif (ref $item and UNIVERSAL::can($item, 'is_empty')
Condition83%5/6
ref $item and UNIVERSAL::can($item, "is_empty") and $item->is_empty
!ll&&!rl&&r
4129
ref $item and UNIVERSAL::can($item, "is_empty")
!ll&&!rl&&r
4021
MC/DC66%2/3
ref $item and UNIVERSAL::can($item, "is_empty") and $item->is_empty
ref $item UNIVERSAL::can($item, "is_empty") $item->is_empty
Truth table75%3/4
A and B and C
A= ref $itemB= UNIVERSAL::can($item, "is_empty")C= $item->is_empty
ABCresult
0XX0
10X0
1100
1111
47 and $item->is_empty) {
48 9 ();
49 } else {
50 16 $item;
51 }
52 } @_;
53 }
54  
55 # sub SELECT {
56 # MY->new(sep => ' ')->concat(SELECT => @_);
57 # }
58  
59 sub CAT {
60 4 ▶ MY->concat_by(_wrap_ws($_[0]), @_[1..$#_]);
Subroutine CAT: called CC=1 SCAR=0.0
Pod documented
61 }
62  
63 sub CSV {
64 1 ▶ MY->concat_by(', ', @_);
Subroutine CSV: called CC=1 SCAR=0.0
Pod documented
65 }
66  
67 sub _wrap_ws {
68 4 ▶ my ($str) = @_;
Subroutine _wrap_ws: called CC=1 SCAR=0.0
69 4 $str =~ s/^(\S)/ $1/;
70 4 $str =~ s/(\S)\z/$1 /;
71 4 $str;
72 }
73  
74 # XXX: Do you want deep copy?
75 sub clone {
76 5 ▶ (my MY $item) = @_;
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
77 5 MY->new(%$item)
78 }
79  
80 sub is_empty {
81 21 ▶ (my MY $item) = @_;
Subroutine is_empty: called CC=1 SCAR=0.0
Pod documented
82 21 $item->{sql} !~ /\S/
83 }
84  
85 sub paren {
86 4 ▶ (my MY $item) = @_;
Subroutine paren: called CC=2 SCAR=7.6
Pod documented
87 4 ▶ if (_nonempty($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_nonempty($item)) { } 4 0 4
88 4 $item->format_by('(%s)')
89 } else {
90 0 return;
91 }
92 }
93  
94 sub paren_nl_indent {
95 1 ▶ (my MY $item, my $indent) = @_;
Subroutine paren_nl_indent: called CC=3 SCAR=12.5
Pod documented
96 1 ▶ if (_nonempty($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_nonempty($item)) { } 1 0 1
97 1 ▶ $item->format_by("(\n%s\n)", $indent || 2)
Condition50%1/2
$indent || 2
l!l
01
MC/DC0%0/1
$indent || 2
$indent
Truth table50%1/2
A
A= $indent
Aresult
11
00
98 } else {
99 0 return;
100 }
101 }
102  
103 sub format_by {
104 5 ▶ (my MY $item, my ($fmt, $indent)) = @_;
Subroutine format_by: called CC=2 SCAR=6.9
Pod documented
105 5 my MY $clone = $item->clone;
106 5 my $sql = $item->{sql};
107 5 ▶ $sql =~ s/^/" " x $indent/emg if $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $indent 1 4 5
108 5 $clone->{sql} = sprintf($fmt, $sql);
109 5 $clone;
110 }
111  
112 sub concat_by {
113 8 ▶ my MY $self = ref $_[0]
Subroutine concat_by: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 8 8
114 ? shift->configure(sep => shift)
115 : shift->new(sep => shift);
116 8 $self->concat(_nonempty(@_));
117 }
118  
119 #
120 # XXX: Could have more extension hook, hmm...
121 #
122 sub concat {
123 62 ▶ my MY $self = ref $_[0] ? shift : shift->new;
Subroutine concat: called CC=7 SCAR=19.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 26 62
124 62 ▶ if (defined $self->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"sql"}) 0 62 62
125 0 croak "concat() called after concat!";
126 }
127 62 my @sql;
128 62 $self->{bind} = [];
129 62 foreach my MY $item (@_) {
130 103 ▶ next unless defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 1 102 103
131 102 ▶ if (not ref $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $item) { } 57 45 102
132 57 push @sql, $item;
133 } else {
134  
135 45 ▶ $item = $self->of_bind_array($item)
Branch: 1 branch
BranchTrueFalseTotal
if ref $item eq "ARRAY" 23 22 45
136 if ref $item eq 'ARRAY';
137  
138 45 $item->validate_placeholders;
139  
140 42 push @sql, $item->{sql};
141 42 push @{$self->{bind}}, @{$item->{bind}};
142 }
143 }
144 59 $self->{sql} = join($self->{sep}, @sql);
145 59 $self
146 }
147  
148 sub of_bind_array {
149 23 ▶ (my MY $self, my $bind_array) = @_;
Subroutine of_bind_array: called CC=1 SCAR=0.0
Pod undocumented
150 23 my ($s, @b) = @$bind_array;
151 23 $self->new(sql => $s, bind => \@b);
152 }
153  
154 sub validate_placeholders {
155 45 ▶ (my MY $self) = @_;
Subroutine validate_placeholders: called CC=3 SCAR=11.0
Pod undocumented
156  
157 45 ▶ my $nbinds = $self->{bind} ? @{$self->{bind}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bind'} ? : 45 0 45
158  
159 45 ▶ unless ($self->count_placeholders == $nbinds) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->count_placeholders == $nbinds) 2 42 44
160 croak "SQL Placeholder mismatch! sql='$self->{sql}' bind="
161 2 .terse_dump($self->{bind});
162 }
163  
164 42 $self;
165 }
166  
167 sub count_placeholders {
168 44 ▶ (my MY $self) = @_;
Subroutine count_placeholders: called CC=2 SCAR=6.9
Pod undocumented
169  
170 44 ▶ unless (defined $self->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"sql"}) 1 43 44
171 1 croak "Undefined SQL Fragment!";
172 }
173  
174 43 $self->{sql} =~ tr,?,?,;
175 }
176  
177 sub as_sql_bind {
178 16 ▶ (my MY $self) = @_;
Subroutine as_sql_bind: called CC=2 SCAR=6.9
Pod documented
179 16 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 15 1 16
180 15 ($self->{sql}, lexpand($self->{bind}));
181 } else {
182 1 [$self->{sql}, lexpand($self->{bind})];
183 }
184 }
185  
186 sub sql_bind_pair {
187 2 ▶ (my MY $self) = @_;
Subroutine sql_bind_pair: called CC=4 SCAR=14.2
Pod documented
188 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 1 2
189 1 ▶ ($self->{sql}, $self->{bind} // [])
Condition50%1/2
$self->{"bind"} // []
l!l
10
MC/DC0%0/1
$self->{"bind"} // []
$self->{"bind"}
Truth table50%1/2
A
A= $self->{"bind"}
Aresult
11
00
190 } else {
191 1 ▶ [$self->{sql}, $self->{bind} // []];
Condition50%1/2
$self->{"bind"} // []
l!l
10
MC/DC0%0/1
$self->{"bind"} // []
$self->{"bind"}
Truth table50%1/2
A
A= $self->{"bind"}
Aresult
11
00
192 }
193 }
194  
195  
196 #========================================
197  
198 sub BQ {
199 0 ▶ if (ref $_[0]) {
Subroutine BQ: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 0 0 0
200 0 croak "Meaningless backtick for reference! ".terse_dump($_[0]);
201 }
202 0 ▶ if ($_[0] =~ /\`/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\`/) 0 0 0
203 0 croak "Can't quote by backtick: text contains backtick! $_[0]";
204 }
205 0 q{`}.$_[0].q{`}
206 }
207  
208 3 1;
209  
210  
211 __END__
↑ Summary Build »