1 package DBIx::Class::Schema::Loader;
5 use base qw/DBIx::Class::Schema/;
6 use base qw/Class::Data::Accessor/;
8 use UNIVERSAL::require;
10 use Scalar::Util qw/ weaken /;
12 # Always remember to do all digits for the version even if they're 0
13 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
14 # brain damage and presumably various other packaging systems too
15 our $VERSION = '0.03004';
17 __PACKAGE__->mk_classaccessor('dump_to_dir');
18 __PACKAGE__->mk_classaccessor('loader');
19 __PACKAGE__->mk_classaccessor('_loader_args');
23 DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
28 use base qw/DBIx::Class::Schema::Loader/;
30 __PACKAGE__->loader_options(
32 constraint => '^foo.*',
36 # in seperate application code ...
40 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
42 my $schema1 = "My::Schema"; $schema1->connection(as above);
46 DBIx::Class::Schema::Loader automates the definition of a
47 L<DBIx::Class::Schema> by scanning database table definitions and
48 setting up the columns and primary keys.
50 DBIx::Class::Schema::Loader currently supports DBI for MySQL,
51 PostgreSQL, SQLite and DB2.
53 See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
54 your own vendor-specific subclass for an unsupported DBD driver.
56 This module requires L<DBIx::Class> 0.06 or later, and obsoletes
57 the older L<DBIx::Class::Loader>.
59 This module is designed more to get you up and running quickly against
60 an existing database, or to be effective for simple situations, rather
61 than to be what you use in the long term for a complex database/project.
63 That being said, transitioning your code from a Schema generated by this
64 module to one that doesn't use this module should be straightforward and
65 painless (as long as you're not using any methods that are now deprecated
66 in this document), so don't shy away from it just for fears of the
67 transition down the road.
73 Example in Synopsis above demonstrates a few common arguments. For
74 detailed information on all of the arguments, most of which are
75 only useful in fairly complex scenarios, see the
76 L<DBIx::Class::Schema::Loader::Base> documentation.
78 This method is *required*, for backwards compatibility reasons. If
79 you do not wish to change any options, just call it with an empty
80 argument list during schema class initialization.
88 if(ref $_[0] eq 'HASH') {
95 my $class = ref $self || $self;
96 $args{schema} = $self;
97 $args{schema_class} = $class;
98 weaken($args{schema}) if ref $self;
100 $self->_loader_args(\%args);
101 $self->_invoke_loader if $self->storage && !$class->loader;
108 my $class = ref $self || $self;
110 $self->_loader_args->{dump_directory} ||= $self->dump_to_dir;
112 # XXX this only works for relative storage_type, like ::DBI ...
113 my $impl = "DBIx::Class::Schema::Loader" . $self->storage_type;
115 croak qq/Could not load storage_type loader "$impl": / .
116 qq/"$UNIVERSAL::require::ERROR"/;
118 # XXX in the future when we get rid of ->loader, the next two
119 # lines can be replaced by "$impl->new(%{$self->_loader_args})->load;"
120 $class->loader($impl->new(%{$self->_loader_args}));
121 $class->loader->load;
128 See L<DBIx::Class::Schema>.
133 my $self = shift->next::method(@_);
135 my $class = ref $self || $self;
136 $self->_invoke_loader if $self->_loader_args && !$class->loader;
143 See L<DBIx::Class::Schema>.
150 my $clone = $self->next::method(@_);
152 $clone->_loader_args($self->_loader_args);
153 $clone->_loader_args->{schema} = $clone;
154 weaken($clone->_loader_args->{schema});
161 Argument: directory name.
163 Calling this as a class method on either L<DBIx::Class::Schema::Loader>
164 or any derived schema class will cause all affected schemas to dump
165 manual versions of themselves to the named directory when they are
166 loaded. In order to be effective, this must be set before defining a
167 connection on this schema class or any derived object (as the loading
168 happens as soon as both a connection and loader_options are set, and
169 only once per class).
171 See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
172 details on the dumping mechanism.
174 This can also be set at module import time via the import option
175 C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
176 C</foo/bar> is the target directory.
180 # My::Schema isa DBIx::Class::Schema::Loader, and has connection info
181 # hardcoded in the class itself:
182 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1
184 # Same, but no hard-coded connection, so we must provide one:
185 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)'
187 # Or as a class method, as long as you get it done *before* defining a
188 # connection on this schema class or any derived object:
190 My::Schema->dump_to_dir('/foo/bar');
191 My::Schema->connection(........);
193 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
197 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
198 My::Schema->connection(.......);
199 My::OtherSchema->connection(.......);
201 # Another alternative to the above:
202 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
205 My::Schema->connection(.......);
206 My::OtherSchema->connection(.......);
213 foreach my $opt (@_) {
214 if($opt =~ m{^dump_to_dir:(.*)$}) {
215 $self->dump_to_dir($1)
217 elsif($opt eq 'make_schema_at') {
219 my $cpkg = (caller)[0];
220 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
225 =head2 make_schema_at
227 This simple function allows one to create a Loader-based schema
228 in-memory on the fly without any on-disk class files of any
229 kind. When used with the C<dump_directory> option, you can
230 use this to generate a rough draft manual schema from a dsn
231 without the intermediate step of creating a physical Loader-based
234 The return value is the input class name.
236 This function can be exported/imported by the normal means, as
237 illustrated in these Examples:
239 # Simple example, creates as a new class 'New::Schema::Name' in
240 # memory in the running perl interpreter.
241 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
244 { relationships => 1, debug => 1 },
245 [ 'dbi:Pg:dbname="foo"','postgres' ],
248 # Complex: dump loaded schema to disk, all from the commandline:
249 perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib -e 'make_schema_at("New::Schema::Name", { relationships => 1 }, [ "dbi:Pg:dbname=foo","postgres" ])'
251 # Same, but inside a script, and using a different way to specify the
253 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
256 { relationships => 1, debug => 1, dump_directory => './lib' },
257 [ 'dbi:Pg:dbname="foo"','postgres' ],
263 my ($target, $opts, $connect_info) = @_;
267 @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/;
270 $target->loader_options($opts);
271 $target->connection(@$connect_info);
276 Using the example in L<DBIx::Class::Manual::ExampleSchema> as a basis
277 replace the DB::Main with the following code:
281 use base qw/DBIx::Class::Schema::Loader/;
283 __PACKAGE__->loader_options(
287 __PACKAGE__->connection('dbi:SQLite:example.db');
291 and remove the Main directory tree (optional). Every thing else
294 =head1 DEPRECATED METHODS
296 You don't need to read anything in this section unless you're upgrading
297 code that was written against pre-0.03 versions of this module. This
298 version is intended to be backwards-compatible with pre-0.03 code, but
299 will issue warnings about your usage of deprecated features/methods.
301 =head2 load_from_connection
303 This deprecated method is now roughly an alias for L</loader_options>.
305 This method *will* disappear in a future version.
307 For now, using this method will invoke the legacy behavior for
308 backwards compatibility, and merely emit a warning about upgrading
311 It also reverts the default inflection scheme to
312 use L<Lingua::EN::Inflect> just like pre-0.03 versions of this
315 You can force these legacy inflections with the
316 option C<legacy_default_inflections>, even after switch over
317 to the preferred L</loader_options> way of doing things.
319 See the source of this method for more details.
323 sub load_from_connection {
324 my ($self, %args) = @_;
326 my $cmds_ver = $Catalyst::Model::DBIC::Schema::VERSION;
328 if($cmds_ver < 0.14) {
329 warn 'You should upgrade your installation of'
330 . ' Catalyst::Model::DBIC::Schema to 0.14 or higher, then:';
332 warn 'You should regenerate your Model files, which may eliminate'
333 . ' the following deprecation warning:';
335 warn 'load_from_connection deprecated, please [re-]read the'
336 . ' [new] DBIx::Class::Schema::Loader documentation';
338 # Support the old connect_info / dsn / etc args...
339 $args{connect_info} = [
342 delete $args{password},
343 delete $args{options},
346 $self->connection(@{delete $args{connect_info}})
347 if $args{connect_info};
349 $self->loader_options('legacy_default_inflections' => 1, %args);
354 This is an accessor in the generated Schema class for accessing
355 the L<DBIx::Class::Schema::Loader::Base> -based loader object
356 that was used during construction. See the
357 L<DBIx::Class::Schema::Loader::Base> docs for more information
358 on the available loader methods there.
360 This accessor is deprecated. Do not use it. Anything you can
361 get from C<loader>, you can get via the normal L<DBIx::Class::Schema>
362 methods, and your code will be more robust and forward-thinking
365 If you're already using C<loader> in your code, make an effort
366 to get rid of it. If you think you've found a situation where it
367 is necessary, let me know and we'll see what we can do to remedy
370 In some future version, this accessor *will* disappear. It was
371 apparently quite a design/API mistake to ever have exposed it to
372 user-land in the first place, all things considered.
376 =head2 Multiple Database Schemas
378 Currently the loader is limited to working within a single schema
379 (using the database vendors' definition of "schema"). If you
380 have a multi-schema database with inter-schema relationships (which
381 is easy to do in PostgreSQL or DB2 for instance), you only get to
382 automatically load the tables of one schema, and any relationships
383 to 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, or perhaps even offering schema
388 constraint/exclusion options just like the table ones.
390 In "normal" L<DBIx::Class::Schema> usage, manually-defined
391 source classes and relationships have no problems crossing vendor schemas.
395 Brandon Black, C<blblack@gmail.com>
397 Based on L<DBIx::Class::Loader> by Sebastian Riedel
399 Based upon the work of IKEBE Tomohiro
403 Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
404 Randal Schwartz, Simon Flack, Matt S Trout, everyone on #dbix-class, and
405 all the others who've helped.
409 This library is free software; you can redistribute it and/or modify it under
410 the same terms as Perl itself.
414 L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>