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)
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.
SQL::Abstract
PK
Table
- DB
Exception
AccessorGroup/);
--- /dev/null
+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;
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();
-package DBICTest;
-
use strict;
use warnings;
-use base qw/DBIx::Class::Core/;
+use DBICTest::Schema;
use DateTime;
my $db_file = "t/var/DBIxClass.db";
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);
$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;
--- /dev/null
+package DBICTest::Schema;
+
+use base qw/DBIx::Class::Schema/;
+
+__PACKAGE__->load_classes(qw/
+ Artist CD Track Tag LinerNotes OneKey TwoKeys FourKeys/);
+
+1;
--- /dev/null
+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;
--- /dev/null
+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;
--- /dev/null
+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;
--- /dev/null
+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;
--- /dev/null
+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;
--- /dev/null
+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;
--- /dev/null
+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;
--- /dev/null
+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;
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();