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");
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');
=head1 METHODS
-=head2 register_class <component> <component_class>
+=head2 register_class <moniker> <component_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
=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 <moniker> <result 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
=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 [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
Uses L<Module::Find> to find all classes under the database class' namespace,
$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);
}
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"} =