1 package DBIx::Class::Schema;
6 use base qw/Class::Data::Inheritable/;
7 use base qw/DBIx::Class/;
9 __PACKAGE__->load_components(qw/Exception/);
10 __PACKAGE__->mk_classdata('class_registrations' => {});
14 DBIx::Class::Schema - composable schemas
22 use base qw/DBIx::Class::Schema/;
24 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
28 package My::Schema::Foo;
30 use base qw/DBIx::Class::Core/;
32 __PACKAGE__->table('foo');
39 My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
43 my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
54 my ($class, $name, $to_register) = @_;
55 my %reg = %{$class->class_registrations};
56 $reg{$name} = $to_register;
57 $class->class_registrations(\%reg);
60 sub registered_classes {
61 return values %{shift->class_registrations};
66 my @comp = grep { $_ !~ /^#/ } @_;
68 eval "require Module::Find;";
69 $class->throw("No arguments to load_classes and couldn't load".
70 " Module::Find ($@)") if $@;
71 @comp = map { substr $_, length "${class}::" }
72 Module::Find::findallmod($class);
74 foreach my $comp (@comp) {
75 my $comp_class = "${class}::${comp}";
76 eval "use $comp_class";
78 $class->register_class($comp => $comp_class);
82 sub compose_connection {
83 my ($class, $target, @info) = @_;
84 my $conn_class = "${target}::_db";
85 $class->setup_connection_class($conn_class, @info);
86 my %reg = %{ $class->class_registrations };
89 while (my ($comp, $comp_class) = each %reg) {
90 my $target_class = "${target}::${comp}";
91 $class->inject_base($target_class, $comp_class, $conn_class);
92 @map{$comp, $comp_class} = ($target_class, $target_class);
96 *{"${target}::class"} =
98 my ($class, $to_map) = @_;
102 $conn_class->class_resolver($target);
105 sub setup_connection_class {
106 my ($class, $target, @info) = @_;
107 $class->inject_base($target => 'DBIx::Class');
108 $target->load_components('DB');
109 $target->connection(@info);
113 my ($class, $target, @to_inject) = @_;
116 unshift(@{"${target}::ISA"}, @to_inject);
126 Matt S. Trout <perl-stuff@trout.me.uk>
130 You may distribute this code under the same terms as Perl itself.