From: Peter Rabbitson Date: Fri, 11 Sep 2009 22:15:50 +0000 (+0000) Subject: Add single() ro RSC X-Git-Tag: v0.08112~27 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4e55c3ae65c1a0c1ef55043fb44bff52d1a1c5a1;p=dbsrgits%2FDBIx-Class.git Add single() ro RSC --- diff --git a/Changes b/Changes index 42f323f..a0d6efb 100644 --- a/Changes +++ b/Changes @@ -8,9 +8,11 @@ Revision history for DBIx::Class dropped during the serial-autodetection rwrite - Make sure ResultSetColumn does not depend on the (undefined) return value of ->cursor->reset() + - Add single() to ResultSetColumn (same semantics as ResultSet) - Make sure to turn off IDENTITY_INSERT after insert() on MSSQL tables that needed it - More informative exception on failing _resolve_relationship + - Fix unreported rollback exceptions in TxnScopeGuard 0.08111 2009-09-06 21:58:00 (UTC) - The hashref to connection_info now accepts a 'dbh_maker' diff --git a/lib/DBIx/Class/ResultSetColumn.pm b/lib/DBIx/Class/ResultSetColumn.pm index 53b0920..ee1ed04 100644 --- a/lib/DBIx/Class/ResultSetColumn.pm +++ b/lib/DBIx/Class/ResultSetColumn.pm @@ -207,6 +207,33 @@ sub first { return $row; } +=head2 single + +=over 4 + +=item Arguments: none + +=item Return Value: $value + +=back + +Much like L fetches one and only one column +value using the cursor directly. If additional rows are present a warning +is issued before discarding the cursor. + +=cut + +sub single { + my $self = shift; + + my $attrs = $self->_resultset->_resolved_attrs; + my ($row) = $self->_resultset->result_source->storage->select_single( + $attrs->{from}, $attrs->{select}, $attrs->{where}, $attrs + ); + + return $row; +} + =head2 min =over 4 diff --git a/t/88result_set_column.t b/t/88result_set_column.t index dde817c..c744121 100644 --- a/t/88result_set_column.t +++ b/t/88result_set_column.t @@ -1,7 +1,8 @@ use strict; -use warnings; +use warnings; use Test::More; +use Test::Warn; use Test::Exception; use lib qw(t/lib); use DBICTest; @@ -31,6 +32,10 @@ is($rs_year->next, 1999, "reset okay"); is($rs_year->first, 1999, "first okay"); +warnings_exist (sub { + is($rs_year->single, 1999, "single okay"); +}, qr/Query returned more than one row/, 'single warned'); + # test +select/+as for single column my $psrs = $schema->resultset('CD')->search({}, {