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 { |
70 | my ($class, $name, $to_register) = @_; |
74b92d9a |
71 | my %reg = %{$class->class_registrations}; |
a02675cd |
72 | $reg{$name} = $to_register; |
74b92d9a |
73 | $class->class_registrations(\%reg); |
74 | } |
75 | |
130c6439 |
76 | =head2 registered_classes |
076652e8 |
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 | |
74b92d9a |
84 | sub registered_classes { |
85 | return values %{shift->class_registrations}; |
a02675cd |
86 | } |
87 | |
130c6439 |
88 | =head2 load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}] |
076652e8 |
89 | |
429bd4f1 |
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>); |
076652e8 |
93 | |
5ce32fc1 |
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 | |
076652e8 |
98 | =cut |
99 | |
a02675cd |
100 | sub load_classes { |
5ce32fc1 |
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 { |
41a6f8c0 |
128 | eval "require Module::Find;"; |
129 | $class->throw("No arguments to load_classes and couldn't load". |
130 | " Module::Find ($@)") if $@; |
5ce32fc1 |
131 | my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class); |
132 | $comps_for{$class} = \@comp; |
41a6f8c0 |
133 | } |
5ce32fc1 |
134 | |
135 | foreach my $prefix (keys %comps_for) { |
136 | foreach my $comp (@{$comps_for{$prefix}||[]}) { |
137 | my $comp_class = "${prefix}::${comp}"; |
5ce32fc1 |
138 | eval "use $comp_class"; # If it fails, assume the user fixed it |
139 | $class->register_class($comp => $comp_class); |
140 | } |
a02675cd |
141 | } |
142 | } |
143 | |
130c6439 |
144 | =head2 compose_connection <target> <@db_info> |
429bd4f1 |
145 | |
146 | This is the most important method in this class. it takes a target namespace, |
147 | as well as dbh connection info, and creates a L<DBIx::Class::DB> class as |
148 | well as subclasses for each of your database classes in this namespace, using |
149 | this connection. |
076652e8 |
150 | |
54540863 |
151 | It will also setup a ->class method on the target class, which lets you |
8c130052 |
152 | resolve database classes based on the schema component name, for example |
153 | |
54540863 |
154 | MyApp::DB->class('Foo') # returns MyApp::DB::Foo, |
8c130052 |
155 | # which ISA MyApp::Schema::Foo |
156 | |
157 | This is the recommended API for accessing Schema generated classes, and |
158 | using it might give you instant advantages with future versions of DBIC. |
159 | |
54540863 |
160 | WARNING: Loading components into Schema classes after compose_connection |
161 | may not cause them to be seen by the classes in your target namespace due |
162 | to the dispatch table approach used by Class::C3. If you do this you may find |
163 | you need to call Class::C3->reinitialize() afterwards to get the behaviour |
164 | you expect. |
165 | |
076652e8 |
166 | =cut |
167 | |
a02675cd |
168 | sub compose_connection { |
169 | my ($class, $target, @info) = @_; |
11b78bd6 |
170 | my $conn_class = "${target}::_db"; |
171 | $class->setup_connection_class($conn_class, @info); |
e678398e |
172 | $class->compose_namespace($target, $conn_class); |
173 | } |
174 | |
175 | sub compose_namespace { |
176 | my ($class, $target, $base) = @_; |
74b92d9a |
177 | my %reg = %{ $class->class_registrations }; |
11b78bd6 |
178 | my %target; |
179 | my %map; |
a02675cd |
180 | while (my ($comp, $comp_class) = each %reg) { |
181 | my $target_class = "${target}::${comp}"; |
e678398e |
182 | $class->inject_base($target_class, $comp_class, $base); |
11b78bd6 |
183 | @map{$comp, $comp_class} = ($target_class, $target_class); |
b7951443 |
184 | } |
11b78bd6 |
185 | { |
186 | no strict 'refs'; |
187 | *{"${target}::class"} = |
188 | sub { |
189 | my ($class, $to_map) = @_; |
190 | return $map{$to_map}; |
191 | }; |
d3dec624 |
192 | *{"${target}::classes"} = sub { return \%map; }; |
11b78bd6 |
193 | } |
e678398e |
194 | $base->class_resolver($target); |
b7951443 |
195 | } |
196 | |
130c6439 |
197 | =head2 setup_connection_class <$target> <@info> |
076652e8 |
198 | |
429bd4f1 |
199 | Sets up a database connection class to inject between the schema |
200 | and the subclasses the schema creates. |
201 | |
076652e8 |
202 | =cut |
203 | |
b7951443 |
204 | sub setup_connection_class { |
205 | my ($class, $target, @info) = @_; |
63e9583a |
206 | $class->inject_base($target => 'DBIx::Class::DB'); |
207 | #$target->load_components('DB'); |
b7951443 |
208 | $target->connection(@info); |
209 | } |
210 | |
a02675cd |
211 | 1; |
c2da098a |
212 | |
c2da098a |
213 | =head1 AUTHORS |
214 | |
daec44b8 |
215 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
c2da098a |
216 | |
217 | =head1 LICENSE |
218 | |
219 | You may distribute this code under the same terms as Perl itself. |
220 | |
221 | =cut |
222 | |