Fixed up PK::Auto::* to use result_source, added connection, connect and clone method...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema.pm
CommitLineData
a02675cd 1package DBIx::Class::Schema;
2
3use strict;
4use warnings;
66d9ef6b 5use UNIVERSAL::require;
a02675cd 6
41a6f8c0 7use base qw/DBIx::Class/;
a02675cd 8
42a1aaa1 9__PACKAGE__->load_components(qw/Exception/);
0dc79249 10__PACKAGE__->mk_classdata('class_mappings' => {});
11__PACKAGE__->mk_classdata('source_registrations' => {});
d7156e50 12__PACKAGE__->mk_classdata('storage_type' => 'DBI');
13__PACKAGE__->mk_classdata('storage');
a02675cd 14
c2da098a 15=head1 NAME
16
17DBIx::Class::Schema - composable schemas
18
19=head1 SYNOPSIS
20
03312470 21in My/Schema.pm
c2da098a 22
23 package My::Schema;
24
25 use base qw/DBIx::Class::Schema/;
26
27 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
28
03312470 29in My/Schema/Foo.pm
c2da098a 30
31 package My::Schema::Foo;
32
03312470 33 use base qw/DBIx::Class/;
c2da098a 34
54540863 35 __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example
c2da098a 36 __PACKAGE__->table('foo');
37 ...
38
03312470 39in My/DB.pm
c2da098a 40
41 use My::Schema;
42
43 My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
44
03312470 45then in app code
c2da098a 46
656796f2 47 my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
c2da098a 48
49=head1 DESCRIPTION
50
429bd4f1 51Creates database classes based on a schema. This allows you to have more than
52one concurrent connection using the same database classes, by making
53subclasses under a new namespace for each connection. If you only need one
54class, you should probably use L<DBIx::Class::DB> directly instead.
55
03312470 56NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS>
57carefully as DBIx::Class does things a little differently. Note in
58particular which module inherits off which.
59
c2da098a 60=head1 METHODS
61
0dc79249 62=head2 register_class <moniker> <component_class>
076652e8 63
66d9ef6b 64Registers a class which isa ResultSourceInstance; equivalent to calling
65
66 $schema->register_source($moniker, $class->result_source_instance);
076652e8 67
c2da098a 68=cut
69
a02675cd 70sub register_class {
0dc79249 71 my ($self, $moniker, $to_register) = @_;
72 $self->register_source($moniker => $to_register->result_source_instance);
74b92d9a 73}
74
0dc79249 75=head2 register_source <moniker> <result source>
076652e8 76
0dc79249 77Registers the result source in the schema with the given moniker
076652e8 78
79=cut
80
0dc79249 81sub register_source {
82 my ($self, $moniker, $source) = @_;
83 my %reg = %{$self->source_registrations};
84 $reg{$moniker} = $source;
85 $self->source_registrations(\%reg);
86 $source->schema($self);
87 if ($source->result_class) {
88 my %map = %{$self->class_mappings};
89 $map{$source->result_class} = $moniker;
90 $self->class_mappings(\%map);
91 }
92}
a02675cd 93
bfb2bd4f 94=head2 class
95
96 my $class = $schema->class('Foo');
97
0dc79249 98Retrieves the result class name for a given result source
bfb2bd4f 99
100=cut
101
102sub class {
0dc79249 103 my ($self, $moniker) = @_;
104 return $self->source($moniker)->result_class;
bfb2bd4f 105}
106
ea20d0fd 107=head2 source
108
109 my $source = $schema->source('Foo');
110
111Returns the result source object for the registered name
112
113=cut
114
115sub source {
0dc79249 116 my ($self, $moniker) = @_;
117 my $sreg = $self->source_registrations;
118 return $sreg->{$moniker} if exists $sreg->{$moniker};
119
120 # if we got here, they probably passed a full class name
121 my $mapped = $self->class_mappings->{$moniker};
122 die "Can't find source for ${moniker}"
123 unless $mapped && exists $sreg->{$mapped};
124 return $sreg->{$mapped};
ea20d0fd 125}
126
0dc79249 127=head2 sources
128
129 my @source_monikers = $schema->sources;
130
131Returns the source monikers of all source registrations on this schema
132
133=cut
134
135sub sources { return keys %{shift->source_registrations}; }
136
ea20d0fd 137=head2 resultset
138
139 my $rs = $schema->resultset('Foo');
140
0dc79249 141Returns the resultset for the registered moniker
ea20d0fd 142
143=cut
144
145sub resultset {
0dc79249 146 my ($self, $moniker) = @_;
147 return $self->source($moniker)->resultset;
ea20d0fd 148}
149
130c6439 150=head2 load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
076652e8 151
429bd4f1 152Uses L<Module::Find> to find all classes under the database class' namespace,
153or uses the classes you select. Then it loads the component (using L<use>),
154and registers them (using B<register_class>);
076652e8 155
5ce32fc1 156It is possible to comment out classes with a leading '#', but note that perl
157will think it's a mistake (trying to use a comment in a qw list) so you'll
158need to add "no warnings 'qw';" before your load_classes call.
159
076652e8 160=cut
161
a02675cd 162sub load_classes {
5ce32fc1 163 my ($class, @params) = @_;
164
165 my %comps_for;
166
167 if (@params) {
168 foreach my $param (@params) {
169 if (ref $param eq 'ARRAY') {
170 # filter out commented entries
171 my @modules = grep { $_ !~ /^#/ } @$param;
172
173 push (@{$comps_for{$class}}, @modules);
174 }
175 elsif (ref $param eq 'HASH') {
176 # more than one namespace possible
177 for my $comp ( keys %$param ) {
178 # filter out commented entries
179 my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}};
180
181 push (@{$comps_for{$comp}}, @modules);
182 }
183 }
184 else {
185 # filter out commented entries
186 push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
187 }
188 }
189 } else {
41a6f8c0 190 eval "require Module::Find;";
191 $class->throw("No arguments to load_classes and couldn't load".
192 " Module::Find ($@)") if $@;
5ce32fc1 193 my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class);
194 $comps_for{$class} = \@comp;
41a6f8c0 195 }
5ce32fc1 196
197 foreach my $prefix (keys %comps_for) {
198 foreach my $comp (@{$comps_for{$prefix}||[]}) {
199 my $comp_class = "${prefix}::${comp}";
5ce32fc1 200 eval "use $comp_class"; # If it fails, assume the user fixed it
bfb2bd4f 201 if ($@) {
202 die $@ unless $@ =~ /Can't locate/;
203 }
5ce32fc1 204 $class->register_class($comp => $comp_class);
205 }
a02675cd 206 }
207}
208
130c6439 209=head2 compose_connection <target> <@db_info>
429bd4f1 210
211This is the most important method in this class. it takes a target namespace,
212as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
213well as subclasses for each of your database classes in this namespace, using
214this connection.
076652e8 215
54540863 216It will also setup a ->class method on the target class, which lets you
8c130052 217resolve database classes based on the schema component name, for example
218
54540863 219 MyApp::DB->class('Foo') # returns MyApp::DB::Foo,
8c130052 220 # which ISA MyApp::Schema::Foo
221
222This is the recommended API for accessing Schema generated classes, and
223using it might give you instant advantages with future versions of DBIC.
224
54540863 225WARNING: Loading components into Schema classes after compose_connection
226may not cause them to be seen by the classes in your target namespace due
227to the dispatch table approach used by Class::C3. If you do this you may find
228you need to call Class::C3->reinitialize() afterwards to get the behaviour
229you expect.
230
076652e8 231=cut
232
a02675cd 233sub compose_connection {
ea20d0fd 234 my ($self, $target, @info) = @_;
66d9ef6b 235 my $base = 'DBIx::Class::ResultSetInstance';
236 $base->require;
237 my $schema = $self->compose_namespace($target, $base);
238 $schema->connection(@info);
0dc79249 239 foreach my $moniker ($schema->sources) {
240 my $source = $schema->source($moniker);
241 my $class = $source->result_class;
242 #warn "$moniker $class $source ".$source->storage;
8c49f629 243 $class->mk_classdata(result_source_instance => $source);
ea20d0fd 244 $class->mk_classdata(resultset_instance => $source->resultset);
66d9ef6b 245 $class->mk_classdata(class_resolver => $schema);
bfb2bd4f 246 }
247 return $schema;
e678398e 248}
249
250sub compose_namespace {
66d9ef6b 251 my ($self, $target, $base) = @_;
252 my %reg = %{ $self->source_registrations };
11b78bd6 253 my %target;
254 my %map;
66d9ef6b 255 my $schema = $self->clone;
256 foreach my $moniker ($schema->sources) {
257 my $source = $schema->source($moniker);
0dc79249 258 my $target_class = "${target}::${moniker}";
66d9ef6b 259 $self->inject_base(
0dc79249 260 $target_class => $source->result_class, ($base ? $base : ())
261 );
66d9ef6b 262 $source->result_class($target_class);
b7951443 263 }
11b78bd6 264 {
265 no strict 'refs';
bfb2bd4f 266 *{"${target}::schema"} =
267 sub { $schema };
1edaf6fe 268 foreach my $meth (qw/class source resultset/) {
269 *{"${target}::${meth}"} =
270 sub { shift->schema->$meth(@_) };
271 }
11b78bd6 272 }
bfb2bd4f 273 return $schema;
b7951443 274}
275
130c6439 276=head2 setup_connection_class <$target> <@info>
076652e8 277
429bd4f1 278Sets up a database connection class to inject between the schema
279and the subclasses the schema creates.
280
076652e8 281=cut
282
b7951443 283sub setup_connection_class {
284 my ($class, $target, @info) = @_;
63e9583a 285 $class->inject_base($target => 'DBIx::Class::DB');
286 #$target->load_components('DB');
b7951443 287 $target->connection(@info);
288}
289
66d9ef6b 290=head2 connection(@args)
291
292Instantiates a new Storage object of type storage_type and passes the
293arguments to $storage->connection_info. Sets the connection in-place on
294the schema.
295
296=cut
297
298sub connection {
299 my ($self, @info) = @_;
300 my $storage_class = 'DBIx::Class::Storage::'.$self->storage_type;
301 $storage_class->require;
302 my $storage = $storage_class->new;
303 $storage->connect_info(\@info);
304 $self->storage($storage);
305 return $self;
306}
307
308=head2 connect(@info)
309
310Conveneience method, equivalent to $schema->clone->connection(@info)
311
312=cut
313
314sub connect { shift->clone->connection(@_) };
315
316=head2 clone
317
318Clones the schema and its associated result_source objects and returns the
319copy.
320
321=cut
322
323sub clone {
324 my ($self) = @_;
325 my $clone = bless({ (ref $self ? %$self : ()) }, ref $self || $self);
326 foreach my $moniker ($self->sources) {
327 my $source = $self->source($moniker);
328 my $new = $source->new($source);
329 $clone->register_source($moniker => $new);
330 }
331 return $clone;
332}
333
a02675cd 3341;
c2da098a 335
c2da098a 336=head1 AUTHORS
337
daec44b8 338Matt S. Trout <mst@shadowcatsystems.co.uk>
c2da098a 339
340=head1 LICENSE
341
342You may distribute this code under the same terms as Perl itself.
343
344=cut
345