From: Peter Rabbitson Date: Sat, 23 May 2009 22:50:08 +0000 (+0000) Subject: Extend distinct deprecation tests and clarify warnings X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=04ebc6f4d9b90ad04258ef202c3879b509dd0b09;p=dbsrgits%2FDBIx-Class-Historic.git Extend distinct deprecation tests and clarify warnings --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 05e4c00..d91d944 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -856,10 +856,10 @@ instead. An example conversion is: sub search_like { my $class = shift; - carp join ("\n", - 'search_like() is deprecated and will be removed in 0.09.', - 'Instead use ->search({ x => { -like => "y%" } })', - '(note the outer pair of {}s - they are important!)' + carp ( + 'search_like() is deprecated and will be removed in DBIC version 0.09.' + .' Instead use ->search({ x => { -like => "y%" } })' + .' (note the outer pair of {}s - they are important!)' ); my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}); my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_}; diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index 90d41b2..65e1a4f 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -193,20 +193,20 @@ sub _recurse_fields { if ($func eq 'distinct') { my $_fields = $fields->{$func}; if (ref $_fields eq 'ARRAY' && @{$_fields} > 1) { - croak "Unsupported syntax, please use " . - "{ group_by => [ qw/" . (join ' ', @$_fields) . "/ ] }" . - " or " . - "{ select => [ qw/" . (join ' ', @$_fields) . "/ ], distinct => 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 "This syntax will be deprecated in 09, please use " . - "{ group_by => '${_fields}' }" . - " or " . - "{ select => '${_fields}', distinct => 1 }"; + carp ( + 'The select => { distinct => ... } syntax will be deprecated in DBIC version 0.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/distinct.t b/t/count/distinct.t index 04900d2..8e956b5 100644 --- a/t/count/distinct.t +++ b/t/count/distinct.t @@ -77,14 +77,23 @@ is($rs->count, 4, 'Count with +select aggreggate'); $rs = $schema->resultset('Tag')->search({}, { select => 'length(me.tag)', distinct => 1 }); is($rs->count, 3, 'Count by distinct function result as select literal'); -my @warnings; -{ - local $SIG{__WARN__} = sub { push @warnings, shift }; +eval { + my @warnings; + local $SIG{__WARN__} = sub { $_[0] =~ /The select => { distinct => ... } syntax will be deprecated/ + ? push @warnings, @_ + : warn @_ + }; my $row = $schema->resultset('Tag')->search({}, { select => { distinct => 'tag' } })->first; -} + is (@warnings, 1, 'Warned about deprecated distinct') if $DBIx::Class::VERSION < 0.09; +}; +ok ($@, 'Exception on deprecated distinct usage thrown') if $DBIx::Class::VERSION >= 0.09; -is(@warnings, 1, 'expecteing warn'); +throws_ok( + sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first }, + qr/select => { distinct => ... } syntax is not supported for multiple columns/, + 'throw on unsupported syntax' +); -dies_ok(sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first }, 'expecting to die'); +# These two rely on the database to throw an exception. This might not be the case one day. Please revise. dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die'); dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { select => { length => 'tag' }, distinct => 1 })->count }, 'expecting to die');