From: Matt S Trout Date: Wed, 18 Jan 2006 21:34:11 +0000 (+0000) Subject: Eliminated result_source_instance requirement in Schema X-Git-Tag: v0.05005~117^2~40 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0dc792491a0296584ea02abe0071ab7bd7d4618a;p=dbsrgits%2FDBIx-Class.git Eliminated result_source_instance requirement in Schema --- diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index e61c1c9..3d79dca 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -269,23 +269,22 @@ sub inflate_result { ref $class || $class); my $schema; PRE: foreach my $pre (keys %{$prefetch||{}}) { - my $rel_obj = $class->relationship_info($pre); - die "Can't prefetch non-eistant relationship ${pre}" unless $rel_obj; - $schema ||= $source->schema; - my $pre_class = $schema->class($rel_obj->{class}); - my $fetched = $pre_class->inflate_result( - $schema->source($pre_class), @{$prefetch->{$pre}}); + my $pre_source = $source->related_source($pre); + die "Can't prefetch non-existant relationship ${pre}" unless $pre_source; + my $fetched = $pre_source->result_class->inflate_result( + $pre_source, @{$prefetch->{$pre}}); + my $accessor = $source->relationship_info($pre)->{attrs}{accessor}; $class->throw("No accessor for prefetched $pre") - unless defined $rel_obj->{attrs}{accessor}; - PRIMARY: foreach my $pri ($rel_obj->{class}->primary_columns) { + unless defined $accessor; + PRIMARY: foreach my $pri ($pre_source->primary_columns) { unless (defined $fetched->get_column($pri)) { undef $fetched; last PRIMARY; } } - if ($rel_obj->{attrs}{accessor} eq 'single') { + if ($accessor eq 'single') { $new->{_relationship_data}{$pre} = $fetched; - } elsif ($rel_obj->{attrs}{accessor} eq 'filter') { + } elsif ($accessor eq 'filter') { $new->{_inflated_column}{$pre} = $fetched; } else { $class->throw("Don't know how to store prefetched $pre"); diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 35268d6..8c3d288 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -7,7 +7,8 @@ use DBIx::Class::DB; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/Exception/); -__PACKAGE__->mk_classdata('class_registrations' => {}); +__PACKAGE__->mk_classdata('class_mappings' => {}); +__PACKAGE__->mk_classdata('source_registrations' => {}); __PACKAGE__->mk_classdata('storage_type' => 'DBI'); __PACKAGE__->mk_classdata('storage'); @@ -58,7 +59,7 @@ particular which module inherits off which. =head1 METHODS -=head2 register_class +=head2 register_class Registers the class in the schema's class_registrations. This is a hash containing database classes, keyed by their monikers. It's used by @@ -67,36 +68,40 @@ compose_connection to create/modify all the existing database classes. =cut sub register_class { - my ($self, $name, $to_register) = @_; - my %reg = %{$self->class_registrations}; - $reg{$name} = $to_register; - $self->class_registrations(\%reg); - $to_register->result_source_instance->schema($self); + my ($self, $moniker, $to_register) = @_; + $self->register_source($moniker => $to_register->result_source_instance); } -=head2 registered_classes - -Simple read-only accessor for the schema's registered classes. See -register_class above if you want to modify it. +=head2 register_source +Registers the result source in the schema with the given moniker =cut -sub registered_classes { - return values %{shift->class_registrations}; -} +sub register_source { + my ($self, $moniker, $source) = @_; + my %reg = %{$self->source_registrations}; + $reg{$moniker} = $source; + $self->source_registrations(\%reg); + $source->schema($self); + if ($source->result_class) { + my %map = %{$self->class_mappings}; + $map{$source->result_class} = $moniker; + $self->class_mappings(\%map); + } +} =head2 class my $class = $schema->class('Foo'); -Shortcut to retrieve a single class by its registered name +Retrieves the result class name for a given result source =cut sub class { - my ($self, $class) = @_; - return $self->class_registrations->{$class}; + my ($self, $moniker) = @_; + return $self->source($moniker)->result_class; } =head2 source @@ -108,25 +113,40 @@ Returns the result source object for the registered name =cut sub source { - my ($self, $class) = @_; - return $self->class_registrations->{$class}->result_source_instance - if $self->class_registrations->{$class}; + my ($self, $moniker) = @_; + my $sreg = $self->source_registrations; + return $sreg->{$moniker} if exists $sreg->{$moniker}; + + # if we got here, they probably passed a full class name + my $mapped = $self->class_mappings->{$moniker}; + die "Can't find source for ${moniker}" + unless $mapped && exists $sreg->{$mapped}; + return $sreg->{$mapped}; } +=head2 sources + + my @source_monikers = $schema->sources; + +Returns the source monikers of all source registrations on this schema + +=cut + +sub sources { return keys %{shift->source_registrations}; } + =head2 resultset my $rs = $schema->resultset('Foo'); -Returns the resultset for the registered name +Returns the resultset for the registered moniker =cut sub resultset { - my ($self, $class) = @_; - return $self->class_registrations->{$class}->result_source_instance->resultset; + my ($self, $moniker) = @_; + return $self->source($moniker)->resultset; } - =head2 load_classes [, (, ), { => []}] Uses L to find all classes under the database class' namespace, @@ -216,11 +236,10 @@ sub compose_connection { $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_instance; - $source = $source->new($source); - $source->schema($schema); - $source->result_class($class); + foreach my $moniker ($schema->sources) { + my $source = $schema->source($moniker); + my $class = $source->result_class; + #warn "$moniker $class $source ".$source->storage; $class->mk_classdata(result_source_instance => $source); $class->mk_classdata(resultset_instance => $source->resultset); } @@ -229,16 +248,21 @@ sub compose_connection { sub compose_namespace { my ($class, $target, $base) = @_; - my %reg = %{ $class->class_registrations }; + my %reg = %{ $class->source_registrations }; my %target; my %map; my $schema = bless({ }, $class); - while (my ($comp, $comp_class) = each %reg) { - my $target_class = "${target}::${comp}"; - $class->inject_base($target_class, $comp_class, ($base ? $base : ())); - @map{$comp, $comp_class} = ($target_class, $target_class); + while (my ($moniker, $source) = each %reg) { + my $target_class = "${target}::${moniker}"; + $class->inject_base( + $target_class => $source->result_class, ($base ? $base : ()) + ); + my $new_source = $source->new($source); + $new_source->result_class($target_class); + $new_source->schema($schema); + $map{$moniker} = $new_source; } - $schema->class_registrations(\%map); + $schema->source_registrations(\%map); { no strict 'refs'; *{"${target}::schema"} =