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