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
47 Creates database classes based on a schema. This allows you to have more than
48 one concurrent connection using the same database classes, by making
49 subclasses under a new namespace for each connection. If you only need one
50 class, you should probably use L<DBIx::Class::DB> directly instead.
56 =item register_class <component> <component_class>
58 Registers the class in the schema's class_registrations. This is a hash
59 containing database classes, keyed by their monikers. It's used by
60 compose_connection to create/modify all the existing database classes.
65 my ($class, $name, $to_register) = @_;
66 my %reg = %{$class->class_registrations};
67 $reg{$name} = $to_register;
68 $class->class_registrations(\%reg);
71 =item registered_classes
73 Simple read-only accessor for the schema's registered classes. See
74 register_class above if you want to modify it.
79 sub registered_classes {
80 return values %{shift->class_registrations};
83 =item load_classes [<classes>}
85 Uses L<Module::Find> to find all classes under the database class' namespace,
86 or uses the classes you select. Then it loads the component (using L<use>),
87 and registers them (using B<register_class>);
93 my @comp = grep { $_ !~ /^#/ } @_;
95 eval "require Module::Find;";
96 $class->throw("No arguments to load_classes and couldn't load".
97 " Module::Find ($@)") if $@;
98 @comp = map { substr $_, length "${class}::" }
99 Module::Find::findallmod($class);
101 foreach my $comp (@comp) {
102 my $comp_class = "${class}::${comp}";
103 eval "use $comp_class"; # If it fails, assume the user fixed it
104 $class->register_class($comp => $comp_class);
108 =item compose_connection <target> <@db_info>
110 This is the most important method in this class. it takes a target namespace,
111 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
112 well as subclasses for each of your database classes in this namespace, using
115 It will also setup a ->table method on the target class, which lets you
116 resolve database classes based on the schema component name, for example
118 MyApp::DB->table('Foo') # returns MyApp::DB::Foo,
119 # which ISA MyApp::Schema::Foo
121 This is the recommended API for accessing Schema generated classes, and
122 using it might give you instant advantages with future versions of DBIC.
126 sub compose_connection {
127 my ($class, $target, @info) = @_;
128 my $conn_class = "${target}::_db";
129 $class->setup_connection_class($conn_class, @info);
130 my %reg = %{ $class->class_registrations };
133 while (my ($comp, $comp_class) = each %reg) {
134 my $target_class = "${target}::${comp}";
135 $class->inject_base($target_class, $conn_class, $comp_class);
136 $target_class->table($comp_class->table);
137 @map{$comp, $comp_class} = ($target_class, $target_class);
141 *{"${target}::class"} =
143 my ($class, $to_map) = @_;
144 return $map{$to_map};
146 *{"${target}::classes"} = sub { return \%map; };
148 $conn_class->class_resolver($target);
151 =item setup_connection_class <$target> <@info>
153 Sets up a database connection class to inject between the schema
154 and the subclasses the schema creates.
158 sub setup_connection_class {
159 my ($class, $target, @info) = @_;
160 $class->inject_base($target => 'DBIx::Class');
161 $target->load_components('DB');
162 $target->connection(@info);
171 Matt S. Trout <mst@shadowcatsystems.co.uk>
175 You may distribute this code under the same terms as Perl itself.