1 package DBIx::Class::Schema;
6 use base qw/Class::Data::Inheritable/;
7 use base qw/DBIx::Class/;
9 __PACKAGE__->load_components(qw/Exception Componentised/);
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
51 =item register_class <component> <component_class>
53 Registers the class in the schema's class_registrations. This is a hash
54 containing components, and their representative classes. It's used by
55 compose_connection to create/modify all the existing database classes.
60 my ($class, $name, $to_register) = @_;
61 my %reg = %{$class->class_registrations};
62 $reg{$name} = $to_register;
63 $class->class_registrations(\%reg);
66 =item registered_classes
68 Simple read-only accessor for the schema's registered classes. See
69 register_class above if you want to modify it.
74 sub registered_classes {
75 return values %{shift->class_registrations};
78 =item load_classes [<classes>}
80 Uses L<Module::Find> to find all components, unless specified explicitly.
81 Then it loads the component (using L<use>), and registers them (using
88 my @comp = grep { $_ !~ /^#/ } @_;
90 eval "require Module::Find;";
91 $class->throw("No arguments to load_classes and couldn't load".
92 " Module::Find ($@)") if $@;
93 @comp = map { substr $_, length "${class}::" }
94 Module::Find::findallmod($class);
96 foreach my $comp (@comp) {
97 my $comp_class = "${class}::${comp}";
98 eval "use $comp_class"; # If it fails, assume the user fixed it
99 $class->register_class($comp => $comp_class);
103 =item compose_connection
107 sub compose_connection {
108 my ($class, $target, @info) = @_;
109 my $conn_class = "${target}::_db";
110 $class->setup_connection_class($conn_class, @info);
111 my %reg = %{ $class->class_registrations };
114 while (my ($comp, $comp_class) = each %reg) {
115 my $target_class = "${target}::${comp}";
116 $class->inject_base($target_class, $conn_class, $comp_class);
117 $target_class->table($comp_class->table);
118 @map{$comp, $comp_class} = ($target_class, $target_class);
122 *{"${target}::class"} =
124 my ($class, $to_map) = @_;
125 return $map{$to_map};
127 *{"${target}::classes"} = sub { return \%map; };
129 $conn_class->class_resolver($target);
132 =item setup_connection_class <$target> <@info>
136 sub setup_connection_class {
137 my ($class, $target, @info) = @_;
138 $class->inject_base($target => 'DBIx::Class');
139 $target->load_components('DB');
140 $target->connection(@info);
149 Matt S. Trout <mst@shadowcatsystems.co.uk>
153 You may distribute this code under the same terms as Perl itself.