File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/UpdateStmt.pm
Criterion Covered Total %
statement 146 147 99.3
branch 30 32 93.7
condition n/a
subroutine 19 19 100.0
pod 5 5 100.0
total 200 203 98.5


line stmt bran cond sub pod time code
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     3   11052 use v5.26;
  3         16  
5 3     3   34 use strict;
  3         9  
  3         141  
6 3     3   21 use warnings;
  3         8  
  3         297  
7 3     3   27 use warnings qw( FATAL utf8 );
  3         7  
  3         247  
8 3     3   31 use utf8;
  3         7  
  3         27  
9 3     3   232 use open qw( :std :utf8 );
  3         12  
  3         30  
10 3     3   635 use Unicode::Normalize qw( NFC );
  3         8  
  3         386  
11 3     3   32 use Unicode::Collate;
  3         14  
  3         122  
12 3     3   22 use Encode qw( decode );
  3         7  
  3         2164  
13              
14             if ( grep /\P{ASCII}/ => @ARGV ) {
15             @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     3   28 use autodie;
  3         6  
  3         32  
24 3     3   29253 use Carp qw( carp croak confess cluck );
  3         8  
  3         444  
25 3     3   66 use English qw( -no_match_vars );
  3         9  
  3         38  
26 3     3   1959 use Data::Dumper qw( Dumper );
  3         7  
  3         1045  
27              
28             # give a full stack dump on any untrapped exceptions
29             local $SIG{ __DIE__ } = sub {
30             confess "Uncaught exception: @_" unless $^S;
31             };
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             local $SIG{ __WARN__ } = sub {
37             if ( $^S ) { cluck "Trapped warning: @_" }
38             else { confess "Deadly warning: @_" }
39             };
40              
41             # Useful common code
42              
43 3     3   35 use parent qw( Pg::SQL::PrettyPrinter::Node );
  3         10  
  3         40  
44              
45             sub new {
46 10     10 1 1046 my $class = shift;
47 10         88 my $self = $class->SUPER::new( @_ );
48 10         26 bless $self, $class;
49              
50 10         91 $self->objectify(
51             qw( targetList whereClause fromClause returningList ),
52             [ 'withClause', 'ctes' ],
53             );
54              
55 10         62 $self->build_set_array();
56              
57 10         42 return $self;
58             }
59              
60             sub build_set_array {
61 10     10 1 25 my $self = shift;
62 10         26 my @set = ();
63 10         28 my $multi_join = 0;
64              
65 10         32 for my $item ( @{ $self->{ 'targetList' } } ) {
  10         42  
66 20         61 my $column = $item->{ 'name' };
67 20 100       59 if ( $multi_join > 0 ) {
68 6         11 push @{ $set[ -1 ]->{ 'cols' } }, $column;
  6         20  
69 6         12 $multi_join--;
70 6         36 next;
71             }
72 14         37 my $val = $item->{ 'val' };
73 14 100       55 if ( 'Pg::SQL::PrettyPrinter::Node::MultiAssignRef' eq ref $val ) {
74 3         23 push @set, { 'cols' => [ $column ], 'val' => $val->{ 'source' } };
75 3         12 $multi_join = $val->{ 'ncolumns' } - 1;
76             }
77             else {
78 11         61 push @set, { 'col' => $column, 'val' => $val };
79             }
80             }
81 10         51 $self->{ '_set' } = \@set;
82             }
83              
84             sub as_text {
85 10     10 1 85 my $self = shift;
86 10         31 my @elements = ();
87 10         26 push @elements, 'UPDATE';
88 10         44 push @elements, $self->relname;
89 10         28 push @elements, 'SET';
90 10         26 my @set_elements = ();
91 10         21 for my $item ( @{ $self->{ '_set' } } ) {
  10         39  
92 14 100       51 if ( exists $item->{ 'col' } ) {
93 11         48 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         25 join( ', ', @{ $item->{ 'cols' } } ),
99 3         9 $item->{ 'val' }->as_text
100             );
101             }
102             }
103 10         39 push @elements, join( ', ', @set_elements );
104 10 100       48 if ( exists $self->{ 'fromClause' } ) {
105 3         11 push @elements, 'FROM';
106 3         10 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'fromClause' } } );
  3         20  
  3         10  
107             }
108 10 100       59 if ( exists $self->{ 'whereClause' } ) {
109 6         22 push @elements, 'WHERE';
110 6         45 push @elements, $self->{ 'whereClause' }->as_text;
111             }
112 10 100       45 if ( exists $self->{ 'returningList' } ) {
113 2         7 push @elements, 'RETURNING';
114 2         5 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'returningList' } } );
  4         16  
  2         9  
115             }
116 10         30 my $prefix = '';
117 10 100       36 if ( exists $self->{ 'withClause' } ) {
118 1         4 $prefix = 'WITH ';
119 1 50       6 $prefix .= 'RECURSIVE ' if $self->{ 'withClause' }->{ 'recursive' };
120 1         3 $prefix .= join( ', ', map { $_->as_text } @{ $self->{ 'withClause' }->{ 'ctes' } } ) . ' ';
  1         8  
  1         5  
121             }
122 10         129 return $prefix . join( ' ', @elements );
123             }
124              
125             sub pretty_print {
126 10     10 1 18694 my $self = shift;
127 10         32 my @lines = ();
128 10         42 push @lines, 'UPDATE ' . $self->relname;
129 10         52 push @lines, 'SET';
130 10         23 for my $item ( @{ $self->{ '_set' } } ) {
  10         38  
131 14 100       52 if ( exists $item->{ 'col' } ) {
132             push @lines, $self->increase_indent(
133             sprintf(
134             '%s = %s,',
135             $self->quote_ident( $item->{ 'col' } ),
136 11         60 $item->{ 'val' }->pretty_print
137             )
138             );
139             }
140             else {
141             push @lines, $self->increase_indent(
142             sprintf(
143             '( %s ) = %s,',
144 3         28 join( ', ', @{ $item->{ 'cols' } } ),
145 3         12 $item->{ 'val' }->pretty_print
146             )
147             );
148             }
149             }
150              
151             # Remove unnecessary trailing , in last element
152 10         68 $lines[ -1 ] =~ s/,\z//;
153              
154 10 100       40 if ( exists $self->{ 'fromClause' } ) {
155 3         7 push @lines, 'FROM';
156 3         9 push @lines, map { $self->increase_indent( $_->pretty_print ) . ',' } @{ $self->{ 'fromClause' } };
  3         25  
  3         11  
157              
158             # Remove unnecessary trailing , in last element
159 3         21 $lines[ -1 ] =~ s/,\z//;
160             }
161              
162 10 100       38 if ( exists $self->{ 'whereClause' } ) {
163 6         19 push @lines, 'WHERE';
164 6         41 push @lines, $self->increase_indent( $self->{ 'whereClause' }->pretty_print );
165             }
166              
167 10 100       41 if ( exists $self->{ 'returningList' } ) {
168 2         8 push @lines, 'RETURNING ';
169 2         6 $lines[ -1 ] .= join( ', ', map { $_->pretty_print } @{ $self->{ 'returningList' } } );
  4         16  
  2         9  
170             }
171              
172 10         74 my $main_body = join( "\n", @lines );
173 10 100       73 return $main_body unless exists $self->{ 'withClause' };
174              
175 1         4 my @cte_def = ();
176              
177 1         4 push @cte_def, map { $_->pretty_print . ',' } @{ $self->{ 'withClause' }->{ 'ctes' } };
  1         6  
  1         6  
178              
179             # Remove unnecessary trailing , in last element
180 1         8 $cte_def[ -1 ] =~ s/,\z//;
181 1 50       6 if ( $self->{ 'withClause' }->{ 'recursive' } ) {
182 0         0 $cte_def[ 0 ] = 'WITH RECURSIVE ' . $cte_def[ 0 ];
183             }
184             else {
185 1         3 $cte_def[ 0 ] = 'WITH ' . $cte_def[ 0 ];
186             }
187              
188 1         3 @lines = ();
189 1         4 push @lines, join( ' ', @cte_def );
190 1         4 push @lines, $main_body;
191 1         7 return join( "\n", @lines );
192             }
193              
194             sub relname {
195 20     20 1 51 my $self = shift;
196 20 100       114 if ( !$self->{ '_relname' } ) {
197 10         27 my $R = $self->{ 'relation' };
198 10         120 my @elements = map { $self->quote_ident( $R->{ $_ } ) }
199 10         30 grep { exists $R->{ $_ } } qw{ catalogname schemaname relname };
  30         93  
200 10         67 $self->{ '_relname' } = join( '.', @elements );
201 10 100       57 if ( $R->{ 'alias' }->{ 'aliasname' } ) {
202 1         7 $self->{ '_relname' } .= ' AS ' . $self->quote_ident( $R->{ 'alias' }->{ 'aliasname' } );
203             }
204             }
205 20         80 return $self->{ '_relname' };
206             }
207              
208             1;
209              
210             # vim: set ft=perl: