From: Matt S Trout Date: Mon, 9 Jan 2006 01:48:20 +0000 (+0000) Subject: More refactoring, added ->source and ->resultset to Schema X-Git-Tag: v0.05005~117^2~75 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ea20d0fdf6d84e0d75d557f71cf7d1233b607b4c;p=dbsrgits%2FDBIx-Class.git More refactoring, added ->source and ->resultset to Schema --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 824c51e..0c74273 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -6,6 +6,7 @@ use overload '0+' => 'count', fallback => 1; use Data::Page; +use Storable; =head1 NAME @@ -37,7 +38,7 @@ sub new { $class->new_result(@_) if ref $class; my ($source, $attrs) = @_; #use Data::Dumper; warn Dumper(@_); - $attrs = { %{ $attrs || {} } }; + $attrs = Storable::dclone($attrs || {}); # { %{ $attrs || {} } }; my %seen; my $alias = ($attrs->{alias} ||= 'me'); if (!$attrs->{select}) { @@ -180,18 +181,13 @@ sub search_related { $self->{source}->result_class->throw( "No such relationship ${rel} in search_related") unless $rel_obj; - my $r_class = $self->{source}->result_class->resolve_class($rel_obj->{class}); - my $source = $r_class->result_source; - $source = bless({ %{$source} }, ref $source || $source); - $source->storage($self->{source}->storage); - $source->result_class($r_class); my $rs = $self->search(undef, { join => $rel }); - #use Data::Dumper; warn Dumper($rs); - return $source->resultset_class->new( - $source, { %{$rs->{attrs}}, - alias => $rel, - select => undef(), - as => undef() } + return $self->{source}->schema->resultset($rel_obj->{class} + )->search( undef, + { %{$rs->{attrs}}, + alias => $rel, + select => undef(), + as => undef() } )->search(@rest); } @@ -290,8 +286,8 @@ sub count { my $attrs = { %{ $self->{attrs} }, select => { 'count' => '*' }, as => [ 'count' ] }; - # offset, order by and page are not needed to count - delete $attrs->{$_} for qw/rows offset order_by page pager/; + # offset, order by and page are not needed to count. record_filter is cdbi + delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/; ($self->{count}) = (ref $self)->new($self->{source}, $attrs)->cursor->next; } diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 502ec55..d900a27 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -98,6 +98,33 @@ sub class { return $self->class_registrations->{$class}; } +=head2 source + + my $source = $schema->source('Foo'); + +Returns the result source object for the registered name + +=cut + +sub source { + my ($self, $class) = @_; + return $self->class_registrations->{$class}->result_source; +} + +=head2 resultset + + my $rs = $schema->resultset('Foo'); + +Returns the resultset for the registered name + +=cut + +sub resultset { + my ($self, $class) = @_; + return $self->class_registrations->{$class}->result_source->resultset; +} + + =head2 load_classes [, (, ), { => []}] Uses L to find all classes under the database class' namespace, @@ -182,10 +209,10 @@ you expect. =cut sub compose_connection { - my ($class, $target, @info) = @_; + my ($self, $target, @info) = @_; my $conn_class = "${target}::_db"; - $class->setup_connection_class($conn_class, @info); - my $schema = $class->compose_namespace($target, $conn_class); + $self->setup_connection_class($conn_class, @info); + my $schema = $self->compose_namespace($target, $conn_class); $schema->storage($conn_class->storage); foreach my $class ($schema->registered_classes) { my $source = $class->result_source; @@ -193,6 +220,7 @@ sub compose_connection { $source->schema($schema); $source->result_class($class); $class->mk_classdata(result_source => $source); + $class->mk_classdata(resultset_instance => $source->resultset); } return $schema; } @@ -205,7 +233,7 @@ sub compose_namespace { my $schema = bless({ }, $class); while (my ($comp, $comp_class) = each %reg) { my $target_class = "${target}::${comp}"; - $class->inject_base($target_class, $comp_class, $base); + $class->inject_base($target_class, $comp_class, ($base ? $base : ())); @map{$comp, $comp_class} = ($target_class, $target_class); } $schema->class_registrations(\%map); diff --git a/lib/DBIx/Class/Table.pm b/lib/DBIx/Class/Table.pm index c7646f2..a232917 100644 --- a/lib/DBIx/Class/Table.pm +++ b/lib/DBIx/Class/Table.pm @@ -66,7 +66,7 @@ Convenience alias to add_columns sub resultset { my $self = shift; - return $self->{resultset} ||= $self->resultset_class->new($self); + return $self->resultset_class->new($self); } =head2 has_column