sub search {
my $self = shift;
my $rs = $self->search_rs( @_ );
- return (wantarray ? $rs->all : $rs);
+
+ my $want = wantarray;
+ if ($want) {
+ return $rs->all;
+ }
+ elsif (defined $want) {
+ return $rs;
+ }
+ else {
+ $self->throw_exception ('->search is *not* a mutator, calling it in void context makes no sense');
+ }
}
=head2 search_rs
If no such constraint is found, C<find> currently defaults to a simple
C<< search->(\%column_values) >> which may or may not do what you expect.
Note that this fallback behavior may be deprecated in further versions. If
-you need to search with arbitrary conditions - use L</search>.
+you need to search with arbitrary conditions - use L</search>. If the query
+resulting from this fallback produces more than one row, a warning to the
+effect is issued, though only the first row is constructed and returned as
+C<$row_object>.
In addition to C<key>, L</find> recognizes and applies standard
L<resultset attributes|/ATTRIBUTES> in the same way as L</search> does.
-If the resulting query produces more than one row, a warning to the effect is
-issued, though only the first row is constructed and returned as C<$row_object>
-
Note that if you have extra concerns about the correctness of the resulting
query you need to specify the C<key> attribute and supply the entire condition
as an argument to find (since it is not always possible to perform the
$final_cond = @unique_queries
? [ map { $self->_qualify_cond_columns($_, $alias) } @unique_queries ]
- : $self->_qualify_cond_columns($call_cond, $alias)
+ : $self->_non_unique_find_fallback ($call_cond, $attrs)
;
}
}
}
+# This is a stop-gap method as agreed during the discussion on find() cleanup:
+# http://lists.scsys.co.uk/pipermail/dbix-class/2010-October/009535.html
+#
+# It is invoked when find() is called in legacy-mode with insufficiently-unique
+# condition. It is provided for overrides until a saner way forward is devised
+#
+# *NOTE* This is not a public method, and it's *GUARANTEED* to disappear down
+# the road. Please adjust your tests accordingly to catch this situation early
+# DBIx::Class::ResultSet->can('_non_unique_find_fallback') is reasonable
+#
+# The method will not be removed without an adequately complete replacement
+# for strict-mode enforcement
+sub _non_unique_find_fallback {
+ my ($self, $cond, $attrs) = @_;
+
+ return $self->_qualify_cond_columns(
+ $cond,
+ exists $attrs->{alias}
+ ? $attrs->{alias}
+ : $self->{attrs}{alias}
+ );
+}
+
+
sub _qualify_cond_columns {
my ($self, $cond, $alias) = @_;
$final_cond = { map { $_ => $final_cond->{$_} } @c_cols };
if (my @missing = grep { ! defined $final_cond->{$_} } (@c_cols) ) {
- $self->throw_exception( sprintf ( "Unable to satisfy constraint '%s', no values for column(s): %s",
+ $self->throw_exception( sprintf ( "Unable to satisfy requested constraint '%s', no values for column(s): %s",
$constraint_name,
join (', ', map { "'$_'" } @missing),
) );
# the tie class for 5.8.1
{
- package DBIx::Class::__DBIC_LAZY_RS_COUNT__;
+ package # hide from pause
+ DBIx::Class::__DBIC_LAZY_RS_COUNT__;
use base qw/Tie::Hash/;
sub FIRSTKEY { my $dummy = scalar keys %{$_[0]{data}}; each %{$_[0]{data}} }
carp ("Useless use of distinct on a grouped resultset ('distinct' is ignored when a 'group_by' is present)");
}
else {
- my $storage = $self->result_source->schema->storage;
- my $rs_column_list = $storage->_resolve_column_info ($attrs->{from});
-
- my $group_spec = $attrs->{group_by} = [];
- my %group_index;
-
- for (@{$attrs->{select}}) {
- if (! ref($_) or ref ($_) ne 'HASH' ) {
- push @$group_spec, $_;
- $group_index{$_}++;
- if ($rs_column_list->{$_} and $_ !~ /\./ ) {
- # add a fully qualified version as well
- $group_index{"$rs_column_list->{$_}{-source_alias}.$_"}++;
- }
- }
- }
- # add any order_by parts that are not already present in the group_by
- # we need to be careful not to add any named functions/aggregates
- # i.e. select => [ ... { count => 'foo', -as 'foocount' } ... ]
- for my $chunk ($storage->_extract_order_columns($attrs->{order_by})) {
-
- # only consider real columns (for functions the user got to do an explicit group_by)
- my $colinfo = $rs_column_list->{$chunk}
- or next;
-
- $chunk = "$colinfo->{-source_alias}.$chunk" if $chunk !~ /\./;
- push @$group_spec, $chunk unless $group_index{$chunk}++;
- }
+ $attrs->{group_by} = $source->storage->_group_over_selection (
+ @{$attrs}{qw/from select order_by/}
+ );
}
}