From: Matt S Trout Date: Sun, 5 Feb 2006 23:25:57 +0000 (+0000) Subject: add $source->resultset_attributes, include_columns rs attr X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5ac6a04477849fabc50271c5b7ab15a080ae0109;p=dbsrgits%2FDBIx-Class-Historic.git add $source->resultset_attributes, include_columns rs attr --- diff --git a/Changes b/Changes index 4cbc7cc..cf6d4c6 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,12 @@ Revision history for DBIx::Class +0.05001 + - added experimental Serialize and ResultSetManager components + - fix to find() for complex resultsets + - added of $storage->debugcb(sub { ... }) + - added $source->resultset_attributes accessor + - added include_columns rs attr + 0.05000 2006-02-01 16:48:30 - assorted doc fixes - remove ObjectCache, not yet working in 0.05 diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index 12fe447..6f2f893 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -13,7 +13,7 @@ sub component_base_class { 'DBIx::Class' } # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports # brain damage and presumably various other packaging systems too -$VERSION = '0.05000'; +$VERSION = '0.05001'; sub MODIFY_CODE_ATTRIBUTES { my ($class,$code,@attrs) = @_; diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index c7a9cc5..c65dc0b 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -78,6 +78,10 @@ sub new { $attrs->{select} = [ map { m/\./ ? $_ : "${alias}.$_" } @cols ]; } $attrs->{as} ||= [ map { m/^$alias\.(.*)$/ ? $1 : $_ } @{$attrs->{select}} ]; + if (my $include = delete $attrs->{include_columns}) { + push(@{$attrs->{select}}, @$include); + push(@{$attrs->{as}}, map { m/([^\.]+)$/; $1; } @$include); + } #use Data::Dumper; warn Dumper(@{$attrs}{qw/select as/}); $attrs->{from} ||= [ { $alias => $source->from } ]; if (my $join = delete $attrs->{join}) { @@ -720,6 +724,14 @@ Shortcut to request a particular set of columns to be retrieved. Adds C onto the start of any column without a C<.> in it and sets C as normal. +=head2 include_columns (arrayref) + +Shortcut to include additional columns in the returned results - for example + + { include_columns => ['foo.name'], join => ['foo'] } + +would add a 'name' column to the information passed to object inflation + =head2 select (arrayref) Indicates which columns should be selected from the storage. You can use diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 5def68f..ceb5d36 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -12,7 +12,7 @@ use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/AccessorGroup/); __PACKAGE__->mk_group_accessors('simple' => - qw/_ordered_columns _columns _primaries _unique_constraints name resultset_class result_class schema from _relationships/); + qw/_ordered_columns _columns _primaries _unique_constraints name resultset_class resultset_attributes result_class schema from _relationships/); =head1 NAME @@ -34,6 +34,7 @@ sub new { $class = ref $class if ref $class; my $new = bless({ %{$attrs || {}} }, $class); $new->{resultset_class} ||= 'DBIx::Class::ResultSet'; + $new->{resultset_attributes} = { %{$new->{resultset_attributes} || {}} }; $new->{_ordered_columns} = [ @{$new->{_ordered_columns}||[]}]; $new->{_columns} = { %{$new->{_columns}||{}} }; $new->{_relationships} = { %{$new->{_relationships}||{}} }; @@ -41,6 +42,23 @@ sub new { return $new; } +=head2 add_columns + + $table->add_columns(qw/col1 col2 col3/); + + $table->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...); + +Adds columns to the result source. If supplied key => hashref pairs uses +the hashref as the column_info for that column. + +=head2 add_column + + $table->add_column('col' => \%info?); + +Convenience alias to add_columns + +=cut + sub add_columns { my ($self, @cols) = @_; $self->_ordered_columns( \@cols ) @@ -63,28 +81,6 @@ sub add_columns { *add_column = \&add_columns; -=head2 add_columns - - $table->add_columns(qw/col1 col2 col3/); - - $table->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...); - -Adds columns to the result source. If supplied key => hashref pairs uses -the hashref as the column_info for that column. - -=head2 add_column - - $table->add_column('col' => \%info?); - -Convenience alias to add_columns - -=cut - -sub resultset { - my $self = shift; - return $self->resultset_class->new($self); -} - =head2 has_column if ($obj->has_column($col)) { ... } @@ -415,7 +411,7 @@ array of column names for each of those relationships. Column names are prefixed relative to the current source, in accordance with where they appear in the supplied relationships. Examples: - my $source = $schema->$resultset('Tag')->source; + my $source = $schema->resultset('Tag')->source; @columns = $source->resolve_prefetch( { cd => 'artist' } ); # @columns = @@ -496,7 +492,28 @@ sub related_source { return $self->schema->source($self->relationship_info($rel)->{source}); } -1; +=head2 resultset + +Returns a resultset for the given source created by calling + +$self->resultset_class->new($self, $self->resultset_attributes) + +=head2 resultset_class + +Simple accessor. + +=head2 resultset_attributes + +Simple accessor. + +=cut + +sub resultset { + my $self = shift; + return $self->resultset_class->new($self, $self->{resultset_attributes}); +} + +=cut =head2 throw_exception diff --git a/lib/DBIx/Class/ResultSourceProxy.pm b/lib/DBIx/Class/ResultSourceProxy.pm index 5aed87b..964805a 100644 --- a/lib/DBIx/Class/ResultSourceProxy.pm +++ b/lib/DBIx/Class/ResultSourceProxy.pm @@ -5,9 +5,13 @@ use warnings; use base qw/DBIx::Class/; -sub iterator_class { shift->result_source_instance->resultset_class(@_) } +sub iterator_class { shift->result_source_instance->resultset_class(@_) } sub resultset_class { shift->result_source_instance->resultset_class(@_) } +sub resultset_attributes { + shift->result_source_instance->resultset_attributes(@_); +} + sub add_columns { my ($class, @cols) = @_; my $source = $class->result_source_instance; diff --git a/t/run/01core.tl b/t/run/01core.tl index 41a3941..412ac8f 100644 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@ -1,7 +1,7 @@ sub run_tests { my $schema = shift; -plan tests => 36; +plan tests => 38; my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); @@ -99,6 +99,11 @@ 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'); +$cd = $schema->resultset("CD")->search(undef, { include_columns => [ 'artist.name' ], join => [ 'artist' ] })->find(1); + +is($cd->title, 'Spoonful of bees', 'Correct CD returned with include'); +is($cd->get_column('name'), 'Caterwauler McCrae', 'Additional column returned'); + # insert_or_update $new = $schema->resultset("Track")->new( { trackid => 100,