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