Commit | Line | Data |
18fca96a |
1 | package DBIx::Class::Schema::Loader; |
a78e3fed |
2 | |
3 | use strict; |
a4a19f3c |
4 | use warnings; |
8a6b44ef |
5 | use base qw/DBIx::Class::Schema/; |
6 | use base qw/Class::Data::Accessor/; |
fa994d3c |
7 | use Carp::Clan qw/^DBIx::Class/; |
457eb8a6 |
8 | use UNIVERSAL::require; |
996be9ee |
9 | use Class::C3; |
c9f1d7b0 |
10 | use Scalar::Util qw/ weaken /; |
3980d69c |
11 | |
a4a19f3c |
12 | # Always remember to do all digits for the version even if they're 0 |
13 | # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports |
14 | # brain damage and presumably various other packaging systems too |
518472fa |
15 | our $VERSION = '0.03010'; |
457eb8a6 |
16 | |
996be9ee |
17 | __PACKAGE__->mk_classaccessor('dump_to_dir'); |
457eb8a6 |
18 | __PACKAGE__->mk_classaccessor('loader'); |
996be9ee |
19 | __PACKAGE__->mk_classaccessor('_loader_args'); |
a78e3fed |
20 | |
21 | =head1 NAME |
22 | |
18fca96a |
23 | DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema |
a78e3fed |
24 | |
25 | =head1 SYNOPSIS |
26 | |
a4a19f3c |
27 | package My::Schema; |
28 | use base qw/DBIx::Class::Schema::Loader/; |
a78e3fed |
29 | |
996be9ee |
30 | __PACKAGE__->loader_options( |
31 | relationships => 1, |
32 | constraint => '^foo.*', |
33 | # debug => 1, |
a78e3fed |
34 | ); |
af6c2665 |
35 | |
a4a19f3c |
36 | # in seperate application code ... |
a78e3fed |
37 | |
a4a19f3c |
38 | use My::Schema; |
a78e3fed |
39 | |
a4a19f3c |
40 | my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs); |
41 | # -or- |
996be9ee |
42 | my $schema1 = "My::Schema"; $schema1->connection(as above); |
074e81cd |
43 | |
996be9ee |
44 | =head1 DESCRIPTION |
074e81cd |
45 | |
fbd83464 |
46 | DBIx::Class::Schema::Loader automates the definition of a |
996be9ee |
47 | L<DBIx::Class::Schema> by scanning database table definitions and |
d65cda9e |
48 | setting up the columns, primary keys, and relationships. |
a78e3fed |
49 | |
d65cda9e |
50 | DBIx::Class::Schema::Loader currently supports only the DBI storage type. |
51 | It has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>, and |
52 | L<DBD::SQLite>. Other DBI drivers may function to a greater or lesser |
53 | degree with this loader, depending on how much of the DBI spec they |
54 | implement, and how standard their implementation is. Patches to make |
55 | other DBDs work correctly welcome. |
a78e3fed |
56 | |
996be9ee |
57 | See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing |
58 | your own vendor-specific subclass for an unsupported DBD driver. |
a78e3fed |
59 | |
996be9ee |
60 | This module requires L<DBIx::Class> 0.06 or later, and obsoletes |
61 | the older L<DBIx::Class::Loader>. |
89ecd854 |
62 | |
996be9ee |
63 | This module is designed more to get you up and running quickly against |
64 | an existing database, or to be effective for simple situations, rather |
65 | than to be what you use in the long term for a complex database/project. |
89ecd854 |
66 | |
67 | That being said, transitioning your code from a Schema generated by this |
68 | module to one that doesn't use this module should be straightforward and |
996be9ee |
69 | painless (as long as you're not using any methods that are now deprecated |
70 | in this document), so don't shy away from it just for fears of the |
71 | transition down the road. |
89ecd854 |
72 | |
a78e3fed |
73 | =head1 METHODS |
74 | |
996be9ee |
75 | =head2 loader_options |
a78e3fed |
76 | |
996be9ee |
77 | Example in Synopsis above demonstrates a few common arguments. For |
78 | detailed information on all of the arguments, most of which are |
79 | only useful in fairly complex scenarios, see the |
80 | L<DBIx::Class::Schema::Loader::Base> documentation. |
a78e3fed |
81 | |
d65cda9e |
82 | This method is *required* at this time, for backwards compatibility |
83 | reasons. If you do not wish to change any options, just call it |
84 | with an empty argument list during schema class initialization. |
85 | |
fa994d3c |
86 | Setting these options explicitly via this method B<after> calling |
87 | C<connection> is deprecated and will stop working in version 0.04000. |
88 | For now the code merely warns about this condition. |
89 | |
90 | The preferred way of doing things is to either call C<loader_options> |
91 | before any connection is made, or embed the C<loader_options> in |
92 | the connection information itself as shown below. |
a78e3fed |
93 | |
996be9ee |
94 | =cut |
1031d4f6 |
95 | |
996be9ee |
96 | sub loader_options { |
97 | my $self = shift; |
98 | |
d65cda9e |
99 | my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_; |
1031d4f6 |
100 | |
996be9ee |
101 | my $class = ref $self || $self; |
102 | $args{schema} = $self; |
103 | $args{schema_class} = $class; |
c9f1d7b0 |
104 | weaken($args{schema}) if ref $self; |
105 | |
996be9ee |
106 | $self->_loader_args(\%args); |
d65cda9e |
107 | if($self->storage && !$class->loader) { |
fa994d3c |
108 | warn "Do not set loader_options after specifying the connection info," |
109 | . " this will be unsupported in 0.04000"; |
d65cda9e |
110 | $self->_invoke_loader; |
111 | } |
996be9ee |
112 | |
113 | $self; |
114 | } |
115 | |
116 | sub _invoke_loader { |
117 | my $self = shift; |
118 | my $class = ref $self || $self; |
119 | |
120 | $self->_loader_args->{dump_directory} ||= $self->dump_to_dir; |
af6c2665 |
121 | |
996be9ee |
122 | # XXX this only works for relative storage_type, like ::DBI ... |
123 | my $impl = "DBIx::Class::Schema::Loader" . $self->storage_type; |
a78e3fed |
124 | $impl->require or |
996be9ee |
125 | croak qq/Could not load storage_type loader "$impl": / . |
3385ac62 |
126 | qq/"$UNIVERSAL::require::ERROR"/; |
af6c2665 |
127 | |
996be9ee |
128 | # XXX in the future when we get rid of ->loader, the next two |
129 | # lines can be replaced by "$impl->new(%{$self->_loader_args})->load;" |
130 | $class->loader($impl->new(%{$self->_loader_args})); |
2a4b8262 |
131 | $class->loader->load; |
996be9ee |
132 | |
996be9ee |
133 | $self; |
134 | } |
135 | |
136 | =head2 connection |
137 | |
d65cda9e |
138 | See L<DBIx::Class::Schema> for basic usage. |
139 | |
140 | If the final argument is a hashref, and it contains a key C<loader_options>, |
141 | that key will be deleted, and its value will be used for the loader options, |
142 | just as if set via the L</loader_options> method above. |
143 | |
144 | The actual auto-loading operation (the heart of this module) will be invoked |
145 | as soon as the connection information is defined. |
996be9ee |
146 | |
147 | =cut |
148 | |
149 | sub connection { |
d65cda9e |
150 | my $self = shift; |
151 | |
152 | if($_[-1] && ref $_[-1] eq 'HASH') { |
153 | if(my $loader_opts = delete $_[-1]->{loader_options}) { |
154 | $self->loader_options($loader_opts); |
fd64d90d |
155 | pop @_ if !keys %{$_[-1]}; |
d65cda9e |
156 | } |
157 | } |
158 | |
159 | $self = $self->next::method(@_); |
996be9ee |
160 | |
161 | my $class = ref $self || $self; |
fa994d3c |
162 | if($self->_loader_args && !$class->loader) { |
163 | $self->_invoke_loader |
164 | } |
996be9ee |
165 | |
166 | return $self; |
167 | } |
168 | |
169 | =head2 clone |
170 | |
074e81cd |
171 | See L<DBIx::Class::Schema>. |
996be9ee |
172 | |
173 | =cut |
174 | |
175 | sub clone { |
176 | my $self = shift; |
177 | |
178 | my $clone = $self->next::method(@_); |
179 | |
fa994d3c |
180 | if($clone->_loader_args) { |
181 | $clone->_loader_args->{schema} = $clone; |
182 | weaken($clone->_loader_args->{schema}); |
183 | } |
996be9ee |
184 | |
185 | $clone; |
186 | } |
187 | |
188 | =head2 dump_to_dir |
189 | |
190 | Argument: directory name. |
191 | |
192 | Calling this as a class method on either L<DBIx::Class::Schema::Loader> |
193 | or any derived schema class will cause all affected schemas to dump |
194 | manual versions of themselves to the named directory when they are |
195 | loaded. In order to be effective, this must be set before defining a |
196 | connection on this schema class or any derived object (as the loading |
074e81cd |
197 | happens as soon as both a connection and loader_options are set, and |
198 | only once per class). |
996be9ee |
199 | |
200 | See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more |
201 | details on the dumping mechanism. |
202 | |
203 | This can also be set at module import time via the import option |
204 | C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where |
205 | C</foo/bar> is the target directory. |
206 | |
207 | Examples: |
208 | |
209 | # My::Schema isa DBIx::Class::Schema::Loader, and has connection info |
210 | # hardcoded in the class itself: |
211 | perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1 |
212 | |
213 | # Same, but no hard-coded connection, so we must provide one: |
214 | perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)' |
215 | |
216 | # Or as a class method, as long as you get it done *before* defining a |
217 | # connection on this schema class or any derived object: |
218 | use My::Schema; |
219 | My::Schema->dump_to_dir('/foo/bar'); |
220 | My::Schema->connection(........); |
221 | |
222 | # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all |
223 | # derived schemas |
224 | use My::Schema; |
225 | use My::OtherSchema; |
226 | DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar'); |
227 | My::Schema->connection(.......); |
228 | My::OtherSchema->connection(.......); |
229 | |
230 | # Another alternative to the above: |
231 | use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |; |
232 | use My::Schema; |
233 | use My::OtherSchema; |
234 | My::Schema->connection(.......); |
235 | My::OtherSchema->connection(.......); |
236 | |
237 | =cut |
238 | |
239 | sub import { |
240 | my $self = shift; |
241 | return if !@_; |
242 | foreach my $opt (@_) { |
243 | if($opt =~ m{^dump_to_dir:(.*)$}) { |
244 | $self->dump_to_dir($1) |
245 | } |
246 | elsif($opt eq 'make_schema_at') { |
247 | no strict 'refs'; |
248 | my $cpkg = (caller)[0]; |
249 | *{"${cpkg}::make_schema_at"} = \&make_schema_at; |
250 | } |
251 | } |
252 | } |
253 | |
254 | =head2 make_schema_at |
255 | |
256 | This simple function allows one to create a Loader-based schema |
257 | in-memory on the fly without any on-disk class files of any |
258 | kind. When used with the C<dump_directory> option, you can |
8f9d7ce5 |
259 | use this to generate a rough draft manual schema from a dsn |
996be9ee |
260 | without the intermediate step of creating a physical Loader-based |
261 | schema class. |
262 | |
483987b9 |
263 | The return value is the input class name. |
264 | |
996be9ee |
265 | This function can be exported/imported by the normal means, as |
266 | illustrated in these Examples: |
267 | |
5223f24a |
268 | # Simple example, creates as a new class 'New::Schema::Name' in |
269 | # memory in the running perl interpreter. |
996be9ee |
270 | use DBIx::Class::Schema::Loader qw/ make_schema_at /; |
271 | make_schema_at( |
272 | 'New::Schema::Name', |
273 | { relationships => 1, debug => 1 }, |
274 | [ 'dbi:Pg:dbname="foo"','postgres' ], |
275 | ); |
276 | |
277 | # Complex: dump loaded schema to disk, all from the commandline: |
5223f24a |
278 | perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib -e 'make_schema_at("New::Schema::Name", { relationships => 1 }, [ "dbi:Pg:dbname=foo","postgres" ])' |
996be9ee |
279 | |
280 | # Same, but inside a script, and using a different way to specify the |
281 | # dump directory: |
282 | use DBIx::Class::Schema::Loader qw/ make_schema_at /; |
283 | make_schema_at( |
284 | 'New::Schema::Name', |
285 | { relationships => 1, debug => 1, dump_directory => './lib' }, |
286 | [ 'dbi:Pg:dbname="foo"','postgres' ], |
287 | ); |
288 | |
289 | =cut |
290 | |
291 | sub make_schema_at { |
292 | my ($target, $opts, $connect_info) = @_; |
293 | |
483987b9 |
294 | { |
295 | no strict 'refs'; |
296 | @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/; |
297 | } |
298 | |
299 | $target->loader_options($opts); |
300 | $target->connection(@$connect_info); |
996be9ee |
301 | } |
302 | |
303 | =head1 EXAMPLE |
304 | |
305 | Using the example in L<DBIx::Class::Manual::ExampleSchema> as a basis |
306 | replace the DB::Main with the following code: |
307 | |
308 | package DB::Main; |
309 | |
310 | use base qw/DBIx::Class::Schema::Loader/; |
311 | |
312 | __PACKAGE__->loader_options( |
313 | relationships => 1, |
314 | debug => 1, |
315 | ); |
316 | __PACKAGE__->connection('dbi:SQLite:example.db'); |
317 | |
318 | 1; |
319 | |
320 | and remove the Main directory tree (optional). Every thing else |
321 | should work the same |
322 | |
323 | =head1 DEPRECATED METHODS |
324 | |
325 | You don't need to read anything in this section unless you're upgrading |
326 | code that was written against pre-0.03 versions of this module. This |
327 | version is intended to be backwards-compatible with pre-0.03 code, but |
328 | will issue warnings about your usage of deprecated features/methods. |
329 | |
d65cda9e |
330 | B<All of these deprecated methods will dissappear in version 0.04000>, |
331 | and converting code that uses these methods should be trivial. |
332 | |
996be9ee |
333 | =head2 load_from_connection |
334 | |
335 | This deprecated method is now roughly an alias for L</loader_options>. |
336 | |
996be9ee |
337 | For now, using this method will invoke the legacy behavior for |
338 | backwards compatibility, and merely emit a warning about upgrading |
339 | your code. |
340 | |
341 | It also reverts the default inflection scheme to |
342 | use L<Lingua::EN::Inflect> just like pre-0.03 versions of this |
343 | module did. |
344 | |
345 | You can force these legacy inflections with the |
d65cda9e |
346 | option L<DBIx::Class::Schema::Loader::Base/legacy_default_inflections>, |
347 | even after switch over to the preferred L</loader_options> way of doing |
348 | things. That option will not go away until at least 0.05. |
996be9ee |
349 | |
350 | See the source of this method for more details. |
351 | |
352 | =cut |
353 | |
354 | sub load_from_connection { |
355 | my ($self, %args) = @_; |
8f9d7ce5 |
356 | |
357 | my $cmds_ver = $Catalyst::Model::DBIC::Schema::VERSION; |
358 | if($cmds_ver) { |
359 | if($cmds_ver < 0.14) { |
360 | warn 'You should upgrade your installation of' |
361 | . ' Catalyst::Model::DBIC::Schema to 0.14 or higher, then:'; |
362 | } |
363 | warn 'You should regenerate your Model files, which may eliminate' |
364 | . ' the following deprecation warning:'; |
365 | } |
d65cda9e |
366 | warn 'load_from_connection deprecated, and will dissappear in 0.04000, ' |
367 | . 'please [re-]read the [new] DBIx::Class::Schema::Loader ' |
368 | . 'documentation'; |
996be9ee |
369 | |
370 | # Support the old connect_info / dsn / etc args... |
371 | $args{connect_info} = [ |
372 | delete $args{dsn}, |
373 | delete $args{user}, |
374 | delete $args{password}, |
375 | delete $args{options}, |
376 | ] if $args{dsn}; |
377 | |
378 | $self->connection(@{delete $args{connect_info}}) |
379 | if $args{connect_info}; |
380 | |
381 | $self->loader_options('legacy_default_inflections' => 1, %args); |
a78e3fed |
382 | } |
383 | |
457eb8a6 |
384 | =head2 loader |
385 | |
386 | This is an accessor in the generated Schema class for accessing |
996be9ee |
387 | the L<DBIx::Class::Schema::Loader::Base> -based loader object |
457eb8a6 |
388 | that was used during construction. See the |
996be9ee |
389 | L<DBIx::Class::Schema::Loader::Base> docs for more information |
457eb8a6 |
390 | on the available loader methods there. |
391 | |
996be9ee |
392 | This accessor is deprecated. Do not use it. Anything you can |
393 | get from C<loader>, you can get via the normal L<DBIx::Class::Schema> |
394 | methods, and your code will be more robust and forward-thinking |
395 | for doing so. |
396 | |
397 | If you're already using C<loader> in your code, make an effort |
398 | to get rid of it. If you think you've found a situation where it |
8f9d7ce5 |
399 | is necessary, let me know and we'll see what we can do to remedy |
996be9ee |
400 | that situation. |
401 | |
402 | In some future version, this accessor *will* disappear. It was |
403 | apparently quite a design/API mistake to ever have exposed it to |
404 | user-land in the first place, all things considered. |
405 | |
406 | =head1 KNOWN ISSUES |
407 | |
408 | =head2 Multiple Database Schemas |
409 | |
410 | Currently the loader is limited to working within a single schema |
411 | (using the database vendors' definition of "schema"). If you |
412 | have a multi-schema database with inter-schema relationships (which |
8f9d7ce5 |
413 | is easy to do in PostgreSQL or DB2 for instance), you only get to |
996be9ee |
414 | automatically load the tables of one schema, and any relationships |
415 | to tables in other schemas will be silently ignored. |
416 | |
417 | At some point in the future, an intelligent way around this might be |
418 | devised, probably by allowing the C<db_schema> option to be an |
d65cda9e |
419 | arrayref of schemas to load. |
89ecd854 |
420 | |
996be9ee |
421 | In "normal" L<DBIx::Class::Schema> usage, manually-defined |
422 | source classes and relationships have no problems crossing vendor schemas. |
89ecd854 |
423 | |
a78e3fed |
424 | =head1 AUTHOR |
425 | |
f654c972 |
426 | Brandon Black, C<blblack@gmail.com> |
fbd83464 |
427 | |
8a6b44ef |
428 | Based on L<DBIx::Class::Loader> by Sebastian Riedel |
a78e3fed |
429 | |
430 | Based upon the work of IKEBE Tomohiro |
431 | |
432 | =head1 THANK YOU |
433 | |
d65cda9e |
434 | Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent |
435 | in a bug report or suggestion. |
a78e3fed |
436 | |
437 | =head1 LICENSE |
438 | |
439 | This library is free software; you can redistribute it and/or modify it under |
440 | the same terms as Perl itself. |
441 | |
442 | =head1 SEE ALSO |
443 | |
996be9ee |
444 | L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema> |
a78e3fed |
445 | |
446 | =cut |
447 | |
448 | 1; |