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.07001';
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',
42 { loader_class => 'MyLoader' } # optionally
46 # from the command line or a shell script with dbicdump (distributed
47 # with this module). Do `perldoc dbicdump` for usage.
48 dbicdump -o dump_directory=./lib \
55 ### or generate and load classes at runtime
56 # note: this technique is not recommended
57 # for use in production code
60 use base qw/DBIx::Class::Schema::Loader/;
62 __PACKAGE__->loader_options(
63 constraint => '^foo.*',
67 #### in application code elsewhere:
71 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
73 my $schema1 = "My::Schema"; $schema1->connection(as above);
77 DBIx::Class::Schema::Loader automates the definition of a
78 L<DBIx::Class::Schema> by scanning database table definitions and
79 setting up the columns, primary keys, and relationships.
81 DBIx::Class::Schema::Loader currently supports only the DBI storage type. It
82 has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>,
83 L<DBD::SQLite>, L<DBD::Sybase> (for Sybase ASE and MSSSQL), L<DBD::ODBC> (for
84 MSSQL) and L<DBD::Oracle>. Other DBI drivers may function to a greater or
85 lesser degree with this loader, depending on how much of the DBI spec they
86 implement, and how standard their implementation is.
88 Patches to make other DBDs work correctly welcome.
90 See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
91 your own vendor-specific subclass for an unsupported DBD driver.
93 This module requires L<DBIx::Class> 0.07006 or later, and obsoletes
94 the older L<DBIx::Class::Loader>.
96 This module is designed more to get you up and running quickly against
97 an existing database, or to be effective for simple situations, rather
98 than to be what you use in the long term for a complex database/project.
100 That being said, transitioning your code from a Schema generated by this
101 module to one that doesn't use this module should be straightforward and
102 painless, so don't shy away from it just for fears of the transition down
111 =item Argument: $loader_class
115 Set the loader class to be instantiated when L</connection> is called.
116 If the classname starts with "::", "DBIx::Class::Schema::Loader" is
117 prepended. Defaults to L<DBIx::Class::Schema/storage_type> (which must
118 start with "::" when using L<DBIx::Class::Schema::Loader>).
120 This is mostly useful for subclassing existing loaders or in conjunction
121 with L</dump_to_dir>.
123 =head2 loader_options
127 =item Argument: \%loader_options
131 Example in Synopsis above demonstrates a few common arguments. For
132 detailed information on all of the arguments, most of which are
133 only useful in fairly complex scenarios, see the
134 L<DBIx::Class::Schema::Loader::Base> documentation.
136 If you intend to use C<loader_options>, you must call
137 C<loader_options> before any connection is made, or embed the
138 C<loader_options> in the connection information itself as shown
139 below. Setting C<loader_options> after the connection has
140 already been made is useless.
147 my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
148 $self->_loader_args(\%args);
155 my $class = ref $self || $self;
157 my $args = $self->_loader_args;
159 # set up the schema/schema_class arguments
160 $args->{schema} = $self;
161 $args->{schema_class} = $class;
162 weaken($args->{schema}) if ref $self;
163 $args->{dump_directory} ||= $self->dump_to_dir;
164 $args->{naming} = $self->naming if $self->naming;
165 $args->{use_namespaces} = $self->use_namespaces if $self->use_namespaces;
167 # XXX this only works for relative storage_type, like ::DBI ...
168 my $loader_class = $self->loader_class;
170 $loader_class = "DBIx::Class::Schema::Loader${loader_class}" if $loader_class =~ /^::/;
171 $args->{loader_class} = $loader_class;
174 my $impl = $loader_class || "DBIx::Class::Schema::Loader" . $self->storage_type;
175 eval { $self->ensure_class_loaded($impl) };
176 croak qq/Could not load loader_class "$impl": "$@"/ if $@;
178 $self->_loader($impl->new(%$args));
179 $self->_loader->load;
180 $self->_loader_invoked(1);
189 =item Arguments: @args
191 =item Return Value: $new_schema
195 See L<DBIx::Class::Schema/connection> for basic usage.
197 If the final argument is a hashref, and it contains the keys C<loader_options>
198 or C<loader_class>, those keys will be deleted, and their values value will be
199 used for the loader options or class, respectively, just as if set via the
200 L</loader_options> or L</loader_class> methods above.
202 The actual auto-loading operation (the heart of this module) will be invoked
203 as soon as the connection information is defined.
210 if($_[-1] && ref $_[-1] eq 'HASH') {
211 for my $option (qw/ loader_class loader_options result_base_class schema_base_class/) {
212 if(my $value = delete $_[-1]->{$option}) {
213 $self->$option($value);
216 pop @_ if !keys %{$_[-1]};
219 $self = $self->next::method(@_);
221 my $class = ref $self || $self;
222 if(!$class->_loader_invoked) {
223 $self->_invoke_loader
231 See L<DBIx::Class::Schema/clone>.
238 my $clone = $self->next::method(@_);
240 if($clone->_loader_args) {
241 $clone->_loader_args->{schema} = $clone;
242 weaken($clone->_loader_args->{schema});
252 =item Argument: $directory
256 Calling this as a class method on either L<DBIx::Class::Schema::Loader>
257 or any derived schema class will cause all schemas to dump
258 manual versions of themselves to the named directory when they are
259 loaded. In order to be effective, this must be set before defining a
260 connection on this schema class or any derived object (as the loading
261 happens as soon as both a connection and loader_options are set, and
262 only once per class).
264 See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
265 details on the dumping mechanism.
267 This can also be set at module import time via the import option
268 C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
269 C</foo/bar> is the target directory.
273 # My::Schema isa DBIx::Class::Schema::Loader, and has connection info
274 # hardcoded in the class itself:
275 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1
277 # Same, but no hard-coded connection, so we must provide one:
278 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)'
280 # Or as a class method, as long as you get it done *before* defining a
281 # connection on this schema class or any derived object:
283 My::Schema->dump_to_dir('/foo/bar');
284 My::Schema->connection(........);
286 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
290 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
291 My::Schema->connection(.......);
292 My::OtherSchema->connection(.......);
294 # Another alternative to the above:
295 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
298 My::Schema->connection(.......);
299 My::OtherSchema->connection(.......);
308 my $cpkg = (caller)[0];
310 foreach my $opt (@_) {
311 if($opt =~ m{^dump_to_dir:(.*)$}) {
312 $self->dump_to_dir($1)
314 elsif($opt eq 'make_schema_at') {
316 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
318 elsif($opt eq 'naming') {
320 *{"${cpkg}::naming"} = sub { $self->naming(@_) };
322 elsif($opt eq 'use_namespaces') {
324 *{"${cpkg}::use_namespaces"} = sub { $self->use_namespaces(@_) };
329 =head2 make_schema_at
333 =item Arguments: $schema_class_name, \%loader_options, \@connect_info
335 =item Return Value: $schema_class_name
339 This function creates a DBIx::Class schema from an existing RDBMS
340 schema. With the C<dump_directory> option, generates a set of
341 DBIx::Class classes from an existing database schema read from the
342 given dsn. Without a C<dump_directory>, creates schema classes in
343 memory at runtime without generating on-disk class files.
345 For a complete list of supported loader_options, see
346 L<DBIx::Class::Schema::Loader::Base>
348 The last hashref in the C<\@connect_info> can specify the L</loader_class>.
350 This function can be imported in the usual way, as illustrated in
353 # Simple example, creates as a new class 'New::Schema::Name' in
354 # memory in the running perl interpreter.
355 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
359 [ 'dbi:Pg:dbname="foo"','postgres','',
360 { loader_class => 'MyLoader' } # optionally
364 # Inside a script, specifying a dump directory in which to write
366 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
369 { debug => 1, dump_directory => './lib' },
370 [ 'dbi:Pg:dbname="foo"','postgres','',
371 { loader_class => 'MyLoader' } # optionally
375 The last hashref in the C<\@connect_info> is checked for loader arguments such
376 as C<loader_options> and C<loader_class>, see L</connection> for more details.
381 my ($target, $opts, $connect_info) = @_;
385 @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/;
388 eval { $target->_loader_invoked(0) };
390 $target->loader_options($opts);
391 $target->connection(@$connect_info);
398 =item Return Value: @new_monikers
402 Re-scans the database for newly added tables since the initial
403 load, and adds them to the schema at runtime, including relationships,
404 etc. Does not process drops or changes.
406 Returns a list of the new monikers added.
410 sub rescan { my $self = shift; $self->_loader->rescan($self) }
416 =item Arguments: \%opts | $ver
420 Controls the naming options for backward compatibility, see
421 L<DBIx::Class::Schema::Loader::Base/naming> for details.
423 To upgrade a dynamic schema, use:
425 __PACKAGE__->naming('current');
427 Can be imported into your dump script and called as a function as well:
431 =head2 use_namespaces
439 Controls the use_namespaces options for backward compatibility, see
440 L<DBIx::Class::Schema::Loader::Base/use_namespaces> for details.
442 To upgrade a dynamic schema, use:
444 __PACKAGE__->use_namespaces(1);
446 Can be imported into your dump script and called as a function as well:
452 =head2 Multiple Database Schemas
454 Currently the loader is limited to working within a single schema
455 (using the underlying RDBMS's definition of "schema"). If you have a
456 multi-schema database with inter-schema relationships (which is easy
457 to do in PostgreSQL or DB2 for instance), you currently can only
458 automatically load the tables of one schema, and relationships to
459 tables in other schemas will be silently ignored.
461 At some point in the future, an intelligent way around this might be
462 devised, probably by allowing the C<db_schema> option to be an
463 arrayref of schemas to load.
465 In "normal" L<DBIx::Class::Schema> usage, manually-defined
466 source classes and relationships have no problems crossing vendor schemas.
468 =head1 ACKNOWLEDGEMENTS
470 Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent
471 in a bug report or suggestion.
473 Based on L<DBIx::Class::Loader> by Sebastian Riedel
475 Based upon the work of IKEBE Tomohiro
479 blblack: Brandon Black <blblack@gmail.com>
483 ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
485 arcanez: Justin Hunter <justin.d.hunter@gmail.com>
487 ash: Ash Berlin <ash@cpan.org>
489 Caelum: Rafael Kitover <rkitover@cpan.org>
491 TSUNODA Kazuya <drk@drk7.jp>
493 rbo: Robert Bohne <rbo@cpan.org>
495 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
497 gugu: Andrey Kostenko <a.kostenko@rambler-co.ru>
499 jhannah: Jay Hannah <jay@jays.net>
501 rbuels: Robert Buels <rmb32@cornell.edu>
503 timbunce: Tim Bunce <timb@cpan.org>
505 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
507 kane: Jos Boumans <kane@cpan.org>
509 waawaamilk: Nigel McNie <nigel@mcnie.name>
511 acmoore: Andrew Moore <amoore@cpan.org>
513 bphillips: Brian Phillips <bphillips@cpan.org>
515 schwern: Michael G. Schwern <mschwern@cpan.org>
517 hobbs: Andrew Rodland <arodland@cpan.org>
519 domm: Thomas Klausner <domm@plix.at>
521 ... and lots of other folks. If we forgot you, please write the current
524 =head1 COPYRIGHT & LICENSE
526 Copyright (c) 2006 - 2009 by the aforementioned
527 L<DBIx::Class::Schema::Loader/AUTHOR> and
528 L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
530 This library is free software; you can redistribute it and/or modify it under
531 the same terms as Perl itself.
535 L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>
540 # vim:et sts=4 sw=4 tw=0: