1 package DBIx::Class::Schema::Loader;
5 use base qw/DBIx::Class::Schema Class::Accessor::Grouped/;
8 use Carp::Clan qw/^DBIx::Class/;
9 use Scalar::Util 'weaken';
10 use Sub::Name 'subname';
13 # Always remember to do all digits for the version even if they're 0
14 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
15 # brain damage and presumably various other packaging systems too
16 our $VERSION = '0.07010';
18 __PACKAGE__->mk_group_accessors('inherited', qw/
27 __PACKAGE__->_loader_args({});
31 DBIx::Class::Schema::Loader - Create a DBIx::Class::Schema based on a database
35 ### use this module to generate a set of class files
38 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
42 dump_directory => './lib',
44 [ 'dbi:Pg:dbname="foo"', 'myuser', 'mypassword',
45 { loader_class => 'MyLoader' } # optionally
49 # from the command line or a shell script with dbicdump (distributed
50 # with this module). Do `perldoc dbicdump` for usage.
51 dbicdump -o dump_directory=./lib \
52 -o components='["InflateColumn::DateTime"]' \
59 ### or generate and load classes at runtime
60 # note: this technique is not recommended
61 # for use in production code
64 use base qw/DBIx::Class::Schema::Loader/;
66 __PACKAGE__->loader_options(
67 constraint => '^foo.*',
71 #### in application code elsewhere:
75 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
77 my $schema1 = "My::Schema"; $schema1->connection(as above);
81 DBIx::Class::Schema::Loader automates the definition of a
82 L<DBIx::Class::Schema> by scanning database table definitions and setting up
83 the columns, primary keys, unique constraints and relationships.
85 See L<dbicdump> for the C<dbicdump> utility.
87 DBIx::Class::Schema::Loader currently supports only the DBI storage type. It
88 has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>,
89 L<DBD::Firebird>, L<DBD::InterBase>, L<DBD::Informix>, L<DBD::SQLAnywhere>,
90 L<DBD::SQLite>, L<DBD::Sybase> (for Sybase ASE and MSSSQL), L<DBD::ODBC> (for
91 MSSQL, MSAccess, Firebird and SQL Anywhere) L<DBD::ADO> (for MSSQL and
92 MSAccess) and L<DBD::Oracle>. Other DBI drivers may function to a greater or
93 lesser degree with this loader, depending on how much of the DBI spec they
94 implement, and how standard their implementation is.
96 Patches to make other DBDs work correctly welcome.
98 See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
99 your own vendor-specific subclass for an unsupported DBD driver.
101 This module requires L<DBIx::Class> 0.08127 or later, and obsoletes the older
102 L<DBIx::Class::Loader>.
104 See L<DBIx::Class::Schema::Loader::Base> for available options.
110 The loader object, as class data on your Schema. For methods available see
111 L<DBIx::Class::Schema::Loader::Base> and L<DBIx::Class::Schema::Loader::DBI>.
124 =item Argument: $loader_class
128 Set the loader class to be instantiated when L</connection> is called.
129 If the classname starts with "::", "DBIx::Class::Schema::Loader" is
130 prepended. Defaults to L<DBIx::Class::Schema/storage_type> (which must
131 start with "::" when using L<DBIx::Class::Schema::Loader>).
133 This is mostly useful for subclassing existing loaders or in conjunction
134 with L</dump_to_dir>.
136 =head2 loader_options
140 =item Argument: \%loader_options
144 Example in Synopsis above demonstrates a few common arguments. For
145 detailed information on all of the arguments, most of which are
146 only useful in fairly complex scenarios, see the
147 L<DBIx::Class::Schema::Loader::Base> documentation.
149 If you intend to use C<loader_options>, you must call
150 C<loader_options> before any connection is made, or embed the
151 C<loader_options> in the connection information itself as shown
152 below. Setting C<loader_options> after the connection has
153 already been made is useless.
160 my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
161 $self->_loader_args(\%args);
168 my $class = ref $self || $self;
170 my $args = $self->_loader_args;
172 # set up the schema/schema_class arguments
173 $args->{schema} = $self;
174 $args->{schema_class} = $class;
175 weaken($args->{schema}) if ref $self;
176 $args->{dump_directory} ||= $self->dump_to_dir;
177 $args->{naming} = $self->naming if $self->naming;
178 $args->{use_namespaces} = $self->use_namespaces if defined $self->use_namespaces;
180 # XXX this only works for relative storage_type, like ::DBI ...
181 my $loader_class = $self->loader_class;
183 $loader_class = "DBIx::Class::Schema::Loader${loader_class}" if $loader_class =~ /^::/;
184 $args->{loader_class} = $loader_class;
187 my $impl = $loader_class || "DBIx::Class::Schema::Loader" . $self->storage_type;
188 eval { $self->ensure_class_loaded($impl) };
189 croak qq/Could not load loader_class "$impl": "$@"/ if $@;
191 $self->loader($impl->new(%$args));
193 $self->_loader_invoked(1);
202 =item Arguments: @args
204 =item Return Value: $new_schema
208 See L<DBIx::Class::Schema/connection> for basic usage.
210 If the final argument is a hashref, and it contains the keys C<loader_options>
211 or C<loader_class>, those keys will be deleted, and their values value will be
212 used for the loader options or class, respectively, just as if set via the
213 L</loader_options> or L</loader_class> methods above.
215 The actual auto-loading operation (the heart of this module) will be invoked
216 as soon as the connection information is defined.
223 if($_[-1] && ref $_[-1] eq 'HASH') {
224 for my $option (qw/loader_class loader_options/) {
225 if(my $value = delete $_[-1]->{$option}) {
226 $self->$option($value);
229 pop @_ if !keys %{$_[-1]};
232 # Make sure we inherit from schema_base_class and load schema_components
234 require DBIx::Class::Schema::Loader::Base;
235 my $temp_loader = DBIx::Class::Schema::Loader::Base->new(
236 %{ $self->_loader_args }
239 if ($temp_loader->schema_base_class || $temp_loader->schema_components) {
240 my @components = @{ $temp_loader->schema_components }
241 if $temp_loader->schema_components;
243 push @components, ('+'.$temp_loader->schema_base_class)
244 if $temp_loader->schema_base_class;
246 $self->load_components(@components);
249 # This hack is necessary if we changed @ISA of $self through ->load_components.
251 no warnings 'redefine';
253 local *connection = subname __PACKAGE__.'::connection' => sub {
255 $self->next::method(@_);
258 $self = $self->connection(@_);
261 my $class = ref $self || $self;
262 if(!$class->_loader_invoked) {
263 $self->_invoke_loader
271 See L<DBIx::Class::Schema/clone>.
278 my $clone = $self->next::method(@_);
280 if($clone->_loader_args) {
281 $clone->_loader_args->{schema} = $clone;
282 weaken($clone->_loader_args->{schema});
292 =item Argument: $directory
296 Calling this as a class method on either L<DBIx::Class::Schema::Loader>
297 or any derived schema class will cause all schemas to dump
298 manual versions of themselves to the named directory when they are
299 loaded. In order to be effective, this must be set before defining a
300 connection on this schema class or any derived object (as the loading
301 happens as soon as both a connection and loader_options are set, and
302 only once per class).
304 See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
305 details on the dumping mechanism.
307 This can also be set at module import time via the import option
308 C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
309 C</foo/bar> is the target directory.
313 # My::Schema isa DBIx::Class::Schema::Loader, and has connection info
314 # hardcoded in the class itself:
315 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1
317 # Same, but no hard-coded connection, so we must provide one:
318 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)'
320 # Or as a class method, as long as you get it done *before* defining a
321 # connection on this schema class or any derived object:
323 My::Schema->dump_to_dir('/foo/bar');
324 My::Schema->connection(........);
326 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
330 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
331 My::Schema->connection(.......);
332 My::OtherSchema->connection(.......);
334 # Another alternative to the above:
335 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
338 My::Schema->connection(.......);
339 My::OtherSchema->connection(.......);
348 my $cpkg = (caller)[0];
350 foreach my $opt (@_) {
351 if($opt =~ m{^dump_to_dir:(.*)$}) {
352 $self->dump_to_dir($1)
354 elsif($opt eq 'make_schema_at') {
356 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
358 elsif($opt eq 'naming') {
360 *{"${cpkg}::naming"} = sub { $self->naming(@_) };
362 elsif($opt eq 'use_namespaces') {
364 *{"${cpkg}::use_namespaces"} = sub { $self->use_namespaces(@_) };
369 =head2 make_schema_at
373 =item Arguments: $schema_class_name, \%loader_options, \@connect_info
375 =item Return Value: $schema_class_name
379 This function creates a DBIx::Class schema from an existing RDBMS
380 schema. With the C<dump_directory> option, generates a set of
381 DBIx::Class classes from an existing database schema read from the
382 given dsn. Without a C<dump_directory>, creates schema classes in
383 memory at runtime without generating on-disk class files.
385 For a complete list of supported loader_options, see
386 L<DBIx::Class::Schema::Loader::Base>
388 The last hashref in the C<\@connect_info> can specify the L</loader_class>.
390 This function can be imported in the usual way, as illustrated in
393 # Simple example, creates as a new class 'New::Schema::Name' in
394 # memory in the running perl interpreter.
395 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
399 [ 'dbi:Pg:dbname="foo"','postgres','',
400 { loader_class => 'MyLoader' } # optionally
404 # Inside a script, specifying a dump directory in which to write
406 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
409 { debug => 1, dump_directory => './lib' },
410 [ 'dbi:Pg:dbname="foo"','postgres','',
411 { loader_class => 'MyLoader' } # optionally
415 The last hashref in the C<\@connect_info> is checked for loader arguments such
416 as C<loader_options> and C<loader_class>, see L</connection> for more details.
421 my ($target, $opts, $connect_info) = @_;
425 @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/;
428 eval { $target->_loader_invoked(0) };
430 $target->loader_options($opts);
431 $target->connection(@$connect_info);
438 =item Return Value: @new_monikers
442 Re-scans the database for newly added tables since the initial
443 load, and adds them to the schema at runtime, including relationships,
444 etc. Does not process drops or changes.
446 Returns a list of the new monikers added.
450 sub rescan { my $self = shift; $self->loader->rescan($self) }
456 =item Arguments: \%opts | $ver
460 Controls the naming options for backward compatibility, see
461 L<DBIx::Class::Schema::Loader::Base/naming> for details.
463 To upgrade a dynamic schema, use:
465 __PACKAGE__->naming('current');
467 Can be imported into your dump script and called as a function as well:
471 =head2 use_namespaces
479 Controls the use_namespaces options for backward compatibility, see
480 L<DBIx::Class::Schema::Loader::Base/use_namespaces> for details.
482 To upgrade a dynamic schema, use:
484 __PACKAGE__->use_namespaces(1);
486 Can be imported into your dump script and called as a function as well:
492 =head2 Multiple Database Schemas
494 See L<DBIx::Class::Schema::Loader::Base/db_schema>.
496 =head1 ACKNOWLEDGEMENTS
498 Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent
499 in a bug report or suggestion.
501 Based on L<DBIx::Class::Loader> by Sebastian Riedel
503 Based upon the work of IKEBE Tomohiro
507 blblack: Brandon Black <blblack@gmail.com>
511 ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
513 arcanez: Justin Hunter <justin.d.hunter@gmail.com>
515 ash: Ash Berlin <ash@cpan.org>
517 btilly: Ben Tilly <btilly@gmail.com>
519 Caelum: Rafael Kitover <rkitover@cpan.org>
521 TSUNODA Kazuya <drk@drk7.jp>
523 rbo: Robert Bohne <rbo@cpan.org>
525 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
527 gugu: Andrey Kostenko <a.kostenko@rambler-co.ru>
529 jhannah: Jay Hannah <jay@jays.net>
531 rbuels: Robert Buels <rmb32@cornell.edu>
533 timbunce: Tim Bunce <timb@cpan.org>
535 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
537 mstratman: Mark A. Stratman <stratman@gmail.com>
539 kane: Jos Boumans <kane@cpan.org>
541 waawaamilk: Nigel McNie <nigel@mcnie.name>
543 acmoore: Andrew Moore <amoore@cpan.org>
545 bphillips: Brian Phillips <bphillips@cpan.org>
547 schwern: Michael G. Schwern <mschwern@cpan.org>
549 hobbs: Andrew Rodland <arodland@cpan.org>
551 domm: Thomas Klausner <domm@plix.at>
553 spb: Stephen Bennett <spb@exherbo.org>
555 Matias E. Fernandez <mfernandez@pisco.ch>
557 Al Newkirk <awncorp@cpan.org>
559 ... and lots of other folks. If we forgot you, please write the current
562 =head1 COPYRIGHT & LICENSE
564 Copyright (c) 2006 - 2009 by the aforementioned
565 L<DBIx::Class::Schema::Loader/AUTHOR> and
566 L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
568 This library is free software; you can redistribute it and/or modify it under
569 the same terms as Perl itself.
573 L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>
578 # vim:et sts=4 sw=4 tw=0: