Schema support added, DBICTest refactored to use it
Matt S Trout [Thu, 4 Aug 2005 16:34:11 +0000 (16:34 +0000)]
15 files changed:
lib/DBIx/Class.pm
lib/DBIx/Class/Core.pm
lib/DBIx/Class/Schema.pm [new file with mode: 0644]
lib/DBIx/Class/Test/SQLite.pm
t/lib/DBICTest.pm
t/lib/DBICTest/Schema.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Artist.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/CD.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/FourKeys.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/LinerNotes.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/OneKey.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Tag.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Track.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/TwoKeys.pm [new file with mode: 0755]
t/testlib/CDBase.pm

index 9cb1b36..bcba15f 100644 (file)
@@ -43,14 +43,14 @@ use base qw/Class::DBI/;
 with
 
 use base qw/DBIx::Class/;
-__PACKAGE__->load_components(qw/CDBICompat Core/);
+__PACKAGE__->load_components(qw/CDBICompat Core DB/);
 
 will probably get you started.
 
 If you're using AUTO_INCREMENT for your primary columns, you'll also want
 yo load the approriate PK::Auto subclass - e.g.
 
-__PACKAGE__->load_components(qw/CDBICompat PK::Auto::SQLite Core/);
+__PACKAGE__->load_components(qw/CDBICompat PK::Auto::SQLite Core DB/);
 
 (with is what ::Test::SQLite does to present the Class::DBI::Test::SQLite
 interface)
@@ -59,7 +59,7 @@ If you fancy playing around with DBIx::Class from scratch, then read the docs
 for ::Table and ::Relationship,
 
 use base qw/DBIx::Class/;
-__PACKAGE__->load_components(qw/Core/);
+__PACKAGE__->load_components(qw/Core DB/);
 
 and have a look at t/lib/DBICTest.pm for a brief example.
 
index b85e9e5..94e1b2a 100644 (file)
@@ -15,7 +15,6 @@ __PACKAGE__->load_components(qw/
   SQL::Abstract
   PK
   Table
-  DB
   Exception
   AccessorGroup/);
 
diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm
new file mode 100644 (file)
index 0000000..fd992e1
--- /dev/null
@@ -0,0 +1,47 @@
+package DBIx::Class::Schema;
+
+use strict;
+use warnings;
+
+use base qw/Class::Data::Inheritable/;
+use DBIx::Class;
+
+__PACKAGE__->mk_classdata('_class_registrations' => {});
+
+sub register_class {
+  my ($class, $name, $to_register) = @_;
+  my %reg = %{$class->_class_registrations};
+  $reg{$name} = $to_register;
+  $class->_class_registrations(\%reg);
+}
+
+sub load_classes {
+  my $class = shift;
+  my @comp = grep { $_ !~ /^#/ } @_;
+  foreach my $comp (@comp) {
+    my $comp_class = "${class}::${comp}";
+    eval "use $comp_class";
+    die $@ if $@;
+    $class->register_class($comp => $comp_class);
+  }
+}
+
+sub compose_connection {
+  my ($class, $target, @info) = @_;
+  {
+    no strict 'refs';
+    unshift(@{"${target}::ISA"}, 'DBIx::Class');
+  }
+  $target->load_components('DB');
+  $target->connection(@info);
+  my %reg = %{ $class->_class_registrations };
+  while (my ($comp, $comp_class) = each %reg) {
+    my $target_class = "${target}::${comp}";
+    {
+      no strict 'refs';
+      unshift(@{"${target_class}::ISA"}, $comp_class, $target);
+    }
+  }
+}
+
+1;
index b6b22fa..94dd418 100644 (file)
@@ -34,7 +34,7 @@ use strict;
 
 use base qw/DBIx::Class/;
 
-__PACKAGE__->load_components(qw/CDBICompat PK::Auto::SQLite Core/);
+__PACKAGE__->load_components(qw/CDBICompat PK::Auto::SQLite Core DB/);
 
 use File::Temp qw/tempfile/;
 my (undef, $DB) = tempfile();
index 559556f..2ecb7ce 100755 (executable)
@@ -1,8 +1,6 @@
-package DBICTest;
-
 use strict;
 use warnings;
-use base qw/DBIx::Class::Core/;
+use DBICTest::Schema;
 use DateTime;
 
 my $db_file = "t/var/DBIxClass.db";
@@ -11,9 +9,9 @@ unlink($db_file) if -e $db_file;
 unlink($db_file . "-journal") if -e $db_file . "-journal";
 mkdir("t/var") unless -d "t/var";
 
-__PACKAGE__->connection("dbi:SQLite:${db_file}");
+DBICTest::Schema->compose_connection('DBICTest' => "dbi:SQLite:${db_file}");
 
-my $dbh = __PACKAGE__->storage->dbh;
+my $dbh = DBICTest->storage->dbh;
 
 my $sql = <<EOSQL;
 CREATE TABLE artist (artistid INTEGER NOT NULL PRIMARY KEY, name VARCHAR);
@@ -106,110 +104,4 @@ EOSQL
 
 $dbh->do($_) for split(/\n\n/, $sql);
 
-package DBICTest::LinerNotes;
-
-use base 'DBICTest';
-
-DBICTest::LinerNotes->table('liner_notes');
-DBICTest::LinerNotes->add_columns(qw/liner_id notes/);
-DBICTest::LinerNotes->set_primary_key('liner_id');
-
-package DBICTest::Tag;
-
-use base 'DBICTest';
-
-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',
-    { 'foreign.cdid' => 'self.cd' }
-);
-
-package DBICTest::Track;
-
-use base 'DBICTest';
-
-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',
-    { 'foreign.cdid' => 'self.cd' }
-);
-
-package DBICTest::CD;
-
-use base 'DBICTest';
-
-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',
-    { 'foreign.artistid' => 'self.artist' }
-);
-DBICTest::CD->add_relationship(
-    tracks => 'DBICTest::Track',
-    { 'foreign.cd' => 'self.cdid' }
-);
-DBICTest::CD->add_relationship(
-    tags => 'DBICTest::Tag',
-    { 'foreign.cd' => 'self.cdid' }
-);
-#DBICTest::CD->might_have(liner_notes => 'DBICTest::LinerNotes' => qw/notes/);
-
-package DBICTest::Artist;
-
-use base 'DBICTest';
-
-DBICTest::Artist->table('artist');
-DBICTest::Artist->add_columns(qw/artistid name/);
-DBICTest::Artist->set_primary_key('artistid');
-DBICTest::Artist->add_relationship(
-    cds => 'DBICTest::CD',
-    { 'foreign.artist' => 'self.artistid' },
-    { order_by => 'year' }
-);
-DBICTest::Artist->add_relationship(
-    twokeys => 'DBICTest::TwoKeys',
-    { 'foreign.artist' => 'self.artistid' }
-);
-DBICTest::Artist->add_relationship(
-    onekeys => 'DBICTest::OneKey',
-    { 'foreign.artist' => 'self.artistid' }
-);
-
-package DBICTest::OneKey;
-
-use base 'DBICTest';
-
-DBICTest::OneKey->table('onekey');
-DBICTest::OneKey->add_columns(qw/id artist cd/);
-DBICTest::OneKey->set_primary_key('id');
-
-package DBICTest::TwoKeys;
-
-use base 'DBICTest';
-
-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',
-    { 'foreign.artistid' => 'self.artist' }
-);
-DBICTest::TwoKeys->add_relationship(
-    cd => 'DBICTest::CD',
-    { 'foreign.cdid' => 'self.cd' }
-);
-
-package DBICTest::FourKeys;
-
-use base 'DBICTest';
-
-DBICTest::FourKeys->table('fourkeys');
-DBICTest::FourKeys->add_columns(qw/foo bar hello goodbye/);
-DBICTest::FourKeys->set_primary_key(qw/foo bar hello goodbye/);
-
 1;
diff --git a/t/lib/DBICTest/Schema.pm b/t/lib/DBICTest/Schema.pm
new file mode 100644 (file)
index 0000000..4c46a54
--- /dev/null
@@ -0,0 +1,8 @@
+package DBICTest::Schema;
+
+use base qw/DBIx::Class::Schema/;
+
+__PACKAGE__->load_classes(qw/
+  Artist CD Track Tag LinerNotes OneKey TwoKeys FourKeys/);
+
+1;
diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm
new file mode 100644 (file)
index 0000000..37aa0b2
--- /dev/null
@@ -0,0 +1,22 @@
+package DBICTest::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',
+    { 'foreign.artist' => 'self.artistid' },
+    { order_by => 'year' }
+);
+DBICTest::Artist->add_relationship(
+    twokeys => 'DBICTest::TwoKeys',
+    { 'foreign.artist' => 'self.artistid' }
+);
+DBICTest::Artist->add_relationship(
+    onekeys => 'DBICTest::OneKey',
+    { 'foreign.artist' => 'self.artistid' }
+);
+
+1;
diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm
new file mode 100644 (file)
index 0000000..46f1b38
--- /dev/null
@@ -0,0 +1,22 @@
+package DBICTest::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',
+    { 'foreign.artistid' => 'self.artist' }
+);
+DBICTest::CD->add_relationship(
+    tracks => 'DBICTest::Track',
+    { 'foreign.cd' => 'self.cdid' }
+);
+DBICTest::CD->add_relationship(
+    tags => 'DBICTest::Tag',
+    { 'foreign.cd' => 'self.cdid' }
+);
+#DBICTest::CD->might_have(liner_notes => 'DBICTest::LinerNotes' => qw/notes/);
+
+1;
diff --git a/t/lib/DBICTest/Schema/FourKeys.pm b/t/lib/DBICTest/Schema/FourKeys.pm
new file mode 100644 (file)
index 0000000..2069280
--- /dev/null
@@ -0,0 +1,9 @@
+package DBICTest::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/);
+
+1;
diff --git a/t/lib/DBICTest/Schema/LinerNotes.pm b/t/lib/DBICTest/Schema/LinerNotes.pm
new file mode 100644 (file)
index 0000000..b841332
--- /dev/null
@@ -0,0 +1,9 @@
+package DBICTest::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');
+
+1;
diff --git a/t/lib/DBICTest/Schema/OneKey.pm b/t/lib/DBICTest/Schema/OneKey.pm
new file mode 100644 (file)
index 0000000..095354b
--- /dev/null
@@ -0,0 +1,10 @@
+package DBICTest::OneKey;
+
+use base 'DBIx::Class::Core';
+
+DBICTest::OneKey->table('onekey');
+DBICTest::OneKey->add_columns(qw/id artist cd/);
+DBICTest::OneKey->set_primary_key('id');
+
+
+1;
diff --git a/t/lib/DBICTest/Schema/Tag.pm b/t/lib/DBICTest/Schema/Tag.pm
new file mode 100644 (file)
index 0000000..242d723
--- /dev/null
@@ -0,0 +1,13 @@
+package DBICTest::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',
+    { 'foreign.cdid' => 'self.cd' }
+);
+
+1;
diff --git a/t/lib/DBICTest/Schema/Track.pm b/t/lib/DBICTest/Schema/Track.pm
new file mode 100644 (file)
index 0000000..cf9d7df
--- /dev/null
@@ -0,0 +1,13 @@
+package DBICTest::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',
+    { 'foreign.cdid' => 'self.cd' }
+);
+
+1;
diff --git a/t/lib/DBICTest/Schema/TwoKeys.pm b/t/lib/DBICTest/Schema/TwoKeys.pm
new file mode 100755 (executable)
index 0000000..12943b8
--- /dev/null
@@ -0,0 +1,17 @@
+package DBICTest::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',
+    { 'foreign.artistid' => 'self.artist' }
+);
+DBICTest::TwoKeys->add_relationship(
+    cd => 'DBICTest::CD',
+    { 'foreign.cdid' => 'self.cd' }
+);
+
+1;
index 6cc4c6d..6fdd422 100644 (file)
@@ -2,7 +2,7 @@ package CDBase;
 
 use strict;
 use base qw(DBIx::Class);
-__PACKAGE__->load_components(qw/CDBICompat Core/);
+__PACKAGE__->load_components(qw/CDBICompat Core DB/);
 
 use File::Temp qw/tempfile/;
 my (undef, $DB) = tempfile();