PrettyPrinter/Node/UpdateStmt.pm

filter: Statementstmt 95.5% 150 / 157 Branchbran 81.5% 31 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.4% 19 / 21 Podpod 100% 5 / 5 totaltotal 92.7% 205 / 221 CC 22 SCAR 15.2
CC
22
Cov
93%
pretty_print
8.0
as_text
8.0
__ANON__
6.0
CRAP 4.6
« PrettyPrinter.pm ↑ Summary PrettyPrinter/Node.pm »
1 package Pg::SQL::PrettyPrinter::Node::UpdateStmt;
2  
3 # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings qw( FATAL utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use open qw( :std :utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Unicode::Normalize qw( NFC );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Unicode::Collate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶if ( grep /\P{ASCII}/ => @ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /\P{ASCII}/u, @ARGV) 0 3 3
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 3 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Carp qw( carp croak confess cluck );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶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 3 };
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 3 };
40  
41 # Useful common code
42  
43 3 ▶use parent qw( Pg::SQL::PrettyPrinter::Node );
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
46 10 my $class = shift;
47 10 my $self = $class->SUPER::new( @_ );
48 10 bless $self, $class;
49  
50 10 $self->objectify(
51 qw( targetList whereClause fromClause returningList ),
52 [ 'withClause', 'ctes' ],
53 );
54  
55 10 $self->build_set_array();
56  
57 10 return $self;
58 }
59  
60 10 ▶sub build_set_array {
Subroutine build_set_array: called CC=4 SCAR=13.9
Pod documented
61 10 my $self = shift;
62 10 my @set = ();
63 10 my $multi_join = 0;
64  
65 10 for my $item ( @{ $self->{ 'targetList' } } ) {
66 20 my $column = $item->{ 'name' };
67 20 ▶ if ( $multi_join > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi_join > 0) 6 14 20
68 6 push @{ $set[ -1 ]->{ 'cols' } }, $column;
69 6 $multi_join--;
70 6 next;
71 }
72 14 my $val = $item->{ 'val' };
73 14 ▶ if ( 'Pg::SQL::PrettyPrinter::Node::MultiAssignRef' eq ref $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Pg::SQL::PrettyPrinter::Node::MultiAssignRef' eq ref $val) { } 3 11 14
74 3 push @set, { 'cols' => [ $column ], 'val' => $val->{ 'source' } };
75 3 $multi_join = $val->{ 'ncolumns' } - 1;
76 }
77 else {
78 11 push @set, { 'col' => $column, 'val' => $val };
79 }
80 }
81 10 $self->{ '_set' } = \@set;
82 }
83  
84 10 ▶sub as_text {
Subroutine as_text: called CC=8 SCAR=20.8
Pod documented
85 10 my $self = shift;
86 10 my @elements = ();
87 10 push @elements, 'UPDATE';
88 10 push @elements, $self->relname;
89 10 push @elements, 'SET';
90 10 my @set_elements = ();
91 10 for my $item ( @{ $self->{ '_set' } } ) {
92 14 ▶ if ( exists $item->{ 'col' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{'col'}) { } 11 3 14
93 11 push @set_elements, sprintf '%s = %s', $self->quote_ident( $item->{ 'col' } ), $item->{ 'val' }->as_text;
94 }
95 else {
96 push @set_elements, sprintf(
97 '( %s ) = %s',
98 3 join( ', ', @{ $item->{ 'cols' } } ),
99 3 $item->{ 'val' }->as_text
100 );
101 }
102 }
103 10 push @elements, join( ', ', @set_elements );
104 10 ▶ if ( exists $self->{ 'fromClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"fromClause"}) 3 7 10
105 3 push @elements, 'FROM';
106 3 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'fromClause' } } );
107 }
108 10 ▶ if ( exists $self->{ 'whereClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"whereClause"}) 6 4 10
109 6 push @elements, 'WHERE';
110 6 push @elements, $self->{ 'whereClause' }->as_text;
111 }
112 10 ▶ if ( exists $self->{ 'returningList' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"returningList"}) 2 8 10
113 2 push @elements, 'RETURNING';
114 2 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'returningList' } } );
115 }
116 10 my $prefix = '';
117 10 ▶ if ( exists $self->{ 'withClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"withClause"}) 1 9 10
118 1 $prefix = 'WITH ';
119 1 ▶ $prefix .= 'RECURSIVE ' if $self->{ 'withClause' }->{ 'recursive' };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"withClause"}{"recursive"} 0 1 1
120 1 $prefix .= join( ', ', map { $_->as_text } @{ $self->{ 'withClause' }->{ 'ctes' } } ) . ' ';
121 }
122 10 return $prefix . join( ' ', @elements );
123 }
124  
125 10 ▶sub pretty_print {
Subroutine pretty_print: called CC=8 SCAR=20.8
Pod documented
126 10 my $self = shift;
127 10 my @lines = ();
128 10 push @lines, 'UPDATE ' . $self->relname;
129 10 push @lines, 'SET';
130 10 for my $item ( @{ $self->{ '_set' } } ) {
131 14 ▶ if ( exists $item->{ 'col' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{'col'}) { } 11 3 14
132 push @lines, $self->increase_indent(
133 sprintf(
134 '%s = %s,',
135 $self->quote_ident( $item->{ 'col' } ),
136 11 $item->{ 'val' }->pretty_print
137 )
138 );
139 }
140 else {
141 push @lines, $self->increase_indent(
142 sprintf(
143 '( %s ) = %s,',
144 3 join( ', ', @{ $item->{ 'cols' } } ),
145 3 $item->{ 'val' }->pretty_print
146 )
147 );
148 }
149 }
150  
151 # Remove unnecessary trailing , in last element
152 10 $lines[ -1 ] =~ s/,\z//;
153  
154 10 ▶ if ( exists $self->{ 'fromClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"fromClause"}) 3 7 10
155 3 push @lines, 'FROM';
156 3 push @lines, map { $self->increase_indent( $_->pretty_print ) . ',' } @{ $self->{ 'fromClause' } };
157  
158 # Remove unnecessary trailing , in last element
159 3 $lines[ -1 ] =~ s/,\z//;
160 }
161  
162 10 ▶ if ( exists $self->{ 'whereClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"whereClause"}) 6 4 10
163 6 push @lines, 'WHERE';
164 6 push @lines, $self->increase_indent( $self->{ 'whereClause' }->pretty_print );
165 }
166  
167 10 ▶ if ( exists $self->{ 'returningList' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"returningList"}) 2 8 10
168 2 push @lines, 'RETURNING ';
169 2 $lines[ -1 ] .= join( ', ', map { $_->pretty_print } @{ $self->{ 'returningList' } } );
170 }
171  
172 10 my $main_body = join( "\n", @lines );
173 10 ▶ return $main_body unless exists $self->{ 'withClause' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"withClause"} 9 1 10
174  
175 1 my @cte_def = ();
176  
177 1 push @cte_def, map { $_->pretty_print . ',' } @{ $self->{ 'withClause' }->{ 'ctes' } };
178  
179 # Remove unnecessary trailing , in last element
180 1 $cte_def[ -1 ] =~ s/,\z//;
181 1 ▶ if ( $self->{ 'withClause' }->{ 'recursive' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'withClause'}{'recursive'}) { } 0 1 1
182 0 $cte_def[ 0 ] = 'WITH RECURSIVE ' . $cte_def[ 0 ];
183 }
184 else {
185 1 $cte_def[ 0 ] = 'WITH ' . $cte_def[ 0 ];
186 }
187  
188 1 @lines = ();
189 1 push @lines, join( ' ', @cte_def );
190 1 push @lines, $main_body;
191 1 return join( "\n", @lines );
192 }
193  
194 20 ▶sub relname {
Subroutine relname: called CC=3 SCAR=11.0
Pod documented
195 20 my $self = shift;
196 20 ▶ if ( !$self->{ '_relname' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_relname"}) 10 10 20
197 10 my $R = $self->{ 'relation' };
198 10 my @elements = map { $self->quote_ident( $R->{ $_ } ) }
199 10 grep { exists $R->{ $_ } } qw{ catalogname schemaname relname };
200 10 $self->{ '_relname' } = join( '.', @elements );
201 10 ▶ if ( $R->{ 'alias' }->{ 'aliasname' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($R->{"alias"}{"aliasname"}) 1 9 10
202 1 $self->{ '_relname' } .= ' AS ' . $self->quote_ident( $R->{ 'alias' }->{ 'aliasname' } );
203 }
204 }
205 20 return $self->{ '_relname' };
206 }
207  
208 3 1;
209  
210 # vim: set ft=perl:
« PrettyPrinter.pm ↑ Summary PrettyPrinter/Node.pm »