version bump to 0.03004
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader.pm
CommitLineData
18fca96a 1package DBIx::Class::Schema::Loader;
a78e3fed 2
3use strict;
a4a19f3c 4use warnings;
8a6b44ef 5use base qw/DBIx::Class::Schema/;
6use base qw/Class::Data::Accessor/;
457eb8a6 7use Carp;
8use UNIVERSAL::require;
996be9ee 9use Class::C3;
10use Data::Dump qw/ dump /;
c9f1d7b0 11use Scalar::Util qw/ weaken /;
3980d69c 12
a4a19f3c 13# Always remember to do all digits for the version even if they're 0
14# i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
15# brain damage and presumably various other packaging systems too
a13b2803 16our $VERSION = '0.03004';
457eb8a6 17
996be9ee 18__PACKAGE__->mk_classaccessor('dump_to_dir');
457eb8a6 19__PACKAGE__->mk_classaccessor('loader');
996be9ee 20__PACKAGE__->mk_classaccessor('_loader_args');
a78e3fed 21
22=head1 NAME
23
18fca96a 24DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
a78e3fed 25
26=head1 SYNOPSIS
27
a4a19f3c 28 package My::Schema;
29 use base qw/DBIx::Class::Schema::Loader/;
a78e3fed 30
996be9ee 31 __PACKAGE__->loader_options(
32 relationships => 1,
33 constraint => '^foo.*',
34 # debug => 1,
a78e3fed 35 );
af6c2665 36
a4a19f3c 37 # in seperate application code ...
a78e3fed 38
a4a19f3c 39 use My::Schema;
a78e3fed 40
a4a19f3c 41 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
42 # -or-
996be9ee 43 my $schema1 = "My::Schema"; $schema1->connection(as above);
074e81cd 44
996be9ee 45=head1 DESCRIPTION
074e81cd 46
fbd83464 47DBIx::Class::Schema::Loader automates the definition of a
996be9ee 48L<DBIx::Class::Schema> by scanning database table definitions and
49setting up the columns and primary keys.
a78e3fed 50
996be9ee 51DBIx::Class::Schema::Loader currently supports DBI for MySQL,
8f9d7ce5 52PostgreSQL, SQLite and DB2.
a78e3fed 53
996be9ee 54See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
55your own vendor-specific subclass for an unsupported DBD driver.
a78e3fed 56
996be9ee 57This module requires L<DBIx::Class> 0.06 or later, and obsoletes
58the older L<DBIx::Class::Loader>.
89ecd854 59
996be9ee 60This module is designed more to get you up and running quickly against
61an existing database, or to be effective for simple situations, rather
62than to be what you use in the long term for a complex database/project.
89ecd854 63
64That being said, transitioning your code from a Schema generated by this
65module to one that doesn't use this module should be straightforward and
996be9ee 66painless (as long as you're not using any methods that are now deprecated
67in this document), so don't shy away from it just for fears of the
68transition down the road.
89ecd854 69
a78e3fed 70=head1 METHODS
71
996be9ee 72=head2 loader_options
a78e3fed 73
996be9ee 74Example in Synopsis above demonstrates a few common arguments. For
75detailed information on all of the arguments, most of which are
76only useful in fairly complex scenarios, see the
77L<DBIx::Class::Schema::Loader::Base> documentation.
a78e3fed 78
996be9ee 79This method is *required*, for backwards compatibility reasons. If
80you do not wish to change any options, just call it with an empty
81argument list during schema class initialization.
a78e3fed 82
996be9ee 83=cut
1031d4f6 84
996be9ee 85sub loader_options {
86 my $self = shift;
87
88 my %args;
89 if(ref $_[0] eq 'HASH') {
90 %args = %{$_[0]};
1031d4f6 91 }
92 else {
996be9ee 93 %args = @_;
1031d4f6 94 }
95
996be9ee 96 my $class = ref $self || $self;
97 $args{schema} = $self;
98 $args{schema_class} = $class;
c9f1d7b0 99 weaken($args{schema}) if ref $self;
100
996be9ee 101 $self->_loader_args(\%args);
102 $self->_invoke_loader if $self->storage && !$class->loader;
103
104 $self;
105}
106
107sub _invoke_loader {
108 my $self = shift;
109 my $class = ref $self || $self;
110
111 $self->_loader_args->{dump_directory} ||= $self->dump_to_dir;
af6c2665 112
996be9ee 113 # XXX this only works for relative storage_type, like ::DBI ...
114 my $impl = "DBIx::Class::Schema::Loader" . $self->storage_type;
a78e3fed 115 $impl->require or
996be9ee 116 croak qq/Could not load storage_type loader "$impl": / .
3385ac62 117 qq/"$UNIVERSAL::require::ERROR"/;
af6c2665 118
996be9ee 119 # XXX in the future when we get rid of ->loader, the next two
120 # lines can be replaced by "$impl->new(%{$self->_loader_args})->load;"
121 $class->loader($impl->new(%{$self->_loader_args}));
2a4b8262 122 $class->loader->load;
996be9ee 123
996be9ee 124 $self;
125}
126
127=head2 connection
128
074e81cd 129See L<DBIx::Class::Schema>.
996be9ee 130
131=cut
132
133sub connection {
134 my $self = shift->next::method(@_);
135
136 my $class = ref $self || $self;
137 $self->_invoke_loader if $self->_loader_args && !$class->loader;
138
139 return $self;
140}
141
142=head2 clone
143
074e81cd 144See L<DBIx::Class::Schema>.
996be9ee 145
146=cut
147
148sub clone {
149 my $self = shift;
150
151 my $clone = $self->next::method(@_);
152
153 $clone->_loader_args($self->_loader_args);
154 $clone->_loader_args->{schema} = $clone;
c9f1d7b0 155 weaken($clone->_loader_args->{schema});
996be9ee 156
157 $clone;
158}
159
160=head2 dump_to_dir
161
162Argument: directory name.
163
164Calling this as a class method on either L<DBIx::Class::Schema::Loader>
165or any derived schema class will cause all affected schemas to dump
166manual versions of themselves to the named directory when they are
167loaded. In order to be effective, this must be set before defining a
168connection on this schema class or any derived object (as the loading
074e81cd 169happens as soon as both a connection and loader_options are set, and
170only once per class).
996be9ee 171
172See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
173details on the dumping mechanism.
174
175This can also be set at module import time via the import option
176C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
177C</foo/bar> is the target directory.
178
179Examples:
180
181 # My::Schema isa DBIx::Class::Schema::Loader, and has connection info
182 # hardcoded in the class itself:
183 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1
184
185 # Same, but no hard-coded connection, so we must provide one:
186 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)'
187
188 # Or as a class method, as long as you get it done *before* defining a
189 # connection on this schema class or any derived object:
190 use My::Schema;
191 My::Schema->dump_to_dir('/foo/bar');
192 My::Schema->connection(........);
193
194 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
195 # derived schemas
196 use My::Schema;
197 use My::OtherSchema;
198 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
199 My::Schema->connection(.......);
200 My::OtherSchema->connection(.......);
201
202 # Another alternative to the above:
203 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
204 use My::Schema;
205 use My::OtherSchema;
206 My::Schema->connection(.......);
207 My::OtherSchema->connection(.......);
208
209=cut
210
211sub import {
212 my $self = shift;
213 return if !@_;
214 foreach my $opt (@_) {
215 if($opt =~ m{^dump_to_dir:(.*)$}) {
216 $self->dump_to_dir($1)
217 }
218 elsif($opt eq 'make_schema_at') {
219 no strict 'refs';
220 my $cpkg = (caller)[0];
221 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
222 }
223 }
224}
225
226=head2 make_schema_at
227
228This simple function allows one to create a Loader-based schema
229in-memory on the fly without any on-disk class files of any
230kind. When used with the C<dump_directory> option, you can
8f9d7ce5 231use this to generate a rough draft manual schema from a dsn
996be9ee 232without the intermediate step of creating a physical Loader-based
233schema class.
234
235This function can be exported/imported by the normal means, as
236illustrated in these Examples:
237
5223f24a 238 # Simple example, creates as a new class 'New::Schema::Name' in
239 # memory in the running perl interpreter.
996be9ee 240 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
241 make_schema_at(
242 'New::Schema::Name',
243 { relationships => 1, debug => 1 },
244 [ 'dbi:Pg:dbname="foo"','postgres' ],
245 );
246
247 # Complex: dump loaded schema to disk, all from the commandline:
5223f24a 248 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" ])'
996be9ee 249
250 # Same, but inside a script, and using a different way to specify the
251 # dump directory:
252 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
253 make_schema_at(
254 'New::Schema::Name',
255 { relationships => 1, debug => 1, dump_directory => './lib' },
256 [ 'dbi:Pg:dbname="foo"','postgres' ],
257 );
258
259=cut
260
261sub make_schema_at {
262 my ($target, $opts, $connect_info) = @_;
263
264 my $opts_dumped = dump($opts);
265 my $cinfo_dumped = dump(@$connect_info);
266 eval qq|
267 package $target;
268 use base qw/DBIx::Class::Schema::Loader/;
269 __PACKAGE__->loader_options($opts_dumped);
270 __PACKAGE__->connection($cinfo_dumped);
271 |;
6b707971 272 croak "make_schema_at failed: $@" if $@;
996be9ee 273}
274
275=head1 EXAMPLE
276
277Using the example in L<DBIx::Class::Manual::ExampleSchema> as a basis
278replace the DB::Main with the following code:
279
280 package DB::Main;
281
282 use base qw/DBIx::Class::Schema::Loader/;
283
284 __PACKAGE__->loader_options(
285 relationships => 1,
286 debug => 1,
287 );
288 __PACKAGE__->connection('dbi:SQLite:example.db');
289
290 1;
291
292and remove the Main directory tree (optional). Every thing else
293should work the same
294
295=head1 DEPRECATED METHODS
296
297You don't need to read anything in this section unless you're upgrading
298code that was written against pre-0.03 versions of this module. This
299version is intended to be backwards-compatible with pre-0.03 code, but
300will issue warnings about your usage of deprecated features/methods.
301
302=head2 load_from_connection
303
304This deprecated method is now roughly an alias for L</loader_options>.
305
8f9d7ce5 306This method *will* disappear in a future version.
996be9ee 307
308For now, using this method will invoke the legacy behavior for
309backwards compatibility, and merely emit a warning about upgrading
310your code.
311
312It also reverts the default inflection scheme to
313use L<Lingua::EN::Inflect> just like pre-0.03 versions of this
314module did.
315
316You can force these legacy inflections with the
317option C<legacy_default_inflections>, even after switch over
318to the preferred L</loader_options> way of doing things.
319
320See the source of this method for more details.
321
322=cut
323
324sub load_from_connection {
325 my ($self, %args) = @_;
8f9d7ce5 326
327 my $cmds_ver = $Catalyst::Model::DBIC::Schema::VERSION;
328 if($cmds_ver) {
329 if($cmds_ver < 0.14) {
330 warn 'You should upgrade your installation of'
331 . ' Catalyst::Model::DBIC::Schema to 0.14 or higher, then:';
332 }
333 warn 'You should regenerate your Model files, which may eliminate'
334 . ' the following deprecation warning:';
335 }
996be9ee 336 warn 'load_from_connection deprecated, please [re-]read the'
8f9d7ce5 337 . ' [new] DBIx::Class::Schema::Loader documentation';
996be9ee 338
339 # Support the old connect_info / dsn / etc args...
340 $args{connect_info} = [
341 delete $args{dsn},
342 delete $args{user},
343 delete $args{password},
344 delete $args{options},
345 ] if $args{dsn};
346
347 $self->connection(@{delete $args{connect_info}})
348 if $args{connect_info};
349
350 $self->loader_options('legacy_default_inflections' => 1, %args);
a78e3fed 351}
352
457eb8a6 353=head2 loader
354
355This is an accessor in the generated Schema class for accessing
996be9ee 356the L<DBIx::Class::Schema::Loader::Base> -based loader object
457eb8a6 357that was used during construction. See the
996be9ee 358L<DBIx::Class::Schema::Loader::Base> docs for more information
457eb8a6 359on the available loader methods there.
360
996be9ee 361This accessor is deprecated. Do not use it. Anything you can
362get from C<loader>, you can get via the normal L<DBIx::Class::Schema>
363methods, and your code will be more robust and forward-thinking
364for doing so.
365
366If you're already using C<loader> in your code, make an effort
367to get rid of it. If you think you've found a situation where it
8f9d7ce5 368is necessary, let me know and we'll see what we can do to remedy
996be9ee 369that situation.
370
371In some future version, this accessor *will* disappear. It was
372apparently quite a design/API mistake to ever have exposed it to
373user-land in the first place, all things considered.
374
375=head1 KNOWN ISSUES
376
377=head2 Multiple Database Schemas
378
379Currently the loader is limited to working within a single schema
380(using the database vendors' definition of "schema"). If you
381have a multi-schema database with inter-schema relationships (which
8f9d7ce5 382is easy to do in PostgreSQL or DB2 for instance), you only get to
996be9ee 383automatically load the tables of one schema, and any relationships
384to tables in other schemas will be silently ignored.
385
386At some point in the future, an intelligent way around this might be
387devised, probably by allowing the C<db_schema> option to be an
388arrayref of schemas to load, or perhaps even offering schema
389constraint/exclusion options just like the table ones.
89ecd854 390
996be9ee 391In "normal" L<DBIx::Class::Schema> usage, manually-defined
392source classes and relationships have no problems crossing vendor schemas.
89ecd854 393
a78e3fed 394=head1 AUTHOR
395
f654c972 396Brandon Black, C<blblack@gmail.com>
fbd83464 397
8a6b44ef 398Based on L<DBIx::Class::Loader> by Sebastian Riedel
a78e3fed 399
400Based upon the work of IKEBE Tomohiro
401
402=head1 THANK YOU
403
404Adam Anderson, Andy Grundman, Autrijus Tang, Dan Kubb, David Naughton,
996be9ee 405Randal Schwartz, Simon Flack, Matt S Trout, everyone on #dbix-class, and
406all the others who've helped.
a78e3fed 407
408=head1 LICENSE
409
410This library is free software; you can redistribute it and/or modify it under
411the same terms as Perl itself.
412
413=head1 SEE ALSO
414
996be9ee 415L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>
a78e3fed 416
417=cut
418
4191;