File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/DeleteStmt.pm
Criterion Covered Total %
statement 111 112 99.1
branch 22 24 91.6
condition n/a
subroutine 18 18 100.0
pod 4 4 100.0
total 155 158 98.1


line stmt bran cond sub pod time code
1             package Pg::SQL::PrettyPrinter::Node::DeleteStmt;
2              
3             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 3     3   11716 use v5.26;
  3         14  
5 3     3   22 use strict;
  3         13  
  3         108  
6 3     3   19 use warnings;
  3         9  
  3         352  
7 3     3   26 use warnings qw( FATAL utf8 );
  3         9  
  3         1066  
8 3     3   24 use utf8;
  3         16  
  3         27  
9 3     3   275 use open qw( :std :utf8 );
  3         7  
  3         33  
10 3     3   728 use Unicode::Normalize qw( NFC );
  3         7  
  3         327  
11 3     3   24 use Unicode::Collate;
  3         59  
  3         139  
12 3     3   20 use Encode qw( decode );
  3         30  
  3         745  
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   42 use autodie;
  3         8  
  3         33  
24 3     3   28697 use Carp qw( carp croak confess cluck );
  3         52  
  3         553  
25 3     3   28 use English qw( -no_match_vars );
  3         11  
  3         42  
26 3     3   1937 use Data::Dumper qw( Dumper );
  3         10  
  3         4205  
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   37 use parent qw( Pg::SQL::PrettyPrinter::Node );
  3         9  
  3         41  
44              
45             sub new {
46 7     7 1 362 my $class = shift;
47 7         66 my $self = $class->SUPER::new( @_ );
48 7         20 bless $self, $class;
49              
50 7         69 $self->objectify(
51             qw( returningList usingClause whereClause ),
52             [ 'withClause', 'ctes' ],
53             );
54              
55 7         36 return $self;
56             }
57              
58             sub as_text {
59 7     7 1 54 my $self = shift;
60 7         18 my @elements = ();
61 7         21 push @elements, 'DELETE FROM';
62 7         34 push @elements, $self->relname;
63 7 100       24 if ( exists $self->{ 'usingClause' } ) {
64 3         12 push @elements, 'USING';
65 3         10 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'usingClause' } } );
  3         48  
  3         29  
66             }
67 7 100       24 if ( exists $self->{ 'whereClause' } ) {
68 4         10 push @elements, 'WHERE';
69 4         28 push @elements, $self->{ 'whereClause' }->as_text;
70             }
71 7 100       29 if ( exists $self->{ 'returningList' } ) {
72 2         5 push @elements, 'RETURNING';
73 2         6 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'returningList' } } );
  4         16  
  2         8  
74             }
75 7         17 my $prefix = '';
76 7 100       26 if ( exists $self->{ 'withClause' } ) {
77 1         4 $prefix = 'WITH ';
78 1 50       7 $prefix .= 'RECURSIVE ' if $self->{ 'withClause' }->{ 'recursive' };
79 1         3 $prefix .= join( ', ', map { $_->as_text } @{ $self->{ 'withClause' }->{ 'ctes' } } ) . ' ';
  1         7  
  1         4  
80             }
81 7         64 return $prefix . join( ' ', @elements );
82             }
83              
84             sub pretty_print {
85 7     7 1 9902 my $self = shift;
86 7         19 my @lines = ();
87 7         25 push @lines, 'DELETE FROM ' . $self->relname;
88              
89 7 100       31 if ( exists $self->{ 'usingClause' } ) {
90 3         11 push @lines, 'USING';
91 3         9 push @lines, map { $self->increase_indent( $_->pretty_print ) . ',' } @{ $self->{ 'usingClause' } };
  3         29  
  3         12  
92              
93             # Remove unnecessary trailing , in last element
94 3         24 $lines[ -1 ] =~ s/,\z//;
95             }
96              
97 7 100       45 if ( exists $self->{ 'whereClause' } ) {
98 4         14 push @lines, 'WHERE';
99 4         27 push @lines, $self->increase_indent( $self->{ 'whereClause' }->pretty_print );
100             }
101              
102 7 100       31 if ( exists $self->{ 'returningList' } ) {
103 2         6 push @lines, 'RETURNING ';
104 2         7 $lines[ -1 ] .= join( ', ', map { $_->pretty_print } @{ $self->{ 'returningList' } } );
  4         19  
  2         7  
105             }
106              
107 7         26 my $main_body = join( "\n", @lines );
108 7 100       48 return $main_body unless exists $self->{ 'withClause' };
109              
110 1         4 my @cte_def = ();
111              
112 1         2 push @cte_def, map { $_->pretty_print . ',' } @{ $self->{ 'withClause' }->{ 'ctes' } };
  1         6  
  1         5  
113              
114             # Remove unnecessary trailing , in last element
115 1         7 $cte_def[ -1 ] =~ s/,\z//;
116 1 50       6 if ( $self->{ 'withClause' }->{ 'recursive' } ) {
117 0         0 $cte_def[ 0 ] = 'WITH RECURSIVE ' . $cte_def[ 0 ];
118             }
119             else {
120 1         2 $cte_def[ 0 ] = 'WITH ' . $cte_def[ 0 ];
121             }
122              
123 1         4 @lines = ();
124 1         5 push @lines, join( ' ', @cte_def );
125 1         3 push @lines, $main_body;
126 1         7 return join( "\n", @lines );
127             }
128              
129             sub relname {
130 14     14 1 33 my $self = shift;
131 14 100       55 if ( !$self->{ '_relname' } ) {
132 7         17 my $R = $self->{ 'relation' };
133 7         47 my @elements = map { $self->quote_ident( $R->{ $_ } ) }
134 7         25 grep { exists $R->{ $_ } } qw{ catalogname schemaname relname };
  21         61  
135 7         37 $self->{ '_relname' } = join( '.', @elements );
136 7 100       32 if ( $R->{ 'alias' }->{ 'aliasname' } ) {
137 1         6 $self->{ '_relname' } .= ' AS ' . $self->quote_ident( $R->{ 'alias' }->{ 'aliasname' } );
138             }
139             }
140 14         54 return $self->{ '_relname' };
141             }
142              
143             1;
144              
145             # vim: set ft=perl: