PrettyPrinter/Node/InsertStmt.pm

filter: Statementstmt 95.9% 165 / 172 Branchbran 81.4% 44 / 54 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 90.9% 20 / 22 Podpod 100% 6 / 6 totaltotal 92.6% 238 / 257 CC 30 SCAR 17.6
CC
30
Cov
93%
conflict_handling
13.0
__ANON__
6.0
__ANON__
6.0
CRAP 5.8
« PrettyPrinter/Node/A_Const.pm ↑ Summary PrettyPrinter.pm »
1 package Pg::SQL::PrettyPrinter::Node::InsertStmt;
2  
3 # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 4 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings qw( FATAL utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use open qw( :std :utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Unicode::Normalize qw( NFC );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Unicode::Collate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶if ( grep /\P{ASCII}/ => @ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /\P{ASCII}/u, @ARGV) 0 4 4
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 4 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Carp qw( carp croak confess cluck );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶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 4 };
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 4 };
40  
41 # Useful common code
42  
43 4 ▶use parent qw( Pg::SQL::PrettyPrinter::Node );
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 16 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
46 16 my $class = shift;
47 16 my $self = $class->SUPER::new( @_ );
48 16 bless $self, $class;
49  
50 16 $self->objectify(
51 qw( selectStmt cols returningList ),
52 [ 'withClause', 'ctes' ],
53 );
54  
55 16 ▶ if ( exists $self->{ 'onConflictClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"onConflictClause"}) 7 9 16
56 7 ▶ croak( 'Unsupported conflict action: ' . $self->{ 'onConflictClause' }->{ 'action' } ) unless $self->{ 'onConflictClause' }->{ 'action' } =~ m{\AONCONFLICT_(?:NOTHING|UPDATE)\z};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"onConflictClause"}{"action"} =~ /\AONCONFLICT_(?:NOTHING|UPDATE)\z/u 0 7 7
57 7 $self->objectify(
58 [ qw{ onConflictClause infer indexElems } ],
59 [ qw{ onConflictClause targetList } ],
60 [ qw{ onConflictClause whereClause } ],
61 );
62 7 $self->build_set_array();
63 }
64  
65 16 return $self;
66 }
67  
68 32 ▶sub relname {
Subroutine relname: called CC=2 SCAR=6.9
Pod documented
69 32 my $self = shift;
70 32 ▶ if ( !$self->{ '_relname' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_relname"}) 16 16 32
71 16 my $R = $self->{ 'relation' };
72 17 my @elements = map { $self->quote_ident( $R->{ $_ } ) }
73 16 grep { exists $R->{ $_ } } qw{ catalogname schemaname relname };
74 16 $self->{ '_relname' } = join( '.', @elements );
75 }
76 32 return $self->{ '_relname' };
77 }
78  
79 16 ▶sub as_text {
Subroutine as_text: called CC=5 SCAR=16.1
Pod documented
80 16 my $self = shift;
81 16 my @elements = ();
82 16 push @elements, 'INSERT INTO';
83 16 push @elements, $self->relname;
84 16 ▶ if ( exists $self->{ 'cols' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cols"}) 12 4 16
85 12 push @elements, '(';
86 12 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'cols' } } );
87 12 push @elements, ')';
88 }
89 16 push @elements, $self->{ 'selectStmt' }->as_text;
90 16 my $prefix = '';
91 16 ▶ if ( exists $self->{ 'withClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"withClause"}) 1 15 16
92 1 $prefix = 'WITH ';
93 1 ▶ $prefix .= 'RECURSIVE ' if $self->{ 'withClause' }->{ 'recursive' };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"withClause"}{"recursive"} 0 1 1
94 1 $prefix .= join( ', ', map { $_->as_text } @{ $self->{ 'withClause' }->{ 'ctes' } } ) . ' ';
95 }
96 16 push @elements, $self->conflict_handling();
97 16 ▶ if ( exists $self->{ 'returningList' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"returningList"}) 3 13 16
98 3 push @elements, 'RETURNING';
99 3 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'returningList' } } );
100 }
101 16 return $prefix . join( ' ', @elements );
102 }
103  
104 16 ▶sub pretty_print {
Subroutine pretty_print: called CC=5 SCAR=16.1
Pod documented
105 16 my $self = shift;
106 16 my @lines = ();
107 16 push @lines, 'INSERT INTO ' . $self->relname;
108 16 ▶ if ( exists $self->{ 'cols' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cols"}) 12 4 16
109 12 $lines[ -1 ] .= ' ( ';
110 12 $lines[ -1 ] .= join( ', ', map { $_->as_text } @{ $self->{ 'cols' } } );
111 12 $lines[ -1 ] .= ' )';
112 }
113 16 push @lines, $self->increase_indent( $self->{ 'selectStmt' }->pretty_print );
114  
115 16 ▶ if ( exists $self->{ 'withClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"withClause"}) 1 15 16
116  
117 1 my @cte_def = ();
118  
119 1 push @cte_def, map { $_->pretty_print . ',' } @{ $self->{ 'withClause' }->{ 'ctes' } };
120  
121 # Remove unnecessary trailing , in last element
122 1 $cte_def[ -1 ] =~ s/,\z//;
123 1 ▶ if ( $self->{ 'withClause' }->{ 'recursive' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'withClause'}{'recursive'}) { } 0 1 1
124 0 $cte_def[ 0 ] = 'WITH RECURSIVE ' . $cte_def[ 0 ];
125 }
126 else {
127 1 $cte_def[ 0 ] = 'WITH ' . $cte_def[ 0 ];
128 }
129  
130 1 unshift @lines, join( ' ', @cte_def );
131 }
132  
133 16 push @lines, map { $self->increase_indent( $_ ) } $self->conflict_handling( 1 );
134  
135 16 ▶ if ( exists $self->{ 'returningList' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"returningList"}) 3 13 16
136 3 push @lines, $self->increase_indent( 'RETURNING ' );
137 3 $lines[ -1 ] .= join( ', ', map { $_->as_text } @{ $self->{ 'returningList' } } );
138 }
139  
140 16 return join( "\n", @lines );
141 }
142  
143 32 ▶sub conflict_handling {
Subroutine conflict_handling: called CC=13 SCAR=25.7
Pod documented
144 32 my $self = shift;
145 32 ▶ my $indent = shift // '';
Condition100%2/2
shift() // ""
l!l
1616
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
146 32 ▶ return unless exists $self->{ 'onConflictClause' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"onConflictClause"} 18 14 32
147  
148 14 my @lines = ();
149 14 my $C = $self->{ 'onConflictClause' };
150 14 my $A = $self->{ 'onConflictClause' }->{ 'action' };
151 14 push @lines, 'ON CONFLICT';
152 14 ▶ if ( $C->{ 'infer' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($C->{"infer"}) 10 4 14
153 10 my $I = $C->{ 'infer' };
154 10 ▶ if ( exists $I->{ 'conname' } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $I->{'conname'}) { } 2 8 10
elsif (exists $I->{'indexElems'}) { } 8 0 8
155 2 $lines[ -1 ] .= ' ON CONSTRAINT ' . $self->quote_ident( $I->{ 'conname' } );
156 }
157 elsif ( exists $I->{ 'indexElems' } ) {
158 8 $lines[ -1 ] .= sprintf ' ( %s )', join( ', ', map { $_->as_text } @{ $I->{ 'indexElems' } } );
159 }
160 }
161 14 ▶ if ( $A eq 'ONCONFLICT_NOTHING' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($A eq 'ONCONFLICT_NOTHING') { } 8 6 14
elsif ($A eq 'ONCONFLICT_UPDATE') { } 6 0 6
162 8 $lines[ -1 ] .= ' DO NOTHING';
163 }
164 elsif ( $A eq 'ONCONFLICT_UPDATE' ) {
165 6 $lines[ -1 ] .= ' DO UPDATE';
166 6 push @lines, 'SET';
167 6 for my $item ( @{ $C->{ '_set' } } ) {
168 12 ▶ if ( exists $item->{ 'col' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{'col'}) { } 10 2 12
169 push @lines, sprintf(
170 '%s = %s,',
171 $self->quote_ident( $item->{ 'col' } ),
172 10 $item->{ 'val' }->pretty_print
173 );
174 }
175 else {
176 push @lines, sprintf(
177 '( %s ) = %s,',
178 2 join( ', ', @{ $item->{ 'cols' } } ),
179 2 $item->{ 'val' }->pretty_print
180 );
181 }
182 12 ▶ $lines[ -1 ] = $self->increase_indent( $lines[ -1 ] ) if $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $indent 6 6 12
183 }
184  
185 # Remove tailing ,
186 6 $lines[ -1 ] =~ s/,\z//;
187  
188 6 ▶ if ( exists $C->{ 'whereClause' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $C->{"whereClause"}) 2 4 6
189 2 push @lines, 'WHERE';
190 2 ▶ if ( $indent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent) { } 1 1 2
191 1 push @lines, $self->increase_indent( $C->{ 'whereClause' }->pretty_print() );
192 }
193 else {
194 1 push @lines, $C->{ 'whereClause' }->as_text;
195 }
196 }
197 }
198 14 return @lines;
199 }
200  
201 7 ▶sub build_set_array {
Subroutine build_set_array: called CC=5 SCAR=16.1
Pod documented
202 7 my $self = shift;
203 7 my $C = $self->{ 'onConflictClause' };
204 7 ▶ return unless exists $C->{ 'targetList' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $C->{"targetList"} 4 3 7
205  
206 3 my @set = ();
207 3 my $multi_join = 0;
208  
209 3 for my $item ( @{ $C->{ 'targetList' } } ) {
210 7 my $column = $item->{ 'name' };
211 7 ▶ if ( $multi_join > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi_join > 0) 1 6 7
212 1 push @{ $set[ -1 ]->{ 'cols' } }, $column;
213 1 $multi_join--;
214 1 next;
215 }
216 6 my $val = $item->{ 'val' };
217 6 ▶ if ( 'Pg::SQL::PrettyPrinter::Node::MultiAssignRef' eq ref $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Pg::SQL::PrettyPrinter::Node::MultiAssignRef' eq ref $val) { } 1 5 6
218 1 push @set, { 'cols' => [ $column ], 'val' => $val->{ 'source' } };
219 1 $multi_join = $val->{ 'ncolumns' } - 1;
220 }
221 else {
222 5 push @set, { 'col' => $column, 'val' => $val };
223 }
224 }
225 3 $C->{ '_set' } = \@set;
226 }
227  
228 4 1;
229  
230 # vim: set ft=perl:
« PrettyPrinter/Node/A_Const.pm ↑ Summary PrettyPrinter.pm »