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/; |
a917fb06 |
7 | use Scalar::Util qw/weaken/; |
7cb86b38 |
8 | require Module::Find; |
a02675cd |
9 | |
41a6f8c0 |
10 | use base qw/DBIx::Class/; |
a02675cd |
11 | |
0dc79249 |
12 | __PACKAGE__->mk_classdata('class_mappings' => {}); |
13 | __PACKAGE__->mk_classdata('source_registrations' => {}); |
1e10a11d |
14 | __PACKAGE__->mk_classdata('storage_type' => '::DBI'); |
d7156e50 |
15 | __PACKAGE__->mk_classdata('storage'); |
82cc0386 |
16 | __PACKAGE__->mk_classdata('exception_action'); |
a02675cd |
17 | |
c2da098a |
18 | =head1 NAME |
19 | |
20 | DBIx::Class::Schema - composable schemas |
21 | |
22 | =head1 SYNOPSIS |
23 | |
24d67825 |
24 | package Library::Schema; |
c2da098a |
25 | use base qw/DBIx::Class::Schema/; |
bab77431 |
26 | |
24d67825 |
27 | # load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD |
28 | __PACKAGE__->load_classes(qw/CD Book DVD/); |
c2da098a |
29 | |
24d67825 |
30 | package Library::Schema::CD; |
03312470 |
31 | use base qw/DBIx::Class/; |
77254782 |
32 | __PACKAGE__->load_components(qw/PK::Auto Core/); # for example |
24d67825 |
33 | __PACKAGE__->table('cd'); |
c2da098a |
34 | |
5d9076f2 |
35 | # Elsewhere in your code: |
24d67825 |
36 | my $schema1 = Library::Schema->connect( |
a3d93194 |
37 | $dsn, |
38 | $user, |
39 | $password, |
24d67825 |
40 | { AutoCommit => 0 }, |
a3d93194 |
41 | ); |
bab77431 |
42 | |
24d67825 |
43 | my $schema2 = Library::Schema->connect($coderef_returning_dbh); |
c2da098a |
44 | |
24d67825 |
45 | # fetch objects using Library::Schema::DVD |
46 | my $resultset = $schema1->resultset('DVD')->search( ... ); |
47 | my @dvd_objects = $schema2->resultset('DVD')->search( ... ); |
c2da098a |
48 | |
49 | =head1 DESCRIPTION |
50 | |
a3d93194 |
51 | Creates database classes based on a schema. This is the recommended way to |
52 | use L<DBIx::Class> and allows you to use more than one concurrent connection |
53 | with your classes. |
429bd4f1 |
54 | |
03312470 |
55 | NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS> |
2053ab2a |
56 | carefully, as DBIx::Class does things a little differently. Note in |
03312470 |
57 | particular which module inherits off which. |
58 | |
c2da098a |
59 | =head1 METHODS |
60 | |
87c4e602 |
61 | =head2 register_class |
62 | |
27f01d1f |
63 | =over 4 |
64 | |
ebc77b53 |
65 | =item Arguments: $moniker, $component_class |
27f01d1f |
66 | |
67 | =back |
076652e8 |
68 | |
71f9df37 |
69 | Registers a class which isa DBIx::Class::ResultSourceProxy. Equivalent to |
2053ab2a |
70 | calling: |
66d9ef6b |
71 | |
181a28f4 |
72 | $schema->register_source($moniker, $component_class->result_source_instance); |
076652e8 |
73 | |
c2da098a |
74 | =cut |
75 | |
a02675cd |
76 | sub register_class { |
0dc79249 |
77 | my ($self, $moniker, $to_register) = @_; |
78 | $self->register_source($moniker => $to_register->result_source_instance); |
74b92d9a |
79 | } |
80 | |
87c4e602 |
81 | =head2 register_source |
82 | |
27f01d1f |
83 | =over 4 |
84 | |
ebc77b53 |
85 | =item Arguments: $moniker, $result_source |
27f01d1f |
86 | |
87 | =back |
076652e8 |
88 | |
82b01c38 |
89 | Registers the L<DBIx::Class::ResultSource> in the schema with the given |
90 | moniker. |
076652e8 |
91 | |
92 | =cut |
93 | |
0dc79249 |
94 | sub register_source { |
95 | my ($self, $moniker, $source) = @_; |
96 | my %reg = %{$self->source_registrations}; |
97 | $reg{$moniker} = $source; |
98 | $self->source_registrations(\%reg); |
99 | $source->schema($self); |
a917fb06 |
100 | weaken($source->{schema}) if ref($self); |
0dc79249 |
101 | if ($source->result_class) { |
102 | my %map = %{$self->class_mappings}; |
103 | $map{$source->result_class} = $moniker; |
104 | $self->class_mappings(\%map); |
105 | } |
75d07914 |
106 | } |
a02675cd |
107 | |
bfb2bd4f |
108 | =head2 class |
109 | |
27f01d1f |
110 | =over 4 |
82b01c38 |
111 | |
ebc77b53 |
112 | =item Arguments: $moniker |
27f01d1f |
113 | |
d601dc88 |
114 | =item Return Value: $classname |
27f01d1f |
115 | |
116 | =back |
82b01c38 |
117 | |
2053ab2a |
118 | Retrieves the result class name for the given moniker. For example: |
82b01c38 |
119 | |
120 | my $class = $schema->class('CD'); |
bfb2bd4f |
121 | |
122 | =cut |
123 | |
124 | sub class { |
0dc79249 |
125 | my ($self, $moniker) = @_; |
126 | return $self->source($moniker)->result_class; |
bfb2bd4f |
127 | } |
128 | |
ea20d0fd |
129 | =head2 source |
130 | |
27f01d1f |
131 | =over 4 |
132 | |
ebc77b53 |
133 | =item Arguments: $moniker |
27f01d1f |
134 | |
d601dc88 |
135 | =item Return Value: $result_source |
82b01c38 |
136 | |
27f01d1f |
137 | =back |
82b01c38 |
138 | |
24d67825 |
139 | my $source = $schema->source('Book'); |
ea20d0fd |
140 | |
82b01c38 |
141 | Returns the L<DBIx::Class::ResultSource> object for the registered moniker. |
ea20d0fd |
142 | |
143 | =cut |
144 | |
145 | sub source { |
0dc79249 |
146 | my ($self, $moniker) = @_; |
147 | my $sreg = $self->source_registrations; |
148 | return $sreg->{$moniker} if exists $sreg->{$moniker}; |
149 | |
150 | # if we got here, they probably passed a full class name |
151 | my $mapped = $self->class_mappings->{$moniker}; |
701da8c4 |
152 | $self->throw_exception("Can't find source for ${moniker}") |
0dc79249 |
153 | unless $mapped && exists $sreg->{$mapped}; |
154 | return $sreg->{$mapped}; |
ea20d0fd |
155 | } |
156 | |
0dc79249 |
157 | =head2 sources |
158 | |
27f01d1f |
159 | =over 4 |
160 | |
d601dc88 |
161 | =item Return Value: @source_monikers |
27f01d1f |
162 | |
163 | =back |
82b01c38 |
164 | |
165 | Returns the source monikers of all source registrations on this schema. |
2053ab2a |
166 | For example: |
82b01c38 |
167 | |
168 | my @source_monikers = $schema->sources; |
0dc79249 |
169 | |
170 | =cut |
171 | |
172 | sub sources { return keys %{shift->source_registrations}; } |
173 | |
ea20d0fd |
174 | =head2 resultset |
175 | |
27f01d1f |
176 | =over 4 |
177 | |
ebc77b53 |
178 | =item Arguments: $moniker |
27f01d1f |
179 | |
d601dc88 |
180 | =item Return Value: $result_set |
82b01c38 |
181 | |
27f01d1f |
182 | =back |
82b01c38 |
183 | |
24d67825 |
184 | my $rs = $schema->resultset('DVD'); |
ea20d0fd |
185 | |
82b01c38 |
186 | Returns the L<DBIx::Class::ResultSet> object for the registered moniker. |
ea20d0fd |
187 | |
188 | =cut |
189 | |
190 | sub resultset { |
0dc79249 |
191 | my ($self, $moniker) = @_; |
192 | return $self->source($moniker)->resultset; |
ea20d0fd |
193 | } |
194 | |
87c4e602 |
195 | =head2 load_classes |
196 | |
27f01d1f |
197 | =over 4 |
198 | |
199 | =item Arguments: @classes?, { $namespace => [ @classes ] }+ |
200 | |
201 | =back |
076652e8 |
202 | |
82b01c38 |
203 | With no arguments, this method uses L<Module::Find> to find all classes under |
204 | the schema's namespace. Otherwise, this method loads the classes you specify |
205 | (using L<use>), and registers them (using L</"register_class">). |
076652e8 |
206 | |
2053ab2a |
207 | It is possible to comment out classes with a leading C<#>, but note that perl |
208 | will think it's a mistake (trying to use a comment in a qw list), so you'll |
209 | need to add C<no warnings 'qw';> before your load_classes call. |
5ce32fc1 |
210 | |
2053ab2a |
211 | Example: |
82b01c38 |
212 | |
213 | My::Schema->load_classes(); # loads My::Schema::CD, My::Schema::Artist, |
75d07914 |
214 | # etc. (anything under the My::Schema namespace) |
82b01c38 |
215 | |
216 | # loads My::Schema::CD, My::Schema::Artist, Other::Namespace::Producer but |
217 | # not Other::Namespace::LinerNotes nor My::Schema::Track |
218 | My::Schema->load_classes(qw/ CD Artist #Track /, { |
219 | Other::Namespace => [qw/ Producer #LinerNotes /], |
220 | }); |
221 | |
076652e8 |
222 | =cut |
223 | |
a02675cd |
224 | sub load_classes { |
5ce32fc1 |
225 | my ($class, @params) = @_; |
bab77431 |
226 | |
5ce32fc1 |
227 | my %comps_for; |
bab77431 |
228 | |
5ce32fc1 |
229 | if (@params) { |
230 | foreach my $param (@params) { |
231 | if (ref $param eq 'ARRAY') { |
232 | # filter out commented entries |
233 | my @modules = grep { $_ !~ /^#/ } @$param; |
bab77431 |
234 | |
5ce32fc1 |
235 | push (@{$comps_for{$class}}, @modules); |
236 | } |
237 | elsif (ref $param eq 'HASH') { |
238 | # more than one namespace possible |
239 | for my $comp ( keys %$param ) { |
240 | # filter out commented entries |
241 | my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}}; |
242 | |
243 | push (@{$comps_for{$comp}}, @modules); |
244 | } |
245 | } |
246 | else { |
247 | # filter out commented entries |
248 | push (@{$comps_for{$class}}, $param) if $param !~ /^#/; |
249 | } |
250 | } |
251 | } else { |
bc0c9800 |
252 | my @comp = map { substr $_, length "${class}::" } |
253 | Module::Find::findallmod($class); |
5ce32fc1 |
254 | $comps_for{$class} = \@comp; |
41a6f8c0 |
255 | } |
5ce32fc1 |
256 | |
e6efde04 |
257 | my @to_register; |
258 | { |
259 | no warnings qw/redefine/; |
260 | local *Class::C3::reinitialize = sub { }; |
261 | foreach my $prefix (keys %comps_for) { |
262 | foreach my $comp (@{$comps_for{$prefix}||[]}) { |
263 | my $comp_class = "${prefix}::${comp}"; |
c037c03a |
264 | $class->ensure_class_loaded($comp_class); |
bab77431 |
265 | $comp_class->source_name($comp) unless $comp_class->source_name; |
266 | |
267 | push(@to_register, [ $comp_class->source_name, $comp_class ]); |
bfb2bd4f |
268 | } |
5ce32fc1 |
269 | } |
a02675cd |
270 | } |
e6efde04 |
271 | Class::C3->reinitialize; |
272 | |
273 | foreach my $to (@to_register) { |
274 | $class->register_class(@$to); |
275 | # if $class->can('result_source_instance'); |
276 | } |
a02675cd |
277 | } |
278 | |
2374c5ff |
279 | =head2 load_namespaces |
280 | |
281 | =over 4 |
282 | |
85bd0538 |
283 | =item Arguments: %options? |
2374c5ff |
284 | |
285 | =back |
286 | |
287 | This is an alternative to L</load_classes> above which assumes an alternative |
c87014e8 |
288 | layout for automatic class loading. It assumes that all result |
289 | classes are underneath a sub-namespace of the schema called C<Result>, any |
7a58f051 |
290 | corresponding ResultSet classes are underneath a sub-namespace of the schema |
46a05fd4 |
291 | called C<ResultSet>. |
2374c5ff |
292 | |
46a05fd4 |
293 | Both of the sub-namespaces are configurable if you don't like the defaults, |
c87014e8 |
294 | via the options C<result_namespace> and C<resultset_namespace>. |
85bd0538 |
295 | |
25fb14bd |
296 | If (and only if) you specify the option C<default_resultset_class>, any found |
c87014e8 |
297 | Result classes for which we do not find a corresponding |
25fb14bd |
298 | ResultSet class will have their C<resultset_class> set to |
299 | C<default_resultset_class>. |
0f4ec1d2 |
300 | |
46a05fd4 |
301 | C<load_namespaces> takes care of calling C<resultset_class> for you where |
302 | neccessary if you didn't do it for yourself. |
f017c022 |
303 | |
0f4ec1d2 |
304 | All of the namespace and classname options to this method are relative to |
305 | the schema classname by default. To specify a fully-qualified name, prefix |
306 | it with a literal C<+>. |
2374c5ff |
307 | |
f017c022 |
308 | Examples: |
2374c5ff |
309 | |
c87014e8 |
310 | # load My::Schema::Result::CD, My::Schema::Result::Artist, |
2374c5ff |
311 | # My::Schema::ResultSet::CD, etc... |
0f4ec1d2 |
312 | My::Schema->load_namespaces; |
313 | |
c87014e8 |
314 | # Override everything to use ugly names. |
315 | # In this example, if there is a My::Schema::Res::Foo, but no matching |
316 | # My::Schema::RSets::Foo, then Foo will have its |
317 | # resultset_class set to My::Schema::RSetBase |
0f4ec1d2 |
318 | My::Schema->load_namespaces( |
c87014e8 |
319 | result_namespace => 'Res', |
0f4ec1d2 |
320 | resultset_namespace => 'RSets', |
25fb14bd |
321 | default_resultset_class => 'RSetBase', |
0f4ec1d2 |
322 | ); |
2374c5ff |
323 | |
0f4ec1d2 |
324 | # Put things in other namespaces |
85bd0538 |
325 | My::Schema->load_namespaces( |
c87014e8 |
326 | result_namespace => '+Some::Place::Results', |
0f4ec1d2 |
327 | resultset_namespace => '+Another::Place::RSets', |
85bd0538 |
328 | ); |
0f4ec1d2 |
329 | |
f017c022 |
330 | If you'd like to use multiple namespaces of each type, simply use an arrayref |
c87014e8 |
331 | of namespaces for that option. In the case that the same result |
46a05fd4 |
332 | (or resultset) class exists in multiple namespaces, the latter entries in |
333 | your list of namespaces will override earlier ones. |
f017c022 |
334 | |
335 | My::Schema->load_namespaces( |
c87014e8 |
336 | # My::Schema::Results_C::Foo takes precedence over My::Schema::Results_B::Foo : |
337 | result_namespace => [ 'Results_A', 'Results_B', 'Results_C' ], |
f017c022 |
338 | resultset_namespace => [ '+Some::Place::RSets', 'RSets' ], |
339 | ); |
85bd0538 |
340 | |
2374c5ff |
341 | =cut |
342 | |
f017c022 |
343 | # Pre-pends our classname to the given relative classname or |
344 | # class namespace, unless there is a '+' prefix, which will |
7a58f051 |
345 | # be stripped. |
f017c022 |
346 | sub _expand_relative_name { |
7a58f051 |
347 | my ($class, $name) = @_; |
348 | return if !$name; |
349 | $name = $class . '::' . $name if ! ($name =~ s/^\+//); |
350 | return $name; |
f017c022 |
351 | } |
352 | |
353 | # returns a hash of $shortname => $fullname for every package |
354 | # found in the given namespaces ($shortname is with the $fullname's |
355 | # namespace stripped off) |
356 | sub _map_namespaces { |
357 | my ($class, @namespaces) = @_; |
358 | |
359 | my @results_hash; |
360 | foreach my $namespace (@namespaces) { |
361 | push( |
362 | @results_hash, |
363 | map { (substr($_, length "${namespace}::"), $_) } |
364 | Module::Find::findallmod($namespace) |
365 | ); |
366 | } |
367 | |
368 | @results_hash; |
369 | } |
370 | |
2374c5ff |
371 | sub load_namespaces { |
85bd0538 |
372 | my ($class, %args) = @_; |
2374c5ff |
373 | |
c87014e8 |
374 | my $result_namespace = delete $args{result_namespace} || 'Result'; |
25fb14bd |
375 | my $resultset_namespace = delete $args{resultset_namespace} || 'ResultSet'; |
25fb14bd |
376 | my $default_resultset_class = delete $args{default_resultset_class}; |
0f4ec1d2 |
377 | |
25fb14bd |
378 | $class->throw_exception('load_namespaces: unknown option(s): ' |
379 | . join(q{,}, map { qq{'$_'} } keys %args)) |
380 | if scalar keys %args; |
381 | |
7a58f051 |
382 | $default_resultset_class |
383 | = $class->_expand_relative_name($default_resultset_class); |
f017c022 |
384 | |
c87014e8 |
385 | for my $arg ($result_namespace, $resultset_namespace) { |
f017c022 |
386 | $arg = [ $arg ] if !ref($arg) && $arg; |
2374c5ff |
387 | |
f017c022 |
388 | $class->throw_exception('load_namespaces: namespace arguments must be ' |
389 | . 'a simple string or an arrayref') |
390 | if ref($arg) ne 'ARRAY'; |
2374c5ff |
391 | |
7a58f051 |
392 | $_ = $class->_expand_relative_name($_) for (@$arg); |
f017c022 |
393 | } |
2374c5ff |
394 | |
c87014e8 |
395 | my %results = $class->_map_namespaces(@$result_namespace); |
f017c022 |
396 | my %resultsets = $class->_map_namespaces(@$resultset_namespace); |
0f4ec1d2 |
397 | |
2374c5ff |
398 | my @to_register; |
399 | { |
25fb14bd |
400 | no warnings 'redefine'; |
2374c5ff |
401 | local *Class::C3::reinitialize = sub { }; |
25fb14bd |
402 | use warnings 'redefine'; |
0f4ec1d2 |
403 | |
c87014e8 |
404 | foreach my $result (keys %results) { |
405 | my $result_class = $results{$result}; |
406 | $class->ensure_class_loaded($result_class); |
407 | $result_class->source_name($result) unless $result_class->source_name; |
0f4ec1d2 |
408 | |
c87014e8 |
409 | my $rs_class = delete $resultsets{$result}; |
410 | my $rs_set = $result_class->resultset_class; |
25fb14bd |
411 | if($rs_set && $rs_set ne 'DBIx::Class::ResultSet') { |
f017c022 |
412 | if($rs_class && $rs_class ne $rs_set) { |
c87014e8 |
413 | warn "We found ResultSet class '$rs_class' for '$result', but it seems " |
414 | . "that you had already set '$result' to use '$rs_set' instead"; |
2374c5ff |
415 | } |
416 | } |
25fb14bd |
417 | elsif($rs_class ||= $default_resultset_class) { |
418 | $class->ensure_class_loaded($rs_class); |
c87014e8 |
419 | $result_class->resultset_class($rs_class); |
0f4ec1d2 |
420 | } |
2374c5ff |
421 | |
c87014e8 |
422 | push(@to_register, [ $result_class->source_name, $result_class ]); |
2374c5ff |
423 | } |
424 | } |
425 | |
0f4ec1d2 |
426 | foreach (sort keys %resultsets) { |
427 | warn "load_namespaces found ResultSet class $_ with no " |
c87014e8 |
428 | . 'corresponding Result class'; |
2374c5ff |
429 | } |
0f4ec1d2 |
430 | |
fdcd8145 |
431 | Class::C3->reinitialize; |
432 | $class->register_class(@$_) for (@to_register); |
433 | |
0f4ec1d2 |
434 | return; |
2374c5ff |
435 | } |
436 | |
87c4e602 |
437 | =head2 compose_connection |
438 | |
27f01d1f |
439 | =over 4 |
440 | |
ebc77b53 |
441 | =item Arguments: $target_namespace, @db_info |
429bd4f1 |
442 | |
d601dc88 |
443 | =item Return Value: $new_schema |
27f01d1f |
444 | |
445 | =back |
076652e8 |
446 | |
2053ab2a |
447 | Calls L<DBIx::Class::Schema/"compose_namespace"> to the target namespace, |
448 | calls L<DBIx::Class::Schema/connection> with @db_info on the new schema, |
449 | then injects the L<DBix::Class::ResultSetProxy> component and a |
450 | resultset_instance classdata entry on all the new classes, in order to support |
82b01c38 |
451 | $target_namespaces::$class->search(...) method calls. |
452 | |
453 | This is primarily useful when you have a specific need for class method access |
454 | to a connection. In normal usage it is preferred to call |
455 | L<DBIx::Class::Schema/connect> and use the resulting schema object to operate |
456 | on L<DBIx::Class::ResultSet> objects with L<DBIx::Class::Schema/resultset> for |
457 | more information. |
54540863 |
458 | |
076652e8 |
459 | =cut |
460 | |
a02675cd |
461 | sub compose_connection { |
ea20d0fd |
462 | my ($self, $target, @info) = @_; |
80c90f5d |
463 | my $base = 'DBIx::Class::ResultSetProxy'; |
8ef144ff |
464 | eval "require ${base};"; |
bc0c9800 |
465 | $self->throw_exception |
466 | ("No arguments to load_classes and couldn't load ${base} ($@)") |
467 | if $@; |
be381829 |
468 | |
469 | if ($self eq $target) { |
470 | # Pathological case, largely caused by the docs on early C::M::DBIC::Plain |
471 | foreach my $moniker ($self->sources) { |
472 | my $source = $self->source($moniker); |
473 | my $class = $source->result_class; |
474 | $self->inject_base($class, $base); |
475 | $class->mk_classdata(resultset_instance => $source->resultset); |
476 | $class->mk_classdata(class_resolver => $self); |
477 | } |
50041f3c |
478 | $self->connection(@info); |
be381829 |
479 | return $self; |
480 | } |
481 | |
66d9ef6b |
482 | my $schema = $self->compose_namespace($target, $base); |
ecceadff |
483 | { |
484 | no strict 'refs'; |
485 | *{"${target}::schema"} = sub { $schema }; |
486 | } |
487 | |
66d9ef6b |
488 | $schema->connection(@info); |
0dc79249 |
489 | foreach my $moniker ($schema->sources) { |
490 | my $source = $schema->source($moniker); |
491 | my $class = $source->result_class; |
492 | #warn "$moniker $class $source ".$source->storage; |
8c49f629 |
493 | $class->mk_classdata(result_source_instance => $source); |
ea20d0fd |
494 | $class->mk_classdata(resultset_instance => $source->resultset); |
66d9ef6b |
495 | $class->mk_classdata(class_resolver => $schema); |
bfb2bd4f |
496 | } |
497 | return $schema; |
e678398e |
498 | } |
499 | |
77254782 |
500 | =head2 compose_namespace |
501 | |
27f01d1f |
502 | =over 4 |
503 | |
504 | =item Arguments: $target_namespace, $additional_base_class? |
82b01c38 |
505 | |
d601dc88 |
506 | =item Return Value: $new_schema |
27f01d1f |
507 | |
508 | =back |
13765dad |
509 | |
82b01c38 |
510 | For each L<DBIx::Class::ResultSource> in the schema, this method creates a |
511 | class in the target namespace (e.g. $target_namespace::CD, |
512 | $target_namespace::Artist) that inherits from the corresponding classes |
513 | attached to the current schema. |
77254782 |
514 | |
82b01c38 |
515 | It also attaches a corresponding L<DBIx::Class::ResultSource> object to the |
516 | new $schema object. If C<$additional_base_class> is given, the new composed |
517 | classes will inherit from first the corresponding classe from the current |
518 | schema then the base class. |
519 | |
2053ab2a |
520 | For example, for a schema with My::Schema::CD and My::Schema::Artist classes, |
82b01c38 |
521 | |
522 | $schema->compose_namespace('My::DB', 'Base::Class'); |
523 | print join (', ', @My::DB::CD::ISA) . "\n"; |
524 | print join (', ', @My::DB::Artist::ISA) ."\n"; |
525 | |
2053ab2a |
526 | will produce the output |
82b01c38 |
527 | |
528 | My::Schema::CD, Base::Class |
529 | My::Schema::Artist, Base::Class |
77254782 |
530 | |
531 | =cut |
532 | |
e678398e |
533 | sub compose_namespace { |
66d9ef6b |
534 | my ($self, $target, $base) = @_; |
535 | my %reg = %{ $self->source_registrations }; |
11b78bd6 |
536 | my %target; |
537 | my %map; |
66d9ef6b |
538 | my $schema = $self->clone; |
e9100ff7 |
539 | { |
540 | no warnings qw/redefine/; |
541 | local *Class::C3::reinitialize = sub { }; |
542 | foreach my $moniker ($schema->sources) { |
543 | my $source = $schema->source($moniker); |
544 | my $target_class = "${target}::${moniker}"; |
545 | $self->inject_base( |
546 | $target_class => $source->result_class, ($base ? $base : ()) |
547 | ); |
548 | $source->result_class($target_class); |
9d3d5af3 |
549 | $target_class->result_source_instance($source) |
550 | if $target_class->can('result_source_instance'); |
e9100ff7 |
551 | } |
b7951443 |
552 | } |
e9100ff7 |
553 | Class::C3->reinitialize(); |
11b78bd6 |
554 | { |
555 | no strict 'refs'; |
1edaf6fe |
556 | foreach my $meth (qw/class source resultset/) { |
557 | *{"${target}::${meth}"} = |
558 | sub { shift->schema->$meth(@_) }; |
559 | } |
11b78bd6 |
560 | } |
bfb2bd4f |
561 | return $schema; |
b7951443 |
562 | } |
563 | |
87c4e602 |
564 | =head2 setup_connection_class |
565 | |
27f01d1f |
566 | =over 4 |
567 | |
ebc77b53 |
568 | =item Arguments: $target, @info |
27f01d1f |
569 | |
570 | =back |
076652e8 |
571 | |
82b01c38 |
572 | Sets up a database connection class to inject between the schema and the |
573 | subclasses that the schema creates. |
429bd4f1 |
574 | |
076652e8 |
575 | =cut |
576 | |
b7951443 |
577 | sub setup_connection_class { |
578 | my ($class, $target, @info) = @_; |
63e9583a |
579 | $class->inject_base($target => 'DBIx::Class::DB'); |
580 | #$target->load_components('DB'); |
b7951443 |
581 | $target->connection(@info); |
582 | } |
583 | |
87c4e602 |
584 | =head2 connection |
585 | |
27f01d1f |
586 | =over 4 |
587 | |
ebc77b53 |
588 | =item Arguments: @args |
66d9ef6b |
589 | |
d601dc88 |
590 | =item Return Value: $new_schema |
27f01d1f |
591 | |
592 | =back |
82b01c38 |
593 | |
594 | Instantiates a new Storage object of type |
595 | L<DBIx::Class::Schema/"storage_type"> and passes the arguments to |
85f78622 |
596 | $storage->connect_info. Sets the connection in-place on the schema. |
597 | |
598 | See L<DBIx::Class::Storage::DBI/"connect_info"> for DBI-specific syntax, |
599 | or L<DBIx::Class::Storage> in general. |
66d9ef6b |
600 | |
601 | =cut |
602 | |
603 | sub connection { |
604 | my ($self, @info) = @_; |
e59d3e5b |
605 | return $self if !@info && $self->storage; |
1e10a11d |
606 | my $storage_class = $self->storage_type; |
607 | $storage_class = 'DBIx::Class::Storage'.$storage_class |
608 | if $storage_class =~ m/^::/; |
8ef144ff |
609 | eval "require ${storage_class};"; |
bc0c9800 |
610 | $self->throw_exception( |
611 | "No arguments to load_classes and couldn't load ${storage_class} ($@)" |
612 | ) if $@; |
82cc0386 |
613 | my $storage = $storage_class->new($self); |
66d9ef6b |
614 | $storage->connect_info(\@info); |
615 | $self->storage($storage); |
616 | return $self; |
617 | } |
618 | |
87c4e602 |
619 | =head2 connect |
620 | |
27f01d1f |
621 | =over 4 |
622 | |
ebc77b53 |
623 | =item Arguments: @info |
66d9ef6b |
624 | |
d601dc88 |
625 | =item Return Value: $new_schema |
27f01d1f |
626 | |
627 | =back |
82b01c38 |
628 | |
629 | This is a convenience method. It is equivalent to calling |
630 | $schema->clone->connection(@info). See L</connection> and L</clone> for more |
631 | information. |
66d9ef6b |
632 | |
633 | =cut |
634 | |
08b515f1 |
635 | sub connect { shift->clone->connection(@_) } |
636 | |
4012acd8 |
637 | =head2 txn_do |
08b515f1 |
638 | |
4012acd8 |
639 | =over 4 |
08b515f1 |
640 | |
4012acd8 |
641 | =item Arguments: C<$coderef>, @coderef_args? |
08b515f1 |
642 | |
4012acd8 |
643 | =item Return Value: The return value of $coderef |
08b515f1 |
644 | |
4012acd8 |
645 | =back |
08b515f1 |
646 | |
4012acd8 |
647 | Executes C<$coderef> with (optional) arguments C<@coderef_args> atomically, |
648 | returning its result (if any). Equivalent to calling $schema->storage->txn_do. |
649 | See L<DBIx::Class::Storage/"txn_do"> for more information. |
08b515f1 |
650 | |
4012acd8 |
651 | This interface is preferred over using the individual methods L</txn_begin>, |
652 | L</txn_commit>, and L</txn_rollback> below. |
08b515f1 |
653 | |
4012acd8 |
654 | =cut |
08b515f1 |
655 | |
4012acd8 |
656 | sub txn_do { |
657 | my $self = shift; |
08b515f1 |
658 | |
4012acd8 |
659 | $self->storage or $self->throw_exception |
660 | ('txn_do called on $schema without storage'); |
08b515f1 |
661 | |
4012acd8 |
662 | $self->storage->txn_do(@_); |
663 | } |
08b515f1 |
664 | |
4012acd8 |
665 | =head2 txn_begin |
a62cf8d4 |
666 | |
4012acd8 |
667 | Begins a transaction (does nothing if AutoCommit is off). Equivalent to |
668 | calling $schema->storage->txn_begin. See |
669 | L<DBIx::Class::Storage::DBI/"txn_begin"> for more information. |
27f01d1f |
670 | |
4012acd8 |
671 | =cut |
82b01c38 |
672 | |
4012acd8 |
673 | sub txn_begin { |
674 | my $self = shift; |
27f01d1f |
675 | |
4012acd8 |
676 | $self->storage or $self->throw_exception |
677 | ('txn_begin called on $schema without storage'); |
a62cf8d4 |
678 | |
4012acd8 |
679 | $self->storage->txn_begin; |
680 | } |
a62cf8d4 |
681 | |
4012acd8 |
682 | =head2 txn_commit |
a62cf8d4 |
683 | |
4012acd8 |
684 | Commits the current transaction. Equivalent to calling |
685 | $schema->storage->txn_commit. See L<DBIx::Class::Storage::DBI/"txn_commit"> |
686 | for more information. |
a62cf8d4 |
687 | |
4012acd8 |
688 | =cut |
a62cf8d4 |
689 | |
4012acd8 |
690 | sub txn_commit { |
691 | my $self = shift; |
a62cf8d4 |
692 | |
4012acd8 |
693 | $self->storage or $self->throw_exception |
694 | ('txn_commit called on $schema without storage'); |
a62cf8d4 |
695 | |
4012acd8 |
696 | $self->storage->txn_commit; |
697 | } |
70634260 |
698 | |
4012acd8 |
699 | =head2 txn_rollback |
a62cf8d4 |
700 | |
4012acd8 |
701 | Rolls back the current transaction. Equivalent to calling |
702 | $schema->storage->txn_rollback. See |
703 | L<DBIx::Class::Storage::DBI/"txn_rollback"> for more information. |
a62cf8d4 |
704 | |
705 | =cut |
706 | |
4012acd8 |
707 | sub txn_rollback { |
708 | my $self = shift; |
a62cf8d4 |
709 | |
19630353 |
710 | $self->storage or $self->throw_exception |
4012acd8 |
711 | ('txn_rollback called on $schema without storage'); |
a62cf8d4 |
712 | |
4012acd8 |
713 | $self->storage->txn_rollback; |
a62cf8d4 |
714 | } |
715 | |
66d9ef6b |
716 | =head2 clone |
717 | |
27f01d1f |
718 | =over 4 |
719 | |
d601dc88 |
720 | =item Return Value: $new_schema |
27f01d1f |
721 | |
722 | =back |
82b01c38 |
723 | |
66d9ef6b |
724 | Clones the schema and its associated result_source objects and returns the |
725 | copy. |
726 | |
727 | =cut |
728 | |
729 | sub clone { |
730 | my ($self) = @_; |
04786a4c |
731 | my $clone = { (ref $self ? %$self : ()) }; |
732 | bless $clone, (ref $self || $self); |
733 | |
66d9ef6b |
734 | foreach my $moniker ($self->sources) { |
735 | my $source = $self->source($moniker); |
736 | my $new = $source->new($source); |
737 | $clone->register_source($moniker => $new); |
738 | } |
82cc0386 |
739 | $clone->storage->set_schema($clone) if $clone->storage; |
66d9ef6b |
740 | return $clone; |
741 | } |
742 | |
87c4e602 |
743 | =head2 populate |
744 | |
27f01d1f |
745 | =over 4 |
746 | |
ebc77b53 |
747 | =item Arguments: $moniker, \@data; |
27f01d1f |
748 | |
749 | =back |
a37a4697 |
750 | |
751 | Populates the source registered with the given moniker with the supplied data. |
82b01c38 |
752 | @data should be a list of listrefs -- the first containing column names, the |
753 | second matching values. |
754 | |
755 | i.e., |
a37a4697 |
756 | |
24d67825 |
757 | $schema->populate('Artist', [ |
758 | [ qw/artistid name/ ], |
759 | [ 1, 'Popular Band' ], |
760 | [ 2, 'Indie Band' ], |
a62cf8d4 |
761 | ... |
762 | ]); |
a37a4697 |
763 | |
764 | =cut |
765 | |
766 | sub populate { |
767 | my ($self, $name, $data) = @_; |
768 | my $rs = $self->resultset($name); |
769 | my @names = @{shift(@$data)}; |
84e3c114 |
770 | my @created; |
a37a4697 |
771 | foreach my $item (@$data) { |
772 | my %create; |
773 | @create{@names} = @$item; |
84e3c114 |
774 | push(@created, $rs->create(\%create)); |
a37a4697 |
775 | } |
84e3c114 |
776 | return @created; |
a37a4697 |
777 | } |
778 | |
82cc0386 |
779 | =head2 exception_action |
780 | |
781 | =over 4 |
782 | |
783 | =item Arguments: $code_reference |
784 | |
785 | =back |
786 | |
db5dc233 |
787 | If C<exception_action> is set for this class/object, L</throw_exception> |
788 | will prefer to call this code reference with the exception as an argument, |
789 | rather than its normal <croak> action. |
790 | |
791 | Your subroutine should probably just wrap the error in the exception |
792 | object/class of your choosing and rethrow. If, against all sage advice, |
793 | you'd like your C<exception_action> to suppress a particular exception |
794 | completely, simply have it return true. |
82cc0386 |
795 | |
796 | Example: |
797 | |
798 | package My::Schema; |
799 | use base qw/DBIx::Class::Schema/; |
800 | use My::ExceptionClass; |
801 | __PACKAGE__->exception_action(sub { My::ExceptionClass->throw(@_) }); |
802 | __PACKAGE__->load_classes; |
803 | |
db5dc233 |
804 | # or: |
82cc0386 |
805 | my $schema_obj = My::Schema->connect( .... ); |
806 | $schema_obj->exception_action(sub { My::ExceptionClass->throw(@_) }); |
807 | |
db5dc233 |
808 | # suppress all exceptions, like a moron: |
809 | $schema_obj->exception_action(sub { 1 }); |
810 | |
5160b401 |
811 | =head2 throw_exception |
701da8c4 |
812 | |
75d07914 |
813 | =over 4 |
82b01c38 |
814 | |
ebc77b53 |
815 | =item Arguments: $message |
82b01c38 |
816 | |
817 | =back |
818 | |
819 | Throws an exception. Defaults to using L<Carp::Clan> to report errors from |
db5dc233 |
820 | user's perspective. See L</exception_action> for details on overriding |
821 | this method's behavior. |
701da8c4 |
822 | |
823 | =cut |
824 | |
825 | sub throw_exception { |
82cc0386 |
826 | my $self = shift; |
db5dc233 |
827 | croak @_ if !$self->exception_action || !$self->exception_action->(@_); |
701da8c4 |
828 | } |
829 | |
ec6704d4 |
830 | =head2 deploy (EXPERIMENTAL) |
1c339d71 |
831 | |
82b01c38 |
832 | =over 4 |
833 | |
6e73ac25 |
834 | =item Arguments: $sqlt_args, $dir |
82b01c38 |
835 | |
836 | =back |
837 | |
838 | Attempts to deploy the schema to the current storage using L<SQL::Translator>. |
ec6704d4 |
839 | |
840 | Note that this feature is currently EXPERIMENTAL and may not work correctly |
841 | across all databases, or fully handle complex relationships. |
1c339d71 |
842 | |
51bace1c |
843 | See L<SQL::Translator/METHODS> for a list of values for C<$sqlt_args>. The most |
844 | common value for this would be C<< { add_drop_table => 1, } >> to have the SQL |
845 | produced include a DROP TABLE statement for each table created. |
846 | |
1c339d71 |
847 | =cut |
848 | |
849 | sub deploy { |
6e73ac25 |
850 | my ($self, $sqltargs, $dir) = @_; |
1c339d71 |
851 | $self->throw_exception("Can't deploy without storage") unless $self->storage; |
6e73ac25 |
852 | $self->storage->deploy($self, undef, $sqltargs, $dir); |
1c339d71 |
853 | } |
854 | |
c0f61310 |
855 | =head2 create_ddl_dir (EXPERIMENTAL) |
856 | |
857 | =over 4 |
858 | |
859 | =item Arguments: \@databases, $version, $directory, $sqlt_args |
860 | |
861 | =back |
862 | |
863 | Creates an SQL file based on the Schema, for each of the specified |
864 | database types, in the given directory. |
865 | |
866 | Note that this feature is currently EXPERIMENTAL and may not work correctly |
867 | across all databases, or fully handle complex relationships. |
868 | |
869 | =cut |
870 | |
6e73ac25 |
871 | sub create_ddl_dir { |
e673f011 |
872 | my $self = shift; |
873 | |
874 | $self->throw_exception("Can't create_ddl_dir without storage") unless $self->storage; |
875 | $self->storage->create_ddl_dir($self, @_); |
876 | } |
877 | |
9b83fccd |
878 | =head2 ddl_filename (EXPERIMENTAL) |
879 | |
880 | my $filename = $table->ddl_filename($type, $dir, $version) |
881 | |
882 | Creates a filename for a SQL file based on the table class name. Not |
883 | intended for direct end user use. |
884 | |
885 | =cut |
886 | |
6e73ac25 |
887 | sub ddl_filename { |
e673f011 |
888 | my ($self, $type, $dir, $version) = @_; |
889 | |
890 | my $filename = ref($self); |
9e7b9292 |
891 | $filename =~ s/::/-/; |
e673f011 |
892 | $filename = "$dir$filename-$version-$type.sql"; |
893 | |
894 | return $filename; |
895 | } |
896 | |
a02675cd |
897 | 1; |
c2da098a |
898 | |
c2da098a |
899 | =head1 AUTHORS |
900 | |
daec44b8 |
901 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
c2da098a |
902 | |
903 | =head1 LICENSE |
904 | |
905 | You may distribute this code under the same terms as Perl itself. |
906 | |
907 | =cut |