use base qw/Class::Data::Inheritable/;
use DBIx::Class::Storage::DBI;
+use DBIx::Class::ClassResolver::PassThrough;
use DBI;
=head1 NAME
=cut
__PACKAGE__->mk_classdata('storage');
+__PACKAGE__->mk_classdata('class_resolver' =>
+ 'DBIx::Class::ClassResolver::PassThrough');
=item connection
sub dbi_rollback { $_[0]->storage->rollback; }
+sub resolve_class { return shift->class_resolver->class(@_); }
+
1;
=back
my ($cond) = $self->_cond_resolve($rel_obj->{cond}, $attrs);
$cond = "${s_cond} AND ${cond}" if $s_cond;
#warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}});
- return $rel_obj->{class}->$meth($cond, @{$attrs->{bind} || []}, $attrs);
+ return $self->resolve_class($rel_obj->{class}
+ )->$meth($cond, @{$attrs->{bind} || []}, $attrs);
}
sub create_related {
$self->_cond_value($attrs, $k => $v);
$fields{$self->_cond_key($attrs, $k)} = (@{delete $attrs->{bind}})[0];
}
- return $rel_obj->{class}->new(\%fields);
+ return $self->resolve_class($rel_obj->{class})->new(\%fields);
}
sub find_or_create_related {
$self->throw( "set_from_related can only handle a hash condition; the "
."condition for $rel is of type ".(ref $cond ? ref $cond : 'plain scalar'))
unless ref $cond eq 'HASH';
- $self->throw( "Object $f_obj isn't a ".$rel_obj->{class} )
- unless $f_obj->isa($rel_obj->{class});
+ my $f_class = $self->resolve_class($rel_obj->{class});
+ $self->throw( "Object $f_obj isn't a ".$f_class )
+ unless $f_obj->isa($f_class);
foreach my $key (keys %$cond) {
next if ref $cond->{$key}; # Skip literals and complex conditions
$self->throw("set_from_related can't handle $key as key")
sub compose_connection {
my ($class, $target, @info) = @_;
- $class->setup_connection_class($target, @info);
+ my $conn_class = "${target}::_db";
+ $class->setup_connection_class($conn_class, @info);
my %reg = %{ $class->class_registrations };
+ my %target;
+ my %map;
while (my ($comp, $comp_class) = each %reg) {
my $target_class = "${target}::${comp}";
- $class->inject_base($target_class, $comp_class, $target);
+ $class->inject_base($target_class, $comp_class, $conn_class);
+ @map{$comp, $comp_class} = ($target_class, $target_class);
}
+ {
+ no strict 'refs';
+ *{"${target}::class"} =
+ sub {
+ my ($class, $to_map) = @_;
+ return $map{$to_map};
+ };
+ }
+ $conn_class->class_resolver($target);
}
sub setup_connection_class {
use_ok('DBICTest');
-use DBIx::Class::CDBICompat::HasA;
-
-unshift(@DBICTest::ISA, 'DBIx::Class::CDBICompat::HasA');
+DBICTest::CD->load_components(qw/CDBICompat::HasA/);
DBICTest::CD->has_a( 'year', 'DateTime',
inflate => sub { DateTime->new( year => shift ) },
DBICTest::Schema->compose_connection('DBICTest' => "dbi:SQLite:${db_file}");
-my $dbh = DBICTest->storage->dbh;
+my $dbh = DBICTest::_db->storage->dbh;
my $sql = <<EOSQL;
CREATE TABLE artist (artistid INTEGER NOT NULL PRIMARY KEY, name VARCHAR);
-package DBICTest::Artist;
+package DBICTest::Schema::Artist;
use base 'DBIx::Class::Core';
-DBICTest::Artist->table('artist');
-DBICTest::Artist->add_columns(qw/artistid name/);
-DBICTest::Artist->set_primary_key('artistid');
-DBICTest::Artist->add_relationship(
- cds => 'DBICTest::CD',
+DBICTest::Schema::Artist->table('artist');
+DBICTest::Schema::Artist->add_columns(qw/artistid name/);
+DBICTest::Schema::Artist->set_primary_key('artistid');
+DBICTest::Schema::Artist->add_relationship(
+ cds => 'DBICTest::Schema::CD',
{ 'foreign.artist' => 'self.artistid' },
{ order_by => 'year' }
);
-DBICTest::Artist->add_relationship(
- twokeys => 'DBICTest::TwoKeys',
+DBICTest::Schema::Artist->add_relationship(
+ twokeys => 'DBICTest::Schema::TwoKeys',
{ 'foreign.artist' => 'self.artistid' }
);
-DBICTest::Artist->add_relationship(
- onekeys => 'DBICTest::OneKey',
+DBICTest::Schema::Artist->add_relationship(
+ onekeys => 'DBICTest::Schema::OneKey',
{ 'foreign.artist' => 'self.artistid' }
);
-package DBICTest::CD;
+package DBICTest::Schema::CD;
use base 'DBIx::Class::Core';
-DBICTest::CD->table('cd');
-DBICTest::CD->add_columns(qw/cdid artist title year/);
-DBICTest::CD->set_primary_key('cdid');
-DBICTest::CD->add_relationship(
- artist => 'DBICTest::Artist',
+DBICTest::Schema::CD->table('cd');
+DBICTest::Schema::CD->add_columns(qw/cdid artist title year/);
+DBICTest::Schema::CD->set_primary_key('cdid');
+DBICTest::Schema::CD->add_relationship(
+ artist => 'DBICTest::Schema::Artist',
{ 'foreign.artistid' => 'self.artist' }
);
-DBICTest::CD->add_relationship(
- tracks => 'DBICTest::Track',
+DBICTest::Schema::CD->add_relationship(
+ tracks => 'DBICTest::Schema::Track',
{ 'foreign.cd' => 'self.cdid' }
);
-DBICTest::CD->add_relationship(
- tags => 'DBICTest::Tag',
+DBICTest::Schema::CD->add_relationship(
+ tags => 'DBICTest::Schema::Tag',
{ 'foreign.cd' => 'self.cdid' }
);
-#DBICTest::CD->might_have(liner_notes => 'DBICTest::LinerNotes' => qw/notes/);
+#DBICTest::Schema::CD->might_have(liner_notes => 'DBICTest::Schema::LinerNotes' => qw/notes/);
1;
-package DBICTest::FourKeys;
+package DBICTest::Schema::FourKeys;
use base 'DBIx::Class::Core';
-DBICTest::FourKeys->table('fourkeys');
-DBICTest::FourKeys->add_columns(qw/foo bar hello goodbye/);
-DBICTest::FourKeys->set_primary_key(qw/foo bar hello goodbye/);
+DBICTest::Schema::FourKeys->table('fourkeys');
+DBICTest::Schema::FourKeys->add_columns(qw/foo bar hello goodbye/);
+DBICTest::Schema::FourKeys->set_primary_key(qw/foo bar hello goodbye/);
1;
-package DBICTest::LinerNotes;
+package DBICTest::Schema::LinerNotes;
use base qw/DBIx::Class::Core/;
-DBICTest::LinerNotes->table('liner_notes');
-DBICTest::LinerNotes->add_columns(qw/liner_id notes/);
-DBICTest::LinerNotes->set_primary_key('liner_id');
+DBICTest::Schema::LinerNotes->table('liner_notes');
+DBICTest::Schema::LinerNotes->add_columns(qw/liner_id notes/);
+DBICTest::Schema::LinerNotes->set_primary_key('liner_id');
1;
-package DBICTest::OneKey;
+package DBICTest::Schema::OneKey;
use base 'DBIx::Class::Core';
-DBICTest::OneKey->table('onekey');
-DBICTest::OneKey->add_columns(qw/id artist cd/);
-DBICTest::OneKey->set_primary_key('id');
+DBICTest::Schema::OneKey->table('onekey');
+DBICTest::Schema::OneKey->add_columns(qw/id artist cd/);
+DBICTest::Schema::OneKey->set_primary_key('id');
1;
-package DBICTest::Tag;
+package DBICTest::Schema::Tag;
use base qw/DBIx::Class::Core/;
-DBICTest::Tag->table('tags');
-DBICTest::Tag->add_columns(qw/tagid cd tag/);
-DBICTest::Tag->set_primary_key('tagid');
-DBICTest::Tag->add_relationship(
- cd => 'DBICTest::CD',
+DBICTest::Schema::Tag->table('tags');
+DBICTest::Schema::Tag->add_columns(qw/tagid cd tag/);
+DBICTest::Schema::Tag->set_primary_key('tagid');
+DBICTest::Schema::Tag->add_relationship(
+ cd => 'DBICTest::Schema::CD',
{ 'foreign.cdid' => 'self.cd' }
);
-package DBICTest::Track;
+package DBICTest::Schema::Track;
use base 'DBIx::Class::Core';
-DBICTest::Track->table('track');
-DBICTest::Track->add_columns(qw/trackid cd position title/);
-DBICTest::Track->set_primary_key('trackid');
-DBICTest::Track->add_relationship(
- cd => 'DBICTest::CD',
+DBICTest::Schema::Track->table('track');
+DBICTest::Schema::Track->add_columns(qw/trackid cd position title/);
+DBICTest::Schema::Track->set_primary_key('trackid');
+DBICTest::Schema::Track->add_relationship(
+ cd => 'DBICTest::Schema::CD',
{ 'foreign.cdid' => 'self.cd' }
);
-package DBICTest::TwoKeys;
+package DBICTest::Schema::TwoKeys;
use base 'DBIx::Class::Core';
-DBICTest::TwoKeys->table('twokeys');
-DBICTest::TwoKeys->add_columns(qw/artist cd/);
-DBICTest::TwoKeys->set_primary_key(qw/artist cd/);
-DBICTest::TwoKeys->add_relationship(
- artist => 'DBICTest::Artist',
+DBICTest::Schema::TwoKeys->table('twokeys');
+DBICTest::Schema::TwoKeys->add_columns(qw/artist cd/);
+DBICTest::Schema::TwoKeys->set_primary_key(qw/artist cd/);
+DBICTest::Schema::TwoKeys->add_relationship(
+ artist => 'DBICTest::Schema::Artist',
{ 'foreign.artistid' => 'self.artist' }
);
-DBICTest::TwoKeys->add_relationship(
- cd => 'DBICTest::CD',
+DBICTest::Schema::TwoKeys->add_relationship(
+ cd => 'DBICTest::Schema::CD',
{ 'foreign.cdid' => 'self.cd' }
);