From: Matt S Trout Date: Fri, 12 Apr 2019 20:45:58 +0000 (+0000) Subject: join support X-Git-Tag: v2.000000~3^2~251 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f79455dc5beb9dd9534c9234b4519504788bb618;p=dbsrgits%2FSQL-Abstract.git join support --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 78407a5..1b021d2 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -590,7 +590,10 @@ sub _expand_hashpair { if ($k =~ /^-/) { return $self->_expand_hashpair_op($k, $v); } elsif ($k =~ /^[^\w]/i) { - return $self->_expand_op(-op, [ $k, @$v ]); + my ($lhs, @rhs) = @$v; + return $self->_expand_op( + -op, [ $k, $self->expand_expr($lhs, -ident), @rhs ] + ); } return $self->_expand_hashpair_ident($k, $v); } diff --git a/lib/SQL/Abstract/ExtraClauses.pm b/lib/SQL/Abstract/ExtraClauses.pm index 9e5ddff..a4da3d7 100644 --- a/lib/SQL/Abstract/ExtraClauses.pm +++ b/lib/SQL/Abstract/ExtraClauses.pm @@ -18,9 +18,81 @@ sub new { $_[0]->_expand_maybe_list_expr($_[1], -ident) }; $new->{expand_clause}{'select.having'} = sub { - $_[0]->expand_expr($_[1], -ident) + $_[0]->expand_expr($_[1]) }; + $new->{expand}{from_list} = '_expand_from_list'; + $new->{render}{from_list} = '_render_from_list'; + $new->{expand}{join} = '_expand_join'; + $new->{render}{join} = '_render_join'; return $new; } +sub _expand_select_clause_from { + my ($self, $from) = @_; + +(from => $self->_expand_from_list(undef, $from)); +} + +sub _expand_from_list { + my ($self, undef, $args) = @_; + if (ref($args) eq 'HASH') { + return { -from_list => [ $self->expand_expr($args) ] }; + } + my @list; + my @args = ref($args) ? @$args : ($args); + while (my $entry = shift @args) { + if (!ref($entry) and $entry =~ /^-/) { + $entry = { $entry => shift @args }; + } + my $aqt = $self->expand_expr($entry, -ident); + if ($aqt->{-join} and not $aqt->{-join}{from}) { + $aqt->{-join}{from} = pop @list; + } + push @list, $aqt; + } + return { -from_list => \@list }; +} + +sub _expand_join { + my ($self, undef, $args) = @_; + my %proto = ( + ref($args) eq 'HASH' + ? %$args + : (to => $args->[0], @{$args}[1..$#$args]) + ); + my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)), + sort keys %proto; + return +{ -join => \%ret }; +} + +sub _render_from_list { + my ($self, $list) = @_; + return $self->_join_parts(', ', map [ $self->render_aqt($_) ], @$list); +} + +sub _render_join { + my ($self, $args) = @_; + + my @parts = ( + [ $self->render_aqt($args->{from}) ], + [ $self->_sqlcase( + ($args->{type} + ? join(' ', split '_', $args->{type}).' ' + : '' + ) + .'join' + ) + ], + [ $self->render_aqt($args->{to}) ], + ($args->{on} ? ( + [ $self->_sqlcase('on') ], + [ $self->render_aqt($args->{on}) ], + ) : ()), + ($args->{using} ? ( + [ $self->_sqlcase('using') ], + [ $self->render_aqt($args->{using}) ], + ) : ()), + ); + return $self->_join_parts(' ', @parts); +} + 1;