PrettyPrinter/Node/SelectStmt.pm

filter: Statementstmt 96.3% 209 / 217 Branchbran 91.0% 71 / 78 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 92.3% 24 / 26 Podpod 100% 10 / 10 totaltotal 95.0% 326 / 343 CC 45 SCAR 18.5
CC
45
Cov
95%
pretty_print_plain
13.0
as_text_plain
11.0
__ANON__
6.0
CRAP 6.4
« PrettyPrinter/Node/TransactionStmt.pm ↑ Summary PrettyPrinter/Node/A_Const.pm »
1 package Pg::SQL::PrettyPrinter::Node::SelectStmt;
2  
3 # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use warnings qw( FATAL utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use open qw( :std :utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Unicode::Normalize qw( NFC );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Unicode::Collate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 ▶if ( grep /\P{ASCII}/ => @ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /\P{ASCII}/u, @ARGV) 0 11 11
15 0 @ARGV = map { decode( 'UTF-8', $_ ) } @ARGV;
16 }
17  
18 # If there is __DATA__,then uncomment next line:
19 # binmode( DATA, ':encoding(UTF-8)' );
20 # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
21  
22 # Useful common code
23 11 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 11 ▶use Carp qw( carp croak confess cluck );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # give a full stack dump on any untrapped exceptions
29 0 ▶local $SIG{ __DIE__ } = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
30 0 ▶ confess "Uncaught exception: @_" unless $^S;
Branch: 1 branch
BranchTrueFalseTotal
unless $^S 0 0 0
31 11 };
32  
33 # now promote run-time warnings into stackdumped exceptions
34 # *unless* we're in an try block, in which
35 # case just generate a clucking stackdump instead
36 0 ▶local $SIG{ __WARN__ } = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
37 0 ▶ if ( $^S ) { cluck "Trapped warning: @_" }
Branch: 1 branch
BranchTrueFalseTotal
if ($^S) { } 0 0 0
38 0 else { confess "Deadly warning: @_" }
39 11 };
40  
41 # Useful common code
42  
43 11 ▶use parent qw( Pg::SQL::PrettyPrinter::Node );
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 119 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
46 119 my $class = shift;
47 119 my $self = $class->SUPER::new( @_ );
48 119 bless $self, $class;
49  
50 119 $self->objectify(
51 'valuesLists',
52 [ 'withClause', 'ctes' ],
53 );
54 119 ▶ if ( $self->setop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->setop) { } 5 114 119
55 5 $self->init_setop;
56 }
57 else {
58 114 $self->init_plain;
59 }
60  
61 119 return $self;
62 }
63  
64 169 ▶sub setop {
Subroutine setop: called CC=3 SCAR=11.0
Pod documented
65 169 my $self = shift;
66 169 ▶ return if $self->{ 'op' } eq 'SETOP_NONE';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"op"} eq "SETOP_NONE" 128 41 169
67 41 my $op = $self->{ 'op' };
68 41 $op =~ s/^SETOP_//;
69 41 ▶ $op .= ' ALL' if $self->{ 'all' };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"all"} 14 27 41
70 41 return $op;
71 }
72  
73 5 ▶sub init_setop {
Subroutine init_setop: called CC=2 SCAR=6.9
Pod documented
74 5 my $self = shift;
75 5 for my $element ( qw( rarg larg ) ) {
76 10 $self->{ $element } = $self->make_from( { 'SelectStmt' => $self->{ $element } } );
77 }
78 }
79  
80 114 ▶sub init_plain {
Subroutine init_plain: called CC=1 SCAR=0.0
Pod documented
81 114 my $self = shift;
82  
83 114 $self->objectify( qw( targetList fromClause whereClause groupClause havingClause sortClause limitCount limitOffset distinctClause lockingClause ) );
84 }
85  
86 122 ▶sub as_text {
Subroutine as_text: called CC=5 SCAR=16.1
Pod documented
87 122 my $self = shift;
88 122 ▶ if ( exists $self->{ 'valuesLists' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"valuesLists"}) 16 106 122
89 16 return sprintf( 'VALUES %s', join( ', ', map { $_->as_text } @{ $self->{ 'valuesLists' } } ) );
90 }
91 106 my $prefix = '';
92 106 ▶ if ( exists $self->{ 'withClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"withClause"}) 2 104 106
93 2 $prefix = 'WITH ';
94 2 ▶ $prefix .= 'RECURSIVE ' if $self->{ 'withClause' }->{ 'recursive' };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"withClause"}{"recursive"} 1 1 2
95 2 $prefix .= join( ', ', map { $_->as_text } @{ $self->{ 'withClause' }->{ 'ctes' } } ) . ' ';
96 }
97 106 ▶ return $prefix . ( $self->{ 'op' } eq 'SETOP_NONE' ? $self->as_text_plain : $self->as_text_setop );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'op'} eq 'SETOP_NONE' ? : 101 5 106
98 }
99  
100 5 ▶sub as_text_setop {
Subroutine as_text_setop: called CC=5 SCAR=16.2
Pod documented
101 5 my $self = shift;
102 5 my @elements = ();
103 5 ▶ if ( ( $self->{ 'larg' }->setop // '' ) eq $self->setop ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{'larg'}->setop // '') eq $self->setop) { } 1 4 5
Condition100%2/2
$self->{"larg"}->setop // ""
l!l
23
MC/DC100%1/1
$self->{"larg"}->setop // ""
$self->{"larg"}->setop
Truth table100%2/2
A
A= $self->{"larg"}->setop
Aresult
11
00
104 1 push @elements, $self->{ 'larg' }->as_text;
105 }
106 else {
107 4 push @elements, '(', $self->{ 'larg' }->as_text, ')';
108 }
109 5 push @elements, $self->setop;
110 5 ▶ if ( ( $self->{ 'rarg' }->setop // '' ) eq $self->setop ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{'rarg'}->setop // '') eq $self->setop) { } 0 5 5
Condition100%2/2
$self->{"rarg"}->setop // ""
l!l
14
MC/DC100%1/1
$self->{"rarg"}->setop // ""
$self->{"rarg"}->setop
Truth table100%2/2
A
A= $self->{"rarg"}->setop
Aresult
11
00
111 0 push @elements, $self->{ 'rarg' }->as_text;
112 }
113 else {
114 5 push @elements, '(', $self->{ 'rarg' }->as_text, ')';
115 }
116 5 return join( " ", @elements );
117 }
118  
119 101 ▶sub as_text_plain {
Subroutine as_text_plain: called CC=11 SCAR=24.0
Pod documented
120 101 my $self = shift;
121 101 my $query = 'SELECT ';
122 101 ▶ if ( exists $self->{ 'distinctClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"distinctClause"}) 2 99 101
123 2 ▶ if ( 0 == scalar @{ $self->{ 'distinctClause' } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == scalar @{$self->{'distinctClause'};}) { } 1 1 2
124 1 $query .= 'DISTINCT ';
125 }
126 else {
127 $query .= sprintf(
128 'DISTINCT ON ( %s ) ',
129 1 join( ', ', map { $_->as_text } @{ $self->{ 'distinctClause' } } )
130 );
131 }
132 }
133 101 $query .= join( ', ', map { $_->as_text } @{ $self->{ 'targetList' } } );
134 101 ▶ if ( exists $self->{ 'fromClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"fromClause"}) 63 38 101
135 63 $query .= ' FROM ' . join( ', ', map { $_->as_text } @{ $self->{ 'fromClause' } } );
136 }
137 101 ▶ if ( exists $self->{ 'whereClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"whereClause"}) 11 90 101
138 11 $query .= ' WHERE ' . $self->{ 'whereClause' }->as_text;
139 }
140 101 ▶ if ( exists $self->{ 'groupClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"groupClause"}) 5 96 101
141 5 $query .= ' GROUP BY ' . join( ', ', map { $_->as_text } @{ $self->{ 'groupClause' } } );
142 }
143 101 ▶ if ( exists $self->{ 'havingClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"havingClause"}) 1 100 101
144 1 $query .= ' HAVING ' . $self->{ 'havingClause' }->as_text;
145 }
146 101 ▶ if ( exists $self->{ 'sortClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"sortClause"}) 12 89 101
147 12 $query .= ' ORDER BY ' . join( ', ', map { $_->as_text } @{ $self->{ 'sortClause' } } );
148 }
149 101 ▶ if ( exists $self->{ 'limitCount' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"limitCount"}) 10 91 101
150 10 $query .= ' LIMIT ' . $self->{ 'limitCount' }->as_text;
151 }
152 101 ▶ if ( exists $self->{ 'limitOffset' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"limitOffset"}) 1 100 101
153 1 $query .= ' OFFSET ' . $self->{ 'limitOffset' }->as_text;
154 }
155 101 ▶ if ( exists $self->{ 'lockingClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"lockingClause"}) 9 92 101
156 $query .= ' ' . join(
157 ' ',
158 9 map { $_->as_text } @{ $self->{ 'lockingClause' } }
159 );
160 }
161 101 return $query;
162 }
163  
164 117 ▶sub pretty_print {
Subroutine pretty_print: called CC=5 SCAR=16.1
Pod documented
165 117 my $self = shift;
166 117 ▶ if ( exists $self->{ 'valuesLists' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"valuesLists"}) 16 101 117
167 16 my @lines = ();
168 16 push @lines, 'VALUES';
169 16 push @lines, map { $self->increase_indent( $_->as_text ) . ',' } @{ $self->{ 'valuesLists' } };
170  
171 # Remove unnecessary trailing , in last element
172 16 $lines[ -1 ] =~ s/,\z//;
173 16 return join( "\n", @lines );
174 }
175  
176 101 ▶ my $main_body = $self->{ 'op' } eq 'SETOP_NONE' ? $self->pretty_print_plain : $self->pretty_print_setop;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'op'} eq 'SETOP_NONE' ? : 96 5 101
177 101 ▶ return $main_body unless exists $self->{ 'withClause' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"withClause"} 99 2 101
178  
179 2 my @cte_def = ();
180  
181 2 push @cte_def, map { $_->pretty_print . ',' } @{ $self->{ 'withClause' }->{ 'ctes' } };
182  
183 # Remove unnecessary trailing , in last element
184 2 $cte_def[ -1 ] =~ s/,\z//;
185 2 ▶ if ( $self->{ 'withClause' }->{ 'recursive' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'withClause'}{'recursive'}) { } 1 1 2
186 1 $cte_def[ 0 ] = 'WITH RECURSIVE ' . $cte_def[ 0 ];
187 }
188 else {
189 1 $cte_def[ 0 ] = 'WITH ' . $cte_def[ 0 ];
190 }
191  
192 2 my @lines = ();
193 2 push @lines, join( ' ', @cte_def );
194 2 push @lines, $main_body;
195 2 return join( "\n", @lines );
196 }
197  
198 5 ▶sub pretty_print_setop {
Subroutine pretty_print_setop: called CC=5 SCAR=16.1
Pod documented
199 5 my $self = shift;
200 5 my @elements = ();
201 5 ▶ if ( ( $self->{ 'larg' }->setop // '' ) eq $self->setop ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{'larg'}->setop // '') eq $self->setop) { } 1 4 5
Condition100%2/2
$self->{"larg"}->setop // ""
l!l
23
MC/DC100%1/1
$self->{"larg"}->setop // ""
$self->{"larg"}->setop
Truth table100%2/2
A
A= $self->{"larg"}->setop
Aresult
11
00
202 1 push @elements, $self->{ 'larg' }->pretty_print;
203 }
204 else {
205 4 push @elements, '(';
206 4 push @elements, $self->increase_indent( $self->{ 'larg' }->pretty_print );
207 4 push @elements, ')';
208 }
209 5 push @elements, $self->setop;
210 5 ▶ if ( ( $self->{ 'rarg' }->setop // '' ) eq $self->setop ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{'rarg'}->setop // '') eq $self->setop) { } 0 5 5
Condition100%2/2
$self->{"rarg"}->setop // ""
l!l
14
MC/DC100%1/1
$self->{"rarg"}->setop // ""
$self->{"rarg"}->setop
Truth table100%2/2
A
A= $self->{"rarg"}->setop
Aresult
11
00
211 0 push @elements, $self->{ 'rarg' }->pretty_print;
212 }
213 else {
214 5 push @elements, '(';
215 5 push @elements, $self->increase_indent( $self->{ 'rarg' }->pretty_print );
216 5 push @elements, ')';
217 }
218 5 return join( "\n", @elements );
219 }
220  
221 96 ▶sub pretty_print_plain {
Subroutine pretty_print_plain: called CC=13 SCAR=25.6
Pod documented
222 96 my $self = shift;
223 96 my @lines = ( 'SELECT' );
224  
225 96 ▶ if ( exists $self->{ 'distinctClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"distinctClause"}) 2 94 96
226 2 ▶ if ( 0 == scalar @{ $self->{ 'distinctClause' } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == scalar @{$self->{'distinctClause'};}) { } 1 1 2
227 1 $lines[ 0 ] .= ' DISTINCT';
228 }
229 else {
230 push @lines, sprintf(
231 $self->increase_indent( 'DISTINCT ON ( %s )' ),
232 1 join( ', ', map { $_->pretty_print } @{ $self->{ 'distinctClause' } } )
233 );
234 }
235 }
236  
237 96 for my $i ( 0 .. $#{ $self->{ 'targetList' } } ) {
238 235 my $is_last = $i == $#{ $self->{ 'targetList' } };
239 235 my $target = $self->{ 'targetList' }->[ $i ];
240 235 my $pretty = $self->increase_indent( $target->pretty_print() );
241 235 ▶ $pretty .= ',' unless $is_last;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_last 139 96 235
242 235 push @lines, $pretty;
243 }
244 96 ▶ if ( exists $self->{ 'fromClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"fromClause"}) 58 38 96
245 58 push @lines, 'FROM';
246 58 push @lines, map { $self->increase_indent( $_->pretty_print ) . ',' } @{ $self->{ 'fromClause' } };
247  
248 # Remove unnecessary trailing , in last element
249 58 $lines[ -1 ] =~ s/,\z//;
250 }
251 96 ▶ if ( exists $self->{ 'whereClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"whereClause"}) 10 86 96
252 10 push @lines, 'WHERE';
253 10 push @lines, $self->increase_indent( $self->{ 'whereClause' }->pretty_print );
254 }
255 96 ▶ if ( exists $self->{ 'groupClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"groupClause"}) 5 91 96
256 5 push @lines, 'GROUP BY';
257 5 push @lines, map { $self->increase_indent( $_->pretty_print ) . ',' } @{ $self->{ 'groupClause' } };
258  
259 # Remove unnecessary trailing , in last element
260 5 $lines[ -1 ] =~ s/,\z//;
261 }
262 96 ▶ if ( exists $self->{ 'havingClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"havingClause"}) 1 95 96
263 1 push @lines, 'HAVING';
264 1 push @lines, $self->increase_indent( $self->{ 'havingClause' }->pretty_print );
265 }
266 96 ▶ if ( exists $self->{ 'sortClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"sortClause"}) 12 84 96
267 12 push @lines, 'ORDER BY';
268 12 push @lines, map { $self->increase_indent( $_->pretty_print ) . ',' } @{ $self->{ 'sortClause' } };
269  
270 # Remove unnecessary trailing , in last element
271 12 $lines[ -1 ] =~ s/,\z//;
272 }
273  
274 96 ▶ if ( exists $self->{ 'limitCount' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"limitCount"}) 10 86 96
275 10 push @lines, 'LIMIT ' . $self->{ 'limitCount' }->pretty_print;
276 }
277 96 ▶ if ( exists $self->{ 'limitOffset' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"limitOffset"}) 1 95 96
278 1 push @lines, 'OFFSET ' . $self->{ 'limitOffset' }->pretty_print;
279 }
280 96 ▶ if ( exists $self->{ 'lockingClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"lockingClause"}) 9 87 96
281 9 push @lines, map { $_->pretty_print } @{ $self->{ 'lockingClause' } };
282 }
283  
284 96 return join( "\n", @lines );
285 }
286  
287 11 1;
« PrettyPrinter/Node/TransactionStmt.pm ↑ Summary PrettyPrinter/Node/A_Const.pm »