From: Matt S Trout Date: Fri, 13 Jan 2006 20:26:50 +0000 (+0000) Subject: ordered_columns patch from Will Hawes X-Git-Tag: v0.05005~117^2~65 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=571dced30707f6cf20e15f8eee0e4cb987c45fd1;p=dbsrgits%2FDBIx-Class.git ordered_columns patch from Will Hawes --- diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 753bc45..a7a802b 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -11,7 +11,7 @@ use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/AccessorGroup/); __PACKAGE__->mk_group_accessors('simple' => - qw/_columns _primaries name resultset_class result_class schema from/); + qw/_ordered_columns _columns _primaries name resultset_class result_class schema from/); =head1 NAME @@ -33,6 +33,7 @@ sub new { $class = ref $class if ref $class; my $new = bless({ %{$attrs || {}} }, $class); $new->{resultset_class} ||= 'DBIx::Class::ResultSet'; + $new->{_ordered_columns} ||= []; $new->{_columns} ||= {}; $new->{name} ||= "!!NAME NOT SET!!"; return $new; @@ -40,6 +41,9 @@ sub new { sub add_columns { my ($self, @cols) = @_; + $self->_ordered_columns( \@cols ) + if !$self->_ordered_columns; + push @{ $self->_ordered_columns }, @cols; while (my $col = shift @cols) { $self->_columns->{$col} = (ref $cols[0] ? shift : {}); } @@ -107,6 +111,19 @@ sub columns { return keys %{shift->_columns}; } +=head2 ordered_columns + + my @column_names = $obj->ordered_columns; + +Like columns(), but returns column names using the order in which they were +originally supplied to add_columns(). + +=cut + +sub ordered_columns { + return @{shift->{_ordered_columns}||[]}; +} + =head2 set_primary_key(@cols) Defines one or more columns as primary key for this source. Should be diff --git a/t/run/01core.tl b/t/run/01core.tl index a038332..4337556 100644 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@ -1,6 +1,6 @@ sub run_tests { -plan tests => 33; +plan tests => 34; my @art = DBICTest->class("Artist")->search({ }, { order_by => 'name DESC'}); @@ -90,6 +90,11 @@ is($cd->year, 2005, 'set_columns ok'); $cd->discard_changes; +# check whether ResultSource->ordered_columns returns columns in order originally supplied +my @cd = DBICTest->class("CD")->find(1)->result_source->ordered_columns; + +is_deeply( \@cd, [qw/cdid artist title year/], 'ordered_columns'); + $cd = DBICTest->class("CD")->search({ title => 'Spoonful of bees' }, { cols => ['title'] })->next; is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');