1 package DBIx::Class::Schema;
7 use base qw/DBIx::Class/;
9 __PACKAGE__->load_components(qw/Exception/);
10 __PACKAGE__->mk_classdata('class_registrations' => {});
11 __PACKAGE__->mk_classdata('storage_type' => 'DBI');
12 __PACKAGE__->mk_classdata('storage');
16 DBIx::Class::Schema - composable schemas
24 use base qw/DBIx::Class::Schema/;
26 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
30 package My::Schema::Foo;
32 use base qw/DBIx::Class/;
34 __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example
35 __PACKAGE__->table('foo');
42 My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
46 my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
50 Creates database classes based on a schema. This allows you to have more than
51 one concurrent connection using the same database classes, by making
52 subclasses under a new namespace for each connection. If you only need one
53 class, you should probably use L<DBIx::Class::DB> directly instead.
55 NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS>
56 carefully as DBIx::Class does things a little differently. Note in
57 particular which module inherits off which.
61 =head2 register_class <component> <component_class>
63 Registers the class in the schema's class_registrations. This is a hash
64 containing database classes, keyed by their monikers. It's used by
65 compose_connection to create/modify all the existing database classes.
70 my ($self, $name, $to_register) = @_;
71 my %reg = %{$self->class_registrations};
72 $reg{$name} = $to_register;
73 $self->class_registrations(\%reg);
74 $to_register->result_source->schema($self);
77 =head2 registered_classes
79 Simple read-only accessor for the schema's registered classes. See
80 register_class above if you want to modify it.
85 sub registered_classes {
86 return values %{shift->class_registrations};
91 my $class = $schema->class('Foo');
93 Shortcut to retrieve a single class by its registered name
98 my ($self, $class) = @_;
99 return $self->class_registrations->{$class};
104 my $source = $schema->source('Foo');
106 Returns the result source object for the registered name
111 my ($self, $class) = @_;
112 return $self->class_registrations->{$class}->result_source;
117 my $rs = $schema->resultset('Foo');
119 Returns the resultset for the registered name
124 my ($self, $class) = @_;
125 return $self->class_registrations->{$class}->result_source->resultset;
129 =head2 load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
131 Uses L<Module::Find> to find all classes under the database class' namespace,
132 or uses the classes you select. Then it loads the component (using L<use>),
133 and registers them (using B<register_class>);
135 It is possible to comment out classes with a leading '#', but note that perl
136 will think it's a mistake (trying to use a comment in a qw list) so you'll
137 need to add "no warnings 'qw';" before your load_classes call.
142 my ($class, @params) = @_;
147 foreach my $param (@params) {
148 if (ref $param eq 'ARRAY') {
149 # filter out commented entries
150 my @modules = grep { $_ !~ /^#/ } @$param;
152 push (@{$comps_for{$class}}, @modules);
154 elsif (ref $param eq 'HASH') {
155 # more than one namespace possible
156 for my $comp ( keys %$param ) {
157 # filter out commented entries
158 my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}};
160 push (@{$comps_for{$comp}}, @modules);
164 # filter out commented entries
165 push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
169 eval "require Module::Find;";
170 $class->throw("No arguments to load_classes and couldn't load".
171 " Module::Find ($@)") if $@;
172 my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class);
173 $comps_for{$class} = \@comp;
176 foreach my $prefix (keys %comps_for) {
177 foreach my $comp (@{$comps_for{$prefix}||[]}) {
178 my $comp_class = "${prefix}::${comp}";
179 eval "use $comp_class"; # If it fails, assume the user fixed it
181 die $@ unless $@ =~ /Can't locate/;
183 $class->register_class($comp => $comp_class);
188 =head2 compose_connection <target> <@db_info>
190 This is the most important method in this class. it takes a target namespace,
191 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
192 well as subclasses for each of your database classes in this namespace, using
195 It will also setup a ->class method on the target class, which lets you
196 resolve database classes based on the schema component name, for example
198 MyApp::DB->class('Foo') # returns MyApp::DB::Foo,
199 # which ISA MyApp::Schema::Foo
201 This is the recommended API for accessing Schema generated classes, and
202 using it might give you instant advantages with future versions of DBIC.
204 WARNING: Loading components into Schema classes after compose_connection
205 may not cause them to be seen by the classes in your target namespace due
206 to the dispatch table approach used by Class::C3. If you do this you may find
207 you need to call Class::C3->reinitialize() afterwards to get the behaviour
212 sub compose_connection {
213 my ($self, $target, @info) = @_;
214 my $conn_class = "${target}::_db";
215 $self->setup_connection_class($conn_class, @info);
216 my $schema = $self->compose_namespace($target, $conn_class);
217 $schema->storage($conn_class->storage);
218 foreach my $class ($schema->registered_classes) {
219 my $source = $class->result_source;
220 $source = $source->new($source);
221 $source->schema($schema);
222 $source->result_class($class);
223 $class->mk_classdata(result_source => $source);
224 $class->mk_classdata(resultset_instance => $source->resultset);
229 sub compose_namespace {
230 my ($class, $target, $base) = @_;
231 my %reg = %{ $class->class_registrations };
234 my $schema = bless({ }, $class);
235 while (my ($comp, $comp_class) = each %reg) {
236 my $target_class = "${target}::${comp}";
237 $class->inject_base($target_class, $comp_class, ($base ? $base : ()));
238 @map{$comp, $comp_class} = ($target_class, $target_class);
240 $schema->class_registrations(\%map);
243 *{"${target}::schema"} =
245 *{"${target}::class"} =
246 sub { shift->schema->class(@_) };
248 $base->class_resolver($target);
252 =head2 setup_connection_class <$target> <@info>
254 Sets up a database connection class to inject between the schema
255 and the subclasses the schema creates.
259 sub setup_connection_class {
260 my ($class, $target, @info) = @_;
261 $class->inject_base($target => 'DBIx::Class::DB');
262 #$target->load_components('DB');
263 $target->connection(@info);
270 Matt S. Trout <mst@shadowcatsystems.co.uk>
274 You may distribute this code under the same terms as Perl itself.