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