1 package DBIx::Class::Schema::Loader;
5 use base qw/DBIx::Class::Schema Class::Accessor::Grouped/;
6 use Carp::Clan qw/^DBIx::Class/;
8 use Scalar::Util qw/ weaken /;
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
13 our $VERSION = '0.05003';
15 __PACKAGE__->mk_group_accessors('inherited', qw/
24 __PACKAGE__->_loader_args({});
28 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
32 ### use this module to generate a set of class files
35 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
39 dump_directory => './lib',
41 [ 'dbi:Pg:dbname="foo"', 'myuser', 'mypassword', { loader_class => 'MyLoader' } ],
44 # from the command line or a shell script with dbicdump (distributed
45 # with this module). Do `perldoc dbicdump` for usage.
46 dbicdump -o dump_directory=./lib \
53 ### or generate and load classes at runtime
54 # note: this technique is not recommended
55 # for use in production code
58 use base qw/DBIx::Class::Schema::Loader/;
60 __PACKAGE__->loader_options(
61 constraint => '^foo.*',
65 #### in application code elsewhere:
69 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
71 my $schema1 = "My::Schema"; $schema1->connection(as above);
75 DBIx::Class::Schema::Loader automates the definition of a
76 L<DBIx::Class::Schema> by scanning database table definitions and
77 setting up the columns, primary keys, and relationships.
79 DBIx::Class::Schema::Loader currently supports only the DBI storage type. It
80 has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>,
81 L<DBD::SQLite>, L<DBD::Sybase> (for Sybase ASE and MSSSQL), L<DBD::ODBC> (for
82 MSSQL) and L<DBD::Oracle>. Other DBI drivers may function to a greater or
83 lesser degree with this loader, depending on how much of the DBI spec they
84 implement, and how standard their implementation is.
86 Patches to make other DBDs work correctly welcome.
88 See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
89 your own vendor-specific subclass for an unsupported DBD driver.
91 This module requires L<DBIx::Class> 0.07006 or later, and obsoletes
92 the older L<DBIx::Class::Loader>.
94 This module is designed more to get you up and running quickly against
95 an existing database, or to be effective for simple situations, rather
96 than to be what you use in the long term for a complex database/project.
98 That being said, transitioning your code from a Schema generated by this
99 module to one that doesn't use this module should be straightforward and
100 painless, so don't shy away from it just for fears of the transition down
109 =item Argument: $loader_class
113 Set the loader class to be instantiated when L</connection> is called.
114 If the classname starts with "::", "DBIx::Class::Schema::Loader" is
115 prepended. Defaults to L<DBIx::Class::Schema/storage_type> (which must
116 start with "::" when using L<DBIx::Class::Schema::Loader>).
118 This is mostly useful for subclassing existing loaders or in conjunction
119 with L</dump_to_dir>.
121 =head2 loader_options
125 =item Argument: \%loader_options
129 Example in Synopsis above demonstrates a few common arguments. For
130 detailed information on all of the arguments, most of which are
131 only useful in fairly complex scenarios, see the
132 L<DBIx::Class::Schema::Loader::Base> documentation.
134 If you intend to use C<loader_options>, you must call
135 C<loader_options> before any connection is made, or embed the
136 C<loader_options> in the connection information itself as shown
137 below. Setting C<loader_options> after the connection has
138 already been made is useless.
145 my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
146 $self->_loader_args(\%args);
153 my $class = ref $self || $self;
155 my $args = $self->_loader_args;
157 # set up the schema/schema_class arguments
158 $args->{schema} = $self;
159 $args->{schema_class} = $class;
160 weaken($args->{schema}) if ref $self;
161 $args->{dump_directory} ||= $self->dump_to_dir;
162 $args->{naming} = $self->naming if $self->naming;
163 $args->{use_namespaces} = $self->use_namespaces if $self->use_namespaces;
165 # XXX this only works for relative storage_type, like ::DBI ...
166 my $loader_class = $self->loader_class;
168 $loader_class = "DBIx::Class::Schema::Loader${loader_class}" if $loader_class =~ /^::/;
169 $args->{loader_class} = $loader_class;
172 my $impl = $loader_class || "DBIx::Class::Schema::Loader" . $self->storage_type;
173 eval { $self->ensure_class_loaded($impl) };
174 croak qq/Could not load loader class "$impl": "$@"/ if $@;
176 $self->_loader($impl->new(%$args));
177 $self->_loader->load;
178 $self->_loader_invoked(1);
187 =item Arguments: @args
189 =item Return Value: $new_schema
193 See L<DBIx::Class::Schema/connection> for basic usage.
195 If the final argument is a hashref, and it contains the keys C<loader_options>
196 or C<loader_class>, those keys will be deleted, and their values value will be
197 used for the loader options or class, respectively, just as if set via the
198 L</loader_options> or L</loader_class> methods above.
200 The actual auto-loading operation (the heart of this module) will be invoked
201 as soon as the connection information is defined.
208 if($_[-1] && ref $_[-1] eq 'HASH') {
209 for my $option (qw/ loader_class loader_options result_base_class schema_base_class/) {
210 if(my $value = delete $_[-1]->{$option}) {
211 $self->$option($value);
214 pop @_ if !keys %{$_[-1]};
217 $self = $self->next::method(@_);
219 my $class = ref $self || $self;
220 if(!$class->_loader_invoked) {
221 $self->_invoke_loader
229 See L<DBIx::Class::Schema/clone>.
236 my $clone = $self->next::method(@_);
238 if($clone->_loader_args) {
239 $clone->_loader_args->{schema} = $clone;
240 weaken($clone->_loader_args->{schema});
250 =item Argument: $directory
254 Calling this as a class method on either L<DBIx::Class::Schema::Loader>
255 or any derived schema class will cause all schemas to dump
256 manual versions of themselves to the named directory when they are
257 loaded. In order to be effective, this must be set before defining a
258 connection on this schema class or any derived object (as the loading
259 happens as soon as both a connection and loader_options are set, and
260 only once per class).
262 See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
263 details on the dumping mechanism.
265 This can also be set at module import time via the import option
266 C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
267 C</foo/bar> is the target directory.
271 # My::Schema isa DBIx::Class::Schema::Loader, and has connection info
272 # hardcoded in the class itself:
273 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1
275 # Same, but no hard-coded connection, so we must provide one:
276 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)'
278 # Or as a class method, as long as you get it done *before* defining a
279 # connection on this schema class or any derived object:
281 My::Schema->dump_to_dir('/foo/bar');
282 My::Schema->connection(........);
284 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
288 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
289 My::Schema->connection(.......);
290 My::OtherSchema->connection(.......);
292 # Another alternative to the above:
293 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
296 My::Schema->connection(.......);
297 My::OtherSchema->connection(.......);
306 my $cpkg = (caller)[0];
308 foreach my $opt (@_) {
309 if($opt =~ m{^dump_to_dir:(.*)$}) {
310 $self->dump_to_dir($1)
312 elsif($opt eq 'make_schema_at') {
314 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
316 elsif($opt eq 'naming') {
318 *{"${cpkg}::naming"} = sub { $self->naming(@_) };
320 elsif($opt eq 'use_namespaces') {
322 *{"${cpkg}::use_namespaces"} = sub { $self->use_namespaces(@_) };
327 =head2 make_schema_at
331 =item Arguments: $schema_class_name, \%loader_options, \@connect_info
333 =item Return Value: $schema_class_name
337 This function creates a DBIx::Class schema from an existing RDBMS
338 schema. With the C<dump_directory> option, generates a set of
339 DBIx::Class classes from an existing database schema read from the
340 given dsn. Without a C<dump_directory>, creates schema classes in
341 memory at runtime without generating on-disk class files.
343 For a complete list of supported loader_options, see
344 L<DBIx::Class::Schema::Loader::Base>
346 This function can be imported in the usual way, as illustrated in
349 # Simple example, creates as a new class 'New::Schema::Name' in
350 # memory in the running perl interpreter.
351 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
355 [ 'dbi:Pg:dbname="foo"','postgres','', { loader_class => 'MyLoader' } ],
358 # Inside a script, specifying a dump directory in which to write
360 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
363 { debug => 1, dump_directory => './lib' },
364 [ 'dbi:Pg:dbname="foo"','postgres','', { loader_class => 'MyLoader' } ],
367 The last hashref in the C<\@connect_info> is checked for loader arguments such
368 as C<loader_options> and C<loader_class>, see L</connection> for more details.
373 my ($target, $opts, $connect_info) = @_;
377 @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/;
380 eval { $target->_loader_invoked(0) };
382 $target->loader_options($opts);
383 $target->connection(@$connect_info);
390 =item Return Value: @new_monikers
394 Re-scans the database for newly added tables since the initial
395 load, and adds them to the schema at runtime, including relationships,
396 etc. Does not process drops or changes.
398 Returns a list of the new monikers added.
402 sub rescan { my $self = shift; $self->_loader->rescan($self) }
408 =item Arguments: \%opts | $ver
412 Controls the naming options for backward compatibility, see
413 L<DBIx::Class::Schema::Loader::Base/naming> for details.
415 To upgrade a dynamic schema, use:
417 __PACKAGE__->naming('current');
419 Can be imported into your dump script and called as a function as well:
423 =head2 use_namespaces
431 Controls the use_namespaces options for backward compatibility, see
432 L<DBIx::Class::Schema::Loader::Base/use_namespaces> for details.
434 To upgrade a dynamic schema, use:
436 __PACKAGE__->use_namespaces(1);
438 Can be imported into your dump script and called as a function as well:
444 =head2 Multiple Database Schemas
446 Currently the loader is limited to working within a single schema
447 (using the underlying RDBMS's definition of "schema"). If you have a
448 multi-schema database with inter-schema relationships (which is easy
449 to do in PostgreSQL or DB2 for instance), you currently can only
450 automatically load the tables of one schema, and relationships to
451 tables in other schemas will be silently ignored.
453 At some point in the future, an intelligent way around this might be
454 devised, probably by allowing the C<db_schema> option to be an
455 arrayref of schemas to load.
457 In "normal" L<DBIx::Class::Schema> usage, manually-defined
458 source classes and relationships have no problems crossing vendor schemas.
460 =head1 ACKNOWLEDGEMENTS
462 Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent
463 in a bug report or suggestion.
465 Based on L<DBIx::Class::Loader> by Sebastian Riedel
467 Based upon the work of IKEBE Tomohiro
471 blblack: Brandon Black <blblack@gmail.com>
475 ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
477 arcanez: Justin Hunter <justin.d.hunter@gmail.com>
479 ash: Ash Berlin <ash@cpan.org>
481 Caelum: Rafael Kitover <rkitover@cpan.org>
483 TSUNODA Kazuya <drk@drk7.jp>
485 rbo: Robert Bohne <rbo@cpan.org>
487 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
489 gugu: Andrey Kostenko <a.kostenko@rambler-co.ru>
491 jhannah: Jay Hannah <jay@jays.net>
493 rbuels: Robert Buels <rmb32@cornell.edu>
495 timbunce: Tim Bunce <timb@cpan.org>
497 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
499 kane: Jos Boumans <kane@cpan.org>
501 waawaamilk: Nigel McNie <nigel@mcnie.name>
503 acmoore: Andrew Moore <amoore@cpan.org>
505 bphillips: Brian Phillips <bphillips@cpan.org>
507 schwern: Michael G. Schwern <mschwern@cpan.org>
509 hobbs: Andrew Rodland <arodland@cpan.org>
511 ... and lots of other folks. If we forgot you, please write the current
514 =head1 COPYRIGHT & LICENSE
516 Copyright (c) 2006 - 2009 by the aforementioned
517 L<DBIx::Class::Schema::Loader/AUTHOR> and
518 L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
520 This library is free software; you can redistribute it and/or modify it under
521 the same terms as Perl itself.
525 L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>
530 # vim:et sts=4 sw=4 tw=0: