X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSQLAHacks.pm;h=f44ed4f462572398842fca02bfd8e8db08968ecc;hb=aa82ce29dcf525f22133a0c6bc9cd9c611767792;hp=61ff365c7fc5e54bf2bdf6c8c3edac2147786e9b;hpb=6ca5c92b53be82e8898c2da3dda0cb5e5cde5674;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index 61ff365..f44ed4f 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -405,35 +405,33 @@ sub _recurse_fields { } elsif ($ref eq 'HASH') { my %hash = %$fields; - my ($select, $as); - if ($hash{-select}) { - $select = $self->_recurse_fields (delete $hash{-select}); - $as = $self->_quote (delete $hash{-as}); - } - else { - my ($func, $args) = each %hash; - delete $hash{$func}; - - if (lc ($func) eq 'distinct' && ref $args eq 'ARRAY' && @$args > 1) { - croak ( - 'The select => { distinct => ... } syntax is not supported for multiple columns.' - .' Instead please use { group_by => [ qw/' . (join ' ', @$args) . '/ ] }' - .' or { select => [ qw/' . (join ' ', @$args) . '/ ], distinct => 1 }' - ); - } - $select = sprintf ('%s( %s )', - $self->_sqlcase($func), - $self->_recurse_fields($args) + my $as = delete $hash{-as}; # if supplied + + my ($func, $args) = each %hash; + delete $hash{$func}; + + if (lc ($func) eq 'distinct' && ref $args eq 'ARRAY' && @$args > 1) { + croak ( + 'The select => { distinct => ... } syntax is not supported for multiple columns.' + .' Instead please use { group_by => [ qw/' . (join ' ', @$args) . '/ ] }' + .' or { select => [ qw/' . (join ' ', @$args) . '/ ], distinct => 1 }' ); } + my $select = sprintf ('%s( %s )%s', + $self->_sqlcase($func), + $self->_recurse_fields($args), + $as + ? sprintf (' %s %s', $self->_sqlcase('as'), $as) + : '' + ); + # there should be nothing left if (keys %hash) { croak "Malformed select argument - too many keys in hash: " . join (',', keys %$fields ); } - $select .= " AS $as" if $as; return $select; } # Is the second check absolutely necessary? @@ -510,15 +508,21 @@ sub _recurse_from { foreach my $j (@join) { my ($to, $on) = @$j; + # check whether a join type exists - my $join_clause = ''; my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to; - if (ref($to_jt) eq 'HASH' and exists($to_jt->{-join_type})) { - $join_clause = ' '.uc($to_jt->{-join_type}).' JOIN '; - } else { - $join_clause = ' JOIN '; + my $join_type; + if (ref($to_jt) eq 'HASH' and defined($to_jt->{-join_type})) { + $join_type = $to_jt->{-join_type}; + $join_type =~ s/^\s+ | \s+$//xg; } - push(@sqlf, $join_clause); + + $join_type ||= $self->_default_jointype; + + my $join_clause = sprintf ('%s JOIN ', + $join_type ? ' ' . uc($join_type) : '' + ); + push @sqlf, $join_clause; if (ref $to eq 'ARRAY') { push(@sqlf, '(', $self->_recurse_from(@$to), ')'); @@ -530,6 +534,8 @@ sub _recurse_from { return join('', @sqlf); } +sub _default_jointype {}; + sub _fold_sqlbind { my ($self, $sqlbind) = @_;