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