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/); |
74b92d9a |
10 | __PACKAGE__->mk_classdata('class_registrations' => {}); |
d7156e50 |
11 | __PACKAGE__->mk_classdata('storage_type' => 'DBI'); |
12 | __PACKAGE__->mk_classdata('storage'); |
a02675cd |
13 | |
c2da098a |
14 | =head1 NAME |
15 | |
16 | DBIx::Class::Schema - composable schemas |
17 | |
18 | =head1 SYNOPSIS |
19 | |
03312470 |
20 | in My/Schema.pm |
c2da098a |
21 | |
22 | package My::Schema; |
23 | |
24 | use base qw/DBIx::Class::Schema/; |
25 | |
26 | __PACKAGE__->load_classes(qw/Foo Bar Baz/); |
27 | |
03312470 |
28 | in My/Schema/Foo.pm |
c2da098a |
29 | |
30 | package My::Schema::Foo; |
31 | |
03312470 |
32 | use base qw/DBIx::Class/; |
c2da098a |
33 | |
54540863 |
34 | __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example |
c2da098a |
35 | __PACKAGE__->table('foo'); |
36 | ... |
37 | |
03312470 |
38 | in My/DB.pm |
c2da098a |
39 | |
40 | use My::Schema; |
41 | |
42 | My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs); |
43 | |
03312470 |
44 | then in app code |
c2da098a |
45 | |
656796f2 |
46 | my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB |
c2da098a |
47 | |
48 | =head1 DESCRIPTION |
49 | |
429bd4f1 |
50 | Creates database classes based on a schema. This allows you to have more than |
51 | one concurrent connection using the same database classes, by making |
52 | subclasses under a new namespace for each connection. If you only need one |
53 | class, you should probably use L<DBIx::Class::DB> directly instead. |
54 | |
03312470 |
55 | NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS> |
56 | carefully as DBIx::Class does things a little differently. Note in |
57 | particular which module inherits off which. |
58 | |
c2da098a |
59 | =head1 METHODS |
60 | |
130c6439 |
61 | =head2 register_class <component> <component_class> |
076652e8 |
62 | |
63 | Registers the class in the schema's class_registrations. This is a hash |
429bd4f1 |
64 | containing database classes, keyed by their monikers. It's used by |
076652e8 |
65 | compose_connection to create/modify all the existing database classes. |
66 | |
c2da098a |
67 | =cut |
68 | |
a02675cd |
69 | sub register_class { |
7fb16f1a |
70 | my ($self, $name, $to_register) = @_; |
71 | my %reg = %{$self->class_registrations}; |
a02675cd |
72 | $reg{$name} = $to_register; |
7fb16f1a |
73 | $self->class_registrations(\%reg); |
8c49f629 |
74 | $to_register->result_source_instance->schema($self); |
74b92d9a |
75 | } |
76 | |
130c6439 |
77 | =head2 registered_classes |
076652e8 |
78 | |
79 | Simple read-only accessor for the schema's registered classes. See |
80 | register_class above if you want to modify it. |
81 | |
82 | |
83 | =cut |
84 | |
74b92d9a |
85 | sub registered_classes { |
86 | return values %{shift->class_registrations}; |
a02675cd |
87 | } |
88 | |
bfb2bd4f |
89 | =head2 class |
90 | |
91 | my $class = $schema->class('Foo'); |
92 | |
93 | Shortcut to retrieve a single class by its registered name |
94 | |
95 | =cut |
96 | |
97 | sub class { |
98 | my ($self, $class) = @_; |
99 | return $self->class_registrations->{$class}; |
100 | } |
101 | |
ea20d0fd |
102 | =head2 source |
103 | |
104 | my $source = $schema->source('Foo'); |
105 | |
106 | Returns the result source object for the registered name |
107 | |
108 | =cut |
109 | |
110 | sub source { |
111 | my ($self, $class) = @_; |
8c49f629 |
112 | return $self->class_registrations->{$class}->result_source_instance |
8452e496 |
113 | if $self->class_registrations->{$class}; |
ea20d0fd |
114 | } |
115 | |
116 | =head2 resultset |
117 | |
118 | my $rs = $schema->resultset('Foo'); |
119 | |
120 | Returns the resultset for the registered name |
121 | |
122 | =cut |
123 | |
124 | sub resultset { |
125 | my ($self, $class) = @_; |
8c49f629 |
126 | return $self->class_registrations->{$class}->result_source_instance->resultset; |
ea20d0fd |
127 | } |
128 | |
129 | |
130c6439 |
130 | =head2 load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}] |
076652e8 |
131 | |
429bd4f1 |
132 | Uses L<Module::Find> to find all classes under the database class' namespace, |
133 | or uses the classes you select. Then it loads the component (using L<use>), |
134 | and registers them (using B<register_class>); |
076652e8 |
135 | |
5ce32fc1 |
136 | It is possible to comment out classes with a leading '#', but note that perl |
137 | will think it's a mistake (trying to use a comment in a qw list) so you'll |
138 | need to add "no warnings 'qw';" before your load_classes call. |
139 | |
076652e8 |
140 | =cut |
141 | |
a02675cd |
142 | sub load_classes { |
5ce32fc1 |
143 | my ($class, @params) = @_; |
144 | |
145 | my %comps_for; |
146 | |
147 | if (@params) { |
148 | foreach my $param (@params) { |
149 | if (ref $param eq 'ARRAY') { |
150 | # filter out commented entries |
151 | my @modules = grep { $_ !~ /^#/ } @$param; |
152 | |
153 | push (@{$comps_for{$class}}, @modules); |
154 | } |
155 | elsif (ref $param eq 'HASH') { |
156 | # more than one namespace possible |
157 | for my $comp ( keys %$param ) { |
158 | # filter out commented entries |
159 | my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}}; |
160 | |
161 | push (@{$comps_for{$comp}}, @modules); |
162 | } |
163 | } |
164 | else { |
165 | # filter out commented entries |
166 | push (@{$comps_for{$class}}, $param) if $param !~ /^#/; |
167 | } |
168 | } |
169 | } else { |
41a6f8c0 |
170 | eval "require Module::Find;"; |
171 | $class->throw("No arguments to load_classes and couldn't load". |
172 | " Module::Find ($@)") if $@; |
5ce32fc1 |
173 | my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class); |
174 | $comps_for{$class} = \@comp; |
41a6f8c0 |
175 | } |
5ce32fc1 |
176 | |
177 | foreach my $prefix (keys %comps_for) { |
178 | foreach my $comp (@{$comps_for{$prefix}||[]}) { |
179 | my $comp_class = "${prefix}::${comp}"; |
5ce32fc1 |
180 | eval "use $comp_class"; # If it fails, assume the user fixed it |
bfb2bd4f |
181 | if ($@) { |
182 | die $@ unless $@ =~ /Can't locate/; |
183 | } |
5ce32fc1 |
184 | $class->register_class($comp => $comp_class); |
185 | } |
a02675cd |
186 | } |
187 | } |
188 | |
130c6439 |
189 | =head2 compose_connection <target> <@db_info> |
429bd4f1 |
190 | |
191 | This is the most important method in this class. it takes a target namespace, |
192 | as well as dbh connection info, and creates a L<DBIx::Class::DB> class as |
193 | well as subclasses for each of your database classes in this namespace, using |
194 | this connection. |
076652e8 |
195 | |
54540863 |
196 | It will also setup a ->class method on the target class, which lets you |
8c130052 |
197 | resolve database classes based on the schema component name, for example |
198 | |
54540863 |
199 | MyApp::DB->class('Foo') # returns MyApp::DB::Foo, |
8c130052 |
200 | # which ISA MyApp::Schema::Foo |
201 | |
202 | This is the recommended API for accessing Schema generated classes, and |
203 | using it might give you instant advantages with future versions of DBIC. |
204 | |
54540863 |
205 | WARNING: Loading components into Schema classes after compose_connection |
206 | may not cause them to be seen by the classes in your target namespace due |
207 | to the dispatch table approach used by Class::C3. If you do this you may find |
208 | you need to call Class::C3->reinitialize() afterwards to get the behaviour |
209 | you expect. |
210 | |
076652e8 |
211 | =cut |
212 | |
a02675cd |
213 | sub compose_connection { |
ea20d0fd |
214 | my ($self, $target, @info) = @_; |
11b78bd6 |
215 | my $conn_class = "${target}::_db"; |
ea20d0fd |
216 | $self->setup_connection_class($conn_class, @info); |
217 | my $schema = $self->compose_namespace($target, $conn_class); |
bfb2bd4f |
218 | $schema->storage($conn_class->storage); |
219 | foreach my $class ($schema->registered_classes) { |
8c49f629 |
220 | my $source = $class->result_source_instance; |
bfb2bd4f |
221 | $source = $source->new($source); |
222 | $source->schema($schema); |
223 | $source->result_class($class); |
8c49f629 |
224 | $class->mk_classdata(result_source_instance => $source); |
ea20d0fd |
225 | $class->mk_classdata(resultset_instance => $source->resultset); |
bfb2bd4f |
226 | } |
227 | return $schema; |
e678398e |
228 | } |
229 | |
230 | sub compose_namespace { |
231 | my ($class, $target, $base) = @_; |
74b92d9a |
232 | my %reg = %{ $class->class_registrations }; |
11b78bd6 |
233 | my %target; |
234 | my %map; |
bfb2bd4f |
235 | my $schema = bless({ }, $class); |
a02675cd |
236 | while (my ($comp, $comp_class) = each %reg) { |
237 | my $target_class = "${target}::${comp}"; |
ea20d0fd |
238 | $class->inject_base($target_class, $comp_class, ($base ? $base : ())); |
11b78bd6 |
239 | @map{$comp, $comp_class} = ($target_class, $target_class); |
b7951443 |
240 | } |
bfb2bd4f |
241 | $schema->class_registrations(\%map); |
11b78bd6 |
242 | { |
243 | no strict 'refs'; |
bfb2bd4f |
244 | *{"${target}::schema"} = |
245 | sub { $schema }; |
1edaf6fe |
246 | foreach my $meth (qw/class source resultset/) { |
247 | *{"${target}::${meth}"} = |
248 | sub { shift->schema->$meth(@_) }; |
249 | } |
11b78bd6 |
250 | } |
e678398e |
251 | $base->class_resolver($target); |
bfb2bd4f |
252 | return $schema; |
b7951443 |
253 | } |
254 | |
130c6439 |
255 | =head2 setup_connection_class <$target> <@info> |
076652e8 |
256 | |
429bd4f1 |
257 | Sets up a database connection class to inject between the schema |
258 | and the subclasses the schema creates. |
259 | |
076652e8 |
260 | =cut |
261 | |
b7951443 |
262 | sub setup_connection_class { |
263 | my ($class, $target, @info) = @_; |
63e9583a |
264 | $class->inject_base($target => 'DBIx::Class::DB'); |
265 | #$target->load_components('DB'); |
b7951443 |
266 | $target->connection(@info); |
267 | } |
268 | |
a02675cd |
269 | 1; |
c2da098a |
270 | |
c2da098a |
271 | =head1 AUTHORS |
272 | |
daec44b8 |
273 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
c2da098a |
274 | |
275 | =head1 LICENSE |
276 | |
277 | You may distribute this code under the same terms as Perl itself. |
278 | |
279 | =cut |
280 | |