From: David Kamholz Date: Wed, 8 Mar 2006 20:22:39 +0000 (+0000) Subject: rename columns attr to cols X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5e8b1b2adbc7b67dc7948a41354e36ce0c7998c6;p=dbsrgits%2FDBIx-Class-Historic.git rename columns attr to cols clean up ResultSet new() a little normalize some doc examples by passing undef as first value to search --- diff --git a/Changes b/Changes index 313caad..012721f 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for DBIx::Class - - Added has_column_loaded to Row + - renamed cols attribute to columns (cols still supported) + - added has_column_loaded to Row - Storage::DBI connect_info supports coderef returning dbh as 1st arg - add_components() doesn't prepend base when comp. prefixed with + - $schema->deploy @@ -70,10 +71,10 @@ Revision history for DBIx::Class - count will now work for grouped resultsets - added accessor => option to column_info to specify accessor name - added $schema->populate to load test data (similar to AR fixtures) - - removed cdbi-t dependencies, only run tests if installed - - Removed DBIx::Class::Exception - - unified throw_exception stuff, using Carp::Clan - - report query when sth generation fails. + - removed cdbi-t dependencies, only run tests if installed + - Removed DBIx::Class::Exception + - unified throw_exception stuff, using Carp::Clan + - report query when sth generation fails. - multi-step prefetch! - inheritance fixes - test tweaks @@ -96,7 +97,7 @@ Revision history for DBIx::Class - made Storage::DBI use prepare_cached safely (thanks to Tim Bunce) - many documentation improvements (thanks guys!) - added ->connection, ->connect, ->register_source and ->clone schema methods - - Use croak instead of die for user errors. + - Use croak instead of die for user errors. 0.04999_02 2006-01-14 07:17:35 - Schema is now self-contained; no requirement for co-operation @@ -139,7 +140,7 @@ Revision history for DBIx::Class 0.03004 - Added an || '' to the CDBICompat stringify to avoid null warnings - - Updated name section for manual pods + - Updated name section for manual pods 0.03003 2005-11-03 17:00:00 - POD fixes. - Changed use to require in Relationship/Base to avoid import. diff --git a/lib/DBIx/Class/CDBICompat/LazyLoading.pm b/lib/DBIx/Class/CDBICompat/LazyLoading.pm index 8a7c17b..3c629ab 100644 --- a/lib/DBIx/Class/CDBICompat/LazyLoading.pm +++ b/lib/DBIx/Class/CDBICompat/LazyLoading.pm @@ -6,7 +6,7 @@ use warnings; sub resultset_instance { my $self = shift; my $rs = $self->next::method(@_); - $rs = $rs->search(undef, { cols => [ $self->columns('Essential') ] }); + $rs = $rs->search(undef, { columns => [ $self->columns('Essential') ] }); return $rs; } diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index 2552b92..715c8f8 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -12,7 +12,7 @@ When you expect a large number of results, you can ask L for a paged resultset, which will fetch only a small number of records at a time: my $rs = $schema->resultset('Artist')->search( - {}, + undef, { page => 1, # page to return (defaults to 1) rows => 10, # number of results per page @@ -24,7 +24,7 @@ paged resultset, which will fetch only a small number of records at a time: The C attribute does not have to be specified in your search: my $rs = $schema->resultset('Artist')->search( - {}, + undef, { rows => 10, } @@ -76,9 +76,9 @@ When you only want selected columns from a table, you can use C to specify which ones you need: my $rs = $schema->resultset('Artist')->search( - {}, + undef, { - cols => [qw/ name /] + columns => [qw/ name /] } ); @@ -94,7 +94,7 @@ stored procedure name). You then use C to set the column name you will use to access the returned value: my $rs = $schema->resultset('Artist')->search( - {}, + undef, { select => [ 'name', { LENGTH => 'name' } ], as => [qw/ name name_length /], @@ -129,7 +129,7 @@ any of your aliases using either of these: =head3 SELECT DISTINCT with multiple columns my $rs = $schema->resultset('Foo')->search( - {}, + undef, { select => [ { distinct => [ $source->columns ] } @@ -141,7 +141,7 @@ any of your aliases using either of these: =head3 SELECT COUNT(DISTINCT colname) my $rs = $schema->resultset('Foo')->search( - {}, + undef, { select => [ { count => { distinct => 'colname' } } @@ -155,7 +155,7 @@ any of your aliases using either of these: L supports C as follows: my $rs = $schema->resultset('Artist')->search( - {}, + undef, { join => [qw/ cds /], select => [ 'name', { count => 'cds.cdid' } ], @@ -330,7 +330,7 @@ From 0.04999_05 onwards, C can be nested more than one relationship deep using the same syntax as a multi-step join: my $rs = $schema->resultset('Tag')->search( - {}, + undef, { prefetch => { cd => 'artist' diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 7109e39..3df6fe1 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -69,29 +69,30 @@ automatically get one from e.g. a L called in scalar context: sub new { my $class = shift; return $class->new_result(@_) if ref $class; + my ($source, $attrs) = @_; #use Data::Dumper; warn Dumper($attrs); $attrs = Storable::dclone($attrs || {}); # { %{ $attrs || {} } }; - my %seen; my $alias = ($attrs->{alias} ||= 'me'); - if ($attrs->{cols} || !$attrs->{select}) { - delete $attrs->{as} if $attrs->{cols}; - my @cols = ($attrs->{cols} - ? @{delete $attrs->{cols}} - : $source->columns); - $attrs->{select} = [ map { m/\./ ? $_ : "${alias}.$_" } @cols ]; + + $attrs->{columns} ||= delete $attrs->{cols} if $attrs->{cols}; + $attrs->{columns} ||= [ $source->columns ] unless $attrs->{select}; + if ($attrs->{columns}) { + delete $attrs->{as}; + $attrs->{select} = [ map { m/\./ ? $_ : "${alias}.$_" } @{delete $attrs->{columns}} ]; } - $attrs->{as} ||= [ map { m/^$alias\.(.*)$/ ? $1 : $_ } @{$attrs->{select}} ]; + $attrs->{as} ||= [ map { m/^\Q$alias.\E(.+)$/ ? $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 } ]; $attrs->{seen_join} ||= {}; + my %seen; if (my $join = delete $attrs->{join}) { - foreach my $j (ref $join eq 'ARRAY' - ? (@{$join}) : ($join)) { + foreach my $j (ref $join eq 'ARRAY' ? @$join : ($join)) { if (ref $j eq 'HASH') { $seen{$_} = 1 foreach keys %$j; } else { @@ -100,38 +101,33 @@ sub new { } push(@{$attrs->{from}}, $source->resolve_join($join, $attrs->{alias}, $attrs->{seen_join})); } + $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct}; - - $attrs->{order_by} = [ $attrs->{order_by} ] - if $attrs->{order_by} && !ref($attrs->{order_by}); + $attrs->{order_by} = [ $attrs->{order_by} ] if !ref($attrs->{order_by}); $attrs->{order_by} ||= []; my $collapse = $attrs->{collapse} || {}; - if (my $prefetch = delete $attrs->{prefetch}) { my @pre_order; - foreach my $p (ref $prefetch eq 'ARRAY' - ? (@{$prefetch}) : ($prefetch)) { - if( ref $p eq 'HASH' ) { + foreach my $p (ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch)) { + if ( ref $p eq 'HASH' ) { foreach my $key (keys %$p) { push(@{$attrs->{from}}, $source->resolve_join($p, $attrs->{alias})) unless $seen{$key}; } - } - else { + } else { push(@{$attrs->{from}}, $source->resolve_join($p, $attrs->{alias})) unless $seen{$p}; } my @prefetch = $source->resolve_prefetch( $p, $attrs->{alias}, {}, \@pre_order, $collapse); - #die Dumper \@cols; push(@{$attrs->{select}}, map { $_->[0] } @prefetch); push(@{$attrs->{as}}, map { $_->[1] } @prefetch); } push(@{$attrs->{order_by}}, @pre_order); } - $attrs->{collapse} = $collapse; +# use Data::Dumper; warn Dumper($collapse) if keys %{$collapse}; if ($attrs->{page}) { $attrs->{rows} ||= 10; @@ -139,11 +135,7 @@ sub new { $attrs->{offset} += ($attrs->{rows} * ($attrs->{page} - 1)); } -#if (keys %{$collapse}) { -# use Data::Dumper; warn Dumper($collapse); -#} - - my $new = { + bless { result_source => $source, result_class => $attrs->{result_class} || $source->result_class, cond => $attrs->{where}, @@ -152,9 +144,8 @@ sub new { count => undef, page => delete $attrs->{page}, pager => undef, - attrs => $attrs }; - bless ($new, $class); - return $new; + attrs => $attrs + }, $class; } =head2 search @@ -163,10 +154,10 @@ sub new { my $new_rs = $rs->search({ foo => 3 }); If you need to pass in additional attributes but no additional condition, -call it as C. +call it as C. # "SELECT foo, bar FROM $class_table" - my @all = $class->search({}, { cols => [qw/foo bar/] }); + my @all = $class->search(undef, { columns => [qw/foo bar/] }); =cut @@ -396,7 +387,7 @@ Returns the next element in the resultset (C is there is none). Can be used to efficiently iterate over records in the resultset: - my $rs = $schema->resultset('CD')->search({}); + my $rs = $schema->resultset('CD')->search; while (my $cd = $rs->next) { print $cd->title; } @@ -1000,13 +991,14 @@ overview of them: Which column(s) to order the results by. This is currently passed through directly to SQL, so you can give e.g. C for a descending order. -=head2 cols +=head2 columns =head3 Arguments: (arrayref) 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. +from that, then auto-populates C from C, usually when C