1 package DBIx::Class::Schema::Loader;
5 use base qw/DBIx::Class::Schema Class::Data::Accessor/;
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.04999_07';
15 __PACKAGE__->mk_classaccessor('_loader_args' => {});
16 __PACKAGE__->mk_classaccessors(qw/dump_to_dir _loader_invoked _loader loader_class/);
20 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
24 ### use this module to generate a set of class files
27 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
31 dump_directory => './lib',
33 [ 'dbi:Pg:dbname="foo"', 'myuser', 'mypassword' ],
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 \
45 ### or generate and load classes at runtime
46 # note: this technique is not recommended
47 # for use in production code
50 use base qw/DBIx::Class::Schema::Loader/;
52 __PACKAGE__->loader_options(
53 constraint => '^foo.*',
57 #### in application code elsewhere:
61 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
63 my $schema1 = "My::Schema"; $schema1->connection(as above);
67 DBIx::Class::Schema::Loader automates the definition of a
68 L<DBIx::Class::Schema> by scanning database table definitions and
69 setting up the columns, primary keys, and relationships.
71 DBIx::Class::Schema::Loader currently supports only the DBI storage type.
72 It has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>,
73 L<DBD::SQLite>, and L<DBD::Oracle>. Other DBI drivers may function to
74 a greater or lesser degree with this loader, depending on how much of the
75 DBI spec they implement, and how standard their implementation is.
77 Patches to make other DBDs work correctly welcome.
79 See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
80 your own vendor-specific subclass for an unsupported DBD driver.
82 This module requires L<DBIx::Class> 0.07006 or later, and obsoletes
83 the older L<DBIx::Class::Loader>.
85 This module is designed more to get you up and running quickly against
86 an existing database, or to be effective for simple situations, rather
87 than to be what you use in the long term for a complex database/project.
89 That being said, transitioning your code from a Schema generated by this
90 module to one that doesn't use this module should be straightforward and
91 painless, so don't shy away from it just for fears of the transition down
100 =item Argument: $loader_class
104 Set the loader class to be instantiated when L</connection> is called.
105 If the classname starts with "::", "DBIx::Class::Schema::Loader" is
106 prepended. Defaults to L<DBIx::Class::Schema/storage_type> (which must
107 start with "::" when using L<DBIx::Class::Schema::Loader>).
109 This is mostly useful for subclassing existing loaders or in conjunction
110 with L</dump_to_dir>.
112 =head2 loader_options
116 =item Argument: \%loader_options
120 Example in Synopsis above demonstrates a few common arguments. For
121 detailed information on all of the arguments, most of which are
122 only useful in fairly complex scenarios, see the
123 L<DBIx::Class::Schema::Loader::Base> documentation.
125 If you intend to use C<loader_options>, you must call
126 C<loader_options> before any connection is made, or embed the
127 C<loader_options> in the connection information itself as shown
128 below. Setting C<loader_options> after the connection has
129 already been made is useless.
136 my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
137 $self->_loader_args(\%args);
144 my $class = ref $self || $self;
146 my $args = $self->_loader_args;
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;
154 # XXX this only works for relative storage_type, like ::DBI ...
155 my $impl = $self->loader_class
156 || "DBIx::Class::Schema::Loader" . $self->storage_type;
157 $impl = "DBIx::Class::Schema::Loader${impl}" if $impl =~ /^::/;
158 eval { $self->ensure_class_loaded($impl) };
159 croak qq/Could not load storage_type loader "$impl": "$@"/ if $@;
161 $self->_loader($impl->new(%$args));
162 $self->_loader->load;
163 $self->_loader_invoked(1);
172 =item Arguments: @args
174 =item Return Value: $new_schema
178 See L<DBIx::Class::Schema/connection> for basic usage.
180 If the final argument is a hashref, and it contains the keys C<loader_options>
181 or C<loader_class>, those keys will be deleted, and their values value will be
182 used for the loader options or class, respectively, just as if set via the
183 L</loader_options> or L</loader_class> methods above.
185 The actual auto-loading operation (the heart of this module) will be invoked
186 as soon as the connection information is defined.
193 if($_[-1] && ref $_[-1] eq 'HASH') {
194 for my $option (qw/ loader_class loader_options result_base_class schema_base_class/) {
195 if(my $value = delete $_[-1]->{$option}) {
196 $self->$option($value);
199 pop @_ if !keys %{$_[-1]};
202 $self = $self->next::method(@_);
204 my $class = ref $self || $self;
205 if(!$class->_loader_invoked) {
206 $self->_invoke_loader
214 See L<DBIx::Class::Schema/clone>.
221 my $clone = $self->next::method(@_);
223 if($clone->_loader_args) {
224 $clone->_loader_args->{schema} = $clone;
225 weaken($clone->_loader_args->{schema});
235 =item Argument: $directory
239 Calling this as a class method on either L<DBIx::Class::Schema::Loader>
240 or any derived schema class will cause all schemas to dump
241 manual versions of themselves to the named directory when they are
242 loaded. In order to be effective, this must be set before defining a
243 connection on this schema class or any derived object (as the loading
244 happens as soon as both a connection and loader_options are set, and
245 only once per class).
247 See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
248 details on the dumping mechanism.
250 This can also be set at module import time via the import option
251 C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
252 C</foo/bar> is the target directory.
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
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", ...)'
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:
266 My::Schema->dump_to_dir('/foo/bar');
267 My::Schema->connection(........);
269 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
273 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
274 My::Schema->connection(.......);
275 My::OtherSchema->connection(.......);
277 # Another alternative to the above:
278 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
281 My::Schema->connection(.......);
282 My::OtherSchema->connection(.......);
289 foreach my $opt (@_) {
290 if($opt =~ m{^dump_to_dir:(.*)$}) {
291 $self->dump_to_dir($1)
293 elsif($opt eq 'make_schema_at') {
295 my $cpkg = (caller)[0];
296 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
301 =head2 make_schema_at
305 =item Arguments: $schema_class_name, \%loader_options, \@connect_info
307 =item Return Value: $schema_class_name
311 This function creates a DBIx::Class schema from an existing RDBMS
312 schema. With the C<dump_directory> option, generates a set of
313 DBIx::Class classes from an existing database schema read from the
314 given dsn. Without a C<dump_directory>, creates schema classes in
315 memory at runtime without generating on-disk class files.
317 For a complete list of supported loader_options, see
318 L<DBIx::Class::Schema::Loader::Base>
320 This function can be imported in the usual way, as illustrated in
323 # Simple example, creates as a new class 'New::Schema::Name' in
324 # memory in the running perl interpreter.
325 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
329 [ 'dbi:Pg:dbname="foo"','postgres' ],
332 # Inside a script, specifying a dump directory in which to write
334 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
337 { debug => 1, dump_directory => './lib' },
338 [ 'dbi:Pg:dbname="foo"','postgres' ],
344 my ($target, $opts, $connect_info) = @_;
348 @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/;
351 $target->loader_options($opts);
352 $target->connection(@$connect_info);
359 =item Return Value: @new_monikers
363 Re-scans the database for newly added tables since the initial
364 load, and adds them to the schema at runtime, including relationships,
365 etc. Does not process drops or changes.
367 Returns a list of the new monikers added.
371 sub rescan { my $self = shift; $self->_loader->rescan($self) }
376 =head2 Multiple Database Schemas
378 Currently the loader is limited to working within a single schema
379 (using the underlying RDBMS's definition of "schema"). If you have a
380 multi-schema database with inter-schema relationships (which is easy
381 to do in PostgreSQL or DB2 for instance), you currently can only
382 automatically load the tables of one schema, and relationships to
383 tables in other schemas will be silently ignored.
385 At some point in the future, an intelligent way around this might be
386 devised, probably by allowing the C<db_schema> option to be an
387 arrayref of schemas to load.
389 In "normal" L<DBIx::Class::Schema> usage, manually-defined
390 source classes and relationships have no problems crossing vendor schemas.
394 Brandon Black, C<blblack@gmail.com>
396 Based on L<DBIx::Class::Loader> by Sebastian Riedel
398 Based upon the work of IKEBE Tomohiro
402 Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent
403 in a bug report or suggestion.
407 This library is free software; you can redistribute it and/or modify it under
408 the same terms as Perl itself.
412 L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>