1 package DBIx::Class::Schema::Loader;
5 use base qw/DBIx::Class::Schema Class::Data::Accessor/;
6 use Carp::Clan qw/^DBIx::Class/;
7 use UNIVERSAL::require;
9 use Scalar::Util qw/ 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.04999_05';
16 __PACKAGE__->mk_classaccessor('_loader_args' => {});
17 __PACKAGE__->mk_classaccessors(qw/dump_to_dir _loader_invoked _loader loader_class/);
21 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
26 use base qw/DBIx::Class::Schema::Loader/;
28 __PACKAGE__->loader_options(
29 constraint => '^foo.*',
33 # in seperate application code ...
37 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
39 my $schema1 = "My::Schema"; $schema1->connection(as above);
43 DBIx::Class::Schema::Loader automates the definition of a
44 L<DBIx::Class::Schema> by scanning database table definitions and
45 setting up the columns, primary keys, and relationships.
47 DBIx::Class::Schema::Loader currently supports only the DBI storage type.
48 It has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>,
49 L<DBD::SQLite>, and L<DBD::Oracle>. Other DBI drivers may function to
50 a greater or lesser degree with this loader, depending on how much of the
51 DBI spec they implement, and how standard their implementation is.
53 Patches to make other DBDs work correctly welcome.
55 See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
56 your own vendor-specific subclass for an unsupported DBD driver.
58 This module requires L<DBIx::Class> 0.07006 or later, and obsoletes
59 the older L<DBIx::Class::Loader>.
61 This module is designed more to get you up and running quickly against
62 an existing database, or to be effective for simple situations, rather
63 than to be what you use in the long term for a complex database/project.
65 That being said, transitioning your code from a Schema generated by this
66 module to one that doesn't use this module should be straightforward and
67 painless, so don't shy away from it just for fears of the transition down
74 Set the loader class to be instantiated when L</connection> is called.
75 If the classname starts with "::", "DBIx::Class::Schema::Loader" is
76 prepended. Defaults to L<DBIx::Class::Schema/storage_type> (which must
77 start with "::" when using L<DBIx::Class::Schema::Loader>).
79 This is mostly useful for subclassing existing loaders or in conjunction
84 Example in Synopsis above demonstrates a few common arguments. For
85 detailed information on all of the arguments, most of which are
86 only useful in fairly complex scenarios, see the
87 L<DBIx::Class::Schema::Loader::Base> documentation.
89 If you intend to use C<loader_options>, you must call
90 C<loader_options> before any connection is made, or embed the
91 C<loader_options> in the connection information itself as shown
92 below. Setting C<loader_options> after the connection has
93 already been made is useless.
100 my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
101 $self->_loader_args(\%args);
108 my $class = ref $self || $self;
110 my $args = $self->_loader_args;
112 # set up the schema/schema_class arguments
113 $args->{schema} = $self;
114 $args->{schema_class} = $class;
115 weaken($args->{schema}) if ref $self;
116 $args->{dump_directory} ||= $self->dump_to_dir;
118 # XXX this only works for relative storage_type, like ::DBI ...
119 my $impl = $self->loader_class
120 || "DBIx::Class::Schema::Loader" . $self->storage_type;
121 $impl = "DBIx::Class::Schema::Loader${impl}" if $impl =~ /^::/;
123 croak qq/Could not load storage_type loader "$impl": / .
124 qq/"$UNIVERSAL::require::ERROR"/;
126 $self->_loader($impl->new(%$args));
127 $self->_loader->load;
128 $self->_loader_invoked(1);
135 See L<DBIx::Class::Schema> for basic usage.
137 If the final argument is a hashref, and it contains the keys C<loader_options>
138 or C<loader_class>, those keys will be deleted, and their values value will be
139 used for the loader options or class, respectively, just as if set via the
140 L</loader_options> or L</loader_class> methods above.
142 The actual auto-loading operation (the heart of this module) will be invoked
143 as soon as the connection information is defined.
150 if($_[-1] && ref $_[-1] eq 'HASH') {
151 for my $option (qw/ loader_class loader_options /) {
152 if(my $value = delete $_[-1]->{$option}) {
153 $self->$option($value);
156 pop @_ if !keys %{$_[-1]};
159 $self = $self->next::method(@_);
161 my $class = ref $self || $self;
162 if(!$class->_loader_invoked) {
163 $self->_invoke_loader
171 See L<DBIx::Class::Schema>.
178 my $clone = $self->next::method(@_);
180 if($clone->_loader_args) {
181 $clone->_loader_args->{schema} = $clone;
182 weaken($clone->_loader_args->{schema});
190 Argument: directory name.
192 Calling this as a class method on either L<DBIx::Class::Schema::Loader>
193 or any derived schema class will cause all affected schemas to dump
194 manual versions of themselves to the named directory when they are
195 loaded. In order to be effective, this must be set before defining a
196 connection on this schema class or any derived object (as the loading
197 happens as soon as both a connection and loader_options are set, and
198 only once per class).
200 See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
201 details on the dumping mechanism.
203 This can also be set at module import time via the import option
204 C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
205 C</foo/bar> is the target directory.
209 # My::Schema isa DBIx::Class::Schema::Loader, and has connection info
210 # hardcoded in the class itself:
211 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1
213 # Same, but no hard-coded connection, so we must provide one:
214 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)'
216 # Or as a class method, as long as you get it done *before* defining a
217 # connection on this schema class or any derived object:
219 My::Schema->dump_to_dir('/foo/bar');
220 My::Schema->connection(........);
222 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
226 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
227 My::Schema->connection(.......);
228 My::OtherSchema->connection(.......);
230 # Another alternative to the above:
231 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
234 My::Schema->connection(.......);
235 My::OtherSchema->connection(.......);
242 foreach my $opt (@_) {
243 if($opt =~ m{^dump_to_dir:(.*)$}) {
244 $self->dump_to_dir($1)
246 elsif($opt eq 'make_schema_at') {
248 my $cpkg = (caller)[0];
249 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
254 =head2 make_schema_at
256 This simple function allows one to create a Loader-based schema
257 in-memory on the fly without any on-disk class files of any
258 kind. When used with the C<dump_directory> option, you can
259 use this to generate a rough draft manual schema from a dsn
260 without the intermediate step of creating a physical Loader-based
263 The return value is the input class name.
265 This function can be exported/imported by the normal means, as
266 illustrated in these Examples:
268 # Simple example, creates as a new class 'New::Schema::Name' in
269 # memory in the running perl interpreter.
270 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
274 [ 'dbi:Pg:dbname="foo"','postgres' ],
277 # Complex: dump loaded schema to disk, all from the commandline:
278 perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib -e 'make_schema_at("New::Schema::Name", { debug => 1 }, [ "dbi:Pg:dbname=foo","postgres" ])'
280 # Same, but inside a script, and using a different way to specify the
282 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
285 { debug => 1, dump_directory => './lib' },
286 [ 'dbi:Pg:dbname="foo"','postgres' ],
292 my ($target, $opts, $connect_info) = @_;
296 @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/;
299 $target->loader_options($opts);
300 $target->connection(@$connect_info);
305 Re-scans the database for newly added tables since the initial
306 load, and adds them to the schema at runtime, including relationships,
307 etc. Does not process drops or changes.
309 Returns a list of the new monikers added.
313 sub rescan { my $self = shift; $self->_loader->rescan($self) }
317 Using the example in L<DBIx::Class::Manual::ExampleSchema> as a basis
318 replace the DB::Main with the following code:
322 use base qw/DBIx::Class::Schema::Loader/;
324 __PACKAGE__->loader_options(
327 __PACKAGE__->connection('dbi:SQLite:example.db');
331 and remove the Main directory tree (optional). Every thing else
336 =head2 Multiple Database Schemas
338 Currently the loader is limited to working within a single schema
339 (using the database vendors' definition of "schema"). If you
340 have a multi-schema database with inter-schema relationships (which
341 is easy to do in PostgreSQL or DB2 for instance), you only get to
342 automatically load the tables of one schema, and any relationships
343 to tables in other schemas will be silently ignored.
345 At some point in the future, an intelligent way around this might be
346 devised, probably by allowing the C<db_schema> option to be an
347 arrayref of schemas to load.
349 In "normal" L<DBIx::Class::Schema> usage, manually-defined
350 source classes and relationships have no problems crossing vendor schemas.
354 Brandon Black, C<blblack@gmail.com>
356 Based on L<DBIx::Class::Loader> by Sebastian Riedel
358 Based upon the work of IKEBE Tomohiro
362 Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent
363 in a bug report or suggestion.
367 This library is free software; you can redistribute it and/or modify it under
368 the same terms as Perl itself.
372 L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>