From: Matt S Trout Date: Sat, 11 Feb 2006 17:33:00 +0000 (+0000) Subject: Added ->single method to ResultSet X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a04ab2850bc896f51f8ccb052e6c8269aa1f3fea;p=dbsrgits%2FDBIx-Class-Historic.git Added ->single method to ResultSet --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index dce4ad5..3ded443 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -240,7 +240,9 @@ sub find { $query->{$self->{attrs}{alias}.'.'.$_} = delete $query->{$_}; } #warn Dumper($query); - return $self->search($query,$attrs)->next; + return (keys %$attrs + ? $self->search($query,$attrs)->single + : $self->single($query)); } =head2 search_related @@ -286,6 +288,34 @@ sub cursor { $attrs->{where},$attrs); } +=head2 single + +Inflates the first result without creating a cursor + +=cut + +sub single { + my ($self, $extra) = @_; + my ($attrs) = $self->{attrs}; + $attrs = { %$attrs }; + if ($extra) { + if (defined $attrs->{where}) { + $attrs->{where} = { + '-and' + => [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ } + delete $attrs->{where}, $extra ] + }; + } else { + $attrs->{where} = $extra; + } + } + my @data = $self->result_source->storage->select_single( + $self->{from}, $attrs->{select}, + $attrs->{where},$attrs); + return (@data ? $self->_construct_object(@data) : ()); +} + + =head2 search_like Perform a search, but use C instead of equality as the condition. Note