Fixed DBICTest Schema class names, added class_resolver system to make them work
Matt S Trout [Sat, 6 Aug 2005 23:52:54 +0000 (23:52 +0000)]
13 files changed:
lib/DBIx/Class/DB.pm
lib/DBIx/Class/Relationship.pm
lib/DBIx/Class/Schema.pm
t/08inflate_has_a.t
t/lib/DBICTest.pm
t/lib/DBICTest/Schema/Artist.pm
t/lib/DBICTest/Schema/CD.pm
t/lib/DBICTest/Schema/FourKeys.pm
t/lib/DBICTest/Schema/LinerNotes.pm
t/lib/DBICTest/Schema/OneKey.pm
t/lib/DBICTest/Schema/Tag.pm
t/lib/DBICTest/Schema/Track.pm
t/lib/DBICTest/Schema/TwoKeys.pm

index 7a3ae1a..6396725 100644 (file)
@@ -2,6 +2,7 @@ package DBIx::Class::DB;
 
 use base qw/Class::Data::Inheritable/;
 use DBIx::Class::Storage::DBI;
+use DBIx::Class::ClassResolver::PassThrough;
 use DBI;
 
 =head1 NAME 
@@ -34,6 +35,8 @@ This class provides a simple way of specifying a database connection.
 =cut
 
 __PACKAGE__->mk_classdata('storage');
+__PACKAGE__->mk_classdata('class_resolver' =>
+                            'DBIx::Class::ClassResolver::PassThrough');
 
 =item connection
 
@@ -71,6 +74,8 @@ Issues a rollback again the current dbh
 
 sub dbi_rollback { $_[0]->storage->rollback; }
 
+sub resolve_class { return shift->class_resolver->class(@_); }
+
 1;
 
 =back
index 14741b0..ac717d8 100644 (file)
@@ -135,7 +135,8 @@ sub _literal_related {
   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 {
@@ -159,7 +160,7 @@ sub new_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 {
@@ -175,8 +176,9 @@ sub set_from_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")
index bdd157b..a88c52d 100644 (file)
@@ -81,12 +81,25 @@ sub load_classes {
 
 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 {
index c621eed..80678d1 100644 (file)
@@ -9,9 +9,7 @@ use lib qw(t/lib);
 
 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 ) },
index 2ecb7ce..1bc01c5 100755 (executable)
@@ -11,7 +11,7 @@ mkdir("t/var") unless -d "t/var";
 
 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);
index 37aa0b2..2fd5da4 100644 (file)
@@ -1,21 +1,21 @@
-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' }
 );
 
index 46f1b38..7196722 100644 (file)
@@ -1,22 +1,22 @@
-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;
index 2069280..be18952 100644 (file)
@@ -1,9 +1,9 @@
-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;
index b841332..442a5c1 100644 (file)
@@ -1,9 +1,9 @@
-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;
index 095354b..72d8de3 100644 (file)
@@ -1,10 +1,10 @@
-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;
index 242d723..4a3c4d2 100644 (file)
@@ -1,12 +1,12 @@
-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' }
 );
 
index cf9d7df..97cb6f4 100644 (file)
@@ -1,12 +1,12 @@
-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' }
 );
 
index 12943b8..f7442c1 100755 (executable)
@@ -1,16 +1,16 @@
-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' }
 );