Database/Util.pm

filter: Statementstmt 62.8% 61 / 97 Branchbran 42.8% 23 / 56 Conditioncond 53.8% 7 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 54.1% 13 / 24 Podpod 0.0% 0 / 2 totaltotal 52.5% 104 / 200 SCAR 50.2
CC
36
Cov
55%
_assertion_to_sql_FIELD_TO
20.0
_assertion_to_sql_AND
12.0
_assertion_to_sql_OR
12.0
CRAP 150.8
↑ Summary Database/Table.pm »
1 1 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package LINQ::Database::Util;
6  
7 1 our $AUTHORITY = 'cpan:TOBYINK';
8 1 our $VERSION = '0.000_002';
9  
10 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub selection_to_sql {
13 6 ▶ my ( $selection, $name_quoter ) = ( shift, @_ );
Subroutine selection_to_sql: called CC=8 SCAR=21.2
Pod undocumented
14
15 6 ▶ return unless blessed( $selection );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($selection) 0 6 6
16 6 ▶ return unless $selection->isa( 'LINQ::FieldSet::Selection' );
Branch: 1 branch
BranchTrueFalseTotal
unless $selection->isa("LINQ::FieldSet::Selection") 0 6 6
17 6 ▶ return if $selection->seen_asterisk;
Branch: 1 branch
BranchTrueFalseTotal
if $selection->seen_asterisk 0 6 6
18
19 $name_quoter ||= sub {
20 4 ▶ my $name = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
21 4 return sprintf( '"%s"', quotemeta( $name ) );
22 6 ▶ };
Condition66%2/3
$name_quoter ||= sub {
l!l&&r!l&&!r
330
MC/DC0%0/2
$name_quoter ||= sub { my $name = shift(); return sprintf('"%s"', quotemeta $name); }
$name_quoter sub { my $name = shift(); return sprintf('"%s"', quotemeta $name); }
Truth table66%2/3
A ||= B
A= $name_quoterB= sub { my $name = shift(); return sprintf('"%s"', quotemeta $name); }
ABresult
1X1
011
000
23
24 6 my @cols;
25 6 for my $field ( @{ $selection->fields } ) {
26 8 my $orig_name = $field->value;
27 8 my $aliased = $field->name;
28 8 ▶ return if ref( $orig_name );
Branch: 1 branch
BranchTrueFalseTotal
if ref $orig_name 0 8 8
29 # uncoverable branch true
30 8 ▶ return if !defined( $aliased );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aliased 0 8 8
31
32 8 push @cols, $name_quoter->( $orig_name );
33 } #/ for my $field ( @{ $self...})
34
35 6 return join( q[, ], @cols );
36 } #/ sub _sql_selection
37  
38 sub assertion_to_sql {
39 5 ▶ my ( $assertion, $name_quoter, $value_quoter ) = ( @_ );
Subroutine assertion_to_sql: called CC=8 SCAR=23.6
Pod undocumented
40
41 5 ▶ return unless blessed( $assertion );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($assertion) 0 5 5
42
43 $name_quoter ||= sub {
44 2 ▶ my $name = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
45 2 return sprintf( '"%s"', quotemeta( $name ) );
46 5 ▶ };
Condition66%2/3
$name_quoter ||= sub {
l!l&&r!l&&!r
230
MC/DC0%0/2
$name_quoter ||= sub { my $name = shift(); return sprintf('"%s"', quotemeta $name); }
$name_quoter sub { my $name = shift(); return sprintf('"%s"', quotemeta $name); }
Truth table66%2/3
A ||= B
A= $name_quoterB= sub { my $name = shift(); return sprintf('"%s"', quotemeta $name); }
ABresult
1X1
011
000
47
48 $value_quoter ||= sub {
49 2 ▶ my $name = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
50 2 return sprintf( '"%s"', quotemeta( $name ) );
51 5 ▶ };
Condition66%2/3
$value_quoter ||= sub {
l!l&&r!l&&!r
230
MC/DC0%0/2
$value_quoter ||= sub { my $name = shift(); return sprintf('"%s"', quotemeta $name); }
$value_quoter sub { my $name = shift(); return sprintf('"%s"', quotemeta $name); }
Truth table66%2/3
A ||= B
A= $value_quoterB= sub { my $name = shift(); return sprintf('"%s"', quotemeta $name); }
ABresult
1X1
011
000
52
53 5 ▶ if ( $assertion->isa( 'LINQ::FieldSet::Assertion::AND' ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($assertion->isa('LINQ::FieldSet::Assertion::AND')) { } 0 5 5
elsif ($assertion->isa('LINQ::FieldSet::Assertion::OR')) { } 0 5 5
elsif ($assertion->isa('LINQ::FieldSet::Assertion::NOT')) { } 0 5 5
elsif ($assertion->isa('LINQ::FieldSet::Assertion')) { } 5 0 5
54 0 return _assertion_to_sql_AND( $assertion, $name_quoter, $value_quoter );
55 }
56 elsif ( $assertion->isa( 'LINQ::FieldSet::Assertion::OR' ) ) {
57 0 return _assertion_to_sql_OR( $assertion, $name_quoter, $value_quoter );
58 }
59 elsif ( $assertion->isa( 'LINQ::FieldSet::Assertion::NOT' ) ) {
60 0 return _assertion_to_sql_NOT( $assertion, $name_quoter, $value_quoter );
61 }
62 elsif ( $assertion->isa( 'LINQ::FieldSet::Assertion' ) ) {
63 5 return _assertion_to_sql_FIELDSET( $assertion, $name_quoter, $value_quoter );
64 }
65 }
66  
67 sub _assertion_to_sql_AND {
68 0 ▶ my ( $assertion, $name_quoter, $value_quoter ) = ( @_ );
Subroutine _assertion_to_sql_AND: not called CC=3 SCAR=24.8
69
70 0 ▶ my $left = assertion_to_sql( $assertion->left, $name_quoter, $value_quoter )
Branch: 1 branch
BranchTrueFalseTotal
my $left = assertion_to_sql($assertion->left, $name_quoter, $value_quoter) or return 0 0 0
71 or return;
72 0 ▶ my $right = assertion_to_sql( $assertion->right, $name_quoter, $value_quoter )
Branch: 1 branch
BranchTrueFalseTotal
my $right = assertion_to_sql($assertion->right, $name_quoter, $value_quoter) or return 0 0 0
73 or return;
74
75 0 return "($left) AND ($right)";
76 }
77  
78 sub _assertion_to_sql_OR {
79 0 ▶ my ( $assertion, $name_quoter, $value_quoter ) = ( @_ );
Subroutine _assertion_to_sql_OR: not called CC=3 SCAR=24.8
80
81 0 ▶ my $left = assertion_to_sql( $assertion->left, $name_quoter, $value_quoter )
Branch: 1 branch
BranchTrueFalseTotal
my $left = assertion_to_sql($assertion->left, $name_quoter, $value_quoter) or return 0 0 0
82 or return;
83 0 ▶ my $right = assertion_to_sql( $assertion->right, $name_quoter, $value_quoter )
Branch: 1 branch
BranchTrueFalseTotal
my $right = assertion_to_sql($assertion->right, $name_quoter, $value_quoter) or return 0 0 0
84 or return;
85
86 0 return "($left) OR ($right)";
87 }
88  
89 sub _assertion_to_sql_NOT {
90 0 ▶ my ( $assertion, $name_quoter, $value_quoter ) = ( @_ );
Subroutine _assertion_to_sql_NOT: not called CC=2 SCAR=17.9
91
92 0 ▶ my $left = assertion_to_sql( $assertion->left, $name_quoter, $value_quoter )
Branch: 1 branch
BranchTrueFalseTotal
my $left = assertion_to_sql($assertion->left, $name_quoter, $value_quoter) or return 0 0 0
93 or return;
94
95 0 return "NOT ($left)";
96 }
97  
98 sub _assertion_to_sql_FIELDSET {
99 5 ▶ my ( $assertion, $name_quoter, $value_quoter ) = ( @_ );
Subroutine _assertion_to_sql_FIELDSET: called CC=3 SCAR=11.0
100
101 5 my @fields;
102 5 for my $field ( @{ $assertion->fields } ) {
103 5 ▶ my $field_sql = _assertion_to_sql_FIELD( $field, $name_quoter, $value_quoter )
Branch: 1 branch
BranchTrueFalseTotal
my $field_sql = _assertion_to_sql_FIELD($field, $name_quoter, $value_quoter) or return 1 4 5
104 or return;
105 4 push @fields, "($field_sql)";
106 }
107
108 4 join " AND ", @fields;
109 }
110  
111 sub _assertion_to_sql_FIELD {
112 5 ▶ my ( $field, $name_quoter, $value_quoter ) = ( @_ );
Subroutine _assertion_to_sql_FIELD: called CC=8 SCAR=23.7
113
114 5 ▶ return if ref( $field->value );
Branch: 1 branch
BranchTrueFalseTotal
if ref $field->value 0 5 5
115 5 my $result;
116
117 5 ▶ if ( exists $field->params->{is} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $field->params->{'is'}) { } 4 1 5
elsif (exists $field->params->{'in'}) { } 0 1 1
elsif (exists $field->params->{'like'}) { } 0 1 1
elsif (exists $field->params->{'to'}) { } 0 1 1
118 4 $result = _assertion_to_sql_FIELD_IS( @_ );
119 }
120 elsif ( exists $field->params->{in} ) {
121 0 $result = _assertion_to_sql_FIELD_IN( @_ );
122 }
123 elsif ( exists $field->params->{like} ) {
124 0 $result = _assertion_to_sql_FIELD_LIKE( @_ );
125 }
126 elsif ( exists $field->params->{to} ) {
127 0 $result = _assertion_to_sql_FIELD_TO( @_ );
128 }
129
130 5 ▶ return unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 1 4 5
131
132 4 ▶ if ( exists $field->params->{nix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->params->{"nix"}) 0 4 4
133 0 return "NOT ($result)";
134 }
135
136 4 return $result;
137 }
138  
139 sub _assertion_to_sql_FIELD_IS {
140 4 ▶ my ( $field, $name_quoter, $value_quoter ) = ( @_ );
Subroutine _assertion_to_sql_FIELD_IS: called CC=4 SCAR=15.5
141
142 4 ▶ my $cmp = $field->params->{cmp} || '==';
Condition50%1/2
$field->params->{'cmp'} || '=='
l!l
04
MC/DC0%0/1
$field->params->{'cmp'} || '=='
$field->params->{'cmp'}
Truth table50%1/2
A
A= $field->params->{'cmp'}
Aresult
11
00
143 4 ▶ if ( $cmp eq '!=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp eq "!=") 0 4 4
144 0 $cmp = '<>'; # SQL syntax <> Perl syntax
145 }
146
147 my $wrapper = $field->params->{nocase}
148 0 ▶ ? sub { sprintf( 'LOWER(%s)', $_[0] ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 4 ▶ : sub { $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$field->params->{'nocase'} ? : 0 4 4
150
151 return sprintf(
152 '%s %s %s',
153 $wrapper->( $name_quoter->( $field->value ) ),
154 $cmp,
155 4 $wrapper->( $value_quoter->( $field->params->{is} ) ),
156 );
157 }
158  
159 sub _assertion_to_sql_FIELD_IN {
160 0 ▶ my ( $field, $name_quoter, $value_quoter ) = ( @_ );
Subroutine _assertion_to_sql_FIELD_IN: not called CC=1 SCAR=6.9
161
162 return sprintf(
163 '%s IN (%s)',
164 $name_quoter->( $field->value ),
165 join(
166 q[, ],
167 0 map $value_quoter->( $_ ), @{ $field->params->{to} },
168 ),
169 );
170 }
171  
172 sub _assertion_to_sql_FIELD_LIKE {
173 0 ▶ my ( $field, $name_quoter, $value_quoter ) = ( @_ );
Subroutine _assertion_to_sql_FIELD_LIKE: not called CC=2 SCAR=17.9
174
175 my $wrapper = $field->params->{nocase}
176 0 ▶ ? sub { sprintf( 'LOWER(%s)', $_[0] ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 0 ▶ : sub { $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$field->params->{'nocase'} ? : 0 0 0
178
179 return sprintf(
180 '%s LIKE %s',
181 $wrapper->( $name_quoter->( $field->value ) ),
182 0 $wrapper->( $value_quoter->( $field->params->{like} ) ),
183 );
184 }
185  
186 sub _assertion_to_sql_FIELD_TO {
187 0 ▶ my ( $field, $name_quoter, $value_quoter ) = ( @_ );
Subroutine _assertion_to_sql_FIELD_TO: not called CC=4 SCAR=30.0
188
189 0 ▶ my $cmp = $field->params->{cmp} || '==';
Condition0%0/2
$field->params->{'cmp'} || '=='
l!l
00
MC/DC0%0/1
$field->params->{'cmp'} || '=='
$field->params->{'cmp'}
Truth table0%0/2
A
A= $field->params->{'cmp'}
Aresult
11
00
190 0 ▶ if ( $cmp eq '!=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp eq "!=") 0 0 0
191 0 $cmp = '<>'; # SQL syntax <> Perl syntax
192 }
193
194 my $wrapper = $field->params->{nocase}
195 0 ▶ ? sub { sprintf( 'LOWER(%s)', $_[0] ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 0 ▶ : sub { $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$field->params->{'nocase'} ? : 0 0 0
197
198 return sprintf(
199 '%s %s %s',
200 $wrapper->( $name_quoter->( $field->value ) ),
201 $cmp,
202 0 $wrapper->( $name_quoter->( $field->params->{to} ) ),
203 );
204 }
205  
206 1 1;
↑ Summary Database/Table.pm »