85c2db6ab576942bf783b10d696f6f8cd95afc69
[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/Core PK::Auto::Pg/); # 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 =over 4
60
61 =item register_class <component> <component_class>
62
63 Registers the class in the schema's class_registrations. This is a hash
64 containing database classes, keyed by their monikers. It's used by
65 compose_connection to create/modify all the existing database classes.
66
67 =cut
68
69 sub register_class {
70   my ($class, $name, $to_register) = @_;
71   my %reg = %{$class->class_registrations};
72   $reg{$name} = $to_register;
73   $class->class_registrations(\%reg);
74 }
75
76 =item registered_classes
77
78 Simple read-only accessor for the schema's registered classes. See 
79 register_class above if you want to modify it.
80
81
82 =cut
83
84 sub registered_classes {
85   return values %{shift->class_registrations};
86 }
87
88 =item  load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
89
90 Uses L<Module::Find> to find all classes under the database class' namespace,
91 or uses the classes you select.  Then it loads the component (using L<use>), 
92 and registers them (using B<register_class>);
93
94 It is possible to comment out classes with a leading '#', but note that perl
95 will think it's a mistake (trying to use a comment in a qw list) so you'll
96 need to add "no warnings 'qw';" before your load_classes call.
97
98 =cut
99
100 sub load_classes {
101   my ($class, @params) = @_;
102   
103   my %comps_for;
104   
105   if (@params) {
106     foreach my $param (@params) {
107       if (ref $param eq 'ARRAY') {
108         # filter out commented entries
109         my @modules = grep { $_ !~ /^#/ } @$param;
110         
111         push (@{$comps_for{$class}}, @modules);
112       }
113       elsif (ref $param eq 'HASH') {
114         # more than one namespace possible
115         for my $comp ( keys %$param ) {
116           # filter out commented entries
117           my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}};
118
119           push (@{$comps_for{$comp}}, @modules);
120         }
121       }
122       else {
123         # filter out commented entries
124         push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
125       }
126     }
127   } else {
128     eval "require Module::Find;";
129     $class->throw("No arguments to load_classes and couldn't load".
130       " Module::Find ($@)") if $@;
131     my @comp = map { substr $_, length "${class}::"  } Module::Find::findallmod($class);
132     $comps_for{$class} = \@comp;
133   }
134
135   foreach my $prefix (keys %comps_for) {
136     foreach my $comp (@{$comps_for{$prefix}||[]}) {
137       my $comp_class = "${prefix}::${comp}";
138       print "$comp_class\n";
139       eval "use $comp_class"; # If it fails, assume the user fixed it
140       $class->register_class($comp => $comp_class);
141     }
142   }
143 }
144
145 =item compose_connection <target> <@db_info>
146
147 This is the most important method in this class. it takes a target namespace,
148 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
149 well as subclasses for each of your database classes in this namespace, using
150 this connection.
151
152 It will also setup a ->table method on the target class, which lets you
153 resolve database classes based on the schema component name, for example
154
155   MyApp::DB->table('Foo') # returns MyApp::DB::Foo, 
156                           # which ISA MyApp::Schema::Foo
157
158 This is the recommended API for accessing Schema generated classes, and 
159 using it might give you instant advantages with future versions of DBIC.
160
161 =cut
162
163 sub compose_connection {
164   my ($class, $target, @info) = @_;
165   my $conn_class = "${target}::_db";
166   $class->setup_connection_class($conn_class, @info);
167   my %reg = %{ $class->class_registrations };
168   my %target;
169   my %map;
170   while (my ($comp, $comp_class) = each %reg) {
171     my $target_class = "${target}::${comp}";
172     $class->inject_base($target_class, $comp_class, $conn_class);
173     $target_class->table($comp_class->table);
174     @map{$comp, $comp_class} = ($target_class, $target_class);
175   }
176   {
177     no strict 'refs';
178     *{"${target}::class"} =
179       sub {
180         my ($class, $to_map) = @_;
181         return $map{$to_map};
182       };
183     *{"${target}::classes"} = sub { return \%map; };
184   }
185   $conn_class->class_resolver($target);
186 }
187
188 =item setup_connection_class <$target> <@info>
189
190 Sets up a database connection class to inject between the schema
191 and the subclasses the schema creates.
192
193 =cut
194
195 sub setup_connection_class {
196   my ($class, $target, @info) = @_;
197   $class->inject_base($target => 'DBIx::Class::DB');
198   #$target->load_components('DB');
199   $target->connection(@info);
200 }
201
202 1;
203
204 =back
205
206 =head1 AUTHORS
207
208 Matt S. Trout <mst@shadowcatsystems.co.uk>
209
210 =head1 LICENSE
211
212 You may distribute this code under the same terms as Perl itself.
213
214 =cut
215