0d6bec84d6ecd125785542ac1c71ecbbb1e075d0
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema.pm
1 package DBIx::Class::Schema;
2
3 use strict;
4 use warnings;
5 use DBIx::Class::DB;
6
7 use base qw/DBIx::Class/;
8
9 __PACKAGE__->load_components(qw/Exception/);
10 __PACKAGE__->mk_classdata('class_registrations' => {});
11
12 =head1 NAME
13
14 DBIx::Class::Schema - composable schemas
15
16 =head1 SYNOPSIS
17
18 in My/Schema.pm
19
20   package My::Schema;
21
22   use base qw/DBIx::Class::Schema/;
23
24   __PACKAGE__->load_classes(qw/Foo Bar Baz/);
25
26 in My/Schema/Foo.pm
27
28   package My::Schema::Foo;
29
30   use base qw/DBIx::Class/;
31
32   __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example
33   __PACKAGE__->table('foo');
34   ...
35
36 in My/DB.pm
37
38   use My::Schema;
39
40   My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
41
42 then in app code
43
44   my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
45
46 =head1 DESCRIPTION
47
48 Creates database classes based on a schema. This allows you to have more than
49 one concurrent connection using the same database classes, by making 
50 subclasses under a new namespace for each connection. If you only need one 
51 class, you should probably use L<DBIx::Class::DB> directly instead.
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 <component> <component_class>
60
61 Registers the class in the schema's class_registrations. This is a hash
62 containing database classes, keyed by their monikers. It's used by
63 compose_connection to create/modify all the existing database classes.
64
65 =cut
66
67 sub register_class {
68   my ($class, $name, $to_register) = @_;
69   my %reg = %{$class->class_registrations};
70   $reg{$name} = $to_register;
71   $class->class_registrations(\%reg);
72 }
73
74 =head2 registered_classes
75
76 Simple read-only accessor for the schema's registered classes. See 
77 register_class above if you want to modify it.
78
79
80 =cut
81
82 sub registered_classes {
83   return values %{shift->class_registrations};
84 }
85
86 =head2  load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
87
88 Uses L<Module::Find> to find all classes under the database class' namespace,
89 or uses the classes you select.  Then it loads the component (using L<use>), 
90 and registers them (using B<register_class>);
91
92 It is possible to comment out classes with a leading '#', but note that perl
93 will think it's a mistake (trying to use a comment in a qw list) so you'll
94 need to add "no warnings 'qw';" before your load_classes call.
95
96 =cut
97
98 sub load_classes {
99   my ($class, @params) = @_;
100   
101   my %comps_for;
102   
103   if (@params) {
104     foreach my $param (@params) {
105       if (ref $param eq 'ARRAY') {
106         # filter out commented entries
107         my @modules = grep { $_ !~ /^#/ } @$param;
108         
109         push (@{$comps_for{$class}}, @modules);
110       }
111       elsif (ref $param eq 'HASH') {
112         # more than one namespace possible
113         for my $comp ( keys %$param ) {
114           # filter out commented entries
115           my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}};
116
117           push (@{$comps_for{$comp}}, @modules);
118         }
119       }
120       else {
121         # filter out commented entries
122         push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
123       }
124     }
125   } else {
126     eval "require Module::Find;";
127     $class->throw("No arguments to load_classes and couldn't load".
128       " Module::Find ($@)") if $@;
129     my @comp = map { substr $_, length "${class}::"  } Module::Find::findallmod($class);
130     $comps_for{$class} = \@comp;
131   }
132
133   foreach my $prefix (keys %comps_for) {
134     foreach my $comp (@{$comps_for{$prefix}||[]}) {
135       my $comp_class = "${prefix}::${comp}";
136       eval "use $comp_class"; # If it fails, assume the user fixed it
137       $class->register_class($comp => $comp_class);
138     }
139   }
140 }
141
142 =head2 compose_connection <target> <@db_info>
143
144 This is the most important method in this class. it takes a target namespace,
145 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
146 well as subclasses for each of your database classes in this namespace, using
147 this connection.
148
149 It will also setup a ->class method on the target class, which lets you
150 resolve database classes based on the schema component name, for example
151
152   MyApp::DB->class('Foo') # returns MyApp::DB::Foo, 
153                           # which ISA MyApp::Schema::Foo
154
155 This is the recommended API for accessing Schema generated classes, and 
156 using it might give you instant advantages with future versions of DBIC.
157
158 WARNING: Loading components into Schema classes after compose_connection
159 may not cause them to be seen by the classes in your target namespace due
160 to the dispatch table approach used by Class::C3. If you do this you may find
161 you need to call Class::C3->reinitialize() afterwards to get the behaviour
162 you expect.
163
164 =cut
165
166 sub compose_connection {
167   my ($class, $target, @info) = @_;
168   my $conn_class = "${target}::_db";
169   $class->setup_connection_class($conn_class, @info);
170   $class->compose_namespace($target, $conn_class);
171 }
172
173 sub compose_namespace {
174   my ($class, $target, $base) = @_;
175   my %reg = %{ $class->class_registrations };
176   my %target;
177   my %map;
178   while (my ($comp, $comp_class) = each %reg) {
179     my $target_class = "${target}::${comp}";
180     $class->inject_base($target_class, $comp_class, $base);
181     @map{$comp, $comp_class} = ($target_class, $target_class);
182   }
183   {
184     no strict 'refs';
185     *{"${target}::class"} =
186       sub {
187         my ($class, $to_map) = @_;
188         return $map{$to_map};
189       };
190     *{"${target}::classes"} = sub { return \%map; };
191   }
192   $base->class_resolver($target);
193 }
194
195 =head2 setup_connection_class <$target> <@info>
196
197 Sets up a database connection class to inject between the schema
198 and the subclasses the schema creates.
199
200 =cut
201
202 sub setup_connection_class {
203   my ($class, $target, @info) = @_;
204   $class->inject_base($target => 'DBIx::Class::DB');
205   #$target->load_components('DB');
206   $target->connection(@info);
207 }
208
209 1;
210
211 =head1 AUTHORS
212
213 Matt S. Trout <mst@shadowcatsystems.co.uk>
214
215 =head1 LICENSE
216
217 You may distribute this code under the same terms as Perl itself.
218
219 =cut
220