Commit | Line | Data |
a02675cd |
1 | package DBIx::Class::Schema; |
2 | |
3 | use strict; |
4 | use warnings; |
63e9583a |
5 | use DBIx::Class::DB; |
a02675cd |
6 | |
41a6f8c0 |
7 | use base qw/DBIx::Class/; |
a02675cd |
8 | |
42a1aaa1 |
9 | __PACKAGE__->load_components(qw/Exception/); |
0dc79249 |
10 | __PACKAGE__->mk_classdata('class_mappings' => {}); |
11 | __PACKAGE__->mk_classdata('source_registrations' => {}); |
d7156e50 |
12 | __PACKAGE__->mk_classdata('storage_type' => 'DBI'); |
13 | __PACKAGE__->mk_classdata('storage'); |
a02675cd |
14 | |
c2da098a |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Schema - composable schemas |
18 | |
19 | =head1 SYNOPSIS |
20 | |
03312470 |
21 | in My/Schema.pm |
c2da098a |
22 | |
23 | package My::Schema; |
24 | |
25 | use base qw/DBIx::Class::Schema/; |
26 | |
27 | __PACKAGE__->load_classes(qw/Foo Bar Baz/); |
28 | |
03312470 |
29 | in My/Schema/Foo.pm |
c2da098a |
30 | |
31 | package My::Schema::Foo; |
32 | |
03312470 |
33 | use base qw/DBIx::Class/; |
c2da098a |
34 | |
54540863 |
35 | __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example |
c2da098a |
36 | __PACKAGE__->table('foo'); |
37 | ... |
38 | |
03312470 |
39 | in My/DB.pm |
c2da098a |
40 | |
41 | use My::Schema; |
42 | |
43 | My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs); |
44 | |
03312470 |
45 | then in app code |
c2da098a |
46 | |
656796f2 |
47 | my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB |
c2da098a |
48 | |
49 | =head1 DESCRIPTION |
50 | |
429bd4f1 |
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. |
55 | |
03312470 |
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. |
59 | |
c2da098a |
60 | =head1 METHODS |
61 | |
0dc79249 |
62 | =head2 register_class <moniker> <component_class> |
076652e8 |
63 | |
64 | Registers the class in the schema's class_registrations. This is a hash |
429bd4f1 |
65 | containing database classes, keyed by their monikers. It's used by |
076652e8 |
66 | compose_connection to create/modify all the existing database classes. |
67 | |
c2da098a |
68 | =cut |
69 | |
a02675cd |
70 | sub register_class { |
0dc79249 |
71 | my ($self, $moniker, $to_register) = @_; |
72 | $self->register_source($moniker => $to_register->result_source_instance); |
74b92d9a |
73 | } |
74 | |
0dc79249 |
75 | =head2 register_source <moniker> <result source> |
076652e8 |
76 | |
0dc79249 |
77 | Registers the result source in the schema with the given moniker |
076652e8 |
78 | |
79 | =cut |
80 | |
0dc79249 |
81 | sub register_source { |
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); |
91 | } |
92 | } |
a02675cd |
93 | |
bfb2bd4f |
94 | =head2 class |
95 | |
96 | my $class = $schema->class('Foo'); |
97 | |
0dc79249 |
98 | Retrieves the result class name for a given result source |
bfb2bd4f |
99 | |
100 | =cut |
101 | |
102 | sub class { |
0dc79249 |
103 | my ($self, $moniker) = @_; |
104 | return $self->source($moniker)->result_class; |
bfb2bd4f |
105 | } |
106 | |
ea20d0fd |
107 | =head2 source |
108 | |
109 | my $source = $schema->source('Foo'); |
110 | |
111 | Returns the result source object for the registered name |
112 | |
113 | =cut |
114 | |
115 | sub source { |
0dc79249 |
116 | my ($self, $moniker) = @_; |
117 | my $sreg = $self->source_registrations; |
118 | return $sreg->{$moniker} if exists $sreg->{$moniker}; |
119 | |
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}; |
ea20d0fd |
125 | } |
126 | |
0dc79249 |
127 | =head2 sources |
128 | |
129 | my @source_monikers = $schema->sources; |
130 | |
131 | Returns the source monikers of all source registrations on this schema |
132 | |
133 | =cut |
134 | |
135 | sub sources { return keys %{shift->source_registrations}; } |
136 | |
ea20d0fd |
137 | =head2 resultset |
138 | |
139 | my $rs = $schema->resultset('Foo'); |
140 | |
0dc79249 |
141 | Returns the resultset for the registered moniker |
ea20d0fd |
142 | |
143 | =cut |
144 | |
145 | sub resultset { |
0dc79249 |
146 | my ($self, $moniker) = @_; |
147 | return $self->source($moniker)->resultset; |
ea20d0fd |
148 | } |
149 | |
130c6439 |
150 | =head2 load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}] |
076652e8 |
151 | |
429bd4f1 |
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>); |
076652e8 |
155 | |
5ce32fc1 |
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. |
159 | |
076652e8 |
160 | =cut |
161 | |
a02675cd |
162 | sub load_classes { |
5ce32fc1 |
163 | my ($class, @params) = @_; |
164 | |
165 | my %comps_for; |
166 | |
167 | if (@params) { |
168 | foreach my $param (@params) { |
169 | if (ref $param eq 'ARRAY') { |
170 | # filter out commented entries |
171 | my @modules = grep { $_ !~ /^#/ } @$param; |
172 | |
173 | push (@{$comps_for{$class}}, @modules); |
174 | } |
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}}; |
180 | |
181 | push (@{$comps_for{$comp}}, @modules); |
182 | } |
183 | } |
184 | else { |
185 | # filter out commented entries |
186 | push (@{$comps_for{$class}}, $param) if $param !~ /^#/; |
187 | } |
188 | } |
189 | } else { |
41a6f8c0 |
190 | eval "require Module::Find;"; |
191 | $class->throw("No arguments to load_classes and couldn't load". |
192 | " Module::Find ($@)") if $@; |
5ce32fc1 |
193 | my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class); |
194 | $comps_for{$class} = \@comp; |
41a6f8c0 |
195 | } |
5ce32fc1 |
196 | |
197 | foreach my $prefix (keys %comps_for) { |
198 | foreach my $comp (@{$comps_for{$prefix}||[]}) { |
199 | my $comp_class = "${prefix}::${comp}"; |
5ce32fc1 |
200 | eval "use $comp_class"; # If it fails, assume the user fixed it |
bfb2bd4f |
201 | if ($@) { |
202 | die $@ unless $@ =~ /Can't locate/; |
203 | } |
5ce32fc1 |
204 | $class->register_class($comp => $comp_class); |
205 | } |
a02675cd |
206 | } |
207 | } |
208 | |
130c6439 |
209 | =head2 compose_connection <target> <@db_info> |
429bd4f1 |
210 | |
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 |
214 | this connection. |
076652e8 |
215 | |
54540863 |
216 | It will also setup a ->class method on the target class, which lets you |
8c130052 |
217 | resolve database classes based on the schema component name, for example |
218 | |
54540863 |
219 | MyApp::DB->class('Foo') # returns MyApp::DB::Foo, |
8c130052 |
220 | # which ISA MyApp::Schema::Foo |
221 | |
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. |
224 | |
54540863 |
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 |
229 | you expect. |
230 | |
076652e8 |
231 | =cut |
232 | |
a02675cd |
233 | sub compose_connection { |
ea20d0fd |
234 | my ($self, $target, @info) = @_; |
11b78bd6 |
235 | my $conn_class = "${target}::_db"; |
ea20d0fd |
236 | $self->setup_connection_class($conn_class, @info); |
237 | my $schema = $self->compose_namespace($target, $conn_class); |
bfb2bd4f |
238 | $schema->storage($conn_class->storage); |
0dc79249 |
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; |
8c49f629 |
243 | $class->mk_classdata(result_source_instance => $source); |
ea20d0fd |
244 | $class->mk_classdata(resultset_instance => $source->resultset); |
bfb2bd4f |
245 | } |
246 | return $schema; |
e678398e |
247 | } |
248 | |
249 | sub compose_namespace { |
250 | my ($class, $target, $base) = @_; |
0dc79249 |
251 | my %reg = %{ $class->source_registrations }; |
11b78bd6 |
252 | my %target; |
253 | my %map; |
bfb2bd4f |
254 | my $schema = bless({ }, $class); |
0dc79249 |
255 | while (my ($moniker, $source) = each %reg) { |
256 | my $target_class = "${target}::${moniker}"; |
257 | $class->inject_base( |
258 | $target_class => $source->result_class, ($base ? $base : ()) |
259 | ); |
260 | my $new_source = $source->new($source); |
261 | $new_source->result_class($target_class); |
262 | $new_source->schema($schema); |
263 | $map{$moniker} = $new_source; |
b7951443 |
264 | } |
0dc79249 |
265 | $schema->source_registrations(\%map); |
11b78bd6 |
266 | { |
267 | no strict 'refs'; |
bfb2bd4f |
268 | *{"${target}::schema"} = |
269 | sub { $schema }; |
1edaf6fe |
270 | foreach my $meth (qw/class source resultset/) { |
271 | *{"${target}::${meth}"} = |
272 | sub { shift->schema->$meth(@_) }; |
273 | } |
11b78bd6 |
274 | } |
e678398e |
275 | $base->class_resolver($target); |
bfb2bd4f |
276 | return $schema; |
b7951443 |
277 | } |
278 | |
130c6439 |
279 | =head2 setup_connection_class <$target> <@info> |
076652e8 |
280 | |
429bd4f1 |
281 | Sets up a database connection class to inject between the schema |
282 | and the subclasses the schema creates. |
283 | |
076652e8 |
284 | =cut |
285 | |
b7951443 |
286 | sub setup_connection_class { |
287 | my ($class, $target, @info) = @_; |
63e9583a |
288 | $class->inject_base($target => 'DBIx::Class::DB'); |
289 | #$target->load_components('DB'); |
b7951443 |
290 | $target->connection(@info); |
291 | } |
292 | |
a02675cd |
293 | 1; |
c2da098a |
294 | |
c2da098a |
295 | =head1 AUTHORS |
296 | |
daec44b8 |
297 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
c2da098a |
298 | |
299 | =head1 LICENSE |
300 | |
301 | You may distribute this code under the same terms as Perl itself. |
302 | |
303 | =cut |
304 | |