Commit | Line | Data |
18fca96a |
1 | package DBIx::Class::Schema::Loader; |
a78e3fed |
2 | |
3 | use strict; |
a4a19f3c |
4 | use warnings; |
65e705c3 |
5 | use base qw/DBIx::Class::Schema Class::Accessor::Grouped/; |
fa994d3c |
6 | use Carp::Clan qw/^DBIx::Class/; |
996be9ee |
7 | use Class::C3; |
c9f1d7b0 |
8 | use Scalar::Util qw/ weaken /; |
3980d69c |
9 | |
a4a19f3c |
10 | # Always remember to do all digits for the version even if they're 0 |
11 | # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports |
12 | # brain damage and presumably various other packaging systems too |
0a701ff3 |
13 | our $VERSION = '0.04999_12'; |
457eb8a6 |
14 | |
65e705c3 |
15 | __PACKAGE__->mk_group_accessors('inherited', qw/ |
16 | _loader_args |
17 | dump_to_dir |
18 | _loader_invoked |
19 | _loader |
20 | loader_class |
21 | naming |
a8d229ff |
22 | /); |
65e705c3 |
23 | __PACKAGE__->_loader_args({}); |
a78e3fed |
24 | |
25 | =head1 NAME |
26 | |
18fca96a |
27 | DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema |
a78e3fed |
28 | |
29 | =head1 SYNOPSIS |
30 | |
707fb247 |
31 | ### use this module to generate a set of class files |
32 | |
33 | # in a script |
34 | use DBIx::Class::Schema::Loader qw/ make_schema_at /; |
35 | make_schema_at( |
36 | 'My::Schema', |
37 | { debug => 1, |
38 | dump_directory => './lib', |
39 | }, |
40 | [ 'dbi:Pg:dbname="foo"', 'myuser', 'mypassword' ], |
41 | ); |
42 | |
43 | # from the command line or a shell script with dbicdump (distributed |
44 | # with this module). Do `perldoc dbicdump` for usage. |
45 | dbicdump -o dump_directory=./lib \ |
46 | -o debug=1 \ |
47 | My::Schema \ |
48 | 'dbi:Pg:dbname=foo' \ |
49 | myuser \ |
50 | mypassword |
51 | |
52 | ### or generate and load classes at runtime |
53 | # note: this technique is not recommended |
54 | # for use in production code |
55 | |
a4a19f3c |
56 | package My::Schema; |
57 | use base qw/DBIx::Class::Schema::Loader/; |
a78e3fed |
58 | |
996be9ee |
59 | __PACKAGE__->loader_options( |
996be9ee |
60 | constraint => '^foo.*', |
61 | # debug => 1, |
a78e3fed |
62 | ); |
af6c2665 |
63 | |
707fb247 |
64 | #### in application code elsewhere: |
a78e3fed |
65 | |
a4a19f3c |
66 | use My::Schema; |
a78e3fed |
67 | |
a4a19f3c |
68 | my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs); |
69 | # -or- |
996be9ee |
70 | my $schema1 = "My::Schema"; $schema1->connection(as above); |
074e81cd |
71 | |
996be9ee |
72 | =head1 DESCRIPTION |
074e81cd |
73 | |
fbd83464 |
74 | DBIx::Class::Schema::Loader automates the definition of a |
996be9ee |
75 | L<DBIx::Class::Schema> by scanning database table definitions and |
d65cda9e |
76 | setting up the columns, primary keys, and relationships. |
a78e3fed |
77 | |
d65cda9e |
78 | DBIx::Class::Schema::Loader currently supports only the DBI storage type. |
3fe9c5d9 |
79 | It has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>, |
80 | L<DBD::SQLite>, and L<DBD::Oracle>. Other DBI drivers may function to |
81 | a greater or lesser degree with this loader, depending on how much of the |
82 | DBI spec they implement, and how standard their implementation is. |
83 | |
84 | Patches to make other DBDs work correctly welcome. |
a78e3fed |
85 | |
996be9ee |
86 | See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing |
87 | your own vendor-specific subclass for an unsupported DBD driver. |
a78e3fed |
88 | |
3fe9c5d9 |
89 | This module requires L<DBIx::Class> 0.07006 or later, and obsoletes |
996be9ee |
90 | the older L<DBIx::Class::Loader>. |
89ecd854 |
91 | |
996be9ee |
92 | This module is designed more to get you up and running quickly against |
93 | an existing database, or to be effective for simple situations, rather |
94 | than to be what you use in the long term for a complex database/project. |
89ecd854 |
95 | |
96 | That being said, transitioning your code from a Schema generated by this |
97 | module to one that doesn't use this module should be straightforward and |
59cfa251 |
98 | painless, so don't shy away from it just for fears of the transition down |
99 | the road. |
89ecd854 |
100 | |
a78e3fed |
101 | =head1 METHODS |
102 | |
29ddb54c |
103 | =head2 loader_class |
104 | |
530e0bf6 |
105 | =over 4 |
106 | |
107 | =item Argument: $loader_class |
108 | |
109 | =back |
110 | |
29ddb54c |
111 | Set the loader class to be instantiated when L</connection> is called. |
112 | If the classname starts with "::", "DBIx::Class::Schema::Loader" is |
113 | prepended. Defaults to L<DBIx::Class::Schema/storage_type> (which must |
114 | start with "::" when using L<DBIx::Class::Schema::Loader>). |
115 | |
116 | This is mostly useful for subclassing existing loaders or in conjunction |
117 | with L</dump_to_dir>. |
118 | |
996be9ee |
119 | =head2 loader_options |
a78e3fed |
120 | |
530e0bf6 |
121 | =over 4 |
122 | |
123 | =item Argument: \%loader_options |
124 | |
125 | =back |
126 | |
996be9ee |
127 | Example in Synopsis above demonstrates a few common arguments. For |
128 | detailed information on all of the arguments, most of which are |
129 | only useful in fairly complex scenarios, see the |
130 | L<DBIx::Class::Schema::Loader::Base> documentation. |
a78e3fed |
131 | |
3fe9c5d9 |
132 | If you intend to use C<loader_options>, you must call |
133 | C<loader_options> before any connection is made, or embed the |
134 | C<loader_options> in the connection information itself as shown |
135 | below. Setting C<loader_options> after the connection has |
59cfa251 |
136 | already been made is useless. |
a78e3fed |
137 | |
996be9ee |
138 | =cut |
1031d4f6 |
139 | |
996be9ee |
140 | sub loader_options { |
141 | my $self = shift; |
65e705c3 |
142 | |
d65cda9e |
143 | my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_; |
996be9ee |
144 | $self->_loader_args(\%args); |
996be9ee |
145 | |
146 | $self; |
147 | } |
148 | |
149 | sub _invoke_loader { |
150 | my $self = shift; |
151 | my $class = ref $self || $self; |
152 | |
59cfa251 |
153 | my $args = $self->_loader_args; |
154 | |
155 | # set up the schema/schema_class arguments |
156 | $args->{schema} = $self; |
157 | $args->{schema_class} = $class; |
158 | weaken($args->{schema}) if ref $self; |
159 | $args->{dump_directory} ||= $self->dump_to_dir; |
a0e0a56a |
160 | $args->{naming} = $self->naming if $self->naming; |
af6c2665 |
161 | |
996be9ee |
162 | # XXX this only works for relative storage_type, like ::DBI ... |
29ddb54c |
163 | my $impl = $self->loader_class |
3953cbee |
164 | || "DBIx::Class::Schema::Loader" . $self->storage_type; |
29ddb54c |
165 | $impl = "DBIx::Class::Schema::Loader${impl}" if $impl =~ /^::/; |
6ae3f335 |
166 | eval { $self->ensure_class_loaded($impl) }; |
167 | croak qq/Could not load storage_type loader "$impl": "$@"/ if $@; |
af6c2665 |
168 | |
b97c2c1e |
169 | $self->_loader($impl->new(%$args)); |
170 | $self->_loader->load; |
59cfa251 |
171 | $self->_loader_invoked(1); |
996be9ee |
172 | |
996be9ee |
173 | $self; |
174 | } |
175 | |
176 | =head2 connection |
177 | |
530e0bf6 |
178 | =over 4 |
179 | |
180 | =item Arguments: @args |
181 | |
182 | =item Return Value: $new_schema |
183 | |
184 | =back |
185 | |
186 | See L<DBIx::Class::Schema/connection> for basic usage. |
d65cda9e |
187 | |
29ddb54c |
188 | If the final argument is a hashref, and it contains the keys C<loader_options> |
189 | or C<loader_class>, those keys will be deleted, and their values value will be |
190 | used for the loader options or class, respectively, just as if set via the |
191 | L</loader_options> or L</loader_class> methods above. |
d65cda9e |
192 | |
193 | The actual auto-loading operation (the heart of this module) will be invoked |
194 | as soon as the connection information is defined. |
996be9ee |
195 | |
196 | =cut |
197 | |
198 | sub connection { |
d65cda9e |
199 | my $self = shift; |
200 | |
201 | if($_[-1] && ref $_[-1] eq 'HASH') { |
17ca645f |
202 | for my $option (qw/ loader_class loader_options result_base_class schema_base_class/) { |
29ddb54c |
203 | if(my $value = delete $_[-1]->{$option}) { |
204 | $self->$option($value); |
205 | } |
d65cda9e |
206 | } |
29ddb54c |
207 | pop @_ if !keys %{$_[-1]}; |
d65cda9e |
208 | } |
209 | |
210 | $self = $self->next::method(@_); |
996be9ee |
211 | |
212 | my $class = ref $self || $self; |
59cfa251 |
213 | if(!$class->_loader_invoked) { |
fa994d3c |
214 | $self->_invoke_loader |
215 | } |
996be9ee |
216 | |
217 | return $self; |
218 | } |
219 | |
220 | =head2 clone |
221 | |
530e0bf6 |
222 | See L<DBIx::Class::Schema/clone>. |
996be9ee |
223 | |
224 | =cut |
225 | |
226 | sub clone { |
227 | my $self = shift; |
228 | |
229 | my $clone = $self->next::method(@_); |
230 | |
fa994d3c |
231 | if($clone->_loader_args) { |
232 | $clone->_loader_args->{schema} = $clone; |
233 | weaken($clone->_loader_args->{schema}); |
234 | } |
996be9ee |
235 | |
236 | $clone; |
237 | } |
238 | |
239 | =head2 dump_to_dir |
240 | |
530e0bf6 |
241 | =over 4 |
242 | |
243 | =item Argument: $directory |
244 | |
245 | =back |
996be9ee |
246 | |
247 | Calling this as a class method on either L<DBIx::Class::Schema::Loader> |
707fb247 |
248 | or any derived schema class will cause all schemas to dump |
996be9ee |
249 | manual versions of themselves to the named directory when they are |
250 | loaded. In order to be effective, this must be set before defining a |
251 | connection on this schema class or any derived object (as the loading |
074e81cd |
252 | happens as soon as both a connection and loader_options are set, and |
253 | only once per class). |
996be9ee |
254 | |
255 | See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more |
256 | details on the dumping mechanism. |
257 | |
258 | This can also be set at module import time via the import option |
259 | C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where |
260 | C</foo/bar> is the target directory. |
261 | |
262 | Examples: |
263 | |
264 | # My::Schema isa DBIx::Class::Schema::Loader, and has connection info |
265 | # hardcoded in the class itself: |
266 | perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1 |
267 | |
268 | # Same, but no hard-coded connection, so we must provide one: |
269 | perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)' |
270 | |
271 | # Or as a class method, as long as you get it done *before* defining a |
272 | # connection on this schema class or any derived object: |
273 | use My::Schema; |
274 | My::Schema->dump_to_dir('/foo/bar'); |
275 | My::Schema->connection(........); |
276 | |
277 | # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all |
278 | # derived schemas |
279 | use My::Schema; |
280 | use My::OtherSchema; |
281 | DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar'); |
282 | My::Schema->connection(.......); |
283 | My::OtherSchema->connection(.......); |
284 | |
285 | # Another alternative to the above: |
286 | use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |; |
287 | use My::Schema; |
288 | use My::OtherSchema; |
289 | My::Schema->connection(.......); |
290 | My::OtherSchema->connection(.......); |
291 | |
292 | =cut |
293 | |
294 | sub import { |
295 | my $self = shift; |
a8d229ff |
296 | |
996be9ee |
297 | return if !@_; |
a8d229ff |
298 | |
299 | my $cpkg = (caller)[0]; |
300 | |
996be9ee |
301 | foreach my $opt (@_) { |
302 | if($opt =~ m{^dump_to_dir:(.*)$}) { |
303 | $self->dump_to_dir($1) |
304 | } |
305 | elsif($opt eq 'make_schema_at') { |
306 | no strict 'refs'; |
996be9ee |
307 | *{"${cpkg}::make_schema_at"} = \&make_schema_at; |
308 | } |
a8d229ff |
309 | elsif($opt eq 'naming') { |
310 | no strict 'refs'; |
311 | *{"${cpkg}::naming"} = sub { $self->naming(@_) }; |
312 | } |
996be9ee |
313 | } |
314 | } |
315 | |
316 | =head2 make_schema_at |
317 | |
530e0bf6 |
318 | =over 4 |
319 | |
707fb247 |
320 | =item Arguments: $schema_class_name, \%loader_options, \@connect_info |
530e0bf6 |
321 | |
707fb247 |
322 | =item Return Value: $schema_class_name |
530e0bf6 |
323 | |
324 | =back |
325 | |
707fb247 |
326 | This function creates a DBIx::Class schema from an existing RDBMS |
327 | schema. With the C<dump_directory> option, generates a set of |
328 | DBIx::Class classes from an existing database schema read from the |
329 | given dsn. Without a C<dump_directory>, creates schema classes in |
330 | memory at runtime without generating on-disk class files. |
996be9ee |
331 | |
707fb247 |
332 | For a complete list of supported loader_options, see |
333 | L<DBIx::Class::Schema::Loader::Base> |
483987b9 |
334 | |
707fb247 |
335 | This function can be imported in the usual way, as illustrated in |
336 | these Examples: |
996be9ee |
337 | |
5223f24a |
338 | # Simple example, creates as a new class 'New::Schema::Name' in |
339 | # memory in the running perl interpreter. |
996be9ee |
340 | use DBIx::Class::Schema::Loader qw/ make_schema_at /; |
341 | make_schema_at( |
342 | 'New::Schema::Name', |
59cfa251 |
343 | { debug => 1 }, |
996be9ee |
344 | [ 'dbi:Pg:dbname="foo"','postgres' ], |
345 | ); |
346 | |
707fb247 |
347 | # Inside a script, specifying a dump directory in which to write |
348 | # class files |
996be9ee |
349 | use DBIx::Class::Schema::Loader qw/ make_schema_at /; |
350 | make_schema_at( |
351 | 'New::Schema::Name', |
59cfa251 |
352 | { debug => 1, dump_directory => './lib' }, |
996be9ee |
353 | [ 'dbi:Pg:dbname="foo"','postgres' ], |
354 | ); |
355 | |
356 | =cut |
357 | |
358 | sub make_schema_at { |
359 | my ($target, $opts, $connect_info) = @_; |
360 | |
483987b9 |
361 | { |
362 | no strict 'refs'; |
363 | @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/; |
364 | } |
365 | |
366 | $target->loader_options($opts); |
367 | $target->connection(@$connect_info); |
996be9ee |
368 | } |
369 | |
b97c2c1e |
370 | =head2 rescan |
371 | |
530e0bf6 |
372 | =over 4 |
373 | |
374 | =item Return Value: @new_monikers |
375 | |
376 | =back |
377 | |
b97c2c1e |
378 | Re-scans the database for newly added tables since the initial |
379 | load, and adds them to the schema at runtime, including relationships, |
380 | etc. Does not process drops or changes. |
381 | |
a60b5b8d |
382 | Returns a list of the new monikers added. |
383 | |
b97c2c1e |
384 | =cut |
385 | |
a60b5b8d |
386 | sub rescan { my $self = shift; $self->_loader->rescan($self) } |
b97c2c1e |
387 | |
a8d229ff |
388 | =head2 naming |
389 | |
390 | =over 4 |
391 | |
392 | =item Arguments: \%opts | $ver |
393 | |
394 | =back |
395 | |
396 | Controls the naming options for backward compatibility, see |
397 | L<DBIx::Class::Schema::Loader::Base/naming> for details. |
398 | |
399 | To upgrade a dynamic schema, use: |
400 | |
401 | __PACKAGE__->naming('current'); |
402 | |
403 | Can be imported into your dump script and called as a function as well: |
404 | |
405 | naming('v4'); |
996be9ee |
406 | |
996be9ee |
407 | =head1 KNOWN ISSUES |
408 | |
409 | =head2 Multiple Database Schemas |
410 | |
411 | Currently the loader is limited to working within a single schema |
707fb247 |
412 | (using the underlying RDBMS's definition of "schema"). If you have a |
413 | multi-schema database with inter-schema relationships (which is easy |
414 | to do in PostgreSQL or DB2 for instance), you currently can only |
415 | automatically load the tables of one schema, and relationships to |
416 | tables in other schemas will be silently ignored. |
996be9ee |
417 | |
418 | At some point in the future, an intelligent way around this might be |
419 | devised, probably by allowing the C<db_schema> option to be an |
d65cda9e |
420 | arrayref of schemas to load. |
89ecd854 |
421 | |
996be9ee |
422 | In "normal" L<DBIx::Class::Schema> usage, manually-defined |
423 | source classes and relationships have no problems crossing vendor schemas. |
89ecd854 |
424 | |
be80bba7 |
425 | =head1 ACKNOWLEDGEMENTS |
a78e3fed |
426 | |
be80bba7 |
427 | Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent |
428 | in a bug report or suggestion. |
fbd83464 |
429 | |
8a6b44ef |
430 | Based on L<DBIx::Class::Loader> by Sebastian Riedel |
a78e3fed |
431 | |
432 | Based upon the work of IKEBE Tomohiro |
433 | |
be80bba7 |
434 | =head1 AUTHOR |
a78e3fed |
435 | |
be80bba7 |
436 | blblack: Brandon Black <blblack@gmail.com> |
437 | |
438 | =head1 CONTRIBUTORS |
439 | |
440 | ilmarii: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> |
441 | |
442 | arcanez: Justin Hunter <justin.d.hunter@gmail.com> |
443 | |
444 | ash: Ash Berlin <ash@cpan.org> |
445 | |
446 | Caelum: Rafael Kitover <rkitover@cpan.org> |
447 | |
448 | TSUNODA Kazuya <drk@drk7.jp> |
449 | |
450 | Robert Bohne <rbo@openserv.org> |
451 | |
1f625792 |
452 | ribasushi: Peter Rabbitson <rabbit+dbic@rabbit.us> |
453 | |
fdd8ff16 |
454 | gugu: Andrey Kostenko <a.kostenko@rambler-co.ru> |
455 | |
65e705c3 |
456 | jhannah: Jay Hannah <jay@jays.net> |
457 | |
be80bba7 |
458 | ... and lots of other folks. If we forgot you, please write the current |
459 | maintainer or RT. |
a78e3fed |
460 | |
9cc8e7e1 |
461 | =head1 COPYRIGHT & LICENSE |
462 | |
463 | Copyright (c) 2006 - 2009 by the aforementioned |
464 | L<DBIx::Class::Schema::Loader/AUTHOR> and |
465 | L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
a78e3fed |
466 | |
467 | This library is free software; you can redistribute it and/or modify it under |
468 | the same terms as Perl itself. |
469 | |
470 | =head1 SEE ALSO |
471 | |
996be9ee |
472 | L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema> |
a78e3fed |
473 | |
474 | =cut |
475 | |
476 | 1; |