Disabled pod test, tweaked storage_type behaviour, couple code fixups
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema.pm
1 package DBIx::Class::Schema;
2
3 use strict;
4 use warnings;
5
6 use Carp qw/croak/;
7 use UNIVERSAL::require;
8
9 use base qw/DBIx::Class/;
10
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');
16
17 =head1 NAME
18
19 DBIx::Class::Schema - composable schemas
20
21 =head1 SYNOPSIS
22
23   package My::Schema;
24   use base qw/DBIx::Class::Schema/;
25   
26   # load My::Schema::Foo, My::Schema::Bar, My::Schema::Baz
27   __PACKAGE__->load_classes(qw/Foo Bar Baz/);
28
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');
33
34   my $schema1 = My::Schema->connect(
35     $dsn,
36     $user,
37     $password,
38     $attrs
39   );
40
41   my $schema2 = My::Schema->connect( ... );
42
43   # fetch objects using My::Schema::Foo
44   my $resultset = $schema1->resultset('Foo')->search( ... );
45   my @objects = $schema2->resultset('Foo')->search( ... );
46
47 =head1 DESCRIPTION
48
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
51 with your classes.
52
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.
56
57 =head1 METHODS
58
59 =head2 register_class <moniker> <component_class>
60
61 Registers a class which isa ResultSourceProxy; equivalent to calling
62
63   $schema->register_source($moniker, $class->result_source_instance);
64
65 =cut
66
67 sub register_class {
68   my ($self, $moniker, $to_register) = @_;
69   $self->register_source($moniker => $to_register->result_source_instance);
70 }
71
72 =head2 register_source <moniker> <result source>
73
74 Registers the result source in the schema with the given moniker
75
76 =cut
77
78 sub register_source {
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);
88   }
89
90
91 =head2 class
92
93   my $class = $schema->class('Foo');
94
95 Retrieves the result class name for a given result source
96
97 =cut
98
99 sub class {
100   my ($self, $moniker) = @_;
101   return $self->source($moniker)->result_class;
102 }
103
104 =head2 source
105
106   my $source = $schema->source('Foo');
107
108 Returns the result source object for the registered name
109
110 =cut
111
112 sub source {
113   my ($self, $moniker) = @_;
114   my $sreg = $self->source_registrations;
115   return $sreg->{$moniker} if exists $sreg->{$moniker};
116
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};
122 }
123
124 =head2 sources
125
126   my @source_monikers = $schema->sources;
127
128 Returns the source monikers of all source registrations on this schema
129
130 =cut
131
132 sub sources { return keys %{shift->source_registrations}; }
133
134 =head2 resultset
135
136   my $rs = $schema->resultset('Foo');
137
138 Returns the resultset for the registered moniker
139
140 =cut
141
142 sub resultset {
143   my ($self, $moniker) = @_;
144   return $self->source($moniker)->resultset;
145 }
146
147 =head2  load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
148
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>);
152
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.
156
157 =cut
158
159 sub load_classes {
160   my ($class, @params) = @_;
161   
162   my %comps_for;
163   
164   if (@params) {
165     foreach my $param (@params) {
166       if (ref $param eq 'ARRAY') {
167         # filter out commented entries
168         my @modules = grep { $_ !~ /^#/ } @$param;
169         
170         push (@{$comps_for{$class}}, @modules);
171       }
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}};
177
178           push (@{$comps_for{$comp}}, @modules);
179         }
180       }
181       else {
182         # filter out commented entries
183         push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
184       }
185     }
186   } else {
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;
192   }
193
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
198       if ($@) {
199         die $@ unless $@ =~ /Can't locate/;
200       }
201       $class->register_class($comp => $comp_class);
202     }
203   }
204 }
205
206 =head2 compose_connection <target> <@db_info>
207
208 This is the most important method in this class. it takes a target namespace,
209 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
210 well as subclasses for each of your database classes in this namespace, using
211 this connection.
212
213 It will also setup a ->class method on the target class, which lets you
214 resolve database classes based on the schema component name, for example
215
216   MyApp::DB->class('Foo') # returns MyApp::DB::Foo, 
217                           # which ISA MyApp::Schema::Foo
218
219 This is the recommended API for accessing Schema generated classes, and 
220 using it might give you instant advantages with future versions of DBIC.
221
222 WARNING: Loading components into Schema classes after compose_connection
223 may not cause them to be seen by the classes in your target namespace due
224 to the dispatch table approach used by Class::C3. If you do this you may find
225 you need to call Class::C3->reinitialize() afterwards to get the behaviour
226 you expect.
227
228 =cut
229
230 sub compose_connection {
231   my ($self, $target, @info) = @_;
232   my $base = 'DBIx::Class::ResultSetProxy';
233   $base->require;
234   my $schema = $self->compose_namespace($target, $base);
235   $schema->connection(@info);
236   foreach my $moniker ($schema->sources) {
237     my $source = $schema->source($moniker);
238     my $class = $source->result_class;
239     #warn "$moniker $class $source ".$source->storage;
240     $class->mk_classdata(result_source_instance => $source);
241     $class->mk_classdata(resultset_instance => $source->resultset);
242     $class->mk_classdata(class_resolver => $schema);
243   }
244   return $schema;
245 }
246
247 sub compose_namespace {
248   my ($self, $target, $base) = @_;
249   my %reg = %{ $self->source_registrations };
250   my %target;
251   my %map;
252   my $schema = $self->clone;
253   foreach my $moniker ($schema->sources) {
254     my $source = $schema->source($moniker);
255     my $target_class = "${target}::${moniker}";
256     $self->inject_base(
257       $target_class => $source->result_class, ($base ? $base : ())
258     );
259     $source->result_class($target_class);
260   }
261   {
262     no strict 'refs';
263     *{"${target}::schema"} =
264       sub { $schema };
265     foreach my $meth (qw/class source resultset/) {
266       *{"${target}::${meth}"} =
267         sub { shift->schema->$meth(@_) };
268     }
269   }
270   return $schema;
271 }
272
273 =head2 setup_connection_class <$target> <@info>
274
275 Sets up a database connection class to inject between the schema
276 and the subclasses the schema creates.
277
278 =cut
279
280 sub setup_connection_class {
281   my ($class, $target, @info) = @_;
282   $class->inject_base($target => 'DBIx::Class::DB');
283   #$target->load_components('DB');
284   $target->connection(@info);
285 }
286
287 =head2 connection(@args)
288
289 Instantiates a new Storage object of type storage_type and passes the
290 arguments to $storage->connection_info. Sets the connection in-place on
291 the schema.
292
293 =cut
294
295 sub connection {
296   my ($self, @info) = @_;
297   my $storage_class = $self->storage_type;
298   $storage_class = 'DBIx::Class::Storage'.$storage_class
299     if $storage_class =~ m/^::/;
300   $storage_class->require;
301   my $storage = $storage_class->new;
302   $storage->connect_info(\@info);
303   $self->storage($storage);
304   return $self;
305 }
306
307 =head2 connect(@info)
308
309 Conveneience method, equivalent to $schema->clone->connection(@info)
310
311 =cut
312
313 sub connect { shift->clone->connection(@_) };
314
315 =head2 clone
316
317 Clones the schema and its associated result_source objects and returns the
318 copy.
319
320 =cut
321
322 sub clone {
323   my ($self) = @_;
324   my $clone = bless({ (ref $self ? %$self : ()) }, ref $self || $self);
325   foreach my $moniker ($self->sources) {
326     my $source = $self->source($moniker);
327     my $new = $source->new($source);
328     $clone->register_source($moniker => $new);
329   }
330   return $clone;
331 }
332
333 1;
334
335 =head1 AUTHORS
336
337 Matt S. Trout <mst@shadowcatsystems.co.uk>
338
339 =head1 LICENSE
340
341 You may distribute this code under the same terms as Perl itself.
342
343 =cut
344