From: Justin Hunter Date: Mon, 11 May 2009 20:41:21 +0000 (+0000) Subject: warn/die based on { select => { distinct => { } } } X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7655b3c9676a47fcdeb490e7ba3f28def301537a;p=dbsrgits%2FDBIx-Class-Historic.git warn/die based on { select => { distinct => { } } } --- diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index a1bb464..f3d870d 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -223,6 +223,22 @@ sub _recurse_fields { } @$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) { + die "Unsupported syntax, please use " . + "{ group_by => [ qw/" . (join ' ', @$_fields) . "/ ] }" . + " or " . + "{ select => [ qw/" . (join ' ', @$_fields) . "/ ], distinct => 1 }"; + } + else { + warn "This syntax will be deprecated in 09, please use " . + "{ group_by => '${_fields}' }" . + " or " . + "{ select => '${_fields}', distinct => 1 }"; + } + } + return $self->_sqlcase($func) .'( '.$self->_recurse_fields($fields->{$func}).' )'; } diff --git a/t/count/count_distinct.t b/t/count/count_distinct.t index 958ce6d..62afa8d 100644 --- a/t/count/count_distinct.t +++ b/t/count/count_distinct.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use Test::Exception; use lib qw(t/lib); @@ -12,7 +13,7 @@ my $schema = DBICTest->init_schema(); eval "use DBD::SQLite"; plan skip_all => 'needs DBD::SQLite for testing' if $@; -plan tests => 16; +plan tests => 18; # The tag Blue is assigned to cds 1 2 3 and 5 # The tag Cheesy is assigned to cds 2 4 and 5 @@ -69,3 +70,13 @@ is($rs->count, 5, 'Count with literal SQL and another single group_by'); $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] }); is($rs->count, 7, 'Count with literal SQL and multiple group_by'); + +my @warnings; +{ + local $SIG{__WARN__} = sub { push @warnings, shift }; + my $row = $schema->resultset('Tag')->search({}, { select => { distinct => 'tag' } })->first; +} + +is(@warnings, 1, 'expecteing warn'); + +dies_ok(sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first }, 'expecting to die');