From: Matt S Trout Date: Mon, 16 Jan 2006 23:33:44 +0000 (+0000) Subject: Made columns ordered by default X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=20518cb4f1edcd17aae541e4ad9c44f85c7cdfcd;p=dbsrgits%2FDBIx-Class-Historic.git Made columns ordered by default --- diff --git a/lib/DBIx/Class/Componentised.pm b/lib/DBIx/Class/Componentised.pm index 2ee9094..a642f85 100644 --- a/lib/DBIx/Class/Componentised.pm +++ b/lib/DBIx/Class/Componentised.pm @@ -8,6 +8,11 @@ sub inject_base { no strict 'refs'; unshift(@{"${target}::ISA"}, grep { $target ne $_ && !$target->isa($_)} @to_inject); } + + # Yes, this is hack. But it *does* work. Please don't submit tickets about + # it on the basis of the comments in Class::C3, the author was on #dbix-class + # while I was implementing this. + my $table = { Class::C3::_dump_MRO_table }; eval "package $target; import Class::C3;" unless exists $table->{$target}; Class::C3::reinitialize() if defined $table->{$target}; diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 285d10f..8d0b0d8 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -44,15 +44,19 @@ sub add_columns { my ($self, @cols) = @_; $self->_ordered_columns( \@cols ) if !$self->_ordered_columns; - push @{ $self->_ordered_columns }, @cols; + my @added; + my $columns = $self->_columns; while (my $col = shift @cols) { my $column_info = ref $cols[0] ? shift : {}; # If next entry is { ... } use that for the column info, if not # use an empty hashref - $self->_columns->{$col} = $column_info; + push(@added, $col) unless exists $columns->{$col}; + + $columns->{$col} = $column_info; } + push @{ $self->_ordered_columns }, @added; } *add_column = \&add_columns; @@ -108,25 +112,14 @@ sub column_info { =head2 columns - my @column_names = $obj->columns; + my @column_names = $obj->columns; + +Returns all column names in the order they were declared to add_columns =cut sub columns { croak "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1); - 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}||[]}; } diff --git a/lib/DBIx/Class/TableInstance.pm b/lib/DBIx/Class/TableInstance.pm index 6a44c84..32c6886 100644 --- a/lib/DBIx/Class/TableInstance.pm +++ b/lib/DBIx/Class/TableInstance.pm @@ -56,6 +56,8 @@ sub table { }); if ($class->can('result_source_instance')) { $table->{_columns} = { %{$class->result_source_instance->{_columns}||{}} }; + $table->{_ordered_columns} = + [ @{$class->result_source_instance->{_ordered_columns}||[]} ]; } } $class->mk_classdata('result_source_instance' => $table); diff --git a/t/run/01core.tl b/t/run/01core.tl index ce41f16..1fbb4eb 100644 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@ -91,10 +91,10 @@ is($cd->year, 2005, 'set_columns ok'); $cd->discard_changes; -# check whether ResultSource->ordered_columns returns columns in order originally supplied -my @cd = $schema->source("CD")->ordered_columns; +# check whether ResultSource->columns returns columns in order originally supplied +my @cd = $schema->source("CD")->columns; -is_deeply( \@cd, [qw/cdid artist title year/], 'ordered_columns'); +is_deeply( \@cd, [qw/cdid artist title year/], 'column order'); $cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { cols => ['title'] })->next; is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');