X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSQLAHacks.pm;h=2698ab7cbfccdcf42d1fec5b425e9aa27e50090e;hb=1e1ce814212b6f20cd7e16f17249d33dd7502692;hp=0494a6a75347324b9ee00c27b7eaedfcae627afa;hpb=893403c81741764c51e7cfff69a24d427d083c33;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index 0494a6a..2698ab7 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -281,7 +281,7 @@ sub __record_alias { return unless $col; - # record unqialified name, undef (no adjustment) if a duplicate is found + # record unqualified name, undef (no adjustment) if a duplicate is found if (exists $register->{$col}) { $register->{$col} = undef; } @@ -382,28 +382,39 @@ sub _recurse_fields { ? ' AS col'.$self->{rownum_hack_count}++ : '') } @$fields); - } elsif ($ref eq 'HASH') { - foreach my $func (keys %$fields) { - if ($func eq 'distinct') { - my $_fields = $fields->{$func}; - if (ref $_fields eq 'ARRAY' && @{$_fields} > 1) { - croak ( - 'The select => { distinct => ... } syntax is not supported for multiple columns.' - .' Instead please use { group_by => [ qw/' . (join ' ', @$_fields) . '/ ] }' - .' or { select => [ qw/' . (join ' ', @$_fields) . '/ ], distinct => 1 }' - ); - } - else { - $_fields = @{$_fields}[0] if ref $_fields eq 'ARRAY'; - carp ( - 'The select => { distinct => ... } syntax will be deprecated in DBIC version 0.09,' - ." please use { group_by => '${_fields}' } or { select => '${_fields}', distinct => 1 }" - ); - } + } + 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 }' + ); } - return $self->_sqlcase($func) - .'( '.$self->_recurse_fields($fields->{$func}).' )'; + $select = sprintf ('%s( %s )', + $self->_sqlcase($func), + $self->_recurse_fields($args) + ); + } + + # 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? elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) { @@ -421,9 +432,8 @@ sub _order_by { my $ret = ''; - if (defined $arg->{group_by}) { - $ret = $self->_sqlcase(' group by ') - .$self->_recurse_fields($arg->{group_by}, { no_rownum_hack => 1 }); + if (my $g = $self->_recurse_fields($arg->{group_by}, { no_rownum_hack => 1 }) ) { + $ret = $self->_sqlcase(' group by ') . $g; } if (defined $arg->{having}) {