1 package DBIx::Class::Schema;
7 use UNIVERSAL::require;
9 use base qw/DBIx::Class/;
11 __PACKAGE__->load_components(qw/Exception/);
12 __PACKAGE__->mk_classdata('class_mappings' => {});
13 __PACKAGE__->mk_classdata('source_registrations' => {});
14 __PACKAGE__->mk_classdata('storage_type' => 'DBI');
15 __PACKAGE__->mk_classdata('storage');
19 DBIx::Class::Schema - composable schemas
27 use base qw/DBIx::Class::Schema/;
29 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
33 package My::Schema::Foo;
35 use base qw/DBIx::Class/;
37 __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example
38 __PACKAGE__->table('foo');
45 My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
49 my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
53 Creates database classes based on a schema. This allows you to have more than
54 one concurrent connection using the same database classes, by making
55 subclasses under a new namespace for each connection. If you only need one
56 class, you should probably use L<DBIx::Class::DB> directly instead.
58 NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS>
59 carefully as DBIx::Class does things a little differently. Note in
60 particular which module inherits off which.
64 =head2 register_class <moniker> <component_class>
66 Registers a class which isa ResultSourceInstance; equivalent to calling
68 $schema->register_source($moniker, $class->result_source_instance);
73 my ($self, $moniker, $to_register) = @_;
74 $self->register_source($moniker => $to_register->result_source_instance);
77 =head2 register_source <moniker> <result source>
79 Registers the result source in the schema with the given moniker
84 my ($self, $moniker, $source) = @_;
85 my %reg = %{$self->source_registrations};
86 $reg{$moniker} = $source;
87 $self->source_registrations(\%reg);
88 $source->schema($self);
89 if ($source->result_class) {
90 my %map = %{$self->class_mappings};
91 $map{$source->result_class} = $moniker;
92 $self->class_mappings(\%map);
98 my $class = $schema->class('Foo');
100 Retrieves the result class name for a given result source
105 my ($self, $moniker) = @_;
106 return $self->source($moniker)->result_class;
111 my $source = $schema->source('Foo');
113 Returns the result source object for the registered name
118 my ($self, $moniker) = @_;
119 my $sreg = $self->source_registrations;
120 return $sreg->{$moniker} if exists $sreg->{$moniker};
122 # if we got here, they probably passed a full class name
123 my $mapped = $self->class_mappings->{$moniker};
124 croak "Can't find source for ${moniker}"
125 unless $mapped && exists $sreg->{$mapped};
126 return $sreg->{$mapped};
131 my @source_monikers = $schema->sources;
133 Returns the source monikers of all source registrations on this schema
137 sub sources { return keys %{shift->source_registrations}; }
141 my $rs = $schema->resultset('Foo');
143 Returns the resultset for the registered moniker
148 my ($self, $moniker) = @_;
149 return $self->source($moniker)->resultset;
152 =head2 load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
154 Uses L<Module::Find> to find all classes under the database class' namespace,
155 or uses the classes you select. Then it loads the component (using L<use>),
156 and registers them (using B<register_class>);
158 It is possible to comment out classes with a leading '#', but note that perl
159 will think it's a mistake (trying to use a comment in a qw list) so you'll
160 need to add "no warnings 'qw';" before your load_classes call.
165 my ($class, @params) = @_;
170 foreach my $param (@params) {
171 if (ref $param eq 'ARRAY') {
172 # filter out commented entries
173 my @modules = grep { $_ !~ /^#/ } @$param;
175 push (@{$comps_for{$class}}, @modules);
177 elsif (ref $param eq 'HASH') {
178 # more than one namespace possible
179 for my $comp ( keys %$param ) {
180 # filter out commented entries
181 my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}};
183 push (@{$comps_for{$comp}}, @modules);
187 # filter out commented entries
188 push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
192 eval "require Module::Find;";
193 $class->throw("No arguments to load_classes and couldn't load".
194 " Module::Find ($@)") if $@;
195 my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class);
196 $comps_for{$class} = \@comp;
199 foreach my $prefix (keys %comps_for) {
200 foreach my $comp (@{$comps_for{$prefix}||[]}) {
201 my $comp_class = "${prefix}::${comp}";
202 eval "use $comp_class"; # If it fails, assume the user fixed it
204 die $@ unless $@ =~ /Can't locate/;
206 $class->register_class($comp => $comp_class);
211 =head2 compose_connection <target> <@db_info>
213 This is the most important method in this class. it takes a target namespace,
214 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
215 well as subclasses for each of your database classes in this namespace, using
218 It will also setup a ->class method on the target class, which lets you
219 resolve database classes based on the schema component name, for example
221 MyApp::DB->class('Foo') # returns MyApp::DB::Foo,
222 # which ISA MyApp::Schema::Foo
224 This is the recommended API for accessing Schema generated classes, and
225 using it might give you instant advantages with future versions of DBIC.
227 WARNING: Loading components into Schema classes after compose_connection
228 may not cause them to be seen by the classes in your target namespace due
229 to the dispatch table approach used by Class::C3. If you do this you may find
230 you need to call Class::C3->reinitialize() afterwards to get the behaviour
235 sub compose_connection {
236 my ($self, $target, @info) = @_;
237 my $base = 'DBIx::Class::ResultSetInstance';
239 my $schema = $self->compose_namespace($target, $base);
240 $schema->connection(@info);
241 foreach my $moniker ($schema->sources) {
242 my $source = $schema->source($moniker);
243 my $class = $source->result_class;
244 #warn "$moniker $class $source ".$source->storage;
245 $class->mk_classdata(result_source_instance => $source);
246 $class->mk_classdata(resultset_instance => $source->resultset);
247 $class->mk_classdata(class_resolver => $schema);
252 sub compose_namespace {
253 my ($self, $target, $base) = @_;
254 my %reg = %{ $self->source_registrations };
257 my $schema = $self->clone;
258 foreach my $moniker ($schema->sources) {
259 my $source = $schema->source($moniker);
260 my $target_class = "${target}::${moniker}";
262 $target_class => $source->result_class, ($base ? $base : ())
264 $source->result_class($target_class);
268 *{"${target}::schema"} =
270 foreach my $meth (qw/class source resultset/) {
271 *{"${target}::${meth}"} =
272 sub { shift->schema->$meth(@_) };
278 =head2 setup_connection_class <$target> <@info>
280 Sets up a database connection class to inject between the schema
281 and the subclasses the schema creates.
285 sub setup_connection_class {
286 my ($class, $target, @info) = @_;
287 $class->inject_base($target => 'DBIx::Class::DB');
288 #$target->load_components('DB');
289 $target->connection(@info);
292 =head2 connection(@args)
294 Instantiates a new Storage object of type storage_type and passes the
295 arguments to $storage->connection_info. Sets the connection in-place on
301 my ($self, @info) = @_;
302 my $storage_class = 'DBIx::Class::Storage::'.$self->storage_type;
303 $storage_class->require;
304 my $storage = $storage_class->new;
305 $storage->connect_info(\@info);
306 $self->storage($storage);
310 =head2 connect(@info)
312 Conveneience method, equivalent to $schema->clone->connection(@info)
316 sub connect { shift->clone->connection(@_) };
320 Clones the schema and its associated result_source objects and returns the
327 my $clone = bless({ (ref $self ? %$self : ()) }, ref $self || $self);
328 foreach my $moniker ($self->sources) {
329 my $source = $self->source($moniker);
330 my $new = $source->new($source);
331 $clone->register_source($moniker => $new);
340 Matt S. Trout <mst@shadowcatsystems.co.uk>
344 You may distribute this code under the same terms as Perl itself.