From: Arthur Axel "fREW" Schmidt Date: Thu, 2 Sep 2010 04:03:05 +0000 (+0000) Subject: rearrange things, looks almost good X-Git-Tag: v1.70~91^2~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e171c446214c4eb647139f8ba9ebe13a048533a8;p=dbsrgits%2FSQL-Abstract.git rearrange things, looks almost good --- diff --git a/lib/SQL/Abstract/Tree.pm b/lib/SQL/Abstract/Tree.pm index 7c207c2..423c2d2 100644 --- a/lib/SQL/Abstract/Tree.pm +++ b/lib/SQL/Abstract/Tree.pm @@ -183,12 +183,22 @@ sub format_keyword { return $keyword } + +my %ghetto_whitespacemap = ( + select => 0, + where => 1, + from => 1, +); + sub whitespace { my ($self, $keyword, $depth) = @_; - if (lc $keyword eq 'from') { - return ['', "\n"]; + + my $before = ''; + my $after = ''; + if (defined $ghetto_whitespacemap{lc $keyword}) { + $before = $self->newline . $self->indent($depth + $ghetto_whitespacemap{lc $keyword}); } - return ['', '']; + return [$before, $after]; } sub newline { "\n" } @@ -198,7 +208,7 @@ sub indent { ' ' x $_[1] } sub unparse { my ($self, $tree, $depth) = @_; - $depth ||= 1; + $depth ||= 0; if (not $tree ) { return ''; @@ -208,23 +218,23 @@ sub unparse { my $cdr = $tree->[1]; if (ref $car) { - return join (" ", map $self->unparse($_), @$tree); + return join ('', map $self->unparse($_, $depth), @$tree); } elsif ($car eq 'LITERAL') { return $cdr->[0]; } elsif ($car eq 'PAREN') { - return '(' . $self->newline . + return '(' . join(' ', - map $self->indent($depth) . $self->unparse($_, $depth + 1), @{$cdr}) - . $self->newline . ')'; + map $self->unparse($_, $depth + 1), @{$cdr}) + . ')'; } elsif ($car eq 'OR' or $car eq 'AND' or (grep { $car =~ /^ $_ $/xi } @binary_op_keywords ) ) { - return join (" $car ", map $self->unparse($_), @{$cdr}); + return join (" $car ", map $self->unparse($_, $depth), @{$cdr}); } else { my ($l, $r) = @{$self->whitespace($car, $depth)}; - return sprintf "%s %s$r", $self->format_keyword($car), $self->unparse($cdr); + return sprintf "$l%s %s$r", $self->format_keyword($car), $self->unparse($cdr, $depth); } }