Version bump for release
[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;
f6148834 5use base qw/DBIx::Class::Schema Class::Data::Accessor/;
fa994d3c 6use Carp::Clan qw/^DBIx::Class/;
457eb8a6 7use UNIVERSAL::require;
996be9ee 8use Class::C3;
c9f1d7b0 9use Scalar::Util qw/ weaken /;
3980d69c 10
a4a19f3c 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
b327622b 14our $VERSION = '0.04999_05';
457eb8a6 15
59cfa251 16__PACKAGE__->mk_classaccessor('_loader_args' => {});
29ddb54c 17__PACKAGE__->mk_classaccessors(qw/dump_to_dir _loader_invoked _loader loader_class/);
a78e3fed 18
19=head1 NAME
20
18fca96a 21DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
a78e3fed 22
23=head1 SYNOPSIS
24
a4a19f3c 25 package My::Schema;
26 use base qw/DBIx::Class::Schema::Loader/;
a78e3fed 27
996be9ee 28 __PACKAGE__->loader_options(
996be9ee 29 constraint => '^foo.*',
30 # debug => 1,
a78e3fed 31 );
af6c2665 32
a4a19f3c 33 # in seperate application code ...
a78e3fed 34
a4a19f3c 35 use My::Schema;
a78e3fed 36
a4a19f3c 37 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
38 # -or-
996be9ee 39 my $schema1 = "My::Schema"; $schema1->connection(as above);
074e81cd 40
996be9ee 41=head1 DESCRIPTION
074e81cd 42
fbd83464 43DBIx::Class::Schema::Loader automates the definition of a
996be9ee 44L<DBIx::Class::Schema> by scanning database table definitions and
d65cda9e 45setting up the columns, primary keys, and relationships.
a78e3fed 46
d65cda9e 47DBIx::Class::Schema::Loader currently supports only the DBI storage type.
3fe9c5d9 48It has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>,
49L<DBD::SQLite>, and L<DBD::Oracle>. Other DBI drivers may function to
50a greater or lesser degree with this loader, depending on how much of the
51DBI spec they implement, and how standard their implementation is.
52
53Patches to make other DBDs work correctly welcome.
a78e3fed 54
996be9ee 55See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
56your own vendor-specific subclass for an unsupported DBD driver.
a78e3fed 57
3fe9c5d9 58This module requires L<DBIx::Class> 0.07006 or later, and obsoletes
996be9ee 59the older L<DBIx::Class::Loader>.
89ecd854 60
996be9ee 61This module is designed more to get you up and running quickly against
62an existing database, or to be effective for simple situations, rather
63than to be what you use in the long term for a complex database/project.
89ecd854 64
65That being said, transitioning your code from a Schema generated by this
66module to one that doesn't use this module should be straightforward and
59cfa251 67painless, so don't shy away from it just for fears of the transition down
68the road.
89ecd854 69
a78e3fed 70=head1 METHODS
71
29ddb54c 72=head2 loader_class
73
74Set the loader class to be instantiated when L</connection> is called.
75If the classname starts with "::", "DBIx::Class::Schema::Loader" is
76prepended. Defaults to L<DBIx::Class::Schema/storage_type> (which must
77start with "::" when using L<DBIx::Class::Schema::Loader>).
78
79This is mostly useful for subclassing existing loaders or in conjunction
80with L</dump_to_dir>.
81
996be9ee 82=head2 loader_options
a78e3fed 83
996be9ee 84Example in Synopsis above demonstrates a few common arguments. For
85detailed information on all of the arguments, most of which are
86only useful in fairly complex scenarios, see the
87L<DBIx::Class::Schema::Loader::Base> documentation.
a78e3fed 88
3fe9c5d9 89If you intend to use C<loader_options>, you must call
90C<loader_options> before any connection is made, or embed the
91C<loader_options> in the connection information itself as shown
92below. Setting C<loader_options> after the connection has
59cfa251 93already been made is useless.
a78e3fed 94
996be9ee 95=cut
1031d4f6 96
996be9ee 97sub loader_options {
98 my $self = shift;
99
d65cda9e 100 my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
996be9ee 101 $self->_loader_args(\%args);
996be9ee 102
103 $self;
104}
105
106sub _invoke_loader {
107 my $self = shift;
108 my $class = ref $self || $self;
109
59cfa251 110 my $args = $self->_loader_args;
111
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;
af6c2665 117
996be9ee 118 # XXX this only works for relative storage_type, like ::DBI ...
29ddb54c 119 my $impl = $self->loader_class
3953cbee 120 || "DBIx::Class::Schema::Loader" . $self->storage_type;
29ddb54c 121 $impl = "DBIx::Class::Schema::Loader${impl}" if $impl =~ /^::/;
a78e3fed 122 $impl->require or
996be9ee 123 croak qq/Could not load storage_type loader "$impl": / .
3385ac62 124 qq/"$UNIVERSAL::require::ERROR"/;
af6c2665 125
b97c2c1e 126 $self->_loader($impl->new(%$args));
127 $self->_loader->load;
59cfa251 128 $self->_loader_invoked(1);
996be9ee 129
996be9ee 130 $self;
131}
132
133=head2 connection
134
d65cda9e 135See L<DBIx::Class::Schema> for basic usage.
136
29ddb54c 137If the final argument is a hashref, and it contains the keys C<loader_options>
138or C<loader_class>, those keys will be deleted, and their values value will be
139used for the loader options or class, respectively, just as if set via the
140L</loader_options> or L</loader_class> methods above.
d65cda9e 141
142The actual auto-loading operation (the heart of this module) will be invoked
143as soon as the connection information is defined.
996be9ee 144
145=cut
146
147sub connection {
d65cda9e 148 my $self = shift;
149
150 if($_[-1] && ref $_[-1] eq 'HASH') {
29ddb54c 151 for my $option (qw/ loader_class loader_options /) {
152 if(my $value = delete $_[-1]->{$option}) {
153 $self->$option($value);
154 }
d65cda9e 155 }
29ddb54c 156 pop @_ if !keys %{$_[-1]};
d65cda9e 157 }
158
159 $self = $self->next::method(@_);
996be9ee 160
161 my $class = ref $self || $self;
59cfa251 162 if(!$class->_loader_invoked) {
fa994d3c 163 $self->_invoke_loader
164 }
996be9ee 165
166 return $self;
167}
168
169=head2 clone
170
074e81cd 171See L<DBIx::Class::Schema>.
996be9ee 172
173=cut
174
175sub clone {
176 my $self = shift;
177
178 my $clone = $self->next::method(@_);
179
fa994d3c 180 if($clone->_loader_args) {
181 $clone->_loader_args->{schema} = $clone;
182 weaken($clone->_loader_args->{schema});
183 }
996be9ee 184
185 $clone;
186}
187
188=head2 dump_to_dir
189
190Argument: directory name.
191
192Calling this as a class method on either L<DBIx::Class::Schema::Loader>
193or any derived schema class will cause all affected schemas to dump
194manual versions of themselves to the named directory when they are
195loaded. In order to be effective, this must be set before defining a
196connection on this schema class or any derived object (as the loading
074e81cd 197happens as soon as both a connection and loader_options are set, and
198only once per class).
996be9ee 199
200See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
201details on the dumping mechanism.
202
203This can also be set at module import time via the import option
204C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
205C</foo/bar> is the target directory.
206
207Examples:
208
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
212
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", ...)'
215
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:
218 use My::Schema;
219 My::Schema->dump_to_dir('/foo/bar');
220 My::Schema->connection(........);
221
222 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
223 # derived schemas
224 use My::Schema;
225 use My::OtherSchema;
226 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
227 My::Schema->connection(.......);
228 My::OtherSchema->connection(.......);
229
230 # Another alternative to the above:
231 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
232 use My::Schema;
233 use My::OtherSchema;
234 My::Schema->connection(.......);
235 My::OtherSchema->connection(.......);
236
237=cut
238
239sub import {
240 my $self = shift;
241 return if !@_;
242 foreach my $opt (@_) {
243 if($opt =~ m{^dump_to_dir:(.*)$}) {
244 $self->dump_to_dir($1)
245 }
246 elsif($opt eq 'make_schema_at') {
247 no strict 'refs';
248 my $cpkg = (caller)[0];
249 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
250 }
251 }
252}
253
254=head2 make_schema_at
255
256This simple function allows one to create a Loader-based schema
257in-memory on the fly without any on-disk class files of any
258kind. When used with the C<dump_directory> option, you can
8f9d7ce5 259use this to generate a rough draft manual schema from a dsn
996be9ee 260without the intermediate step of creating a physical Loader-based
261schema class.
262
483987b9 263The return value is the input class name.
264
996be9ee 265This function can be exported/imported by the normal means, as
266illustrated in these Examples:
267
5223f24a 268 # Simple example, creates as a new class 'New::Schema::Name' in
269 # memory in the running perl interpreter.
996be9ee 270 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
271 make_schema_at(
272 'New::Schema::Name',
59cfa251 273 { debug => 1 },
996be9ee 274 [ 'dbi:Pg:dbname="foo"','postgres' ],
275 );
276
277 # Complex: dump loaded schema to disk, all from the commandline:
59cfa251 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" ])'
996be9ee 279
280 # Same, but inside a script, and using a different way to specify the
281 # dump directory:
282 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
283 make_schema_at(
284 'New::Schema::Name',
59cfa251 285 { debug => 1, dump_directory => './lib' },
996be9ee 286 [ 'dbi:Pg:dbname="foo"','postgres' ],
287 );
288
289=cut
290
291sub make_schema_at {
292 my ($target, $opts, $connect_info) = @_;
293
483987b9 294 {
295 no strict 'refs';
296 @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/;
297 }
298
299 $target->loader_options($opts);
300 $target->connection(@$connect_info);
996be9ee 301}
302
b97c2c1e 303=head2 rescan
304
305Re-scans the database for newly added tables since the initial
306load, and adds them to the schema at runtime, including relationships,
307etc. Does not process drops or changes.
308
a60b5b8d 309Returns a list of the new monikers added.
310
b97c2c1e 311=cut
312
a60b5b8d 313sub rescan { my $self = shift; $self->_loader->rescan($self) }
b97c2c1e 314
996be9ee 315=head1 EXAMPLE
316
317Using the example in L<DBIx::Class::Manual::ExampleSchema> as a basis
318replace the DB::Main with the following code:
319
320 package DB::Main;
321
322 use base qw/DBIx::Class::Schema::Loader/;
323
324 __PACKAGE__->loader_options(
996be9ee 325 debug => 1,
326 );
327 __PACKAGE__->connection('dbi:SQLite:example.db');
328
329 1;
330
331and remove the Main directory tree (optional). Every thing else
332should work the same
333
996be9ee 334=head1 KNOWN ISSUES
335
336=head2 Multiple Database Schemas
337
338Currently the loader is limited to working within a single schema
339(using the database vendors' definition of "schema"). If you
340have a multi-schema database with inter-schema relationships (which
8f9d7ce5 341is easy to do in PostgreSQL or DB2 for instance), you only get to
996be9ee 342automatically load the tables of one schema, and any relationships
343to tables in other schemas will be silently ignored.
344
345At some point in the future, an intelligent way around this might be
346devised, probably by allowing the C<db_schema> option to be an
d65cda9e 347arrayref of schemas to load.
89ecd854 348
996be9ee 349In "normal" L<DBIx::Class::Schema> usage, manually-defined
350source classes and relationships have no problems crossing vendor schemas.
89ecd854 351
a78e3fed 352=head1 AUTHOR
353
f654c972 354Brandon Black, C<blblack@gmail.com>
fbd83464 355
8a6b44ef 356Based on L<DBIx::Class::Loader> by Sebastian Riedel
a78e3fed 357
358Based upon the work of IKEBE Tomohiro
359
360=head1 THANK YOU
361
d65cda9e 362Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent
363in a bug report or suggestion.
a78e3fed 364
365=head1 LICENSE
366
367This library is free software; you can redistribute it and/or modify it under
368the same terms as Perl itself.
369
370=head1 SEE ALSO
371
996be9ee 372L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>
a78e3fed 373
374=cut
375
3761;