1 package DBIx::Class::Schema::Loader;
5 use base qw/DBIx::Class::Schema Class::Accessor::Grouped/;
7 use Carp::Clan qw/^DBIx::Class/;
8 use Scalar::Util 'weaken';
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
14 our $VERSION = '0.07010';
16 __PACKAGE__->mk_group_accessors('inherited', qw/
25 __PACKAGE__->_loader_args({});
29 DBIx::Class::Schema::Loader - Create a DBIx::Class::Schema based on a database
33 ### use this module to generate a set of class files
36 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
40 dump_directory => './lib',
42 [ 'dbi:Pg:dbname="foo"', 'myuser', 'mypassword',
43 { loader_class => 'MyLoader' } # optionally
47 # from the command line or a shell script with dbicdump (distributed
48 # with this module). Do `perldoc dbicdump` for usage.
49 dbicdump -o dump_directory=./lib \
50 -o components='["InflateColumn::DateTime"]' \
57 ### or generate and load classes at runtime
58 # note: this technique is not recommended
59 # for use in production code
62 use base qw/DBIx::Class::Schema::Loader/;
64 __PACKAGE__->loader_options(
65 constraint => '^foo.*',
69 #### in application code elsewhere:
73 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
75 my $schema1 = "My::Schema"; $schema1->connection(as above);
79 DBIx::Class::Schema::Loader automates the definition of a
80 L<DBIx::Class::Schema> by scanning database table definitions and setting up
81 the columns, primary keys, unique constraints and relationships.
83 See L<dbicdump> for the C<dbicdump> utility.
85 DBIx::Class::Schema::Loader currently supports only the DBI storage type. It
86 has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>,
87 L<DBD::Firebird>, L<DBD::InterBase>, L<DBD::Informix>, L<DBD::SQLAnywhere>,
88 L<DBD::SQLite>, L<DBD::Sybase> (for Sybase ASE and MSSSQL), L<DBD::ODBC> (for
89 MSSQL, MSAccess, Firebird and SQL Anywhere) L<DBD::ADO> (for MSSQL and
90 MSAccess) and L<DBD::Oracle>. Other DBI drivers may function to a greater or
91 lesser degree with this loader, depending on how much of the DBI spec they
92 implement, and how standard their implementation is.
94 Patches to make other DBDs work correctly welcome.
96 See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
97 your own vendor-specific subclass for an unsupported DBD driver.
99 This module requires L<DBIx::Class> 0.08127 or later, and obsoletes the older
100 L<DBIx::Class::Loader>.
102 See L<DBIx::Class::Schema::Loader::Base> for available options.
108 The loader object, as class data on your Schema. For methods available see
109 L<DBIx::Class::Schema::Loader::Base> and L<DBIx::Class::Schema::Loader::DBI>.
122 =item Argument: $loader_class
126 Set the loader class to be instantiated when L</connection> is called.
127 If the classname starts with "::", "DBIx::Class::Schema::Loader" is
128 prepended. Defaults to L<DBIx::Class::Schema/storage_type> (which must
129 start with "::" when using L<DBIx::Class::Schema::Loader>).
131 This is mostly useful for subclassing existing loaders or in conjunction
132 with L</dump_to_dir>.
134 =head2 loader_options
138 =item Argument: \%loader_options
142 Example in Synopsis above demonstrates a few common arguments. For
143 detailed information on all of the arguments, most of which are
144 only useful in fairly complex scenarios, see the
145 L<DBIx::Class::Schema::Loader::Base> documentation.
147 If you intend to use C<loader_options>, you must call
148 C<loader_options> before any connection is made, or embed the
149 C<loader_options> in the connection information itself as shown
150 below. Setting C<loader_options> after the connection has
151 already been made is useless.
158 my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
159 $self->_loader_args(\%args);
166 my $class = ref $self || $self;
168 my $args = $self->_loader_args;
170 # set up the schema/schema_class arguments
171 $args->{schema} = $self;
172 $args->{schema_class} = $class;
173 weaken($args->{schema}) if ref $self;
174 $args->{dump_directory} ||= $self->dump_to_dir;
175 $args->{naming} = $self->naming if $self->naming;
176 $args->{use_namespaces} = $self->use_namespaces if defined $self->use_namespaces;
178 # XXX this only works for relative storage_type, like ::DBI ...
179 my $loader_class = $self->loader_class;
181 $loader_class = "DBIx::Class::Schema::Loader${loader_class}" if $loader_class =~ /^::/;
182 $args->{loader_class} = $loader_class;
185 my $impl = $loader_class || "DBIx::Class::Schema::Loader" . $self->storage_type;
186 eval { $self->ensure_class_loaded($impl) };
187 croak qq/Could not load loader_class "$impl": "$@"/ if $@;
189 $self->loader($impl->new(%$args));
191 $self->_loader_invoked(1);
200 =item Arguments: @args
202 =item Return Value: $new_schema
206 See L<DBIx::Class::Schema/connection> for basic usage.
208 If the final argument is a hashref, and it contains the keys C<loader_options>
209 or C<loader_class>, those keys will be deleted, and their values value will be
210 used for the loader options or class, respectively, just as if set via the
211 L</loader_options> or L</loader_class> methods above.
213 The actual auto-loading operation (the heart of this module) will be invoked
214 as soon as the connection information is defined.
221 if($_[-1] && ref $_[-1] eq 'HASH') {
222 for my $option (qw/ loader_class loader_options result_base_class schema_base_class/) {
223 if(my $value = delete $_[-1]->{$option}) {
224 $self->$option($value);
227 pop @_ if !keys %{$_[-1]};
230 $self = $self->next::method(@_);
232 my $class = ref $self || $self;
233 if(!$class->_loader_invoked) {
234 $self->_invoke_loader
242 See L<DBIx::Class::Schema/clone>.
249 my $clone = $self->next::method(@_);
251 if($clone->_loader_args) {
252 $clone->_loader_args->{schema} = $clone;
253 weaken($clone->_loader_args->{schema});
263 =item Argument: $directory
267 Calling this as a class method on either L<DBIx::Class::Schema::Loader>
268 or any derived schema class will cause all schemas to dump
269 manual versions of themselves to the named directory when they are
270 loaded. In order to be effective, this must be set before defining a
271 connection on this schema class or any derived object (as the loading
272 happens as soon as both a connection and loader_options are set, and
273 only once per class).
275 See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
276 details on the dumping mechanism.
278 This can also be set at module import time via the import option
279 C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
280 C</foo/bar> is the target directory.
284 # My::Schema isa DBIx::Class::Schema::Loader, and has connection info
285 # hardcoded in the class itself:
286 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1
288 # Same, but no hard-coded connection, so we must provide one:
289 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)'
291 # Or as a class method, as long as you get it done *before* defining a
292 # connection on this schema class or any derived object:
294 My::Schema->dump_to_dir('/foo/bar');
295 My::Schema->connection(........);
297 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
301 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
302 My::Schema->connection(.......);
303 My::OtherSchema->connection(.......);
305 # Another alternative to the above:
306 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
309 My::Schema->connection(.......);
310 My::OtherSchema->connection(.......);
319 my $cpkg = (caller)[0];
321 foreach my $opt (@_) {
322 if($opt =~ m{^dump_to_dir:(.*)$}) {
323 $self->dump_to_dir($1)
325 elsif($opt eq 'make_schema_at') {
327 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
329 elsif($opt eq 'naming') {
331 *{"${cpkg}::naming"} = sub { $self->naming(@_) };
333 elsif($opt eq 'use_namespaces') {
335 *{"${cpkg}::use_namespaces"} = sub { $self->use_namespaces(@_) };
340 =head2 make_schema_at
344 =item Arguments: $schema_class_name, \%loader_options, \@connect_info
346 =item Return Value: $schema_class_name
350 This function creates a DBIx::Class schema from an existing RDBMS
351 schema. With the C<dump_directory> option, generates a set of
352 DBIx::Class classes from an existing database schema read from the
353 given dsn. Without a C<dump_directory>, creates schema classes in
354 memory at runtime without generating on-disk class files.
356 For a complete list of supported loader_options, see
357 L<DBIx::Class::Schema::Loader::Base>
359 The last hashref in the C<\@connect_info> can specify the L</loader_class>.
361 This function can be imported in the usual way, as illustrated in
364 # Simple example, creates as a new class 'New::Schema::Name' in
365 # memory in the running perl interpreter.
366 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
370 [ 'dbi:Pg:dbname="foo"','postgres','',
371 { loader_class => 'MyLoader' } # optionally
375 # Inside a script, specifying a dump directory in which to write
377 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
380 { debug => 1, dump_directory => './lib' },
381 [ 'dbi:Pg:dbname="foo"','postgres','',
382 { loader_class => 'MyLoader' } # optionally
386 The last hashref in the C<\@connect_info> is checked for loader arguments such
387 as C<loader_options> and C<loader_class>, see L</connection> for more details.
392 my ($target, $opts, $connect_info) = @_;
396 @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/;
399 eval { $target->_loader_invoked(0) };
401 $target->loader_options($opts);
402 $target->connection(@$connect_info);
409 =item Return Value: @new_monikers
413 Re-scans the database for newly added tables since the initial
414 load, and adds them to the schema at runtime, including relationships,
415 etc. Does not process drops or changes.
417 Returns a list of the new monikers added.
421 sub rescan { my $self = shift; $self->loader->rescan($self) }
427 =item Arguments: \%opts | $ver
431 Controls the naming options for backward compatibility, see
432 L<DBIx::Class::Schema::Loader::Base/naming> for details.
434 To upgrade a dynamic schema, use:
436 __PACKAGE__->naming('current');
438 Can be imported into your dump script and called as a function as well:
442 =head2 use_namespaces
450 Controls the use_namespaces options for backward compatibility, see
451 L<DBIx::Class::Schema::Loader::Base/use_namespaces> for details.
453 To upgrade a dynamic schema, use:
455 __PACKAGE__->use_namespaces(1);
457 Can be imported into your dump script and called as a function as well:
463 =head2 Multiple Database Schemas
465 See L<DBIx::Class::Schema::Loader::Base/db_schema>.
467 =head1 ACKNOWLEDGEMENTS
469 Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent
470 in a bug report or suggestion.
472 Based on L<DBIx::Class::Loader> by Sebastian Riedel
474 Based upon the work of IKEBE Tomohiro
478 blblack: Brandon Black <blblack@gmail.com>
482 ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
484 arcanez: Justin Hunter <justin.d.hunter@gmail.com>
486 ash: Ash Berlin <ash@cpan.org>
488 btilly: Ben Tilly <btilly@gmail.com>
490 Caelum: Rafael Kitover <rkitover@cpan.org>
492 TSUNODA Kazuya <drk@drk7.jp>
494 rbo: Robert Bohne <rbo@cpan.org>
496 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
498 gugu: Andrey Kostenko <a.kostenko@rambler-co.ru>
500 jhannah: Jay Hannah <jay@jays.net>
502 rbuels: Robert Buels <rmb32@cornell.edu>
504 timbunce: Tim Bunce <timb@cpan.org>
506 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
508 mstratman: Mark A. Stratman <stratman@gmail.com>
510 kane: Jos Boumans <kane@cpan.org>
512 waawaamilk: Nigel McNie <nigel@mcnie.name>
514 acmoore: Andrew Moore <amoore@cpan.org>
516 bphillips: Brian Phillips <bphillips@cpan.org>
518 schwern: Michael G. Schwern <mschwern@cpan.org>
520 hobbs: Andrew Rodland <arodland@cpan.org>
522 domm: Thomas Klausner <domm@plix.at>
524 spb: Stephen Bennett <spb@exherbo.org>
526 Matias E. Fernandez <mfernandez@pisco.ch>
528 Al Newkirk <awncorp@cpan.org>
530 ... and lots of other folks. If we forgot you, please write the current
533 =head1 COPYRIGHT & LICENSE
535 Copyright (c) 2006 - 2009 by the aforementioned
536 L<DBIx::Class::Schema::Loader/AUTHOR> and
537 L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
539 This library is free software; you can redistribute it and/or modify it under
540 the same terms as Perl itself.
544 L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>
549 # vim:et sts=4 sw=4 tw=0: