Commit | Line | Data |
18fca96a |
1 | package DBIx::Class::Schema::Loader; |
a78e3fed |
2 | |
3 | use strict; |
a4a19f3c |
4 | use warnings; |
f6148834 |
5 | use base qw/DBIx::Class::Schema Class::Data::Accessor/; |
fa994d3c |
6 | use Carp::Clan qw/^DBIx::Class/; |
457eb8a6 |
7 | use UNIVERSAL::require; |
996be9ee |
8 | use Class::C3; |
c9f1d7b0 |
9 | use Scalar::Util qw/ weaken /; |
3980d69c |
10 | |
a4a19f3c |
11 | # Always remember to do all digits for the version even if they're 0 |
12 | # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports |
13 | # brain damage and presumably various other packaging systems too |
32f784fc |
14 | our $VERSION = '0.03999_01'; |
457eb8a6 |
15 | |
59cfa251 |
16 | __PACKAGE__->mk_classaccessor('_loader_args' => {}); |
79fe0081 |
17 | __PACKAGE__->mk_classaccessors(qw/dump_to_dir _loader_invoked _loader/); |
a78e3fed |
18 | |
19 | =head1 NAME |
20 | |
18fca96a |
21 | DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema |
a78e3fed |
22 | |
23 | =head1 SYNOPSIS |
24 | |
a4a19f3c |
25 | package My::Schema; |
26 | use base qw/DBIx::Class::Schema::Loader/; |
a78e3fed |
27 | |
996be9ee |
28 | __PACKAGE__->loader_options( |
996be9ee |
29 | constraint => '^foo.*', |
30 | # debug => 1, |
a78e3fed |
31 | ); |
af6c2665 |
32 | |
a4a19f3c |
33 | # in seperate application code ... |
a78e3fed |
34 | |
a4a19f3c |
35 | use My::Schema; |
a78e3fed |
36 | |
a4a19f3c |
37 | my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs); |
38 | # -or- |
996be9ee |
39 | my $schema1 = "My::Schema"; $schema1->connection(as above); |
074e81cd |
40 | |
996be9ee |
41 | =head1 DESCRIPTION |
074e81cd |
42 | |
fbd83464 |
43 | DBIx::Class::Schema::Loader automates the definition of a |
996be9ee |
44 | L<DBIx::Class::Schema> by scanning database table definitions and |
d65cda9e |
45 | setting up the columns, primary keys, and relationships. |
a78e3fed |
46 | |
d65cda9e |
47 | DBIx::Class::Schema::Loader currently supports only the DBI storage type. |
48 | It has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>, and |
49 | L<DBD::SQLite>. Other DBI drivers may function to a greater or lesser |
50 | degree with this loader, depending on how much of the DBI spec they |
51 | implement, and how standard their implementation is. Patches to make |
52 | other DBDs work correctly welcome. |
a78e3fed |
53 | |
996be9ee |
54 | See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing |
55 | your own vendor-specific subclass for an unsupported DBD driver. |
a78e3fed |
56 | |
996be9ee |
57 | This module requires L<DBIx::Class> 0.06 or later, and obsoletes |
58 | the older L<DBIx::Class::Loader>. |
89ecd854 |
59 | |
996be9ee |
60 | This module is designed more to get you up and running quickly against |
61 | an existing database, or to be effective for simple situations, rather |
62 | than to be what you use in the long term for a complex database/project. |
89ecd854 |
63 | |
64 | That being said, transitioning your code from a Schema generated by this |
65 | module to one that doesn't use this module should be straightforward and |
59cfa251 |
66 | painless, so don't shy away from it just for fears of the transition down |
67 | the road. |
89ecd854 |
68 | |
a78e3fed |
69 | =head1 METHODS |
70 | |
996be9ee |
71 | =head2 loader_options |
a78e3fed |
72 | |
996be9ee |
73 | Example in Synopsis above demonstrates a few common arguments. For |
74 | detailed information on all of the arguments, most of which are |
75 | only useful in fairly complex scenarios, see the |
76 | L<DBIx::Class::Schema::Loader::Base> documentation. |
a78e3fed |
77 | |
59cfa251 |
78 | One must call C<loader_options> before any connection is made, |
79 | or embed the C<loader_options> in the connection information itself |
80 | as shown below. Setting C<loader_options> after the connection has |
81 | already been made is useless. |
a78e3fed |
82 | |
996be9ee |
83 | =cut |
1031d4f6 |
84 | |
996be9ee |
85 | sub loader_options { |
86 | my $self = shift; |
87 | |
d65cda9e |
88 | my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_; |
996be9ee |
89 | $self->_loader_args(\%args); |
996be9ee |
90 | |
91 | $self; |
92 | } |
93 | |
94 | sub _invoke_loader { |
95 | my $self = shift; |
96 | my $class = ref $self || $self; |
97 | |
59cfa251 |
98 | my $args = $self->_loader_args; |
99 | |
100 | # set up the schema/schema_class arguments |
101 | $args->{schema} = $self; |
102 | $args->{schema_class} = $class; |
103 | weaken($args->{schema}) if ref $self; |
104 | $args->{dump_directory} ||= $self->dump_to_dir; |
af6c2665 |
105 | |
996be9ee |
106 | # XXX this only works for relative storage_type, like ::DBI ... |
107 | my $impl = "DBIx::Class::Schema::Loader" . $self->storage_type; |
a78e3fed |
108 | $impl->require or |
996be9ee |
109 | croak qq/Could not load storage_type loader "$impl": / . |
3385ac62 |
110 | qq/"$UNIVERSAL::require::ERROR"/; |
af6c2665 |
111 | |
b97c2c1e |
112 | $self->_loader($impl->new(%$args)); |
113 | $self->_loader->load; |
59cfa251 |
114 | $self->_loader_invoked(1); |
996be9ee |
115 | |
996be9ee |
116 | $self; |
117 | } |
118 | |
119 | =head2 connection |
120 | |
d65cda9e |
121 | See L<DBIx::Class::Schema> for basic usage. |
122 | |
123 | If the final argument is a hashref, and it contains a key C<loader_options>, |
124 | that key will be deleted, and its value will be used for the loader options, |
125 | just as if set via the L</loader_options> method above. |
126 | |
127 | The actual auto-loading operation (the heart of this module) will be invoked |
128 | as soon as the connection information is defined. |
996be9ee |
129 | |
130 | =cut |
131 | |
132 | sub connection { |
d65cda9e |
133 | my $self = shift; |
134 | |
135 | if($_[-1] && ref $_[-1] eq 'HASH') { |
136 | if(my $loader_opts = delete $_[-1]->{loader_options}) { |
137 | $self->loader_options($loader_opts); |
fd64d90d |
138 | pop @_ if !keys %{$_[-1]}; |
d65cda9e |
139 | } |
140 | } |
141 | |
142 | $self = $self->next::method(@_); |
996be9ee |
143 | |
144 | my $class = ref $self || $self; |
59cfa251 |
145 | if(!$class->_loader_invoked) { |
fa994d3c |
146 | $self->_invoke_loader |
147 | } |
996be9ee |
148 | |
149 | return $self; |
150 | } |
151 | |
152 | =head2 clone |
153 | |
074e81cd |
154 | See L<DBIx::Class::Schema>. |
996be9ee |
155 | |
156 | =cut |
157 | |
158 | sub clone { |
159 | my $self = shift; |
160 | |
161 | my $clone = $self->next::method(@_); |
162 | |
fa994d3c |
163 | if($clone->_loader_args) { |
164 | $clone->_loader_args->{schema} = $clone; |
165 | weaken($clone->_loader_args->{schema}); |
166 | } |
996be9ee |
167 | |
168 | $clone; |
169 | } |
170 | |
171 | =head2 dump_to_dir |
172 | |
173 | Argument: directory name. |
174 | |
175 | Calling this as a class method on either L<DBIx::Class::Schema::Loader> |
176 | or any derived schema class will cause all affected schemas to dump |
177 | manual versions of themselves to the named directory when they are |
178 | loaded. In order to be effective, this must be set before defining a |
179 | connection on this schema class or any derived object (as the loading |
074e81cd |
180 | happens as soon as both a connection and loader_options are set, and |
181 | only once per class). |
996be9ee |
182 | |
183 | See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more |
184 | details on the dumping mechanism. |
185 | |
186 | This can also be set at module import time via the import option |
187 | C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where |
188 | C</foo/bar> is the target directory. |
189 | |
190 | Examples: |
191 | |
192 | # My::Schema isa DBIx::Class::Schema::Loader, and has connection info |
193 | # hardcoded in the class itself: |
194 | perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1 |
195 | |
196 | # Same, but no hard-coded connection, so we must provide one: |
197 | perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)' |
198 | |
199 | # Or as a class method, as long as you get it done *before* defining a |
200 | # connection on this schema class or any derived object: |
201 | use My::Schema; |
202 | My::Schema->dump_to_dir('/foo/bar'); |
203 | My::Schema->connection(........); |
204 | |
205 | # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all |
206 | # derived schemas |
207 | use My::Schema; |
208 | use My::OtherSchema; |
209 | DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar'); |
210 | My::Schema->connection(.......); |
211 | My::OtherSchema->connection(.......); |
212 | |
213 | # Another alternative to the above: |
214 | use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |; |
215 | use My::Schema; |
216 | use My::OtherSchema; |
217 | My::Schema->connection(.......); |
218 | My::OtherSchema->connection(.......); |
219 | |
220 | =cut |
221 | |
222 | sub import { |
223 | my $self = shift; |
224 | return if !@_; |
225 | foreach my $opt (@_) { |
226 | if($opt =~ m{^dump_to_dir:(.*)$}) { |
227 | $self->dump_to_dir($1) |
228 | } |
229 | elsif($opt eq 'make_schema_at') { |
230 | no strict 'refs'; |
231 | my $cpkg = (caller)[0]; |
232 | *{"${cpkg}::make_schema_at"} = \&make_schema_at; |
233 | } |
234 | } |
235 | } |
236 | |
237 | =head2 make_schema_at |
238 | |
239 | This simple function allows one to create a Loader-based schema |
240 | in-memory on the fly without any on-disk class files of any |
241 | kind. When used with the C<dump_directory> option, you can |
8f9d7ce5 |
242 | use this to generate a rough draft manual schema from a dsn |
996be9ee |
243 | without the intermediate step of creating a physical Loader-based |
244 | schema class. |
245 | |
483987b9 |
246 | The return value is the input class name. |
247 | |
996be9ee |
248 | This function can be exported/imported by the normal means, as |
249 | illustrated in these Examples: |
250 | |
5223f24a |
251 | # Simple example, creates as a new class 'New::Schema::Name' in |
252 | # memory in the running perl interpreter. |
996be9ee |
253 | use DBIx::Class::Schema::Loader qw/ make_schema_at /; |
254 | make_schema_at( |
255 | 'New::Schema::Name', |
59cfa251 |
256 | { debug => 1 }, |
996be9ee |
257 | [ 'dbi:Pg:dbname="foo"','postgres' ], |
258 | ); |
259 | |
260 | # Complex: dump loaded schema to disk, all from the commandline: |
59cfa251 |
261 | perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib -e 'make_schema_at("New::Schema::Name", { debug => 1 }, [ "dbi:Pg:dbname=foo","postgres" ])' |
996be9ee |
262 | |
263 | # Same, but inside a script, and using a different way to specify the |
264 | # dump directory: |
265 | use DBIx::Class::Schema::Loader qw/ make_schema_at /; |
266 | make_schema_at( |
267 | 'New::Schema::Name', |
59cfa251 |
268 | { debug => 1, dump_directory => './lib' }, |
996be9ee |
269 | [ 'dbi:Pg:dbname="foo"','postgres' ], |
270 | ); |
271 | |
272 | =cut |
273 | |
274 | sub make_schema_at { |
275 | my ($target, $opts, $connect_info) = @_; |
276 | |
483987b9 |
277 | { |
278 | no strict 'refs'; |
279 | @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/; |
280 | } |
281 | |
282 | $target->loader_options($opts); |
283 | $target->connection(@$connect_info); |
996be9ee |
284 | } |
285 | |
b97c2c1e |
286 | =head2 rescan |
287 | |
288 | Re-scans the database for newly added tables since the initial |
289 | load, and adds them to the schema at runtime, including relationships, |
290 | etc. Does not process drops or changes. |
291 | |
a60b5b8d |
292 | Returns a list of the new monikers added. |
293 | |
b97c2c1e |
294 | =cut |
295 | |
a60b5b8d |
296 | sub rescan { my $self = shift; $self->_loader->rescan($self) } |
b97c2c1e |
297 | |
996be9ee |
298 | =head1 EXAMPLE |
299 | |
300 | Using the example in L<DBIx::Class::Manual::ExampleSchema> as a basis |
301 | replace the DB::Main with the following code: |
302 | |
303 | package DB::Main; |
304 | |
305 | use base qw/DBIx::Class::Schema::Loader/; |
306 | |
307 | __PACKAGE__->loader_options( |
996be9ee |
308 | debug => 1, |
309 | ); |
310 | __PACKAGE__->connection('dbi:SQLite:example.db'); |
311 | |
312 | 1; |
313 | |
314 | and remove the Main directory tree (optional). Every thing else |
315 | should work the same |
316 | |
996be9ee |
317 | =head1 KNOWN ISSUES |
318 | |
319 | =head2 Multiple Database Schemas |
320 | |
321 | Currently the loader is limited to working within a single schema |
322 | (using the database vendors' definition of "schema"). If you |
323 | have a multi-schema database with inter-schema relationships (which |
8f9d7ce5 |
324 | is easy to do in PostgreSQL or DB2 for instance), you only get to |
996be9ee |
325 | automatically load the tables of one schema, and any relationships |
326 | to tables in other schemas will be silently ignored. |
327 | |
328 | At some point in the future, an intelligent way around this might be |
329 | devised, probably by allowing the C<db_schema> option to be an |
d65cda9e |
330 | arrayref of schemas to load. |
89ecd854 |
331 | |
996be9ee |
332 | In "normal" L<DBIx::Class::Schema> usage, manually-defined |
333 | source classes and relationships have no problems crossing vendor schemas. |
89ecd854 |
334 | |
a78e3fed |
335 | =head1 AUTHOR |
336 | |
f654c972 |
337 | Brandon Black, C<blblack@gmail.com> |
fbd83464 |
338 | |
8a6b44ef |
339 | Based on L<DBIx::Class::Loader> by Sebastian Riedel |
a78e3fed |
340 | |
341 | Based upon the work of IKEBE Tomohiro |
342 | |
343 | =head1 THANK YOU |
344 | |
d65cda9e |
345 | Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent |
346 | in a bug report or suggestion. |
a78e3fed |
347 | |
348 | =head1 LICENSE |
349 | |
350 | This library is free software; you can redistribute it and/or modify it under |
351 | the same terms as Perl itself. |
352 | |
353 | =head1 SEE ALSO |
354 | |
996be9ee |
355 | L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema> |
a78e3fed |
356 | |
357 | =cut |
358 | |
359 | 1; |