1 package DBIx::Class::Schema;
5 use UNIVERSAL::require;
7 use base qw/DBIx::Class/;
9 __PACKAGE__->load_components(qw/Exception/);
10 __PACKAGE__->mk_classdata('class_mappings' => {});
11 __PACKAGE__->mk_classdata('source_registrations' => {});
12 __PACKAGE__->mk_classdata('storage_type' => 'DBI');
13 __PACKAGE__->mk_classdata('storage');
17 DBIx::Class::Schema - composable schemas
25 use base qw/DBIx::Class::Schema/;
27 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
31 package My::Schema::Foo;
33 use base qw/DBIx::Class/;
35 __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example
36 __PACKAGE__->table('foo');
43 My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
47 my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
51 Creates database classes based on a schema. This allows you to have more than
52 one concurrent connection using the same database classes, by making
53 subclasses under a new namespace for each connection. If you only need one
54 class, you should probably use L<DBIx::Class::DB> directly instead.
56 NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS>
57 carefully as DBIx::Class does things a little differently. Note in
58 particular which module inherits off which.
62 =head2 register_class <moniker> <component_class>
64 Registers a class which isa ResultSourceInstance; equivalent to calling
66 $schema->register_source($moniker, $class->result_source_instance);
71 my ($self, $moniker, $to_register) = @_;
72 $self->register_source($moniker => $to_register->result_source_instance);
75 =head2 register_source <moniker> <result source>
77 Registers the result source in the schema with the given moniker
82 my ($self, $moniker, $source) = @_;
83 my %reg = %{$self->source_registrations};
84 $reg{$moniker} = $source;
85 $self->source_registrations(\%reg);
86 $source->schema($self);
87 if ($source->result_class) {
88 my %map = %{$self->class_mappings};
89 $map{$source->result_class} = $moniker;
90 $self->class_mappings(\%map);
96 my $class = $schema->class('Foo');
98 Retrieves the result class name for a given result source
103 my ($self, $moniker) = @_;
104 return $self->source($moniker)->result_class;
109 my $source = $schema->source('Foo');
111 Returns the result source object for the registered name
116 my ($self, $moniker) = @_;
117 my $sreg = $self->source_registrations;
118 return $sreg->{$moniker} if exists $sreg->{$moniker};
120 # if we got here, they probably passed a full class name
121 my $mapped = $self->class_mappings->{$moniker};
122 die "Can't find source for ${moniker}"
123 unless $mapped && exists $sreg->{$mapped};
124 return $sreg->{$mapped};
129 my @source_monikers = $schema->sources;
131 Returns the source monikers of all source registrations on this schema
135 sub sources { return keys %{shift->source_registrations}; }
139 my $rs = $schema->resultset('Foo');
141 Returns the resultset for the registered moniker
146 my ($self, $moniker) = @_;
147 return $self->source($moniker)->resultset;
150 =head2 load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
152 Uses L<Module::Find> to find all classes under the database class' namespace,
153 or uses the classes you select. Then it loads the component (using L<use>),
154 and registers them (using B<register_class>);
156 It is possible to comment out classes with a leading '#', but note that perl
157 will think it's a mistake (trying to use a comment in a qw list) so you'll
158 need to add "no warnings 'qw';" before your load_classes call.
163 my ($class, @params) = @_;
168 foreach my $param (@params) {
169 if (ref $param eq 'ARRAY') {
170 # filter out commented entries
171 my @modules = grep { $_ !~ /^#/ } @$param;
173 push (@{$comps_for{$class}}, @modules);
175 elsif (ref $param eq 'HASH') {
176 # more than one namespace possible
177 for my $comp ( keys %$param ) {
178 # filter out commented entries
179 my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}};
181 push (@{$comps_for{$comp}}, @modules);
185 # filter out commented entries
186 push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
190 eval "require Module::Find;";
191 $class->throw("No arguments to load_classes and couldn't load".
192 " Module::Find ($@)") if $@;
193 my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class);
194 $comps_for{$class} = \@comp;
197 foreach my $prefix (keys %comps_for) {
198 foreach my $comp (@{$comps_for{$prefix}||[]}) {
199 my $comp_class = "${prefix}::${comp}";
200 eval "use $comp_class"; # If it fails, assume the user fixed it
202 die $@ unless $@ =~ /Can't locate/;
204 $class->register_class($comp => $comp_class);
209 =head2 compose_connection <target> <@db_info>
211 This is the most important method in this class. it takes a target namespace,
212 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
213 well as subclasses for each of your database classes in this namespace, using
216 It will also setup a ->class method on the target class, which lets you
217 resolve database classes based on the schema component name, for example
219 MyApp::DB->class('Foo') # returns MyApp::DB::Foo,
220 # which ISA MyApp::Schema::Foo
222 This is the recommended API for accessing Schema generated classes, and
223 using it might give you instant advantages with future versions of DBIC.
225 WARNING: Loading components into Schema classes after compose_connection
226 may not cause them to be seen by the classes in your target namespace due
227 to the dispatch table approach used by Class::C3. If you do this you may find
228 you need to call Class::C3->reinitialize() afterwards to get the behaviour
233 sub compose_connection {
234 my ($self, $target, @info) = @_;
235 my $base = 'DBIx::Class::ResultSetInstance';
237 my $schema = $self->compose_namespace($target, $base);
238 $schema->connection(@info);
239 foreach my $moniker ($schema->sources) {
240 my $source = $schema->source($moniker);
241 my $class = $source->result_class;
242 #warn "$moniker $class $source ".$source->storage;
243 $class->mk_classdata(result_source_instance => $source);
244 $class->mk_classdata(resultset_instance => $source->resultset);
245 $class->mk_classdata(class_resolver => $schema);
250 sub compose_namespace {
251 my ($self, $target, $base) = @_;
252 my %reg = %{ $self->source_registrations };
255 my $schema = $self->clone;
256 foreach my $moniker ($schema->sources) {
257 my $source = $schema->source($moniker);
258 my $target_class = "${target}::${moniker}";
260 $target_class => $source->result_class, ($base ? $base : ())
262 $source->result_class($target_class);
266 *{"${target}::schema"} =
268 foreach my $meth (qw/class source resultset/) {
269 *{"${target}::${meth}"} =
270 sub { shift->schema->$meth(@_) };
276 =head2 setup_connection_class <$target> <@info>
278 Sets up a database connection class to inject between the schema
279 and the subclasses the schema creates.
283 sub setup_connection_class {
284 my ($class, $target, @info) = @_;
285 $class->inject_base($target => 'DBIx::Class::DB');
286 #$target->load_components('DB');
287 $target->connection(@info);
290 =head2 connection(@args)
292 Instantiates a new Storage object of type storage_type and passes the
293 arguments to $storage->connection_info. Sets the connection in-place on
299 my ($self, @info) = @_;
300 my $storage_class = 'DBIx::Class::Storage::'.$self->storage_type;
301 $storage_class->require;
302 my $storage = $storage_class->new;
303 $storage->connect_info(\@info);
304 $self->storage($storage);
308 =head2 connect(@info)
310 Conveneience method, equivalent to $schema->clone->connection(@info)
314 sub connect { shift->clone->connection(@_) };
318 Clones the schema and its associated result_source objects and returns the
325 my $clone = bless({ (ref $self ? %$self : ()) }, ref $self || $self);
326 foreach my $moniker ($self->sources) {
327 my $source = $self->source($moniker);
328 my $new = $source->new($source);
329 $clone->register_source($moniker => $new);
338 Matt S. Trout <mst@shadowcatsystems.co.uk>
342 You may distribute this code under the same terms as Perl itself.