| 1 | package SQL::Abstract::Formatter; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '2.000003'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | require SQL::Abstract::Parts; # it loads us too, don't cross the streams | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | has indent_by => (is => 'ro', default => ' '); | |||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | has max_width => (is => 'ro', default => 78); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 5277 | ▶ | sub _join { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_join: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 5277 | shift; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 5277 | return SQL::Abstract::Parts::stringify(\@_); | |||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 962 | ▶ | sub format { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 962 | my ($self, $join, @parts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 962 | $self->_fold_sql('', '', @{$self->_simplify($join, @parts)}); | |||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 12404 | ▶ | sub _simplify { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_simplify: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 12404 | my ($self, $join, @parts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 12404 | ▶ | return '' unless @parts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 12404 | ▶ | return $parts[0] if @parts == 1 and !ref($parts[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@parts == 1 and not ref $parts[0]
MC/DC0%0/2
@parts == 1 and not ref $parts[0]
@parts == 1
not ref $parts[0]
Truth table66%2/3
A and not(B)
A= @parts == 1B= not ref $parts[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 12404 | ▶ | return $self->_simplify(@{$parts[0]}) if @parts == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 11250 | ▶ | return [ $join, map ref() ? $self->_simplify(@$_) : $_, @parts ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3176 | ▶ | sub _fold_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fold_sql: called CC=21 SCAR=30.5
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 3176 | my ($self, $indent0, $indent, $join, @parts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 3176 | my @res; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 3176 | my $w = $self->max_width; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 3176 | my $join_len = 0; | |||||||||||||||||||||||||||||||||||||||||
| 36 | (s/, \z/,\n/ and $join_len = 1) | ||||||||||||||||||||||||||||||||||||||||||
| 37 | or s/\A /\n/ | ||||||||||||||||||||||||||||||||||||||||||
| 38 | or $_ = "\n" | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 3176 | ▶ | for my $line_join = $join; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
s/, \z/,\n/ and $join_len = 1 or s/\A /\n/
s/, \z/,\n/ and $join_len = 1
MC/DC0%0/2
s/, \z/,\n/ and $join_len = 1 or s/\A /\n/
s/, \z/,\n/
s/\A /\n/
Truth table66%2/3
A or B
A= s/, \z/,\n/B= s/\A /\n/
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3176 | my ($nl_pre, $nl_post) = split "\n", $line_join, -1; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 3176 | my $line = $indent0; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 3176 | my ($lead0) = $indent0 =~ /\A(\s*)/; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 3176 | my $fresh = 1; # $line holds no part of this level yet | |||||||||||||||||||||||||||||||||||||||||
| 44 | 3176 | my $line_proto = $indent.$nl_post; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 3176 | PART: foreach my $idx (0..$#parts) { | |||||||||||||||||||||||||||||||||||||||||
| 46 | 8017 | my $p = $parts[$idx]; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 8017 | my $last = $idx == $#parts; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 8017 | ▶ | my $pre = $fresh ? '' : $join; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 8017 | ▶ | my $j_part = $pre.(my $j = ref($p) ? $self->_join(@$p) : $p); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 8017 | ▶ | if (length($j_part) + length($line) + $join_len <= $w) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 4204 | $line .= $j_part; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 4204 | $fresh = 0; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 4204 | next PART; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 3813 | ▶ | if (ref($p) and $p->[1] eq '(' and $p->[-1] eq ')') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
ref $p and $p->[1] eq "(" and $p->[-1] eq ")"
ref $p and $p->[1] eq "("
MC/DC66%2/3
ref $p and $p->[1] eq "(" and $p->[-1] eq ")"
ref $p
$p->[1] eq "("
$p->[-1] eq ")"
Truth table75%3/4
A and B and C
A= ref $pB= $p->[1] eq "("C= $p->[-1] eq ")"
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 967 | my ($lead) = $line =~ /\A(\s*)/; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 967 | my $innerdent = $lead.$self->indent_by; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 967 | push @res, $line.$pre.'('."\n"; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 967 | my @inner = @{$p}[2 .. $#$p - 1]; | |||||||||||||||||||||||||||||||||||||||||
| 60 | my $folded = $self->_fold_sql($innerdent, $innerdent, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 967 | ▶ | (@inner == 1 and ref($inner[0])) ? @{$inner[0]} : ($p->[0], @inner) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@inner == 1 and ref $inner[0]
MC/DC50%1/2
@inner == 1 and ref $inner[0]
@inner == 1
ref $inner[0]
Truth table66%2/3
A and B
A= @inner == 1B= ref $inner[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 967 | push @res, $folded."\n"; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 967 | $line = $lead.')'; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 967 | $fresh = 0; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 967 | next PART; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 2846 | ▶ | my $innerdent = (@res ? $indent : $lead0).$self->indent_by; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2846 | ▶ | my $start = $fresh ? $line : $line_proto; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 2846 | ▶ | push @res, $line.$nl_pre."\n" unless $fresh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 2846 | $line = $start.$j; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 2846 | $fresh = 0; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 2846 | ▶ | next PART if !ref($p) or length($line) <= $w; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not ref $p or length $line <= $w
MC/DC100%2/2
not ref $p or length $line <= $w
not ref $p
length $line <= $w
Truth table100%3/3
not(A) or B
A= not ref $pB= length $line <= $w
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 1247 | my $folded = $self->_fold_sql($start, $innerdent, @$p); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 1247 | ▶ | push @res, $folded.($last ? '' : $nl_pre."\n"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 1247 | ▶ | $line = $last ? '' : $line_proto; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 1247 | $fresh = 1; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 3176 | return join '', @res, $line; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||