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
24 use base qw/DBIx::Class::Schema/;
26 # load My::Schema::Foo, My::Schema::Bar, My::Schema::Baz
27 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
29 package My::Schema::Foo;
30 use base qw/DBIx::Class/;
31 __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example
32 __PACKAGE__->table('foo');
34 my $schema1 = My::Schema->connect(
41 my $schema2 = My::Schema->connect( ... );
43 # fetch objects using My::Schema::Foo
44 my $resultset = $schema1->resultset('Foo')->search( ... );
45 my @objects = $schema2->resultset('Foo')->search( ... );
49 Creates database classes based on a schema. This is the recommended way to
50 use L<DBIx::Class> and allows you to use more than one concurrent connection
53 NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS>
54 carefully as DBIx::Class does things a little differently. Note in
55 particular which module inherits off which.
59 =head2 register_class <moniker> <component_class>
61 Registers a class which isa ResultSourceProxy; equivalent to calling
63 $schema->register_source($moniker, $class->result_source_instance);
68 my ($self, $moniker, $to_register) = @_;
69 $self->register_source($moniker => $to_register->result_source_instance);
72 =head2 register_source <moniker> <result source>
74 Registers the result source in the schema with the given moniker
79 my ($self, $moniker, $source) = @_;
80 my %reg = %{$self->source_registrations};
81 $reg{$moniker} = $source;
82 $self->source_registrations(\%reg);
83 $source->schema($self);
84 if ($source->result_class) {
85 my %map = %{$self->class_mappings};
86 $map{$source->result_class} = $moniker;
87 $self->class_mappings(\%map);
93 my $class = $schema->class('Foo');
95 Retrieves the result class name for a given result source
100 my ($self, $moniker) = @_;
101 return $self->source($moniker)->result_class;
106 my $source = $schema->source('Foo');
108 Returns the result source object for the registered name
113 my ($self, $moniker) = @_;
114 my $sreg = $self->source_registrations;
115 return $sreg->{$moniker} if exists $sreg->{$moniker};
117 # if we got here, they probably passed a full class name
118 my $mapped = $self->class_mappings->{$moniker};
119 croak "Can't find source for ${moniker}"
120 unless $mapped && exists $sreg->{$mapped};
121 return $sreg->{$mapped};
126 my @source_monikers = $schema->sources;
128 Returns the source monikers of all source registrations on this schema
132 sub sources { return keys %{shift->source_registrations}; }
136 my $rs = $schema->resultset('Foo');
138 Returns the resultset for the registered moniker
143 my ($self, $moniker) = @_;
144 return $self->source($moniker)->resultset;
147 =head2 load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
149 Uses L<Module::Find> to find all classes under the database class' namespace,
150 or uses the classes you select. Then it loads the component (using L<use>),
151 and registers them (using B<register_class>);
153 It is possible to comment out classes with a leading '#', but note that perl
154 will think it's a mistake (trying to use a comment in a qw list) so you'll
155 need to add "no warnings 'qw';" before your load_classes call.
160 my ($class, @params) = @_;
165 foreach my $param (@params) {
166 if (ref $param eq 'ARRAY') {
167 # filter out commented entries
168 my @modules = grep { $_ !~ /^#/ } @$param;
170 push (@{$comps_for{$class}}, @modules);
172 elsif (ref $param eq 'HASH') {
173 # more than one namespace possible
174 for my $comp ( keys %$param ) {
175 # filter out commented entries
176 my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}};
178 push (@{$comps_for{$comp}}, @modules);
182 # filter out commented entries
183 push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
187 eval "require Module::Find;";
188 $class->throw("No arguments to load_classes and couldn't load".
189 " Module::Find ($@)") if $@;
190 my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class);
191 $comps_for{$class} = \@comp;
194 foreach my $prefix (keys %comps_for) {
195 foreach my $comp (@{$comps_for{$prefix}||[]}) {
196 my $comp_class = "${prefix}::${comp}";
197 eval "use $comp_class"; # If it fails, assume the user fixed it
199 die $@ unless $@ =~ /Can't locate/;
201 $class->register_class($comp => $comp_class);
202 # if $class->can('result_source_instance');
207 =head2 compose_connection <target> <@db_info>
209 This is the most important method in this class. it takes a target namespace,
210 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
211 well as subclasses for each of your database classes in this namespace, using
214 It will also setup a ->class method on the target class, which lets you
215 resolve database classes based on the schema component name, for example
217 MyApp::DB->class('Foo') # returns MyApp::DB::Foo,
218 # which ISA MyApp::Schema::Foo
220 This is the recommended API for accessing Schema generated classes, and
221 using it might give you instant advantages with future versions of DBIC.
223 WARNING: Loading components into Schema classes after compose_connection
224 may not cause them to be seen by the classes in your target namespace due
225 to the dispatch table approach used by Class::C3. If you do this you may find
226 you need to call Class::C3->reinitialize() afterwards to get the behaviour
231 sub compose_connection {
232 my ($self, $target, @info) = @_;
233 my $base = 'DBIx::Class::ResultSetProxy';
236 if ($self eq $target) {
237 # Pathological case, largely caused by the docs on early C::M::DBIC::Plain
238 foreach my $moniker ($self->sources) {
239 my $source = $self->source($moniker);
240 my $class = $source->result_class;
241 $self->inject_base($class, $base);
242 $class->mk_classdata(resultset_instance => $source->resultset);
243 $class->mk_classdata(class_resolver => $self);
248 my $schema = $self->compose_namespace($target, $base);
249 $schema->connection(@info);
250 foreach my $moniker ($schema->sources) {
251 my $source = $schema->source($moniker);
252 my $class = $source->result_class;
253 #warn "$moniker $class $source ".$source->storage;
254 $class->mk_classdata(result_source_instance => $source);
255 $class->mk_classdata(resultset_instance => $source->resultset);
256 $class->mk_classdata(class_resolver => $schema);
261 sub compose_namespace {
262 my ($self, $target, $base) = @_;
263 my %reg = %{ $self->source_registrations };
266 my $schema = $self->clone;
267 foreach my $moniker ($schema->sources) {
268 my $source = $schema->source($moniker);
269 my $target_class = "${target}::${moniker}";
271 $target_class => $source->result_class, ($base ? $base : ())
273 $source->result_class($target_class);
277 *{"${target}::schema"} =
279 foreach my $meth (qw/class source resultset/) {
280 *{"${target}::${meth}"} =
281 sub { shift->schema->$meth(@_) };
287 =head2 setup_connection_class <$target> <@info>
289 Sets up a database connection class to inject between the schema
290 and the subclasses the schema creates.
294 sub setup_connection_class {
295 my ($class, $target, @info) = @_;
296 $class->inject_base($target => 'DBIx::Class::DB');
297 #$target->load_components('DB');
298 $target->connection(@info);
301 =head2 connection(@args)
303 Instantiates a new Storage object of type storage_type and passes the
304 arguments to $storage->connect_info. Sets the connection in-place on
310 my ($self, @info) = @_;
311 my $storage_class = $self->storage_type;
312 $storage_class = 'DBIx::Class::Storage'.$storage_class
313 if $storage_class =~ m/^::/;
314 $storage_class->require;
315 my $storage = $storage_class->new;
316 $storage->connect_info(\@info);
317 $self->storage($storage);
321 =head2 connect(@info)
323 Conveneience method, equivalent to $schema->clone->connection(@info)
327 sub connect { shift->clone->connection(@_) };
331 Clones the schema and its associated result_source objects and returns the
338 my $clone = bless({ (ref $self ? %$self : ()) }, ref $self || $self);
339 foreach my $moniker ($self->sources) {
340 my $source = $self->source($moniker);
341 my $new = $source->new($source);
342 $clone->register_source($moniker => $new);
351 Matt S. Trout <mst@shadowcatsystems.co.uk>
355 You may distribute this code under the same terms as Perl itself.