Commit | Line | Data |
a02675cd |
1 | package DBIx::Class::Schema; |
2 | |
3 | use strict; |
4 | use warnings; |
aa562407 |
5 | |
701da8c4 |
6 | use Carp::Clan qw/^DBIx::Class/; |
a02675cd |
7 | |
41a6f8c0 |
8 | use base qw/DBIx::Class/; |
a02675cd |
9 | |
0dc79249 |
10 | __PACKAGE__->mk_classdata('class_mappings' => {}); |
11 | __PACKAGE__->mk_classdata('source_registrations' => {}); |
1e10a11d |
12 | __PACKAGE__->mk_classdata('storage_type' => '::DBI'); |
d7156e50 |
13 | __PACKAGE__->mk_classdata('storage'); |
a02675cd |
14 | |
c2da098a |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Schema - composable schemas |
18 | |
19 | =head1 SYNOPSIS |
20 | |
c2da098a |
21 | package My::Schema; |
c2da098a |
22 | use base qw/DBIx::Class::Schema/; |
a3d93194 |
23 | |
24 | # load My::Schema::Foo, My::Schema::Bar, My::Schema::Baz |
c2da098a |
25 | __PACKAGE__->load_classes(qw/Foo Bar Baz/); |
26 | |
c2da098a |
27 | package My::Schema::Foo; |
03312470 |
28 | use base qw/DBIx::Class/; |
54540863 |
29 | __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example |
c2da098a |
30 | __PACKAGE__->table('foo'); |
c2da098a |
31 | |
5d9076f2 |
32 | # Elsewhere in your code: |
a3d93194 |
33 | my $schema1 = My::Schema->connect( |
34 | $dsn, |
35 | $user, |
36 | $password, |
37 | $attrs |
38 | ); |
c2da098a |
39 | |
a3d93194 |
40 | my $schema2 = My::Schema->connect( ... ); |
c2da098a |
41 | |
a3d93194 |
42 | # fetch objects using My::Schema::Foo |
43 | my $resultset = $schema1->resultset('Foo')->search( ... ); |
44 | my @objects = $schema2->resultset('Foo')->search( ... ); |
c2da098a |
45 | |
46 | =head1 DESCRIPTION |
47 | |
a3d93194 |
48 | Creates database classes based on a schema. This is the recommended way to |
49 | use L<DBIx::Class> and allows you to use more than one concurrent connection |
50 | with your classes. |
429bd4f1 |
51 | |
03312470 |
52 | NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS> |
53 | carefully as DBIx::Class does things a little differently. Note in |
54 | particular which module inherits off which. |
55 | |
c2da098a |
56 | =head1 METHODS |
57 | |
87c4e602 |
58 | =head2 register_class |
59 | |
60 | =head3 Arguments: <moniker> <component_class> |
076652e8 |
61 | |
80c90f5d |
62 | Registers a class which isa ResultSourceProxy; equivalent to calling |
66d9ef6b |
63 | |
181a28f4 |
64 | $schema->register_source($moniker, $component_class->result_source_instance); |
076652e8 |
65 | |
c2da098a |
66 | =cut |
67 | |
a02675cd |
68 | sub register_class { |
0dc79249 |
69 | my ($self, $moniker, $to_register) = @_; |
70 | $self->register_source($moniker => $to_register->result_source_instance); |
74b92d9a |
71 | } |
72 | |
87c4e602 |
73 | =head2 register_source |
74 | |
75 | =head3 Arguments: <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}; |
701da8c4 |
122 | $self->throw_exception("Can't find source for ${moniker}") |
0dc79249 |
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 | |
87c4e602 |
150 | =head2 load_classes |
151 | |
152 | =head3 Arguments: [<classes>, (<class>, <class>), {<namespace> => [<classes>]}] |
076652e8 |
153 | |
429bd4f1 |
154 | Uses L<Module::Find> to find all classes under the database class' namespace, |
155 | or uses the classes you select. Then it loads the component (using L<use>), |
156 | and registers them (using B<register_class>); |
076652e8 |
157 | |
5ce32fc1 |
158 | It is possible to comment out classes with a leading '#', but note that perl |
159 | will think it's a mistake (trying to use a comment in a qw list) so you'll |
160 | need to add "no warnings 'qw';" before your load_classes call. |
161 | |
076652e8 |
162 | =cut |
163 | |
a02675cd |
164 | sub load_classes { |
5ce32fc1 |
165 | my ($class, @params) = @_; |
166 | |
167 | my %comps_for; |
168 | |
169 | if (@params) { |
170 | foreach my $param (@params) { |
171 | if (ref $param eq 'ARRAY') { |
172 | # filter out commented entries |
173 | my @modules = grep { $_ !~ /^#/ } @$param; |
174 | |
175 | push (@{$comps_for{$class}}, @modules); |
176 | } |
177 | elsif (ref $param eq 'HASH') { |
178 | # more than one namespace possible |
179 | for my $comp ( keys %$param ) { |
180 | # filter out commented entries |
181 | my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}}; |
182 | |
183 | push (@{$comps_for{$comp}}, @modules); |
184 | } |
185 | } |
186 | else { |
187 | # filter out commented entries |
188 | push (@{$comps_for{$class}}, $param) if $param !~ /^#/; |
189 | } |
190 | } |
191 | } else { |
41a6f8c0 |
192 | eval "require Module::Find;"; |
701da8c4 |
193 | $class->throw_exception("No arguments to load_classes and couldn't load". |
41a6f8c0 |
194 | " Module::Find ($@)") if $@; |
5ce32fc1 |
195 | my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class); |
196 | $comps_for{$class} = \@comp; |
41a6f8c0 |
197 | } |
5ce32fc1 |
198 | |
199 | foreach my $prefix (keys %comps_for) { |
200 | foreach my $comp (@{$comps_for{$prefix}||[]}) { |
201 | my $comp_class = "${prefix}::${comp}"; |
5ce32fc1 |
202 | eval "use $comp_class"; # If it fails, assume the user fixed it |
bfb2bd4f |
203 | if ($@) { |
204 | die $@ unless $@ =~ /Can't locate/; |
205 | } |
5ce32fc1 |
206 | $class->register_class($comp => $comp_class); |
be381829 |
207 | # if $class->can('result_source_instance'); |
5ce32fc1 |
208 | } |
a02675cd |
209 | } |
210 | } |
211 | |
87c4e602 |
212 | =head2 compose_connection |
213 | |
214 | =head3 Arguments: <target> <@db_info> |
429bd4f1 |
215 | |
216 | This is the most important method in this class. it takes a target namespace, |
217 | as well as dbh connection info, and creates a L<DBIx::Class::DB> class as |
218 | well as subclasses for each of your database classes in this namespace, using |
219 | this connection. |
076652e8 |
220 | |
54540863 |
221 | It will also setup a ->class method on the target class, which lets you |
8c130052 |
222 | resolve database classes based on the schema component name, for example |
223 | |
54540863 |
224 | MyApp::DB->class('Foo') # returns MyApp::DB::Foo, |
8c130052 |
225 | # which ISA MyApp::Schema::Foo |
226 | |
227 | This is the recommended API for accessing Schema generated classes, and |
228 | using it might give you instant advantages with future versions of DBIC. |
229 | |
54540863 |
230 | WARNING: Loading components into Schema classes after compose_connection |
231 | may not cause them to be seen by the classes in your target namespace due |
232 | to the dispatch table approach used by Class::C3. If you do this you may find |
233 | you need to call Class::C3->reinitialize() afterwards to get the behaviour |
234 | you expect. |
235 | |
076652e8 |
236 | =cut |
237 | |
a02675cd |
238 | sub compose_connection { |
ea20d0fd |
239 | my ($self, $target, @info) = @_; |
80c90f5d |
240 | my $base = 'DBIx::Class::ResultSetProxy'; |
8ef144ff |
241 | eval "require ${base};"; |
242 | $self->throw_exception("No arguments to load_classes and couldn't load". |
243 | " ${base} ($@)") if $@; |
be381829 |
244 | |
245 | if ($self eq $target) { |
246 | # Pathological case, largely caused by the docs on early C::M::DBIC::Plain |
247 | foreach my $moniker ($self->sources) { |
248 | my $source = $self->source($moniker); |
249 | my $class = $source->result_class; |
250 | $self->inject_base($class, $base); |
251 | $class->mk_classdata(resultset_instance => $source->resultset); |
252 | $class->mk_classdata(class_resolver => $self); |
253 | } |
50041f3c |
254 | $self->connection(@info); |
be381829 |
255 | return $self; |
256 | } |
257 | |
66d9ef6b |
258 | my $schema = $self->compose_namespace($target, $base); |
ecceadff |
259 | { |
260 | no strict 'refs'; |
261 | *{"${target}::schema"} = sub { $schema }; |
262 | } |
263 | |
66d9ef6b |
264 | $schema->connection(@info); |
0dc79249 |
265 | foreach my $moniker ($schema->sources) { |
266 | my $source = $schema->source($moniker); |
267 | my $class = $source->result_class; |
268 | #warn "$moniker $class $source ".$source->storage; |
8c49f629 |
269 | $class->mk_classdata(result_source_instance => $source); |
ea20d0fd |
270 | $class->mk_classdata(resultset_instance => $source->resultset); |
66d9ef6b |
271 | $class->mk_classdata(class_resolver => $schema); |
bfb2bd4f |
272 | } |
273 | return $schema; |
e678398e |
274 | } |
275 | |
276 | sub compose_namespace { |
66d9ef6b |
277 | my ($self, $target, $base) = @_; |
278 | my %reg = %{ $self->source_registrations }; |
11b78bd6 |
279 | my %target; |
280 | my %map; |
66d9ef6b |
281 | my $schema = $self->clone; |
282 | foreach my $moniker ($schema->sources) { |
283 | my $source = $schema->source($moniker); |
0dc79249 |
284 | my $target_class = "${target}::${moniker}"; |
66d9ef6b |
285 | $self->inject_base( |
0dc79249 |
286 | $target_class => $source->result_class, ($base ? $base : ()) |
287 | ); |
66d9ef6b |
288 | $source->result_class($target_class); |
b7951443 |
289 | } |
11b78bd6 |
290 | { |
291 | no strict 'refs'; |
1edaf6fe |
292 | foreach my $meth (qw/class source resultset/) { |
293 | *{"${target}::${meth}"} = |
294 | sub { shift->schema->$meth(@_) }; |
295 | } |
11b78bd6 |
296 | } |
bfb2bd4f |
297 | return $schema; |
b7951443 |
298 | } |
299 | |
87c4e602 |
300 | =head2 setup_connection_class |
301 | |
302 | =head3 Arguments: <$target> <@info> |
076652e8 |
303 | |
429bd4f1 |
304 | Sets up a database connection class to inject between the schema |
305 | and the subclasses the schema creates. |
306 | |
076652e8 |
307 | =cut |
308 | |
b7951443 |
309 | sub setup_connection_class { |
310 | my ($class, $target, @info) = @_; |
63e9583a |
311 | $class->inject_base($target => 'DBIx::Class::DB'); |
312 | #$target->load_components('DB'); |
b7951443 |
313 | $target->connection(@info); |
314 | } |
315 | |
87c4e602 |
316 | =head2 connection |
317 | |
318 | =head3 Arguments: (@args) |
66d9ef6b |
319 | |
320 | Instantiates a new Storage object of type storage_type and passes the |
19feb86b |
321 | arguments to $storage->connect_info. Sets the connection in-place on |
66d9ef6b |
322 | the schema. |
323 | |
324 | =cut |
325 | |
326 | sub connection { |
327 | my ($self, @info) = @_; |
1e10a11d |
328 | my $storage_class = $self->storage_type; |
329 | $storage_class = 'DBIx::Class::Storage'.$storage_class |
330 | if $storage_class =~ m/^::/; |
8ef144ff |
331 | eval "require ${storage_class};"; |
332 | $self->throw_exception("No arguments to load_classes and couldn't load". |
333 | " ${storage_class} ($@)") if $@; |
66d9ef6b |
334 | my $storage = $storage_class->new; |
335 | $storage->connect_info(\@info); |
336 | $self->storage($storage); |
337 | return $self; |
338 | } |
339 | |
87c4e602 |
340 | =head2 connect |
341 | |
342 | =head3 Arguments: (@info) |
66d9ef6b |
343 | |
344 | Conveneience method, equivalent to $schema->clone->connection(@info) |
345 | |
346 | =cut |
347 | |
08b515f1 |
348 | sub connect { shift->clone->connection(@_) } |
349 | |
350 | =head2 txn_begin |
351 | |
352 | Begins a transaction (does nothing if AutoCommit is off). |
353 | |
354 | =cut |
355 | |
356 | sub txn_begin { shift->storage->txn_begin } |
357 | |
358 | =head2 txn_commit |
359 | |
360 | Commits the current transaction. |
361 | |
362 | =cut |
363 | |
364 | sub txn_commit { shift->storage->txn_commit } |
365 | |
366 | =head2 txn_rollback |
367 | |
368 | Rolls back the current transaction. |
369 | |
370 | =cut |
371 | |
372 | sub txn_rollback { shift->storage->txn_rollback } |
66d9ef6b |
373 | |
a62cf8d4 |
374 | =head2 txn_do |
375 | |
181a28f4 |
376 | =head3 Arguments: <$coderef>, [@coderef_args] |
a62cf8d4 |
377 | |
181a28f4 |
378 | Executes C<$coderef> with (optional) arguments C<@coderef_args> |
379 | transactionally, returning its result (if any). If an exception is |
380 | caught, a rollback is issued and the exception is rethrown. If the |
381 | rollback fails, (i.e. throws an exception) an exception is thrown that |
382 | includes a "Rollback failed" message. |
a62cf8d4 |
383 | |
384 | For example, |
385 | |
386 | my $foo = $schema->resultset('foo')->find(1); |
387 | |
388 | my $coderef = sub { |
389 | my ($foo, @bars) = @_; |
390 | |
391 | # If any one of these fails, the entire transaction fails |
392 | $foo->create_related('bars', { |
393 | col => $_ |
394 | }) foreach (@bars); |
395 | |
396 | return $foo->bars; |
397 | }; |
398 | |
399 | my $rs; |
400 | eval { |
401 | $rs = $schema->txn_do($coderef, $foo, qw/foo bar baz/); |
402 | }; |
403 | |
404 | if ($@) { |
405 | my $error = $@; |
406 | if ($error =~ /Rollback failed/) { |
407 | die "something terrible has happened!"; |
408 | } else { |
409 | deal_with_failed_transaction(); |
410 | die $error; |
411 | } |
412 | } |
413 | |
181a28f4 |
414 | Nested transactions work as expected (i.e. only the outermost |
a62cf8d4 |
415 | transaction will issue a txn_commit on the Schema's storage) |
416 | |
417 | =cut |
418 | |
419 | sub txn_do { |
420 | my ($self, $coderef, @args) = @_; |
421 | |
171dadd7 |
422 | ref $self or $self->throw_exception |
423 | ('Cannot execute txn_do as a class method'); |
424 | ref $coderef eq 'CODE' or $self->throw_exception |
425 | ('$coderef must be a CODE reference'); |
a62cf8d4 |
426 | |
427 | my (@return_values, $return_value); |
428 | |
429 | $self->txn_begin; # If this throws an exception, no rollback is needed |
430 | |
431 | my $wantarray = wantarray; # Need to save this since it's reset in eval{} |
432 | |
433 | eval { |
434 | # Need to differentiate between scalar/list context to allow for returning |
435 | # a list in scalar context to get the size of the list |
436 | if ($wantarray) { |
437 | @return_values = $coderef->(@args); |
438 | } else { |
439 | $return_value = $coderef->(@args); |
440 | } |
441 | $self->txn_commit; |
442 | }; |
443 | |
444 | if ($@) { |
445 | my $error = $@; |
446 | |
447 | eval { |
448 | $self->txn_rollback; |
449 | }; |
450 | |
451 | if ($@) { |
452 | my $rollback_error = $@; |
453 | my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION"; |
454 | $self->throw_exception($error) # propagate nested rollback |
455 | if $rollback_error =~ /$exception_class/; |
456 | |
457 | $self->throw_exception("Transaction aborted: $error. Rollback failed: ". |
458 | $rollback_error); |
459 | } else { |
460 | $self->throw_exception($error); # txn failed but rollback succeeded |
461 | } |
462 | } |
463 | |
464 | return $wantarray ? @return_values : $return_value; |
465 | } |
466 | |
66d9ef6b |
467 | =head2 clone |
468 | |
469 | Clones the schema and its associated result_source objects and returns the |
470 | copy. |
471 | |
472 | =cut |
473 | |
474 | sub clone { |
475 | my ($self) = @_; |
476 | my $clone = bless({ (ref $self ? %$self : ()) }, ref $self || $self); |
477 | foreach my $moniker ($self->sources) { |
478 | my $source = $self->source($moniker); |
479 | my $new = $source->new($source); |
480 | $clone->register_source($moniker => $new); |
481 | } |
482 | return $clone; |
483 | } |
484 | |
87c4e602 |
485 | =head2 populate |
486 | |
487 | =head3 Arguments: ($moniker, \@data); |
a37a4697 |
488 | |
489 | Populates the source registered with the given moniker with the supplied data. |
490 | @data should be a list of listrefs, the first containing column names, the |
491 | second matching values - i.e. |
492 | |
a62cf8d4 |
493 | $schema->populate('Foo', [ |
494 | [ qw/foo_id foo_string/ ], |
495 | [ 1, 'One' ], |
496 | [ 2, 'Two' ], |
497 | ... |
498 | ]); |
a37a4697 |
499 | |
500 | =cut |
501 | |
502 | sub populate { |
503 | my ($self, $name, $data) = @_; |
504 | my $rs = $self->resultset($name); |
505 | my @names = @{shift(@$data)}; |
506 | foreach my $item (@$data) { |
507 | my %create; |
508 | @create{@names} = @$item; |
509 | $rs->create(\%create); |
510 | } |
511 | } |
512 | |
5160b401 |
513 | =head2 throw_exception |
701da8c4 |
514 | |
515 | Defaults to using Carp::Clan to report errors from user perspective. |
516 | |
517 | =cut |
518 | |
519 | sub throw_exception { |
520 | my ($self) = shift; |
521 | croak @_; |
522 | } |
523 | |
a02675cd |
524 | 1; |
c2da098a |
525 | |
c2da098a |
526 | =head1 AUTHORS |
527 | |
daec44b8 |
528 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
c2da098a |
529 | |
530 | =head1 LICENSE |
531 | |
532 | You may distribute this code under the same terms as Perl itself. |
533 | |
534 | =cut |
535 | |